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 "debug.h" 22 #include "display.h" 23 #include "font.h" 24 #include "init.h" 25 #include "pic32_c.h" 26 #include "utils.h" 27 #include "vga_display.h" 28 29 /* Specific functionality. */ 30 31 #include "devconfig.h" 32 #include "display_config.h" 33 #include "main.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 SCROLL_XSTEP 1 43 44 /* DMA-based transfers: single channel by default. */ 45 46 #else 47 #define SCROLL_XSTEP LINE_CHANNELS 48 #endif 49 50 51 52 /* Define timers if not indicated in the build configuration. */ 53 54 #ifndef LINE_TIMER 55 #define LINE_TIMER 2 56 #endif 57 58 #ifndef TRANSFER_TIMER 59 #define TRANSFER_TIMER 0 60 #endif 61 62 /* Define different output ports and pins for parallel mode. */ 63 64 #ifndef PARALLEL_MODE 65 #define VGA_OUTPUT PORTB 66 #define OC1_PIN RPA0R 67 #define OC2_PIN RPA1R 68 #define LED_PIN (1 << 3) 69 #else 70 #define VGA_OUTPUT PM_REG(0, PMxDIN) 71 #define OC1_PIN RPB4R 72 #define OC2_PIN RPB5R 73 #define LED_PIN (1 << 2) 74 #endif 75 76 77 78 /* Define the relationship between source images and the screen. */ 79 80 #ifndef SOURCE_HEIGHT 81 #define SOURCE_HEIGHT 256 82 #endif 83 84 #define SOURCE_YSTEP (SOURCE_HEIGHT / LINE_COUNT) 85 86 87 88 /* Framebuffer memory. */ 89 90 static uint8_t framebuffer[FRAME_SIZE * FRAME_COUNT]; 91 92 93 94 /* Bundled image and font data. */ 95 96 extern uint8_t screendata[]; 97 extern uint32_t screendata_width, screendata_height; 98 extern uint8_t sprite[]; 99 extern uint32_t sprite_width, sprite_height; 100 101 extern uint8_t fontchars[]; 102 extern uint32_t fonttable[]; 103 extern uint32_t fontbase, fontlimit; 104 105 static font_config_t font_config; 106 107 108 109 /* Copy to the store from the display, then blit the image. */ 110 111 static void plot_sprite(uint8_t *background, int x, int y) 112 { 113 copy_display(&display_config, background, 114 sprite_width, sprite_height / SOURCE_YSTEP, 1, 115 x, y, -1, 0); 116 copy_display(&display_config, sprite, 117 sprite_width, sprite_height, SOURCE_YSTEP, 118 x, y, 0x8c, 1); 119 } 120 121 /* Copy to the display from the store, restoring the original background. */ 122 123 static void unplot_sprite(uint8_t *background, int x, int y) 124 { 125 copy_display(&display_config, background, 126 sprite_width, sprite_height / SOURCE_YSTEP, 1, 127 x, y, -1, 1); 128 } 129 130 /* Plot the revealed region at the edge of the screen after scrolling. */ 131 132 static void plot_screen_edge(int xorigin, int yorigin, int xstep) 133 { 134 /* Determine positions within the image. */ 135 136 int xpos = wrap_value(xorigin, screendata_width); 137 int ypos = wrap_value(yorigin, screendata_height); 138 139 /* The display region is either the left or right edge. */ 140 141 int xdisplay = xstep < 0 ? 0 : screendata_width - xstep; 142 143 /* The source region depends on the origin within the background image. */ 144 145 int xsource = wrap_value(xdisplay + xpos, screendata_width); 146 147 /* The column width is the absolute increment. */ 148 149 int width = xstep < 0 ? -xstep : xstep; 150 151 /* Not all of the column may be available if close to the edge of the 152 image, requiring multiple slices. */ 153 154 int available = screendata_width - xsource; 155 156 while (width) 157 { 158 /* Plot a column in two pieces if the vertical origin is 159 non-zero. The first piece is at (xdisplay, 0) and 160 provides the lower part of the background image displaced 161 upwards (or downwards having wrapped around) on the 162 screen. */ 163 164 copy_display_section(&display_config, screendata, 165 screendata_width, screendata_height, 166 xsource, ypos, 167 width, screendata_height - ypos, 168 SOURCE_YSTEP, 169 xdisplay, 0, 170 -1, 1); 171 172 /* The second column is at (xdisplay, h - ypos) and 173 provides the upper part of the background image displaced 174 downwards (or upwards having wrapped around) on the 175 screen. */ 176 177 if (ypos) 178 copy_display_section(&display_config, screendata, 179 screendata_width, screendata_height, 180 xsource, 0, 181 width, ypos, 182 SOURCE_YSTEP, 183 xdisplay, (screendata_height - ypos) / SOURCE_YSTEP, 184 -1, 1); 185 186 /* Get the next slice of the column. */ 187 188 if (available < width) 189 { 190 width -= available; 191 xsource = 0; 192 xdisplay = wrap_value(xdisplay + available, screendata_width); 193 available = screendata_width; 194 } 195 else 196 width = 0; 197 } 198 } 199 200 /* Move a sprite around on the framebuffer. */ 201 202 static void animate(uint32_t delay) 203 { 204 /* Stored region behind the sprite. */ 205 206 uint8_t background[FRAME_COUNT][(sprite_width * sprite_height) / SOURCE_YSTEP]; 207 208 /* Positions of the stored regions for each frame. */ 209 210 int background_x[FRAME_COUNT], background_y[FRAME_COUNT]; 211 int background_stored[FRAME_COUNT]; 212 213 /* Screen start values for each frame. */ 214 215 uint32_t frame_offset[FRAME_COUNT]; 216 217 /* Sprite position. */ 218 219 int x, y; 220 221 /* Scrolling directions. */ 222 223 int dir[] = {1, 0, -1, 0, 1}; 224 int dirindex = 0; 225 226 /* Scrolling position. */ 227 228 int xorigin = 0, yorigin = 0; 229 230 /* Scrolling positions for each frame. */ 231 232 int xorigins[FRAME_COUNT], yorigins[FRAME_COUNT]; 233 234 /* Scroll increments and replotted column details. */ 235 236 int xdir, ydir, xstep, ystep; 237 238 /* Current frame being accessed. */ 239 240 int frame; 241 242 for (frame = 0; frame < display_config.frames; frame++) 243 { 244 background_stored[frame] = 0; 245 frame_offset[frame] = 0; 246 xorigins[frame] = xorigin; 247 yorigins[frame] = yorigin; 248 } 249 250 frame = display_config.frame; 251 252 /* Animation loop. */ 253 254 while (1) 255 { 256 for (y = 0; y < display_config.line_count - sprite_height; y++) 257 { 258 for (x = 0; x < display_config.line_length - sprite_width; x++) 259 { 260 plot_sprite(background[frame], x, y); 261 262 /* Record the stored background. */ 263 264 background_x[frame] = x; 265 background_y[frame] = y; 266 background_stored[frame] = 1; 267 268 /* Update the display with the frame details. */ 269 270 vga_set_frame(&display_config); 271 wait(delay); 272 273 /* Select the next frame to plot to. */ 274 275 frame = wrap_value(frame + 1, display_config.frames); 276 select_frame(&display_config, frame, frame_offset[frame]); 277 278 /* Prepare the frame for updates. */ 279 280 if (background_stored[frame]) 281 unplot_sprite(background[frame], background_x[frame], background_y[frame]); 282 283 /* Scroll in the indicated direction. */ 284 285 xdir = dir[dirindex]; 286 ydir = dir[dirindex + 1]; 287 288 /* Update the origin if appropriate. */ 289 290 /* Due to the effect of a simple screen start increment in the 291 dual channel configuration, horizontal scrolling involves two 292 pixel increments and thus requires a two-pixel column to be 293 plotted per scrolling increment. */ 294 295 if (xdir) 296 xorigin += xdir * SCROLL_XSTEP; 297 298 /* For vertically-scaled backgrounds, the full resolution image 299 is traversed by multiples of the scrolling increment. */ 300 301 if (ydir) 302 yorigin += ydir * SOURCE_YSTEP; 303 304 /* Determine the magnitude of the scrolling required from this 305 frame to the current origin. */ 306 307 xstep = xorigin - xorigins[frame]; 308 ystep = yorigin - yorigins[frame]; 309 310 /* Scroll the frame. */ 311 312 scroll_display(&display_config, xstep / SCROLL_XSTEP, 313 ystep / SOURCE_YSTEP); 314 315 /* Record the new screen start offset. */ 316 317 frame_offset[frame] = get_start_offset(&display_config); 318 319 /* For horizontal scrolling, plot the exposed column at the left 320 (if scrolling left) or at the right (if scrolling right). */ 321 322 if (xstep) 323 plot_screen_edge(xorigin, yorigin, xstep); 324 325 /* Record the origin for this frame. */ 326 327 xorigins[frame] = xorigin; 328 yorigins[frame] = yorigin; 329 } 330 331 /* Switch direction periodically. */ 332 333 dirindex = wrap_value(dirindex + 1, 4); 334 } 335 } 336 } 337 338 /* Fill the screen with characters. */ 339 340 static void write_chars(void) 341 { 342 const int line_height = 9; 343 int x = 0, y = 0; 344 char c; 345 346 font_config.chars = (char_definition_t *) fontchars; 347 font_config.table = fonttable; 348 font_config.base = fontbase; 349 font_config.limit = fontlimit; 350 351 while (y + line_height < display_config.line_count) 352 { 353 for (c = (char) font_config.base; c < (char) font_config.limit; c++) 354 { 355 if (x + get_char_definition(&font_config, c)->width > display_config.line_length) 356 { 357 x = 0; y += line_height; 358 } 359 360 if (y + line_height >= display_config.line_count) 361 break; 362 363 x = write_char(&display_config, &font_config, c, x, y, 0xff); 364 } 365 } 366 } 367 368 /* Set up a background. */ 369 370 static void setup(void) 371 { 372 int frame; 373 374 for (frame = 0; frame < display_config.frames; frame++) 375 { 376 /* Obtain the frame. */ 377 378 select_frame(&display_config, frame, 0); 379 380 /* Plot the image centred on the screen. */ 381 382 copy_display(&display_config, screendata, 383 screendata_width, screendata_height, 384 SOURCE_YSTEP, 385 (display_config.line_length - screendata_width) / 2, 386 (display_config.line_count - (screendata_height / SOURCE_YSTEP)) / 2, 387 -1, 1); 388 389 /* Write a sequence of characters. */ 390 391 write_chars(); 392 } 393 394 select_frame(&display_config, 0, 0); 395 } 396 397 398 399 /* Main program. */ 400 401 void main(void) 402 { 403 init_memory(); 404 init_pins(); 405 init_outputs(); 406 407 unlock_config(); 408 config_oc(); 409 config_uart(); 410 lock_config(); 411 412 #ifndef TRANSFER_CPU 413 init_dma(); 414 #endif 415 416 #ifdef PARALLEL_MODE 417 init_pm(); 418 419 /* Configure parallel master mode. */ 420 421 pm_init(0, 0b10); 422 pm_set_output(0, 1, 0); 423 pm_on(0); 424 #endif 425 426 uart_init(1, FPB, 115200); 427 uart_on(1); 428 429 /* Initialise memory for a double-buffered display. */ 430 431 init_display(&display_config, framebuffer, LINE_LENGTH, LINE_COUNT, FRAME_COUNT); 432 433 /* Initialise VGA output with one or two line channels, configuring a line 434 timer and any transfer timer, with an initiating channel being introduced 435 if a transfer timer is specified. */ 436 437 init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); 438 439 /* Configure VGA output transfer to the output register, also configuring 440 output compare units for horizontal and vertical sync. */ 441 442 vga_configure_transfer(VGA_OUTPUT); 443 vga_configure_sync(1, 2); 444 445 interrupts_on(); 446 447 /* Move a sprite around on the screen with a delay between each movement. */ 448 449 setup(); 450 animate(1 << 18); 451 } 452 453 454 455 /* Exception and interrupt handlers. */ 456 457 void exception_handler(void) 458 { 459 blink(1 << 22, PORTA, LED_PIN); 460 } 461 462 void interrupt_handler(void) 463 { 464 vga_interrupt_handler(); 465 } 466 467 468 469 /* Peripheral pin configuration. */ 470 471 void config_oc(void) 472 { 473 /* Map OC1. */ 474 475 REG(OC1_PIN) = 0b0101; 476 477 /* Map OC2. */ 478 479 REG(OC2_PIN) = 0b0101; 480 } 481 482 void config_uart(void) 483 { 484 /* Map U1RX to RPB13. */ 485 486 REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ 487 488 /* Map U1TX to RPB15. */ 489 490 REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ 491 492 /* Set RPB13 to input. */ 493 494 SET_REG(TRISB, 1 << 13); 495 }