-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol_unit.bmm
130 lines (113 loc) · 2.66 KB
/
control_unit.bmm
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
`timescale 1ns / 1ps
module control_unit(
input [31:0] iw, // instruction word
output [3:0] ALUCtrl,
output [1:0] ALUOp,
output regDst,
output regWrite,
output ALUSrc,
output memWrite,
output memRead,
output memToReg,
output branch
);
wire [5:0] opcode;
wire [4:0] rs;
wire [4:0] rt;
wire [15:0] func;
reg [3:0] ALUCtrlReg;
reg [1:0] ALUOpReg;
reg regDstReg;
reg regWriteReg;
reg ALUSrcReg;
reg memWriteReg;
reg memReadReg;
reg memToRegReg;
reg branchReg;
// extracting instruction word
assign opcode = iw[31:26];
assign rs = iw[25:21];
assign rt = iw[20:16];
assign func = iw[5:0];
always @(opcode,func)
begin
case (opcode)
6'b000000: // R-type
begin
ALUOpReg <= 2'b00;
regDstReg <= 1'b1;
regWriteReg <= 1'b1;
ALUSrcReg <= 1'b0;
memWriteReg <= 1'b0;
memReadReg <= 1'b0;
memToRegReg <= 1'b0;
branchReg <= 1'b0;
case (func)
6'b100000:begin ALUCtrlReg <= 4'b0010; end // add
6'b100010:begin ALUCtrlReg <= 4'b0110; end // subtract
6'b100100:begin ALUCtrlReg <= 4'b0000; end // AND
6'b100101:begin ALUCtrlReg <= 4'b0001; end // OR
6'b101010:begin ALUCtrlReg <= 4'b0111; end // set-on-less-than
default:begin ALUCtrlReg <= 4'bxxxx; end
endcase
end
6'b100010: // load
begin
ALUCtrlReg <= 4'b0010;
ALUOpReg <= 2'b00;
regDstReg <= 1'b0;
regWriteReg <= 1'b1;
ALUSrcReg <= 1'b1;
memWriteReg <= 1'b0;
memReadReg <= 1'b1;
memToRegReg <= 1'b1;
branchReg <= 1'b0;
end
6'b101011: // store
begin
ALUCtrlReg <= 4'b0010;
ALUOpReg <= 2'b00;
regDstReg <= 1'bx;
regWriteReg <= 1'b0;
ALUSrcReg <= 1'b1;
memWriteReg <= 1'b1;
memReadReg <= 1'b0;
memToRegReg <= 1'bx;
branchReg <= 1'b0;
end
6'b000010: // branch
begin
ALUCtrlReg <= 4'b0110;
ALUOpReg <= 2'b01;
regDstReg <= 1'bx;
regWriteReg <= 1'bx;
ALUSrcReg <= 1'b0;
memWriteReg <= 1'b0;
memReadReg <= 1'b0;
memToRegReg <= 1'bx;
branchReg <= 1'b1;
end
default:
begin
ALUCtrlReg <= 4'bxxxx;
ALUOpReg <= 2'bxx;
regDstReg <= 1'bx;
regWriteReg <= 1'bx;
ALUSrcReg <= 1'bx;
memWriteReg <= 1'bx;
memReadReg <= 1'bx;
memToRegReg <= 1'bx;
branchReg <= 1'bx;
end
endcase
end
assign ALUCtrl = ALUCtrlReg;
assign ALUOp = ALUOpReg;
assign regDst = regDstReg;
assign regWrite = regWriteReg;
assign ALUSrc = ALUSrcReg;
assign memWrite = memWriteReg;
assign memRead = memReadReg;
assign memToReg = memToRegReg;
assign branch = branchReg;
endmodule