2023-02-09 14:46:21 +00:00
|
|
|
`include "proc_state_def.v"
|
2023-02-08 09:18:00 +00:00
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
module mux4 (in1,in2,in3,in4, sel,out);
|
|
|
|
input [0:1] sel;
|
|
|
|
parameter WIDTH=16;
|
|
|
|
input [WIDTH-1:0] in1,in2,in3,in4;
|
|
|
|
output [WIDTH-1:0] out;
|
|
|
|
assign out = (sel == 'b00) ? in1 :
|
|
|
|
(sel == 'b01) ? in2 :
|
|
|
|
(sel == 'b10) ? in3 :
|
|
|
|
in4;
|
2023-02-09 20:16:50 +00:00
|
|
|
endmodule
|
|
|
|
|
2023-02-11 01:05:19 +00:00
|
|
|
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 HALT,output reg ERROR);
|
2023-02-09 20:16:50 +00:00
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
/*** Global Definitions ***/
|
|
|
|
// State
|
2023-02-09 14:46:21 +00:00
|
|
|
reg [3:0] state;
|
2023-02-08 12:07:42 +00:00
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
// Registers
|
2023-02-08 12:07:42 +00:00
|
|
|
reg [19:0] ProgCount;
|
2023-02-09 14:46:21 +00:00
|
|
|
reg [15:0] CIR;
|
|
|
|
reg [15:0] PARAM1;
|
|
|
|
reg [15:0] PARAM2;
|
2023-02-10 12:02:20 +00:00
|
|
|
reg unaligned_access;
|
2023-02-08 12:07:42 +00:00
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
// Execution units
|
2023-02-09 20:16:50 +00:00
|
|
|
reg [1:0] in1_sel;
|
|
|
|
reg [1:0] in2_sel;
|
|
|
|
reg [1:0] out_sel;
|
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
/*** RESET LOGIC ***/
|
2023-02-08 12:07:42 +00:00
|
|
|
always @(negedge reset) begin
|
|
|
|
if (reset==0) begin
|
|
|
|
@(posedge clock);
|
2023-02-10 18:20:28 +00:00
|
|
|
state=`PROC_HALT_STATE;
|
2023-02-08 12:07:42 +00:00
|
|
|
ProgCount=0;//TODO: Reset Vector
|
2023-02-09 14:46:21 +00:00
|
|
|
HALT=0;
|
2023-02-10 01:45:27 +00:00
|
|
|
reg_read=1;
|
|
|
|
reg_write=1;
|
|
|
|
reg_read_read=1;
|
2023-02-10 12:02:20 +00:00
|
|
|
unaligned_access=0;
|
2023-02-10 01:45:27 +00:00
|
|
|
ALU_OUT=1;
|
2023-02-10 18:20:28 +00:00
|
|
|
@(posedge reset)
|
2023-02-09 14:46:21 +00:00
|
|
|
@(negedge clock);
|
|
|
|
state=`PROC_IF_STATE_ENTRY;
|
2023-02-08 12:07:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-10 01:45:27 +00:00
|
|
|
/*** ALU and EXEC stage logic ***/
|
|
|
|
|
|
|
|
//Architectural Register file
|
|
|
|
reg [2:0] reg_addr;
|
|
|
|
reg [15:0] reg_data;
|
|
|
|
reg reg_read;
|
|
|
|
reg reg_write;
|
|
|
|
reg [2:0] reg_read_addr;
|
|
|
|
reg [15:0] reg_read_data;
|
|
|
|
reg reg_read_read;
|
|
|
|
wire [15:0] reg_data_;
|
|
|
|
assign reg_data_=reg_data;
|
|
|
|
register_file register_file(reg_addr,reg_data_,reg_read,reg_write,reg_read_addr,reg_read_data,reg_read_read);
|
|
|
|
|
|
|
|
//ALU
|
|
|
|
mux4 #(.WIDTH(16)) MUX16_1A(
|
|
|
|
PARAM1,
|
|
|
|
16'b0,
|
|
|
|
16'b0,
|
|
|
|
16'b0,
|
|
|
|
in1_sel,
|
|
|
|
ADDER16_1A);
|
|
|
|
|
|
|
|
mux4 #(.WIDTH(16)) MUX16_1B(
|
2023-02-10 15:30:59 +00:00
|
|
|
PARAM2,
|
2023-02-10 01:45:27 +00:00
|
|
|
reg_read_data,
|
|
|
|
16'b0,
|
|
|
|
16'b0,
|
|
|
|
in2_sel,
|
|
|
|
ADDER16_1B);
|
|
|
|
|
|
|
|
wire [15:0] ADDER16_1A;
|
|
|
|
wire [15:0] ADDER16_1B;
|
|
|
|
wire [15:0] ADDER16_1O;
|
|
|
|
wire ADDER16_1C;
|
|
|
|
reg ALU_OUT;
|
|
|
|
reg [15:0] temp_out;
|
|
|
|
ADDER16 ADDER16_1(ADDER16_1A,ADDER16_1B,ALU_OUT,ADDER16_1O,ADDER16_1C);
|
|
|
|
|
|
|
|
/*** Processor stages ***/
|
2023-02-10 14:39:34 +00:00
|
|
|
|
2023-02-11 01:05:19 +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 @(negedge clock) begin
|
|
|
|
case(state)
|
|
|
|
`PROC_IF_WRITE_CIR:begin
|
2023-02-10 12:02:20 +00:00
|
|
|
if(unaligned_access)begin
|
|
|
|
CIR[15:8] <= external_data_bus[7:0];
|
|
|
|
ProgCount=ProgCount+1;
|
|
|
|
state=`PROC_IF_STATE_EXTRA_FETCH_SET;
|
|
|
|
end else begin
|
|
|
|
CIR <= external_data_bus;
|
|
|
|
state=`PROC_DE_STATE_ENTRY;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
`PROC_IF_STATE_EXTRA_FETCH:begin
|
|
|
|
CIR[7:0] <= external_data_bus[15:8];
|
|
|
|
state=`PROC_DE_STATE_ENTRY;
|
2023-02-09 14:46:21 +00:00
|
|
|
end
|
2023-02-09 20:16:50 +00:00
|
|
|
`PROC_EX_STATE_EXIT:begin
|
2023-02-10 01:45:27 +00:00
|
|
|
case(out_sel)
|
2023-02-10 14:39:34 +00:00
|
|
|
2'b11:begin
|
2023-02-10 01:45:27 +00:00
|
|
|
reg_write=0;
|
2023-02-10 14:39:34 +00:00
|
|
|
state=`PROC_IF_STATE_ENTRY;
|
2023-02-10 01:45:27 +00:00
|
|
|
end
|
|
|
|
default:begin
|
2023-02-10 14:39:34 +00:00
|
|
|
`invalid_instruction
|
2023-02-10 01:45:27 +00:00
|
|
|
end
|
|
|
|
endcase
|
2023-02-09 20:16:50 +00:00
|
|
|
end
|
2023-02-10 12:02:20 +00:00
|
|
|
`PROC_DE_LOAD_16_EXTRA_FETCH_SET:begin
|
|
|
|
external_address_bus = ProgCount;
|
|
|
|
state=`PROC_DE_LOAD_16_EXTRA_FETCH;
|
|
|
|
end
|
2023-02-09 14:46:21 +00:00
|
|
|
endcase
|
2023-02-08 12:07:42 +00:00
|
|
|
end
|
|
|
|
|
2023-02-09 14:46:21 +00:00
|
|
|
always @(posedge clock) begin
|
|
|
|
case(state)
|
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-11 01:05:19 +00:00
|
|
|
ERROR=0;
|
2023-02-09 14:46:21 +00:00
|
|
|
external_address_bus <= ProgCount;
|
|
|
|
read <= 0;
|
|
|
|
write <= 1;
|
2023-02-10 01:45:27 +00:00
|
|
|
reg_read_read=1;
|
|
|
|
reg_write=1;
|
|
|
|
ALU_OUT=1;
|
2023-02-09 14:46:21 +00:00
|
|
|
state=`PROC_IF_WRITE_CIR;
|
|
|
|
end
|
2023-02-10 12:02:20 +00:00
|
|
|
`PROC_IF_STATE_EXTRA_FETCH_SET:begin
|
|
|
|
external_address_bus <= ProgCount;
|
|
|
|
state=`PROC_IF_STATE_EXTRA_FETCH;
|
|
|
|
end
|
2023-02-09 14:46:21 +00:00
|
|
|
`PROC_DE_STATE_ENTRY:begin
|
|
|
|
case(CIR[15:10])
|
2023-02-10 12:02:20 +00:00
|
|
|
6'b000001 : begin
|
|
|
|
/* ADD, ... */
|
|
|
|
if ( CIR[9:9] == 0 )begin
|
|
|
|
/* Add Immediate to accumulator */
|
|
|
|
unaligned_access=~unaligned_access;
|
|
|
|
in1_sel=2'b00;
|
|
|
|
in2_sel=2'b01;
|
2023-02-10 14:39:34 +00:00
|
|
|
out_sel=2'b11;
|
2023-02-10 12:02:20 +00:00
|
|
|
reg_read_addr=3'b000;
|
|
|
|
reg_addr=3'b000;
|
|
|
|
reg_read_read=0;
|
|
|
|
ALU_OUT=0;
|
|
|
|
state=`PROC_DE_LOAD_16_PARAM;
|
|
|
|
end else begin
|
|
|
|
`invalid_instruction
|
|
|
|
end
|
|
|
|
end
|
2023-02-09 14:46:21 +00:00
|
|
|
6'b100000 : begin
|
2023-02-09 20:16:50 +00:00
|
|
|
/* ADD, ADC, SUB, SBB, CMP , AND, ... */
|
2023-02-09 14:46:21 +00:00
|
|
|
case (CIR[5:3])
|
2023-02-09 20:16:50 +00:00
|
|
|
3'b000 : begin
|
|
|
|
/* Add Immediate to register/memory */
|
2023-02-10 12:02:20 +00:00
|
|
|
if(unaligned_access==0)begin
|
|
|
|
ProgCount=ProgCount+1;
|
|
|
|
external_address_bus <= ProgCount;
|
|
|
|
end
|
2023-02-09 20:16:50 +00:00
|
|
|
in1_sel=2'b00;
|
|
|
|
in2_sel=2'b01;
|
2023-02-10 14:39:34 +00:00
|
|
|
out_sel=CIR[7:6];
|
2023-02-10 01:45:27 +00:00
|
|
|
reg_read_addr=CIR[2:0];
|
|
|
|
reg_addr=CIR[2:0];
|
|
|
|
reg_read_read=0;
|
|
|
|
ALU_OUT=0;
|
2023-02-09 14:46:21 +00:00
|
|
|
state=`PROC_DE_LOAD_16_PARAM;
|
|
|
|
end
|
|
|
|
default:begin
|
2023-02-09 20:16:50 +00:00
|
|
|
`invalid_instruction
|
2023-02-09 14:46:21 +00:00
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
2023-02-10 15:30:59 +00:00
|
|
|
6'b101100,
|
|
|
|
6'b101101,
|
|
|
|
6'b101110,
|
|
|
|
6'b101111 : begin
|
|
|
|
/*Move Immediate to register*/
|
|
|
|
unaligned_access=~unaligned_access;
|
|
|
|
in1_sel=2'b00;
|
|
|
|
in2_sel=2'b00;
|
|
|
|
out_sel=2'b11;
|
|
|
|
reg_addr=CIR[10:8];
|
|
|
|
ALU_OUT=0;
|
|
|
|
PARAM2=0;
|
|
|
|
state=`PROC_DE_LOAD_16_PARAM;
|
|
|
|
end
|
2023-02-09 14:46:21 +00:00
|
|
|
6'b111111 : begin
|
2023-02-09 20:16:50 +00:00
|
|
|
/* INC */
|
2023-02-09 14:46:21 +00:00
|
|
|
if (CIR[9:9] == 1 ) begin
|
|
|
|
case (CIR[5:3])
|
|
|
|
3'b000 :begin
|
2023-02-09 20:16:50 +00:00
|
|
|
/* Increment Register or Memmory */
|
2023-02-10 12:02:20 +00:00
|
|
|
if(unaligned_access==0)begin
|
|
|
|
ProgCount=ProgCount+1;
|
|
|
|
external_address_bus <= ProgCount;
|
|
|
|
end
|
2023-02-09 20:16:50 +00:00
|
|
|
in1_sel=2'b00;
|
|
|
|
in2_sel=2'b01;
|
2023-02-10 14:39:34 +00:00
|
|
|
out_sel=CIR[7:6];
|
2023-02-09 20:16:50 +00:00
|
|
|
PARAM1=1;
|
2023-02-10 01:45:27 +00:00
|
|
|
reg_read_addr=CIR[2:0];
|
|
|
|
reg_addr=CIR[2:0];
|
|
|
|
reg_read_read=0;
|
|
|
|
ALU_OUT=0;
|
2023-02-09 14:46:21 +00:00
|
|
|
state=`PROC_EX_STATE_ENTRY;
|
|
|
|
end
|
|
|
|
default:begin
|
2023-02-09 20:16:50 +00:00
|
|
|
`invalid_instruction
|
2023-02-09 14:46:21 +00:00
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end else begin
|
2023-02-09 20:16:50 +00:00
|
|
|
`invalid_instruction
|
2023-02-09 14:46:21 +00:00
|
|
|
end
|
|
|
|
end
|
2023-02-10 18:20:28 +00:00
|
|
|
6'b111101 : begin
|
|
|
|
/*HLT, CMC, TEST, NOT, NEG, MUL, IMUL, .... */
|
|
|
|
case (CIR[9:8])
|
|
|
|
2'b00:begin
|
|
|
|
/* HLT*/
|
|
|
|
unaligned_access=~unaligned_access;
|
|
|
|
HALT=1;
|
|
|
|
state=`PROC_HALT_STATE;
|
|
|
|
end
|
|
|
|
default:begin
|
|
|
|
`invalid_instruction;
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
|
|
|
|
end
|
2023-02-09 14:46:21 +00:00
|
|
|
default:begin
|
2023-02-09 20:16:50 +00:00
|
|
|
`invalid_instruction
|
2023-02-09 14:46:21 +00:00
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
`PROC_DE_LOAD_16_PARAM:begin
|
2023-02-10 12:02:20 +00:00
|
|
|
if(unaligned_access==1)begin
|
2023-02-10 14:08:39 +00:00
|
|
|
PARAM1[7:0] = external_data_bus[7:0];
|
2023-02-10 12:02:20 +00:00
|
|
|
ProgCount=ProgCount+1;
|
|
|
|
state=`PROC_DE_LOAD_16_EXTRA_FETCH_SET;
|
|
|
|
end else begin
|
2023-02-10 14:08:39 +00:00
|
|
|
PARAM1[7:0] = external_data_bus[15:8];
|
|
|
|
PARAM1[15:8] = external_data_bus[7:0];
|
2023-02-10 12:02:20 +00:00
|
|
|
ProgCount=ProgCount+1;
|
|
|
|
state=`PROC_EX_STATE_ENTRY;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
`PROC_DE_LOAD_16_EXTRA_FETCH:begin
|
2023-02-10 14:08:39 +00:00
|
|
|
PARAM1[15:8] = external_data_bus[15:8];
|
2023-02-09 14:46:21 +00:00
|
|
|
state=`PROC_EX_STATE_ENTRY;
|
|
|
|
end
|
|
|
|
`PROC_EX_STATE_ENTRY:begin
|
2023-02-10 01:45:27 +00:00
|
|
|
reg_data=ADDER16_1O;
|
2023-02-09 20:16:50 +00:00
|
|
|
state=`PROC_EX_STATE_EXIT;
|
2023-02-11 01:05:19 +00:00
|
|
|
ERROR=0;
|
2023-02-09 14:46:21 +00:00
|
|
|
end
|
|
|
|
endcase
|
2023-02-08 12:07:42 +00:00
|
|
|
end
|
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
|