-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCU_FSM.sv
More file actions
193 lines (166 loc) · 6.08 KB
/
Copy pathCU_FSM.sv
File metadata and controls
193 lines (166 loc) · 6.08 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 10/25/2024 02:23:05 PM
// Design Name:
// Module Name: CU_FSM
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module CU_FSM(
input rst,
input clk,
input [6:0] opcode, //this is called IR[6:0]
input [2:0] funct3, //this is called IR[14:12], the exact instruction from the opcode series
input intr, //interrupt
//outputs
output logic PC_WE,
output logic RF_WE,
output logic mem_WE2,
output logic memRDEN1,
output logic memRDEN2,
output logic reset,
///not used
output logic csr_WE,
output logic int_taken,
output logic mret_exec
);
typedef enum { ST_INIT, ST_FETCH, ST_EXEC, ST_WRITEBACK, ST_INTR } state_type;
state_type NS, PS;
//state reg
always_ff @( posedge clk ) begin
if (rst) PS <= ST_INIT;
else PS <= NS;
end
always_comb begin
//sets all outputs to 0 each clock cycle
PC_WE = 1'b0;//pc write en
RF_WE = 1'b0;//reg file write en
mem_WE2 = 1'b0;//mem file write en 2
memRDEN1 = 1'b0;//mem file read en instruction
memRDEN2 = 1'b0;//mem file read en data
reset = 1'b0;//resets the PC
csr_WE = 1'b0;//write en for csr
int_taken = 1'b0;//csr int taken
mret_exec = 1'b0;//csr mret exec
//case block
case(PS)
//init state
ST_INIT: begin
reset = 1'b1;
NS = ST_FETCH;
end
//fetch state
ST_FETCH: begin
memRDEN1 = 1'b1;
NS = ST_EXEC;
end
//exec state. most difficult state
ST_EXEC: begin
case(opcode)
// CSR type ///////////////////////////////////////////////////////////////////////////////
7'b1110011: begin
case(funct3)
//"mret"
3'b000: begin
PC_WE = 1;
mret_exec = 1'b1;
end
//"csrrw"
3'b001: begin
PC_WE = 1;
RF_WE = 1;
csr_WE = 1;
end
//"csrrs"
3'b010: begin
PC_WE = 1;
RF_WE = 1;
csr_WE = 1;
end
//"csrrc"
3'b011: begin
PC_WE = 1;
RF_WE = 1;
csr_WE = 1;
end
endcase
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
// R type /////////////////////////////////////////////////////////////////////////////////
7'b0110011: begin
PC_WE = 1;
RF_WE = 1;
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
// I type /////////////////////////////////////////////////////////////////////////////////
7'b0010011: begin
PC_WE = 1;
RF_WE = 1;
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
7'b1100111: begin //jalr
PC_WE = 1;
RF_WE = 1;
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
7'b0000011: begin //load from memory instructions lw, lh, lhu
memRDEN2 = 1;
NS = ST_WRITEBACK;
end
// S type /////////////////////////////////////////////////////////////////////////////////
7'b0100011: begin
PC_WE = 1;
mem_WE2 = 1;
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
// B type /////////////////////////////////////////////////////////////////////////////////
7'b1100011: begin
PC_WE = 1;
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
// U type /////////////////////////////////////////////////////////////////////////////////
7'b0110111: begin
PC_WE = 1;
RF_WE = 1;
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
7'b0010111: begin
PC_WE = 1;
RF_WE = 1;
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
// J type /////////////////////////////////////////////////////////////////////////////////
7'b1101111: begin
PC_WE = 1;
RF_WE = 1;
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
default:NS = ST_INIT; //blank default
endcase
end
ST_WRITEBACK: begin
PC_WE = 1;
RF_WE = 1;
NS = (intr==1'b1) ? ST_INTR : ST_FETCH;
end
ST_INTR: begin
PC_WE = 1;
int_taken = 1'b1;
NS = ST_FETCH;
end
default: NS = ST_INIT;
endcase
end
endmodule