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