-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSRAM_CTR.v
198 lines (189 loc) · 3.26 KB
/
SRAM_CTR.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
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
194
195
196
197
198
// build a module for ForwardUnit detection
module SRAM_CTR
(
clk,
rst,
MEM_R_EN,
MEM_W_EN,
SRAMaddress,
SRAMWEn,
SRAMdata,
SRAM_NOT_READY,
writeData,
address,
readData
);
// define input and output ports
input clk;
input MEM_R_EN;
input MEM_W_EN;
input rst;
output SRAMWEn;
output [17:0] SRAMaddress;
input [31:0] writeData;
input [15:0] address;
output SRAM_NOT_READY;
output [31:0] readData;
inout [15:0] SRAMdata;
// parameters
localparam INIT = 0;
localparam READ_1 = 1;
localparam READ_2 = 2;
localparam WRITE_1 = 3;
localparam WAIT = 4;
// wire and registers
reg SRAMWEn;
reg [17:0] SRAMaddress;
reg InnerStall;
reg [2:0] counter;
reg [2:0] presentState;
reg [2:0] nextState;
reg [15:0] readData_L;
reg [15:0] readData_H;
reg [15:0] data_to_sram;
// build module
assign SRAM_NOT_READY = |counter | InnerStall;
assign readData = { readData_H, readData_L };
always @(negedge clk) begin
if (rst)
begin
readData_L <= 16'b0;
readData_H <= 16'b0;
end
else
if ( !(presentState ^ READ_1) )
readData_L <= SRAMdata;
if ( !(presentState ^ READ_2) )
readData_H <= SRAMdata;
begin
end
end
// stall counter
always @(posedge clk)
begin
if( rst )
begin
counter <= 3'b0;
end
else
begin
if( InnerStall )
begin
counter <= 3'h4;
end
if( ~InnerStall & |counter )
begin
counter <= counter - 1;
end
end
end
// controller
always @( posedge clk )
begin
if( rst )
presentState <= INIT;
else
presentState <= nextState;
end
always @(*) begin
case (presentState)
INIT:
begin
if (MEM_R_EN)
begin
nextState = READ_1;
end
else if (MEM_W_EN)
begin
nextState = WRITE_1;
end
else
begin
nextState = INIT;
end
end
READ_1:
begin
nextState = READ_2;
end
READ_2:
begin
nextState = WAIT;
end
WRITE_1:
begin
nextState = WAIT;
end
WAIT:
if ( SRAM_NOT_READY )
begin
nextState = WAIT;
end
else
begin
nextState = INIT;
end
endcase
end
always @(*)
begin
case(presentState)
INIT:
begin
if (MEM_R_EN)
begin
InnerStall = 1'b1;
SRAMWEn = 1'b1;
SRAMaddress = { 1'b0, address, 1'b0 };
data_to_sram = {16{1'bz}};
end
else
begin
if (MEM_W_EN)
begin
InnerStall = 1'b1;
SRAMWEn = 1'b0;
SRAMaddress = { 1'b0, address, 1'b0 };
data_to_sram = writeData[15:0];
end
else
begin
InnerStall = 1'b0;
SRAMWEn = 1'b1;
SRAMaddress = { 1'b0, address, 1'b0 };
data_to_sram = {16{1'bz}};
end
end
end
READ_1:
begin
InnerStall = 1'b0;
SRAMWEn = 1'b1;
SRAMaddress = { 1'b0, address, 1'b1 };
data_to_sram = {16{1'bz}};
end
READ_2:
begin
InnerStall = 1'b0;
SRAMWEn = 1'b1;
SRAMaddress = { 1'b0, address, 1'b1 };
data_to_sram = {16{1'bz}};
end
WRITE_1:
begin
InnerStall = 1'b0;
SRAMWEn = 1'b0;
SRAMaddress = { 1'b0, address, 1'b1 };
data_to_sram = writeData[31:16];
end
WAIT:
begin
InnerStall = 1'b0;
SRAMWEn = 1'b1;
SRAMaddress = { 1'b0, address, 1'b1 };
data_to_sram = {16{1'bz}};
end
endcase
end
assign SRAMdata = data_to_sram;
endmodule