-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch.v
73 lines (66 loc) · 1.47 KB
/
branch.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Branch estimation
and Branch-and-link descrimination (not implemented here)
*/
module branch(
input [31:0] Instr,
input [31:0] R1,
input [31:0] R2,
output reg Branch,
output reg BLink
);
//BGEZ and BLTZ have the same opcode
parameter BZ = 6'b1;
parameter BGEZ = 4'b1;
parameter BLTZ = 4'b0;
parameter BEQ = 6'b100;
parameter BNE = 6'b101;
parameter BLEZ = 6'b110;
parameter BGTZ = 6'b111;
wire [5:0] opcode;
wire [3:0] rt;
/* use a temporary variable to avoid repeating Blink every time */
reg _BLink;
assign opcode = Instr[31:26];
assign link = Instr[20];
assign rt = Instr[19:16];
always @*
begin
case(opcode)
BZ:
begin
case(rt)
BGEZ:
begin
Branch = ($signed(R1) >= $signed(0));
_BLink = Branch & link;
end
BLTZ:
begin
Branch = ($signed(R1) < $signed(0));
_BLink = Branch & link;
end
default:
begin
Branch = 0;
_BLink = 0;
end
endcase
end
BLEZ:
Branch = ($signed(R1) <= $signed(0));
BGTZ:
Branch = ($signed(R1) > $signed(0));
BEQ:
Branch = R1 == R2;
BNE:
Branch = R1 != R2;
default:
Branch = 0;
endcase
if(_BLink)
BLink = 1;
else
BLink = 0;
end
endmodule