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