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(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 /* Move a sprite around on the framebuffer. */ 116 117 static void animate(uint32_t delay) 118 { 119 /* Stores of background details, replotted when moving the sprite. */ 120 121 Sprite(s, &sprite, &display_config, SOURCE_YSTEP); 122 123 /* Sprite position. */ 124 125 int x, y; 126 127 /* Scrolling directions. */ 128 129 int dir[] = {1, 0, -1, 0, 1}; 130 int dirindex = 0; 131 132 /* Scroll increments. */ 133 134 int xdir, ydir; 135 136 /* Scrolling viewport. */ 137 138 Viewport(v, &display_config, SCROLL_XSTEP, SOURCE_YSTEP, plot_screen_edge); 139 140 /* Reset the viewport to a well-defined state. */ 141 142 viewport_set_origin(&v, 0, 0); 143 144 /* Animation loop. */ 145 146 while (1) 147 { 148 for (y = 0; y < display_config.line_count - s.image->height; y++) 149 { 150 for (x = 0; x < display_config.line_length - s.image->width; x++) 151 { 152 image_plot_sprite(&s, x, y, 0x8c); 153 154 /* Update the display with the frame details. */ 155 156 vga_set_frame(&display_config); 157 wait(delay); 158 159 /* Select the next frame to plot to. */ 160 161 display_select_next_frame(&display_config); 162 163 /* Prepare the frame for updates. */ 164 165 image_unplot_sprite(&s); 166 167 /* Scroll in the indicated direction. */ 168 169 xdir = dir[dirindex]; 170 ydir = dir[dirindex + 1]; 171 172 /* Update the origin if appropriate. */ 173 174 /* Due to the effect of a simple screen start increment in the 175 dual channel configuration, horizontal scrolling involves two 176 pixel increments and thus requires a two-pixel column to be 177 plotted per scrolling increment. */ 178 179 /* For vertically-scaled backgrounds, the full resolution image 180 is traversed by multiples of the scrolling increment. */ 181 182 viewport_update_origin(&v, xdir * SCROLL_XSTEP, 183 ydir * SOURCE_YSTEP); 184 } 185 186 /* Switch direction periodically. */ 187 188 dirindex = wrap_value(dirindex + 1, 4); 189 } 190 } 191 } 192 193 /* Fill the screen with characters. */ 194 195 static void write_chars(void) 196 { 197 const int line_height = 9; 198 int x = 0, y = 0; 199 char c; 200 201 init_font(&font_config, fontchars, fonttable, fontbase, fontlimit); 202 203 while (y + line_height < display_config.line_count) 204 { 205 for (c = (char) font_config.base; c < (char) font_config.limit; c++) 206 { 207 if (x + get_char_definition(&font_config, c)->width > display_config.line_length) 208 { 209 x = 0; y += line_height; 210 } 211 212 if (y + line_height >= display_config.line_count) 213 break; 214 215 x = write_char(&display_config, &font_config, c, x, y, 0xff); 216 } 217 } 218 } 219 220 /* Set up a background. */ 221 222 static void setup(void) 223 { 224 int frame; 225 226 for (frame = 0; frame < display_config.frames; frame++) 227 { 228 /* Obtain the frame. */ 229 230 display_select_frame(&display_config, frame); 231 232 /* Tile the image on the screen. */ 233 234 image_tile_sprite(&scr, 0, 0, display_config.line_length, 235 display_config.line_count * scr.yscale, 236 0, 0); 237 238 /* Write a sequence of characters. */ 239 240 write_chars(); 241 } 242 243 display_select_frame(&display_config, 0); 244 } 245 246 247 248 /* Main program. */ 249 250 void main(void) 251 { 252 init_memory(); 253 init_pins(); 254 init_outputs(); 255 256 unlock_config(); 257 config_oc(); 258 config_uart(); 259 lock_config(); 260 261 #ifndef TRANSFER_CPU 262 init_dma(); 263 #endif 264 265 #ifdef PARALLEL_MODE 266 init_pm(); 267 268 /* Configure parallel master mode. */ 269 270 pm_init(0, 0b10); 271 pm_set_output(0, 1, 0); 272 pm_on(0); 273 #endif 274 275 uart_init(1, FPB, 115200); 276 uart_on(1); 277 278 /* Set the display to a well-defined state. */ 279 280 init_display(&display_config); 281 282 /* Initialise VGA output with one or two line channels, configuring a line 283 timer and any transfer timer, with an initiating channel being introduced 284 if a transfer timer is specified. */ 285 286 init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); 287 288 /* Configure VGA output transfer to the output register, also configuring 289 output compare units for horizontal and vertical sync. */ 290 291 vga_configure_transfer(VGA_OUTPUT); 292 vga_configure_sync(1, 2); 293 294 interrupts_on(); 295 296 /* Move a sprite around on the screen with a delay between each movement. */ 297 298 setup(); 299 animate(1 << 18); 300 } 301 302 303 304 /* Exception and interrupt handlers. */ 305 306 void exception_handler(void) 307 { 308 blink(1 << 22, PORTA, LED_PIN); 309 } 310 311 void interrupt_handler(void) 312 { 313 vga_interrupt_handler(); 314 } 315 316 317 318 /* Peripheral pin configuration. */ 319 320 void config_oc(void) 321 { 322 /* Map OC1. */ 323 324 REG(OC1_PIN) = 0b0101; 325 326 /* Map OC2. */ 327 328 REG(OC2_PIN) = 0b0101; 329 } 330 331 void config_uart(void) 332 { 333 /* Map U1RX to RPB13. */ 334 335 REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ 336 337 /* Map U1TX to RPB15. */ 338 339 REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ 340 341 /* Set RPB13 to input. */ 342 343 SET_REG(TRISB, 1 << 13); 344 }