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