paul@87 | 1 | /* |
paul@87 | 2 | * Generate a VGA signal using a PIC32 microcontroller. |
paul@87 | 3 | * |
paul@87 | 4 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@87 | 5 | * |
paul@87 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@87 | 7 | * it under the terms of the GNU General Public License as published by |
paul@87 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@87 | 9 | * (at your option) any later version. |
paul@87 | 10 | * |
paul@87 | 11 | * This program is distributed in the hope that it will be useful, |
paul@87 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@87 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@87 | 14 | * GNU General Public License for more details. |
paul@87 | 15 | * |
paul@87 | 16 | * You should have received a copy of the GNU General Public License |
paul@87 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@87 | 18 | */ |
paul@87 | 19 | |
paul@87 | 20 | #include "pic32_c.h" |
paul@87 | 21 | #include "init.h" |
paul@87 | 22 | #include "vga_display_cpu.h" |
paul@87 | 23 | |
paul@87 | 24 | |
paul@87 | 25 | |
paul@87 | 26 | /* Display state. */ |
paul@87 | 27 | |
paul@87 | 28 | vga_display_t vga_display; |
paul@87 | 29 | |
paul@87 | 30 | |
paul@87 | 31 | |
paul@87 | 32 | /* Initialise the state machine. */ |
paul@87 | 33 | |
paul@87 | 34 | void init_vga(display_config_t *display_config, int line_timer) |
paul@87 | 35 | { |
paul@87 | 36 | /* Display parameters. */ |
paul@87 | 37 | |
paul@87 | 38 | vga_display.display_config = display_config; |
paul@87 | 39 | |
paul@87 | 40 | /* Initial state. */ |
paul@87 | 41 | |
paul@87 | 42 | vga_display.state_handler = vbp_active; |
paul@87 | 43 | vga_display.line = 0; |
paul@87 | 44 | |
paul@87 | 45 | /* Configure a general display timer to start line data transfer and for the |
paul@87 | 46 | horizontal sync. */ |
paul@87 | 47 | |
paul@87 | 48 | vga_display.line_timer = line_timer; |
paul@87 | 49 | } |
paul@87 | 50 | |
paul@87 | 51 | /* Initialise a separate transfer timer if different from the general display |
paul@87 | 52 | timer. */ |
paul@87 | 53 | |
paul@87 | 54 | void init_vga_with_timer(display_config_t *display_config, int line_timer) |
paul@87 | 55 | { |
paul@87 | 56 | /* Initialise the basic properties of the display. */ |
paul@87 | 57 | |
paul@87 | 58 | init_vga(display_config, line_timer); |
paul@87 | 59 | |
paul@87 | 60 | /* Configure a line timer for horizontal sync and line data transfers. The |
paul@87 | 61 | interrupt is handled to produce line data and to drive the display state |
paul@87 | 62 | machine. */ |
paul@87 | 63 | |
paul@87 | 64 | /* The timer has no prescaling (0). */ |
paul@87 | 65 | |
paul@87 | 66 | timer_init(line_timer, 0, display_config->hfreq_limit); |
paul@87 | 67 | timer_init_interrupt(line_timer, 7, 3); |
paul@87 | 68 | timer_on(line_timer); |
paul@87 | 69 | } |
paul@87 | 70 | |
paul@87 | 71 | |
paul@87 | 72 | |
paul@87 | 73 | /* Configure the output port. */ |
paul@87 | 74 | |
paul@87 | 75 | void vga_configure_transfer(uint32_t output) |
paul@87 | 76 | { |
paul@87 | 77 | vga_display.output = output; |
paul@87 | 78 | } |
paul@87 | 79 | |
paul@87 | 80 | /* Configure output compare units for horizontal and vertical sync. */ |
paul@87 | 81 | |
paul@87 | 82 | void vga_configure_sync(int hsync_unit, int vsync_unit) |
paul@87 | 83 | { |
paul@87 | 84 | /* Record the peripherals in use. */ |
paul@87 | 85 | |
paul@87 | 86 | vga_display.hsync_unit = hsync_unit; |
paul@87 | 87 | vga_display.vsync_unit = vsync_unit; |
paul@87 | 88 | |
paul@87 | 89 | /* Horizontal sync. */ |
paul@87 | 90 | |
paul@87 | 91 | /* Configure output compare in dual compare (continuous output) mode using |
paul@87 | 92 | the timer as time base. */ |
paul@87 | 93 | |
paul@87 | 94 | oc_init(hsync_unit, 0b101, vga_display.line_timer); |
paul@87 | 95 | oc_set_pulse(hsync_unit, vga_display.display_config->hsync_end); |
paul@87 | 96 | oc_set_pulse_end(hsync_unit, vga_display.display_config->hsync_start); |
paul@87 | 97 | oc_init_interrupt(hsync_unit, 7, 3); |
paul@87 | 98 | oc_on(hsync_unit); |
paul@87 | 99 | |
paul@87 | 100 | /* Vertical sync. */ |
paul@87 | 101 | |
paul@87 | 102 | /* Configure output compare in single compare (output driven low) mode using |
paul@87 | 103 | the timer as time base. The unit is enabled later. It is only really used |
paul@87 | 104 | to achieve precisely-timed level transitions in hardware. */ |
paul@87 | 105 | |
paul@87 | 106 | oc_init(vsync_unit, 0b010, vga_display.line_timer); |
paul@87 | 107 | oc_set_pulse(vsync_unit, 0); |
paul@87 | 108 | } |
paul@87 | 109 | |
paul@87 | 110 | |
paul@87 | 111 | |
paul@87 | 112 | /* Interrupt handlers. */ |
paul@87 | 113 | |
paul@87 | 114 | void vga_interrupt_handler(void) |
paul@87 | 115 | { |
paul@87 | 116 | vga_display.line += 1; |
paul@87 | 117 | vga_display.state_handler(); |
paul@87 | 118 | } |
paul@87 | 119 | |
paul@87 | 120 | /* Visible region pixel output handler. */ |
paul@87 | 121 | |
paul@87 | 122 | void vga_transfer_interrupt_handler(void) |
paul@87 | 123 | { |
paul@87 | 124 | display_config_t *cfg = vga_display.display_config; |
paul@87 | 125 | uint8_t *current, *end, *output; |
paul@87 | 126 | |
paul@87 | 127 | if (vga_display.state_handler != visible_active) |
paul@87 | 128 | return; |
paul@87 | 129 | |
paul@87 | 130 | /* Generate the pixel signal. */ |
paul@87 | 131 | |
paul@87 | 132 | output = (uint8_t *) vga_display.output; |
paul@87 | 133 | end = vga_display.linedata + cfg->line_length; |
paul@87 | 134 | |
paul@87 | 135 | /* This is potentially not as efficient as loading words and shifting bytes |
paul@87 | 136 | but it appears difficult to implement that approach without experiencing |
paul@87 | 137 | data load exceptions. */ |
paul@87 | 138 | |
paul@87 | 139 | for (current = vga_display.linedata; current < end; current++) |
paul@87 | 140 | REG(output) = *current; |
paul@87 | 141 | |
paul@87 | 142 | /* Reset the signal level. */ |
paul@87 | 143 | |
paul@87 | 144 | REG(output) = 0; |
paul@87 | 145 | } |
paul@87 | 146 | |
paul@87 | 147 | |
paul@87 | 148 | |
paul@87 | 149 | /* Vertical back porch region. */ |
paul@87 | 150 | |
paul@87 | 151 | void vbp_active(void) |
paul@87 | 152 | { |
paul@87 | 153 | if (vga_display.line < vga_display.display_config->visible_start) |
paul@87 | 154 | return; |
paul@87 | 155 | |
paul@87 | 156 | /* Enter the visible region. */ |
paul@87 | 157 | |
paul@87 | 158 | vga_display.state_handler = visible_active; |
paul@87 | 159 | |
paul@87 | 160 | /* Set the line address. */ |
paul@87 | 161 | |
paul@87 | 162 | vga_display.linedata = vga_display.display_config->screen_start; |
paul@87 | 163 | } |
paul@87 | 164 | |
paul@87 | 165 | /* Visible region. */ |
paul@87 | 166 | |
paul@87 | 167 | void visible_active(void) |
paul@87 | 168 | { |
paul@87 | 169 | display_config_t *cfg = vga_display.display_config; |
paul@87 | 170 | |
paul@87 | 171 | if (vga_display.line < cfg->vfp_start) |
paul@87 | 172 | { |
paul@87 | 173 | /* Update the line address and handle wraparound. */ |
paul@87 | 174 | |
paul@87 | 175 | if (!(vga_display.line % cfg->line_multiplier)) |
paul@87 | 176 | { |
paul@87 | 177 | vga_display.linedata += cfg->line_length; |
paul@87 | 178 | |
paul@87 | 179 | if (vga_display.linedata >= cfg->screen_limit) |
paul@87 | 180 | vga_display.linedata -= cfg->screen_size; |
paul@87 | 181 | } |
paul@87 | 182 | |
paul@87 | 183 | return; |
paul@87 | 184 | } |
paul@87 | 185 | |
paul@87 | 186 | /* End the visible region. */ |
paul@87 | 187 | |
paul@87 | 188 | vga_display.state_handler = vfp_active; |
paul@87 | 189 | } |
paul@87 | 190 | |
paul@87 | 191 | /* Vertical front porch region. */ |
paul@87 | 192 | |
paul@87 | 193 | void vfp_active(void) |
paul@87 | 194 | { |
paul@87 | 195 | if (vga_display.line < vga_display.display_config->vsync_start) |
paul@87 | 196 | return; |
paul@87 | 197 | |
paul@87 | 198 | /* Enter the vertical sync region. */ |
paul@87 | 199 | |
paul@87 | 200 | vga_display.state_handler = vsync_active; |
paul@87 | 201 | |
paul@87 | 202 | /* Bring vsync low when the next line starts. */ |
paul@87 | 203 | |
paul@87 | 204 | vsync_low(); |
paul@87 | 205 | } |
paul@87 | 206 | |
paul@87 | 207 | /* Vertical sync region. */ |
paul@87 | 208 | |
paul@87 | 209 | void vsync_active(void) |
paul@87 | 210 | { |
paul@87 | 211 | if (vga_display.line < vga_display.display_config->vsync_end) |
paul@87 | 212 | return; |
paul@87 | 213 | |
paul@87 | 214 | /* Start again at the top of the display. */ |
paul@87 | 215 | |
paul@87 | 216 | vga_display.line = 0; |
paul@87 | 217 | vga_display.state_handler = vbp_active; |
paul@87 | 218 | |
paul@87 | 219 | /* Bring vsync high when the next line starts. */ |
paul@87 | 220 | |
paul@87 | 221 | vsync_high(); |
paul@87 | 222 | } |
paul@87 | 223 | |
paul@87 | 224 | |
paul@87 | 225 | |
paul@87 | 226 | /* Bring vsync low (single compare, output driven low) when the next line |
paul@87 | 227 | starts. */ |
paul@87 | 228 | |
paul@87 | 229 | void vsync_low(void) |
paul@87 | 230 | { |
paul@87 | 231 | oc_init(vga_display.vsync_unit, 0b010, vga_display.line_timer); |
paul@87 | 232 | oc_on(vga_display.vsync_unit); |
paul@87 | 233 | } |
paul@87 | 234 | |
paul@87 | 235 | /* Bring vsync high (single compare, output driven high) when the next line |
paul@87 | 236 | starts. */ |
paul@87 | 237 | |
paul@87 | 238 | void vsync_high(void) |
paul@87 | 239 | { |
paul@87 | 240 | oc_init(vga_display.vsync_unit, 0b001, vga_display.line_timer); |
paul@87 | 241 | oc_on(vga_display.vsync_unit); |
paul@87 | 242 | } |