-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainfsm.v
102 lines (97 loc) · 2.24 KB
/
mainfsm.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
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
module mainfsm (
clk,
reset,
Op,
Funct,
IRWrite,
AdrSrc,
ALUSrcA,
ALUSrcB,
ResultSrc,
NextPC,
RegW,
MemW,
Branch,
ALUOp
);
input wire clk;
input wire reset;
input wire [1:0] Op;
input wire [5:0] Funct;
output wire NextPC;
output wire Branch;
output wire MemW;
output wire RegW;
output wire IRWrite;
output wire AdrSrc;
output wire [1:0] ResultSrc;
output wire [1:0] ALUSrcA;
output wire [1:0] ALUSrcB;
output wire ALUOp;
reg [3:0] state;
reg [3:0] nextstate;
reg [12:0] controls;
localparam [3:0] FETCH = 0;
localparam [3:0] DECODE = 1;
localparam [3:0] MEMADR = 2;
localparam [3:0] MEMRD = 3;
localparam [3:0] MEMWB = 4;
localparam [3:0] MEMWR = 5;
localparam [3:0] EXECUTER = 6;
localparam [3:0] EXECUTEI = 7;
localparam [3:0] ALUWB = 8;
localparam [3:0] BRANCH = 9;
localparam [3:0] UNKNOWN = 10;
// state register
always @(posedge clk or posedge reset)
if (reset)
state <= FETCH;
else
state <= nextstate;
// next state logic
always @(*)
casex (state)
FETCH: nextstate = DECODE;
DECODE:
case (Op)
2'b00:
if (Funct[5])
nextstate = EXECUTEI;
else
nextstate = EXECUTER;
2'b01: nextstate = MEMADR;
2'b10: nextstate = BRANCH;
default: nextstate = UNKNOWN;
endcase
MEMADR:
if (Funct[0])
nextstate = MEMRD;
else
nextstate = MEMWR;
MEMRD: nextstate = MEMWB;
MEMWB: nextstate = FETCH;
MEMWR: nextstate = FETCH;
EXECUTER: nextstate = ALUWB;
EXECUTEI: nextstate = ALUWB;
ALUWB: nextstate = FETCH;
BRANCH: nextstate = FETCH;
default: nextstate = FETCH;
endcase
// state-dependent output logic
always @(*) begin
case (state)
FETCH: controls = 13'b1000101001100;
DECODE: controls = 13'b0000001001100;
MEMADR: controls = 13'b0000000000010;
MEMRD: controls = 13'b0000010000000;
MEMWB: controls = 13'b0001000100000;
MEMWR: controls = 13'b0010010000000;
EXECUTER: controls = 13'b0000000000001;
EXECUTEI: controls = 13'b0000000000011;
ALUWB: controls = 13'b0001000000000;
BRANCH: controls = 13'b0100001010010;
default: controls = 13'bxxxxxxxxxxxxx;
endcase
end
assign {NextPC, Branch, MemW, RegW, IRWrite, AdrSrc, ResultSrc, ALUSrcA, ALUSrcB, ALUOp} = controls;
endmodule