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