2023-05-29 01:28:56 +00:00
|
|
|
/* decoder.v - Implementation of instruction decoding logic
|
2023-02-17 18:08:09 +00:00
|
|
|
|
|
|
|
This file is part of the 9086 project.
|
|
|
|
|
|
|
|
Copyright (c) 2023 Efthymios Kritikos
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2023-05-11 11:11:17 +00:00
|
|
|
`include "exec_state_def.v"
|
2023-02-17 18:08:09 +00:00
|
|
|
`include "alu_header.v"
|
2023-02-19 16:22:23 +00:00
|
|
|
`include "ucode_header.v"
|
2023-05-07 12:34:15 +00:00
|
|
|
`include "error_header.v"
|
2023-05-27 22:35:00 +00:00
|
|
|
`include "config.v"
|
|
|
|
|
2023-05-29 01:28:56 +00:00
|
|
|
`define DE_STATE_BITS 2
|
|
|
|
`define DE_STATE_ENTRY 2'b01
|
|
|
|
`define DE_HALT 2'b10
|
2023-05-27 22:35:00 +00:00
|
|
|
|
|
|
|
module decoder(
|
2023-06-01 01:13:55 +00:00
|
|
|
/***************** GENERAL *****************/
|
|
|
|
/* Input from sys. */ input clock, input reset
|
|
|
|
/* Output to sys. */ ,output reg MEM_OR_IO_LATCHED, output reg [`ERROR_BITS-1:0] ERROR_LATCHED, output reg HALT_LATCHED
|
|
|
|
|
|
|
|
/*************** INPUT FROM IF ***************/
|
|
|
|
/* */ ,input wire [31:0] IF2DE_INSTRUCTION, input wire VALID_INSTRUCTION
|
|
|
|
/* */ ,input wire [15:0] INSTRUCTION_LOCATION
|
|
|
|
|
|
|
|
/*************** OUTPUT TO IF ***************/
|
|
|
|
/* OUTPUT TO IF */ ,output reg VALID_INSTRUCTION_ACK
|
|
|
|
|
|
|
|
/*************** INPUT FROM EX ***************/
|
|
|
|
/* */ ,input wire [7:0] EX2DE_FLAGS,input wire next_exec
|
|
|
|
|
|
|
|
/*************** OUTPUT TO DE ***************/
|
|
|
|
/* SYNC SIGNALS */ ,output reg set_initial_values, output reg valid_exec_data
|
|
|
|
/* INSTR. PARAMS */ ,output reg Wbit_LATCHED, output reg [2:0] IN_MOD_LATCHED, output reg [2:0] OUT_MOD_LATCHED, output reg [2:0] RM_LATCHED
|
|
|
|
/* DATA */ ,output reg [15:0] PARAM1_LATCHED, output reg [15:0] PARAM2_LATCHED,output reg [15:0] ProgCount
|
|
|
|
/* STATE CONTROL */ ,output reg [`EXEC_STATE_BITS-1:0] next_state_LATCHED
|
|
|
|
/* ALU CONTROL */ ,output reg [1:0] in_alu_sel1_LATCHED, output reg [1:0] in_alu_sel2_LATCHED,output reg [2:0] ALU_OP_LATCHED
|
|
|
|
/* OTHER */ ,output reg memio_address_select_LATCHED
|
|
|
|
|
|
|
|
/************* OUTPUT TO REGISTERS ************/
|
|
|
|
/* */ ,output reg [3:0] reg_write_addr_LATCHED, output reg [3:0] reg_read_port2_addr_LATCHED, output reg [3:0] reg_read_port1_addr_LATCHED
|
|
|
|
|
2023-05-27 22:35:00 +00:00
|
|
|
`ifdef CALCULATE_IPC
|
2023-06-01 01:13:55 +00:00
|
|
|
/* STATISTICS */ ,output reg new_instruction
|
2023-05-27 22:35:00 +00:00
|
|
|
`endif
|
2023-06-01 01:13:55 +00:00
|
|
|
|
2023-05-27 22:35:00 +00:00
|
|
|
);
|
|
|
|
|
2023-05-29 01:28:56 +00:00
|
|
|
reg SIMPLE_MICRO; /* use simple decodings (=0) or microcode data (=1) */
|
2023-05-27 22:35:00 +00:00
|
|
|
wire [`UCODE_ADDR_BITS-1:0] ucode_seq_addr_entry;
|
|
|
|
reg [`UCODE_ADDR_BITS-1:0] ucode_seq_addr;
|
|
|
|
wire DEPENDS_ON_PREVIOUS;
|
2023-05-29 01:28:56 +00:00
|
|
|
wire set_params;
|
2023-05-27 22:35:00 +00:00
|
|
|
|
2023-06-01 01:13:55 +00:00
|
|
|
wire MEM_OR_IO, HALT,Wbit,memio_address_select;
|
|
|
|
wire [1:0] in_alu_sel1,in_alu_sel2;
|
|
|
|
wire [2:0] IN_MOD,OUT_MOD,RM,ALU_OP;
|
|
|
|
wire [3:0] reg_write_addr,reg_read_port2_addr,reg_read_port1_addr;
|
|
|
|
wire [15:0] PARAM1,PARAM2;
|
|
|
|
wire [`EXEC_STATE_BITS-1:0] next_state;
|
|
|
|
wire [`ERROR_BITS-1:0] ERROR;
|
|
|
|
|
2023-05-27 22:35:00 +00:00
|
|
|
instruction_decode instruction_decode(
|
2023-06-01 01:13:55 +00:00
|
|
|
/* INPUT */ IF2DE_INSTRUCTION,{8'h0,EX2DE_FLAGS}
|
|
|
|
/* MICROCODE */ ,ucode_seq_addr_entry,SIMPLE_MICRO,ucode_seq_addr
|
|
|
|
/* OUTPUT */ ,DEPENDS_ON_PREVIOUS, set_params, MEM_OR_IO,ERROR, HALT
|
|
|
|
|
|
|
|
/*************** INPUT FROM DE ***************/
|
|
|
|
/* INSTR. PARAMS */ ,Wbit, IN_MOD, OUT_MOD, RM
|
|
|
|
/* DATA */ ,PARAM1, PARAM2
|
|
|
|
/* STATE CONTROL */ ,next_state
|
|
|
|
/* ALU CONTROL */ ,in_alu_sel1,in_alu_sel2, ALU_OP
|
|
|
|
/* OTHER */ ,memio_address_select
|
|
|
|
|
|
|
|
/***************** REGISTERS *****************/
|
|
|
|
/* */ ,reg_write_addr, reg_read_port2_addr, reg_read_port1_addr
|
|
|
|
);
|
2023-05-27 22:35:00 +00:00
|
|
|
|
2023-05-29 01:28:56 +00:00
|
|
|
reg [`DE_STATE_BITS-1:0] de_state;
|
2023-05-27 22:35:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
always @(negedge reset) begin
|
2023-05-29 01:28:56 +00:00
|
|
|
de_state <= `DE_HALT; //TODO: race condition ??
|
2023-05-27 22:35:00 +00:00
|
|
|
`ifdef CALCULATE_IPC
|
|
|
|
new_instruction<=0;
|
|
|
|
`endif
|
2023-06-01 01:13:55 +00:00
|
|
|
valid_exec_data<=0;
|
|
|
|
instant_response <= 0;
|
|
|
|
stalled_response <= 0;
|
2023-05-27 22:35:00 +00:00
|
|
|
end
|
2023-06-01 01:13:55 +00:00
|
|
|
|
2023-05-27 22:35:00 +00:00
|
|
|
always @(posedge reset) begin
|
2023-06-01 01:13:55 +00:00
|
|
|
de_state <= `DE_STATE_ENTRY;
|
2023-05-27 22:35:00 +00:00
|
|
|
/* need early init */
|
2023-10-31 19:01:34 +00:00
|
|
|
ucode_seq_addr <= `UCODE_NO_INSTRUCTION;
|
|
|
|
SIMPLE_MICRO <= 0;
|
|
|
|
owe_set_init <= 0;
|
|
|
|
set_initial_values<=0;
|
|
|
|
wait_exec<=0;
|
|
|
|
first_ucode <= 0;
|
|
|
|
HALT_LATCHED <= 0;
|
|
|
|
ERROR_LATCHED <= `ERROR_BITS'h0;
|
2023-05-27 22:35:00 +00:00
|
|
|
VALID_INSTRUCTION_ACK <= 0;
|
|
|
|
end
|
|
|
|
|
|
|
|
wire [2:0] instr_end;
|
|
|
|
InstrSize InstrSize({IF2DE_INSTRUCTION[31:24],IF2DE_INSTRUCTION[21:19]},instr_end);
|
|
|
|
|
|
|
|
reg owe_set_init;
|
|
|
|
|
|
|
|
//TODO: Why do we need to make a local copy on a register for the code inside the always @(next_state) to read it?
|
|
|
|
// For some reason the raw VALID_INSTRUCTION signal reads always 1 and it has something to do with the block
|
|
|
|
// being triggered by next_exec
|
|
|
|
reg VALID_INSTRUCTION_lc;
|
|
|
|
always @(VALID_INSTRUCTION)begin VALID_INSTRUCTION_lc<=VALID_INSTRUCTION; end
|
|
|
|
|
|
|
|
reg instant_response, stalled_response;
|
|
|
|
reg wait_exec;
|
|
|
|
|
|
|
|
always @(next_exec) begin
|
2023-05-29 01:28:56 +00:00
|
|
|
de_state<=`DE_STATE_ENTRY;
|
2023-05-27 22:35:00 +00:00
|
|
|
if ( VALID_INSTRUCTION_lc == 1 && DEPENDS_ON_PREVIOUS == 0 && ucode_seq_addr_entry==`UCODE_NO_INSTRUCTION) begin
|
|
|
|
instant_response <= !instant_response;
|
|
|
|
end else begin
|
|
|
|
wait_exec<=0;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-29 01:28:56 +00:00
|
|
|
reg first_ucode;
|
|
|
|
|
2023-05-27 22:35:00 +00:00
|
|
|
always @(instant_response or stalled_response) begin
|
2023-06-01 01:13:55 +00:00
|
|
|
//`define LATCH(VAR) VAR_LATCHED <= VAR; //TODO would this work?
|
|
|
|
IN_MOD_LATCHED <= IN_MOD;
|
|
|
|
OUT_MOD_LATCHED <= OUT_MOD;
|
|
|
|
RM_LATCHED <= RM;
|
|
|
|
MEM_OR_IO_LATCHED <= MEM_OR_IO;
|
|
|
|
PARAM1_LATCHED <= PARAM1;
|
|
|
|
PARAM2_LATCHED <= PARAM2;
|
|
|
|
ALU_OP_LATCHED <= ALU_OP;
|
|
|
|
in_alu_sel1_LATCHED <= in_alu_sel1;
|
|
|
|
in_alu_sel2_LATCHED <= in_alu_sel2;
|
|
|
|
reg_read_port1_addr_LATCHED <= reg_read_port1_addr;
|
|
|
|
reg_read_port2_addr_LATCHED <= reg_read_port2_addr;
|
|
|
|
reg_write_addr_LATCHED <= reg_write_addr;
|
|
|
|
Wbit_LATCHED <= Wbit;
|
|
|
|
ERROR_LATCHED <= ERROR;
|
|
|
|
HALT_LATCHED <= HALT;
|
|
|
|
next_state_LATCHED <= next_state;
|
|
|
|
memio_address_select_LATCHED <= memio_address_select;
|
2023-05-27 22:35:00 +00:00
|
|
|
|
|
|
|
if ( (ucode_seq_addr==`UCODE_NO_INSTRUCTION) && (ucode_seq_addr_entry!=`UCODE_NO_INSTRUCTION) )begin
|
|
|
|
/* switch to microcode decoding */
|
|
|
|
ucode_seq_addr <= ucode_seq_addr_entry;
|
|
|
|
SIMPLE_MICRO <= 1;
|
2023-05-29 01:28:56 +00:00
|
|
|
first_ucode <= 1;
|
|
|
|
set_initial_values <= !set_initial_values;
|
|
|
|
/*keep de_state the same and rerun decode this time with all the data from the microcode rom*/
|
2023-05-27 22:35:00 +00:00
|
|
|
end else begin
|
2023-05-29 01:28:56 +00:00
|
|
|
if(SIMPLE_MICRO==0||first_ucode==1||owe_set_init==1)begin
|
|
|
|
first_ucode <= 0;
|
|
|
|
/* This runs at the start of the execution of an 8086 instruction */
|
|
|
|
`ifdef DEBUG_PC_ADDRESS
|
|
|
|
$display("Running command at %04x (%08x)",INSTRUCTION_LOCATION,IF2DE_INSTRUCTION);
|
|
|
|
`endif
|
|
|
|
`ifdef CALCULATE_IPC
|
|
|
|
new_instruction <= !new_instruction;
|
|
|
|
`endif
|
|
|
|
owe_set_init<=0;
|
|
|
|
ProgCount <= INSTRUCTION_LOCATION+{12'b0,instr_end};
|
|
|
|
VALID_INSTRUCTION_ACK <= !VALID_INSTRUCTION_ACK;
|
|
|
|
end
|
|
|
|
if(set_params)begin
|
|
|
|
set_initial_values <= !set_initial_values;
|
|
|
|
end
|
2023-05-27 22:35:00 +00:00
|
|
|
/* This runs at the start of each execution cycle, with microcode this is more than once per 8086 instruction */
|
|
|
|
valid_exec_data<=!valid_exec_data;
|
|
|
|
if( SIMPLE_MICRO == 1 ) begin
|
|
|
|
ucode_seq_addr <= ucode_seq_addr_entry; /*Reused for next address*/
|
|
|
|
if( ucode_seq_addr_entry == `UCODE_NO_INSTRUCTION )begin
|
|
|
|
/*Finished microcode*/
|
|
|
|
SIMPLE_MICRO <= 0;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
wait_exec<=1;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
always @(posedge clock) begin
|
2023-05-29 01:28:56 +00:00
|
|
|
case(de_state)
|
|
|
|
`DE_STATE_ENTRY:begin
|
2023-05-27 22:35:00 +00:00
|
|
|
if ( ( VALID_INSTRUCTION==1 || SIMPLE_MICRO == 1 ) && wait_exec==0) begin
|
|
|
|
stalled_response <= !stalled_response;
|
|
|
|
end
|
|
|
|
end
|
2023-05-29 01:28:56 +00:00
|
|
|
`DE_HALT:begin
|
2023-05-27 22:35:00 +00:00
|
|
|
end
|
|
|
|
default:begin
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
|
|
|
|
endmodule
|
|
|
|
|
|
|
|
|
2023-06-01 01:13:55 +00:00
|
|
|
///////////////////////// Instruction specific decoding ///////////////////////////////////
|
2023-05-27 22:35:00 +00:00
|
|
|
|
2023-02-17 18:08:09 +00:00
|
|
|
|
2023-02-19 16:22:23 +00:00
|
|
|
module microcode(
|
2023-06-01 01:13:55 +00:00
|
|
|
input [`UCODE_ADDR_BITS-1:0] ADDR
|
|
|
|
,output [`UCODE_DATA_BITS-1:0] DATA
|
2023-02-19 16:22:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
initial begin
|
2023-11-02 00:23:35 +00:00
|
|
|
`ifndef YOSYS
|
2023-02-19 16:22:23 +00:00
|
|
|
string ucode_path;
|
|
|
|
if($value$plusargs("MICROCODE=%s",ucode_path))begin
|
2023-03-04 06:22:28 +00:00
|
|
|
$readmemb(ucode_path,ucode_rom,0,`UCODE_SIZE-1);
|
2023-02-19 16:22:23 +00:00
|
|
|
end else begin
|
|
|
|
$display("Please supply microcode rom file as a runtime vvp argument +MICROCODE=<path>");
|
|
|
|
$finish;
|
|
|
|
end
|
2023-11-02 00:23:35 +00:00
|
|
|
`else
|
|
|
|
//TODO: don't have it hard coded
|
|
|
|
$readmemb("ucode.txt",ucode_rom,0,`UCODE_SIZE-1);
|
|
|
|
`endif
|
2023-02-19 16:22:23 +00:00
|
|
|
end
|
|
|
|
|
2023-03-04 06:22:28 +00:00
|
|
|
reg [`UCODE_DATA_BITS-1:0] ucode_rom [ 0:`UCODE_SIZE-1 ];
|
2023-02-19 16:22:23 +00:00
|
|
|
|
2023-03-04 06:22:28 +00:00
|
|
|
assign DATA=ucode_rom[ADDR];
|
2023-02-19 16:22:23 +00:00
|
|
|
|
|
|
|
endmodule
|
2023-02-17 18:08:09 +00:00
|
|
|
|
2023-05-27 22:35:00 +00:00
|
|
|
module instruction_decode(
|
2023-06-01 01:13:55 +00:00
|
|
|
/* INPUTS */ input wire [31:0] INSTRUCTION,input wire [15:0] FLAGS
|
|
|
|
/* MICROCODE */ ,output reg [`UCODE_ADDR_BITS-1:0] seq_addr_entry, input wire SIMPLE_MICRO, input wire [`UCODE_ADDR_BITS-1:0] seq_addr_input
|
|
|
|
/* OUTPUT */ ,output reg DEPENDS_ON_PREVIOUS, output reg set_params,output reg MEM_OR_IO, output reg [`ERROR_BITS-1:0] ERROR, output reg HALT
|
|
|
|
|
|
|
|
/*************** INPUT FROM DE ***************/
|
|
|
|
/* INSTR. PARAMS */ ,output reg Wbit, output reg [2:0] IN_MOD, output reg [2:0] OUT_MOD, output reg [2:0] RM
|
|
|
|
/* DATA */ ,output reg [15:0] PARAM1, output reg [15:0] PARAM2
|
|
|
|
/* STATE CONTROL */ ,output reg [`EXEC_STATE_BITS-1:0] next_state
|
|
|
|
/* ALU CONTROL */ ,output reg [1:0] in_alu_sel1, output reg [1:0] in_alu_sel2,output reg [2:0] ALU_OP
|
|
|
|
/* OTHER */ ,output reg memio_address_select
|
|
|
|
|
|
|
|
/***************** REGISTERS *****************/
|
|
|
|
/* */ ,output reg [3:0] reg_write_addr, output reg [3:0] reg_read_port2_addr, output reg [3:0] reg_read_port1_addr
|
2023-02-17 18:08:09 +00:00
|
|
|
);
|
2023-05-27 22:35:00 +00:00
|
|
|
|
2023-05-13 12:45:15 +00:00
|
|
|
/* DEPENDS_ON_PREVIOUS - This encodes weather the instruction requires the previous to be finished in order to be decoded. This, for example, affects
|
|
|
|
* conditional jumps since flags are checked during decode.
|
|
|
|
*/
|
2023-05-11 15:28:10 +00:00
|
|
|
|
|
|
|
/* verilator lint_off UNUSEDSIGNAL */
|
2023-02-19 16:22:23 +00:00
|
|
|
wire [`UCODE_DATA_BITS-1:0] ucode_data;
|
2023-05-11 15:28:10 +00:00
|
|
|
/* verilator lint_on UNUSEDSIGNAL */
|
2023-03-04 06:22:28 +00:00
|
|
|
|
2023-02-22 01:28:23 +00:00
|
|
|
microcode ucode(seq_addr_input,ucode_data);
|
2023-02-19 16:22:23 +00:00
|
|
|
|
2023-05-17 20:28:50 +00:00
|
|
|
`define invalid_instruction next_state=`EXEC_WAIT;ERROR<=`ERR_UNIMPL_INSTRUCTION;IN_MOD=3'b011;seq_addr_entry<=`UCODE_NO_INSTRUCTION;
|
2023-05-19 16:59:20 +00:00
|
|
|
`define unimpl_addressing_mode next_state=`EXEC_WAIT;ERROR <= `ERR_UNIMPL_ADDRESSING_MODE;IN_MOD=3'b011;seq_addr_entry<=`UCODE_NO_INSTRUCTION;
|
2023-03-04 06:22:28 +00:00
|
|
|
|
2023-02-17 18:08:09 +00:00
|
|
|
|
2023-02-24 11:54:13 +00:00
|
|
|
//TODO: A possible optimisation for instruction with 8bit parameter and
|
|
|
|
//opcode_size=0 would be to set PARAM1 here instead of sending execution over
|
2023-05-11 11:11:17 +00:00
|
|
|
//to EXEC_DE_LOAD_8_PARAM
|
2023-02-17 18:08:09 +00:00
|
|
|
|
2023-05-10 03:05:56 +00:00
|
|
|
`define normal_instruction seq_addr_entry<=`UCODE_NO_INSTRUCTION;ERROR<=`ERR_NO_ERROR;HALT<=0;MEM_OR_IO=0;
|
|
|
|
`define normal_microcoded ERROR<=`ERR_NO_ERROR;HALT<=0;MEM_OR_IO=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
|
2023-05-29 01:28:56 +00:00
|
|
|
reg [1:0] PARAM_ACTION;
|
|
|
|
`define NO_LOAD 2'b00
|
|
|
|
`define LOAD_8 2'b01
|
|
|
|
`define LOAD_16 2'b10
|
|
|
|
|
2023-06-01 01:13:55 +00:00
|
|
|
reg Sbit,opcode_size;
|
|
|
|
|
2023-05-29 01:28:56 +00:00
|
|
|
// I use blocking for basically putting names on the different fields of INSTRUCTION and
|
2023-03-04 06:22:28 +00:00
|
|
|
// then branching off of that instead of the raw bits. otherwise the code
|
|
|
|
// would be identical
|
2023-06-01 01:13:55 +00:00
|
|
|
/* verilator lint_off BLKSEQ */
|
2023-05-29 01:28:56 +00:00
|
|
|
always @( FLAGS or INSTRUCTION or SIMPLE_MICRO or seq_addr_input ) begin
|
|
|
|
set_params = 1;
|
|
|
|
PARAM_ACTION = `NO_LOAD;
|
|
|
|
Sbit=0;//TODO: If no Sbit we assume it's 0,right?
|
2023-02-22 01:28:23 +00:00
|
|
|
if (SIMPLE_MICRO==0)begin
|
2023-05-29 01:28:56 +00:00
|
|
|
casez({INSTRUCTION[31:24],INSTRUCTION[21:19]})
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b0000_010?_??? : begin
|
2023-02-24 11:54:13 +00:00
|
|
|
/* ADD - Add Immediate word/byte to accumulator */
|
2023-02-22 01:28:23 +00:00
|
|
|
/* 0 0 0 0 0 1 0 W | DATA | DATA if W |*/
|
2023-05-11 15:28:10 +00:00
|
|
|
opcode_size=0;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
2023-03-04 06:22:28 +00:00
|
|
|
IN_MOD=3'b011;
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b00;
|
|
|
|
in_alu_sel2=2'b01;
|
2023-02-23 14:48:48 +00:00
|
|
|
OUT_MOD=3'b011;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
reg_read_port2_addr={Wbit,3'b000};
|
|
|
|
reg_write_addr={Wbit,3'b000};
|
2023-05-11 15:28:10 +00:00
|
|
|
ALU_OP=`ALU_OP_ADD;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-24 12:27:29 +00:00
|
|
|
if(Wbit)
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_16;
|
2023-02-24 12:27:29 +00:00
|
|
|
else
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_8;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1000_00??_101, /* SUB */
|
|
|
|
11'b1000_00??_000 : /* ADD */ begin
|
2023-02-24 11:54:13 +00:00
|
|
|
/* ADD - Add Immediate word/byte to register/memory */
|
2023-02-22 01:28:23 +00:00
|
|
|
/* 1 0 0 0 0 0 S W | MOD 0 0 0 R/M | < DISP LO > | < DISP HI > | DATA | DATA if W | */
|
2023-02-26 02:46:43 +00:00
|
|
|
/* SUB - Subtract immediate word/byte from register/memory */
|
2023-02-24 14:09:10 +00:00
|
|
|
/* 1 0 0 0 0 0 S W | MOD 1 0 1 R/M | < DISP LO > | < DISP HI > | DATA | DATA if W | */
|
2023-02-22 01:28:23 +00:00
|
|
|
opcode_size=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
|
|
|
Sbit=INSTRUCTION[25:25];
|
|
|
|
IN_MOD={1'b0,INSTRUCTION[23:22]};
|
|
|
|
RM=INSTRUCTION[18:16];
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b00;
|
2023-03-04 06:22:28 +00:00
|
|
|
if(IN_MOD==3'b011)begin
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b01;
|
2023-02-24 14:09:10 +00:00
|
|
|
reg_read_port2_addr={Wbit,RM};
|
|
|
|
reg_write_addr={Wbit,RM};
|
|
|
|
end else begin
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b00;
|
2023-02-24 14:09:10 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
OUT_MOD=IN_MOD;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-05-29 01:28:56 +00:00
|
|
|
case({Sbit,Wbit}) // TODO: Isn't this supposed to be just a LOAD_8?
|
2023-02-24 11:54:13 +00:00
|
|
|
2'b00,2'b11:begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_8;
|
2023-02-24 11:54:13 +00:00
|
|
|
end
|
|
|
|
2'b01:begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_16;
|
2023-02-24 11:54:13 +00:00
|
|
|
end
|
|
|
|
default:begin
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
endcase
|
2023-05-29 01:28:56 +00:00
|
|
|
case(INSTRUCTION[21:19])
|
2023-05-11 15:28:10 +00:00
|
|
|
3'b000: ALU_OP=`ALU_OP_ADD;
|
|
|
|
3'b101: ALU_OP=`ALU_OP_SUB_REVERSE;
|
2023-02-24 12:18:17 +00:00
|
|
|
default:begin
|
|
|
|
/*Should be impossible*/
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
endcase
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1000_00??_111 : begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* CMP - compare Immediate with register / memory */
|
|
|
|
/* 1 0 0 0 0 0 S W | MOD 1 1 1 R/M | < DISP LO > | < DISP HI > | DATA | DATA if W | */
|
|
|
|
opcode_size=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
|
|
|
Sbit=INSTRUCTION[25:25];
|
|
|
|
IN_MOD={1'b0,INSTRUCTION[23:22]};
|
|
|
|
RM=INSTRUCTION[18:16];
|
2023-05-11 11:11:17 +00:00
|
|
|
if ( {Sbit,Wbit} == 2'b10 )begin
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
in_alu_sel1=2'b00;
|
2023-02-24 02:18:48 +00:00
|
|
|
OUT_MOD=3'b100;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-05-19 16:59:20 +00:00
|
|
|
ALU_OP=`ALU_OP_SUB_REVERSE;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
if(IN_MOD==3'b011)begin
|
2023-02-24 02:18:48 +00:00
|
|
|
/*compare register with param*/
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b01;
|
2023-02-22 01:28:23 +00:00
|
|
|
reg_read_port2_addr={Wbit,RM};
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-02-22 01:28:23 +00:00
|
|
|
end else begin
|
2023-02-24 02:18:48 +00:00
|
|
|
/*compare register indirect access
|
|
|
|
* with param */
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b00;
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_MEMIO_READ;
|
2023-05-21 00:48:50 +00:00
|
|
|
/*will call MEMIO_READ after EXEC_DE_LOAD..*/
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-05-21 00:48:50 +00:00
|
|
|
if(Wbit)
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_16;
|
2023-05-21 00:48:50 +00:00
|
|
|
else
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_8;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-05-29 01:28:56 +00:00
|
|
|
11'b1011_????_??? : begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* MOV - Move Immediate byte to register */
|
|
|
|
/* 1 0 1 1 W REG | DATA | DATA if W |*/
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[27:27]; /* IS 0 */
|
2023-02-22 01:28:23 +00:00
|
|
|
opcode_size=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
IN_MOD=3'b011;
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b00;
|
|
|
|
in_alu_sel2=2'b00;
|
2023-02-23 14:48:48 +00:00
|
|
|
OUT_MOD=3'b011;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-05-29 01:28:56 +00:00
|
|
|
reg_write_addr={Wbit,INSTRUCTION[26:24]};
|
|
|
|
if(Wbit)
|
|
|
|
PARAM_ACTION=`LOAD_16;
|
|
|
|
else
|
|
|
|
PARAM_ACTION=`LOAD_8;
|
2023-02-22 01:28:23 +00:00
|
|
|
PARAM2=0;
|
2023-05-11 15:28:10 +00:00
|
|
|
ALU_OP=`ALU_OP_ADD;
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-19 16:22:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1000_10??_??? : begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* MOV - Reg/Mem to/from register */
|
|
|
|
/* 1 0 0 0 1 0 D W | MOD REG RM | < DISP LO > | < DISP HI > |*/
|
|
|
|
opcode_size=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
RM=INSTRUCTION[18:16];
|
|
|
|
Wbit=INSTRUCTION[24:24];
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b00;
|
2023-02-24 02:18:48 +00:00
|
|
|
PARAM1=0;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-05-29 01:28:56 +00:00
|
|
|
if(INSTRUCTION[25:25] == 1)begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* Mem/Reg to reg */
|
2023-05-29 01:28:56 +00:00
|
|
|
IN_MOD={1'b0,INSTRUCTION[23:22]};
|
2023-03-04 06:22:28 +00:00
|
|
|
if(IN_MOD==3'b011)begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/*Reg to Reg*/
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b01;
|
2023-02-24 02:18:48 +00:00
|
|
|
reg_read_port2_addr={Wbit,RM};
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-02-22 01:28:23 +00:00
|
|
|
end else begin
|
|
|
|
/*Mem to Reg*/
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b00;
|
|
|
|
next_state=`EXEC_MEMIO_READ;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-02-23 14:48:48 +00:00
|
|
|
OUT_MOD=3'b011;
|
2023-05-29 01:28:56 +00:00
|
|
|
reg_write_addr={Wbit,INSTRUCTION[21:19]};
|
2023-02-17 18:08:09 +00:00
|
|
|
end else begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* Reg to Mem/Reg */
|
2023-03-03 06:29:06 +00:00
|
|
|
IN_MOD=3'b011;
|
2023-05-29 01:28:56 +00:00
|
|
|
OUT_MOD={1'b0,INSTRUCTION[23:22]};
|
2023-03-04 06:22:28 +00:00
|
|
|
if(IN_MOD==3'b011)begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/*Reg to Reg*/
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b01;
|
2023-02-22 01:28:23 +00:00
|
|
|
reg_write_addr={Wbit,RM};
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-02-22 01:28:23 +00:00
|
|
|
end else begin
|
|
|
|
/*Reg to Mem*/
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b00;
|
|
|
|
next_state=`EXEC_DE_LOAD_REG_TO_PARAM;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-05-29 01:28:56 +00:00
|
|
|
reg_read_port2_addr={Wbit,INSTRUCTION[21:19]};
|
2023-02-17 18:08:09 +00:00
|
|
|
end
|
2023-05-11 15:28:10 +00:00
|
|
|
ALU_OP=`ALU_OP_ADD;
|
2023-03-04 06:22:28 +00:00
|
|
|
|
|
|
|
`normal_instruction;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b0100_????_???:begin//DEC
|
2023-02-22 01:28:23 +00:00
|
|
|
/* DEC - Decrement Register */
|
|
|
|
/* | 0 1 0 0 1 REG | */
|
|
|
|
/* INC - Increment Register */
|
|
|
|
/* | 0 1 0 0 0 REG | */
|
|
|
|
opcode_size=0;
|
|
|
|
Wbit=1;
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b01;
|
|
|
|
in_alu_sel2=2'b00;
|
2023-02-23 14:48:48 +00:00
|
|
|
OUT_MOD=3'b011;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
IN_MOD=3'b011;
|
2023-02-22 01:28:23 +00:00
|
|
|
PARAM2=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
reg_read_port1_addr={1'b1,INSTRUCTION[26:24]};
|
|
|
|
reg_write_addr={1'b1,INSTRUCTION[26:24]};
|
|
|
|
if(INSTRUCTION[27:27]==0)
|
2023-05-11 15:28:10 +00:00
|
|
|
ALU_OP=`ALU_OP_ADD;
|
2023-02-22 01:28:23 +00:00
|
|
|
else
|
2023-05-11 15:28:10 +00:00
|
|
|
ALU_OP=`ALU_OP_SUB;
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1111_111?_00? : begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* INC - Register/Memory */
|
|
|
|
/* 1 1 1 1 1 1 1 W | MOD 0 0 0 R/M | < DISP LO> | < DISP HI> */
|
|
|
|
/* DEC - Register/Memory */
|
|
|
|
/* 1 1 1 1 1 1 1 W | MOD 0 0 1 R/M | < DISP LO> | < DISP HI> */
|
|
|
|
opcode_size=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
|
|
|
IN_MOD={1'b0,INSTRUCTION[23:22]};
|
|
|
|
RM=INSTRUCTION[18:16];
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=(IN_MOD==3'b011)? 2'b01 : 2'b00;
|
|
|
|
in_alu_sel1=2'b00;/* number 1 */
|
2023-02-24 02:18:48 +00:00
|
|
|
PARAM1=1;
|
2023-03-04 06:22:28 +00:00
|
|
|
OUT_MOD=IN_MOD;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-02-17 18:08:09 +00:00
|
|
|
|
2023-03-04 06:22:28 +00:00
|
|
|
/*in case IN_MOD=011 */
|
2023-02-24 02:18:48 +00:00
|
|
|
reg_read_port2_addr={1'b0,RM};
|
2023-02-22 01:28:23 +00:00
|
|
|
reg_write_addr={1'b0,RM};
|
2023-02-17 18:08:09 +00:00
|
|
|
|
2023-05-29 01:28:56 +00:00
|
|
|
ALU_OP=(INSTRUCTION[19:19]==1)?`ALU_OP_SUB_REVERSE:`ALU_OP_ADD;
|
2023-03-04 06:22:28 +00:00
|
|
|
if ( IN_MOD == 3'b011 )
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-02-22 01:28:23 +00:00
|
|
|
else
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_MEMIO_READ;
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1111_0100_??? : begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* HLT - Halt */
|
|
|
|
/* 1 1 1 1 0 1 0 0 | */
|
|
|
|
opcode_size=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
IN_MOD=3'b011;
|
|
|
|
HALT<=1;
|
2023-05-07 12:34:15 +00:00
|
|
|
ERROR<=`ERR_NO_ERROR;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
seq_addr_entry<=`UCODE_NO_INSTRUCTION;
|
2023-05-17 20:28:50 +00:00
|
|
|
next_state=`EXEC_WAIT;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b0011_110?_??? : begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* CMP - Compare Immediate with accumulator */
|
|
|
|
/* 0 0 1 1 1 1 0 W | DATA | DATA if W |*/
|
|
|
|
/* */
|
|
|
|
/* NOTE: 8086 doc doesn't show the third byte but the */
|
|
|
|
/* W flag and my assembler seem to disagree */
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
2023-02-22 01:28:23 +00:00
|
|
|
opcode_size=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
IN_MOD=3'b011;
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b00;
|
|
|
|
in_alu_sel2=2'b01;
|
2023-02-22 01:28:23 +00:00
|
|
|
reg_read_port2_addr={Wbit,3'b000};
|
2023-02-23 14:48:48 +00:00
|
|
|
OUT_MOD=3'b100;
|
2023-05-19 16:59:20 +00:00
|
|
|
ALU_OP=`ALU_OP_SUB_REVERSE;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
if(Wbit==1)
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_16;
|
2023-02-22 01:28:23 +00:00
|
|
|
else begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_8;
|
|
|
|
//PARAM1[7:0]=INSTRUCTION[7:0]; TODO:needed?
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b0111_????_???:begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* Conditional relative jumps */
|
|
|
|
/* JE/JZ - Jump on Zero */
|
|
|
|
/* 0 1 1 1 0 1 0 0 | IP-INC8 |*/
|
|
|
|
/* JS - Jump on Sign */
|
|
|
|
/* 0 1 1 1 1 0 0 0 | IP-INC8 |*/
|
|
|
|
/* JNS -Jump on not Sign */
|
|
|
|
/* 0 1 1 1 1 0 0 1 | IP-INC8 |*/
|
|
|
|
/* .... */
|
|
|
|
Wbit=1;
|
|
|
|
opcode_size=0;
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b10;
|
|
|
|
in_alu_sel2=2'b00;
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM2={{8{INSTRUCTION[23:23]}},INSTRUCTION[23:16]};
|
2023-05-11 15:28:10 +00:00
|
|
|
ALU_OP=`ALU_OP_ADD_SIGNED_B;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-02-23 14:48:48 +00:00
|
|
|
OUT_MOD=3'b101;
|
2023-05-29 01:28:56 +00:00
|
|
|
case(INSTRUCTION[27:25])
|
2023-02-22 01:28:23 +00:00
|
|
|
3'b000: begin
|
|
|
|
/* Jump on (not) Overflow */
|
2023-05-29 01:28:56 +00:00
|
|
|
if(FLAGS[11:11]==INSTRUCTION[24:24])
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_NEXT_INSTRUCTION;
|
2023-02-22 01:28:23 +00:00
|
|
|
else begin
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-05-21 00:48:50 +00:00
|
|
|
`normal_instruction;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
|
|
|
3'b010: begin
|
|
|
|
/* Jump on (not) Zero */
|
2023-05-29 01:28:56 +00:00
|
|
|
if(FLAGS[6:6]==INSTRUCTION[24:24])
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_NEXT_INSTRUCTION;
|
2023-02-22 01:28:23 +00:00
|
|
|
else
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-05-21 00:48:50 +00:00
|
|
|
`normal_instruction;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
|
|
|
3'b100: begin
|
|
|
|
/* Jump on (not) Sign */
|
2023-05-29 01:28:56 +00:00
|
|
|
if(FLAGS[7:7]==INSTRUCTION[24:24])
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_NEXT_INSTRUCTION;
|
2023-02-22 01:28:23 +00:00
|
|
|
else
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-05-21 00:48:50 +00:00
|
|
|
`normal_instruction;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
|
|
|
3'b101: begin
|
|
|
|
/* Jump on (not) Parity */
|
2023-05-29 01:28:56 +00:00
|
|
|
if(FLAGS[2:2]==INSTRUCTION[24:24])
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_NEXT_INSTRUCTION;
|
2023-02-22 01:28:23 +00:00
|
|
|
else
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-05-21 00:48:50 +00:00
|
|
|
`normal_instruction;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-05-19 16:59:20 +00:00
|
|
|
3'b001: begin
|
|
|
|
/* Jump on (not) Carry */
|
2023-05-29 01:28:56 +00:00
|
|
|
if(FLAGS[0:0]==INSTRUCTION[24:24])
|
2023-05-19 16:59:20 +00:00
|
|
|
next_state=`EXEC_NEXT_INSTRUCTION;
|
|
|
|
else
|
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-05-21 00:48:50 +00:00
|
|
|
`normal_instruction;
|
2023-05-19 16:59:20 +00:00
|
|
|
end
|
2023-02-22 01:28:23 +00:00
|
|
|
default:begin
|
|
|
|
`invalid_instruction; /*We don't support that condition*/
|
|
|
|
end
|
|
|
|
endcase
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=1;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1110_1011_???:begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* JMP - Unconditional jump direct within segment (short) */
|
|
|
|
/* | 1 1 1 0 1 0 1 1 | IP-INC-LO | */
|
|
|
|
opcode_size=0;
|
|
|
|
Wbit=1;
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b10;
|
|
|
|
in_alu_sel2=2'b00;
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM2={{8{INSTRUCTION[23:23]}},INSTRUCTION[23:16]};
|
2023-05-11 15:28:10 +00:00
|
|
|
ALU_OP=`ALU_OP_ADD_SIGNED_B;
|
2023-02-23 14:48:48 +00:00
|
|
|
OUT_MOD=3'b101;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1110_1000_???:begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/* CALL - Direct call within segment */
|
|
|
|
/* 1 1 1 0 1 0 0 0 | IP-INC-LO | IP-INC-HI |*/
|
|
|
|
|
|
|
|
// Microcode instruction
|
|
|
|
opcode_size=0;
|
|
|
|
Wbit=1;
|
|
|
|
Sbit=1;
|
2023-02-26 02:46:43 +00:00
|
|
|
PARAM2=2; //subtract from sp
|
2023-03-04 06:22:28 +00:00
|
|
|
seq_addr_entry<=`UCODE_CALL_ENTRY;
|
2023-05-07 12:34:15 +00:00
|
|
|
`normal_microcoded
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1100_0011_???:begin
|
2023-02-23 14:48:48 +00:00
|
|
|
/* RET - Return from call within segment */
|
|
|
|
/* | 1 1 0 0 0 0 1 1 | */
|
|
|
|
|
|
|
|
// Microcode instruction
|
|
|
|
opcode_size=0;
|
|
|
|
Wbit=1;
|
|
|
|
Sbit=0;
|
2023-02-24 07:32:27 +00:00
|
|
|
PARAM1=2;
|
2023-03-04 06:22:28 +00:00
|
|
|
seq_addr_entry<=`UCODE_RET_ENTRY;
|
2023-05-07 12:34:15 +00:00
|
|
|
`normal_microcoded
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-23 14:48:48 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1010_101?_???:begin
|
2023-02-24 05:01:55 +00:00
|
|
|
/* STOS - Write byte/word to [DI] and increment accordingly */
|
|
|
|
/* | 1 0 1 0 1 0 1 W | */
|
|
|
|
opcode_size=0;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
2023-02-24 05:01:55 +00:00
|
|
|
Sbit=0;
|
2023-03-04 06:22:28 +00:00
|
|
|
RM=3'b101;
|
|
|
|
seq_addr_entry<=`UCODE_STOS_ENTRY;
|
2023-05-07 12:34:15 +00:00
|
|
|
`normal_microcoded
|
2023-02-24 05:01:55 +00:00
|
|
|
PARAM2=(Wbit==1)?2:1;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-24 05:01:55 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b0101_0???_???:begin
|
2023-02-24 07:32:27 +00:00
|
|
|
/* PUSH - SP-=2; [SP]=REG */
|
|
|
|
/* | 0 1 0 1 0 REG | */
|
|
|
|
opcode_size=0;
|
|
|
|
Wbit=1;
|
|
|
|
Sbit=0;
|
|
|
|
PARAM2=2;
|
2023-05-29 01:28:56 +00:00
|
|
|
reg_read_port2_addr={1'b1,INSTRUCTION[26:24]};
|
2023-03-04 06:22:28 +00:00
|
|
|
seq_addr_entry<=`UCODE_PUSH_ENTRY;
|
2023-05-07 12:34:15 +00:00
|
|
|
`normal_microcoded
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-02-24 07:32:27 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1111_011?_000:begin
|
2023-05-23 15:18:33 +00:00
|
|
|
/* TEST - Bitwise AND of immediate and registers/memory affecting only flags */
|
2023-02-24 10:08:01 +00:00
|
|
|
/* 1 1 1 1 0 1 1 W | MOD 0 0 0 R/M | < DISP-LO > | < DISP-HI > | DATA | DATA if W */
|
|
|
|
opcode_size=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
|
|
|
IN_MOD={1'b0,INSTRUCTION[23:22]};
|
|
|
|
RM={INSTRUCTION[18:16]};
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-02-24 10:08:01 +00:00
|
|
|
if(Wbit==1)begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_16;
|
2023-02-24 10:08:01 +00:00
|
|
|
end else begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_8;
|
2023-02-24 10:08:01 +00:00
|
|
|
end
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b00; /* PARAM1 */
|
2023-05-11 15:28:10 +00:00
|
|
|
ALU_OP=`ALU_OP_AND;
|
2023-02-24 10:08:01 +00:00
|
|
|
case(IN_MOD)
|
2023-03-04 06:22:28 +00:00
|
|
|
3'b011:begin
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b01;
|
2023-02-24 10:08:01 +00:00
|
|
|
reg_read_port2_addr={Wbit,RM};
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-02-24 10:08:01 +00:00
|
|
|
end
|
|
|
|
default:begin
|
|
|
|
`invalid_instruction
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_MEMIO_READ;
|
2023-02-24 10:08:01 +00:00
|
|
|
end
|
|
|
|
endcase
|
|
|
|
OUT_MOD=3'b100;/*NULL*/
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-24 10:08:01 +00:00
|
|
|
end
|
2023-05-19 16:59:20 +00:00
|
|
|
11'b1010_100?_???:begin
|
|
|
|
/* TEST - Bitwise AND of immediate and accumulator affecting only flags */
|
|
|
|
/* 1 0 1 0 1 0 0 W | DATA | DATA if W | */
|
|
|
|
opcode_size=0;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
2023-05-19 16:59:20 +00:00
|
|
|
IN_MOD=3'b011;
|
|
|
|
RM=3'b000;
|
|
|
|
MEM_OR_IO=0;
|
|
|
|
if(Wbit==1)begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_16;
|
2023-05-19 16:59:20 +00:00
|
|
|
end else begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_8;
|
2023-05-19 16:59:20 +00:00
|
|
|
end
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-05-19 16:59:20 +00:00
|
|
|
in_alu_sel1=2'b00; /* PARAM1 */
|
|
|
|
ALU_OP=`ALU_OP_AND;
|
|
|
|
in_alu_sel2=2'b01;
|
|
|
|
reg_read_port2_addr={Wbit,RM};
|
|
|
|
OUT_MOD=3'b100;/*NULL*/
|
|
|
|
`normal_instruction;
|
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
|
|
|
memio_address_select=0;
|
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b0101_1???_???:begin
|
2023-02-24 11:31:15 +00:00
|
|
|
/* POP - REG=[SP]; SP+=2 */
|
|
|
|
/* | 0 1 0 1 1 REG | */
|
|
|
|
opcode_size=0;
|
|
|
|
Wbit=1;
|
|
|
|
Sbit=0;
|
|
|
|
PARAM1=2;
|
2023-05-29 01:28:56 +00:00
|
|
|
reg_write_addr={1'b1,INSTRUCTION[26:24]};
|
2023-03-04 06:22:28 +00:00
|
|
|
seq_addr_entry<=`UCODE_POP_ENTRY;
|
2023-05-07 12:34:15 +00:00
|
|
|
`normal_microcoded
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-24 11:31:15 +00:00
|
|
|
end
|
2023-02-24 13:04:32 +00:00
|
|
|
11'b1111_1111_100:begin
|
|
|
|
/* JMP - Unconditional indirect within segment jump */
|
2023-02-24 17:36:41 +00:00
|
|
|
/* 1 1 1 1 1 1 1 1 | MOD 1 0 0 R/M | < DISP-LO > | < DISP-HI > */
|
2023-02-24 13:04:32 +00:00
|
|
|
opcode_size=1;
|
|
|
|
Wbit=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
IN_MOD={1'b0,INSTRUCTION[23:22]};
|
|
|
|
RM=INSTRUCTION[18:16];
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b11;
|
2023-03-04 06:22:28 +00:00
|
|
|
if (IN_MOD==3'b011)begin
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b01;
|
2023-02-24 13:04:32 +00:00
|
|
|
reg_read_port2_addr={Wbit,RM};
|
2023-05-11 11:11:17 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-02-24 13:04:32 +00:00
|
|
|
end else begin
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel2=2'b00;
|
|
|
|
next_state=`EXEC_MEMIO_READ;
|
2023-02-24 13:04:32 +00:00
|
|
|
end
|
2023-05-11 15:28:10 +00:00
|
|
|
ALU_OP=`ALU_OP_ADD;
|
2023-02-24 13:04:32 +00:00
|
|
|
OUT_MOD=3'b101;
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-02-24 13:04:32 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
11'b1100_011?_000:begin
|
2023-02-24 15:25:45 +00:00
|
|
|
/* MOV - Move immediate to register/memory */
|
|
|
|
/* 1 1 0 0 0 1 1 W | MOD 0 0 0 R/M | < DISP-LO > | < DISP-HI > | DATA | DATA if W */
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
2023-02-24 15:25:45 +00:00
|
|
|
opcode_size=1;
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b00;
|
|
|
|
in_alu_sel2=2'b11;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-02-24 15:25:45 +00:00
|
|
|
if(Wbit==1)begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_16;
|
2023-02-24 15:25:45 +00:00
|
|
|
end else begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_8;
|
2023-02-24 15:25:45 +00:00
|
|
|
end
|
|
|
|
|
2023-05-29 01:28:56 +00:00
|
|
|
OUT_MOD={1'b0,INSTRUCTION[23:22]};
|
2023-02-24 15:25:45 +00:00
|
|
|
IN_MOD=3'b011;
|
2023-05-29 01:28:56 +00:00
|
|
|
RM=INSTRUCTION[18:16];
|
2023-03-04 06:22:28 +00:00
|
|
|
`normal_instruction;
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-03-08 07:26:28 +00:00
|
|
|
end
|
|
|
|
11'b1100_1101_???:begin
|
2023-05-23 15:18:33 +00:00
|
|
|
/* INT - Execute interrupt handler */
|
2023-03-08 07:26:28 +00:00
|
|
|
/* 1 1 0 0 1 1 0 1 | DATA |*/
|
2023-03-21 14:51:39 +00:00
|
|
|
// [skipped] 1) push flags
|
|
|
|
// [skipped] 2) clear trap and interrupt enable flag
|
|
|
|
// [skipped] 3) push CS
|
|
|
|
// [skipped] 4) fetch CS from interrupt table
|
|
|
|
// 5) push ProgCount
|
|
|
|
// 6) fetch ProgCount from interrupt table
|
2023-03-08 07:26:28 +00:00
|
|
|
opcode_size=0;
|
|
|
|
Wbit=1;
|
|
|
|
Sbit=0;
|
|
|
|
PARAM2=2;
|
|
|
|
seq_addr_entry<=`UCODE_INT_ENTRY;
|
2023-05-07 12:34:15 +00:00
|
|
|
`normal_microcoded
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=0;
|
2023-03-09 06:02:41 +00:00
|
|
|
end
|
|
|
|
11'b1110_011?_???:begin
|
2023-05-23 15:18:33 +00:00
|
|
|
/* OUT - Write AL or AX to a defined output port */
|
2023-03-09 06:02:41 +00:00
|
|
|
/* | 1 1 1 0 0 1 1 W | DATA 8 | */
|
|
|
|
memio_address_select=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
2023-03-09 06:02:41 +00:00
|
|
|
opcode_size=0;
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1=2'b00;
|
|
|
|
in_alu_sel2=2'b11;
|
2023-03-09 06:02:41 +00:00
|
|
|
reg_read_port1_addr={Wbit,3'b000};
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_8;
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=1;
|
2023-05-10 03:05:56 +00:00
|
|
|
HALT <= 0;
|
2023-03-09 06:02:41 +00:00
|
|
|
PARAM1=0;
|
|
|
|
OUT_MOD={3'b000};
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-09 06:02:41 +00:00
|
|
|
IN_MOD=3'b011;
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-03-09 06:02:41 +00:00
|
|
|
end
|
|
|
|
11'b1100_1111_???:begin
|
|
|
|
/* IRET - Return from interrupt */
|
|
|
|
/* | 1 1 0 0 1 1 1 1 | */
|
2023-03-21 14:51:39 +00:00
|
|
|
// Since we only push one thing on the stack
|
2023-03-09 06:02:41 +00:00
|
|
|
// on INT we can just reuse the code from RET
|
|
|
|
opcode_size=0;
|
|
|
|
Wbit=1;
|
|
|
|
Sbit=0;
|
|
|
|
PARAM1=2;
|
|
|
|
seq_addr_entry<=`UCODE_RET_ENTRY;
|
2023-05-07 12:34:15 +00:00
|
|
|
`normal_microcoded
|
2023-05-13 12:45:15 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-03-09 06:02:41 +00:00
|
|
|
memio_address_select=0;
|
2023-02-24 15:25:45 +00:00
|
|
|
end
|
2023-05-19 16:59:20 +00:00
|
|
|
11'b1000_000?_100,11'b1000_000?_001:begin
|
|
|
|
/* OR - Bitwise OR immediate and register/mem */
|
|
|
|
/* 1 0 0 0 0 0 0 W | MOD 0 0 1 R/M | < DISP-LO > | < DISP-HI > | DATA | DATA if W */
|
|
|
|
/* AND - Bitwise AND immediate and register/mem */
|
|
|
|
/* 1 0 0 0 0 0 0 W | MOD 1 0 0 R/M | < DISP-LO > | < DISP-HI > | DATA | DATA if W */
|
|
|
|
opcode_size=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
|
|
|
IN_MOD={1'b0,INSTRUCTION[23:22]};
|
|
|
|
RM={INSTRUCTION[18:16]};
|
2023-05-19 16:59:20 +00:00
|
|
|
MEM_OR_IO=0;
|
|
|
|
if(Wbit==1)begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_16;
|
2023-05-19 16:59:20 +00:00
|
|
|
end else begin
|
2023-05-29 01:28:56 +00:00
|
|
|
PARAM_ACTION=`LOAD_8;
|
2023-05-19 16:59:20 +00:00
|
|
|
end
|
|
|
|
in_alu_sel1=2'b00; /* PARAM1 */
|
2023-05-29 01:28:56 +00:00
|
|
|
case(INSTRUCTION[21:19])
|
2023-05-19 16:59:20 +00:00
|
|
|
3'b100: ALU_OP=`ALU_OP_AND;
|
|
|
|
3'b001: ALU_OP=`ALU_OP_OR;
|
|
|
|
default:begin end
|
|
|
|
endcase
|
|
|
|
case(IN_MOD)
|
|
|
|
3'b011:begin
|
|
|
|
in_alu_sel2=2'b01;
|
|
|
|
reg_read_port2_addr={Wbit,RM};
|
|
|
|
reg_write_addr={Wbit,RM};
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
2023-05-19 16:59:20 +00:00
|
|
|
end
|
|
|
|
default:begin
|
|
|
|
`unimpl_addressing_mode
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_MEMIO_READ;
|
2023-05-19 16:59:20 +00:00
|
|
|
end
|
|
|
|
endcase
|
|
|
|
OUT_MOD=IN_MOD;
|
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
|
|
|
memio_address_select=0;
|
|
|
|
`normal_instruction;
|
|
|
|
end
|
2023-05-21 00:48:50 +00:00
|
|
|
11'b0000_00??_???,11'b0010_10??_???,11'b0011_10??_???:begin
|
|
|
|
/* CMP - Compare Register/memory and register */
|
|
|
|
/* 0 0 1 1 1 0 D W | MOD REG R/M | < DISP LO > | < DISP HI > | */
|
|
|
|
/* SUB - Reg/memory with register to either */
|
|
|
|
/* 0 0 1 0 1 0 D W | MOD REG R/M | < DISP LO > | < DISP HI > | */
|
2023-05-19 16:59:20 +00:00
|
|
|
/* ADD - Reg/memory with register to either */
|
|
|
|
/* 0 0 0 0 0 0 D W | MOD REG R/M | < DISP LO > | < DISP HI > | */
|
|
|
|
opcode_size=1;
|
2023-05-29 01:28:56 +00:00
|
|
|
Wbit=INSTRUCTION[24:24];
|
2023-05-19 16:59:20 +00:00
|
|
|
Sbit=0;
|
|
|
|
IN_MOD=3'b011;
|
2023-05-29 01:28:56 +00:00
|
|
|
RM=INSTRUCTION[18:16];
|
2023-05-19 16:59:20 +00:00
|
|
|
in_alu_sel1=2'b01;//constantly register
|
2023-05-29 01:28:56 +00:00
|
|
|
reg_read_port1_addr={Wbit,INSTRUCTION[21:19]};
|
2023-05-19 16:59:20 +00:00
|
|
|
if(IN_MOD==3'b011)begin
|
|
|
|
in_alu_sel2=2'b01;
|
|
|
|
reg_read_port2_addr={Wbit,RM};
|
|
|
|
reg_write_addr={Wbit,RM};
|
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
|
|
|
end else begin
|
|
|
|
in_alu_sel2=2'b00;
|
2023-05-29 01:28:56 +00:00
|
|
|
if(Wbit==1)begin
|
|
|
|
PARAM_ACTION=`LOAD_16;
|
|
|
|
end else begin
|
|
|
|
PARAM_ACTION=`LOAD_8;
|
|
|
|
end
|
2023-05-19 16:59:20 +00:00
|
|
|
end
|
|
|
|
MEM_OR_IO=0;
|
|
|
|
memio_address_select=0;
|
2023-05-29 01:28:56 +00:00
|
|
|
case (INSTRUCTION[29:26])
|
2023-05-21 00:48:50 +00:00
|
|
|
4'b0000: ALU_OP=`ALU_OP_ADD;
|
|
|
|
4'b1010: ALU_OP=`ALU_OP_SUB;
|
|
|
|
4'b1110: ALU_OP=`ALU_OP_SUB_REVERSE;
|
|
|
|
default: begin end
|
|
|
|
endcase
|
2023-05-29 01:28:56 +00:00
|
|
|
case (INSTRUCTION[29:26])
|
|
|
|
4'b0000: OUT_MOD={1'b0,INSTRUCTION[23:22]};
|
|
|
|
4'b1010: OUT_MOD={1'b0,INSTRUCTION[23:22]};
|
2023-05-21 00:48:50 +00:00
|
|
|
4'b1110: OUT_MOD=3'b100; /* NULL */
|
|
|
|
default: begin end
|
|
|
|
endcase
|
2023-05-19 16:59:20 +00:00
|
|
|
DEPENDS_ON_PREVIOUS<=0;
|
2023-05-29 01:28:56 +00:00
|
|
|
next_state=`EXEC_WRITE_ENTRY;
|
|
|
|
if(INSTRUCTION[25:25]==1'b0) begin
|
2023-05-21 00:48:50 +00:00
|
|
|
`normal_instruction;
|
|
|
|
end else begin
|
|
|
|
`unimpl_addressing_mode;
|
|
|
|
end
|
2023-05-19 16:59:20 +00:00
|
|
|
end
|
2023-02-22 01:28:23 +00:00
|
|
|
default:begin
|
|
|
|
`invalid_instruction
|
2023-02-19 16:22:23 +00:00
|
|
|
end
|
2023-02-22 01:28:23 +00:00
|
|
|
endcase
|
|
|
|
end else begin
|
|
|
|
/*Microcode output*/
|
|
|
|
//Sbit, Wbit, opcode_size and the others are still latched
|
|
|
|
//from when we ordered the switch to microcode
|
2023-03-04 06:22:28 +00:00
|
|
|
seq_addr_entry <= ucode_data[`UCODE_ADDR_BITS-1:0];
|
2023-03-08 07:26:28 +00:00
|
|
|
case(ucode_data[8:6])
|
2023-05-29 01:28:56 +00:00
|
|
|
3'b000: begin next_state=`EXEC_WRITE_ENTRY; set_params = 0; end
|
|
|
|
3'b001: begin next_state=`EXEC_WRITE_ENTRY; PARAM_ACTION=`LOAD_16;end
|
|
|
|
3'b010: begin next_state=`EXEC_WRITE_ENTRY; PARAM_ACTION=`LOAD_8;end
|
|
|
|
3'b011: begin next_state=`EXEC_MEMIO_READ;set_params = 0; end
|
|
|
|
3'b100: begin next_state=`EXEC_MEMIO_READ_SETADDR;set_params = 0; end
|
2023-03-08 07:26:28 +00:00
|
|
|
default: begin end /*impossible*/
|
2023-02-22 01:28:23 +00:00
|
|
|
endcase
|
2023-05-03 23:48:55 +00:00
|
|
|
if(ucode_data[36:36]==0) /*Set reg write address*/
|
|
|
|
reg_write_addr = ucode_data[12:9 ];
|
2023-05-11 11:11:17 +00:00
|
|
|
in_alu_sel1 = ucode_data[14:13];
|
|
|
|
in_alu_sel2 = ucode_data[16:15];
|
2023-05-03 23:48:55 +00:00
|
|
|
OUT_MOD = ucode_data[19:17];
|
2023-02-26 02:46:43 +00:00
|
|
|
/*1:1 map essentially but I want to keep the spec for these bits separate
|
2023-02-22 01:28:23 +00:00
|
|
|
* from the alu op select bits*/
|
2023-03-08 07:26:28 +00:00
|
|
|
case(ucode_data[22:20])
|
2023-05-11 15:28:10 +00:00
|
|
|
3'b000: ALU_OP=`ALU_OP_ADD;
|
|
|
|
3'b001: ALU_OP=`ALU_OP_SUB;
|
|
|
|
3'b010: ALU_OP=`ALU_OP_AND;
|
|
|
|
3'b011: ALU_OP=`ALU_OP_OR;
|
|
|
|
3'b100: ALU_OP=`ALU_OP_XOR;
|
|
|
|
3'b101: ALU_OP=`ALU_OP_ADD_SIGNED_B;
|
|
|
|
3'b110: ALU_OP=`ALU_OP_SUB_REVERSE;
|
|
|
|
3'b111: ALU_OP=`ALU_OP_SHIFT_LEFT;
|
2023-03-04 06:22:28 +00:00
|
|
|
default: begin end
|
2023-02-22 01:28:23 +00:00
|
|
|
endcase
|
2023-05-03 23:48:55 +00:00
|
|
|
if(ucode_data[34:34]==0) /* Set reg read port 1 address */
|
2023-03-08 07:26:28 +00:00
|
|
|
reg_read_port1_addr=ucode_data[26:23];
|
|
|
|
IN_MOD=ucode_data[29:27];
|
2023-05-03 23:48:55 +00:00
|
|
|
if(ucode_data[35:35]==0) /* Set reg read port 1 address */
|
2023-03-08 07:26:28 +00:00
|
|
|
reg_read_port2_addr=ucode_data[33:30];
|
2023-05-03 23:48:55 +00:00
|
|
|
if(ucode_data[37:37]==1) /* Overwrite Wbit */
|
|
|
|
Wbit=ucode_data[38:38];
|
2023-03-08 07:26:28 +00:00
|
|
|
memio_address_select=ucode_data[39:39];
|
2023-03-09 06:02:41 +00:00
|
|
|
MEM_OR_IO=0;
|
2023-05-07 12:34:15 +00:00
|
|
|
HALT <= 0;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-05-29 01:28:56 +00:00
|
|
|
|
|
|
|
if(PARAM_ACTION==`LOAD_8)begin
|
|
|
|
if(opcode_size==0)begin
|
|
|
|
if({Sbit,Wbit}==2'b11)begin
|
|
|
|
/*signed "16bit" read*/
|
|
|
|
PARAM1 = {{8{INSTRUCTION[23:23]}},INSTRUCTION[23:16]};
|
|
|
|
end else begin
|
|
|
|
//PARAM1[7:0] = INSTRUCTION[23:16];
|
|
|
|
PARAM1 = {8'b0,INSTRUCTION[23:16]};
|
|
|
|
end
|
|
|
|
end else begin
|
|
|
|
if({Sbit,Wbit}==2'b11)begin
|
|
|
|
/*signed "16bit" read*/
|
|
|
|
PARAM1 = {{8{INSTRUCTION[15:15]}},INSTRUCTION[15:8]};
|
|
|
|
end else begin
|
|
|
|
//PARAM1[7:0] = INSTRUCTION[15:8];
|
|
|
|
PARAM1 = {8'b0,INSTRUCTION[15:8]};
|
|
|
|
end
|
|
|
|
end
|
|
|
|
case(IN_MOD)
|
|
|
|
3'b000,3'b001,3'b010: next_state = `EXEC_MEMIO_READ;
|
|
|
|
default: next_state = `EXEC_WRITE_ENTRY;
|
|
|
|
endcase
|
|
|
|
end else if (PARAM_ACTION == `LOAD_16) begin
|
|
|
|
if(opcode_size==0)begin
|
|
|
|
PARAM1[7:0] = INSTRUCTION[23:16];
|
|
|
|
PARAM1[15:8] = INSTRUCTION[15:8];
|
|
|
|
end else begin
|
|
|
|
PARAM1[15:8] = INSTRUCTION[7:0];
|
|
|
|
PARAM1[7:0] = INSTRUCTION[15:8];
|
|
|
|
end
|
|
|
|
case(IN_MOD)
|
|
|
|
3'b000,3'b001,3'b010: next_state = `EXEC_MEMIO_READ;
|
|
|
|
default: next_state = `EXEC_WRITE_ENTRY;
|
|
|
|
endcase
|
|
|
|
end
|
2023-02-17 18:08:09 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
`undef invalid_instruction
|
2023-02-17 18:08:09 +00:00
|
|
|
|
|
|
|
endmodule
|
2023-06-01 01:13:55 +00:00
|
|
|
/* verilator lint_on BLKSEQ */
|
2023-05-11 15:28:10 +00:00
|
|
|
|
|
|
|
|
2023-05-29 01:28:56 +00:00
|
|
|
/* IN: {INSTRUCTION[31:24],INSTRUCTION[21:19]} */
|
2023-05-11 15:28:10 +00:00
|
|
|
/* OUT: number in bytes */
|
|
|
|
module InstrSize ( input [10:0] IN, output reg [2:0] VERDICT );
|
|
|
|
always @( IN ) begin
|
|
|
|
casez(IN)
|
2023-11-01 05:00:09 +00:00
|
|
|
11'b0000_010?_??? : VERDICT = 3'd2+{2'b0,IN[3:3]}; /* ADD - Add Immediate word/byte to accumulator */
|
|
|
|
11'b1000_00??_101 : VERDICT = 3'd3+{2'b0,(IN[4:3]==2'b01)}; /* SUB - Subtract immediate word/byte from register/memory */
|
|
|
|
11'b1000_00??_000 : VERDICT = 3'd3+{2'b0,(IN[4:3]==2'b01)}; /* ADD - Add Immediate word/byte to register/memory */
|
|
|
|
11'b1000_00??_111 : VERDICT = 3'd3+{2'b0,(IN[4:3]==2'b01)}; /* CMP - compare Immediate with register / memory */
|
|
|
|
11'b1011_????_??? : VERDICT = 3'd2+{2'b0,IN[6:6]}; /* MOV - Move Immediate byte to register */
|
|
|
|
11'b1000_10??_??? : VERDICT = 3'd2; /* MOV - Reg/Mem to/from register */
|
|
|
|
11'b0100_????_??? : VERDICT = 3'd1; /* DEC - Decrement Register | INC - Increment Register */
|
|
|
|
11'b1111_111?_00? : VERDICT = 3'd2; /* INC - Register/Memory | DEC - Register/Memory */
|
|
|
|
11'b1111_0100_??? : VERDICT = 3'd1; /* HLT - Halt */
|
|
|
|
11'b0011_110?_??? : VERDICT = 3'd2+{2'b0,IN[3:3]}; /* CMP - Compare Immediate with accumulator */
|
|
|
|
11'b0111_????_??? : VERDICT = 3'd2; /* Conditional relative jumps ( JE/JZ, JS/JNS ... ) */
|
|
|
|
11'b1110_1011_??? : VERDICT = 3'd2; /* JMP - Unconditional jump direct within segment (short) */
|
|
|
|
11'b1110_1000_??? : VERDICT = 3'd3; /* CALL - Direct call within segment */
|
|
|
|
11'b1100_0011_??? : VERDICT = 3'd1; /* RET - Return from call within segment */
|
|
|
|
11'b1010_101?_??? : VERDICT = 3'd1; /* STOS - Write byte/word to [DI] and increment accordingly */
|
|
|
|
11'b0101_0???_??? : VERDICT = 3'd1; /* PUSH - SP-=2; [SP]=REG */
|
|
|
|
11'b1111_011?_000 : VERDICT = 3'd3+{2'b0,IN[3:3]}; /* TEST - Bitwise AND affecting only flags */
|
|
|
|
11'b0101_1???_??? : VERDICT = 3'd1; /* POP - REG=[SP]; SP+=2 */
|
|
|
|
11'b1111_1111_100 : VERDICT = 3'd2; /* JMP - Unconditional indirect within segment jump */
|
|
|
|
11'b1100_011?_000 : VERDICT = 3'd3+{2'b0,IN[3:3]}; /* MOV - Move immediate to register/memory */
|
|
|
|
11'b1100_1101_??? : VERDICT = 3'd2; /* INT - execute interrupt handler */
|
|
|
|
11'b1110_011?_??? : VERDICT = 3'd2; /* OUT - write AL or AX to a defined output port */
|
|
|
|
11'b1100_1111_??? : VERDICT = 3'd1; /* IRET - Return from interrupt */
|
|
|
|
11'b1000_000?_100 : VERDICT = 3'd3+{2'b0,IN[3:3]}; /* AND - Bitwise AND immediate and register/mem */
|
|
|
|
11'b1000_000?_001 : VERDICT = 3'd3+{2'b0,IN[3:3]}; /* OR - Bitwise OR immediate and register/mem */
|
|
|
|
11'b1010_100?_??? : VERDICT = 3'd2+{2'b0,IN[3:3]}; /* TEST - Bitwise AND affecting only flags */
|
|
|
|
11'b0000_00??_??? : VERDICT = 3'd2; /* ADD - Reg/memory with register to either */
|
|
|
|
11'b0010_10??_??? : VERDICT = 3'd2; /* SUB - Reg/memory with register to either */
|
|
|
|
11'b0011_10??_??? : VERDICT = 3'd2; /* CMP - Compare Register/memory and register */
|
|
|
|
default: VERDICT = 3'd7;
|
2023-05-11 15:28:10 +00:00
|
|
|
endcase
|
|
|
|
end
|
|
|
|
endmodule
|
|
|
|
|
2023-05-16 17:07:28 +00:00
|
|
|
`ifdef INCLUDE_EARLY_CALC_CIRUIT
|
2023-05-11 15:28:10 +00:00
|
|
|
module Is1 ( input [7:0] IN, output reg VERDICT );
|
|
|
|
always @( IN ) begin
|
|
|
|
casez(IN)
|
2023-11-01 05:00:09 +00:00
|
|
|
8'b0100_???? : VERDICT = 1; /* DEC - Decrement Register | INC - Increment Register */
|
|
|
|
8'b1111_0100 : VERDICT = 1; /* HLT - Halt */
|
|
|
|
8'b1100_0011 : VERDICT = 1; /* RET - Return from call within segment */
|
|
|
|
8'b1010_101? : VERDICT = 1; /* STOS - Write byte/word to [DI] and increment accordingly */
|
|
|
|
8'b0101_0??? : VERDICT = 1; /* PUSH - SP-=2; [SP]=REG */
|
|
|
|
8'b0101_1??? : VERDICT = 1; /* POP - REG=[SP]; SP+=2 */
|
|
|
|
8'b1100_1111 : VERDICT = 1; /* IRET - Return from interrupt */
|
|
|
|
default:begin VERDICT = 0; end
|
2023-05-11 15:28:10 +00:00
|
|
|
endcase
|
|
|
|
end
|
|
|
|
endmodule
|
|
|
|
`endif
|
2023-05-19 16:59:20 +00:00
|
|
|
|
|
|
|
`undef unimpl_addressing_mode
|