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@118 | 24 | #include "image.h" |
paul@24 | 25 | #include "init.h" |
paul@108 | 26 | #include "pic32_c.h" |
paul@108 | 27 | #include "utils.h" |
paul@108 | 28 | #include "vga_display.h" |
paul@116 | 29 | #include "viewport.h" |
paul@41 | 30 | |
paul@41 | 31 | /* Specific functionality. */ |
paul@41 | 32 | |
paul@47 | 33 | #include "devconfig.h" |
paul@49 | 34 | #include "display_config.h" |
paul@78 | 35 | #include "main.h" |
paul@87 | 36 | |
paul@88 | 37 | |
paul@88 | 38 | |
paul@88 | 39 | /* Define DMA channels if not indicated in the build configuration. */ |
paul@88 | 40 | |
paul@88 | 41 | /* CPU-based transfers: no channels. */ |
paul@88 | 42 | |
paul@88 | 43 | #ifdef TRANSFER_CPU |
paul@88 | 44 | #define SCROLL_XSTEP 1 |
paul@88 | 45 | |
paul@88 | 46 | /* DMA-based transfers: single channel by default. */ |
paul@88 | 47 | |
paul@87 | 48 | #else |
paul@88 | 49 | #define SCROLL_XSTEP LINE_CHANNELS |
paul@87 | 50 | #endif |
paul@24 | 51 | |
paul@24 | 52 | |
paul@41 | 53 | |
paul@59 | 54 | /* Define timers if not indicated in the build configuration. */ |
paul@59 | 55 | |
paul@59 | 56 | #ifndef LINE_TIMER |
paul@59 | 57 | #define LINE_TIMER 2 |
paul@59 | 58 | #endif |
paul@59 | 59 | |
paul@59 | 60 | #ifndef TRANSFER_TIMER |
paul@59 | 61 | #define TRANSFER_TIMER 0 |
paul@59 | 62 | #endif |
paul@59 | 63 | |
paul@72 | 64 | /* Define different output ports and pins for parallel mode. */ |
paul@72 | 65 | |
paul@72 | 66 | #ifndef PARALLEL_MODE |
paul@72 | 67 | #define VGA_OUTPUT PORTB |
paul@72 | 68 | #define OC1_PIN RPA0R |
paul@72 | 69 | #define OC2_PIN RPA1R |
paul@72 | 70 | #define LED_PIN (1 << 3) |
paul@72 | 71 | #else |
paul@72 | 72 | #define VGA_OUTPUT PM_REG(0, PMxDIN) |
paul@72 | 73 | #define OC1_PIN RPB4R |
paul@72 | 74 | #define OC2_PIN RPB5R |
paul@72 | 75 | #define LED_PIN (1 << 2) |
paul@72 | 76 | #endif |
paul@72 | 77 | |
paul@59 | 78 | |
paul@59 | 79 | |
paul@101 | 80 | /* Define the relationship between source images and the screen. */ |
paul@101 | 81 | |
paul@101 | 82 | #ifndef SOURCE_HEIGHT |
paul@101 | 83 | #define SOURCE_HEIGHT 256 |
paul@101 | 84 | #endif |
paul@101 | 85 | |
paul@101 | 86 | #define SOURCE_YSTEP (SOURCE_HEIGHT / LINE_COUNT) |
paul@101 | 87 | |
paul@101 | 88 | |
paul@101 | 89 | |
paul@93 | 90 | /* Framebuffer memory. */ |
paul@93 | 91 | |
paul@93 | 92 | static uint8_t framebuffer[FRAME_SIZE * FRAME_COUNT]; |
paul@93 | 93 | |
paul@115 | 94 | /* Screen start values for each frame. */ |
paul@115 | 95 | |
paul@115 | 96 | uint8_t *screen_starts[FRAME_COUNT]; |
paul@115 | 97 | |
paul@116 | 98 | /* Origin details for the viewport. */ |
paul@116 | 99 | |
paul@116 | 100 | int xorigins[FRAME_COUNT], yorigins[FRAME_COUNT]; |
paul@116 | 101 | |
paul@93 | 102 | |
paul@93 | 103 | |
paul@78 | 104 | /* Bundled image and font data. */ |
paul@69 | 105 | |
paul@118 | 106 | extern image_t screendata; |
paul@118 | 107 | extern image_t sprite; |
paul@74 | 108 | |
paul@78 | 109 | extern uint8_t fontchars[]; |
paul@78 | 110 | extern uint32_t fonttable[]; |
paul@78 | 111 | extern uint32_t fontbase, fontlimit; |
paul@78 | 112 | |
paul@78 | 113 | static font_config_t font_config; |
paul@78 | 114 | |
paul@69 | 115 | |
paul@69 | 116 | |
paul@94 | 117 | /* Plot the revealed region at the edge of the screen after scrolling. */ |
paul@94 | 118 | |
paul@116 | 119 | static void plot_screen_edge(viewport_t *v, int xorigin, int yorigin, |
paul@116 | 120 | int xstep, int ystep) |
paul@94 | 121 | { |
paul@103 | 122 | /* Determine positions within the image. */ |
paul@103 | 123 | |
paul@118 | 124 | int xpos = wrap_value(xorigin, screendata.width); |
paul@118 | 125 | int ypos = wrap_value(yorigin, screendata.height); |
paul@103 | 126 | |
paul@94 | 127 | /* The display region is either the left or right edge. */ |
paul@94 | 128 | |
paul@118 | 129 | int xdisplay = xstep < 0 ? 0 : screendata.width - xstep; |
paul@94 | 130 | |
paul@94 | 131 | /* The source region depends on the origin within the background image. */ |
paul@94 | 132 | |
paul@118 | 133 | int xsource = wrap_value(xdisplay + xpos, screendata.width); |
paul@94 | 134 | |
paul@94 | 135 | /* The column width is the absolute increment. */ |
paul@94 | 136 | |
paul@94 | 137 | int width = xstep < 0 ? -xstep : xstep; |
paul@94 | 138 | |
paul@103 | 139 | /* Not all of the column may be available if close to the edge of the |
paul@103 | 140 | image, requiring multiple slices. */ |
paul@103 | 141 | |
paul@118 | 142 | int available = screendata.width - xsource; |
paul@94 | 143 | |
paul@103 | 144 | while (width) |
paul@103 | 145 | { |
paul@103 | 146 | /* Plot a column in two pieces if the vertical origin is |
paul@103 | 147 | non-zero. The first piece is at (xdisplay, 0) and |
paul@103 | 148 | provides the lower part of the background image displaced |
paul@103 | 149 | upwards (or downwards having wrapped around) on the |
paul@103 | 150 | screen. */ |
paul@94 | 151 | |
paul@118 | 152 | display_copy_section(&display_config, screendata.image, |
paul@118 | 153 | screendata.width, screendata.height, |
paul@103 | 154 | xsource, ypos, |
paul@118 | 155 | width, screendata.height - ypos, |
paul@116 | 156 | v->yscale, |
paul@103 | 157 | xdisplay, 0, |
paul@94 | 158 | -1, 1); |
paul@103 | 159 | |
paul@103 | 160 | /* The second column is at (xdisplay, h - ypos) and |
paul@103 | 161 | provides the upper part of the background image displaced |
paul@103 | 162 | downwards (or upwards having wrapped around) on the |
paul@103 | 163 | screen. */ |
paul@94 | 164 | |
paul@103 | 165 | if (ypos) |
paul@118 | 166 | display_copy_section(&display_config, screendata.image, |
paul@118 | 167 | screendata.width, screendata.height, |
paul@103 | 168 | xsource, 0, |
paul@103 | 169 | width, ypos, |
paul@116 | 170 | v->yscale, |
paul@118 | 171 | xdisplay, (screendata.height - ypos) / v->yscale, |
paul@103 | 172 | -1, 1); |
paul@103 | 173 | |
paul@103 | 174 | /* Get the next slice of the column. */ |
paul@94 | 175 | |
paul@103 | 176 | if (available < width) |
paul@103 | 177 | { |
paul@103 | 178 | width -= available; |
paul@103 | 179 | xsource = 0; |
paul@118 | 180 | xdisplay = wrap_value(xdisplay + available, screendata.width); |
paul@118 | 181 | available = screendata.width; |
paul@103 | 182 | } |
paul@103 | 183 | else |
paul@103 | 184 | width = 0; |
paul@103 | 185 | } |
paul@94 | 186 | } |
paul@94 | 187 | |
paul@74 | 188 | /* Move a sprite around on the framebuffer. */ |
paul@74 | 189 | |
paul@74 | 190 | static void animate(uint32_t delay) |
paul@74 | 191 | { |
paul@117 | 192 | /* Stores of background details, replotted when moving the sprite. */ |
paul@102 | 193 | |
paul@121 | 194 | Sprite(s, &sprite, &display_config, SOURCE_YSTEP); |
paul@102 | 195 | |
paul@88 | 196 | /* Sprite position. */ |
paul@88 | 197 | |
paul@74 | 198 | int x, y; |
paul@88 | 199 | |
paul@88 | 200 | /* Scrolling directions. */ |
paul@88 | 201 | |
paul@94 | 202 | int dir[] = {1, 0, -1, 0, 1}; |
paul@94 | 203 | int dirindex = 0; |
paul@88 | 204 | |
paul@116 | 205 | /* Scroll increments. */ |
paul@88 | 206 | |
paul@116 | 207 | int xdir, ydir; |
paul@102 | 208 | |
paul@116 | 209 | /* Scrolling viewport. */ |
paul@88 | 210 | |
paul@116 | 211 | viewport_t v; |
paul@102 | 212 | |
paul@116 | 213 | /* Initialise the viewport. */ |
paul@102 | 214 | |
paul@116 | 215 | init_viewport(&v, &display_config, xorigins, yorigins, |
paul@116 | 216 | SCROLL_XSTEP, SOURCE_YSTEP, plot_screen_edge); |
paul@102 | 217 | |
paul@102 | 218 | /* Animation loop. */ |
paul@74 | 219 | |
paul@74 | 220 | while (1) |
paul@83 | 221 | { |
paul@120 | 222 | for (y = 0; y < display_config.line_count - s.image->height; y++) |
paul@83 | 223 | { |
paul@120 | 224 | for (x = 0; x < display_config.line_length - s.image->width; x++) |
paul@74 | 225 | { |
paul@121 | 226 | image_plot_sprite(&s, x, y, 0x8c); |
paul@74 | 227 | |
paul@92 | 228 | /* Update the display with the frame details. */ |
paul@74 | 229 | |
paul@92 | 230 | vga_set_frame(&display_config); |
paul@74 | 231 | wait(delay); |
paul@74 | 232 | |
paul@102 | 233 | /* Select the next frame to plot to. */ |
paul@102 | 234 | |
paul@116 | 235 | display_select_next_frame(&display_config); |
paul@102 | 236 | |
paul@102 | 237 | /* Prepare the frame for updates. */ |
paul@102 | 238 | |
paul@121 | 239 | image_unplot_sprite(&s); |
paul@83 | 240 | |
paul@83 | 241 | /* Scroll in the indicated direction. */ |
paul@83 | 242 | |
paul@94 | 243 | xdir = dir[dirindex]; |
paul@94 | 244 | ydir = dir[dirindex + 1]; |
paul@94 | 245 | |
paul@102 | 246 | /* Update the origin if appropriate. */ |
paul@83 | 247 | |
paul@102 | 248 | /* Due to the effect of a simple screen start increment in the |
paul@102 | 249 | dual channel configuration, horizontal scrolling involves two |
paul@102 | 250 | pixel increments and thus requires a two-pixel column to be |
paul@103 | 251 | plotted per scrolling increment. */ |
paul@102 | 252 | |
paul@103 | 253 | /* For vertically-scaled backgrounds, the full resolution image |
paul@103 | 254 | is traversed by multiples of the scrolling increment. */ |
paul@103 | 255 | |
paul@116 | 256 | viewport_update_origin(&v, xdir * SCROLL_XSTEP, |
paul@116 | 257 | ydir * SOURCE_YSTEP); |
paul@74 | 258 | } |
paul@83 | 259 | |
paul@83 | 260 | /* Switch direction periodically. */ |
paul@83 | 261 | |
paul@94 | 262 | dirindex = wrap_value(dirindex + 1, 4); |
paul@83 | 263 | } |
paul@83 | 264 | } |
paul@74 | 265 | } |
paul@74 | 266 | |
paul@78 | 267 | /* Fill the screen with characters. */ |
paul@78 | 268 | |
paul@78 | 269 | static void write_chars(void) |
paul@78 | 270 | { |
paul@109 | 271 | const int line_height = 9; |
paul@78 | 272 | int x = 0, y = 0; |
paul@78 | 273 | char c; |
paul@78 | 274 | |
paul@110 | 275 | init_font(&font_config, fontchars, fonttable, fontbase, fontlimit); |
paul@78 | 276 | |
paul@109 | 277 | while (y + line_height < display_config.line_count) |
paul@109 | 278 | { |
paul@78 | 279 | for (c = (char) font_config.base; c < (char) font_config.limit; c++) |
paul@78 | 280 | { |
paul@109 | 281 | if (x + get_char_definition(&font_config, c)->width > display_config.line_length) |
paul@78 | 282 | { |
paul@109 | 283 | x = 0; y += line_height; |
paul@78 | 284 | } |
paul@109 | 285 | |
paul@109 | 286 | if (y + line_height >= display_config.line_count) |
paul@109 | 287 | break; |
paul@109 | 288 | |
paul@109 | 289 | x = write_char(&display_config, &font_config, c, x, y, 0xff); |
paul@78 | 290 | } |
paul@109 | 291 | } |
paul@78 | 292 | } |
paul@78 | 293 | |
paul@98 | 294 | /* Set up a background. */ |
paul@98 | 295 | |
paul@98 | 296 | static void setup(void) |
paul@98 | 297 | { |
paul@98 | 298 | int frame; |
paul@98 | 299 | |
paul@98 | 300 | for (frame = 0; frame < display_config.frames; frame++) |
paul@98 | 301 | { |
paul@98 | 302 | /* Obtain the frame. */ |
paul@98 | 303 | |
paul@115 | 304 | display_select_frame(&display_config, frame); |
paul@98 | 305 | |
paul@98 | 306 | /* Plot the image centred on the screen. */ |
paul@98 | 307 | |
paul@118 | 308 | display_copy(&display_config, screendata.image, |
paul@118 | 309 | screendata.width, screendata.height, |
paul@101 | 310 | SOURCE_YSTEP, |
paul@118 | 311 | (display_config.line_length - screendata.width) / 2, |
paul@118 | 312 | (display_config.line_count - (screendata.height / SOURCE_YSTEP)) / 2, |
paul@101 | 313 | -1, 1); |
paul@98 | 314 | |
paul@98 | 315 | /* Write a sequence of characters. */ |
paul@98 | 316 | |
paul@98 | 317 | write_chars(); |
paul@98 | 318 | } |
paul@98 | 319 | |
paul@115 | 320 | display_select_frame(&display_config, 0); |
paul@98 | 321 | } |
paul@98 | 322 | |
paul@24 | 323 | |
paul@24 | 324 | |
paul@24 | 325 | /* Main program. */ |
paul@24 | 326 | |
paul@24 | 327 | void main(void) |
paul@24 | 328 | { |
paul@24 | 329 | init_memory(); |
paul@24 | 330 | init_pins(); |
paul@24 | 331 | init_outputs(); |
paul@24 | 332 | |
paul@24 | 333 | unlock_config(); |
paul@24 | 334 | config_oc(); |
paul@24 | 335 | config_uart(); |
paul@24 | 336 | lock_config(); |
paul@24 | 337 | |
paul@87 | 338 | #ifndef TRANSFER_CPU |
paul@24 | 339 | init_dma(); |
paul@87 | 340 | #endif |
paul@57 | 341 | |
paul@72 | 342 | #ifdef PARALLEL_MODE |
paul@72 | 343 | init_pm(); |
paul@72 | 344 | |
paul@72 | 345 | /* Configure parallel master mode. */ |
paul@72 | 346 | |
paul@72 | 347 | pm_init(0, 0b10); |
paul@72 | 348 | pm_set_output(0, 1, 0); |
paul@72 | 349 | pm_on(0); |
paul@72 | 350 | #endif |
paul@72 | 351 | |
paul@93 | 352 | uart_init(1, FPB, 115200); |
paul@93 | 353 | uart_on(1); |
paul@93 | 354 | |
paul@93 | 355 | /* Initialise memory for a double-buffered display. */ |
paul@93 | 356 | |
paul@115 | 357 | init_display(&display_config, framebuffer, LINE_LENGTH, LINE_COUNT, |
paul@115 | 358 | FRAME_COUNT, screen_starts); |
paul@93 | 359 | |
paul@59 | 360 | /* Initialise VGA output with one or two line channels, configuring a line |
paul@59 | 361 | timer and any transfer timer, with an initiating channel being introduced |
paul@59 | 362 | if a transfer timer is specified. */ |
paul@57 | 363 | |
paul@59 | 364 | init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); |
paul@24 | 365 | |
paul@59 | 366 | /* Configure VGA output transfer to the output register, also configuring |
paul@59 | 367 | output compare units for horizontal and vertical sync. */ |
paul@24 | 368 | |
paul@72 | 369 | vga_configure_transfer(VGA_OUTPUT); |
paul@59 | 370 | vga_configure_sync(1, 2); |
paul@24 | 371 | |
paul@24 | 372 | interrupts_on(); |
paul@24 | 373 | |
paul@74 | 374 | /* Move a sprite around on the screen with a delay between each movement. */ |
paul@74 | 375 | |
paul@98 | 376 | setup(); |
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 | } |