A CPU written in SystemVerilog for ECE 271.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.4 KiB

* Programmable CPU to run arbitrary assembly.
* clk, reset parameters behave as expected.
* inputs are data from the outside world, that are read via CPU instruction.
* prog, pinst, and paddr are all used to program the CPU:
* - prog is a flag. When high, instead of executing, it writes instructions to memory.
* - paddr is the address at which instructions are inserted.
module cpu (input logic clk, reset,
input logic prog,
input logic [15:0] inputs,
input logic[31:0] pinst,
input logic[7:0] paddr,
output logic [31:0] disp);
logic [7:0] pc;
logic [7:0] pc_next, pc_compute;
logic [31:0] inst;
logic [5:0] op;
logic [2:0] rd, rs, rt;
logic [31:0] rd_val, rs_val, rt_val;
logic [15:0] const_val;
logic [31:0] const_extend;
logic [31:0] cpu_disp;
logic [31:0] reg_alu_out, const_alu_out, val_out;
logic should_jump, should_write, use_const;
assign op = inst[31:26];
assign rd = inst[25:23];
assign rs = inst[22:20];
assign rt = inst[19:17];
assign const_val = inst[15:0];
assign should_write = inst[31];
assign use_const = inst[30];
assign should_jump = inst[29];
assign const_extend = const_val;
registers #(32) regs(
memory #(32) insts(
alu #(32) reg_alu(
alu #(32) const_alu(
mux2 #(32) out_mux(
mux2 #(32) rd_mux(
.right({16'b0, inputs}),
.select(~inst[28] & inst[27] & inst[26]),
assign pc_compute = rt_val + const_val;
mux2 #(8) pc_mux(
.left(pc + 8'b01),
.select(should_jump & (inst[28] | (inst[26] ^ (rs_val == 0)))),
always_ff@(posedge clk)
if(reset) begin
pc <= 0;
cpu_disp <= 0;
end else if(!prog) begin
6'b0000000: cpu_disp <= rs_val;
pc <= pc_next;
assign disp = cpu_disp;