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(banner_config, LINE_LENGTH, 10, 1); 88 static Display(display_config, LINE_LENGTH, LINE_COUNT - 10, FRAME_COUNT); 89 90 91 92 /* Bundled image and font data. */ 93 94 extern image_t screendata; 95 extern image_t sprite; 96 97 static SpriteOverwriting(scr, &screendata, &display_config, FRAME_COUNT, -1, SOURCE_YSTEP); 98 99 extern uint8_t fontchars[]; 100 extern font_range_t fontbase; 101 102 static Font(font_config, fontchars, &fontbase); 103 104 105 106 /* Plot the revealed region at the edge of the screen after scrolling. */ 107 108 static void plot_screen_edge(int xorigin, int yorigin, int xstep, int ystep) 109 { 110 image_update_scrolled_tiled_image(&scr, xorigin, yorigin, xstep, ystep); 111 } 112 113 114 115 /* Update the sprite position. */ 116 117 static void update_sprite_position(sprite_t *s, int *xdir, int *ydir) 118 { 119 position_t *last = image_get_stored_position(s, wrap_value(display_config.frame - 1, display_config.frames)); 120 position_t *next = image_get_sprite_position(s); 121 122 if (((*xdir > 0) && (last->x < display_config.line_length - s->image->width - *xdir)) || 123 ((*xdir < 0) && (last->x > -*xdir))) 124 { 125 next->x = last->x + *xdir; 126 } 127 else 128 { 129 next->x = last->x; 130 if (*xdir) 131 *xdir = -*xdir; 132 } 133 134 if (((*ydir > 0) && (last->y < display_config.line_count - s->image->height / s->yscale - *ydir)) || 135 ((*ydir < 0) && (last->y > -*ydir))) 136 { 137 next->y = last->y + *ydir; 138 } 139 else 140 { 141 next->y = last->y; 142 if (*ydir) 143 *ydir = -*ydir; 144 } 145 } 146 147 /* Move a sprite around on the framebuffer. */ 148 149 static void animate(uint32_t delay) 150 { 151 /* Stores of background details, replotted when moving the sprite. */ 152 153 Sprite(s1, &sprite, &display_config, FRAME_COUNT, 0x0c, SOURCE_YSTEP); 154 Sprite(s2, &sprite, &display_config, FRAME_COUNT, 0x0c, SOURCE_YSTEP); 155 Sprite(s3, &sprite, &display_config, FRAME_COUNT, 0x0c, SOURCE_YSTEP); 156 157 /* Scrolling viewport. */ 158 159 Viewport(v, &display_config, SCROLL_XSTEP, SOURCE_YSTEP, plot_screen_edge); 160 161 /* Reset the viewport to a well-defined state. */ 162 163 viewport_set_origin(&v, 0, 0); 164 165 /* Sprite and scrolling directions. */ 166 167 int xdir[] = {1, -1, 0}, ydir[] = {1, 0, -1}; 168 169 /* Sprite positions. */ 170 171 image_set_sprite_position(&s1, 0, 0); 172 image_set_sprite_position(&s2, display_config.line_length - s2.image->width, 0); 173 image_set_sprite_position(&s3, 0, display_config.line_count - s3.image->height); 174 175 /* Animation loop. */ 176 177 while (1) 178 { 179 image_plot_sprite(&s1); 180 image_plot_sprite(&s2); 181 image_plot_sprite(&s3); 182 183 /* Update the display with the frame details. */ 184 185 vga_set_frame(&display_config, 1); 186 wait(delay); 187 188 /* Select the next frame to plot to. */ 189 190 display_select_next_frame(&display_config); 191 192 /* Prepare the frame for updates. */ 193 194 viewport_unplot_sprite_from_image(&v, &s1, &scr); 195 viewport_unplot_sprite_from_image(&v, &s2, &scr); 196 viewport_unplot_sprite_from_image(&v, &s3, &scr); 197 198 /* Update the origin to scroll the screen. */ 199 200 /* Due to the effect of a simple screen start increment in the 201 dual channel configuration, horizontal scrolling involves two 202 pixel increments and thus requires a two-pixel column to be 203 plotted per scrolling increment. */ 204 205 /* For vertically-scaled backgrounds, the full resolution image 206 is traversed by multiples of the scrolling increment. */ 207 208 viewport_update_origin(&v, xdir[0] * SCROLL_XSTEP, 209 ydir[0] * SOURCE_YSTEP); 210 211 /* Update the position of the sprite being plotted. */ 212 213 update_sprite_position(&s1, &xdir[0], &ydir[0]); 214 update_sprite_position(&s2, &xdir[1], &ydir[1]); 215 update_sprite_position(&s3, &xdir[2], &ydir[2]); 216 } 217 } 218 219 /* Set up a banner. */ 220 221 static void setup_banner(void) 222 { 223 int frame, x = 0, y = 0; 224 225 for (frame = 0; frame < banner_config.frames; frame++) 226 { 227 /* Obtain the frame. */ 228 229 display_select_frame(&banner_config, frame); 230 231 write_string(&banner_config, &font_config, "VGA display example", &x, &y, 0xff); 232 } 233 234 display_select_frame(&banner_config, 0); 235 } 236 237 /* Set up a background. */ 238 239 static void setup_background(void) 240 { 241 int frame; 242 243 for (frame = 0; frame < display_config.frames; frame++) 244 { 245 /* Obtain the frame. */ 246 247 display_select_frame(&display_config, frame); 248 249 /* Tile the image on the screen. */ 250 251 image_tile_sprite(&scr, 0, 0, display_config.line_length, 252 display_config.line_count * scr.yscale, 253 0, 0); 254 } 255 256 display_select_frame(&display_config, 0); 257 } 258 259 static void setup(void) 260 { 261 setup_banner(); 262 setup_background(); 263 } 264 265 266 267 /* Main program. */ 268 269 void main(void) 270 { 271 init_memory(); 272 init_pins(); 273 init_outputs(); 274 275 unlock_config(); 276 config_oc(); 277 config_uart(); 278 lock_config(); 279 280 #ifndef TRANSFER_CPU 281 init_dma(); 282 #endif 283 284 #ifdef PARALLEL_MODE 285 init_pm(); 286 287 /* Configure parallel master mode. */ 288 289 pm_init(0, 0b10); 290 pm_set_output(0, 1, 0); 291 pm_on(0); 292 #endif 293 294 uart_init(1, FPB, 115200); 295 uart_on(1); 296 297 /* Set the display to a well-defined state. */ 298 299 init_display(&banner_config); 300 init_display(&display_config); 301 302 /* Initialise VGA output with one or two line channels, configuring a line 303 timer and any transfer timer, with an initiating channel being introduced 304 if a transfer timer is specified. */ 305 306 init_vga_with_timers(&banner_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); 307 vga_add_window(&display_config); 308 309 /* Configure VGA output transfer to the output register, also configuring 310 output compare units for horizontal and vertical sync. */ 311 312 vga_configure_transfer(VGA_OUTPUT); 313 vga_configure_sync(1, 2); 314 315 interrupts_on(); 316 317 /* Move a sprite around on the screen with a delay between each movement. */ 318 319 setup(); 320 animate(1); 321 } 322 323 324 325 /* Exception and interrupt handlers. */ 326 327 void exception_handler(void) 328 { 329 blink(1 << 22, PORTA, LED_PIN); 330 } 331 332 void interrupt_handler(void) 333 { 334 vga_interrupt_handler(); 335 } 336 337 338 339 /* Peripheral pin configuration. */ 340 341 void config_oc(void) 342 { 343 /* Map OC1. */ 344 345 REG(OC1_PIN) = 0b0101; 346 347 /* Map OC2. */ 348 349 REG(OC2_PIN) = 0b0101; 350 } 351 352 void config_uart(void) 353 { 354 /* Map U1RX to RPB13. */ 355 356 REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ 357 358 /* Map U1TX to RPB15. */ 359 360 REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ 361 362 /* Set RPB13 to input. */ 363 364 SET_REG(TRISB, 1 << 13); 365 }