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 "pic32_c.h" 22 #include "init.h" 23 #include "debug.h" 24 25 /* Specific functionality. */ 26 27 #include "devconfig.h" 28 #include "display.h" 29 #include "display_config.h" 30 #include "font.h" 31 #include "main.h" 32 #include "vga.h" 33 #include "vga_display.h" 34 35 36 37 /* Define timers if not indicated in the build configuration. */ 38 39 #ifndef LINE_CHANNELS 40 #define LINE_CHANNELS 1 41 #endif 42 43 #ifndef LINE_TIMER 44 #define LINE_TIMER 2 45 #endif 46 47 #ifndef TRANSFER_TIMER 48 #define TRANSFER_TIMER 0 49 #endif 50 51 /* Define different output ports and pins for parallel mode. */ 52 53 #ifndef PARALLEL_MODE 54 #define VGA_OUTPUT PORTB 55 #define OC1_PIN RPA0R 56 #define OC2_PIN RPA1R 57 #define LED_PIN (1 << 3) 58 #else 59 #define VGA_OUTPUT PM_REG(0, PMxDIN) 60 #define OC1_PIN RPB4R 61 #define OC2_PIN RPB5R 62 #define LED_PIN (1 << 2) 63 #endif 64 65 66 67 /* Bundled image and font data. */ 68 69 extern uint8_t screendata[]; 70 extern uint32_t screendata_width, screendata_height; 71 extern uint8_t sprite[]; 72 extern uint32_t sprite_width, sprite_height; 73 74 extern uint8_t fontchars[]; 75 extern uint32_t fonttable[]; 76 extern uint32_t fontbase, fontlimit; 77 78 static font_config_t font_config; 79 80 81 82 /* Busy wait. */ 83 84 static void wait(uint32_t delay) 85 { 86 uint32_t counter = delay; 87 88 if (!delay) return; 89 while (counter--) __asm__(""); /* retain loop */ 90 } 91 92 /* Blink an attached LED with delays implemented using a loop. */ 93 94 static void blink(uint32_t delay, uint32_t port, uint32_t pins) 95 { 96 /* Clear outputs (LED). */ 97 98 CLR_REG(port, pins); 99 100 while (1) 101 { 102 wait(delay); 103 104 /* Invert outputs (LED). */ 105 106 INV_REG(port, pins); 107 } 108 } 109 110 /* Move a sprite around on the framebuffer. */ 111 112 static void animate(uint32_t delay) 113 { 114 uint8_t background[sprite_width * sprite_height]; 115 int x, y; 116 117 while (1) 118 for (y = 0; y < screendata_height - sprite_height; y++) 119 for (x = 0; x < screendata_width - sprite_width; x++) 120 { 121 /* Copy to the store from the display, then blit the image. */ 122 123 copy_display(&display_config, background, sprite_width, sprite_height, x, y, -1, 0); 124 copy_display(&display_config, sprite, sprite_width, sprite_height, x, y, 0x8c, 1); 125 126 wait(delay); 127 128 /* Copy to the display from the store, restoring the original 129 background. */ 130 131 copy_display(&display_config, background, sprite_width, sprite_height, x, y, -1, 1); 132 } 133 } 134 135 /* Fill the screen with characters. */ 136 137 static void write_chars(void) 138 { 139 int x = 0, y = 0; 140 char c; 141 142 font_config.chars = (char_definition_t *) fontchars; 143 font_config.table = fonttable; 144 font_config.base = fontbase; 145 font_config.limit = fontlimit; 146 147 while (y < display_config.line_count) 148 for (c = (char) font_config.base; c < (char) font_config.limit; c++) 149 { 150 x = write_char(&display_config, &font_config, c, x, y, 0xff); 151 152 if (x > display_config.line_length) 153 { 154 x = 0; y += 9; 155 } 156 } 157 } 158 159 160 161 /* Main program. */ 162 163 void main(void) 164 { 165 init_memory(); 166 init_pins(); 167 init_outputs(); 168 169 unlock_config(); 170 config_oc(); 171 config_uart(); 172 lock_config(); 173 174 init_dma(); 175 176 #ifdef PARALLEL_MODE 177 init_pm(); 178 179 /* Configure parallel master mode. */ 180 181 pm_init(0, 0b10); 182 pm_set_output(0, 1, 0); 183 pm_on(0); 184 #endif 185 186 /* Initialise VGA output with one or two line channels, configuring a line 187 timer and any transfer timer, with an initiating channel being introduced 188 if a transfer timer is specified. */ 189 190 init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); 191 192 /* Configure VGA output transfer to the output register, also configuring 193 output compare units for horizontal and vertical sync. */ 194 195 vga_configure_transfer(VGA_OUTPUT); 196 vga_configure_sync(1, 2); 197 198 uart_init(1, FPB, 115200); 199 uart_on(1); 200 201 interrupts_on(); 202 203 /* Plot the image centred on the screen. */ 204 205 copy_display(&display_config, screendata, screendata_width, screendata_height, 206 (LINE_LENGTH - screendata_width) / 2, (LINE_COUNT - screendata_height) / 2, -1, 1); 207 208 /* Write a sequence of characters. */ 209 210 write_chars(); 211 212 /* Move a sprite around on the screen with a delay between each movement. */ 213 214 animate(1 << 24); 215 } 216 217 218 219 /* Exception and interrupt handlers. */ 220 221 void exception_handler(void) 222 { 223 blink(1 << 22, PORTA, LED_PIN); 224 } 225 226 void interrupt_handler(void) 227 { 228 uint32_t ifs; 229 230 /* Check for a OC1 interrupt condition. */ 231 232 ifs = REG(OCIFS) & OC_INT_FLAGS(1, OCxIF); 233 234 if (ifs) 235 { 236 vga_interrupt_handler(); 237 CLR_REG(OCIFS, ifs); 238 } 239 } 240 241 242 243 /* Peripheral pin configuration. */ 244 245 void config_oc(void) 246 { 247 /* Map OC1. */ 248 249 REG(OC1_PIN) = 0b0101; 250 251 /* Map OC2. */ 252 253 REG(OC2_PIN) = 0b0101; 254 } 255 256 void config_uart(void) 257 { 258 /* Map U1RX to RPB13. */ 259 260 REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ 261 262 /* Map U1TX to RPB15. */ 263 264 REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ 265 266 /* Set RPB13 to input. */ 267 268 SET_REG(TRISB, 1 << 13); 269 }