Skip to content

Commit 6bf32a5

Browse files
committed
Lab5 - Correct skeleton code set.
1 parent 5453963 commit 6bf32a5

29 files changed

+19483
-2
lines changed

Lab05/Lab5-skeleton/ALU.v

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
`include "alu_op.v"
2+
3+
module ALU (input [7:0] alu_op,
4+
input [31:0] alu_in_1,
5+
input [31:0] alu_in_2,
6+
output reg [31:0] alu_result,
7+
output alu_bcond);
8+
9+
always @(*) begin
10+
alu_bcond = 0;
11+
alu_result = 0;
12+
case(alu_op)
13+
`ADD : alu_result = alu_in_1 + alu_in_2;
14+
`SUB : alu_result = alu_in_1 - alu_in_2;
15+
`SLL : alu_result = alu_in_1 << alu_in_2;
16+
`XOR : alu_result = alu_in_1 ^ alu_in_2;
17+
`OR : alu_result = alu_in_1 | alu_in_2;
18+
`AND : alu_result = alu_in_1 & alu_in_2;
19+
`SRL : alu_result = alu_in_1 >> alu_in_2;
20+
`SRA : alu_result = alu_in_1 >>> alu_in_2;
21+
`BEQ : if(alu_in_1 == alu_in_2) alu_bcond = 1;
22+
`BNE : if(alu_in_1 != alu_in_2) alu_bcond = 1;
23+
`BLT : if(alu_in_1 < alu_in_2) alu_bcond = 1;
24+
`BGE : if(alu_in_1 >= alu_in_2) alu_bcond = 1;
25+
default: alu_result = 0;
26+
endcase
27+
end
28+
29+
endmodule
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
`include "alu_op.v"
2+
`include "opcodes.v"
3+
4+
module ALUControlUnit(input [31:0] instr,
5+
output reg [7:0] alu_op);
6+
7+
wire [6:0] opcode = instr[6:0];
8+
wire [2:0] funct3 = instr[14:12];
9+
wire Instr30 = instr[30];
10+
11+
wire [6:0] funct7;
12+
assign funct7 = {1'b0, Instr30, 5'b00000};
13+
14+
always @(*) begin
15+
case(opcode)
16+
`ARITHMETIC : begin
17+
case(funct3)
18+
`FUNCT3_ADD : begin // with `FUNCT7_SUB
19+
if(funct7 == `FUNCT7_SUB) alu_op = `SUB;
20+
else alu_op = `ADD;
21+
end
22+
`FUNCT3_SLL : alu_op = `SLL;
23+
`FUNCT3_XOR : alu_op = `XOR;
24+
`FUNCT3_OR : alu_op = `OR;
25+
`FUNCT3_AND : alu_op = `AND;
26+
`FUNCT3_SRL : alu_op = `SRL;
27+
default: alu_op = `ADD;
28+
endcase
29+
end
30+
`ARITHMETIC_IMM : begin
31+
case(funct3)
32+
`FUNCT3_ADD : alu_op = `ADD;
33+
`FUNCT3_SLL : alu_op = `SLL;
34+
`FUNCT3_XOR : alu_op = `XOR;
35+
`FUNCT3_OR : alu_op = `OR;
36+
`FUNCT3_AND : alu_op = `AND;
37+
`FUNCT3_SRL : alu_op = `SRL;
38+
default: alu_op = `ADD;
39+
endcase
40+
end
41+
`LOAD, `STORE, `JALR : begin
42+
alu_op = `ADD;
43+
end
44+
`BRANCH : begin
45+
case(funct3)
46+
`FUNCT3_BEQ : alu_op = `BEQ;
47+
`FUNCT3_BNE : alu_op = `BNE;
48+
`FUNCT3_BLT : alu_op = `BLT;
49+
`FUNCT3_BGE : alu_op = `BGE;
50+
default: alu_op = `BEQ;
51+
endcase
52+
end
53+
default: alu_op = `ADD;
54+
endcase
55+
end
56+
57+
endmodule

Lab05/Lab5-skeleton/Adder.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Adder(input [31:0] in0,
2+
input [31:0] in1,
3+
output [31:0] out);
4+
5+
assign out = in0 + in1;
6+
7+
endmodule
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module BranchPredictUnit(
2+
input [31:0] current_pc,
3+
output[31:0] predict_pc,
4+
input clk,
5+
input reset,
6+
7+
input update,
8+
input [31:0] faux_pas_pc,
9+
input actual_behavior
10+
);
11+
12+
wire [24:0] tag = current_pc[31:7];
13+
wire [4:0] index = current_pc[6:2];
14+
15+
reg [24:0] tag_table[0:31];
16+
reg [31:0] BTB[0:31];
17+
reg [4:0] BHSR;
18+
reg [1:0] PHT[0:31];
19+
20+
wire [24:0] upd_tag = faux_pas_pc[31:7];
21+
wire [4:0] upd_index = faux_pas_pc[6:2];
22+
23+
24+
always @(posedge clk) begin
25+
if (reset) begin
26+
BHSR <= 5'b0;
27+
for (integer i = 0; i < 32; i = i + 1) begin
28+
tag_table[i] <= 25'b0;
29+
BTB[i] <= 32'b0;
30+
PHT[i] <= 2'b00;
31+
end
32+
end
33+
else if(update) begin
34+
BHSR <= {BHSR[3:0], actual_behavior};
35+
tag_table[upd_index] <= upd_tag;
36+
BTB[upd_index] <= faux_pas_pc;
37+
if (actual_behavior) begin
38+
if (PHT[upd_index ^ BHSR] < 2'b11) begin
39+
PHT[upd_index ^ BHSR] <= PHT[upd_index ^ BHSR] + 1;
40+
end
41+
end
42+
else begin
43+
if (PHT[upd_index ^ BHSR] > 2'b00) begin
44+
PHT[upd_index ^ BHSR] <= PHT[upd_index ^ BHSR] - 1;
45+
end
46+
end
47+
end
48+
end
49+
50+
/* Gshare */
51+
assign predict_pc = (tag == tag_table[index]) && (PHT[index ^ BHSR] > 2'b01) ? BTB[index] : current_pc + 4;
52+
53+
/* always not taken */
54+
// assign predict_pc = current_pc + 4;
55+
56+
/* always taken */
57+
// assign predict_pc = (tag == tag_table[index]) ? BTB[index] : current_pc + 4;
58+
59+
endmodule

Lab05/Lab5-skeleton/BubbleGen.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module BubbleGen(
2+
// input taken,
3+
// input hit,
4+
input IF_wrong,
5+
input ID_wrong,
6+
input EX_wrong,
7+
output IF_is_bubble,
8+
output ID_is_bubble
9+
);
10+
11+
assign ID_is_bubble = EX_wrong;
12+
assign IF_is_bubble = EX_wrong || ID_wrong;
13+
// assign isbubble = taken ^ hit;
14+
15+
endmodule

Lab05/Lab5-skeleton/ControlUnit.v

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
`include "opcodes.v"
2+
3+
module ControlUnit (input [6:0] Instr,
4+
output MemRead,
5+
output MemtoReg,
6+
output MemWrite,
7+
output ALUSrc,
8+
output RegWrite,
9+
output PCtoReg,
10+
output Branch,
11+
output JAL,
12+
output JALR,
13+
output is_ecall);
14+
15+
assign MemRead = (Instr == `LOAD);
16+
assign MemtoReg = (Instr == `LOAD);
17+
assign MemWrite = (Instr == `STORE);
18+
assign ALUSrc = (Instr != `ARITHMETIC) && (Instr != `BRANCH);
19+
assign RegWrite = (Instr != `STORE) && (Instr != `BRANCH);
20+
assign PCtoReg = (Instr == `JAL) || (Instr == `JALR);
21+
22+
assign Branch = (Instr == `BRANCH);
23+
assign JAL = (Instr == `JAL);
24+
assign JALR = (Instr == `JALR);
25+
26+
assign is_ecall = (Instr == `ECALL);
27+
28+
endmodule
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
`include "opcodes.v"
2+
3+
module ControlflowDetectUnit(
4+
input [6:0] Instr,
5+
output is_ctrlflow
6+
);
7+
8+
assign is_ctrlflow = (Instr == `BRANCH) || (Instr == `JAL) || (Instr == `JALR);
9+
10+
endmodule
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module DataForwardingUnit (
2+
input [4:0] ID_EX_rs1,
3+
input [4:0] ID_EX_rs2,
4+
input [4:0] EX_MEM_rd,
5+
input [4:0] MEM_WB_rd,
6+
input EX_MEM_reg_write,
7+
input MEM_WB_reg_write,
8+
input ID_ctrl_is_ecall,
9+
output [1:0] forward_a,
10+
output [1:0] forward_b,
11+
output forward_ecall
12+
);
13+
14+
assign forward_a = (ID_EX_rs1 != 0 && ID_EX_rs1 == EX_MEM_rd && EX_MEM_reg_write) ? 2'b10 :
15+
(ID_EX_rs1 != 0 && ID_EX_rs1 == MEM_WB_rd && MEM_WB_reg_write) ? 2'b01 :
16+
2'b00;
17+
assign forward_b = (ID_EX_rs2 != 0 && ID_EX_rs2 == EX_MEM_rd && EX_MEM_reg_write) ? 2'b10 :
18+
(ID_EX_rs2 != 0 && ID_EX_rs2 == MEM_WB_rd && MEM_WB_reg_write) ? 2'b01 :
19+
2'b00;
20+
assign forward_ecall = ID_ctrl_is_ecall && (EX_MEM_rd == 17) && EX_MEM_reg_write;
21+
endmodule
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
`include "opcodes.v"
2+
3+
module HazardDetectionUnit(
4+
input [6:0] opcode,
5+
input [4:0] ID_rs1,
6+
input [4:0] ID_rs2,
7+
input [4:0] ID_EX_rd,
8+
input ID_EX_mem_read,
9+
input ID_ctrl_is_ecall,
10+
output PC_Write,
11+
output IF_ID_Write,
12+
output ID_CtrlUnitMux_sel
13+
);
14+
15+
wire use_rs1 = ID_rs1 != 0 && ((opcode == `ARITHMETIC) || (opcode == `ARITHMETIC_IMM) || (opcode == `LOAD) || (opcode == `STORE) || (opcode == `BRANCH));
16+
wire use_rs2 = ID_rs2 != 0 && ((opcode == `ARITHMETIC) || (opcode == `STORE) || (opcode == `BRANCH));
17+
18+
wire stall = (ID_ctrl_is_ecall && ID_EX_rd == ID_rs1) || (ID_EX_mem_read && (((ID_EX_rd == ID_rs1) && (use_rs1) || ((ID_EX_rd == ID_rs2) && (use_rs2)))));
19+
20+
assign PC_Write = !stall;
21+
assign IF_ID_Write = !stall;
22+
assign ID_CtrlUnitMux_sel = stall;
23+
24+
endmodule
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
`include "opcodes.v"
2+
3+
module ImmediateGenerator (input [31:0] Instr,
4+
output reg [31:0] imm_gen_out);
5+
6+
wire [6:0] op = Instr[6:0];
7+
8+
initial begin
9+
imm_gen_out = 0;
10+
end
11+
12+
13+
always @(*) begin
14+
imm_gen_out = 0;
15+
case(op)
16+
`ARITHMETIC_IMM, `LOAD, `JALR: begin
17+
imm_gen_out[11:0] = Instr[31:20];
18+
imm_gen_out[31:12] = {20{imm_gen_out[11]}};
19+
end
20+
`STORE : begin
21+
imm_gen_out[4:0] = Instr[11:7];
22+
imm_gen_out[11:5] = Instr[31:25];
23+
imm_gen_out[31:12] = {20{imm_gen_out[11]}};
24+
end
25+
`JAL : begin
26+
imm_gen_out[10:1] = Instr[30:21];
27+
imm_gen_out[11] = Instr[20];
28+
imm_gen_out[19:12] = Instr[19:12];
29+
imm_gen_out[20] = Instr[31];
30+
imm_gen_out[31:21] = {11{imm_gen_out[20]}};
31+
end
32+
`BRANCH : begin
33+
imm_gen_out[4:1] = Instr[11:8];
34+
imm_gen_out[10:5] = Instr[30:25];
35+
imm_gen_out[11] = Instr[7];
36+
imm_gen_out[12] = Instr[31];
37+
imm_gen_out[31:13] = {19{imm_gen_out[12]}};
38+
end
39+
default: imm_gen_out = 0;
40+
endcase
41+
end
42+
43+
44+
endmodule

0 commit comments

Comments
 (0)