OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [fpga/] [experiments/] [memctrl/] [sim/] [memctrl-2/] [ramctrl/] [ramctrl.v] - Blame information for rev 314

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 314 hellwig
//
2
// ramctrl.v -- RAM controller
3
//
4
 
5
 
6
`include "ramctrl/k4s561632e.v"
7
 
8
 
9
`timescale 1ns/10ps
10
`default_nettype none
11
 
12
 
13
`define MODE            13'h0032        // CL = 3, sequ. burst length = 4
14
 
15
`define CMD_MRSET       3'b000          // mode register set
16
`define CMD_ARFRS       3'b001          // auto refresh
17
`define CMD_PRCHG       3'b010          // precharge (deactivate row/rows)
18
`define CMD_ACTV        3'b011          // select bank, activate row
19
`define CMD_WRITE       3'b100          // select bank & column, start write
20
`define CMD_READ        3'b101          // select bank & column, start read
21
`define CMD_BSTOP       3'b110          // burst stop
22
`define CMD_NOP         3'b111          // no operation
23
 
24
//
25
// Note: The FSM is a registered Mealy machine. Its actions, which
26
//       are noted here for a specific state, take place in the next
27
//       clock cycle. This is only a notational problem: the actions
28
//       should in fact be associated with state transitions.
29
//
30
// ST_RESET                             // NOP, CKE=0, CS_N=1, wait 100 us
31
`define ST_INIT0        5'd0            // NOP, CKE=1, CS_N=0, wait 100 us
32
`define ST_INIT1        5'd1            // PRECHARGE ALL
33
`define ST_INIT2        5'd2            // NOP, wait tRP - 1 cycle
34
`define ST_INIT3        5'd3            // AUTO REFRESH
35
`define ST_INIT4        5'd4            // NOP, wait tRFC - 1 cycle
36
`define ST_INIT5        5'd5            // AUTO REFRESH
37
`define ST_INIT6        5'd6            // NOP, wait tRFC - 1 cycle
38
`define ST_INIT7        5'd7            // MODE REGISTER SET
39
`define ST_INIT8        5'd8            // NOP, wait tMRD - 1 cycle
40
`define ST_IDLE         5'd9            // AUTO REFRESH, ACTIVE, or NOP
41
`define ST_RFRSH        5'd10           // NOP, wait tRFC - 1 cycle
42
`define ST_WRDATA0      5'd11           // NOP, wait tRCD - 1 cycle
43
`define ST_WRDATA1      5'd12           // WRITE, de=1
44
`define ST_WRDATA2      5'd13           // NOP, wait 3 cycles
45
`define ST_WRDATA3      5'd14           // NOP, ack=1, de=0, wait 2 cycles
46
`define ST_WRDATA4      5'd15           // NOP, ack=0, wait 2 cycles
47
`define ST_RDDATA0      5'd16           // NOP, wait tRCD - 1 cycle
48
`define ST_RDDATA1      5'd17           // READ
49
`define ST_RDDATA2      5'd18           // NOP, wait 2 cycles
50
`define ST_RDDATA3      5'd19           // NOP, ld=1, wait 4 cycles
51
`define ST_RDDATA4      5'd20           // NOP, ack=1, ld=0, wait 2 cycles
52
`define ST_RDDATA5      5'd21           // NOP, ack=0, wait 3 cycles
53
`define ST_RDINST0      5'd22           // NOP, wait tRCD - 1 cycle
54
`define ST_RDINST1      5'd23           // READ
55
`define ST_RDINST2      5'd24           // NOP, wait 2 cycles
56
`define ST_RDINST3      5'd25           // NOP, ld=1, wait 4 cycles
57
`define ST_RDINST4      5'd26           // NOP, ack=1, ld=0, wait 2 cycles
58
`define ST_RDINST5      5'd27           // NOP, ack=0, wait 3 cycles
59
`define ST_ILLDA0       5'd28           // NOP, data_timeout=1, wait 2 cycles
60
`define ST_ILLDA1       5'd29           // NOP, data_timeout=0
61
`define ST_ILLIA0       5'd30           // NOP, inst_timeout=1, wait 2 cycles
62
`define ST_ILLIA1       5'd31           // NOP, inst_timeout=0
63
 
64
`define T_INIT0         14'd10000       // min 100 usec with CKE = 0
65
`define T_INIT1         14'd10000       // min 100 usec with CKE = 1
66
`define T_RP            14'd3           // min 20 ns row precharge time
67
`define T_RFC           14'd9           // min 66 ns auto refresh period
68
`define T_MRD           14'd3           // min load mode register delay
69
`define T_RCD           14'd3           // min 20 ns active-to-rw delay
70
 
71
`define REFCNT          10'd780          // 8192 refresh cycles per 64 ms
72
 
73
 
74
module ramctrl(clk_ok, clk,
75
               inst_stb, inst_addr,
76
               inst_dout, inst_ack,
77
               inst_timeout,
78
               data_stb, data_we,
79
               data_addr, data_din,
80
               data_dout, data_ack,
81
               data_timeout);
82
    input clk_ok;
83
    input clk;
84
    input inst_stb;
85
    input [25:0] inst_addr;
86
    output [63:0] inst_dout;
87
    output reg inst_ack;
88
    output reg inst_timeout;
89
    input data_stb;
90
    input data_we;
91
    input [25:0] data_addr;
92
    input [63:0] data_din;
93
    output [63:0] data_dout;
94
    output reg data_ack;
95
    output reg data_timeout;
96
 
97
  wire sdram_clk;
98
  wire sdram_clk_aux;
99
  reg sdram_cke;
100
  reg sdram_cs_n;
101
  wire sdram_ras_n;
102
  wire sdram_cas_n;
103
  wire sdram_we_n;
104
  reg [1:0] sdram_ba;
105
  reg [12:0] sdram_a;
106
  wire [1:0] sdram_dqm;
107
  wire [15:0] sdram_dq;
108
 
109
  reg [1:0] ram_cnt;
110
  wire [15:0] ram_dout;
111
  reg ram_de;
112
  reg [63:0] data;
113
  reg data_ld;
114
 
115
  wire inst_addr_out_of_range;
116
  wire data_addr_out_of_range;
117
 
118
  reg [2:0] ram_cmd;
119
  reg [1:0] ram_dqm;
120
  reg [13:0] count;
121
  reg [4:0] state;
122
 
123
  reg [9:0] refcnt;
124
  reg refflg;
125
  reg refrst;
126
 
127
  //
128
  // create ram instance
129
  //
130
 
131
  sdram sdram_1(
132
    .clk(sdram_clk),
133
    .cke(sdram_cke),
134
    .csb(sdram_cs_n),
135
    .rasb(sdram_ras_n),
136
    .casb(sdram_cas_n),
137
    .web(sdram_we_n),
138
    .ba(sdram_ba[1:0]),
139
    .ad(sdram_a[12:0]),
140
    .dqm(sdram_dqm[1:0]),
141
    .dqi(sdram_dq[15:0])
142
  );
143
 
144
  //
145
  // clock output to ram
146
  // the necessary phase shift will be accomplished by
147
  // a DLL if the controller is implemented on an FPGA
148
  //
149
 
150
  not #4 not_1(sdram_clk_aux, clk);
151
  not #5 not_2(sdram_clk, sdram_clk_aux);
152
 
153
  //
154
  // data output to ram
155
  //
156
 
157
  assign ram_dout[15:0] =
158
    ~ram_cnt[1] ? (~ram_cnt[0] ? data_din[63:48] : data_din[47:32]) :
159
                  (~ram_cnt[0] ? data_din[31:16] : data_din[15: 0]);
160
  assign sdram_dq[15:0] = ram_de ? ram_dout[15:0] : 16'hzzzz;
161
 
162
  //
163
  // data output to cache
164
  //
165
 
166
  always @(posedge clk) begin
167
    if (data_ld & (ram_cnt[1:0] == 2'b00)) begin
168
      data[63:48] <= sdram_dq[15:0];
169
    end
170
    if (data_ld & (ram_cnt[1:0] == 2'b01)) begin
171
      data[47:32] <= sdram_dq[15:0];
172
    end
173
    if (data_ld & (ram_cnt[1:0] == 2'b10)) begin
174
      data[31:16] <= sdram_dq[15:0];
175
    end
176
    if (data_ld & (ram_cnt[1:0] == 2'b11)) begin
177
      data[15: 0] <= sdram_dq[15:0];
178
    end
179
  end
180
 
181
  assign inst_dout[63:0] = data[63:0];
182
  assign data_dout[63:0] = data[63:0];
183
 
184
  //
185
  // address range check
186
  //
187
 
188
  assign inst_addr_out_of_range = | inst_addr[25:22];
189
  assign data_addr_out_of_range = | data_addr[25:22];
190
 
191
  //
192
  // ramctrl state machine
193
  //
194
 
195
  assign sdram_ras_n = ram_cmd[2];
196
  assign sdram_cas_n = ram_cmd[1];
197
  assign sdram_we_n = ram_cmd[0];
198
 
199
  assign sdram_dqm[1] = ram_dqm[1];
200
  assign sdram_dqm[0] = ram_dqm[0];
201
 
202
  always @(posedge clk or negedge clk_ok) begin
203
    // asynchronous reset
204
    if (~clk_ok) begin
205
      inst_ack <= 0;
206
      inst_timeout <= 0;
207
      data_ack <= 0;
208
      data_timeout <= 0;
209
      sdram_cke <= 0;
210
      sdram_cs_n <= 1;
211
      ram_cnt <= 0;
212
      ram_de <= 0;
213
      data_ld <= 0;
214
      ram_cmd <= `CMD_NOP;
215
      ram_dqm <= 2'b11;
216
      count <= `T_INIT0 - 1;
217
      state <= `ST_INIT0;
218
      refrst <= 0;
219
    end else begin
220
      if (|count[13:0]) begin
221
        // wait until count = 0
222
        // if count is loaded with N on a state transition, the
223
        // new state will last for (N+1)/fclk cycles before (!)
224
        // any action specified in the new state will take place
225
        count <= count - 1;
226
        // ram_cnt cycles through the 16-bit half-words in SDRAM
227
        // during burst read/writes while the main state machine
228
        // waits, thus it must be incremented here
229
        ram_cnt <= ram_cnt + 1;
230
      end else begin
231
        case (state)
232
          //----------------------------
233
          // init
234
          //----------------------------
235
          `ST_INIT0:
236
            begin
237
              sdram_cke <= 1;
238
              sdram_cs_n <= 0;
239
              ram_cmd <= `CMD_NOP;
240
              count <= `T_INIT1 - 1;
241
              state <= `ST_INIT1;
242
            end
243
          `ST_INIT1:
244
            begin
245
              ram_cmd <= `CMD_PRCHG;
246
              sdram_ba <= 2'b00;        // don't care
247
              sdram_a <= 13'h0400;      // precharge all
248
              state <= `ST_INIT2;
249
            end
250
          `ST_INIT2:
251
            begin
252
              ram_cmd <= `CMD_NOP;
253
              count <= `T_RP - 2;
254
              state <= `ST_INIT3;
255
            end
256
          `ST_INIT3:
257
            begin
258
              ram_cmd <= `CMD_ARFRS;
259
              state <= `ST_INIT4;
260
            end
261
          `ST_INIT4:
262
            begin
263
              ram_cmd <= `CMD_NOP;
264
              count <= `T_RFC - 2;
265
              state <= `ST_INIT5;
266
            end
267
          `ST_INIT5:
268
            begin
269
              ram_cmd <= `CMD_ARFRS;
270
              state <= `ST_INIT6;
271
            end
272
          `ST_INIT6:
273
            begin
274
              ram_cmd <= `CMD_NOP;
275
              count <= `T_RFC - 2;
276
              state <= `ST_INIT7;
277
            end
278
          `ST_INIT7:
279
            begin
280
              ram_cmd <= `CMD_MRSET;
281
              sdram_ba <= 2'b00;
282
              sdram_a <= `MODE;
283
              state <= `ST_INIT8;
284
            end
285
          `ST_INIT8:
286
            begin
287
              ram_cmd <= `CMD_NOP;
288
              ram_dqm <= 2'b00;
289
              count <= `T_MRD - 2;
290
              state <= `ST_IDLE;
291
            end
292
          //----------------------------
293
          // idle
294
          //----------------------------
295
          `ST_IDLE:
296
            begin
297
              if (refflg) begin
298
                // refresh request
299
                ram_cmd <= `CMD_ARFRS;
300
                state <= `ST_RFRSH;
301
                refrst <= 1;
302
              end else begin
303
                if (data_stb) begin
304
                  if (data_addr_out_of_range) begin
305
                    // illegal data address
306
                    ram_cmd <= `CMD_NOP;
307
                    state <= `ST_ILLDA0;
308
                  end else begin
309
                    // data address is ok
310
                    if (data_we) begin
311
                      // data write request
312
                      ram_cmd <= `CMD_ACTV;
313
                      sdram_ba <= data_addr[21:20];
314
                      sdram_a <= data_addr[19:7];
315
                      state <= `ST_WRDATA0;
316
                    end else begin
317
                      // data read request
318
                      ram_cmd <= `CMD_ACTV;
319
                      sdram_ba <= data_addr[21:20];
320
                      sdram_a <= data_addr[19:7];
321
                      state <= `ST_RDDATA0;
322
                    end
323
                  end
324
                end else begin
325
                  if (inst_stb) begin
326
                    if (inst_addr_out_of_range) begin
327
                      // illegal inst address
328
                      ram_cmd <= `CMD_NOP;
329
                      state <= `ST_ILLIA0;
330
                    end else begin
331
                      // inst address is ok
332
                      // inst read request
333
                      ram_cmd <= `CMD_ACTV;
334
                      sdram_ba <= inst_addr[21:20];
335
                      sdram_a <= inst_addr[19:7];
336
                      state <= `ST_RDINST0;
337
                    end
338
                  end else begin
339
                    // no request
340
                    ram_cmd <= `CMD_NOP;
341
                    state <= `ST_IDLE;
342
                  end
343
                end
344
              end
345
            end
346
          //----------------------------
347
          // refresh
348
          //----------------------------
349
          `ST_RFRSH:
350
            begin
351
              ram_cmd <= `CMD_NOP;
352
              count <= `T_RFC - 2;
353
              state <= `ST_IDLE;
354
              refrst <= 0;
355
            end
356
          //----------------------------
357
          // write data
358
          //----------------------------
359
          `ST_WRDATA0:
360
            begin
361
              ram_cmd <= `CMD_NOP;
362
              count <= `T_RCD - 2;
363
              state <= `ST_WRDATA1;
364
            end
365
          `ST_WRDATA1:
366
            begin
367
              ram_cnt <= 0;
368
              ram_de <= 1;
369
              ram_cmd <= `CMD_WRITE;
370
              sdram_ba <= data_addr[21:20];
371
              sdram_a <= { 6'b0010, data_addr[6:0], 2'b00 };
372
              state <= `ST_WRDATA2;
373
            end
374
          `ST_WRDATA2:
375
            begin
376
              ram_cnt <= ram_cnt + 1;
377
              ram_cmd <= `CMD_NOP;
378
              count <= 2;
379
              state <= `ST_WRDATA3;
380
            end
381
          `ST_WRDATA3:
382
            begin
383
              data_ack <= 1;
384
              ram_de <= 0;
385
              ram_cmd <= `CMD_NOP;
386
              count <= 1;
387
              state <= `ST_WRDATA4;
388
            end
389
          `ST_WRDATA4:
390
            begin
391
              data_ack <= 0;
392
              ram_cmd <= `CMD_NOP;
393
              count <= 1;
394
              state <= `ST_IDLE;
395
            end
396
          //----------------------------
397
          // read data
398
          //----------------------------
399
          `ST_RDDATA0:
400
            begin
401
              ram_cmd <= `CMD_NOP;
402
              count <= `T_RCD - 2;
403
              state <= `ST_RDDATA1;
404
            end
405
          `ST_RDDATA1:
406
            begin
407
              ram_cmd <= `CMD_READ;
408
              sdram_ba <= data_addr[21:20];
409
              sdram_a <= { 6'b0010, data_addr[6:0], 2'b00 };
410
              state <= `ST_RDDATA2;
411
            end
412
          `ST_RDDATA2:
413
            begin
414
              ram_cmd <= `CMD_NOP;
415
              count <= 1;
416
              state <= `ST_RDDATA3;
417
            end
418
          `ST_RDDATA3:
419
            begin
420
              ram_cnt <= 0;
421
              data_ld <= 1;
422
              ram_cmd <= `CMD_NOP;
423
              count <= 3;
424
              state <= `ST_RDDATA4;
425
            end
426
          `ST_RDDATA4:
427
            begin
428
              data_ack <= 1;
429
              data_ld <= 0;
430
              ram_cmd <= `CMD_NOP;
431
              count <= 1;
432
              state <= `ST_RDDATA5;
433
            end
434
          `ST_RDDATA5:
435
            begin
436
              data_ack <= 0;
437
              ram_cmd <= `CMD_NOP;
438
              count <= 2;
439
              state <= `ST_IDLE;
440
            end
441
          //----------------------------
442
          // read inst
443
          //----------------------------
444
          `ST_RDINST0:
445
            begin
446
              ram_cmd <= `CMD_NOP;
447
              count <= `T_RCD - 2;
448
              state <= `ST_RDINST1;
449
            end
450
          `ST_RDINST1:
451
            begin
452
              ram_cmd <= `CMD_READ;
453
              sdram_ba <= inst_addr[21:20];
454
              sdram_a <= { 6'b0010, inst_addr[6:0], 2'b00 };
455
              state <= `ST_RDINST2;
456
            end
457
          `ST_RDINST2:
458
            begin
459
              ram_cmd <= `CMD_NOP;
460
              count <= 1;
461
              state <= `ST_RDINST3;
462
            end
463
          `ST_RDINST3:
464
            begin
465
              ram_cnt <= 0;
466
              data_ld <= 1;
467
              ram_cmd <= `CMD_NOP;
468
              count <= 3;
469
              state <= `ST_RDINST4;
470
            end
471
          `ST_RDINST4:
472
            begin
473
              inst_ack <= 1;
474
              data_ld <= 0;
475
              ram_cmd <= `CMD_NOP;
476
              count <= 1;
477
              state <= `ST_RDINST5;
478
            end
479
          `ST_RDINST5:
480
            begin
481
              inst_ack <= 0;
482
              ram_cmd <= `CMD_NOP;
483
              count <= 2;
484
              state <= `ST_IDLE;
485
            end
486
          //----------------------------
487
          // illegal data address
488
          //----------------------------
489
          `ST_ILLDA0:
490
            begin
491
              data_timeout <= 1;
492
              ram_cmd <= `CMD_NOP;
493
              count <= 1;
494
              state <= `ST_ILLDA1;
495
            end
496
          `ST_ILLDA1:
497
            begin
498
              data_timeout <= 0;
499
              ram_cmd <= `CMD_NOP;
500
              state <= `ST_IDLE;
501
            end
502
          //----------------------------
503
          // illegal inst address
504
          //----------------------------
505
          `ST_ILLIA0:
506
            begin
507
              inst_timeout <= 1;
508
              ram_cmd <= `CMD_NOP;
509
              count <= 1;
510
              state <= `ST_ILLIA1;
511
            end
512
          `ST_ILLIA1:
513
            begin
514
              inst_timeout <= 0;
515
              ram_cmd <= `CMD_NOP;
516
              state <= `ST_IDLE;
517
            end
518
          //----------------------------
519
          // not used
520
          //----------------------------
521
          default:
522
            begin
523
              inst_ack <= 0;
524
              inst_timeout <= 0;
525
              data_ack <= 0;
526
              data_timeout <= 0;
527
              sdram_cke <= 0;
528
              sdram_cs_n <= 1;
529
              ram_cnt <= 0;
530
              ram_de <= 0;
531
              data_ld <= 0;
532
              ram_cmd <= `CMD_NOP;
533
              ram_dqm <= 2'b11;
534
              count <= `T_INIT0 - 1;
535
              state <= `ST_INIT0;
536
              refrst <= 0;
537
            end
538
        endcase
539
      end
540
    end
541
  end
542
 
543
  //
544
  // refresh counter
545
  //
546
 
547
  always @(posedge clk or negedge clk_ok) begin
548
    if (~clk_ok) begin
549
      refcnt <= 10'd0;
550
    end else begin
551
      if (refcnt == 10'd0) begin
552
        refcnt <= `REFCNT;
553
        refflg <= 1;
554
      end else begin
555
        refcnt <= refcnt - 1;
556
        if (refrst) begin
557
          refflg <= 0;
558
        end
559
      end
560
    end
561
  end
562
 
563
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.