paul@50 | 1 | /* |
paul@50 | 2 | * Generate a VGA signal using a PIC32 microcontroller. |
paul@50 | 3 | * |
paul@50 | 4 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@50 | 5 | * |
paul@50 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@50 | 7 | * it under the terms of the GNU General Public License as published by |
paul@50 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@50 | 9 | * (at your option) any later version. |
paul@50 | 10 | * |
paul@50 | 11 | * This program is distributed in the hope that it will be useful, |
paul@50 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@50 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@50 | 14 | * GNU General Public License for more details. |
paul@50 | 15 | * |
paul@50 | 16 | * You should have received a copy of the GNU General Public License |
paul@50 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@50 | 18 | */ |
paul@50 | 19 | |
paul@50 | 20 | #include "pic32_c.h" |
paul@53 | 21 | #include "init.h" |
paul@50 | 22 | #include "vga_display.h" |
paul@50 | 23 | |
paul@50 | 24 | |
paul@50 | 25 | |
paul@50 | 26 | /* Display state. */ |
paul@50 | 27 | |
paul@50 | 28 | vga_display_t vga_display; |
paul@50 | 29 | |
paul@50 | 30 | |
paul@50 | 31 | |
paul@50 | 32 | /* Initialise the state machine. */ |
paul@50 | 33 | |
paul@50 | 34 | void init_vga(display_config_t *display_config, |
paul@50 | 35 | void (*start_visible)(), |
paul@50 | 36 | void (*update_visible)(), |
paul@50 | 37 | void (*stop_visible)(), |
paul@50 | 38 | void (*vsync_high)(), |
paul@50 | 39 | void (*vsync_low)()) |
paul@50 | 40 | { |
paul@50 | 41 | /* Display parameters. */ |
paul@50 | 42 | |
paul@50 | 43 | vga_display.display_config = display_config; |
paul@50 | 44 | |
paul@50 | 45 | /* Display state handlers. */ |
paul@50 | 46 | |
paul@50 | 47 | vga_display.start_visible = start_visible; |
paul@50 | 48 | vga_display.update_visible = update_visible; |
paul@50 | 49 | vga_display.stop_visible = stop_visible; |
paul@50 | 50 | |
paul@50 | 51 | /* Vertical sync operations. */ |
paul@50 | 52 | |
paul@50 | 53 | vga_display.vsync_high = vsync_high; |
paul@50 | 54 | vga_display.vsync_low = vsync_low; |
paul@50 | 55 | |
paul@50 | 56 | /* Initial state. */ |
paul@50 | 57 | |
paul@50 | 58 | vga_display.state_handler = vbp_active; |
paul@50 | 59 | vga_display.line = 0; |
paul@50 | 60 | } |
paul@50 | 61 | |
paul@53 | 62 | /* Configure a timer and output compare units for horizontal and vertical |
paul@53 | 63 | sync. */ |
paul@53 | 64 | |
paul@53 | 65 | void vga_configure_sync(int hsync_unit, int vsync_unit, int timer) |
paul@53 | 66 | { |
paul@53 | 67 | /* Configure a timer for the horizontal sync. The timer has no prescaling |
paul@53 | 68 | (0). */ |
paul@53 | 69 | |
paul@53 | 70 | timer_init(timer, 0, vga_display.display_config->hfreq_limit); |
paul@53 | 71 | timer_on(timer); |
paul@53 | 72 | |
paul@53 | 73 | /* Horizontal sync. */ |
paul@53 | 74 | |
paul@53 | 75 | /* Configure output compare in dual compare (continuous output) mode using |
paul@53 | 76 | the timer as time base. The interrupt condition drives the first DMA |
paul@53 | 77 | channel and is handled to drive the display state machine. */ |
paul@53 | 78 | |
paul@53 | 79 | oc_init(hsync_unit, 0b101, timer); |
paul@53 | 80 | oc_set_pulse(hsync_unit, vga_display.display_config->hsync_end); |
paul@53 | 81 | oc_set_pulse_end(hsync_unit, vga_display.display_config->hsync_start); |
paul@53 | 82 | oc_init_interrupt(hsync_unit, 7, 3); |
paul@53 | 83 | oc_on(hsync_unit); |
paul@53 | 84 | |
paul@53 | 85 | /* Vertical sync. */ |
paul@53 | 86 | |
paul@53 | 87 | /* Configure output compare in single compare (output driven low) mode using |
paul@53 | 88 | the timer as time base. The unit is enabled later. It is only really used |
paul@53 | 89 | to achieve precisely-timed level transitions in hardware. */ |
paul@53 | 90 | |
paul@53 | 91 | oc_init(vsync_unit, 0b010, timer); |
paul@53 | 92 | oc_set_pulse(vsync_unit, 0); |
paul@53 | 93 | } |
paul@53 | 94 | |
paul@50 | 95 | |
paul@50 | 96 | |
paul@50 | 97 | /* Interrupt handlers. */ |
paul@50 | 98 | |
paul@50 | 99 | void vga_interrupt_handler(void) |
paul@50 | 100 | { |
paul@50 | 101 | vga_display.line += 1; |
paul@50 | 102 | vga_display.state_handler(); |
paul@50 | 103 | } |
paul@50 | 104 | |
paul@50 | 105 | |
paul@50 | 106 | |
paul@50 | 107 | /* Vertical back porch region. */ |
paul@50 | 108 | |
paul@50 | 109 | void vbp_active(void) |
paul@50 | 110 | { |
paul@50 | 111 | if (vga_display.line < vga_display.display_config->visible_start) |
paul@50 | 112 | return; |
paul@50 | 113 | |
paul@50 | 114 | /* Enter the visible region. */ |
paul@50 | 115 | |
paul@50 | 116 | vga_display.state_handler = visible_active; |
paul@50 | 117 | |
paul@50 | 118 | /* Set the line address. */ |
paul@50 | 119 | |
paul@50 | 120 | vga_display.linedata = vga_display.display_config->screen_start; |
paul@50 | 121 | vga_display.start_visible(&vga_display); |
paul@50 | 122 | } |
paul@50 | 123 | |
paul@50 | 124 | /* Visible region. */ |
paul@50 | 125 | |
paul@50 | 126 | void visible_active(void) |
paul@50 | 127 | { |
paul@50 | 128 | if (vga_display.line < vga_display.display_config->vfp_start) |
paul@50 | 129 | { |
paul@50 | 130 | /* Update the line address and handle wraparound. */ |
paul@50 | 131 | |
paul@50 | 132 | if (!(vga_display.line % vga_display.display_config->line_multiplier)) |
paul@50 | 133 | { |
paul@50 | 134 | vga_display.linedata += vga_display.display_config->line_length; |
paul@50 | 135 | |
paul@50 | 136 | if (vga_display.linedata >= vga_display.display_config->screen_limit) |
paul@50 | 137 | vga_display.linedata -= vga_display.display_config->screen_size; |
paul@50 | 138 | } |
paul@50 | 139 | |
paul@50 | 140 | vga_display.update_visible(&vga_display); |
paul@50 | 141 | return; |
paul@50 | 142 | } |
paul@50 | 143 | |
paul@50 | 144 | /* End the visible region. */ |
paul@50 | 145 | |
paul@50 | 146 | vga_display.state_handler = vfp_active; |
paul@50 | 147 | |
paul@50 | 148 | /* Disable the channel for the next line. */ |
paul@50 | 149 | |
paul@50 | 150 | vga_display.stop_visible(&vga_display); |
paul@50 | 151 | } |
paul@50 | 152 | |
paul@50 | 153 | /* Vertical front porch region. */ |
paul@50 | 154 | |
paul@50 | 155 | void vfp_active(void) |
paul@50 | 156 | { |
paul@50 | 157 | if (vga_display.line < vga_display.display_config->vsync_start) |
paul@50 | 158 | return; |
paul@50 | 159 | |
paul@50 | 160 | /* Enter the vertical sync region. */ |
paul@50 | 161 | |
paul@50 | 162 | vga_display.state_handler = vsync_active; |
paul@50 | 163 | |
paul@50 | 164 | /* Bring vsync low when the next line starts. */ |
paul@50 | 165 | |
paul@50 | 166 | vga_display.vsync_low(); |
paul@50 | 167 | } |
paul@50 | 168 | |
paul@50 | 169 | /* Vertical sync region. */ |
paul@50 | 170 | |
paul@50 | 171 | void vsync_active(void) |
paul@50 | 172 | { |
paul@50 | 173 | if (vga_display.line < vga_display.display_config->vsync_end) |
paul@50 | 174 | return; |
paul@50 | 175 | |
paul@50 | 176 | /* Start again at the top of the display. */ |
paul@50 | 177 | |
paul@50 | 178 | vga_display.line = 0; |
paul@50 | 179 | vga_display.state_handler = vbp_active; |
paul@50 | 180 | |
paul@50 | 181 | /* Bring vsync high when the next line starts. */ |
paul@50 | 182 | |
paul@50 | 183 | vga_display.vsync_high(); |
paul@50 | 184 | } |