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@41 | 28 | |
paul@41 | 29 | /* Specific functionality. */ |
paul@41 | 30 | |
paul@47 | 31 | #include "devconfig.h" |
paul@49 | 32 | #include "display_config.h" |
paul@78 | 33 | #include "main.h" |
paul@87 | 34 | |
paul@88 | 35 | |
paul@88 | 36 | |
paul@88 | 37 | /* Define DMA channels if not indicated in the build configuration. */ |
paul@88 | 38 | |
paul@88 | 39 | /* CPU-based transfers: no channels. */ |
paul@88 | 40 | |
paul@88 | 41 | #ifdef TRANSFER_CPU |
paul@88 | 42 | #define SCROLL_XSTEP 1 |
paul@88 | 43 | |
paul@88 | 44 | /* DMA-based transfers: single channel by default. */ |
paul@88 | 45 | |
paul@87 | 46 | #else |
paul@88 | 47 | #define SCROLL_XSTEP LINE_CHANNELS |
paul@87 | 48 | #endif |
paul@24 | 49 | |
paul@24 | 50 | |
paul@41 | 51 | |
paul@59 | 52 | /* Define timers if not indicated in the build configuration. */ |
paul@59 | 53 | |
paul@59 | 54 | #ifndef LINE_TIMER |
paul@59 | 55 | #define LINE_TIMER 2 |
paul@59 | 56 | #endif |
paul@59 | 57 | |
paul@59 | 58 | #ifndef TRANSFER_TIMER |
paul@59 | 59 | #define TRANSFER_TIMER 0 |
paul@59 | 60 | #endif |
paul@59 | 61 | |
paul@72 | 62 | /* Define different output ports and pins for parallel mode. */ |
paul@72 | 63 | |
paul@72 | 64 | #ifndef PARALLEL_MODE |
paul@72 | 65 | #define VGA_OUTPUT PORTB |
paul@72 | 66 | #define OC1_PIN RPA0R |
paul@72 | 67 | #define OC2_PIN RPA1R |
paul@72 | 68 | #define LED_PIN (1 << 3) |
paul@72 | 69 | #else |
paul@72 | 70 | #define VGA_OUTPUT PM_REG(0, PMxDIN) |
paul@72 | 71 | #define OC1_PIN RPB4R |
paul@72 | 72 | #define OC2_PIN RPB5R |
paul@72 | 73 | #define LED_PIN (1 << 2) |
paul@72 | 74 | #endif |
paul@72 | 75 | |
paul@59 | 76 | |
paul@59 | 77 | |
paul@101 | 78 | /* Define the relationship between source images and the screen. */ |
paul@101 | 79 | |
paul@101 | 80 | #ifndef SOURCE_HEIGHT |
paul@101 | 81 | #define SOURCE_HEIGHT 256 |
paul@101 | 82 | #endif |
paul@101 | 83 | |
paul@101 | 84 | #define SOURCE_YSTEP (SOURCE_HEIGHT / LINE_COUNT) |
paul@101 | 85 | |
paul@101 | 86 | |
paul@101 | 87 | |
paul@93 | 88 | /* Framebuffer memory. */ |
paul@93 | 89 | |
paul@93 | 90 | static uint8_t framebuffer[FRAME_SIZE * FRAME_COUNT]; |
paul@93 | 91 | |
paul@115 | 92 | /* Screen start values for each frame. */ |
paul@115 | 93 | |
paul@115 | 94 | uint8_t *screen_starts[FRAME_COUNT]; |
paul@115 | 95 | |
paul@93 | 96 | |
paul@93 | 97 | |
paul@78 | 98 | /* Bundled image and font data. */ |
paul@69 | 99 | |
paul@69 | 100 | extern uint8_t screendata[]; |
paul@69 | 101 | extern uint32_t screendata_width, screendata_height; |
paul@74 | 102 | extern uint8_t sprite[]; |
paul@74 | 103 | extern uint32_t sprite_width, sprite_height; |
paul@74 | 104 | |
paul@78 | 105 | extern uint8_t fontchars[]; |
paul@78 | 106 | extern uint32_t fonttable[]; |
paul@78 | 107 | extern uint32_t fontbase, fontlimit; |
paul@78 | 108 | |
paul@78 | 109 | static font_config_t font_config; |
paul@78 | 110 | |
paul@69 | 111 | |
paul@69 | 112 | |
paul@94 | 113 | /* Copy to the store from the display, then blit the image. */ |
paul@94 | 114 | |
paul@94 | 115 | static void plot_sprite(uint8_t *background, int x, int y) |
paul@94 | 116 | { |
paul@111 | 117 | display_copy(&display_config, background, |
paul@101 | 118 | sprite_width, sprite_height / SOURCE_YSTEP, 1, |
paul@94 | 119 | x, y, -1, 0); |
paul@111 | 120 | display_copy(&display_config, sprite, |
paul@101 | 121 | sprite_width, sprite_height, SOURCE_YSTEP, |
paul@94 | 122 | x, y, 0x8c, 1); |
paul@94 | 123 | } |
paul@94 | 124 | |
paul@94 | 125 | /* Copy to the display from the store, restoring the original background. */ |
paul@94 | 126 | |
paul@94 | 127 | static void unplot_sprite(uint8_t *background, int x, int y) |
paul@94 | 128 | { |
paul@111 | 129 | display_copy(&display_config, background, |
paul@101 | 130 | sprite_width, sprite_height / SOURCE_YSTEP, 1, |
paul@94 | 131 | x, y, -1, 1); |
paul@94 | 132 | } |
paul@94 | 133 | |
paul@94 | 134 | /* Plot the revealed region at the edge of the screen after scrolling. */ |
paul@94 | 135 | |
paul@94 | 136 | static void plot_screen_edge(int xorigin, int yorigin, int xstep) |
paul@94 | 137 | { |
paul@103 | 138 | /* Determine positions within the image. */ |
paul@103 | 139 | |
paul@103 | 140 | int xpos = wrap_value(xorigin, screendata_width); |
paul@103 | 141 | int ypos = wrap_value(yorigin, screendata_height); |
paul@103 | 142 | |
paul@94 | 143 | /* The display region is either the left or right edge. */ |
paul@94 | 144 | |
paul@94 | 145 | int xdisplay = xstep < 0 ? 0 : screendata_width - xstep; |
paul@94 | 146 | |
paul@94 | 147 | /* The source region depends on the origin within the background image. */ |
paul@94 | 148 | |
paul@103 | 149 | int xsource = wrap_value(xdisplay + xpos, screendata_width); |
paul@94 | 150 | |
paul@94 | 151 | /* The column width is the absolute increment. */ |
paul@94 | 152 | |
paul@94 | 153 | int width = xstep < 0 ? -xstep : xstep; |
paul@94 | 154 | |
paul@103 | 155 | /* Not all of the column may be available if close to the edge of the |
paul@103 | 156 | image, requiring multiple slices. */ |
paul@103 | 157 | |
paul@103 | 158 | int available = screendata_width - xsource; |
paul@94 | 159 | |
paul@103 | 160 | while (width) |
paul@103 | 161 | { |
paul@103 | 162 | /* Plot a column in two pieces if the vertical origin is |
paul@103 | 163 | non-zero. The first piece is at (xdisplay, 0) and |
paul@103 | 164 | provides the lower part of the background image displaced |
paul@103 | 165 | upwards (or downwards having wrapped around) on the |
paul@103 | 166 | screen. */ |
paul@94 | 167 | |
paul@111 | 168 | display_copy_section(&display_config, screendata, |
paul@94 | 169 | screendata_width, screendata_height, |
paul@103 | 170 | xsource, ypos, |
paul@103 | 171 | width, screendata_height - ypos, |
paul@101 | 172 | SOURCE_YSTEP, |
paul@103 | 173 | xdisplay, 0, |
paul@94 | 174 | -1, 1); |
paul@103 | 175 | |
paul@103 | 176 | /* The second column is at (xdisplay, h - ypos) and |
paul@103 | 177 | provides the upper part of the background image displaced |
paul@103 | 178 | downwards (or upwards having wrapped around) on the |
paul@103 | 179 | screen. */ |
paul@94 | 180 | |
paul@103 | 181 | if (ypos) |
paul@111 | 182 | display_copy_section(&display_config, screendata, |
paul@103 | 183 | screendata_width, screendata_height, |
paul@103 | 184 | xsource, 0, |
paul@103 | 185 | width, ypos, |
paul@103 | 186 | SOURCE_YSTEP, |
paul@103 | 187 | xdisplay, (screendata_height - ypos) / SOURCE_YSTEP, |
paul@103 | 188 | -1, 1); |
paul@103 | 189 | |
paul@103 | 190 | /* Get the next slice of the column. */ |
paul@94 | 191 | |
paul@103 | 192 | if (available < width) |
paul@103 | 193 | { |
paul@103 | 194 | width -= available; |
paul@103 | 195 | xsource = 0; |
paul@103 | 196 | xdisplay = wrap_value(xdisplay + available, screendata_width); |
paul@103 | 197 | available = screendata_width; |
paul@103 | 198 | } |
paul@103 | 199 | else |
paul@103 | 200 | width = 0; |
paul@103 | 201 | } |
paul@94 | 202 | } |
paul@94 | 203 | |
paul@74 | 204 | /* Move a sprite around on the framebuffer. */ |
paul@74 | 205 | |
paul@74 | 206 | static void animate(uint32_t delay) |
paul@74 | 207 | { |
paul@88 | 208 | /* Stored region behind the sprite. */ |
paul@88 | 209 | |
paul@102 | 210 | uint8_t background[FRAME_COUNT][(sprite_width * sprite_height) / SOURCE_YSTEP]; |
paul@102 | 211 | |
paul@102 | 212 | /* Positions of the stored regions for each frame. */ |
paul@102 | 213 | |
paul@102 | 214 | int background_x[FRAME_COUNT], background_y[FRAME_COUNT]; |
paul@102 | 215 | int background_stored[FRAME_COUNT]; |
paul@102 | 216 | |
paul@88 | 217 | /* Sprite position. */ |
paul@88 | 218 | |
paul@74 | 219 | int x, y; |
paul@88 | 220 | |
paul@88 | 221 | /* Scrolling directions. */ |
paul@88 | 222 | |
paul@94 | 223 | int dir[] = {1, 0, -1, 0, 1}; |
paul@94 | 224 | int dirindex = 0; |
paul@88 | 225 | |
paul@88 | 226 | /* Scrolling position. */ |
paul@88 | 227 | |
paul@88 | 228 | int xorigin = 0, yorigin = 0; |
paul@88 | 229 | |
paul@102 | 230 | /* Scrolling positions for each frame. */ |
paul@102 | 231 | |
paul@102 | 232 | int xorigins[FRAME_COUNT], yorigins[FRAME_COUNT]; |
paul@102 | 233 | |
paul@94 | 234 | /* Scroll increments and replotted column details. */ |
paul@88 | 235 | |
paul@102 | 236 | int xdir, ydir, xstep, ystep; |
paul@102 | 237 | |
paul@102 | 238 | /* Current frame being accessed. */ |
paul@102 | 239 | |
paul@102 | 240 | int frame; |
paul@102 | 241 | |
paul@102 | 242 | for (frame = 0; frame < display_config.frames; frame++) |
paul@102 | 243 | { |
paul@102 | 244 | background_stored[frame] = 0; |
paul@102 | 245 | xorigins[frame] = xorigin; |
paul@102 | 246 | yorigins[frame] = yorigin; |
paul@102 | 247 | } |
paul@102 | 248 | |
paul@102 | 249 | frame = display_config.frame; |
paul@102 | 250 | |
paul@102 | 251 | /* Animation loop. */ |
paul@74 | 252 | |
paul@74 | 253 | while (1) |
paul@83 | 254 | { |
paul@102 | 255 | for (y = 0; y < display_config.line_count - sprite_height; y++) |
paul@83 | 256 | { |
paul@102 | 257 | for (x = 0; x < display_config.line_length - sprite_width; x++) |
paul@74 | 258 | { |
paul@102 | 259 | plot_sprite(background[frame], x, y); |
paul@102 | 260 | |
paul@102 | 261 | /* Record the stored background. */ |
paul@102 | 262 | |
paul@102 | 263 | background_x[frame] = x; |
paul@102 | 264 | background_y[frame] = y; |
paul@102 | 265 | background_stored[frame] = 1; |
paul@74 | 266 | |
paul@92 | 267 | /* Update the display with the frame details. */ |
paul@74 | 268 | |
paul@92 | 269 | vga_set_frame(&display_config); |
paul@74 | 270 | wait(delay); |
paul@74 | 271 | |
paul@102 | 272 | /* Select the next frame to plot to. */ |
paul@102 | 273 | |
paul@102 | 274 | frame = wrap_value(frame + 1, display_config.frames); |
paul@115 | 275 | display_select_frame(&display_config, frame); |
paul@102 | 276 | |
paul@102 | 277 | /* Prepare the frame for updates. */ |
paul@102 | 278 | |
paul@102 | 279 | if (background_stored[frame]) |
paul@102 | 280 | unplot_sprite(background[frame], background_x[frame], background_y[frame]); |
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@102 | 294 | if (xdir) |
paul@102 | 295 | xorigin += xdir * SCROLL_XSTEP; |
paul@84 | 296 | |
paul@103 | 297 | /* For vertically-scaled backgrounds, the full resolution image |
paul@103 | 298 | is traversed by multiples of the scrolling increment. */ |
paul@103 | 299 | |
paul@94 | 300 | if (ydir) |
paul@102 | 301 | yorigin += ydir * SOURCE_YSTEP; |
paul@102 | 302 | |
paul@102 | 303 | /* Determine the magnitude of the scrolling required from this |
paul@102 | 304 | frame to the current origin. */ |
paul@102 | 305 | |
paul@102 | 306 | xstep = xorigin - xorigins[frame]; |
paul@102 | 307 | ystep = yorigin - yorigins[frame]; |
paul@102 | 308 | |
paul@102 | 309 | /* Scroll the frame. */ |
paul@102 | 310 | |
paul@111 | 311 | display_scroll(&display_config, xstep / SCROLL_XSTEP, |
paul@102 | 312 | ystep / SOURCE_YSTEP); |
paul@102 | 313 | |
paul@83 | 314 | /* For horizontal scrolling, plot the exposed column at the left |
paul@83 | 315 | (if scrolling left) or at the right (if scrolling right). */ |
paul@83 | 316 | |
paul@102 | 317 | if (xstep) |
paul@102 | 318 | plot_screen_edge(xorigin, yorigin, xstep); |
paul@83 | 319 | |
paul@102 | 320 | /* Record the origin for this frame. */ |
paul@84 | 321 | |
paul@102 | 322 | xorigins[frame] = xorigin; |
paul@102 | 323 | yorigins[frame] = yorigin; |
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 | } |