9086/system/processor.v

250 lines
7.8 KiB
Coq
Raw Normal View History

/* 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"
2023-02-08 09:18:00 +00:00
`define PROC_STATE_BITS 3
`define PROC_RESET 3'b000
`define PROC_DE_STATE_ENTRY 3'b001
`define PROC_WAIT 3'b010
`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 ( input clock, input reset, output [19:0] external_address_bus, inout [15:0] external_data_bus,output read, output write,output BHE,output IOMEM, output wire HALT,output [`ERROR_BITS-1:0] ERROR);
/* 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;
2023-02-14 13:13:40 +00:00
reg [`PROC_STATE_BITS-1:0] state;
2023-02-08 12:07:42 +00:00
/*############ 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 stall;
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,stall
/* 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;
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
/* */ ,state,SIMPLE_MICRO,stall
);
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;
decoder decoder(
/* INPUTS */ INSTRUCTION[31:16],FLAGS,
/* MICROCODE */ ucode_seq_addr_entry,SIMPLE_MICRO,ucode_seq_addr,
/* OUTPUT */ DE_OUTPUT
);
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
state <= `PROC_HALT; //TODO: race condition ??
end
always @(posedge reset) begin
state <= `PROC_RESET;
end
/*** Processor stages ***/
wire [2:0] instr_end;
InstrSize InstrSize({INSTRUCTION[31:24],INSTRUCTION[21:19]},instr_end);
reg [23:0] INSTRUCTION_BUFFER;
2023-02-10 14:39:34 +00:00
always @(posedge clock) begin
case(state)
`PROC_RESET:begin
ucode_seq_addr <= `UCODE_NO_INSTRUCTION;
DE_OUTPUT_sampled <= 0;
SIMPLE_MICRO <= 0;
state <= `PROC_DE_STATE_ENTRY;
end
`PROC_DE_STATE_ENTRY:begin
if (!stall) begin
if(VALID_INSTRUCTION==1) begin
DE_OUTPUT_sampled <= DE_OUTPUT;
if(SIMPLE_MICRO==0)begin
set_initial_values<=0;
`ifdef DEBUG_PC_ADDRESS
$display("Running command at %04x (%08x)",INSTRUCTION_LOCATION,INSTRUCTION);
`endif
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 state the same and rerun decode this time with all the data from the microcode rom*/
end else begin
valid_exec_data <= 1;
state <= `PROC_WAIT;
end
end
end
end
`PROC_WAIT:begin
if(!stall) begin
set_initial_values<=1;
valid_exec_data<=0;
state <= `PROC_DE_STATE_ENTRY;
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
2023-02-13 10:36:37 +00:00
end
end
`PROC_HALT:begin
end
default:begin
end
2023-02-13 10:36:37 +00:00
endcase
2023-02-08 12:07:42 +00:00
end
2023-02-08 09:18:00 +00:00
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
2023-02-08 09:18:00 +00:00
endmodule