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 display_copy(&display_config, background, 114 sprite_width, sprite_height / SOURCE_YSTEP, 1, 115 x, y, -1, 0); 116 display_copy(&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 display_copy(&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 display_copy_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 display_copy_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 display_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 display_scroll(&display_config, xstep / SCROLL_XSTEP, 313 ystep / SOURCE_YSTEP); 314 315 /* Record the new screen start offset. */ 316 317 frame_offset[frame] = display_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 init_font(&font_config, fontchars, fonttable, fontbase, fontlimit); 347 348 while (y + line_height < display_config.line_count) 349 { 350 for (c = (char) font_config.base; c < (char) font_config.limit; c++) 351 { 352 if (x + get_char_definition(&font_config, c)->width > display_config.line_length) 353 { 354 x = 0; y += line_height; 355 } 356 357 if (y + line_height >= display_config.line_count) 358 break; 359 360 x = write_char(&display_config, &font_config, c, x, y, 0xff); 361 } 362 } 363 } 364 365 /* Set up a background. */ 366 367 static void setup(void) 368 { 369 int frame; 370 371 for (frame = 0; frame < display_config.frames; frame++) 372 { 373 /* Obtain the frame. */ 374 375 display_select_frame(&display_config, frame, 0); 376 377 /* Plot the image centred on the screen. */ 378 379 display_copy(&display_config, screendata, 380 screendata_width, screendata_height, 381 SOURCE_YSTEP, 382 (display_config.line_length - screendata_width) / 2, 383 (display_config.line_count - (screendata_height / SOURCE_YSTEP)) / 2, 384 -1, 1); 385 386 /* Write a sequence of characters. */ 387 388 write_chars(); 389 } 390 391 display_select_frame(&display_config, 0, 0); 392 } 393 394 395 396 /* Main program. */ 397 398 void main(void) 399 { 400 init_memory(); 401 init_pins(); 402 init_outputs(); 403 404 unlock_config(); 405 config_oc(); 406 config_uart(); 407 lock_config(); 408 409 #ifndef TRANSFER_CPU 410 init_dma(); 411 #endif 412 413 #ifdef PARALLEL_MODE 414 init_pm(); 415 416 /* Configure parallel master mode. */ 417 418 pm_init(0, 0b10); 419 pm_set_output(0, 1, 0); 420 pm_on(0); 421 #endif 422 423 uart_init(1, FPB, 115200); 424 uart_on(1); 425 426 /* Initialise memory for a double-buffered display. */ 427 428 init_display(&display_config, framebuffer, LINE_LENGTH, LINE_COUNT, FRAME_COUNT); 429 430 /* Initialise VGA output with one or two line channels, configuring a line 431 timer and any transfer timer, with an initiating channel being introduced 432 if a transfer timer is specified. */ 433 434 init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); 435 436 /* Configure VGA output transfer to the output register, also configuring 437 output compare units for horizontal and vertical sync. */ 438 439 vga_configure_transfer(VGA_OUTPUT); 440 vga_configure_sync(1, 2); 441 442 interrupts_on(); 443 444 /* Move a sprite around on the screen with a delay between each movement. */ 445 446 setup(); 447 animate(1 << 18); 448 } 449 450 451 452 /* Exception and interrupt handlers. */ 453 454 void exception_handler(void) 455 { 456 blink(1 << 22, PORTA, LED_PIN); 457 } 458 459 void interrupt_handler(void) 460 { 461 vga_interrupt_handler(); 462 } 463 464 465 466 /* Peripheral pin configuration. */ 467 468 void config_oc(void) 469 { 470 /* Map OC1. */ 471 472 REG(OC1_PIN) = 0b0101; 473 474 /* Map OC2. */ 475 476 REG(OC2_PIN) = 0b0101; 477 } 478 479 void config_uart(void) 480 { 481 /* Map U1RX to RPB13. */ 482 483 REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ 484 485 /* Map U1TX to RPB15. */ 486 487 REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ 488 489 /* Set RPB13 to input. */ 490 491 SET_REG(TRISB, 1 << 13); 492 }