-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryController.sv
263 lines (245 loc) · 8.15 KB
/
MemoryController.sv
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//**************************************************************//
//** SDRAM Controller v.01a - SS 2019 **//
//** Based on initial expermiments with Hynix HY57V561620F **//
//** 256Mb (16Mx16bit) 4 bank SDRAM **//
//** Basic functionality only, no burst access. **//
//**************************************************************//
//AC timings
//localparam DRIVECLOCKPERIOD = 31.25; //Testing at 32.00Mhz - period @ 31.25ns
localparam DRIVECLOCKPERIOD = 20; //Testing at 50.00Mhz - period @ 20ns
//localparam DRIVECLOCKPERIOD = 7.58; //132Mhz design max - period @ ~7.58ns
//localparam DRIVECLOCKPERIOD = 400; //Testing at 10.00Mhz - period @ 400ns
localparam iDelay = 200 * 1000; //Init delay in ns
localparam tRC = 63; //RAS CYCLE TIME in nS
localparam tRCD = 20; //RAS TO CAS DELAY in nS
localparam tRAS = 42; //RAS MINIMUM ACTIVE TIME in nS
localparam tRP = 20; //RAS PRECHARGE TIME in nS
localparam tRRD = 15; //RAS TO RAS BANK ACTIVE DELAY in nS
localparam tREF = 64; //MAX refresh time in msec
//Physical RAM properties
localparam noBanks = 4;
localparam noRows = 8192;
//Calculated or specified cycle delays
localparam tCCD = 1; //CAS TO CAS Delay in CLOCK CYCLES
localparam tDPL = 2; //DataIn to Precharge delay in CLOCK CYCLES
parameter int iDelayCycles = (iDelay + DRIVECLOCKPERIOD - 1) / DRIVECLOCKPERIOD; //Init delay in CLOCK cycles
parameter int tRCDelayCycles = (tRC + DRIVECLOCKPERIOD - 1) / DRIVECLOCKPERIOD; //RCD delay in CLOCK cycles
parameter int tRCDDelayCycles = (tRCD + DRIVECLOCKPERIOD - 1) / DRIVECLOCKPERIOD; //RCD delay in CLOCK cycles
parameter int tRPCycles = (tRP + DRIVECLOCKPERIOD - 1) / DRIVECLOCKPERIOD; //Precharge delay in CLOCK cycles
parameter int tRASMaxCycles = (100000/DRIVECLOCKPERIOD); //Maximum row open time in CLOCK cycles
localparam CASCycles = 4; //CAS address strobe delay in CLOCK cycles
localparam autoRefreshInitCycles = 8; //Number of complete bank REFRESH cycles in init phase;
parameter int interRefreshCycles = (tREF * 1000000) / noRows - 1000; //1000 cycle fudge factor.
module MemoryController(input logic ramclk,
//Read handling
input logic readRequest, //Incoming read request
output logic readRequestGrant, //Incoming read request grant
input logic [31:0] readAddress, //The read address
output logic [15:0] readData, //The data output
output logic readValid, //advise the caller the data on the bus is valid.
//Write handling
input logic writeRequest,
output logic writeRequestGrant, //Incoming write request grant
input logic [31:0] writeAddress, //The write address
input logic [15:0] writeData, //The input data to be written.
output logic writeCommit, //advise caller the data on the bus has been written.
//Output SDRAM signals
output logic CKE, CS , RAS, CAS, WE,
output logic [12:0] addr,
output logic [1:0] bank = 2'b00,
output logic [1:0] dqm,
inout wire [15:0] dq
);
typedef enum logic [3:0] {
MODEREG = 4'b0000,
NOP = 4'b0111,
BANKACTIVE = 4'b0011,
READ = 4'b0101,
WRITE = 4'b0100,
PRECHARGE = 4'b0010,
AUTOREFRESH = 4'b0001
} Commands;
typedef enum logic [3:0]{
INIT, INITREFRESH, INITSETREG, IDLE, READHANDLING, WRITEHANDLING, REFRESH
} InternalState;
InternalState state = INIT;
Commands command = NOP;
logic requestRead;
logic requestWrite;
logic [15:0] subState = 0; //Tracks substates within states.
logic [15:0] delayCounter = 0; //Tracks clock cycle delays within states.
logic [15:0] refreshDelayCounter = 0;
logic [12:0] autoRefreshCounter = 0;
logic [4:0] initRefreshCounter = 0;
logic [15:0] rowOpenCounter = 0;
logic moveToRead, moveToWrite;
assign moveToRead = readRequest;
assign moveToWrite = ~readRequest & writeRequest;
assign CKE = 1'bZ;
assign dqm = 2'b00;
assign CS = command[3];
assign RAS = command[2];
assign CAS = command [1];
assign WE = command[0];
//TRISTATE - URGHH.
logic dataout_enable /* synthesis preserve */;
logic [15:0]dq_i; //This is the internal input net, driven from a constant assign from dq.
assign dq_i = dq; //Drives the internal value from the input;
logic [15:0]dq_o =0; //And this, this is the internal output net - see the assign of DB.
assign dq = dataout_enable ? dq_o : 16'bZZZZZZZZZZZZZZZZ; //Output values when dataout_enable is true, otherwise HZ.
always @ (posedge ramclk) begin
writeCommit <= 0;
readValid <= 0;
readRequestGrant <= 0;
writeRequestGrant <= 0;
refreshDelayCounter <= refreshDelayCounter + 16'b1;
case (state)
INIT: begin
dataout_enable <= 0;
command <= NOP;
if (delayCounter == iDelayCycles) begin
state <= INITREFRESH;
delayCounter <= 0;
subState <= 0;
end
delayCounter <= delayCounter + 16'd1;
end
INITREFRESH: begin
//If we've hit the require number of refreshes
if (initRefreshCounter == autoRefreshInitCycles) begin
state <= INITSETREG; //Go to set register
subState <= 0; //Clear the substate counter
autoRefreshCounter <= 0; //and the autorefresh counter (we'll need it shortly).
end
else
begin
command <= AUTOREFRESH; //Set the autorefresh command.
if (autoRefreshCounter == 8191) begin //If we've counted up to the row count
autoRefreshCounter <= 0; //reset the row counter
initRefreshCounter <= initRefreshCounter + 5'd1; //and increment the cycle count.
end
autoRefreshCounter <= autoRefreshCounter + 13'd1;
end
end
INITSETREG: begin
if (subState == 0) begin //One shot
addr <= 13'b000001000110000;
dataout_enable <= 1;
// A9 - 1 for single write. A6:4 = 011 for CAS3. A3 = 0 for burst sequential.
//A2:0 = 000 for burst length 1.
command <= MODEREG;
subState <= 1;
end
else begin
state <= IDLE;
subState <= 0;
refreshDelayCounter <= 0;
end
end
IDLE: begin
subState <= 0;
delayCounter <= 0;
command <= NOP;
dataout_enable <= 0;
if (refreshDelayCounter > interRefreshCycles) begin
state <= REFRESH;
refreshDelayCounter <=0;
end
if (moveToRead) begin
readRequestGrant <= 1;
state <=READHANDLING;
end
if (moveToWrite) begin
writeRequestGrant <= 1;
state <= WRITEHANDLING;
end
end
WRITEHANDLING: begin
HandleW();
end
READHANDLING: begin
HandleR();
end
REFRESH: begin
command <= NOP;
if (delayCounter == 0) command <= AUTOREFRESH; //Set the autorefresh command.
if (tRCDelayCycles == tRCDelayCycles) begin
state <= IDLE;
end
end
endcase
end
task HandleR();
dataout_enable <= 0;
command <= NOP;
case (subState)
0: begin
bank <= readAddress[23:22];
addr <= readAddress[21:9];
if (delayCounter == 0) command <= BANKACTIVE;
delayCounter <= delayCounter + 16'd1;
if (delayCounter == tRCDDelayCycles) begin
subState <= 1;
delayCounter <= 0;
end
end
1: begin
if (delayCounter == 0) command <= READ; //read command in the first cycle after tRCD.
addr <= readAddress[8:0];
delayCounter <= delayCounter + 16'd1;
if (delayCounter == CASCycles) begin
delayCounter <= 0;
readData <= dq_i;
readValid <= 1;
subState <= 2;
end
end
2: begin
if (delayCounter == 0) command <= PRECHARGE;
delayCounter <= delayCounter + 16'd1;
if (delayCounter == tRPCycles) begin
subState <= 0;
state <= IDLE;
writeCommit <= 1;
end
end
endcase
endtask
task HandleW();
command <= NOP;
case (subState)
0: begin //Activate bank & row
bank <= writeAddress[23:22];
addr <= writeAddress[21:9];
if (delayCounter == 0) command <= BANKACTIVE;
delayCounter <= delayCounter + 16'd1;
if (delayCounter == tRCDDelayCycles) begin
subState <= 1;
delayCounter <= 0;
end
end
1: begin
if (delayCounter == 0) command <= WRITE;
addr <= writeAddress[8:0];
dataout_enable <= 1;
dq_o <= writeData;
delayCounter <= delayCounter + 16'd1;
if (delayCounter == tDPL) begin
subState <= 2;
delayCounter <= 0;
end
end
2: begin
if (delayCounter == 0) command <= PRECHARGE;
delayCounter <= delayCounter + 16'd1;
if (delayCounter == tRPCycles) begin
dq_o <= 0;
dataout_enable <= 0;
subState <= 0;
state <= IDLE;
writeCommit <= 1;
end
end
endcase
endtask
endmodule