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