From 04d1d3e2fe5a442ae7af9512e09878c0ef118b12 Mon Sep 17 00:00:00 2001 From: Riley King-Saunders Date: Thu, 15 Oct 2026 15:53:59 +1100 Subject: [PATCH] Added control unit. TODO: Test --- modules/assignment2.sv | 4 ++-- modules/control_unit.sv | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/modules/assignment2.sv b/modules/assignment2.sv index 09d0af5..36426c5 100644 --- a/modules/assignment2.sv +++ b/modules/assignment2.sv @@ -66,9 +66,9 @@ module assignment2 #(width=5, ramsz=9, romsz=10) ( register_file #(width, 5) REGS ( clk, rd_we, - (RDST) ? instruction[15:11] : instruction[20:16], rs, - instruction[25:21], rt, instruction[20:16], rd, + (RDST) ? instruction[15:11] : instruction[20:16], rt, + instruction[25:21], rs, regs_out ); // Always use sign-extension diff --git a/modules/control_unit.sv b/modules/control_unit.sv index 8b1cedf..869db29 100644 --- a/modules/control_unit.sv +++ b/modules/control_unit.sv @@ -4,10 +4,13 @@ module control_unit ( input logic [5:0] op, input logic [5:0] func, - output logic [0:0] M2R, output logic [0:0] M2W, - output logic [0:0] B, - output logic [3:0] ALU_FN, output logic [0:0] ALU_SRC, - output logic [0:0] RDST, output logic [0:0] RW + output logic [0:0] M2R, // Enables writing data stored in memory at the address (rs/A)+(ALU_SRC ? imm : rt/B) into rd + output logic [0:0] M2W, // Enable writing data stored in rt/B into memory at the address (rs/A)+(ALU_SRC ? imm : rt/B) + output logic [0:0] B, // AND'ed with flags to determine if branch is permitted + output logic [3:0] ALU_FN, // Controls what function the ALU is to perform + output logic [0:0] ALU_SRC, // Controls whether to use the rt/B register read port or 16-bit immediate + output logic [0:0] RDST, // Controls where to look in the instruction encoding for the register write address + output logic [0:0] RW // Enable register write port ); logic [0:0] TYPE_R; logic [0:0] TYPE_B1; @@ -20,8 +23,14 @@ module control_unit ( instruction_classifier CLASSIFIER (op, TYPE_R, TYPE_B1, TYPE_J, TYPE_B2, TYPE_I, TYPE_F, TYPE_M); always_comb begin - // TODO + M2R = TYPE_M & op[3]; + M2W = TYPE_M & ~op[3]; + B = TYPE_B1 | TYPE_B2 | TYPE_J; + ALU_FN = (func & {(6){TYPE_R}}) | ( {4'b0000, op[1:0]} & {(6){TYPE_I}}); + ALU_SRC = TYPE_B1 | TYPE_B2 | TYPE_I | TYPE_M; + RDST = TYPE_I; // Imm ops store back into rt + RW = TYPE_R | TYPE_I | (TYPE_M & op[3]); // All ALU R-type ops, Addi/Addiu ops, and memory loads end -endmodule; +endmodule `endif \ No newline at end of file