|
| 1 | +// Copyright (c) 2020 FPGAcademy |
| 2 | +// Please see license at https://github.com/fpgacademy/DESim |
| 3 | + |
| 4 | +#include "fpga.h" |
| 5 | +#include "helper.h" |
| 6 | + |
| 7 | +fpga::fpga(vpiHandle clk_handle, vpiHandle switch_handle, vpiHandle key_handle, |
| 8 | + vpiHandle led_handle, vpiHandle hex_handle, vpiHandle key_action_handle, |
| 9 | + vpiHandle scan_code_handle, vpiHandle ps2_lock_handle, |
| 10 | + vpiHandle x_handle, vpiHandle y_handle, vpiHandle color_handle, vpiHandle plot_handle, |
| 11 | + vpiHandle gpio_handle) { |
| 12 | + // basic I/O |
| 13 | + this->leds = new fpga_output_device(NUM_LEDS, vpiBinStrVal, 'l', led_handle); |
| 14 | + this->hex_displays = new fpga_output_device(HEX_SIG_LENGTH, vpiHexStrVal, 'h', hex_handle); |
| 15 | + |
| 16 | + this->switches = new fpga_input_device(NUM_SW, vpiBinStrVal, "0000000000", switch_handle); |
| 17 | + this->keys = new fpga_input_device(NUM_KEYS, vpiBinStrVal, "1111", key_handle); |
| 18 | + |
| 19 | + // gpio |
| 20 | + this->gpio = new fpga_inout_device(NUM_GPIO, vpiBinStrVal, 'g', gpio_handle); |
| 21 | + |
| 22 | + // ps2 |
| 23 | + this->ps2_locks = new fpga_output_device(NUM_PS2_LOCKS, vpiBinStrVal, 'p', ps2_lock_handle); |
| 24 | + this->keyboard = new keyboard_input(key_action_handle, scan_code_handle); |
| 25 | + |
| 26 | + // vga |
| 27 | + this->clk_handle = clk_handle; |
| 28 | + this->vga_x_handle = x_handle; |
| 29 | + this->vga_y_handle = y_handle; |
| 30 | + this->vga_color_handle = color_handle; |
| 31 | + this->vga_plot_handle = plot_handle; |
| 32 | + |
| 33 | + this->rwsync_cb_reg = 0; |
| 34 | + this->rosync_cb_reg = 0; |
| 35 | + |
| 36 | + this->keep_alive_cb_handle = NULL; |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +void fpga::UpdateOutputValue(char device_type, s_vpi_value *value) { |
| 41 | + if (device_type == 'l') { |
| 42 | + if(this->leds->UpdateValue(value)){ |
| 43 | + vpi_printf_helper("Error: incorrect led value format\n"); |
| 44 | + vpi_control(vpiFinish, 1); |
| 45 | + } |
| 46 | + } else if(device_type == 'h') { |
| 47 | + if(this->hex_displays->UpdateValue(value)){ |
| 48 | + vpi_printf_helper("Error: incorrect hex display value format\n"); |
| 49 | + vpi_control(vpiFinish, 1); |
| 50 | + } |
| 51 | + } else if(device_type == 'p') { |
| 52 | + if(this->ps2_locks->UpdateValue(value)){ |
| 53 | + vpi_printf_helper("Error: incorrect ps2 lock value format\n"); |
| 54 | + vpi_control(vpiFinish, 1); |
| 55 | + } |
| 56 | + }else if(device_type == 'g'){ |
| 57 | + if(this->gpio->UpdateOutputValue(value)){ |
| 58 | + vpi_printf_helper("Error: incorrect gpio value format\n"); |
| 59 | + vpi_control(vpiFinish, 1); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Send output signals to GUI |
| 65 | +void fpga::SendOutputValue(sockfd server) { |
| 66 | + this->leds->SendValue(server); |
| 67 | + this->hex_displays->SendValue(server); |
| 68 | + this->ps2_locks->SendValue(server); |
| 69 | + this->gpio->SendValue(server); |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +void fpga::SetInitialSimInput() { |
| 74 | + this->switches->SetInitialValue(); |
| 75 | + this->keys->SetInitialValue(); |
| 76 | + this->keyboard->SetInitialValue(); |
| 77 | +} |
| 78 | + |
| 79 | +// Process input signals from GUI |
| 80 | +int fpga::HandleInput(char *msg) { |
| 81 | + |
| 82 | + char *tokens = strpbrk(msg, " \t\r\n\v"); |
| 83 | + if (tokens) { |
| 84 | + // truncate msg in order to compare the first word of msg |
| 85 | + *tokens = 0; |
| 86 | + tokens++; |
| 87 | + } |
| 88 | + |
| 89 | + if (!tokens) { |
| 90 | + vpi_printf_helper("Warning: malformed command. Ignoring...\n"); |
| 91 | + vpi_mcd_flush(1); |
| 92 | + return 0; |
| 93 | + } |
| 94 | + if (!strcmp(msg, "SW")) { |
| 95 | + if(this->switches->UpdateValue(tokens)){ |
| 96 | + vpi_printf_helper("Warning: malformed switch command. Ignoring...\n"); |
| 97 | + vpi_mcd_flush(1); |
| 98 | + } |
| 99 | + |
| 100 | + } else if (!strcmp(msg, "KEY")) { |
| 101 | + if(this->keys->UpdateValue(tokens)){ |
| 102 | + vpi_printf_helper("Warning: malformed push button command. Ignoring...\n"); |
| 103 | + vpi_mcd_flush(1); |
| 104 | + } |
| 105 | + |
| 106 | + } else if (!strcmp(msg, "KB")) { |
| 107 | + if(this->keyboard->UpdateValue(tokens)){ |
| 108 | + vpi_printf_helper("Warning: malformed keyboard scan code. Ignoring...\n"); |
| 109 | + vpi_mcd_flush(1); |
| 110 | + } |
| 111 | + |
| 112 | + } else if (!strcmp(msg, "GPIO")) { |
| 113 | + if(this->gpio->UpdateInputValue(tokens)){ |
| 114 | + vpi_printf_helper("Warning: malformed gpio command. Ignoring...\n"); |
| 115 | + vpi_mcd_flush(1); |
| 116 | + } |
| 117 | + }else if (!strcmp(msg, "end")) { |
| 118 | + vpi_control(vpiFinish, 1); |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + return 0; |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +void fpga::SendVGAPixelValue(sockfd server){ |
| 127 | + |
| 128 | + s_vpi_value val = {.format = vpiIntVal}; |
| 129 | + vpi_get_value(this->vga_plot_handle, &val); |
| 130 | + |
| 131 | + // first check if plot is 1, then send pixel value |
| 132 | + if(val.value.integer != 0){ |
| 133 | + int x, y, color; |
| 134 | + |
| 135 | + vpi_get_value(this->vga_x_handle, &val); |
| 136 | + x = val.value.integer; |
| 137 | + vpi_get_value(this->vga_y_handle, &val); |
| 138 | + y = val.value.integer; |
| 139 | + vpi_get_value(this->vga_color_handle, &val); |
| 140 | + color = val.value.integer; |
| 141 | + |
| 142 | + char line[80]; |
| 143 | + //write at most size bytes (including '\0') |
| 144 | + snprintf(line, 80, "c %03d %03d %d\n", x, y, color); |
| 145 | + |
| 146 | + int rc = send(server, line, 12, 0); |
| 147 | + if (rc <= 0) { |
| 148 | + vpi_printf_helper("Could not send VGA signal to GUI\n"); |
| 149 | + vpi_mcd_flush(1); |
| 150 | + vpi_control(vpiFinish, 1); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +fpga::~fpga(){ |
| 158 | + delete this->leds; |
| 159 | + delete this->hex_displays; |
| 160 | + delete this->switches; |
| 161 | + delete this->keys; |
| 162 | + delete this->gpio; |
| 163 | + delete this->ps2_locks; |
| 164 | + delete this->keyboard; |
| 165 | + |
| 166 | +} |
| 167 | + |
| 168 | +bool fpga::operator = (fpga const &other){ |
| 169 | + // devices should be the same |
| 170 | + if(this->leds != other.leds) return false; |
| 171 | + if(this->hex_displays != other.hex_displays) return false; |
| 172 | + if(this->switches != other.switches) return false; |
| 173 | + if(this->keys != other.keys) return false; |
| 174 | + if(this->ps2_locks != other.ps2_locks) return false; |
| 175 | + if(this->keyboard != other.keyboard) return false; |
| 176 | + if(this->gpio != other.gpio) return false; |
| 177 | + |
| 178 | + // device handles |
| 179 | + if(this->clk_handle != other.clk_handle) return false; |
| 180 | + if(this->vga_x_handle != other.vga_x_handle) return false; |
| 181 | + if(this->vga_y_handle != other.vga_y_handle) return false; |
| 182 | + if(this->vga_color_handle != other.vga_y_handle) return false; |
| 183 | + if(this->vga_plot_handle != other.vga_plot_handle) return false; |
| 184 | + |
| 185 | + // read/write/keep alive callback can be either registered or not |
| 186 | + return true; |
| 187 | +} |
| 188 | + |
| 189 | + |
| 190 | +fpga::fpga(fpga const &other){ |
| 191 | + this->leds = new fpga_output_device(*(other.leds)); |
| 192 | + this->hex_displays = new fpga_output_device(*(other.hex_displays)); |
| 193 | + this->switches = new fpga_input_device(*(other.switches)); |
| 194 | + this->keys = new fpga_input_device(*(other.keys)); |
| 195 | + this->ps2_locks = new fpga_output_device(*(other.ps2_locks)); |
| 196 | + this->keyboard = new keyboard_input(*(other.keyboard)); |
| 197 | + this->gpio = new fpga_inout_device(*(other.gpio)); |
| 198 | + |
| 199 | + this->clk_handle = other.clk_handle; |
| 200 | + this->vga_x_handle = other.vga_x_handle; |
| 201 | + this->vga_y_handle = other.vga_y_handle; |
| 202 | + this->vga_color_handle = other.vga_color_handle; |
| 203 | + this->vga_plot_handle = other.vga_plot_handle; |
| 204 | + |
| 205 | + this->rwsync_cb_reg = 0; |
| 206 | + this->rosync_cb_reg = 0; |
| 207 | + this->keep_alive_cb_handle = NULL; |
| 208 | +} |
| 209 | + |
| 210 | + |
| 211 | + |
0 commit comments