2022-08-14 23:05:44 +00:00
|
|
|
// Module Name: Ctrl
|
2022-08-14 00:37:13 +00:00
|
|
|
// Project Name: CSE141L
|
2022-08-12 05:01:28 +00:00
|
|
|
// control decoder (combinational, not clocked)
|
2022-08-13 22:34:01 +00:00
|
|
|
|
2022-08-14 00:37:13 +00:00
|
|
|
import Definitions::*;
|
2022-08-12 05:01:28 +00:00
|
|
|
|
2022-08-14 00:37:13 +00:00
|
|
|
module Ctrl #(
|
|
|
|
parameter W = 8,
|
|
|
|
parameter T = 10
|
|
|
|
) (
|
|
|
|
input logic [8:0] Instruction,
|
2022-08-14 01:06:19 +00:00
|
|
|
input logic [W-1:0] ALU_Out,
|
2022-08-14 00:37:13 +00:00
|
|
|
input logic [W-1:0] RegOutA, RegOutB, // select from register inputs or immediate inputs
|
2022-08-14 20:44:51 +00:00
|
|
|
input logic [T-1:0] ProgCtr_p1,
|
2022-08-14 00:37:13 +00:00
|
|
|
input logic [W-1:0] mem_out,
|
2022-08-14 01:06:19 +00:00
|
|
|
output op_mne ALU_OP, // control ALU operation
|
2022-08-14 00:37:13 +00:00
|
|
|
output logic [W-1:0] ALU_A, ALU_B,
|
|
|
|
output logic RegWrite, Done_in,
|
2022-08-14 20:44:51 +00:00
|
|
|
output logic [3:0] RaddrA, RaddrB, Waddr,
|
|
|
|
output logic [W-1:0] RegInput,
|
2022-08-14 00:37:13 +00:00
|
|
|
output logic BranchEZ, BranchNZ, BranchAlways,
|
|
|
|
output logic write_mem
|
|
|
|
);
|
2022-08-12 05:01:28 +00:00
|
|
|
|
2022-08-14 00:37:13 +00:00
|
|
|
logic [7:0] I_Immediate;
|
|
|
|
logic [7:0] T_Immediate;
|
|
|
|
logic [3:0] A_operand;
|
|
|
|
logic [3:0] S_operand;
|
|
|
|
logic [3:0] G_operand;
|
2022-08-12 05:01:28 +00:00
|
|
|
|
2022-08-14 00:37:13 +00:00
|
|
|
assign I_Immediate = Instruction[7:0];
|
|
|
|
assign T_Immediate = Instruction[2:0];
|
|
|
|
assign A_operand = Instruction[3:0];
|
2022-08-14 01:54:10 +00:00
|
|
|
assign S_operand = {1'b1, Instruction[2:0]};
|
|
|
|
assign G_operand = {1'b0, Instruction[2:0]};
|
2022-08-12 05:01:28 +00:00
|
|
|
|
2022-08-14 00:37:13 +00:00
|
|
|
always_comb begin
|
|
|
|
// default values for an invalid NOP instruction, proper NOP instruction encoded as a LSH by 0
|
|
|
|
ALU_OP = NOP;
|
|
|
|
ALU_A = RegOutA;
|
2022-08-20 05:44:47 +00:00
|
|
|
ALU_B = RegOutB;
|
2022-08-14 00:37:13 +00:00
|
|
|
RegWrite = 'b1;
|
|
|
|
Done_in = 'b0;
|
|
|
|
RaddrA = 'b0;
|
|
|
|
RaddrB = 'b0;
|
|
|
|
Waddr = 'b0;
|
|
|
|
RegInput = ALU_Out;
|
|
|
|
BranchEZ = 'b0;
|
|
|
|
BranchNZ = 'b0;
|
|
|
|
BranchAlways = 'b0;
|
|
|
|
write_mem = 'b0;
|
2022-08-20 04:16:03 +00:00
|
|
|
casez(Instruction[8:3])
|
|
|
|
'b1_????_?: begin // LDI
|
2022-08-14 00:37:13 +00:00
|
|
|
ALU_A = I_Immediate;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_0000_?: begin // PUT
|
2022-08-14 00:37:13 +00:00
|
|
|
Waddr = A_operand;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_0001_?: begin // GET
|
2022-08-14 00:37:13 +00:00
|
|
|
RaddrA = A_operand;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_0010_0: begin // LDW
|
2022-08-14 00:37:13 +00:00
|
|
|
RaddrA = S_operand;
|
|
|
|
RegInput = mem_out;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_0010_1: begin // STW
|
2022-08-14 00:37:13 +00:00
|
|
|
RaddrA = S_operand;
|
|
|
|
RegWrite = 'b0;
|
|
|
|
write_mem = 'b1;
|
|
|
|
end
|
2022-08-20 06:34:51 +00:00
|
|
|
'b0_0011_0: begin // NXT
|
2022-08-20 05:44:47 +00:00
|
|
|
if(S_operand == 'd8 || S_operand == 'd9 || S_operand == 'd10) begin
|
|
|
|
ALU_OP = SUB;
|
|
|
|
ALU_B = 'b1;
|
|
|
|
end
|
|
|
|
else if (S_operand == 'd11 || S_operand == 'd12 || S_operand == 'd13) begin
|
|
|
|
ALU_OP = ADD;
|
|
|
|
ALU_B = 'b1;
|
|
|
|
end
|
2022-08-14 00:37:13 +00:00
|
|
|
else ALU_OP = NOP;
|
|
|
|
RaddrA = S_operand;
|
|
|
|
Waddr = S_operand;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_0011_1: begin //CLB
|
2022-08-14 00:37:13 +00:00
|
|
|
ALU_OP = CLB;
|
|
|
|
RaddrA = G_operand;
|
|
|
|
Waddr = G_operand;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_0100_?: begin // ADD
|
2022-08-14 00:37:13 +00:00
|
|
|
ALU_OP = ADD;
|
2022-08-20 06:16:33 +00:00
|
|
|
RaddrA = A_operand;
|
2022-08-14 00:37:13 +00:00
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_0110_?: begin // ORR
|
2022-08-14 00:37:13 +00:00
|
|
|
ALU_OP = ORR;
|
2022-08-20 06:16:33 +00:00
|
|
|
RaddrA = A_operand;
|
2022-08-14 00:37:13 +00:00
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_0111_?: begin // AND
|
2022-08-14 00:37:13 +00:00
|
|
|
ALU_OP = AND;
|
2022-08-20 06:16:33 +00:00
|
|
|
RaddrA = A_operand;
|
2022-08-14 00:37:13 +00:00
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_1000_0: begin // LSH
|
2022-08-14 00:37:13 +00:00
|
|
|
ALU_OP = LSH;
|
|
|
|
ALU_A = T_Immediate;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_1000_1: begin // PTY
|
2022-08-14 00:37:13 +00:00
|
|
|
ALU_OP = RXOR_7;
|
|
|
|
RaddrA = G_operand;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_1001_?: begin // CHK
|
2022-08-14 00:37:13 +00:00
|
|
|
ALU_OP = RXOR_8;
|
|
|
|
RaddrA = A_operand;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_1010_?: begin // XOR
|
2022-08-14 00:37:13 +00:00
|
|
|
ALU_OP = XOR;
|
2022-08-20 06:16:33 +00:00
|
|
|
RaddrA = A_operand;
|
2022-08-14 00:37:13 +00:00
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_1011_?: begin // DNE
|
2022-08-14 00:37:13 +00:00
|
|
|
Done_in = 'b1;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_1110_0: begin // JNZ
|
2022-08-14 00:37:13 +00:00
|
|
|
RegWrite = 'b0;
|
|
|
|
RaddrA = G_operand;
|
|
|
|
BranchNZ = 'b1;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_1110_1: begin // JEZ
|
2022-08-14 00:37:13 +00:00
|
|
|
RegWrite = 'b0;
|
|
|
|
RaddrA = G_operand;
|
|
|
|
BranchEZ = 'b1;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_1111_0: begin // JMP
|
2022-08-14 00:37:13 +00:00
|
|
|
RegWrite = 'b0;
|
|
|
|
RaddrA = G_operand;
|
|
|
|
BranchAlways = 'b1;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
'b0_1111_1: begin // JAL
|
2022-08-14 00:37:13 +00:00
|
|
|
RaddrA = G_operand;
|
|
|
|
Waddr = 'd14; // write to link register specifically
|
2022-08-20 04:16:03 +00:00
|
|
|
RegInput = ProgCtr_p1[7:0]; // write the value pc+4
|
2022-08-14 00:37:13 +00:00
|
|
|
BranchAlways = 'b1;
|
|
|
|
end
|
2022-08-20 04:16:03 +00:00
|
|
|
default: begin
|
|
|
|
RegWrite = 'b0;
|
|
|
|
end
|
2022-08-14 00:37:13 +00:00
|
|
|
endcase
|
|
|
|
end
|
2022-08-12 05:01:28 +00:00
|
|
|
|
|
|
|
endmodule
|
|
|
|
|