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