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