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