This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdram.v
1163 lines (1081 loc) · 31.7 KB
/
sdram.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
//
// Filename: https://github.com/ZipCPU/arrowzip/blob/master/rtl/arrowzip/wbsdram.v
// {{{
// Project: ArrowZip, a demonstration of the Arrow MAX1000 FPGA board
//
// Purpose: Provide 32-bit wishbone access to the SDRAM memory on a MAX1000
// board. Specifically, on each access, the controller will
// activate an appropriate bank of RAM (the SDRAM has four banks), and
// then issue the read/write command. In the case of walking off the
// bank, the controller will activate the next bank before you get to it.
// Upon concluding any wishbone access, all banks will be precharged and
// returned to idle.
//
// This particular implementation represents a second generation version
// because my first version was too complex. To speed things up, this
// version includes an extra wait state where the wishbone inputs are
// clocked into a flip flop before any action is taken on them.
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2015-2021, Gisselquist Technology, LLC
// {{{
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
// target there if the PDF file isn't present.) If not, see
// <http://www.gnu.org/licenses/> for a copy.
// }}}
// License: GPL, v3, as defined and found on www.gnu.org,
// {{{
// http://www.gnu.org/licenses/gpl.html
//
////////////////////////////////////////////////////////////////////////////////
//
//`default_nettype none
// }}}
`define DMOD_GETINPUT 1'b0
`define DMOD_PUTOUTPUT 1'b1
`define RAM_OPERATIONAL 2'b00
`define RAM_POWER_UP 2'b01
`define RAM_SET_MODE 2'b10
`define RAM_INITIAL_REFRESH 2'b11
// }}}
module wbsdram #(
// {{{
parameter RDLY = 6,
parameter NCA=8, NRA=12, AW=(NCA+NRA+2)-1, DW=32,
parameter [NCA-2:0] COL_THRESHOLD = -16
// }}}
) (
// {{{
input wire i_clk,
// Wishbone
// {{{
// inputs
input wire i_wb_cyc, i_wb_stb, i_wb_we,
input wire [(AW-1):0] i_wb_addr,
input wire [(DW-1):0] i_wb_data,
input wire [(DW/8-1):0] i_wb_sel,
// outputs
output reg o_wb_stall,
output wire o_wb_ack,
output wire [31:0] o_wb_data,
// }}}
// SDRAM control
output reg o_ram_cs_n,
output wire o_ram_cke,
output reg o_ram_ras_n, o_ram_cas_n, o_ram_we_n,
output reg [1:0] o_ram_bs,
output reg [11:0] o_ram_addr,
output reg o_ram_dmod,
input [15:0] i_ram_data,
output reg [15:0] o_ram_data,
output reg [1:0] o_ram_dqm,
output wire [(DW-1):0] o_debug
// }}}
);
// Local declarations
// {{{
reg need_refresh;
reg [9:0] refresh_clk;
wire refresh_cmd;
reg in_refresh;
reg [2:0] in_refresh_clk;
reg [2:0] bank_active [0:3];
reg [(RDLY-1):0] r_barrell_ack;
reg r_pending;
reg r_we;
reg [(AW-1):0] r_addr;
reg [31:0] r_data;
reg [3:0] r_sel;
reg [(AW-NCA-2):0] bank_row [0:3];
reg [2:0] clocks_til_idle;
reg [1:0] m_state;
wire bus_cyc;
reg nxt_dmod;
wire pending;
reg [(AW-1):0] fwd_addr;
wire [1:0] wb_bs, r_bs, fwd_bs; // Bank select
wire [NRA-1:0] wb_row, r_row, fwd_row;
reg r_bank_valid;
reg fwd_bank_valid;
reg maintenance_mode;
reg m_ram_cs_n, m_ram_ras_n, m_ram_cas_n, m_ram_we_n, m_ram_dmod;
reg [(NRA-1):0] m_ram_addr;
reg startup_hold;
reg [15:0] startup_idle;
reg [3:0] maintenance_clocks;
reg maintenance_clocks_zero;
reg [15:0] last_ram_data;
// }}}
// Calculate some metrics
// {{{
//
// First, do we *need* a refresh now --- i.e., must we break out of
// whatever we are doing to issue a refresh command?
//
// The step size here must be such that 8192 charges may be done in
// 64 ms. Thus for a clock of:
// ClkRate(MHz) (64ms/1000(ms/s)*ClkRate)/8192
// 100 MHz 781
// 96 MHz 750
// 92 MHz 718
// 88 MHz 687
// 84 MHz 656
// 80 MHz 625
// 50 MHz 390
//
// However, since we do two refresh cycles everytime we need a refresh,
// this standard is close to overkill--but we'll use it anyway. At
// some later time we should address this, once we are entirely
// convinced that the memory is otherwise working without failure. Of
// course, at that time, it may no longer be a priority ...
// // }}}
////////////////////////////////////////////////////////////////////////
//
// Clock counting to know when to refresh
// {{{
////////////////////////////////////////////////////////////////////////
//
assign refresh_cmd = (!o_ram_cs_n)&&(!o_ram_ras_n)&&(!o_ram_cas_n)&&(o_ram_we_n);
// refresh_clk
// {{{
initial refresh_clk = 0;
always @(posedge i_clk)
begin
if (refresh_cmd)
refresh_clk <= 10'd390; // Make suitable for 50 MHz clk
else if (|refresh_clk)
refresh_clk <= refresh_clk - 10'h1;
end
// }}}
// need_refresh
// {{{
initial need_refresh = 1'b0;
always @(posedge i_clk)
need_refresh <= (refresh_clk == 10'h00)&&(!refresh_cmd);
// }}}
// in_refresh_clk
// {{{
initial in_refresh_clk = 3'h0;
always @(posedge i_clk)
if (refresh_cmd)
in_refresh_clk <= 3'h6;
else if (|in_refresh_clk)
in_refresh_clk <= in_refresh_clk - 3'h1;
// }}}
// in_refresh
// {{{
initial in_refresh = 0;
always @(posedge i_clk)
in_refresh <= (in_refresh_clk != 3'h0)||(refresh_cmd);
`ifdef FORMAL
always @(posedge i_clk)
if (in_refresh)
assert((refresh_cmd)||($past(in_refresh_clk) <= 3'h6));
always @(posedge i_clk)
if (in_refresh)
assert(refresh_clk==10'd619+{{(7){1'b0}},in_refresh_clk});
`endif
// }}}
//
// Second, do we *need* a precharge now --- must be break out of
// whatever we are doing to issue a precharge command?
//
// Keep in mind, the number of clocks to wait has to be reduced by
// the amount of time it may take us to go into a precharge state.
// You may also notice that the precharge requirement is tighter
// than this one, so ... perhaps this isn't as required?
//
// }}}
////////////////////////////////////////////////////////////////////////
//
// Incoming bus request handling and skidbuffer
// {{{
////////////////////////////////////////////////////////////////////////
//
//
assign bus_cyc = ((i_wb_cyc)&&(i_wb_stb)&&(!o_wb_stall));
// fwd_addr, r_*: Pre-process pending operations
// {{{
initial r_pending = 1'b0;
initial r_addr = 0;
initial fwd_addr = { {(AW-(NCA)){1'b0}}, 1'b1, {(NCA-1){1'b0}} };
always @(posedge i_clk)
begin
fwd_addr[NCA-2:0] <= 0;
if (bus_cyc)
begin
r_pending <= 1'b1;
r_we <= i_wb_we;
r_addr <= i_wb_addr;
r_data <= i_wb_data;
r_sel <= i_wb_sel;
fwd_addr[AW-1:NCA-1]<=i_wb_addr[(AW-1):(NCA-1)] + 1'b1;
end else if ((!o_ram_cs_n)&&(o_ram_ras_n)&&(!o_ram_cas_n))
r_pending <= 1'b0;
else if (!i_wb_cyc)
r_pending <= 1'b0;
end
`ifdef FORMAL
always @(*)
assert(fwd_addr[AW-1:NCA-1] == r_addr[(AW-1):(NCA-1)] + 1'b1);
always @(*)
assert(fwd_addr[NCA-3:0] == 0);
`endif
// }}}
assign wb_bs = i_wb_addr[NCA:NCA-1];
assign r_bs = r_addr[NCA:NCA-1];
assign fwd_bs = fwd_addr[NCA:NCA-1];
assign wb_row = i_wb_addr[AW-1:NCA+1];
assign r_row = r_addr[AW-1:NCA+1];
assign fwd_row = fwd_addr[AW-1:NCA+1];
// r_bank_valid
// {{{
initial r_bank_valid = 1'b0;
always @(posedge i_clk)
if (bus_cyc)
r_bank_valid <=((bank_active[wb_bs][2])
&&(bank_row[wb_bs] == wb_row));
else
r_bank_valid <= ((bank_active[r_bs][2])
&&(bank_row[r_bs] == r_row));
// }}}
// fwd_bank_valid
// {{{
initial fwd_bank_valid = 0;
always @(posedge i_clk)
fwd_bank_valid <= ((bank_active[fwd_bs][2])
&&(bank_row[fwd_bs] == fwd_row));
// }}}
assign pending = (r_pending)&&(o_wb_stall);
// }}}
////////////////////////////////////////////////////////////////////////
//
// SDRAM protocol handling
// {{{
////////////////////////////////////////////////////////////////////////
//
// Address MAP:
// {{{
// 22-bits bits in, 23-bits out
//
// 22 1111 1111 1100 0000 0000
// 10 9876 5432 1098 7654 3210
// rr rrrr rrrr rrBB cccc cccc 0
// 8765 4321 0
// }}}
// Monster state machine
// {{{
initial r_barrell_ack = 0;
initial clocks_til_idle = 3'h0;
initial o_wb_stall = 1'b1;
initial o_ram_dmod = `DMOD_GETINPUT;
initial nxt_dmod = `DMOD_GETINPUT;
initial o_ram_cs_n = 1'b0;
initial o_ram_ras_n = 1'b1;
initial o_ram_cas_n = 1'b1;
initial o_ram_we_n = 1'b1;
initial o_ram_dqm = 2'b11;
assign o_ram_cke = 1'b1;
initial bank_active[0] = 3'b000;
initial bank_active[1] = 3'b000;
initial bank_active[2] = 3'b000;
initial bank_active[3] = 3'b000;
always @(posedge i_clk)
if (maintenance_mode)
begin
// {{{
bank_active[0] <= 0;
bank_active[1] <= 0;
bank_active[2] <= 0;
bank_active[3] <= 0;
r_barrell_ack[(RDLY-1):0] <= 0;
o_wb_stall <= 1'b1;
//
o_ram_cs_n <= m_ram_cs_n;
o_ram_ras_n <= m_ram_ras_n;
o_ram_cas_n <= m_ram_cas_n;
o_ram_we_n <= m_ram_we_n;
o_ram_dmod <= m_ram_dmod;
o_ram_addr <= m_ram_addr;
o_ram_bs <= 2'b00;
nxt_dmod <= `DMOD_GETINPUT;
// }}}
end else begin
o_wb_stall <= (r_pending)||(bus_cyc);
if (!i_wb_cyc)
r_barrell_ack <= 0;
else
r_barrell_ack <= r_barrell_ack >> 1;
nxt_dmod <= `DMOD_GETINPUT;
o_ram_dmod <= nxt_dmod;
// default bank_active
// {{{
// We assume that, whatever state the bank is in, that it
// continues in that state and set up a series of shift
// registers to contain that information. If it will not
// continue in that state, all that therefore needs to be
// done is to set bank_active[?][2] below.
//
bank_active[0] <= { bank_active[0][2], bank_active[0][2:1] };
bank_active[1] <= { bank_active[1][2], bank_active[1][2:1] };
bank_active[2] <= { bank_active[2][2], bank_active[2][2:1] };
bank_active[3] <= { bank_active[3][2], bank_active[3][2:1] };
// }}}
o_ram_cs_n <= (!i_wb_cyc);
// o_ram_cke <= 1'b1;
if (|clocks_til_idle[2:0])
clocks_til_idle[2:0] <= clocks_til_idle[2:0] - 3'h1;
// Default command is a
// NOOP if (i_wb_cyc)
// Device deselect if (!i_wb_cyc)
// o_ram_cs_n <= (!i_wb_cyc) above, NOOP
o_ram_ras_n <= 1'b1;
o_ram_cas_n <= 1'b1;
o_ram_we_n <= 1'b1;
// o_ram_data <= r_data[15:0];
if (nxt_dmod)
;
else
if ((!i_wb_cyc)||(need_refresh))
begin // Issue a precharge all command (if any banks are open),
// otherwise an autorefresh command
if ((bank_active[0][2:1]==2'b10)
||(bank_active[1][2:1]==2'b10)
||(bank_active[2][2:1]==2'b10)
||(bank_active[3][2:1]==2'b10)
||(|clocks_til_idle[2:0]))
begin
// Do nothing this clock
// Can't precharge a bank immediately after
// activating it
end else if (bank_active[0][2]
||(bank_active[1][2])
||(bank_active[2][2])
||(bank_active[3][2]))
begin // Close all active banks
// {{{
o_ram_cs_n <= 1'b0;
o_ram_ras_n <= 1'b0;
o_ram_cas_n <= 1'b1;
o_ram_we_n <= 1'b0;
o_ram_addr[10] <= 1'b1;
bank_active[0][2] <= 1'b0;
bank_active[1][2] <= 1'b0;
bank_active[2][2] <= 1'b0;
bank_active[3][2] <= 1'b0;
// }}}
end else if ((|bank_active[0])
||(|bank_active[1])
||(|bank_active[2])
||(|bank_active[3]))
// Can't precharge yet, the bus is still busy
begin end else if ((!in_refresh)&&((refresh_clk[9:8]==2'b00)||(need_refresh)))
begin // Send autorefresh command
// {{{
o_ram_cs_n <= 1'b0;
o_ram_ras_n <= 1'b0;
o_ram_cas_n <= 1'b0;
o_ram_we_n <= 1'b1;
// }}}
end // Else just send NOOP's, the default command
end else if (in_refresh)
begin
// NOOPS only here, until we are out of refresh
end else if ((pending)&&(!r_bank_valid)&&(bank_active[r_bs]==3'h0))
begin // Need to activate the requested bank
// {{{
o_ram_cs_n <= 1'b0;
o_ram_ras_n <= 1'b0;
o_ram_cas_n <= 1'b1;
o_ram_we_n <= 1'b1;
o_ram_addr <= r_row;
o_ram_bs <= r_bs;
// clocks_til_idle[2:0] <= 1;
bank_active[r_bs][2] <= 1'b1;
bank_row[r_bs] <= r_row;
// }}}
end else if ((pending)&&(!r_bank_valid)
&&(bank_active[r_bs]==3'b111))
begin // Need to close an active bank
// {{{
o_ram_cs_n <= 1'b0;
o_ram_ras_n <= 1'b0;
o_ram_cas_n <= 1'b1;
o_ram_we_n <= 1'b0;
// o_ram_addr <= r_addr[(AW-1):(NCA+2)];
o_ram_addr[10]<= 1'b0;
o_ram_bs <= r_bs;
// clocks_til_idle[2:0] <= 1;
bank_active[r_bs][2] <= 1'b0;
// bank_row[r_bs] <= r_row;
// }}}
end else if ((pending)&&(!r_we)
&&(bank_active[r_bs][2])
&&(r_bank_valid)
&&(clocks_til_idle[2:0] < 4))
begin // Issue the read command
// {{{
o_ram_cs_n <= 1'b0;
o_ram_ras_n <= 1'b1;
o_ram_cas_n <= 1'b0;
o_ram_we_n <= 1'b1;
o_ram_addr <= { 4'h0, r_addr[NCA-2:0], 1'b0 };
o_ram_bs <= r_bs;
clocks_til_idle[2:0] <= 4;
o_wb_stall <= 1'b0;
r_barrell_ack[(RDLY-1)] <= 1'b1;
// }}}
end else if ((pending)&&(r_we)
&&(bank_active[r_bs][2])
&&(r_bank_valid)
&&(clocks_til_idle[2:0] == 0))
begin // Issue the write command
// {{{
o_ram_cs_n <= 1'b0;
o_ram_ras_n <= 1'b1;
o_ram_cas_n <= 1'b0;
o_ram_we_n <= 1'b0;
o_ram_addr <= { 4'h0, r_addr[NCA-2:0], 1'b0 };
o_ram_bs <= r_bs;
clocks_til_idle[2:0] <= 3'h1;
o_wb_stall <= 1'b0;
r_barrell_ack[1] <= 1'b1;
// o_ram_data <= r_data[31:16];
//
o_ram_dmod <= `DMOD_PUTOUTPUT;
nxt_dmod <= `DMOD_PUTOUTPUT;
// }}}
end else if ((r_pending)&&(r_addr[(NCA-2):0] >= COL_THRESHOLD)
&&(!fwd_bank_valid))
begin
// Do I need to close the next bank I'll need?
if (bank_active[fwd_bs][2:1]==2'b11)
begin // Need to close the bank first
// {{{
o_ram_cs_n <= 1'b0;
o_ram_ras_n <= 1'b0;
o_ram_cas_n <= 1'b1;
o_ram_we_n <= 1'b0;
o_ram_addr[10] <= 1'b0;
o_ram_bs <= fwd_bs;
bank_active[fwd_bs][2] <= 1'b0;
// }}}
end else if (bank_active[fwd_bs]==0)
begin
// Need to (pre-)activate the next bank
// {{{
o_ram_cs_n <= 1'b0;
o_ram_ras_n <= 1'b0;
o_ram_cas_n <= 1'b1;
o_ram_we_n <= 1'b1;
o_ram_addr <= fwd_row;
o_ram_bs <= fwd_bs;
// clocks_til_idle[3:0] <= 1;
bank_active[fwd_bs] <= 3'h4;
bank_row[fwd_bs] <= fwd_row;
// }}}
end
end
if (!i_wb_cyc)
r_barrell_ack <= 0;
end
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// Startup handling
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// startup_idle, startup_hold
// {{{
initial startup_idle = 16'd20500;
initial startup_hold = 1'b1;
always @(posedge i_clk)
if (|startup_idle)
startup_idle <= startup_idle - 1'b1;
always @(posedge i_clk)
startup_hold <= |startup_idle;
`ifdef FORMAL
always @(*)
if (startup_hold)
assert(maintenance_mode);
always @(*)
if (|startup_idle)
assert(startup_hold);
`endif
// }}}
// Maintenance state machine
// {{{
initial maintenance_mode = 1'b1;
initial maintenance_clocks = 4'hf;
initial maintenance_clocks_zero = 1'b0;
initial m_ram_addr = { 2'b00, 1'b0, 2'b00, 3'b010, 1'b0, 3'b001 };
initial m_state = `RAM_POWER_UP;
initial m_ram_cs_n = 1'b1;
initial m_ram_ras_n = 1'b1;
initial m_ram_cas_n = 1'b1;
initial m_ram_we_n = 1'b1;
initial m_ram_dmod = `DMOD_GETINPUT;
always @(posedge i_clk)
begin
if (!maintenance_clocks_zero)
begin
maintenance_clocks <= maintenance_clocks - 4'h1;
maintenance_clocks_zero <= (maintenance_clocks == 4'h1);
end
// The only time the RAM address matters is when we set
// the mode. At other times, addr[10] matters, but the rest
// is ignored. Hence ... we'll set it to a constant.
m_ram_addr <= { 2'b00, 1'b0, 2'b00, 3'b010, 1'b0, 3'b001 };
if (m_state == `RAM_POWER_UP)
begin
// {{{
// All signals must be held in NOOP state during powerup
// m_ram_cke <= 1'b1;
m_ram_cs_n <= 1'b1;
m_ram_ras_n <= 1'b1;
m_ram_cas_n <= 1'b1;
m_ram_we_n <= 1'b1;
m_ram_dmod <= `DMOD_GETINPUT;
if (!startup_hold)
begin
m_state <= `RAM_SET_MODE;
maintenance_clocks <= 4'h3;
maintenance_clocks_zero <= 1'b0;
// Precharge all cmd
m_ram_cs_n <= 1'b0;
m_ram_ras_n <= 1'b0;
m_ram_cas_n <= 1'b1;
m_ram_we_n <= 1'b0;
m_ram_addr[10] <= 1'b1;
end
// }}}
end else if (m_state == `RAM_SET_MODE)
begin
// {{{
// Wait
m_ram_cs_n <= 1'b1;
m_ram_cs_n <= 1'b1;
m_ram_ras_n <= 1'b1;
m_ram_cas_n <= 1'b1;
m_ram_we_n <= 1'b1;
m_ram_addr[10] <= 1'b1;
if (maintenance_clocks_zero)
begin
// Set mode cycle
m_ram_cs_n <= 1'b0;
m_ram_ras_n <= 1'b0;
m_ram_cas_n <= 1'b0;
m_ram_we_n <= 1'b0;
m_ram_dmod <= `DMOD_GETINPUT;
m_ram_addr[10] <= 1'b0;
m_state <= `RAM_INITIAL_REFRESH;
maintenance_clocks <= 4'hc;
maintenance_clocks_zero <= 1'b0;
end
// }}}
end else if (m_state == `RAM_INITIAL_REFRESH)
begin
// {{{
// Refresh command
if (maintenance_clocks > 4'ha)
// Wait two clocks first
m_ram_cs_n <= 1'b1;
else if (maintenance_clocks > 4'h2)
m_ram_cs_n <= 1'b0;
else
m_ram_cs_n <= 1'b1;
m_ram_ras_n <= 1'b0;
m_ram_cas_n <= 1'b0;
m_ram_we_n <= 1'b1;
m_ram_dmod <= `DMOD_GETINPUT;
// m_ram_addr <= { 3'b000, 1'b0, 2'b00, 3'b010, 1'b0, 3'b001 };
if (maintenance_clocks_zero)
maintenance_mode <= 1'b0;
// }}}
end
end
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// Bus return handling
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// o_ram_data
// {{{
always @(posedge i_clk)
if (nxt_dmod)
o_ram_data <= r_data[15:0];
else
o_ram_data <= r_data[31:16];
// }}}
// o_ram_dqm -- byte strobes
// {{{
always @(posedge i_clk)
if (maintenance_mode)
o_ram_dqm <= 2'b11;
else if (r_we)
begin
if (nxt_dmod)
o_ram_dqm <= ~r_sel[1:0];
else
o_ram_dqm <= ~r_sel[3:2];
end else
o_ram_dqm <= 2'b00;
// }}}
`ifdef VERILATOR
// {{{
// While I hate to build something that works one way under Verilator
// and another way in practice, this really isn't that. The problem
// \/erilator is having is resolved in toplevel.v---one file that
// \/erilator doesn't implement. In toplevel.v, there's not only a
// single clocked latch but two taking place. Here, we replicate one
// of those. The second takes place (somehow) within the sdramsim.cpp
// file.
reg [15:0] ram_data;
always @(posedge i_clk)
ram_data <= i_ram_data;
always @(posedge i_clk)
last_ram_data <= ram_data;
assign o_wb_data = { last_ram_data, ram_data };
// }}}
`else
always @(posedge i_clk)
last_ram_data <= i_ram_data;
assign o_wb_data = { last_ram_data, i_ram_data };
`endif
assign o_wb_ack = r_barrell_ack[0];
// }}}
////////////////////////////////////////////////////////////////////////
//
// Debugging bus
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// The following outputs are not necessary for the functionality of
// the SDRAM, but they can be used to feed an external "scope" to
// get an idea of what the internals of this SDRAM are doing.
//
// Just be aware of the r_we: it is set based upon the currently pending
// transaction, or (if none is pending) based upon the last transaction.
// If you want to capture the first value "written" to the device,
// you'll need to write a nothing value to the device to set r_we.
// The first value "written" to the device can be caught in the next
// interaction after that.
//
reg trigger;
always @(posedge i_clk)
trigger <= ((o_wb_data[15:0]==o_wb_data[31:16])
&&(o_wb_ack)&&(!i_wb_we));
assign o_debug = { i_wb_cyc,i_wb_stb,i_wb_we,o_wb_ack, o_wb_stall, // 5
o_ram_cs_n, o_ram_ras_n, o_ram_cas_n, o_ram_we_n, o_ram_bs,//6
o_ram_dmod, r_pending, // 2
trigger, // 1
o_ram_addr[9:0], // 10 more
(r_we) ? { o_ram_data[7:0] } // 8 values
: { o_wb_data[23:20], o_wb_data[3:0] }
// i_ram_data[7:0]
};
// }}}
// Make Verilator happy
// {{{
// verilator lint_off UNUSED
wire unused;
assign unused = &{ 1'b0, fwd_addr[NCA-1:0] };
// verilator lint_on UNUSED
// }}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal properties
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
localparam REFRESH_CLOCKS = 6;
localparam ACTIVATE_CLOCKS = 6;
reg f_past_valid;
wire [(5-1):0] f_nreqs, f_nacks, f_outstanding;
wire f_reset;
wire [(2+AW+DW+DW/8-1):0] f_pending, f_request;
wire [4:0] f_cmd;
reg [(AW-1):0] f_next_addr;
wire [NRA-1:0] f_next_row, f_this_row;
wire [1:0] f_next_bank, f_this_bank;
reg [3:0] f_acks_pending;
// This device is 23MB, assert such
always @(*)
assert(AW == 21);
always @(*)
assert(NRA+NCA+2 == AW+1);
always @(*)
if (o_ram_dmod)
assume(i_ram_data == o_ram_data);
initial f_past_valid = 1'b0;
always @(posedge i_clk)
f_past_valid <= 1'b1;
assign f_reset = !f_past_valid;
always @(*)
if (o_ram_dmod)
assert(i_ram_data == o_ram_data);
// Properties
// 1. Wishbone
fwb_slave #(
// {{{
.AW(AW), .DW(DW),
.F_MAX_STALL(ACTIVATE_CLOCKS + REFRESH_CLOCKS
+ ACTIVATE_CLOCKS + RDLY
+ACTIVATE_CLOCKS),
.F_MAX_ACK_DELAY(REFRESH_CLOCKS
+ ACTIVATE_CLOCKS
+ ACTIVATE_CLOCKS
+ ACTIVATE_CLOCKS+RDLY),
.F_LGDEPTH(5)
// }}}
) fwb(
// {{{
i_clk, f_reset,
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr,
i_wb_data, i_wb_sel,
o_wb_ack, o_wb_stall, o_wb_data, 1'b0,
f_nreqs, f_nacks, f_outstanding
// }}}
);
// 2. Proper startup ...
// 3. Operation
// 4. Refresh
// 4. SDRAM request == WB request
//
// Once we leave maintenance mode (i.e. startup sequence), we *cannot*
// go back into it.
always @(posedge i_clk)
if ((f_past_valid)&&(!$past(maintenance_mode)))
assert(!maintenance_mode);
// On the very first clock, we must always start up in maintenance mode
always @(posedge i_clk)
if (!f_past_valid)
assert(maintenance_mode);
// Just to make things simpler, assume no accesses to the core during
// maintenance mode. Such accesses might violate our minimum
// acknowledgement time criteria for the wishbone above
always @(posedge i_clk)
if ((f_past_valid)&&(maintenance_mode))
assume(!i_wb_stb);
// Likewise, assert that there are *NO* outstanding transactions in
// this maintenance mode
always @(posedge i_clk)
if ((f_past_valid)&&(maintenance_mode))
assert(f_outstanding == 0);
// ... and that while we are in maintenance mode, any incoming request
// is stalled. This guarantees that our assumptions above are kept
// valid.
always @(posedge i_clk)
if ((f_past_valid)&&(maintenance_mode))
assume(o_wb_stall);
// If there are no attempts to access memory while in maintenance
// mode, then there should never be any pending operations upon
// completion of maintenance mode
always @(posedge i_clk)
if ((f_past_valid)&&(maintenance_mode))
assert(!r_pending);
assign f_pending = { r_pending, r_we, r_addr, r_data, r_sel };
assign f_request = { i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel };
always @(posedge i_clk)
if ((f_past_valid)&&($past(r_pending))&&($past(i_wb_cyc))
&&(($past(o_ram_cs_n))
||(!$past(o_ram_ras_n))
||($past(o_ram_cas_n))) )
assert($stable(f_pending));
assign f_cmd = { o_ram_addr[10],
o_ram_cs_n, o_ram_ras_n, o_ram_cas_n, o_ram_we_n };
`define F_MODE_SET 5'b?0000
`define F_BANK_PRECHARGE 5'b00010
`define F_PRECHARGE_ALL 5'b10010
`define F_BANK_ACTIVATE 5'b?0011
`define F_WRITE 5'b00100
`define F_READ 5'b00101
`define F_REFRESH 5'b?0001
`define F_NOOP 5'b?0111
`define F_BANK_ACTIVATE_S 4'b0011
`define F_REFRESH_S 4'b0001
`define F_NOOP_S 4'b0111
always @(*)
begin
f_next_addr = 0;
f_next_addr[(AW-1):NCA-1] = r_addr[(AW-1):NCA-1] + 1'b1;
end
assign f_next_row = f_next_addr[(AW-1):(NCA+1)];
assign f_next_bank = f_next_addr[NCA:NCA-1];
assign f_this_bank = r_bs;
assign f_this_row = r_row;
always @(*)
if (o_ram_cs_n==1'b0) casez(f_cmd)
`F_MODE_SET: begin end
`F_BANK_PRECHARGE: begin end
`F_PRECHARGE_ALL: begin end
`F_BANK_ACTIVATE: begin end
`F_WRITE: begin end
`F_READ: begin end
`F_REFRESH: begin end
default: assert(f_cmd[3:0] == `F_NOOP_S);
endcase
always @(posedge i_clk)
if ((f_past_valid)&&(!maintenance_mode))
casez(f_cmd)
`F_BANK_ACTIVATE: begin
// Can only activate de-activated banks
assert(bank_active[o_ram_bs][1:0] == 0);
// Need to activate the right bank
if (o_ram_bs == $past(f_this_bank))
assert($past(f_this_row)==o_ram_addr);
else if (o_ram_bs != 0)
begin
assert(o_ram_bs == $past(f_next_bank));
assert($past(f_this_row)==o_ram_addr);
end else begin
assert(o_ram_bs == $past(f_next_bank));
assert($past(f_next_row)==o_ram_addr);
end end
`F_BANK_PRECHARGE: begin
// Can only precharge (de-active) a fully active bank
assert(bank_active[o_ram_bs] == 3'b011);
end
`F_PRECHARGE_ALL: begin
// If pre-charging all, one of the banks must be active and in
// need of a pre-charge
assert(
(bank_active[0] == 3'b011)
||(bank_active[1] == 3'b011)
||(bank_active[2] == 3'b011)
||(bank_active[3] == 3'b011) );
end
`F_WRITE: begin
assert($past(r_we));
assert(bank_active[o_ram_bs] == 3'b111);
assert(bank_row[o_ram_bs] == $past(f_this_row));
assert(o_ram_bs == $past(f_this_bank));
assert(o_ram_addr[0] == 1'b0);
end
`F_READ: begin
assert(!$past(r_we));
assert(bank_active[o_ram_bs] == 3'b111);
assert(bank_row[o_ram_bs] == $past(f_this_row));
assert(o_ram_bs == $past(f_this_bank));
assert(o_ram_addr[0] == 1'b0);
end
`F_REFRESH: begin
// When giving a reset command, *all* banks must be inactive
assert( (bank_active[0] == 3'h0)
&&(bank_active[1] == 3'h0)
&&(bank_active[2] == 3'h0)
&&(bank_active[3] == 3'h0) );
end
default: assert((o_ram_cs_n)||(f_cmd[3:0] == `F_NOOP_S));
endcase
integer f_k;
always @(posedge i_clk)
if ((f_past_valid)&&(!$past(maintenance_mode)))
begin
for(f_k=0; f_k<4; f_k=f_k+1)
if (((f_cmd[3:0] != `F_BANK_ACTIVATE_S))
||(o_ram_bs != f_k[1:0]))
assert($stable(bank_row[f_k[1:0]]));
end
always @(posedge i_clk)
if ((f_past_valid)&&(!$past(maintenance_mode))
&&($past(f_cmd) != `F_READ)
&&($past(f_cmd) != `F_WRITE) )
begin
if (($past(r_pending))&&($past(i_wb_cyc)))
assert($stable(f_pending));
end
always @(posedge i_clk)
if ((f_past_valid)&&(!maintenance_mode))
if ((r_pending)&&(f_cmd != `F_READ)&&(f_cmd != `F_WRITE))
assert(o_wb_stall);
always @(posedge i_clk)
if ((f_past_valid)&&(!$past(maintenance_mode)))
casez($past(f_cmd))
`F_BANK_ACTIVATE: begin
assert(bank_active[$past(o_ram_bs)] == 3'b110);
assert(bank_row[$past(o_ram_bs)] == $past(o_ram_addr));
end
`F_BANK_PRECHARGE: begin
assert(bank_active[$past(o_ram_bs)] == 3'b001);
end
`F_PRECHARGE_ALL: begin
assert(bank_active[0][2] == 1'b0);
assert(bank_active[1][2] == 1'b0);
assert(bank_active[2][2] == 1'b0);
assert(bank_active[3][2] == 1'b0);
end
// `F_WRITE:
// `F_READ:
`F_REFRESH: begin
assert(r_barrell_ack == 0);
end