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@50 | 21 | #include "vga_display.h" |
paul@50 | 22 | |
paul@50 | 23 | |
paul@50 | 24 | |
paul@50 | 25 | /* Display state. */ |
paul@50 | 26 | |
paul@50 | 27 | vga_display_t vga_display; |
paul@50 | 28 | |
paul@50 | 29 | |
paul@50 | 30 | |
paul@50 | 31 | /* Initialise the state machine. */ |
paul@50 | 32 | |
paul@50 | 33 | void init_vga(display_config_t *display_config, |
paul@50 | 34 | void (*start_visible)(), |
paul@50 | 35 | void (*update_visible)(), |
paul@50 | 36 | void (*stop_visible)(), |
paul@50 | 37 | void (*vsync_high)(), |
paul@50 | 38 | void (*vsync_low)()) |
paul@50 | 39 | { |
paul@50 | 40 | /* Display parameters. */ |
paul@50 | 41 | |
paul@50 | 42 | vga_display.display_config = display_config; |
paul@50 | 43 | |
paul@50 | 44 | /* Display state handlers. */ |
paul@50 | 45 | |
paul@50 | 46 | vga_display.start_visible = start_visible; |
paul@50 | 47 | vga_display.update_visible = update_visible; |
paul@50 | 48 | vga_display.stop_visible = stop_visible; |
paul@50 | 49 | |
paul@50 | 50 | /* Vertical sync operations. */ |
paul@50 | 51 | |
paul@50 | 52 | vga_display.vsync_high = vsync_high; |
paul@50 | 53 | vga_display.vsync_low = vsync_low; |
paul@50 | 54 | |
paul@50 | 55 | /* Initial state. */ |
paul@50 | 56 | |
paul@50 | 57 | vga_display.state_handler = vbp_active; |
paul@50 | 58 | vga_display.line = 0; |
paul@50 | 59 | } |
paul@50 | 60 | |
paul@50 | 61 | |
paul@50 | 62 | |
paul@50 | 63 | /* Interrupt handlers. */ |
paul@50 | 64 | |
paul@50 | 65 | void vga_interrupt_handler(void) |
paul@50 | 66 | { |
paul@50 | 67 | vga_display.line += 1; |
paul@50 | 68 | vga_display.state_handler(); |
paul@50 | 69 | } |
paul@50 | 70 | |
paul@50 | 71 | |
paul@50 | 72 | |
paul@50 | 73 | /* Vertical back porch region. */ |
paul@50 | 74 | |
paul@50 | 75 | void vbp_active(void) |
paul@50 | 76 | { |
paul@50 | 77 | if (vga_display.line < vga_display.display_config->visible_start) |
paul@50 | 78 | return; |
paul@50 | 79 | |
paul@50 | 80 | /* Enter the visible region. */ |
paul@50 | 81 | |
paul@50 | 82 | vga_display.state_handler = visible_active; |
paul@50 | 83 | |
paul@50 | 84 | /* Set the line address. */ |
paul@50 | 85 | |
paul@50 | 86 | vga_display.linedata = vga_display.display_config->screen_start; |
paul@50 | 87 | vga_display.start_visible(&vga_display); |
paul@50 | 88 | } |
paul@50 | 89 | |
paul@50 | 90 | /* Visible region. */ |
paul@50 | 91 | |
paul@50 | 92 | void visible_active(void) |
paul@50 | 93 | { |
paul@50 | 94 | if (vga_display.line < vga_display.display_config->vfp_start) |
paul@50 | 95 | { |
paul@50 | 96 | /* Update the line address and handle wraparound. */ |
paul@50 | 97 | |
paul@50 | 98 | if (!(vga_display.line % vga_display.display_config->line_multiplier)) |
paul@50 | 99 | { |
paul@50 | 100 | vga_display.linedata += vga_display.display_config->line_length; |
paul@50 | 101 | |
paul@50 | 102 | if (vga_display.linedata >= vga_display.display_config->screen_limit) |
paul@50 | 103 | vga_display.linedata -= vga_display.display_config->screen_size; |
paul@50 | 104 | } |
paul@50 | 105 | |
paul@50 | 106 | vga_display.update_visible(&vga_display); |
paul@50 | 107 | return; |
paul@50 | 108 | } |
paul@50 | 109 | |
paul@50 | 110 | /* End the visible region. */ |
paul@50 | 111 | |
paul@50 | 112 | vga_display.state_handler = vfp_active; |
paul@50 | 113 | |
paul@50 | 114 | /* Disable the channel for the next line. */ |
paul@50 | 115 | |
paul@50 | 116 | vga_display.stop_visible(&vga_display); |
paul@50 | 117 | } |
paul@50 | 118 | |
paul@50 | 119 | /* Vertical front porch region. */ |
paul@50 | 120 | |
paul@50 | 121 | void vfp_active(void) |
paul@50 | 122 | { |
paul@50 | 123 | if (vga_display.line < vga_display.display_config->vsync_start) |
paul@50 | 124 | return; |
paul@50 | 125 | |
paul@50 | 126 | /* Enter the vertical sync region. */ |
paul@50 | 127 | |
paul@50 | 128 | vga_display.state_handler = vsync_active; |
paul@50 | 129 | |
paul@50 | 130 | /* Bring vsync low when the next line starts. */ |
paul@50 | 131 | |
paul@50 | 132 | vga_display.vsync_low(); |
paul@50 | 133 | } |
paul@50 | 134 | |
paul@50 | 135 | /* Vertical sync region. */ |
paul@50 | 136 | |
paul@50 | 137 | void vsync_active(void) |
paul@50 | 138 | { |
paul@50 | 139 | if (vga_display.line < vga_display.display_config->vsync_end) |
paul@50 | 140 | return; |
paul@50 | 141 | |
paul@50 | 142 | /* Start again at the top of the display. */ |
paul@50 | 143 | |
paul@50 | 144 | vga_display.line = 0; |
paul@50 | 145 | vga_display.state_handler = vbp_active; |
paul@50 | 146 | |
paul@50 | 147 | /* Bring vsync high when the next line starts. */ |
paul@50 | 148 | |
paul@50 | 149 | vga_display.vsync_high(); |
paul@50 | 150 | } |