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@108 | 21 | #include "debug.h" |
paul@108 | 22 | #include "display.h" |
paul@108 | 23 | #include "font.h" |
paul@118 | 24 | #include "image.h" |
paul@24 | 25 | #include "init.h" |
paul@108 | 26 | #include "pic32_c.h" |
paul@108 | 27 | #include "utils.h" |
paul@116 | 28 | #include "viewport.h" |
paul@41 | 29 | |
paul@41 | 30 | /* Specific functionality. */ |
paul@41 | 31 | |
paul@47 | 32 | #include "devconfig.h" |
paul@78 | 33 | #include "main.h" |
paul@125 | 34 | #include "vga_display.h" |
paul@87 | 35 | |
paul@88 | 36 | |
paul@88 | 37 | |
paul@125 | 38 | /* Define the scroll increment depending on how many DMA channels are used. |
paul@125 | 39 | CPU-driven transfers permit an increment of 1. */ |
paul@88 | 40 | |
paul@88 | 41 | #ifdef TRANSFER_CPU |
paul@88 | 42 | #define SCROLL_XSTEP 1 |
paul@87 | 43 | #else |
paul@88 | 44 | #define SCROLL_XSTEP LINE_CHANNELS |
paul@87 | 45 | #endif |
paul@24 | 46 | |
paul@24 | 47 | |
paul@41 | 48 | |
paul@59 | 49 | /* Define timers if not indicated in the build configuration. */ |
paul@59 | 50 | |
paul@59 | 51 | #ifndef LINE_TIMER |
paul@59 | 52 | #define LINE_TIMER 2 |
paul@59 | 53 | #endif |
paul@59 | 54 | |
paul@59 | 55 | #ifndef TRANSFER_TIMER |
paul@59 | 56 | #define TRANSFER_TIMER 0 |
paul@59 | 57 | #endif |
paul@59 | 58 | |
paul@72 | 59 | /* Define different output ports and pins for parallel mode. */ |
paul@72 | 60 | |
paul@72 | 61 | #ifndef PARALLEL_MODE |
paul@72 | 62 | #define VGA_OUTPUT PORTB |
paul@72 | 63 | #define OC1_PIN RPA0R |
paul@72 | 64 | #define OC2_PIN RPA1R |
paul@72 | 65 | #define LED_PIN (1 << 3) |
paul@72 | 66 | #else |
paul@72 | 67 | #define VGA_OUTPUT PM_REG(0, PMxDIN) |
paul@72 | 68 | #define OC1_PIN RPB4R |
paul@72 | 69 | #define OC2_PIN RPB5R |
paul@72 | 70 | #define LED_PIN (1 << 2) |
paul@72 | 71 | #endif |
paul@72 | 72 | |
paul@59 | 73 | |
paul@59 | 74 | |
paul@101 | 75 | /* Define the relationship between source images and the screen. */ |
paul@101 | 76 | |
paul@101 | 77 | #ifndef SOURCE_HEIGHT |
paul@101 | 78 | #define SOURCE_HEIGHT 256 |
paul@101 | 79 | #endif |
paul@101 | 80 | |
paul@101 | 81 | #define SOURCE_YSTEP (SOURCE_HEIGHT / LINE_COUNT) |
paul@101 | 82 | |
paul@101 | 83 | |
paul@101 | 84 | |
paul@123 | 85 | /* Initialise memory for a multi-frame display. */ |
paul@93 | 86 | |
paul@133 | 87 | static Display(banner_config, LINE_LENGTH, 10, 1); |
paul@133 | 88 | static Display(display_config, LINE_LENGTH, LINE_COUNT - 10, FRAME_COUNT); |
paul@115 | 89 | |
paul@93 | 90 | |
paul@93 | 91 | |
paul@78 | 92 | /* Bundled image and font data. */ |
paul@69 | 93 | |
paul@118 | 94 | extern image_t screendata; |
paul@118 | 95 | extern image_t sprite; |
paul@74 | 96 | |
paul@136 | 97 | static SpriteOverwriting(scr, &screendata, &display_config, FRAME_COUNT, SOURCE_YSTEP); |
paul@128 | 98 | |
paul@78 | 99 | extern uint8_t fontchars[]; |
paul@135 | 100 | extern font_range_t fontbase; |
paul@78 | 101 | |
paul@135 | 102 | static Font(font_config, fontchars, &fontbase); |
paul@78 | 103 | |
paul@69 | 104 | |
paul@69 | 105 | |
paul@94 | 106 | /* Plot the revealed region at the edge of the screen after scrolling. */ |
paul@94 | 107 | |
paul@129 | 108 | static void plot_screen_edge(int xorigin, int yorigin, int xstep, int ystep) |
paul@94 | 109 | { |
paul@130 | 110 | image_update_scrolled_tiled_image(&scr, xorigin, yorigin, xstep, ystep); |
paul@94 | 111 | } |
paul@94 | 112 | |
paul@129 | 113 | |
paul@129 | 114 | |
paul@74 | 115 | /* Move a sprite around on the framebuffer. */ |
paul@74 | 116 | |
paul@74 | 117 | static void animate(uint32_t delay) |
paul@74 | 118 | { |
paul@117 | 119 | /* Stores of background details, replotted when moving the sprite. */ |
paul@102 | 120 | |
paul@136 | 121 | Sprite(s, &sprite, &display_config, FRAME_COUNT, SOURCE_YSTEP); |
paul@102 | 122 | |
paul@88 | 123 | /* Sprite position. */ |
paul@88 | 124 | |
paul@74 | 125 | int x, y; |
paul@88 | 126 | |
paul@88 | 127 | /* Scrolling directions. */ |
paul@88 | 128 | |
paul@94 | 129 | int dir[] = {1, 0, -1, 0, 1}; |
paul@94 | 130 | int dirindex = 0; |
paul@88 | 131 | |
paul@116 | 132 | /* Scroll increments. */ |
paul@88 | 133 | |
paul@116 | 134 | int xdir, ydir; |
paul@102 | 135 | |
paul@116 | 136 | /* Scrolling viewport. */ |
paul@88 | 137 | |
paul@122 | 138 | Viewport(v, &display_config, SCROLL_XSTEP, SOURCE_YSTEP, plot_screen_edge); |
paul@102 | 139 | |
paul@122 | 140 | /* Reset the viewport to a well-defined state. */ |
paul@102 | 141 | |
paul@122 | 142 | viewport_set_origin(&v, 0, 0); |
paul@102 | 143 | |
paul@102 | 144 | /* Animation loop. */ |
paul@74 | 145 | |
paul@74 | 146 | while (1) |
paul@83 | 147 | { |
paul@120 | 148 | for (y = 0; y < display_config.line_count - s.image->height; y++) |
paul@83 | 149 | { |
paul@120 | 150 | for (x = 0; x < display_config.line_length - s.image->width; x++) |
paul@74 | 151 | { |
paul@134 | 152 | image_plot_sprite(&s, x, y, 0x0c); |
paul@74 | 153 | |
paul@92 | 154 | /* Update the display with the frame details. */ |
paul@74 | 155 | |
paul@133 | 156 | vga_set_frame(&display_config, 1); |
paul@74 | 157 | wait(delay); |
paul@74 | 158 | |
paul@102 | 159 | /* Select the next frame to plot to. */ |
paul@102 | 160 | |
paul@116 | 161 | display_select_next_frame(&display_config); |
paul@102 | 162 | |
paul@102 | 163 | /* Prepare the frame for updates. */ |
paul@102 | 164 | |
paul@137 | 165 | viewport_unplot_sprite_from_image(&v, &s, &scr); |
paul@83 | 166 | |
paul@83 | 167 | /* Scroll in the indicated direction. */ |
paul@83 | 168 | |
paul@94 | 169 | xdir = dir[dirindex]; |
paul@94 | 170 | ydir = dir[dirindex + 1]; |
paul@94 | 171 | |
paul@102 | 172 | /* Update the origin if appropriate. */ |
paul@83 | 173 | |
paul@102 | 174 | /* Due to the effect of a simple screen start increment in the |
paul@102 | 175 | dual channel configuration, horizontal scrolling involves two |
paul@102 | 176 | pixel increments and thus requires a two-pixel column to be |
paul@103 | 177 | plotted per scrolling increment. */ |
paul@102 | 178 | |
paul@103 | 179 | /* For vertically-scaled backgrounds, the full resolution image |
paul@103 | 180 | is traversed by multiples of the scrolling increment. */ |
paul@103 | 181 | |
paul@116 | 182 | viewport_update_origin(&v, xdir * SCROLL_XSTEP, |
paul@116 | 183 | ydir * SOURCE_YSTEP); |
paul@74 | 184 | } |
paul@83 | 185 | |
paul@83 | 186 | /* Switch direction periodically. */ |
paul@83 | 187 | |
paul@94 | 188 | dirindex = wrap_value(dirindex + 1, 4); |
paul@83 | 189 | } |
paul@83 | 190 | } |
paul@74 | 191 | } |
paul@74 | 192 | |
paul@133 | 193 | /* Set up a banner. */ |
paul@78 | 194 | |
paul@133 | 195 | static void setup_banner(void) |
paul@78 | 196 | { |
paul@133 | 197 | int frame, x = 0, y = 0; |
paul@78 | 198 | |
paul@133 | 199 | for (frame = 0; frame < banner_config.frames; frame++) |
paul@109 | 200 | { |
paul@133 | 201 | /* Obtain the frame. */ |
paul@133 | 202 | |
paul@133 | 203 | display_select_frame(&banner_config, frame); |
paul@109 | 204 | |
paul@133 | 205 | write_string(&banner_config, &font_config, "VGA display example", &x, &y, 0xff); |
paul@133 | 206 | } |
paul@109 | 207 | |
paul@133 | 208 | display_select_frame(&banner_config, 0); |
paul@78 | 209 | } |
paul@78 | 210 | |
paul@98 | 211 | /* Set up a background. */ |
paul@98 | 212 | |
paul@133 | 213 | static void setup_background(void) |
paul@98 | 214 | { |
paul@98 | 215 | int frame; |
paul@98 | 216 | |
paul@98 | 217 | for (frame = 0; frame < display_config.frames; frame++) |
paul@98 | 218 | { |
paul@98 | 219 | /* Obtain the frame. */ |
paul@98 | 220 | |
paul@115 | 221 | display_select_frame(&display_config, frame); |
paul@98 | 222 | |
paul@129 | 223 | /* Tile the image on the screen. */ |
paul@98 | 224 | |
paul@129 | 225 | image_tile_sprite(&scr, 0, 0, display_config.line_length, |
paul@129 | 226 | display_config.line_count * scr.yscale, |
paul@129 | 227 | 0, 0); |
paul@98 | 228 | } |
paul@98 | 229 | |
paul@115 | 230 | display_select_frame(&display_config, 0); |
paul@98 | 231 | } |
paul@98 | 232 | |
paul@133 | 233 | static void setup(void) |
paul@133 | 234 | { |
paul@133 | 235 | setup_banner(); |
paul@133 | 236 | setup_background(); |
paul@133 | 237 | } |
paul@133 | 238 | |
paul@24 | 239 | |
paul@24 | 240 | |
paul@24 | 241 | /* Main program. */ |
paul@24 | 242 | |
paul@24 | 243 | void main(void) |
paul@24 | 244 | { |
paul@24 | 245 | init_memory(); |
paul@24 | 246 | init_pins(); |
paul@24 | 247 | init_outputs(); |
paul@24 | 248 | |
paul@24 | 249 | unlock_config(); |
paul@24 | 250 | config_oc(); |
paul@24 | 251 | config_uart(); |
paul@24 | 252 | lock_config(); |
paul@24 | 253 | |
paul@87 | 254 | #ifndef TRANSFER_CPU |
paul@24 | 255 | init_dma(); |
paul@87 | 256 | #endif |
paul@57 | 257 | |
paul@72 | 258 | #ifdef PARALLEL_MODE |
paul@72 | 259 | init_pm(); |
paul@72 | 260 | |
paul@72 | 261 | /* Configure parallel master mode. */ |
paul@72 | 262 | |
paul@72 | 263 | pm_init(0, 0b10); |
paul@72 | 264 | pm_set_output(0, 1, 0); |
paul@72 | 265 | pm_on(0); |
paul@72 | 266 | #endif |
paul@72 | 267 | |
paul@93 | 268 | uart_init(1, FPB, 115200); |
paul@93 | 269 | uart_on(1); |
paul@93 | 270 | |
paul@123 | 271 | /* Set the display to a well-defined state. */ |
paul@93 | 272 | |
paul@133 | 273 | init_display(&banner_config); |
paul@123 | 274 | init_display(&display_config); |
paul@93 | 275 | |
paul@59 | 276 | /* Initialise VGA output with one or two line channels, configuring a line |
paul@59 | 277 | timer and any transfer timer, with an initiating channel being introduced |
paul@59 | 278 | if a transfer timer is specified. */ |
paul@57 | 279 | |
paul@133 | 280 | init_vga_with_timers(&banner_config, LINE_CHANNELS, LINE_TIMER, TRANSFER_TIMER); |
paul@133 | 281 | vga_add_window(&display_config); |
paul@24 | 282 | |
paul@59 | 283 | /* Configure VGA output transfer to the output register, also configuring |
paul@59 | 284 | output compare units for horizontal and vertical sync. */ |
paul@24 | 285 | |
paul@72 | 286 | vga_configure_transfer(VGA_OUTPUT); |
paul@59 | 287 | vga_configure_sync(1, 2); |
paul@24 | 288 | |
paul@24 | 289 | interrupts_on(); |
paul@24 | 290 | |
paul@74 | 291 | /* Move a sprite around on the screen with a delay between each movement. */ |
paul@74 | 292 | |
paul@98 | 293 | setup(); |
paul@96 | 294 | animate(1 << 18); |
paul@24 | 295 | } |
paul@24 | 296 | |
paul@24 | 297 | |
paul@24 | 298 | |
paul@24 | 299 | /* Exception and interrupt handlers. */ |
paul@24 | 300 | |
paul@24 | 301 | void exception_handler(void) |
paul@24 | 302 | { |
paul@79 | 303 | blink(1 << 22, PORTA, LED_PIN); |
paul@24 | 304 | } |
paul@24 | 305 | |
paul@24 | 306 | void interrupt_handler(void) |
paul@24 | 307 | { |
paul@88 | 308 | vga_interrupt_handler(); |
paul@24 | 309 | } |
paul@24 | 310 | |
paul@24 | 311 | |
paul@24 | 312 | |
paul@24 | 313 | /* Peripheral pin configuration. */ |
paul@24 | 314 | |
paul@24 | 315 | void config_oc(void) |
paul@24 | 316 | { |
paul@72 | 317 | /* Map OC1. */ |
paul@24 | 318 | |
paul@72 | 319 | REG(OC1_PIN) = 0b0101; |
paul@24 | 320 | |
paul@72 | 321 | /* Map OC2. */ |
paul@24 | 322 | |
paul@72 | 323 | REG(OC2_PIN) = 0b0101; |
paul@24 | 324 | } |
paul@24 | 325 | |
paul@24 | 326 | void config_uart(void) |
paul@24 | 327 | { |
paul@24 | 328 | /* Map U1RX to RPB13. */ |
paul@24 | 329 | |
paul@24 | 330 | REG(U1RXR) = 0b0011; /* U1RXR<3:0> = 0011 (RPB13) */ |
paul@24 | 331 | |
paul@24 | 332 | /* Map U1TX to RPB15. */ |
paul@24 | 333 | |
paul@24 | 334 | REG(RPB15R) = 0b0001; /* RPB15R<3:0> = 0001 (U1TX) */ |
paul@24 | 335 | |
paul@24 | 336 | /* Set RPB13 to input. */ |
paul@24 | 337 | |
paul@24 | 338 | SET_REG(TRISB, 1 << 13); |
paul@24 | 339 | } |