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