312 lines
10 KiB
Verilog
312 lines
10 KiB
Verilog
/* 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/>. */
|
|
|
|
`include "exec_state_def.v"
|
|
`include "alu_header.v"
|
|
`include "config.v"
|
|
`include "ucode_header.v"
|
|
`include "error_header.v"
|
|
|
|
`define PROC_STATE_BITS 3
|
|
`define PROC_RESET 3'b000
|
|
`define PROC_DE_STATE_ENTRY 3'b001
|
|
`define PROC_HALT 3'b011
|
|
|
|
//HALT: active high
|
|
//IOMEM: 1=IO 0=MEM
|
|
//BHE: active low
|
|
//write: active low
|
|
//read: active low
|
|
//reset: active low
|
|
|
|
module processor (
|
|
/* MISC */ input clock, input reset, output wire HALT,output [`ERROR_BITS-1:0] ERROR
|
|
/* MEMORY / IO */ ,output [19:0] external_address_bus, inout [15:0] external_data_bus,output read, output write,output BHE,output IOMEM
|
|
`ifdef CALCULATE_IPC
|
|
/* STATISTICS */ ,output reg new_instruction
|
|
`endif
|
|
`ifdef OTUPUT_JSON_STATISTICS
|
|
/* */ ,output wire [`L1_CACHE_SIZE-1:0] L1_SIZE_STAT, output wire VALID_INSTRUCTION_STAT
|
|
`endif
|
|
);
|
|
|
|
/* If there is an error either from the decoder or execution unit set it to ERROR */
|
|
assign ERROR=(DE_ERROR!=`ERR_NO_ERROR)?DE_ERROR:(EXEC_ERROR!=`ERR_NO_ERROR)?EXEC_ERROR:`ERR_NO_ERROR;
|
|
|
|
reg [`PROC_STATE_BITS-1:0] proc_state;
|
|
|
|
/*############ Execution Unit ################################################### */
|
|
|
|
wire [1:0] in_alu_sel1, in_alu_sel2;
|
|
assign in_alu_sel1 = DE_OUTPUT_sampled[44:43];
|
|
assign in_alu_sel2 = DE_OUTPUT_sampled[46:45];
|
|
|
|
wire [`EXEC_STATE_BITS-1:0] exec_state;
|
|
|
|
reg valid_exec_data, set_initial_values;
|
|
|
|
wire [`ERROR_BITS-1:0] EXEC_ERROR;
|
|
|
|
wire use_exec_reg_addr;
|
|
|
|
wire [3:0] EXEC_reg_read_port1_addr;
|
|
wire [15:0] ALU_O;
|
|
wire [7:0]EXEC_FLAGS;
|
|
|
|
wire [15:0] PARAM1_INIT, PARAM2_INIT;
|
|
assign PARAM1_INIT = DE_OUTPUT_sampled[23:8];
|
|
assign PARAM2_INIT = DE_OUTPUT_sampled[39:24];
|
|
|
|
wire [2:0] IN_MOD,OUT_MOD;
|
|
assign IN_MOD=DE_OUTPUT_sampled[2:0];
|
|
assign OUT_MOD=DE_OUTPUT_sampled[49:47];
|
|
|
|
wire [`ALU_OP_BITS-1:0] ALU_OP;
|
|
assign ALU_OP = DE_OUTPUT_sampled[42:40];
|
|
|
|
wire next_exec;
|
|
execute_unit execute_unit (
|
|
/* GENERAL */ clock, reset, Wbit, Sbit, opcode_size, INSTRUCTION_BUFFER,valid_exec_data
|
|
/* */ ,IN_MOD, OUT_MOD,memio_address_select, ProgCount, RM, EXEC_ERROR, write
|
|
/* */ ,set_initial_values,next_exec
|
|
/* PARAM */ ,PARAM1_INIT,PARAM2_INIT
|
|
/* STATE CONTROL */ ,exec_state, next_state
|
|
/* ALU CONTROL */ ,in_alu_sel1, in_alu_sel2, ALU_OP, ALU_O
|
|
/* REGISTER DATA */ ,reg_read_port1_data, reg_read_port2_data, EXEC_reg_read_port1_addr, use_exec_reg_addr, reg_write_we
|
|
/* FLAFS */ ,EXEC_FLAGS
|
|
/* BIU */ ,BIU_ADDRESS_INPUT, biu_write_request, biu_read_request, BIU_VALID_DATA, BIU_DATA, biu_data_direction, biu_jump_req
|
|
);
|
|
|
|
/*############ Bus Interface Unit ############################################### */
|
|
|
|
wire [15:0] INSTRUCTION_LOCATION, BIU_ADDRESS_INPUT;
|
|
wire [15:0] BIU_DATA;
|
|
wire [31:0] INSTRUCTION;
|
|
wire biu_write_request, biu_read_request, BIU_VALID_DATA;
|
|
wire biu_jump_req, biu_data_direction,VALID_INSTRUCTION;
|
|
reg valid_instruction_ack;
|
|
|
|
BIU BIU(
|
|
/* Outside world */ clock,reset,external_address_bus
|
|
/* */ ,external_data_bus,read,write,BHE,IOMEM
|
|
/* Internal */ ,INSTRUCTION,VALID_INSTRUCTION,INSTRUCTION_LOCATION,biu_jump_req
|
|
/* */ ,BIU_ADDRESS_INPUT,BIU_DATA,biu_write_request,biu_read_request,Wbit,BIU_VALID_DATA,MEM_OR_IO
|
|
/* */ ,valid_instruction_ack
|
|
`ifdef OTUPUT_JSON_STATISTICS
|
|
/* Statistics */ ,L1_SIZE_STAT, VALID_INSTRUCTION_STAT
|
|
`endif
|
|
);
|
|
|
|
assign BIU_DATA= biu_data_direction ? 16'hz : (memio_address_select ? reg_read_port1_data : ALU_O);
|
|
|
|
/*############ Decoder ########################################################## */
|
|
|
|
wire [`UCODE_ADDR_BITS-1:0] ucode_seq_addr_entry;
|
|
reg [`UCODE_ADDR_BITS-1:0] ucode_seq_addr;
|
|
reg SIMPLE_MICRO; /* output simple decodings (=0) or microcode data (=1) */
|
|
|
|
wire [`EXEC_STATE_BITS+`ERROR_BITS+65:0] DE_OUTPUT;
|
|
reg [`EXEC_STATE_BITS+`ERROR_BITS+65:0] DE_OUTPUT_sampled;
|
|
|
|
wire DE_DEPENDS_ON_PREVIOUS;
|
|
|
|
decoder decoder(
|
|
/* INPUT */ INSTRUCTION[31:16],FLAGS,
|
|
/* MICROCODE */ ucode_seq_addr_entry,SIMPLE_MICRO,ucode_seq_addr,
|
|
/* OUTPUT */ DE_OUTPUT,DE_DEPENDS_ON_PREVIOUS
|
|
);
|
|
|
|
wire [2:0] RM;
|
|
assign RM = DE_OUTPUT_sampled[5:3];
|
|
|
|
wire memio_address_select;
|
|
assign memio_address_select=DE_OUTPUT_sampled[6:6];
|
|
|
|
wire [3:0] DE_reg_read_port1_addr,DE_reg_read_port2_addr;
|
|
assign DE_reg_read_port1_addr=DE_OUTPUT_sampled[53:50];
|
|
assign DE_reg_read_port2_addr=DE_OUTPUT_sampled[57:54];
|
|
|
|
wire [3:0] reg_write_addr;
|
|
assign reg_write_addr=DE_OUTPUT_sampled[61:58];
|
|
|
|
wire MEM_OR_IO;
|
|
assign MEM_OR_IO = DE_OUTPUT_sampled[7:7];
|
|
|
|
wire Wbit, Sbit, opcode_size;
|
|
assign opcode_size=DE_OUTPUT_sampled[62:62];
|
|
assign Sbit=DE_OUTPUT_sampled[63:63];
|
|
assign Wbit=DE_OUTPUT_sampled[64:64];
|
|
|
|
wire [`ERROR_BITS-1:0] DE_ERROR;
|
|
assign HALT = DE_OUTPUT_sampled[65:65];
|
|
assign DE_ERROR = DE_OUTPUT_sampled[`ERROR_BITS+65:66];
|
|
|
|
wire [`EXEC_STATE_BITS-1:0] next_state;
|
|
assign next_state=DE_OUTPUT_sampled[`EXEC_STATE_BITS+`ERROR_BITS+65:`ERROR_BITS+66];
|
|
|
|
/*############ Registers ######################################################## */
|
|
|
|
reg [15:0] FLAGS;
|
|
reg [15:0] ProgCount;
|
|
|
|
wire [3:0] reg_read_port1_addr;
|
|
assign reg_read_port1_addr = use_exec_reg_addr ? EXEC_reg_read_port1_addr : DE_reg_read_port1_addr;
|
|
|
|
wire [15:0] reg_read_port1_data, reg_read_port2_data;
|
|
|
|
wire reg_write_we;
|
|
|
|
register_file register_file(
|
|
/* WRITE */ .write_port1_addr(reg_write_addr),
|
|
/* */ .write_port1_data(ALU_O),
|
|
/* */ .write_port1_we(reg_write_we),
|
|
/* READ 1 */ .read_port1_addr(reg_read_port1_addr),
|
|
/* */ .read_port1_data(reg_read_port1_data),
|
|
/* READ 2 */ .read_port2_addr(DE_reg_read_port2_addr),
|
|
/* */ .read_port2_data(reg_read_port2_data)
|
|
);
|
|
|
|
/*############ Processor State Machine ########################################## */
|
|
|
|
/*** RESET LOGIC ***/
|
|
|
|
always @(negedge reset) begin
|
|
proc_state <= `PROC_HALT; //TODO: race condition ??
|
|
`ifdef CALCULATE_IPC
|
|
new_instruction<=0;
|
|
`endif
|
|
end
|
|
always @(posedge reset) begin
|
|
proc_state <= `PROC_RESET;
|
|
valid_instruction_ack <= 0; // needs early init
|
|
end
|
|
|
|
/*** Processor stages ***/
|
|
|
|
wire [2:0] instr_end;
|
|
InstrSize InstrSize({INSTRUCTION[31:24],INSTRUCTION[21:19]},instr_end);
|
|
|
|
reg [23:0] INSTRUCTION_BUFFER;
|
|
|
|
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
|
|
|
|
always @(next_exec) begin
|
|
proc_state<=`PROC_DE_STATE_ENTRY;
|
|
if ( VALID_INSTRUCTION_lc == 1 && SIMPLE_MICRO == 0 && DE_DEPENDS_ON_PREVIOUS == 0 && ucode_seq_addr_entry==`UCODE_NO_INSTRUCTION) begin
|
|
`ifdef DEBUG_PC_ADDRESS
|
|
$display("Running command at %04x (%08x)",INSTRUCTION_LOCATION,INSTRUCTION);
|
|
`endif
|
|
`ifdef CALCULATE_IPC
|
|
new_instruction <= !new_instruction;
|
|
`endif
|
|
DE_OUTPUT_sampled <= DE_OUTPUT;
|
|
set_initial_values<= !set_initial_values;
|
|
valid_instruction_ack <= !valid_instruction_ack;
|
|
ProgCount <= INSTRUCTION_LOCATION+{12'b0,instr_end};
|
|
INSTRUCTION_BUFFER<=INSTRUCTION[23:0];
|
|
wait_exec<=1;
|
|
valid_exec_data<=1;
|
|
end else begin
|
|
wait_exec<=0;
|
|
valid_exec_data<=0;
|
|
end
|
|
end
|
|
|
|
reg wait_exec;
|
|
|
|
always @(posedge clock) begin
|
|
case(proc_state)
|
|
`PROC_RESET:begin
|
|
ucode_seq_addr <= `UCODE_NO_INSTRUCTION;
|
|
DE_OUTPUT_sampled <= 0;
|
|
SIMPLE_MICRO <= 0;
|
|
proc_state <= `PROC_DE_STATE_ENTRY;
|
|
owe_set_init <= 0;
|
|
set_initial_values<=0;
|
|
wait_exec<=0;
|
|
end
|
|
`PROC_DE_STATE_ENTRY:begin
|
|
if( VALID_INSTRUCTION==1 || SIMPLE_MICRO == 1 ) begin
|
|
if(wait_exec==0) begin
|
|
DE_OUTPUT_sampled <= DE_OUTPUT;
|
|
|
|
if(SIMPLE_MICRO==0||owe_set_init==1)begin
|
|
/* This runs at the start of the execution of an 8086 instruction */
|
|
`ifdef DEBUG_PC_ADDRESS
|
|
$display("Running command at %04x (%08x)",INSTRUCTION_LOCATION,INSTRUCTION);
|
|
`endif
|
|
`ifdef CALCULATE_IPC
|
|
new_instruction <= !new_instruction;
|
|
`endif
|
|
valid_instruction_ack <= !valid_instruction_ack;
|
|
owe_set_init<=0;
|
|
set_initial_values<= !set_initial_values;
|
|
ProgCount <= INSTRUCTION_LOCATION+{12'b0,instr_end};
|
|
INSTRUCTION_BUFFER<=INSTRUCTION[23:0];
|
|
end
|
|
|
|
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;
|
|
/*keep proc_state the same and rerun decode this time with all the data from the microcode rom*/
|
|
end else begin
|
|
/* This runs at the start of each execution cycle, with microcode this is more than once per 8086 instruction */
|
|
valid_exec_data <= 1;
|
|
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 else begin
|
|
if( DE_DEPENDS_ON_PREVIOUS == 0 )
|
|
if ( (ucode_seq_addr==`UCODE_NO_INSTRUCTION) && (ucode_seq_addr_entry!=`UCODE_NO_INSTRUCTION) && valid_exec_data==0 )begin
|
|
/*switch to microcode decoding*/
|
|
ucode_seq_addr <= ucode_seq_addr_entry;
|
|
SIMPLE_MICRO <= 1;
|
|
owe_set_init <= 1;
|
|
/*keep proc_state the same and rerun decode this time with all the data from the microcode rom*/
|
|
end
|
|
end
|
|
end
|
|
end
|
|
`PROC_HALT:begin
|
|
end
|
|
default:begin
|
|
end
|
|
endcase
|
|
end
|
|
|
|
always @(exec_state) begin
|
|
if(exec_state == `EXEC_DONE)
|
|
FLAGS <= {8'b0,EXEC_FLAGS}; //TODO: don't set all of them all the time!
|
|
end
|
|
|
|
endmodule
|