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