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