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