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