2023-02-13 16:49:17 +00:00
|
|
|
/* processor.v - implementation of most functions of the 9086 processor
|
|
|
|
|
|
|
|
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-02-09 14:46:21 +00:00
|
|
|
`include "proc_state_def.v"
|
2023-02-11 14:43:53 +00:00
|
|
|
`include "alu_header.v"
|
2023-02-13 15:24:21 +00:00
|
|
|
`include "config.v"
|
2023-02-22 01:28:23 +00:00
|
|
|
`include "ucode_header.v"
|
2023-02-08 09:18:00 +00:00
|
|
|
|
2023-03-03 06:29:06 +00:00
|
|
|
//HALT: active high
|
|
|
|
//ERROR: active high
|
|
|
|
//IOMEM: 1=IO 0=MEM
|
|
|
|
//write: active low
|
|
|
|
//read: active low
|
|
|
|
//reset: active low
|
|
|
|
|
|
|
|
module processor ( input clock, input reset, output reg [19:0] external_address_bus, inout [15:0] external_data_bus,output reg read, output reg write,output reg BHE,output reg IOMEM, output reg HALT,output reg ERROR);
|
2023-02-09 20:16:50 +00:00
|
|
|
|
2023-02-14 13:13:40 +00:00
|
|
|
/*if we don't read, output the register to have the bus stable by the write falling edge*/
|
|
|
|
reg [15:0] data_bus_output_register;
|
2023-02-19 00:20:53 +00:00
|
|
|
assign external_data_bus=read?data_bus_output_register:16'hz;
|
2023-02-14 13:13:40 +00:00
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
/*** Global Definitions ***/
|
2023-02-19 00:20:53 +00:00
|
|
|
|
2023-02-14 13:13:40 +00:00
|
|
|
reg [`PROC_STATE_BITS-1:0] state;
|
2023-02-08 12:07:42 +00:00
|
|
|
|
2023-02-22 01:28:23 +00:00
|
|
|
/*############ Decoder ########################################################## */
|
2023-03-03 20:43:25 +00:00
|
|
|
wire Wbit, Sbit, opcode_size;
|
2023-02-17 18:08:09 +00:00
|
|
|
wire [`PROC_STATE_BITS-1:0] next_state;
|
|
|
|
wire [2:0]RM;
|
2023-02-22 01:28:23 +00:00
|
|
|
wire [15:0]DE_PARAM1;// Input param1 form decoder to alu
|
2023-02-17 18:08:09 +00:00
|
|
|
wire [15:0]DE_PARAM2;
|
|
|
|
wire DE_ERROR,DE_HALT;
|
2023-02-22 01:28:23 +00:00
|
|
|
wire [3:0]DE_reg_read_port1_addr,DE_reg_write_addr,DE_reg_read_port2_addr;
|
|
|
|
wire [11:0]DE_REGISTER_CONTROL;
|
2023-03-03 20:43:25 +00:00
|
|
|
wire [2:0]INSTRUCTION_INFO;
|
2023-02-22 01:28:23 +00:00
|
|
|
wire [1:0]DECODER_SIGNALS;
|
|
|
|
wire [`UCODE_ADDR_BITS-1:0] ucode_seq_addr_entry;
|
|
|
|
|
2023-03-03 06:54:33 +00:00
|
|
|
reg SIMPLE_MICRO; /* output simple decodings (=0) or microcode data (=1) */
|
2023-03-03 06:29:06 +00:00
|
|
|
wire [2:0] DE_instruction_size;
|
|
|
|
reg instruction_size_init;
|
|
|
|
wire [2:0] instruction_size;
|
|
|
|
assign instruction_size = instruction_size_init ? 3'b010 : DE_instruction_size;
|
2023-02-22 01:28:23 +00:00
|
|
|
|
2023-02-17 18:08:09 +00:00
|
|
|
decoder decoder(
|
2023-03-03 06:29:06 +00:00
|
|
|
.CIR(CIR),
|
|
|
|
.FLAGS(FLAGS),
|
|
|
|
.INSTRUCTION_INFO(INSTRUCTION_INFO),
|
|
|
|
.DECODER_SIGNALS(DECODER_SIGNALS),
|
|
|
|
.next_state(next_state),
|
|
|
|
.IN_MOD(IN_MOD),
|
|
|
|
.RM(RM),
|
|
|
|
.PARAM1(DE_PARAM1),
|
|
|
|
.PARAM2(DE_PARAM2),
|
|
|
|
.in_alu1_sel1(in_alu1_sel1),
|
|
|
|
.in_alu1_sel2(in_alu1_sel2),
|
|
|
|
.OUT_MOD(OUT_MOD),
|
|
|
|
.REGISTER_FILE_CONTROL(DE_REGISTER_CONTROL),
|
|
|
|
.ALU_1OP(ALU_1OP),
|
|
|
|
.seq_addr_entry(ucode_seq_addr_entry),
|
|
|
|
.SIMPLE_MICRO(SIMPLE_MICRO),
|
|
|
|
.seq_addr_input(ucode_seq_addr),
|
|
|
|
.instruction_size(DE_instruction_size)
|
2023-02-17 18:08:09 +00:00
|
|
|
);
|
|
|
|
|
2023-03-03 20:43:25 +00:00
|
|
|
assign Wbit=INSTRUCTION_INFO[2:2];
|
|
|
|
assign Sbit=INSTRUCTION_INFO[1:1];
|
2023-03-03 06:29:06 +00:00
|
|
|
assign opcode_size=INSTRUCTION_INFO[0:0];
|
2023-02-22 01:28:23 +00:00
|
|
|
|
|
|
|
assign DE_reg_write_addr=DE_REGISTER_CONTROL[11:8];
|
|
|
|
assign DE_reg_read_port1_addr=DE_REGISTER_CONTROL[7:4];
|
|
|
|
assign DE_reg_read_port2_addr=DE_REGISTER_CONTROL[3:0];
|
|
|
|
|
|
|
|
assign DE_HALT=DECODER_SIGNALS[0:0];
|
|
|
|
assign DE_ERROR=DECODER_SIGNALS[1:1];
|
|
|
|
|
|
|
|
reg [`UCODE_ADDR_BITS-1:0] ucode_seq_addr;
|
|
|
|
|
|
|
|
/*############ REGISTERS ########################################################## */
|
|
|
|
|
2023-02-09 14:46:21 +00:00
|
|
|
reg [15:0] CIR;
|
|
|
|
reg [15:0] PARAM1;
|
|
|
|
reg [15:0] PARAM2;
|
2023-02-17 18:08:09 +00:00
|
|
|
|
2023-03-04 06:22:28 +00:00
|
|
|
// verilator lint_off UNDRIVEN
|
|
|
|
reg [15:0] FLAGS;
|
|
|
|
// verilator lint_on UNDRIVEN
|
2023-02-12 01:05:39 +00:00
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
//Architectural Register file
|
2023-02-11 13:41:12 +00:00
|
|
|
reg [3:0] reg_write_addr;
|
2023-02-15 01:28:02 +00:00
|
|
|
wire [15:0] reg_write_data;
|
2023-02-11 13:41:12 +00:00
|
|
|
reg reg_write_we;
|
2023-02-15 01:28:02 +00:00
|
|
|
reg [3:0] reg_read_port1_addr;
|
|
|
|
reg [15:0] reg_read_port1_data;
|
2023-02-22 01:28:23 +00:00
|
|
|
reg [3:0] reg_read_port2_addr;
|
|
|
|
reg [15:0] reg_read_port2_data;
|
2023-02-15 01:28:02 +00:00
|
|
|
reg [1:0] reg_write_in_sel;
|
|
|
|
mux4 #(.WIDTH(16)) REG_FILE_WRITE_IN_MUX(
|
|
|
|
ALU_1O,
|
|
|
|
16'hz,
|
|
|
|
16'hz,
|
|
|
|
16'hz,
|
|
|
|
reg_write_in_sel,
|
|
|
|
reg_write_data);
|
2023-03-03 06:29:06 +00:00
|
|
|
register_file register_file(
|
|
|
|
.write_port1_addr(reg_write_addr),
|
|
|
|
.write_port1_data(reg_write_data),
|
|
|
|
.write_port1_we(reg_write_we),
|
|
|
|
.read_port1_addr(reg_read_port1_addr),
|
|
|
|
.read_port1_data(reg_read_port1_data),
|
|
|
|
.read_port2_addr(reg_read_port2_addr),
|
|
|
|
.read_port2_data(reg_read_port2_data)
|
|
|
|
);
|
|
|
|
|
|
|
|
reg [15:0] ProgCount;
|
2023-03-04 06:22:28 +00:00
|
|
|
|
|
|
|
// verilator lint_off UNUSEDSIGNAL
|
|
|
|
wire [15:0] ProgCount_next_opcode;
|
|
|
|
wire [15:0] ProgCount_arg;
|
|
|
|
assign ProgCount_next_opcode=ProgCount+{13'b0,instruction_size};
|
|
|
|
assign ProgCount_arg=ProgCount+{15'b0,opcode_size}+16'd1;
|
|
|
|
// verilator lint_on UNUSEDSIGNAL
|
2023-02-15 01:28:02 +00:00
|
|
|
|
2023-02-22 01:28:23 +00:00
|
|
|
/*############ ALU / Execution units ########################################################## */
|
|
|
|
// ALU 1
|
2023-02-15 01:28:02 +00:00
|
|
|
reg [1:0] in_alu1_sel1;
|
|
|
|
reg [1:0] in_alu1_sel2;
|
2023-02-23 14:48:48 +00:00
|
|
|
/* OUT_MOD : { EXTRA_FUNCTIONS_BIT[0:0], MOD_OR_EXTRA_FUNCTION[1:0] } */
|
|
|
|
reg [2:0] IN_MOD;
|
|
|
|
reg [2:0] OUT_MOD;
|
2023-02-10 01:45:27 +00:00
|
|
|
|
|
|
|
mux4 #(.WIDTH(16)) MUX16_1A(
|
2023-02-24 05:01:55 +00:00
|
|
|
/*0*/ PARAM1,
|
|
|
|
/*1*/ reg_read_port1_data,
|
2023-03-03 06:29:06 +00:00
|
|
|
/*2*/ ProgCount[15:0],
|
2023-03-04 06:22:28 +00:00
|
|
|
/*3*/ 16'd0, /*0 Constant*/
|
2023-02-15 01:28:02 +00:00
|
|
|
in_alu1_sel1,
|
2023-02-11 14:43:53 +00:00
|
|
|
ALU_1A);
|
2023-02-10 01:45:27 +00:00
|
|
|
|
|
|
|
mux4 #(.WIDTH(16)) MUX16_1B(
|
2023-02-24 05:01:55 +00:00
|
|
|
/*0*/ PARAM2,
|
|
|
|
/*1*/ reg_read_port2_data,
|
2023-03-03 06:29:06 +00:00
|
|
|
/*2*/ ProgCount[15:0],
|
2023-03-04 06:22:28 +00:00
|
|
|
/*3*/ 16'd0, /*0 Constant*/
|
2023-02-15 01:28:02 +00:00
|
|
|
in_alu1_sel2,
|
2023-02-11 14:43:53 +00:00
|
|
|
ALU_1B);
|
2023-02-10 01:45:27 +00:00
|
|
|
|
2023-02-11 14:43:53 +00:00
|
|
|
wire [15:0] ALU_1A;
|
|
|
|
wire [15:0] ALU_1B;
|
|
|
|
wire [15:0] ALU_1O;
|
|
|
|
reg [`ALU_OP_BITS-1:0]ALU_1OP;
|
2023-02-15 01:28:02 +00:00
|
|
|
wire [7:0] ALU_1FLAGS;
|
2023-03-03 06:29:06 +00:00
|
|
|
ALU ALU1(
|
|
|
|
.A(ALU_1A),
|
|
|
|
.B(ALU_1B),
|
|
|
|
.OUT(ALU_1O),
|
|
|
|
.op(ALU_1OP),
|
|
|
|
.FLAGS(ALU_1FLAGS),
|
|
|
|
.Wbit(Wbit)
|
|
|
|
);
|
2023-02-10 01:45:27 +00:00
|
|
|
|
2023-02-22 01:28:23 +00:00
|
|
|
/*############ Processor state machine ########################################################## */
|
|
|
|
|
|
|
|
/*** RESET LOGIC ***/
|
2023-03-04 06:22:28 +00:00
|
|
|
/* verilator lint_off MULTIDRIVEN */
|
2023-02-22 01:28:23 +00:00
|
|
|
always @(negedge reset) begin
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_HALT_STATE; //TODO: race condition ??
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
always @(posedge reset) begin
|
|
|
|
state <= `PROC_RESET;
|
|
|
|
end
|
|
|
|
/* verilator lint_on MULTIDRIVEN */
|
2023-02-22 01:28:23 +00:00
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
/*** Processor stages ***/
|
2023-03-04 06:22:28 +00:00
|
|
|
`define invalid_instruction state <= `PROC_IF_STATE_ENTRY;ERROR <= 1;
|
2023-02-10 14:39:34 +00:00
|
|
|
|
2023-02-09 14:46:21 +00:00
|
|
|
always @(posedge clock) begin
|
|
|
|
case(state)
|
2023-03-04 06:22:28 +00:00
|
|
|
`PROC_RESET:begin
|
|
|
|
ucode_seq_addr <= `UCODE_NO_INSTRUCTION;
|
|
|
|
ProgCount <= 0;//TODO: Reset Vector
|
|
|
|
HALT <= 0;
|
|
|
|
ERROR <= 0;
|
|
|
|
IOMEM <= 0;
|
|
|
|
SIMPLE_MICRO <= 0;
|
|
|
|
reg_write_we <= 1;
|
|
|
|
instruction_size_init <= 1;
|
|
|
|
state <= `PROC_IF_STATE_ENTRY;
|
|
|
|
end
|
2023-02-10 18:20:28 +00:00
|
|
|
`PROC_HALT_STATE:begin
|
|
|
|
end
|
2023-02-09 14:46:21 +00:00
|
|
|
`PROC_IF_STATE_ENTRY:begin
|
2023-02-13 15:24:21 +00:00
|
|
|
`ifdef DEBUG_PC_ADDRESS
|
|
|
|
/* Weird (possible bug) where even though the
|
|
|
|
* testbench stop the clock after ERROR gets
|
|
|
|
* raised the logic for the rising edge still
|
|
|
|
* gets triggered printing this debug message. */
|
2023-03-03 06:29:06 +00:00
|
|
|
if(ERROR!=1)begin
|
|
|
|
if(instruction_size==1)
|
|
|
|
$display("Fetched instruction at %0x",ProgCount - 1);
|
|
|
|
else
|
|
|
|
$display("Fetched instruction at %0x",ProgCount - 0);
|
|
|
|
end
|
2023-02-13 15:24:21 +00:00
|
|
|
`endif
|
2023-03-04 06:22:28 +00:00
|
|
|
BHE <= 0;
|
|
|
|
external_address_bus <= {4'b0,ProgCount};
|
|
|
|
read <= 0;
|
|
|
|
write <= 1;
|
|
|
|
reg_write_we <= 1;
|
|
|
|
state <= `PROC_IF_WRITE_CIR;
|
|
|
|
reg_write_in_sel <= 2'b00;
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
|
|
|
`PROC_IF_WRITE_CIR:begin
|
|
|
|
/*I built the entire decode stage with CIR
|
|
|
|
* being big endian so just convert it here*/
|
|
|
|
|
|
|
|
if(instruction_size==1)begin
|
|
|
|
/*Half on CIR half on this address */
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_DE_STATE_ENTRY;
|
2023-03-03 06:29:06 +00:00
|
|
|
if(ProgCount[0:0]==1)begin
|
2023-03-04 06:22:28 +00:00
|
|
|
CIR <= {CIR[7:0],external_data_bus[15:8]};
|
2023-03-03 06:29:06 +00:00
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
CIR <= {CIR[7:0],external_data_bus[7:0]};
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
ProgCount <= ProgCount+1;
|
2023-03-03 06:29:06 +00:00
|
|
|
end else begin
|
|
|
|
if(ProgCount[0:0]==1)begin
|
|
|
|
/* Half on this address half on the next*/
|
2023-03-04 06:22:28 +00:00
|
|
|
ProgCount <= ProgCount+1;
|
2023-03-03 06:29:06 +00:00
|
|
|
CIR[15:8] <= external_data_bus[15:8];
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_IF_STATE_EXTRA_FETCH_SET;
|
2023-03-03 06:29:06 +00:00
|
|
|
end else begin
|
|
|
|
/* Both on this address! */
|
2023-03-04 06:22:28 +00:00
|
|
|
ProgCount <= ProgCount+2;
|
2023-03-03 06:29:06 +00:00
|
|
|
CIR <= {external_data_bus[7:0],external_data_bus[15:8]};
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_DE_STATE_ENTRY;
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-09 14:46:21 +00:00
|
|
|
end
|
2023-02-10 12:02:20 +00:00
|
|
|
`PROC_IF_STATE_EXTRA_FETCH_SET:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
external_address_bus <= {4'b0,ProgCount};
|
|
|
|
BHE <= 0;
|
|
|
|
state <= `PROC_IF_STATE_EXTRA_FETCH;
|
2023-02-10 12:02:20 +00:00
|
|
|
end
|
2023-03-03 06:29:06 +00:00
|
|
|
`PROC_IF_STATE_EXTRA_FETCH:begin
|
|
|
|
CIR[7:0] <= external_data_bus[7:0];
|
2023-03-04 06:22:28 +00:00
|
|
|
ProgCount <= ProgCount+1;
|
|
|
|
state <= `PROC_DE_STATE_ENTRY;
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
2023-02-09 14:46:21 +00:00
|
|
|
`PROC_DE_STATE_ENTRY:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
external_address_bus <= {4'b0,ProgCount};
|
2023-02-22 01:28:23 +00:00
|
|
|
if(SIMPLE_MICRO==0)begin
|
2023-03-03 06:29:06 +00:00
|
|
|
/*This flag is set at reset and jump because
|
|
|
|
* at IF we need to know the size of the
|
2023-03-03 06:54:33 +00:00
|
|
|
* previous instruction (specifically if it was
|
2023-03-03 06:29:06 +00:00
|
|
|
* a single byte and the value would be
|
|
|
|
* incorrect in both cases. So when it gets
|
|
|
|
* set reset it only at the start of the next
|
|
|
|
* 8086 instruction */
|
2023-03-04 06:22:28 +00:00
|
|
|
instruction_size_init <= 0;
|
2023-03-03 06:29:06 +00:00
|
|
|
|
2023-02-22 01:28:23 +00:00
|
|
|
/* We cannot set these directly within
|
2023-02-26 02:46:43 +00:00
|
|
|
* microcode so don't overwrite useful values
|
2023-02-22 01:28:23 +00:00
|
|
|
* each time the next microcode is executed.
|
|
|
|
* Note this still allows to set initial values
|
|
|
|
* at the start of the microcode */
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1 <= DE_PARAM1;
|
|
|
|
PARAM2 <= DE_PARAM2;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
ERROR <= DE_ERROR;
|
|
|
|
HALT <= DE_HALT;
|
|
|
|
reg_read_port1_addr <= DE_reg_read_port1_addr;
|
|
|
|
reg_read_port2_addr <= DE_reg_read_port2_addr;
|
|
|
|
reg_write_addr <= DE_reg_write_addr;
|
2023-02-22 01:28:23 +00:00
|
|
|
if ( (ucode_seq_addr==`UCODE_NO_INSTRUCTION) && (ucode_seq_addr_entry!=`UCODE_NO_INSTRUCTION) )begin
|
|
|
|
/*switch to microcode decoding*/
|
2023-03-04 06:22:28 +00:00
|
|
|
ucode_seq_addr <= ucode_seq_addr_entry;
|
|
|
|
SIMPLE_MICRO <= 1;
|
2023-02-22 01:28:23 +00:00
|
|
|
/*keep state the same and rerun decode this time with all the data from the microcode rom*/
|
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= next_state;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-02-09 14:46:21 +00:00
|
|
|
end
|
2023-02-15 01:28:02 +00:00
|
|
|
`PROC_DE_LOAD_REG_TO_PARAM:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM2<=reg_read_port2_data;
|
2023-03-03 06:29:06 +00:00
|
|
|
case(IN_MOD)
|
2023-03-04 06:22:28 +00:00
|
|
|
3'b000,3'b001,3'b010: state <= `PROC_MEMIO_READ;
|
|
|
|
default: state <= `PROC_EX_STATE_ENTRY;
|
2023-03-03 06:29:06 +00:00
|
|
|
endcase
|
2023-02-15 01:28:02 +00:00
|
|
|
end
|
2023-02-15 03:53:05 +00:00
|
|
|
`PROC_DE_LOAD_8_PARAM:begin
|
2023-02-17 18:08:09 +00:00
|
|
|
if(opcode_size==0)begin
|
2023-02-15 03:53:05 +00:00
|
|
|
if({Sbit,Wbit}==2'b11)begin
|
2023-02-17 18:08:09 +00:00
|
|
|
/*signed "16bit" read*/
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1 <= {{8{CIR[7:7]}},CIR[7:0]};
|
2023-02-15 03:53:05 +00:00
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1[7:0] <= CIR[7:0];
|
2023-02-15 03:53:05 +00:00
|
|
|
end
|
2023-02-24 14:09:10 +00:00
|
|
|
case(IN_MOD)
|
2023-03-04 06:22:28 +00:00
|
|
|
3'b000,3'b001,3'b010: state <= `PROC_MEMIO_READ;
|
|
|
|
default: state <= `PROC_EX_STATE_ENTRY;
|
2023-02-24 14:09:10 +00:00
|
|
|
endcase
|
2023-02-15 03:53:05 +00:00
|
|
|
end else begin
|
2023-03-03 06:29:06 +00:00
|
|
|
if(ProgCount[0:0]==1)begin
|
|
|
|
if({Sbit,Wbit}==2'b11)begin
|
|
|
|
/*signed "16bit" read*/
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1 <= {{8{external_data_bus[15:15]}},external_data_bus[15:8]};
|
2023-03-03 06:29:06 +00:00
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1[7:0] <= external_data_bus[15:8];
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
|
|
|
end else begin
|
2023-02-17 18:08:09 +00:00
|
|
|
if({Sbit,Wbit}==2'b11)begin
|
|
|
|
/*signed "16bit" read*/
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1 <= {{8{external_data_bus[7:7]}},external_data_bus[7:0]};
|
2023-02-17 18:08:09 +00:00
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1[7:0] <= external_data_bus[7:0];
|
2023-02-17 18:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
ProgCount <= ProgCount+1;
|
2023-03-03 06:29:06 +00:00
|
|
|
case(IN_MOD)
|
2023-03-04 06:22:28 +00:00
|
|
|
3'b000,3'b001,3'b010: state <= `PROC_MEMIO_READ;
|
|
|
|
default: state <= `PROC_EX_STATE_ENTRY;
|
2023-03-03 06:29:06 +00:00
|
|
|
endcase
|
2023-02-15 03:53:05 +00:00
|
|
|
end
|
|
|
|
end
|
2023-02-09 14:46:21 +00:00
|
|
|
`PROC_DE_LOAD_16_PARAM:begin
|
2023-02-17 18:08:09 +00:00
|
|
|
if(opcode_size==0)begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1[7:0] <= CIR[7:0];
|
2023-03-03 06:29:06 +00:00
|
|
|
if(ProgCount[0:0]==1)begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1[15:8] <= external_data_bus[15:8];
|
2023-02-17 18:08:09 +00:00
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1[15:8] <= external_data_bus[7:0];
|
2023-02-17 18:08:09 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
ProgCount <= ProgCount+1;
|
2023-02-24 02:18:48 +00:00
|
|
|
case(IN_MOD)
|
2023-03-04 06:22:28 +00:00
|
|
|
3'b000,3'b001,3'b010: state <= `PROC_MEMIO_READ;
|
|
|
|
default: state <= `PROC_EX_STATE_ENTRY;
|
2023-02-24 02:18:48 +00:00
|
|
|
endcase
|
2023-02-10 12:02:20 +00:00
|
|
|
end else begin
|
2023-03-03 06:29:06 +00:00
|
|
|
if(ProgCount[0:0]==1)begin
|
2023-03-04 06:22:28 +00:00
|
|
|
ProgCount <= ProgCount+1;
|
|
|
|
PARAM1[7:0] <= external_data_bus[15:8];
|
|
|
|
state <= `PROC_DE_LOAD_16_EXTRA_FETCH_SET;
|
2023-02-17 18:08:09 +00:00
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM1 <= external_data_bus;
|
|
|
|
ProgCount <= ProgCount+2;
|
2023-02-24 02:18:48 +00:00
|
|
|
case(IN_MOD)
|
2023-03-04 06:22:28 +00:00
|
|
|
3'b000,3'b001,3'b010: state <= `PROC_MEMIO_READ;
|
|
|
|
default: state <= `PROC_EX_STATE_ENTRY;
|
2023-02-24 02:18:48 +00:00
|
|
|
endcase
|
2023-02-17 18:08:09 +00:00
|
|
|
end
|
2023-02-10 12:02:20 +00:00
|
|
|
end
|
|
|
|
end
|
2023-03-03 06:29:06 +00:00
|
|
|
`PROC_DE_LOAD_16_EXTRA_FETCH_SET:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
external_address_bus <= {4'b0,ProgCount};
|
|
|
|
state <= `PROC_DE_LOAD_16_EXTRA_FETCH;
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
2023-02-10 12:02:20 +00:00
|
|
|
`PROC_DE_LOAD_16_EXTRA_FETCH:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
ProgCount <= ProgCount+1;
|
|
|
|
PARAM1[15:8] <= external_data_bus[7:0];
|
2023-02-24 02:18:48 +00:00
|
|
|
case(IN_MOD)
|
2023-03-04 06:22:28 +00:00
|
|
|
3'b000,3'b001,3'b010: state <= `PROC_MEMIO_READ;
|
|
|
|
default: state <= `PROC_EX_STATE_ENTRY;
|
2023-02-24 02:18:48 +00:00
|
|
|
endcase
|
2023-02-09 14:46:21 +00:00
|
|
|
end
|
2023-02-24 02:18:48 +00:00
|
|
|
`PROC_MEMIO_READ:begin
|
2023-02-11 20:27:28 +00:00
|
|
|
/*Decode MOD R/M, read the data and place it to PARAM1*/
|
2023-02-23 14:48:48 +00:00
|
|
|
case (IN_MOD)
|
|
|
|
3'b000,
|
|
|
|
3'b001,
|
|
|
|
3'b010:begin
|
|
|
|
case (RM)
|
|
|
|
3'b000:begin
|
|
|
|
/*[BX]+[SI]*/
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b001:begin
|
|
|
|
/*[BX]+[SI]*/
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b010:begin
|
|
|
|
/*[BP]+[SI]*/
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b011:begin
|
|
|
|
/*[BP]+[DI]*/
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b100:begin
|
|
|
|
/*[SI]*/
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_read_port1_addr <= 4'b1110;
|
|
|
|
state <= `PROC_MEMIO_READ_SETADDR;
|
2023-02-23 14:48:48 +00:00
|
|
|
end
|
|
|
|
3'b101:begin
|
|
|
|
/*[DI]*/
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_read_port1_addr <= 4'b1111;
|
|
|
|
state <= `PROC_MEMIO_READ_SETADDR;
|
2023-02-23 14:48:48 +00:00
|
|
|
end
|
|
|
|
3'b110:begin
|
|
|
|
/*d16 */
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b111:begin
|
|
|
|
/*[BX]*/
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_read_port1_addr <= 4'b1011;
|
|
|
|
state <= `PROC_MEMIO_READ_SETADDR;
|
2023-02-23 14:48:48 +00:00
|
|
|
end
|
|
|
|
endcase
|
|
|
|
if(IN_MOD!=3'b000)begin
|
|
|
|
/*Actually check if 01 and add the 8bits or if 10 add the 16bits ....*/
|
|
|
|
`invalid_instruction;
|
|
|
|
end
|
2023-02-11 20:27:28 +00:00
|
|
|
end
|
2023-02-24 02:18:48 +00:00
|
|
|
3'b110:begin /* SP Indirect read*/
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_read_port1_addr <= 4'b1100;
|
|
|
|
state <= `PROC_MEMIO_READ_SETADDR;
|
2023-02-11 20:27:28 +00:00
|
|
|
end
|
2023-02-23 14:48:48 +00:00
|
|
|
default:begin
|
2023-02-11 20:27:28 +00:00
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
endcase
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
|
|
|
`PROC_MEMIO_READ_SETADDR:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
external_address_bus <= {4'b0,reg_read_port1_data[15:0]};
|
|
|
|
state <= reg_read_port1_data[0:0]?`PROC_MEMIO_GET_UNALIGNED_DATA:`PROC_MEMIO_GET_ALIGNED_DATA;
|
2023-02-13 10:36:37 +00:00
|
|
|
end
|
|
|
|
`PROC_MEMIO_GET_ALIGNED_DATA:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM2 <= (Wbit==1)? external_data_bus : {8'b0,external_data_bus[7:0]} ;
|
|
|
|
state <= `PROC_EX_STATE_ENTRY;
|
2023-02-13 10:36:37 +00:00
|
|
|
end
|
|
|
|
`PROC_MEMIO_GET_UNALIGNED_DATA:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM2 <= {8'b0,external_data_bus[15:8]};
|
2023-02-13 10:36:37 +00:00
|
|
|
if(Wbit==1) begin
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_MEMIO_GET_SECOND_BYTE;
|
2023-02-13 10:36:37 +00:00
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_EX_STATE_ENTRY;
|
2023-02-13 10:36:37 +00:00
|
|
|
end
|
|
|
|
end
|
2023-03-03 06:29:06 +00:00
|
|
|
`PROC_MEMIO_GET_SECOND_BYTE:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
external_address_bus <= external_address_bus+1;
|
|
|
|
state <= `PROC_MEMIO_GET_SECOND_BYTE1;
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
|
|
|
`PROC_MEMIO_GET_SECOND_BYTE1:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
PARAM2[15:8] <= external_data_bus[7:0];
|
|
|
|
state <= `PROC_EX_STATE_ENTRY;
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
2023-02-13 10:36:37 +00:00
|
|
|
`PROC_EX_STATE_ENTRY:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
external_address_bus <= {4'b0,ProgCount};
|
|
|
|
FLAGS[7:0] <= ALU_1FLAGS[7:0];
|
2023-02-26 02:46:43 +00:00
|
|
|
case(OUT_MOD)
|
|
|
|
3'b000,
|
|
|
|
3'b001,
|
|
|
|
3'b010 : begin
|
|
|
|
case (RM) /* Duplicate code with write... */
|
|
|
|
3'b000:begin
|
|
|
|
/*[BX]+[SI]*/
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b001:begin
|
|
|
|
/*[BX]+[SI]*/
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b010:begin
|
|
|
|
/*[BP]+[SI]*/
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b011:begin
|
|
|
|
/*[BP]+[DI]*/
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b100:begin
|
|
|
|
/*[SI]*/
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_read_port1_addr <= 4'b1110;
|
|
|
|
state <= `PROC_MEMIO_WRITE;
|
2023-02-26 02:46:43 +00:00
|
|
|
end
|
|
|
|
3'b101:begin
|
|
|
|
/*[DI]*/
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_read_port1_addr <= 4'b1111;
|
|
|
|
state <= `PROC_MEMIO_WRITE;
|
2023-02-26 02:46:43 +00:00
|
|
|
end
|
|
|
|
3'b110:begin
|
|
|
|
/*d16 */
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
3'b111:begin
|
|
|
|
/*[BX]*/
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_read_port1_addr <= 4'b1011;
|
|
|
|
state <= `PROC_MEMIO_WRITE;
|
2023-02-26 02:46:43 +00:00
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
3'b011:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_write_we <= 0;
|
2023-02-26 02:46:43 +00:00
|
|
|
if (ucode_seq_addr==`UCODE_NO_INSTRUCTION)
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_IF_STATE_ENTRY;
|
2023-02-26 02:46:43 +00:00
|
|
|
else
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_NEXT_MICROCODE;
|
2023-02-26 02:46:43 +00:00
|
|
|
end
|
|
|
|
3'b100:begin /*No output*/
|
|
|
|
if (ucode_seq_addr==`UCODE_NO_INSTRUCTION)
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_IF_STATE_ENTRY;
|
2023-02-26 02:46:43 +00:00
|
|
|
else
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_NEXT_MICROCODE;
|
2023-02-26 02:46:43 +00:00
|
|
|
end
|
|
|
|
3'b101:begin /* Program Counter*/
|
2023-03-04 06:22:28 +00:00
|
|
|
ProgCount <= ALU_1O[15:0];
|
|
|
|
instruction_size_init <= 1;
|
2023-02-26 02:46:43 +00:00
|
|
|
if (ucode_seq_addr==`UCODE_NO_INSTRUCTION)
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_IF_STATE_ENTRY;
|
2023-02-26 02:46:43 +00:00
|
|
|
else
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_NEXT_MICROCODE;
|
2023-02-26 02:46:43 +00:00
|
|
|
end
|
|
|
|
3'b110:begin /* SP Indirect write*/
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_read_port1_addr <= 4'b1100;
|
|
|
|
state <= `PROC_MEMIO_WRITE;
|
2023-02-26 02:46:43 +00:00
|
|
|
end
|
|
|
|
default:begin
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
endcase
|
2023-02-13 10:36:37 +00:00
|
|
|
end
|
2023-02-14 13:13:40 +00:00
|
|
|
`PROC_MEMIO_WRITE:begin
|
2023-02-15 01:28:02 +00:00
|
|
|
/* ADDRESS: reg_read_port1_data DATA:ALU1_O */
|
2023-02-15 03:53:05 +00:00
|
|
|
`ifdef DEBUG_MEMORY_WRITES
|
|
|
|
$display("Writing at %04x , %04x",reg_read_port1_data,ALU_1O);
|
|
|
|
`endif
|
2023-03-04 06:22:28 +00:00
|
|
|
external_address_bus <= {4'b0,reg_read_port1_data[15:0]};
|
|
|
|
state <= (Wbit==0) ? `PROC_MEMIO_PUT_BYTE : (reg_read_port1_data[0:0]?`PROC_MEMIO_PUT_UNALIGNED_16BIT_DATA:`PROC_MEMIO_PUT_ALIGNED_16BIT_DATA) ;
|
2023-02-14 13:13:40 +00:00
|
|
|
end
|
2023-03-03 06:29:06 +00:00
|
|
|
`PROC_MEMIO_PUT_UNALIGNED_16BIT_DATA:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
read <= 1;
|
|
|
|
BHE <= 0;
|
|
|
|
data_bus_output_register <= {ALU_1O[7:0],ALU_1O[15:8]};
|
|
|
|
state <= `PROC_MEMIO_PUT_UNALIGNED_PREP_NEXT;
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
|
|
|
`PROC_MEMIO_PUT_UNALIGNED_PREP_NEXT:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
write <= 0;
|
|
|
|
state <= `PROC_MEMIO_PUT_UNALIGNED_PREP_NEXT2;
|
2023-02-15 01:28:02 +00:00
|
|
|
end
|
|
|
|
`PROC_MEMIO_PUT_UNALIGNED_PREP_NEXT2:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
write <= 1;
|
|
|
|
external_address_bus <= external_address_bus+1;
|
|
|
|
BHE <= 1;
|
|
|
|
state <= `PROC_MEMIO_WRITE_EXIT;
|
2023-02-15 01:28:02 +00:00
|
|
|
end
|
2023-03-03 06:29:06 +00:00
|
|
|
`PROC_MEMIO_PUT_ALIGNED_16BIT_DATA:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
read <= 1;
|
|
|
|
data_bus_output_register <= {ALU_1O[15:8],ALU_1O[7:0]};
|
|
|
|
state <= `PROC_MEMIO_WRITE_EXIT;
|
2023-02-15 01:28:02 +00:00
|
|
|
end
|
2023-03-03 06:29:06 +00:00
|
|
|
`PROC_MEMIO_PUT_BYTE:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
read <= 1;
|
|
|
|
state <= `PROC_MEMIO_WRITE_EXIT;
|
2023-03-03 06:29:06 +00:00
|
|
|
if(reg_read_port1_data[0:0]==0) begin
|
2023-03-04 06:22:28 +00:00
|
|
|
BHE <= 1;
|
|
|
|
data_bus_output_register <= {8'b0,ALU_1O[7:0]};
|
2023-03-03 06:29:06 +00:00
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
data_bus_output_register <= {ALU_1O[7:0],8'b0};
|
2023-03-03 06:29:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
`PROC_MEMIO_WRITE_EXIT:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
write <= 0;
|
2023-03-03 06:29:06 +00:00
|
|
|
if (ucode_seq_addr==`UCODE_NO_INSTRUCTION)
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_IF_STATE_ENTRY;
|
2023-03-03 06:29:06 +00:00
|
|
|
else
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_NEXT_MICROCODE;
|
2023-02-15 03:53:05 +00:00
|
|
|
end
|
2023-02-22 01:28:23 +00:00
|
|
|
`PROC_NEXT_MICROCODE:begin
|
2023-03-04 06:22:28 +00:00
|
|
|
read <= 0;
|
|
|
|
write <= 1; // maybe we are coming from MEMIO_WRITE
|
|
|
|
BHE <= 0;
|
|
|
|
ucode_seq_addr <= ucode_seq_addr_entry; /*Reused for next address*/
|
|
|
|
if( ucode_seq_addr_entry == `UCODE_NO_INSTRUCTION )begin
|
2023-02-22 01:28:23 +00:00
|
|
|
/*Finished microcode*/
|
2023-03-04 06:22:28 +00:00
|
|
|
SIMPLE_MICRO <= 0;
|
|
|
|
state <= `PROC_IF_STATE_ENTRY;
|
2023-02-22 01:28:23 +00:00
|
|
|
end else begin
|
2023-03-04 06:22:28 +00:00
|
|
|
state <= `PROC_DE_STATE_ENTRY;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
reg_write_we <= 1;
|
2023-02-22 01:28:23 +00:00
|
|
|
end
|
2023-02-19 00:20:53 +00:00
|
|
|
default:begin
|
|
|
|
end
|
2023-02-13 10:36:37 +00:00
|
|
|
endcase
|
2023-02-08 12:07:42 +00:00
|
|
|
end
|
2023-03-04 06:22:28 +00:00
|
|
|
`undef invalid_instruction
|
2023-02-08 09:18:00 +00:00
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
|
2023-02-08 09:18:00 +00:00
|
|
|
endmodule
|