paul@24 | 1 | /* |
paul@24 | 2 | * Generate a VGA signal using a PIC32 microcontroller. |
paul@24 | 3 | * |
paul@24 | 4 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@24 | 5 | * |
paul@24 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@24 | 7 | * it under the terms of the GNU General Public License as published by |
paul@24 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@24 | 9 | * (at your option) any later version. |
paul@24 | 10 | * |
paul@24 | 11 | * This program is distributed in the hope that it will be useful, |
paul@24 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@24 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@24 | 14 | * GNU General Public License for more details. |
paul@24 | 15 | * |
paul@24 | 16 | * You should have received a copy of the GNU General Public License |
paul@24 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@24 | 18 | */ |
paul@24 | 19 | |
paul@24 | 20 | |
paul@24 | 21 | #include "pic32_c.h" |
paul@24 | 22 | #include "init.h" |
paul@24 | 23 | #include "debug.h" |
paul@41 | 24 | |
paul@41 | 25 | /* Specific functionality. */ |
paul@41 | 26 | |
paul@47 | 27 | #include "devconfig.h" |
paul@41 | 28 | #include "display.h" |
paul@49 | 29 | #include "display_config.h" |
paul@78 | 30 | #include "font.h" |
paul@78 | 31 | #include "main.h" |
paul@78 | 32 | #include "vga.h" |
paul@50 | 33 | #include "vga_display.h" |
paul@24 | 34 | |
paul@24 | 35 | |
paul@41 | 36 | |
paul@59 | 37 | /* Define timers if not indicated in the build configuration. */ |
paul@59 | 38 | |
paul@59 | 39 | #ifndef LINE_CHANNELS |
paul@59 | 40 | #define LINE_CHANNELS 1 |
paul@59 | 41 | #endif |
paul@59 | 42 | |
paul@59 | 43 | #ifndef LINE_TIMER |
paul@59 | 44 | #define LINE_TIMER 2 |
paul@59 | 45 | #endif |
paul@59 | 46 | |
paul@59 | 47 | #ifndef TRANSFER_TIMER |
paul@59 | 48 | #define TRANSFER_TIMER 0 |
paul@59 | 49 | #endif |
paul@59 | 50 | |
paul@72 | 51 | /* Define different output ports and pins for parallel mode. */ |
paul@72 | 52 | |
paul@72 | 53 | #ifndef PARALLEL_MODE |
paul@72 | 54 | #define VGA_OUTPUT PORTB |
paul@72 | 55 | #define OC1_PIN RPA0R |
paul@72 | 56 | #define OC2_PIN RPA1R |
paul@72 | 57 | #define LED_PIN (1 << 3) |
paul@72 | 58 | #else |
paul@72 | 59 | #define VGA_OUTPUT PM_REG(0, PMxDIN) |
paul@72 | 60 | #define OC1_PIN RPB4R |
paul@72 | 61 | #define OC2_PIN RPB5R |
paul@72 | 62 | #define LED_PIN (1 << 2) |
paul@72 | 63 | #endif |
paul@72 | 64 | |
paul@59 | 65 | |
paul@59 | 66 | |
paul@78 | 67 | /* Bundled image and font data. */ |
paul@69 | 68 | |
paul@69 | 69 | extern uint8_t screendata[]; |
paul@69 | 70 | extern uint32_t screendata_width, screendata_height; |
paul@74 | 71 | extern uint8_t sprite[]; |
paul@74 | 72 | extern uint32_t sprite_width, sprite_height; |
paul@74 | 73 | |
paul@78 | 74 | extern uint8_t fontchars[]; |
paul@78 | 75 | extern uint32_t fonttable[]; |
paul@78 | 76 | extern uint32_t fontbase, fontlimit; |
paul@78 | 77 | |
paul@78 | 78 | static font_config_t font_config; |
paul@78 | 79 | |
paul@69 | 80 | |
paul@69 | 81 | |
paul@74 | 82 | /* Busy wait. */ |
paul@74 | 83 | |
paul@74 | 84 | static void wait(uint32_t delay) |
paul@74 | 85 | { |
paul@74 | 86 | uint32_t counter = delay; |
paul@74 | 87 | |
paul@74 | 88 | if (!delay) return; |
paul@74 | 89 | while (counter--) __asm__(""); /* retain loop */ |
paul@74 | 90 | } |
paul@69 | 91 | |
paul@24 | 92 | /* Blink an attached LED with delays implemented using a loop. */ |
paul@24 | 93 | |
paul@24 | 94 | static void blink(uint32_t delay, uint32_t port, uint32_t pins) |
paul@24 | 95 | { |
paul@24 | 96 | /* Clear outputs (LED). */ |
paul@24 | 97 | |
paul@24 | 98 | CLR_REG(port, pins); |
paul@24 | 99 | |
paul@24 | 100 | while (1) |
paul@24 | 101 | { |
paul@74 | 102 | wait(delay); |
paul@24 | 103 | |
paul@24 | 104 | /* Invert outputs (LED). */ |
paul@24 | 105 | |
paul@24 | 106 | INV_REG(port, pins); |
paul@24 | 107 | } |
paul@24 | 108 | } |
paul@24 | 109 | |
paul@74 | 110 | /* Move a sprite around on the framebuffer. */ |
paul@74 | 111 | |
paul@74 | 112 | static void animate(uint32_t delay) |
paul@74 | 113 | { |
paul@74 | 114 | uint8_t background[sprite_width * sprite_height]; |
paul@74 | 115 | int x, y; |
paul@83 | 116 | int dir[] = {1, 0, -1, 0, 1}, i = 0, width, column_width; |
paul@83 | 117 | int xsource, xdisplay, xorigin = 0; |
paul@74 | 118 | |
paul@74 | 119 | while (1) |
paul@83 | 120 | { |
paul@74 | 121 | for (y = 0; y < screendata_height - sprite_height; y++) |
paul@83 | 122 | { |
paul@74 | 123 | for (x = 0; x < screendata_width - sprite_width; x++) |
paul@74 | 124 | { |
paul@74 | 125 | /* Copy to the store from the display, then blit the image. */ |
paul@74 | 126 | |
paul@83 | 127 | copy_display(&display_config, background, |
paul@83 | 128 | sprite_width, sprite_height, |
paul@83 | 129 | x, y, -1, 0); |
paul@83 | 130 | copy_display(&display_config, sprite, |
paul@83 | 131 | sprite_width, sprite_height, |
paul@83 | 132 | x, y, 0x8c, 1); |
paul@74 | 133 | |
paul@74 | 134 | wait(delay); |
paul@74 | 135 | |
paul@74 | 136 | /* Copy to the display from the store, restoring the original |
paul@74 | 137 | background. */ |
paul@74 | 138 | |
paul@83 | 139 | copy_display(&display_config, background, |
paul@83 | 140 | sprite_width, sprite_height, |
paul@83 | 141 | x, y, -1, 1); |
paul@83 | 142 | |
paul@83 | 143 | /* Scroll in the indicated direction. */ |
paul@83 | 144 | |
paul@83 | 145 | scroll_display(&display_config, dir[i], dir[i + 1]); |
paul@83 | 146 | |
paul@83 | 147 | /* For horizontal scrolling, plot the exposed column at the left |
paul@83 | 148 | (if scrolling left) or at the right (if scrolling right). */ |
paul@83 | 149 | |
paul@83 | 150 | if (dir[i]) |
paul@83 | 151 | { |
paul@83 | 152 | /* Due to the effect of a simple screen start increment in |
paul@83 | 153 | the dual channel configuration, horizontal scrolling |
paul@83 | 154 | involves two pixel increments and thus requires a two- |
paul@83 | 155 | pixel column to be plotted. */ |
paul@83 | 156 | |
paul@83 | 157 | width = dir[i] * LINE_CHANNELS; |
paul@83 | 158 | column_width = width < 0 ? -width : width; |
paul@83 | 159 | |
paul@83 | 160 | /* Plot either at the left or right edge. */ |
paul@83 | 161 | |
paul@83 | 162 | xdisplay = width < 0 ? 0 : screendata_width - width; |
paul@83 | 163 | |
paul@83 | 164 | /* Determine the location of the column to be plotted. */ |
paul@83 | 165 | |
paul@83 | 166 | xorigin += width; |
paul@83 | 167 | xsource = (xdisplay + xorigin) % screendata_width; |
paul@83 | 168 | |
paul@83 | 169 | copy_display_section(&display_config, screendata, |
paul@83 | 170 | screendata_width, screendata_height, |
paul@83 | 171 | xsource, 0, |
paul@83 | 172 | column_width, screendata_height, |
paul@83 | 173 | xdisplay, 0, |
paul@83 | 174 | -1, 1); |
paul@83 | 175 | } |
paul@74 | 176 | } |
paul@83 | 177 | |
paul@83 | 178 | /* Switch direction periodically. */ |
paul@83 | 179 | |
paul@83 | 180 | i++; |
paul@83 | 181 | if (i == 4) |
paul@83 | 182 | i = 0; |
paul@83 | 183 | } |
paul@83 | 184 | } |
paul@74 | 185 | } |
paul@74 | 186 | |
paul@78 | 187 | /* Fill the screen with characters. */ |
paul@78 | 188 | |
paul@78 | 189 | static void write_chars(void) |
paul@78 | 190 | { |
paul@78 | 191 | int x = 0, y = 0; |
paul@78 | 192 | char c; |
paul@78 | 193 | |
paul@78 | 194 | font_config.chars = (char_definition_t *) fontchars; |
paul@78 | 195 | font_config.table = fonttable; |
paul@78 | 196 | font_config.base = fontbase; |
paul@78 | 197 | font_config.limit = fontlimit; |
paul@78 | 198 | |
paul@78 | 199 | while (y < display_config.line_count) |
paul@78 | 200 | for (c = (char) font_config.base; c < (char) font_config.limit; c++) |
paul@78 | 201 | { |
paul@78 | 202 | x = write_char(&display_config, &font_config, c, x, y, 0xff); |
paul@78 | 203 | |
paul@78 | 204 | if (x > display_config.line_length) |
paul@78 | 205 | { |
paul@78 | 206 | x = 0; y += 9; |
paul@78 | 207 | } |
paul@78 | 208 | } |
paul@78 | 209 | } |
paul@78 | 210 | |
paul@24 | 211 | |
paul@24 | 212 | |
paul@24 | 213 | /* Main program. */ |
paul@24 | 214 | |
paul@24 | 215 | void main(void) |
paul@24 | 216 | { |
paul@24 | 217 | init_memory(); |
paul@24 | 218 | init_pins(); |
paul@24 | 219 | init_outputs(); |
paul@24 | 220 | |
paul@24 | 221 | unlock_config(); |
paul@24 | 222 | config_oc(); |
paul@24 | 223 | config_uart(); |
paul@24 | 224 | lock_config(); |
paul@24 | 225 | |
paul@24 | 226 | init_dma(); |
paul@57 | 227 | |
paul@72 | 228 | #ifdef PARALLEL_MODE |
paul@72 | 229 | init_pm(); |
paul@72 | 230 | |
paul@72 | 231 | /* Configure parallel master mode. */ |
paul@72 | 232 | |
paul@72 | 233 | pm_init(0, 0b10); |
paul@72 | 234 | pm_set_output(0, 1, 0); |
paul@72 | 235 | pm_on(0); |
paul@72 | 236 | #endif |
paul@72 | 237 | |
paul@59 | 238 | /* Initialise VGA output with one or two line channels, configuring a line |
paul@59 | 239 | timer and any transfer timer, with an initiating channel being introduced |
paul@59 | 240 | if a transfer timer is specified. */ |
paul@57 | 241 | |
paul@59 | 242 | init_vga_with_timers(&display_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); |
paul@24 | 243 | |
paul@59 | 244 | /* Configure VGA output transfer to the output register, also configuring |
paul@59 | 245 | output compare units for horizontal and vertical sync. */ |
paul@24 | 246 | |
paul@72 | 247 | vga_configure_transfer(VGA_OUTPUT); |
paul@59 | 248 | vga_configure_sync(1, 2); |
paul@24 | 249 | |
paul@47 | 250 | uart_init(1, FPB, 115200); |
paul@24 | 251 | uart_on(1); |
paul@24 | 252 | |
paul@24 | 253 | interrupts_on(); |
paul@24 | 254 | |
paul@74 | 255 | /* Plot the image centred on the screen. */ |
paul@74 | 256 | |
paul@74 | 257 | copy_display(&display_config, screendata, screendata_width, screendata_height, |
paul@83 | 258 | (LINE_LENGTH - screendata_width) / 2, |
paul@83 | 259 | (LINE_COUNT - screendata_height) / 2, -1, 1); |
paul@74 | 260 | |
paul@78 | 261 | /* Write a sequence of characters. */ |
paul@78 | 262 | |
paul@78 | 263 | write_chars(); |
paul@78 | 264 | |
paul@74 | 265 | /* Move a sprite around on the screen with a delay between each movement. */ |
paul@74 | 266 | |
paul@83 | 267 | animate(1 << 21); |
paul@24 | 268 | } |
paul@24 | 269 | |
paul@24 | 270 | |
paul@24 | 271 | |
paul@24 | 272 | /* Exception and interrupt handlers. */ |
paul@24 | 273 | |
paul@24 | 274 | void exception_handler(void) |
paul@24 | 275 | { |
paul@79 | 276 | blink(1 << 22, PORTA, LED_PIN); |
paul@24 | 277 | } |
paul@24 | 278 | |
paul@24 | 279 | void interrupt_handler(void) |
paul@24 | 280 | { |
paul@24 | 281 | uint32_t ifs; |
paul@24 | 282 | |
paul@24 | 283 | /* Check for a OC1 interrupt condition. */ |
paul@24 | 284 | |
paul@24 | 285 | ifs = REG(OCIFS) & OC_INT_FLAGS(1, OCxIF); |
paul@24 | 286 | |
paul@24 | 287 | if (ifs) |
paul@24 | 288 | { |
paul@50 | 289 | vga_interrupt_handler(); |
paul@24 | 290 | CLR_REG(OCIFS, ifs); |
paul@24 | 291 | } |
paul@24 | 292 | } |
paul@24 | 293 | |
paul@24 | 294 | |
paul@24 | 295 | |
paul@24 | 296 | /* Peripheral pin configuration. */ |
paul@24 | 297 | |
paul@24 | 298 | void config_oc(void) |
paul@24 | 299 | { |
paul@72 | 300 | /* Map OC1. */ |
paul@24 | 301 | |
paul@72 | 302 | REG(OC1_PIN) = 0b0101; |
paul@24 | 303 | |
paul@72 | 304 | /* Map OC2. */ |
paul@24 | 305 | |
paul@72 | 306 | REG(OC2_PIN) = 0b0101; |
paul@24 | 307 | } |
paul@24 | 308 | |
paul@24 | 309 | void config_uart(void) |
paul@24 | 310 | { |
paul@24 | 311 | /* Map U1RX to RPB13. */ |
paul@24 | 312 | |
paul@24 | 313 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@24 | 314 | |
paul@24 | 315 | /* Map U1TX to RPB15. */ |
paul@24 | 316 | |
paul@24 | 317 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@24 | 318 | |
paul@24 | 319 | /* Set RPB13 to input. */ |
paul@24 | 320 | |
paul@24 | 321 | SET_REG(TRISB, 1 << 13); |
paul@24 | 322 | } |