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