2023-10-21 13:57:58 +00:00
|
|
|
module risc_v_cpu (input clock, reset, output [31:0] out);
|
|
|
|
|
2023-10-23 02:24:09 +00:00
|
|
|
wire alu_src, alu_not;
|
|
|
|
wire [3:0] alu_func;
|
2023-10-23 02:32:25 +00:00
|
|
|
wire [31:0] alu_in_b, alu_out;
|
2023-10-23 02:24:09 +00:00
|
|
|
|
2023-10-23 02:32:25 +00:00
|
|
|
wire reg_we;
|
2023-10-23 02:24:09 +00:00
|
|
|
wire [1:0] reg_sel_data_in;
|
|
|
|
wire [4:0] reg_sel_out_a, reg_sel_out_b, reg_sel_in;
|
2023-10-23 02:32:25 +00:00
|
|
|
wire [31:0] reg_data_out_a, reg_data_out_b, reg_data_in;
|
2023-10-21 13:57:58 +00:00
|
|
|
|
|
|
|
wire [31:0] instruction;
|
2023-10-23 02:24:09 +00:00
|
|
|
|
|
|
|
wire mem_we;
|
2023-10-23 02:32:25 +00:00
|
|
|
wire [31:0] mem_out;
|
2023-10-23 02:24:09 +00:00
|
|
|
|
2023-10-21 13:57:58 +00:00
|
|
|
wire [1:0] jmp_pc;
|
2023-10-23 02:24:09 +00:00
|
|
|
wire b_pc;
|
2023-10-21 13:57:58 +00:00
|
|
|
|
2023-10-23 02:32:25 +00:00
|
|
|
wire adder_pc;
|
2023-10-23 02:24:09 +00:00
|
|
|
wire [31:0] imm;
|
2023-10-21 13:57:58 +00:00
|
|
|
|
2023-10-23 02:24:09 +00:00
|
|
|
wire [31:0] pc_addr;
|
|
|
|
wire [31:0] pc_new_addr;
|
2023-10-21 13:57:58 +00:00
|
|
|
|
|
|
|
wire [1:0] pc_in;
|
|
|
|
|
|
|
|
|
|
|
|
wire [31:0] pc_store;
|
|
|
|
|
|
|
|
decoder decoder (
|
|
|
|
.instruction(instruction),
|
2023-10-23 02:24:09 +00:00
|
|
|
.imm(imm),
|
|
|
|
.reg_we(reg_we),
|
2023-10-21 13:57:58 +00:00
|
|
|
.adder_pc(adder_pc),
|
2023-10-23 02:24:09 +00:00
|
|
|
.reg_sel_data_in(reg_sel_data_in),
|
|
|
|
.reg_sel_out_a(reg_sel_out_a),
|
|
|
|
.reg_sel_out_b(reg_sel_out_b),
|
|
|
|
.reg_sel_in(reg_sel_in),
|
|
|
|
.alu_src(alu_src),
|
|
|
|
.alu_func(alu_func),
|
2023-10-21 13:57:58 +00:00
|
|
|
.mem_we(mem_we),
|
|
|
|
.jmp_pc(jmp_pc),
|
|
|
|
.b_pc(b_pc),
|
|
|
|
.alu_not(alu_not)
|
|
|
|
);
|
|
|
|
|
|
|
|
registers_bank registers_bank (
|
|
|
|
.clock(clock),
|
|
|
|
.reset(reset),
|
2023-10-23 02:24:09 +00:00
|
|
|
.we(reg_we),
|
|
|
|
.sel_in(reg_sel_in),
|
|
|
|
.sel_out_a(reg_sel_out_a),
|
|
|
|
.sel_out_b(reg_sel_out_b),
|
|
|
|
.data_in(reg_data_in),
|
2023-10-23 02:32:25 +00:00
|
|
|
.data_out_a(reg_data_out_a),
|
|
|
|
.data_out_b(reg_data_out_b)
|
2023-10-21 13:57:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
mux2_1 mux2_1_1 (
|
2023-10-23 02:32:25 +00:00
|
|
|
.A(reg_data_out_b),
|
2023-10-23 02:24:09 +00:00
|
|
|
.B(imm),
|
|
|
|
.S(alu_src),
|
|
|
|
.O(alu_in_b)
|
2023-10-21 13:57:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
alu alu (
|
2023-10-23 02:32:25 +00:00
|
|
|
.input_a(reg_data_out_a),
|
2023-10-23 02:24:09 +00:00
|
|
|
.input_b(alu_in_b),
|
|
|
|
.op_code(alu_func),
|
2023-10-21 13:57:58 +00:00
|
|
|
.out(alu_out)
|
|
|
|
);
|
|
|
|
|
|
|
|
mux2_1 #(2) mux2_1_2 (
|
|
|
|
.A(jmp_pc),
|
|
|
|
.B({alu_out[1], (alu_not ? ~alu_out[0] : alu_out[0])}),
|
|
|
|
.S(b_pc),
|
|
|
|
.O(pc_in)
|
|
|
|
);
|
|
|
|
|
|
|
|
mux4_1 mux4_1_1 (
|
2023-10-23 02:24:09 +00:00
|
|
|
.A(pc_addr + 4),
|
|
|
|
.B(pc_addr + imm),
|
2023-10-21 13:57:58 +00:00
|
|
|
.C(alu_out),
|
|
|
|
.D(0),
|
|
|
|
.S(pc_in),
|
2023-10-23 02:24:09 +00:00
|
|
|
.O(pc_new_addr)
|
2023-10-21 13:57:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
program_counter program_counter (
|
|
|
|
.clock(clock),
|
2023-10-22 13:41:39 +00:00
|
|
|
.reset(reset),
|
2023-10-23 02:24:09 +00:00
|
|
|
.pc_new_addr(pc_new_addr),
|
|
|
|
.pc_addr(pc_addr)
|
2023-10-21 13:57:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
instruction uut_instruction (
|
2023-10-23 02:24:09 +00:00
|
|
|
.address(pc_addr),
|
2023-10-21 13:57:58 +00:00
|
|
|
.instruction(instruction)
|
|
|
|
);
|
|
|
|
|
|
|
|
memory memory (
|
|
|
|
.clock(clock),
|
|
|
|
.reset(reset),
|
|
|
|
.we(mem_we),
|
|
|
|
.address(alu_out),
|
2023-10-23 02:32:25 +00:00
|
|
|
.data_in(reg_data_out_b),
|
|
|
|
.data_out(mem_out)
|
2023-10-21 13:57:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
mux4_1 mux4_1_2 (
|
2023-10-23 02:24:09 +00:00
|
|
|
.A(alu_out),
|
2023-10-23 02:32:25 +00:00
|
|
|
.B(mem_out),
|
2023-10-23 02:24:09 +00:00
|
|
|
.C(pc_addr + 4),
|
|
|
|
.D(pc_addr + alu_out),
|
|
|
|
.S(reg_sel_data_in),
|
|
|
|
.O(reg_data_in)
|
2023-10-21 13:57:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
endmodule
|