1 /* 2 * Generate a VGA signal using a PIC32 microcontroller. 3 * 4 * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 21 #include "pic32_c.h" 22 #include "init.h" 23 #include "debug.h" 24 25 /* Specific functionality. */ 26 27 #include "devconfig.h" 28 #include "display.h" 29 #include "display_config.h" 30 #include "font.h" 31 #include "main.h" 32 #include "vga.h" 33 #include "vga_display.h" 34 35 36 37 /* Define DMA channels if not indicated in the build configuration. */ 38 39 /* CPU-based transfers: no channels. */ 40 41 #ifdef TRANSFER_CPU 42 #define LINE_CHANNELS 0 43 #define SCROLL_XSTEP 1 44 45 /* DMA-based transfers: single channel by default. */ 46 47 #else 48 49 #ifndef LINE_CHANNELS 50 #define LINE_CHANNELS 1 51 #endif 52 53 #define SCROLL_XSTEP LINE_CHANNELS 54 #endif 55 56 57 58 /* Define timers if not indicated in the build configuration. */ 59 60 #ifndef LINE_TIMER 61 #define LINE_TIMER 2 62 #endif 63 64 #ifndef TRANSFER_TIMER 65 #define TRANSFER_TIMER 0 66 #endif 67 68 /* Define different output ports and pins for parallel mode. */ 69 70 #ifndef PARALLEL_MODE 71 #define VGA_OUTPUT PORTB 72 #define OC1_PIN RPA0R 73 #define OC2_PIN RPA1R 74 #define LED_PIN (1 << 3) 75 #else 76 #define VGA_OUTPUT PM_REG(0, PMxDIN) 77 #define OC1_PIN RPB4R 78 #define OC2_PIN RPB5R 79 #define LED_PIN (1 << 2) 80 #endif 81 82 83 84 /* Define the relationship between source images and the screen. */ 85 86 #ifndef SOURCE_HEIGHT 87 #define SOURCE_HEIGHT 256 88 #endif 89 90 #define SOURCE_YSTEP (SOURCE_HEIGHT / LINE_COUNT) 91 92 93 94 /* Framebuffer memory. */ 95 96 static uint8_t framebuffer[FRAME_SIZE * FRAME_COUNT]; 97 98 99 100 /* Bundled image and font data. */ 101 102 extern uint8_t screendata[]; 103 extern uint32_t screendata_width, screendata_height; 104 extern uint8_t sprite[]; 105 extern uint32_t sprite_width, sprite_height; 106 107 extern uint8_t fontchars[]; 108 extern uint32_t fonttable[]; 109 extern uint32_t fontbase, fontlimit; 110 111 static font_config_t font_config; 112 113 114 115 /* Busy wait. */ 116 117 static void wait(uint32_t delay) 118 { 119 uint32_t counter = delay; 120 121 if (!delay) return; 122 while (counter--) __asm__(""); /* retain loop */ 123 } 124 125 /* Blink an attached LED with delays implemented using a loop. */ 126 127 static void blink(uint32_t delay, uint32_t port, uint32_t pins) 128 { 129 /* Clear outputs (LED). */ 130 131 CLR_REG(port, pins); 132 133 while (1) 134 { 135 wait(delay); 136 137 /* Invert outputs (LED). */ 138 139 INV_REG(port, pins); 140 } 141 } 142 143 /* Copy to the store from the display, then blit the image. */ 144 145 static void plot_sprite(uint8_t *background, int x, int y) 146 { 147 copy_display(&display_config, background, 148 sprite_width, sprite_height / SOURCE_YSTEP, 1, 149 x, y, -1, 0); 150 copy_display(&display_config, sprite, 151 sprite_width, sprite_height, SOURCE_YSTEP, 152 x, y, 0x8c, 1); 153 } 154 155 /* Copy to the display from the store, restoring the original background. */ 156 157 static void unplot_sprite(uint8_t *background, int x, int y) 158 { 159 copy_display(&display_config, background, 160 sprite_width, sprite_height / SOURCE_YSTEP, 1, 161 x, y, -1, 1); 162 } 163 164 /* Plot the revealed region at the edge of the screen after scrolling. */ 165 166 static void plot_screen_edge(int xorigin, int yorigin, int xstep) 167 { 168 /* The display region is either the left or right edge. */ 169 170 int xdisplay = xstep < 0 ? 0 : screendata_width - xstep; 171 172 /* The source region depends on the origin within the background image. */ 173 174 int xsource = (xdisplay + xorigin) % screendata_width; 175 176 /* The column width is the absolute increment. */ 177 178 int width = xstep < 0 ? -xstep : xstep; 179 180 /* Plot a column in two pieces if the vertical origin is 181 non-zero. The first piece is at (xdisplay, 0) and 182 provides the lower part of the background image displaced 183 upwards (or downwards having wrapped around) on the 184 screen. */ 185 186 copy_display_section(&display_config, screendata, 187 screendata_width, screendata_height, 188 xsource, yorigin, 189 width, screendata_height - yorigin, 190 SOURCE_YSTEP, 191 xdisplay, 0, 192 -1, 1); 193 194 /* The second column is at (xdisplay, h - yorigin) and 195 provides the upper part of the background image displaced 196 downwards (or upwards having wrapped around) on the 197 screen. */ 198 199 if (yorigin) 200 copy_display_section(&display_config, screendata, 201 screendata_width, screendata_height, 202 xsource, 0, 203 width, yorigin, 204 SOURCE_YSTEP, 205 xdisplay, (screendata_height - yorigin) / SOURCE_YSTEP, 206 -1, 1); 207 } 208 209 /* Wrap a value within the bounds [0, limit) provided value is already within 210 the bounds [-limit, limit * 2). */ 211 212 static int wrap_value(int value, int limit) 213 { 214 if (value < 0) 215 return value + limit; 216 else if (value >= limit) 217 return value - limit; 218 else 219 return value; 220 } 221 222 /* Move a sprite around on the framebuffer. */ 223 224 static void animate(uint32_t delay) 225 { 226 /* Stored region behind the sprite. */ 227 228 uint8_t background[(sprite_width * sprite_height) / SOURCE_YSTEP]; 229 230 /* Sprite position. */ 231 232 int x, y; 233 234 /* Scrolling directions. */ 235 236 int dir[] = {1, 0, -1, 0, 1}; 237 int dirindex = 0; 238 239 /* Scrolling position. */ 240 241 int xorigin = 0, yorigin = 0; 242 243 /* Scroll increments and replotted column details. */ 244 245 int xdir, ydir, xstep; 246 247 while (1) 248 { 249 for (y = 0; y < screendata_height - sprite_height; y++) 250 { 251 for (x = 0; x < screendata_width - sprite_width; x++) 252 { 253 plot_sprite(background, x, y); 254 255 /* Update the display with the frame details. */ 256 257 vga_set_frame(&display_config); 258 wait(delay); 259 260 unplot_sprite(background, x, y); 261 262 /* Scroll in the indicated direction. */ 263 264 xdir = dir[dirindex]; 265 ydir = dir[dirindex + 1]; 266 267 scroll_display(&display_config, xdir, ydir); 268 269 /* Update the vertical origin if appropriate. */ 270 271 if (ydir) 272 yorigin = wrap_value(yorigin + ydir * SOURCE_YSTEP, 273 screendata_height); 274 275 /* For horizontal scrolling, plot the exposed column at the left 276 (if scrolling left) or at the right (if scrolling right). */ 277 278 if (xdir) 279 { 280 /* Due to the effect of a simple screen start increment in 281 the dual channel configuration, horizontal scrolling 282 involves two pixel increments and thus requires a two- 283 pixel column to be plotted. */ 284 285 xstep = xdir * SCROLL_XSTEP; 286 287 /* Determine the location of the column to be plotted. */ 288 289 xorigin = wrap_value(xorigin + xstep, screendata_width); 290 291 /* Plot either at the left or right edge. */ 292 293 plot_screen_edge(xorigin, yorigin, xstep); 294 } 295 } 296 297 /* Switch direction periodically. */ 298 299 dirindex = wrap_value(dirindex + 1, 4); 300 } 301 } 302 } 303 304 /* Fill the screen with characters. */ 305 306 static void write_chars(void) 307 { 308 int x = 0, y = 0; 309 char c; 310 311 font_config.chars = (char_definition_t *) fontchars; 312 font_config.table = fonttable; 313 font_config.base = fontbase; 314 font_config.limit = fontlimit; 315 316 while (y < display_config.line_count) 317 for (c = (char) font_config.base; c < (char) font_config.limit; c++) 318 { 319 x = write_char(&display_config, &font_config, c, x, y, 0xff); 320 321 if (x > display_config.line_length) 322 { 323 x = 0; y += 9; 324 } 325 } 326 } 327 328 /* Set up a background. */ 329 330 static void setup(void) 331 { 332 int frame; 333 334 for (frame = 0; frame < display_config.frames; frame++) 335 { 336 /* Obtain the frame. */ 337 338 select_frame(&display_config, frame, 0); 339 340 /* Plot the image centred on the screen. */ 341 342 copy_display(&display_config, screendata, 343 screendata_width, screendata_height, 344 SOURCE_YSTEP, 345 (display_config.line_length - screendata_width) / 2, 346 (display_config.line_count - (screendata_height / SOURCE_YSTEP)) / 2, 347 -1, 1); 348 349 /* Write a sequence of characters. */ 350 351 write_chars(); 352 } 353 354 select_frame(&display_config, 0, 0); 355 } 356 357 358 359 /* Main program. */ 360 361 void main(void) 362 { 363 init_memory(); 364 init_pins(); 365 init_outputs(); 366 367 unlock_config(); 368 config_oc(); 369 config_uart(); 370 lock_config(); 371 372 #ifndef TRANSFER_CPU 373 init_dma(); 374 #endif 375 376 #ifdef PARALLEL_MODE 377 init_pm(); 378 379 /* Configure parallel master mode. */ 380 381 pm_init(0, 0b10); 382 pm_set_output(0, 1, 0); 383 pm_on(0); 384 #endif 385 386 uart_init(1, FPB, 115200); 387 uart_on(1); 388 389 /* Initialise memory for a double-buffered display. */ 390 391 init_display(&display_config, framebuffer, LINE_LENGTH, LINE_COUNT, FRAME_COUNT); 392 393 /* Initialise VGA output with one or two line channels, configuring a line 394 timer and any transfer timer, with an initiating channel being introduced 395 if a transfer timer is specified. */ 396 397 init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); 398 399 /* Configure VGA output transfer to the output register, also configuring 400 output compare units for horizontal and vertical sync. */ 401 402 vga_configure_transfer(VGA_OUTPUT); 403 vga_configure_sync(1, 2); 404 405 interrupts_on(); 406 407 /* Move a sprite around on the screen with a delay between each movement. */ 408 409 setup(); 410 animate(1 << 18); 411 } 412 413 414 415 /* Exception and interrupt handlers. */ 416 417 void exception_handler(void) 418 { 419 blink(1 << 22, PORTA, LED_PIN); 420 } 421 422 void interrupt_handler(void) 423 { 424 vga_interrupt_handler(); 425 } 426 427 428 429 /* Peripheral pin configuration. */ 430 431 void config_oc(void) 432 { 433 /* Map OC1. */ 434 435 REG(OC1_PIN) = 0b0101; 436 437 /* Map OC2. */ 438 439 REG(OC2_PIN) = 0b0101; 440 } 441 442 void config_uart(void) 443 { 444 /* Map U1RX to RPB13. */ 445 446 REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ 447 448 /* Map U1TX to RPB15. */ 449 450 REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ 451 452 /* Set RPB13 to input. */ 453 454 SET_REG(TRISB, 1 << 13); 455 }