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