| 1 |
2 |
jamieiles |
// Copyright Jamie Iles, 2017
|
| 2 |
|
|
//
|
| 3 |
|
|
// This file is part of s80x86.
|
| 4 |
|
|
//
|
| 5 |
|
|
// s80x86 is free software: you can redistribute it and/or modify
|
| 6 |
|
|
// it under the terms of the GNU General Public License as published by
|
| 7 |
|
|
// the Free Software Foundation, either version 3 of the License, or
|
| 8 |
|
|
// (at your option) any later version.
|
| 9 |
|
|
//
|
| 10 |
|
|
// s80x86 is distributed in the hope that it will be useful,
|
| 11 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 |
|
|
// GNU General Public License for more details.
|
| 14 |
|
|
//
|
| 15 |
|
|
// You should have received a copy of the GNU General Public License
|
| 16 |
|
|
// along with s80x86. If not, see .
|
| 17 |
|
|
|
| 18 |
|
|
// vi: ft=systemverilog
|
| 19 |
|
|
`ifndef MICROCODE_ROM_PATH
|
| 20 |
|
|
`define MICROCODE_ROM_PATH "."
|
| 21 |
|
|
`endif
|
| 22 |
|
|
|
| 23 |
|
|
module Microcode(input logic clk,
|
| 24 |
|
|
input logic reset,
|
| 25 |
|
|
input logic nmi_pulse,
|
| 26 |
|
|
input logic intr,
|
| 27 |
|
|
output logic inta,
|
| 28 |
|
|
output logic irq_to_mdr,
|
| 29 |
|
|
output logic start_interrupt,
|
| 30 |
|
|
output logic do_escape_fault,
|
| 31 |
|
|
input logic stall,
|
| 32 |
|
|
input logic divide_error,
|
| 33 |
|
|
input logic rm_is_reg,
|
| 34 |
|
|
input logic [2:0] modrm_reg,
|
| 35 |
|
|
input logic int_enabled,
|
| 36 |
|
|
input logic zf,
|
| 37 |
|
|
input logic tf,
|
| 38 |
|
|
output logic [15:0] microcode_immediate,
|
| 39 |
|
|
output logic [8:0] update_flags,
|
| 40 |
|
|
output logic use_microcode_immediate,
|
| 41 |
|
|
output logic segment_override,
|
| 42 |
|
|
output logic [7:0] opcode,
|
| 43 |
|
|
input logic jump_taken,
|
| 44 |
|
|
input logic rb_zero,
|
| 45 |
|
|
output logic lock,
|
| 46 |
|
|
output logic multibit_shift,
|
| 47 |
|
|
output logic is_hlt,
|
| 48 |
|
|
output logic next_microinstruction,
|
| 49 |
|
|
// Microinstruction fields.
|
| 50 |
|
|
<%#exported_fields%>
|
| 51 |
|
|
output logic <%type%><%name%>,
|
| 52 |
|
|
<%/exported_fields%>
|
| 53 |
|
|
output logic width,
|
| 54 |
|
|
output logic reg_wr_en,
|
| 55 |
|
|
// Fifo Read Port.
|
| 56 |
|
|
output logic fifo_rd_en,
|
| 57 |
|
|
input logic [7:0] fifo_rd_data,
|
| 58 |
|
|
input logic fifo_empty,
|
| 59 |
|
|
input logic fifo_resetting,
|
| 60 |
|
|
output logic loop_next,
|
| 61 |
|
|
input logic loop_done,
|
| 62 |
|
|
// Debug
|
| 63 |
|
|
output logic debug_stopped,
|
| 64 |
|
|
input logic debug_seize,
|
| 65 |
|
|
input logic [7:0] debug_addr,
|
| 66 |
|
|
input logic debug_run);
|
| 67 |
|
|
|
| 68 |
|
|
localparam num_instructions = <%num_instructions%>;
|
| 69 |
|
|
localparam addr_bits = <%addr_bits%>;
|
| 70 |
|
|
localparam reset_address = <%addr_bits%>'h129;
|
| 71 |
|
|
localparam nmi_address = <%addr_bits%>'h12a;
|
| 72 |
|
|
localparam irq_address = <%addr_bits%>'h12b;
|
| 73 |
|
|
localparam single_step_address = <%addr_bits%>'h12c;
|
| 74 |
|
|
localparam divide_error_address = <%addr_bits%>'h101;
|
| 75 |
|
|
localparam next_instruction_address = <%addr_bits%>'h100;
|
| 76 |
|
|
localparam debug_wait_address = <%addr_bits%>'h102;
|
| 77 |
|
|
localparam do_int_address = <%addr_bits%>'h12d;
|
| 78 |
|
|
|
| 79 |
|
|
typedef struct packed {
|
| 80 |
|
|
<%#fields%>
|
| 81 |
|
|
logic <%type%><%name%>;
|
| 82 |
|
|
<%/fields%>
|
| 83 |
|
|
} microcode_instruction;
|
| 84 |
|
|
|
| 85 |
|
|
microcode_instruction mem[num_instructions] /* synthesis ram_init_file = "microcode.mif" */;
|
| 86 |
|
|
microcode_instruction current;
|
| 87 |
|
|
reg [addr_bits-1:0] addr;
|
| 88 |
|
|
wire [addr_bits-1:0] next_addr;
|
| 89 |
|
|
|
| 90 |
|
|
assign segment_override = current.prefix_type == PrefixType_SEGMENT_OVERRIDE;
|
| 91 |
|
|
assign use_microcode_immediate = |current.immediate;
|
| 92 |
|
|
|
| 93 |
|
|
always_comb begin
|
| 94 |
|
|
case (current.immediate)
|
| 95 |
|
|
<%#immediates%>
|
| 96 |
|
|
<%idx%>: microcode_immediate = 16'h<%val%>;
|
| 97 |
|
|
<%/immediates%>
|
| 98 |
|
|
default: microcode_immediate = 16'h0;
|
| 99 |
|
|
endcase
|
| 100 |
|
|
end
|
| 101 |
|
|
|
| 102 |
|
|
always_comb begin
|
| 103 |
|
|
case (current.update_flags)
|
| 104 |
|
|
<%#flags%>
|
| 105 |
|
|
<%idx%>: update_flags = 9'h<%val%>;
|
| 106 |
|
|
<%/flags%>
|
| 107 |
|
|
default: update_flags = 9'h0;
|
| 108 |
|
|
endcase
|
| 109 |
|
|
end
|
| 110 |
|
|
|
| 111 |
|
|
<%#exported_fields%>
|
| 112 |
|
|
assign <%name%> = current.<%name%>;
|
| 113 |
|
|
<%/exported_fields%>
|
| 114 |
|
|
|
| 115 |
|
|
assign fifo_rd_en = !stall && next_addr == {{addr_bits-8{1'b0}}, fifo_rd_data};
|
| 116 |
|
|
|
| 117 |
|
|
reg [1:0] rep_prefix_type;
|
| 118 |
|
|
wire has_rep_prefix = (rep_prefix_type == PrefixType_REPE ||
|
| 119 |
|
|
rep_prefix_type == PrefixType_REPNE);
|
| 120 |
|
|
reg rep_complete;
|
| 121 |
|
|
|
| 122 |
|
|
assign debug_stopped = addr == debug_wait_address;
|
| 123 |
|
|
assign multibit_shift = opcode == 8'hd2 ||
|
| 124 |
|
|
opcode == 8'hd3 ||
|
| 125 |
|
|
opcode == 8'hc0 ||
|
| 126 |
|
|
opcode == 8'hc1;
|
| 127 |
|
|
assign do_escape_fault = opcode[7:3] == 5'b11011 && next_addr == do_int_address;
|
| 128 |
|
|
|
| 129 |
|
|
reg nmi_pending;
|
| 130 |
|
|
reg ext_int_inhibit;
|
| 131 |
|
|
wire take_nmi = (nmi_pending | nmi_pulse) & !ext_int_inhibit;
|
| 132 |
|
|
wire take_irq = intr & int_enabled & !ext_int_inhibit;
|
| 133 |
|
|
wire do_single_step = current.next_instruction & !ext_int_inhibit &
|
| 134 |
|
|
(trap_flag_set | (start_instruction & tf)) &
|
| 135 |
|
|
current.next != debug_wait_address;
|
| 136 |
|
|
assign start_interrupt = next_addr == nmi_address ||
|
| 137 |
|
|
next_addr == irq_address;
|
| 138 |
|
|
assign irq_to_mdr = next_addr == irq_address;
|
| 139 |
|
|
reg trap_flag_set;
|
| 140 |
|
|
assign is_hlt = opcode == 8'hf4;
|
| 141 |
|
|
|
| 142 |
|
|
reg seized;
|
| 143 |
|
|
wire seizing = debug_seize & ~seized;
|
| 144 |
|
|
|
| 145 |
|
|
reg start_instruction;
|
| 146 |
|
|
|
| 147 |
|
|
assign loop_next = !stall && current.jump_type == JumpType_LOOP_DONE;
|
| 148 |
|
|
|
| 149 |
|
|
assign reg_wr_en = current.rd_sel_source != RDSelSource_NONE & ~segment_wr_en;
|
| 150 |
|
|
|
| 151 |
|
|
assign next_microinstruction = addr != next_addr;
|
| 152 |
|
|
|
| 153 |
|
|
always_comb begin
|
| 154 |
|
|
case (current.width)
|
| 155 |
|
|
WidthType_W8: width = 1'b1;
|
| 156 |
|
|
WidthType_W16: width = 1'b0;
|
| 157 |
|
|
WidthType_WAUTO: width = ~opcode[0];
|
| 158 |
|
|
default: width = 1'b0;
|
| 159 |
|
|
endcase
|
| 160 |
|
|
end
|
| 161 |
|
|
|
| 162 |
|
|
always_ff @(posedge clk)
|
| 163 |
|
|
inta <= next_addr == irq_address && addr != irq_address;
|
| 164 |
|
|
|
| 165 |
|
|
always_ff @(posedge clk or posedge reset)
|
| 166 |
|
|
if (reset)
|
| 167 |
|
|
start_instruction <= 1'b0;
|
| 168 |
|
|
else
|
| 169 |
|
|
start_instruction <= fifo_rd_en;
|
| 170 |
|
|
|
| 171 |
|
|
always_ff @(posedge clk or posedge reset)
|
| 172 |
|
|
if (reset)
|
| 173 |
|
|
trap_flag_set <= 1'b0;
|
| 174 |
|
|
else if (next_addr == single_step_address)
|
| 175 |
|
|
trap_flag_set <= 1'b0;
|
| 176 |
|
|
else if (start_instruction)
|
| 177 |
|
|
trap_flag_set <= tf;
|
| 178 |
|
|
|
| 179 |
|
|
always_ff @(posedge clk or posedge reset)
|
| 180 |
|
|
if (reset)
|
| 181 |
|
|
ext_int_inhibit <= 1'b0;
|
| 182 |
|
|
else if (current.ext_int_inhibit && current.next != debug_wait_address)
|
| 183 |
|
|
ext_int_inhibit <= 1'b1;
|
| 184 |
|
|
else if (fifo_rd_en && !stall)
|
| 185 |
|
|
ext_int_inhibit <= 1'b0;
|
| 186 |
|
|
|
| 187 |
|
|
`ifdef verilator
|
| 188 |
|
|
initial $readmemb({{`MICROCODE_ROM_PATH, "/microcode.bin"}}, mem);
|
| 189 |
|
|
`endif
|
| 190 |
|
|
|
| 191 |
|
|
always_comb begin
|
| 192 |
|
|
case (rep_prefix_type)
|
| 193 |
|
|
PrefixType_REPE: rep_complete = ~zf;
|
| 194 |
|
|
PrefixType_REPNE: rep_complete = zf;
|
| 195 |
|
|
default: rep_complete = 1'b0;
|
| 196 |
|
|
endcase
|
| 197 |
|
|
end
|
| 198 |
|
|
|
| 199 |
|
|
always_ff @(posedge clk or posedge reset)
|
| 200 |
|
|
if (reset)
|
| 201 |
|
|
nmi_pending <= 1'b0;
|
| 202 |
|
|
else if (next_addr == nmi_address)
|
| 203 |
|
|
nmi_pending <= 1'b0;
|
| 204 |
|
|
else if (nmi_pulse)
|
| 205 |
|
|
nmi_pending <= 1'b1;
|
| 206 |
|
|
|
| 207 |
|
|
always_ff @(posedge clk or posedge reset)
|
| 208 |
|
|
if (reset)
|
| 209 |
|
|
opcode <= 8'b0;
|
| 210 |
|
|
else if (fifo_rd_en)
|
| 211 |
|
|
opcode <= fifo_rd_data;
|
| 212 |
|
|
|
| 213 |
|
|
always_ff @(posedge clk or posedge reset)
|
| 214 |
|
|
if (reset)
|
| 215 |
|
|
rep_prefix_type <= 2'b0;
|
| 216 |
|
|
else if (next_instruction)
|
| 217 |
|
|
rep_prefix_type <= 2'b0;
|
| 218 |
|
|
else if (current.prefix_type == PrefixType_REPNE ||
|
| 219 |
|
|
current.prefix_type == PrefixType_REPE)
|
| 220 |
|
|
rep_prefix_type <= current.prefix_type;
|
| 221 |
|
|
|
| 222 |
|
|
always_ff @(posedge clk or posedge reset)
|
| 223 |
|
|
if (reset)
|
| 224 |
|
|
seized <= 1'b0;
|
| 225 |
|
|
else if (debug_stopped)
|
| 226 |
|
|
seized <= 1'b1;
|
| 227 |
|
|
else if (!debug_seize)
|
| 228 |
|
|
seized <= 1'b0;
|
| 229 |
|
|
|
| 230 |
|
|
always_comb begin
|
| 231 |
|
|
if (reset)
|
| 232 |
|
|
next_addr = reset_address;
|
| 233 |
|
|
else if (stall)
|
| 234 |
|
|
next_addr = addr;
|
| 235 |
|
|
else if (debug_stopped && debug_run)
|
| 236 |
|
|
next_addr = {{addr_bits - 9{1'b0}}, 1'b1, debug_addr};
|
| 237 |
|
|
else if (current.ext_int_yield && seizing)
|
| 238 |
|
|
next_addr = debug_wait_address;
|
| 239 |
|
|
else if (current.ext_int_yield && take_nmi)
|
| 240 |
|
|
next_addr = nmi_address;
|
| 241 |
|
|
else if (current.ext_int_yield && take_irq)
|
| 242 |
|
|
next_addr = irq_address;
|
| 243 |
|
|
else if (current.jump_type == JumpType_JUMP_TAKEN)
|
| 244 |
|
|
next_addr = jump_taken ? current.next : addr + 1'b1;
|
| 245 |
|
|
else if (current.jump_type == JumpType_ZERO)
|
| 246 |
|
|
next_addr = zf ? current.next : addr + 1'b1;
|
| 247 |
|
|
else if (current.jump_type == JumpType_HAS_NO_REP_PREFIX)
|
| 248 |
|
|
next_addr = ~has_rep_prefix ? current.next : addr + 1'b1;
|
| 249 |
|
|
else if (current.jump_type == JumpType_REP_NOT_COMPLETE)
|
| 250 |
|
|
next_addr = !rep_complete ? current.next : addr + 1'b1;
|
| 251 |
|
|
else if (current.jump_type == JumpType_DISPATCH_REG)
|
| 252 |
|
|
next_addr = current.next + {{addr_bits-3{1'b0}}, modrm_reg};
|
| 253 |
|
|
else if (current.jump_type == JumpType_RM_REG_MEM)
|
| 254 |
|
|
next_addr = current.next + {{addr_bits-1{1'b0}}, ~rm_is_reg};
|
| 255 |
|
|
else if (current.jump_type == JumpType_OPCODE)
|
| 256 |
|
|
next_addr = !fifo_empty ? {{addr_bits-8{1'b0}}, fifo_rd_data} : addr;
|
| 257 |
|
|
else if (current.jump_type == JumpType_RB_ZERO)
|
| 258 |
|
|
next_addr = rb_zero ? current.next : addr + 1'b1;
|
| 259 |
|
|
else if (current.jump_type == JumpType_LOOP_DONE)
|
| 260 |
|
|
next_addr = loop_done ? current.next : addr + 1'b1;
|
| 261 |
|
|
else if (divide_error)
|
| 262 |
|
|
next_addr = divide_error_address;
|
| 263 |
|
|
else if (current.next_instruction && take_nmi)
|
| 264 |
|
|
next_addr = nmi_address;
|
| 265 |
|
|
else if (current.next_instruction && take_irq)
|
| 266 |
|
|
next_addr = irq_address;
|
| 267 |
|
|
else if (current.next_instruction && do_single_step)
|
| 268 |
|
|
next_addr = single_step_address;
|
| 269 |
|
|
else if (current.next_instruction && debug_seize)
|
| 270 |
|
|
next_addr = debug_wait_address;
|
| 271 |
|
|
else if (current.next_instruction)
|
| 272 |
|
|
next_addr = !fifo_empty && !fifo_resetting ?
|
| 273 |
|
|
{{addr_bits-8{1'b0}}, fifo_rd_data} : next_instruction_address;
|
| 274 |
|
|
else
|
| 275 |
|
|
next_addr = current.next;
|
| 276 |
|
|
end
|
| 277 |
|
|
|
| 278 |
|
|
always @(posedge clk)
|
| 279 |
|
|
addr <= next_addr;
|
| 280 |
|
|
|
| 281 |
|
|
always @(posedge clk)
|
| 282 |
|
|
current <= mem[next_addr];
|
| 283 |
|
|
|
| 284 |
|
|
always_ff @(posedge clk or posedge reset)
|
| 285 |
|
|
if (reset)
|
| 286 |
|
|
lock <= 1'b0;
|
| 287 |
|
|
else if (!stall && current.next_instruction)
|
| 288 |
|
|
lock <= 1'b0;
|
| 289 |
|
|
else if (opcode == 8'hf0)
|
| 290 |
|
|
lock <= 1'b1;
|
| 291 |
|
|
|
| 292 |
|
|
`ifdef verilator
|
| 293 |
|
|
export "DPI-C" function get_microcode_address;
|
| 294 |
|
|
|
| 295 |
|
|
function [addr_bits-1:0] get_microcode_address;
|
| 296 |
|
|
get_microcode_address = addr;
|
| 297 |
|
|
endfunction
|
| 298 |
|
|
|
| 299 |
|
|
export "DPI-C" function get_ext_int_yield;
|
| 300 |
|
|
|
| 301 |
|
|
function logic get_ext_int_yield;
|
| 302 |
|
|
get_ext_int_yield = current.ext_int_yield;
|
| 303 |
|
|
endfunction
|
| 304 |
|
|
|
| 305 |
|
|
int microcode_coverage[num_instructions];
|
| 306 |
|
|
|
| 307 |
|
|
always_ff @(posedge clk)
|
| 308 |
|
|
microcode_coverage[addr] <= microcode_coverage[addr] + 1;
|
| 309 |
|
|
|
| 310 |
|
|
export "DPI-C" function get_microcode_num_instructions;
|
| 311 |
|
|
|
| 312 |
|
|
function int get_microcode_num_instructions;
|
| 313 |
|
|
get_microcode_num_instructions = num_instructions;
|
| 314 |
|
|
endfunction
|
| 315 |
|
|
|
| 316 |
|
|
export "DPI-C" function get_microcode_coverage_bin;
|
| 317 |
|
|
|
| 318 |
|
|
function int get_microcode_coverage_bin;
|
| 319 |
|
|
input int bin;
|
| 320 |
|
|
get_microcode_coverage_bin = microcode_coverage[bin];
|
| 321 |
|
|
endfunction
|
| 322 |
|
|
`endif
|
| 323 |
|
|
|
| 324 |
|
|
endmodule
|