-
Notifications
You must be signed in to change notification settings - Fork 2
/
ddr.v
1617 lines (1435 loc) · 66.8 KB
/
ddr.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
`include "includes.v"
`timescale 1 ns / 1 ps
`default_nettype none
/***********************************************************************
This file is part of the ChipWhisperer Project. See www.newae.com for more
details, or the codebase at http://www.chipwhisperer.com
Built from fifo_top_husky.v and modified for Pro.
Copyright (c) 2022, NewAE Technology Inc. All rights reserved.
Author: Jean-Pierre Thibault <[email protected]>
chipwhisperer is free software: 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.
chipwhisperer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with chipwhisperer. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
module ddr (
input wire reset,
output wire postddr_fifo_empty,
//FIFO to USB Read Interface
input wire clk_usb,
input wire low_res, // if set, return just 8 bits per sample; if clear return all 12 bits per sample
input wire low_res_lsb, // useless except for testing: if set, return the 8 LSB bits when in low_res mode
input wire [16:0] stream_segment_threshold,
output reg stream_segment_available,
input wire fifo_read_fifoen,
output reg [7:0] fifo_read_data,
input wire fast_fifo_read_mode, // not to be confused with the ADC fast FIFO, this denote fast reading of the *slow* FIFO
input wire stream_mode, //1=Enable stream mode, 0=Normal
input wire no_underflow_errors, // disables flagging of *slow* FIFO underflow errors only
input wire [31:0] max_samples_i,
// ADC
input wire capture_go_adc,
input wire write_done_adc,
output wire preddr_adc_fifo_rd,
input wire [63:0] preddr_adc_fifo_dout,
input wire preddr_adc_fifo_empty,
// LA
input wire capture_go_la,
input wire write_done_la,
output wire preddr_la_fifo_rd,
input wire [63:0] preddr_la_fifo_dout,
input wire preddr_la_fifo_empty,
// trace
input wire capture_go_trace,
input wire write_done_trace,
output wire preddr_trace_fifo_rd,
input wire [63:0] preddr_trace_fifo_dout,
input wire preddr_trace_fifo_empty,
input wire single_write,
input wire single_read,
input wire [29:0] single_address,
input wire [63:0] single_write_data,
output reg [63:0] single_read_data,
output wire ddr_read_data_done,
output wire ddr_single_done,
output reg ddr_write_data_done,
input wire [29:0] ddr_la_start_address,
input wire [29:0] ddr_trace_start_address,
input wire ddr_start_la_read,
input wire ddr_start_trace_read,
input wire ddr_start_adc_read,
// DDR
output wire [15:0] ddr3_addr,
output wire [2:0] ddr3_ba,
output wire ddr3_cas_n,
output wire ddr3_ck_n,
output wire ddr3_ck_p,
output wire ddr3_cke,
output wire ddr3_ras_n,
output wire ddr3_reset_n,
output wire ddr3_we_n,
inout wire [7:0] ddr3_dq,
inout wire ddr3_dqs_n,
inout wire ddr3_dqs_p,
output wire ddr3_dm,
output wire ddr3_cs_n,
output wire ddr3_odt,
output wire [6:0] ddr3_stat,
output wire ddr_test_pass,
output wire ddr_test_fail,
input wire ddr_test_clear_fail,
input wire ddr_rwtest_en,
input wire use_ddr,
output wire [15:0] ddr_test_iteration,
output wire [7:0] ddr_test_errors,
output reg [31:0] ddr_read_read,
output reg [31:0] ddr_read_idle,
output reg [31:0] ddr_write_write,
output reg [31:0] ddr_write_idle,
output reg [15:0] ddr_max_read_stall_count,
output reg [15:0] ddr_max_write_stall_count,
input wire [11:0] temp_out,
output reg [2:0] ddr_state,
output reg fifo_overflow_ddr,
input wire arm_pulse_usb,
output wire postddr_fifo_overflow,
output reg postddr_fifo_underflow_masked,
output reg reading_too_soon_error,
output reg ddr_full_error,
input wire source_flushing,
output wire postddr_flush,
// debug only:
input wire preddr_adc_fifo_underflow,
input wire error_flag,
output reg [31:0] fifo_read_count,
output reg [31:0] fifo_read_count_error_freeze,
output wire postddr_fifo_rd,
// CW310-specific:
input wire ADC_clk_fbp,
input wire ADC_clk_fbn,
output wire ui_clk,
input wire tb_ui_clk
);
parameter pDDR_INC_ADDR = 30'd8;
parameter pDDR_MAX_ADDR = 30'h1FFF_FFF8;
parameter pMAX_UNDERFLOWS = 3;
localparam CMD_WRITE = 3'b000;
localparam CMD_READ = 3'b001;
// application interface to DDR3:
wire [29:0] app_addr;
wire [2:0] app_cmd;
wire app_en;
wire [31:0] app_wdf_data;
wire app_wdf_end;
wire app_wdf_wren;
wire [29:0] rwtest_app_addr;
wire [2:0] rwtest_app_cmd;
wire rwtest_app_en;
wire [31:0] rwtest_app_wdf_data;
wire rwtest_app_wdf_end;
wire rwtest_app_wdf_wren;
wire [29:0] main_app_addr;
reg [29:0] adc_app_addr;
reg [29:0] la_app_addr;
reg [29:0] trace_app_addr;
wire [2:0] main_app_cmd;
reg main_app_en;
wire [31:0] main_app_wdf_data;
wire [63:0] preddr_wdata;
reg main_app_wdf_end;
reg main_app_wdf_wren;
wire [31:0] single_app_wdf_data;
wire ui_clk_sync_rst; // TODO: do we need this output?
wire [31:0] app_rd_data;
reg [31:0] app_rd_data_r;
wire app_rd_data_end;
wire app_rd_data_valid;
wire app_rdy;
wire app_wdf_rdy;
wire [255:0] ddr3_ila_basic_w;
wire dbg_pi_phaselock_err;
wire dbg_pi_dqsfound_err;
wire dbg_wrlvl_err;
wire [1:0] dbg_rdlvl_err;
wire dbg_wrcal_err;
wire init_calib_complete;
wire [3:0] ddr_test_state;
wire ddr_test_comp_error;
wire ddr_test_comp_good;
wire [31:0] ddr_test_expected_data;
wire [29:0] ddr_test_verify_addr;
wire [31:0] ddr_test_lfsr;
assign dbg_pi_phaselock_err = ddr3_ila_basic_w[6];
assign dbg_pi_dqsfound_err = ddr3_ila_basic_w[9];
assign dbg_wrlvl_err = ddr3_ila_basic_w[3];
assign dbg_rdlvl_err = ddr3_ila_basic_w[15:14];
assign dbg_wrcal_err = ddr3_ila_basic_w[21];
assign ddr3_stat = {dbg_pi_phaselock_err,
dbg_pi_dqsfound_err,
dbg_wrlvl_err,
dbg_rdlvl_err,
dbg_wrcal_err,
init_calib_complete
};
assign app_addr = (ddr_rwtest_en)? rwtest_app_addr : main_app_addr;
assign app_cmd = (ddr_rwtest_en)? rwtest_app_cmd : main_app_cmd;
assign app_en = (ddr_rwtest_en)? rwtest_app_en : main_app_en;
assign app_wdf_end = (ddr_rwtest_en)? rwtest_app_wdf_end : main_app_wdf_end;
assign app_wdf_wren = (ddr_rwtest_en)? rwtest_app_wdf_wren : main_app_wdf_wren;
assign app_wdf_data = (ddr_rwtest_en)? rwtest_app_wdf_data :
(single_write_ui)?single_app_wdf_data : main_app_wdf_data;
assign main_app_addr = (source_select == pADC_SOURCE)? adc_app_addr :
(source_select == pLA_SOURCE)? la_app_addr :
trace_app_addr;
localparam pS_DDR_IDLE = 3'd0;
localparam pS_DDR_WRITE1 = 3'd1;
localparam pS_DDR_WRITE2 = 3'd2;
localparam pS_DDR_WAIT_READ = 3'd3;
localparam pS_DDR_READ1 = 3'd4;
localparam pS_DDR_READ2 = 3'd5;
localparam pS_DDR_WAIT_WRITE = 3'd6;
localparam pS_DDR_ADC_BYPASS = 3'd7;
reg [2:0] next_ddr_state, ddr_state_r; // TODO-later: trim size
// TODO: flag error if arm event occurs when we are not idle
assign ddr_read_data_done = ddr_reading && ( (source_select == pADC_SOURCE)? (adc_top_app_addr == main_app_addr) :
(source_select == pLA_SOURCE)? (la_top_app_addr == main_app_addr) :
(source_select == pTRACE_SOURCE)? (trace_top_app_addr == main_app_addr) : 1'b0 );
reg incr_app_address;
reg reset_app_address;
reg [29:0] adc_top_app_addr;
reg [29:0] la_top_app_addr;
reg [29:0] trace_top_app_addr;
reg ddr_writing_r;
wire ddr_writing = (ddr_state == pS_DDR_WAIT_WRITE) ||
(ddr_state == pS_DDR_WRITE1) ||
(ddr_state == pS_DDR_WRITE2);
wire ddr_reading = (ddr_state == pS_DDR_WAIT_READ) ||
(ddr_state == pS_DDR_READ1) ||
(ddr_state == pS_DDR_READ2);
assign main_app_cmd = (ddr_writing)? CMD_WRITE : CMD_READ;
assign main_app_wdf_data = (ddr_state == pS_DDR_WRITE1)? preddr_wdata[31:0] : preddr_wdata[63:32];
assign preddr_wdata = (source_select == pADC_SOURCE)? preddr_adc_fifo_dout :
(source_select == pLA_SOURCE)? preddr_la_fifo_dout :
preddr_trace_fifo_dout;
assign single_app_wdf_data = (ddr_state == pS_DDR_WRITE1)? single_write_data[31:0] : single_write_data[63:32];
reg preddr_fifo_rd_r;
wire single_write_ui;
wire single_write_ui_r;
wire single_read_ui;
wire single_read_ui_r;
reg single_write_done;
reg single_read_done;
assign ddr_single_done = single_write_done || single_read_done;
assign postddr_flush = pre_read_flush_usb;
reg read_started;
reg pre_read_flush;
reg [63:0] postddr_fifo_din;
wire postddr_fifo_full;
wire postddr_fifo_prog_full;
reg postddr_fifo_wr;
reg postddr_fifo_rd_slow;
wire postddr_fifo_rd_fast;
wire [63:0] postddr_fifo_dout;
wire postddr_fifo_underflow;
reg postddr_fifo_underflow_sticky;
reg [1:0] postddr_fifo_underflow_count;
reg first_read;
reg first_read_done;
reg read_update;
wire read_update_usb;
reg [31:0] write_count;
(* ASYNC_REG = "TRUE" *) reg [31:0] write_count_to_usb;
reg [5:0] write_cycle_count = 0;
reg [31:0] read_count;
reg slow_fifo_rd_slow;
wire slow_fifo_rd_fast;
reg [3:0] slow_read_count;
reg ddr_start_adc_read_pulsed;
reg ddr_start_la_read_pulsed;
reg ddr_start_trace_read_pulsed;
reg ddr_start_adc_read_r;
reg ddr_start_la_read_r;
reg ddr_start_trace_read_r;
wire start_read_ui_pulse;
wire start_adc_read_ui_pulse;
wire start_la_read_ui_pulse;
wire start_trace_read_ui_pulse;
reg start_adc_read_usb_pulse;
reg start_la_read_usb_pulse;
reg start_trace_read_usb_pulse;
wire first_read_pulse_ui;
reg postddr_fifo_rd_r;
reg postddr_effective_underflow;
reg postddr_effective_underflow_charged;
////////////////////////////
// Post-DDR FIFO read logic:
////////////////////////////
wire fresh_start_usb = arm_pulse_usb || start_adc_read_usb_pulse || start_la_read_usb_pulse || start_trace_read_usb_pulse;
wire fresh_start_ui;
always @(*) begin
if (stream_mode)
// TODO: is this still relevant?
postddr_fifo_underflow_masked = postddr_fifo_underflow && (read_count < max_samples_i) && ~no_underflow_errors;
else
// fifo_read_fifoen check catches a case where while postddr_fifo_underflow doesn't occur, the read is still too
// soon in that slow_read_count will get messed up
postddr_fifo_underflow_masked = ~no_underflow_errors && ( (postddr_fifo_underflow && (postddr_fifo_underflow_count == pMAX_UNDERFLOWS)) ||
(fifo_read_fifoen && postddr_fifo_empty && ~first_read_done) ||
postddr_effective_underflow );
end
always @(posedge clk_usb) begin
if (reset) begin
postddr_fifo_underflow_sticky <= 0;
postddr_fifo_underflow_count <= 0;
end
else begin
// Xilinx FIFO asserts "underflow" for a single cycle only:
if (fresh_start_usb)
postddr_fifo_underflow_sticky <= 0;
else if (postddr_fifo_underflow || postddr_effective_underflow)
postddr_fifo_underflow_sticky <= 1;
// SAM3U likes to read multiples of 4 bytes, so we don't flag an
// underflow unless we observe at least 3 underflow reads
if (fresh_start_usb)
postddr_fifo_underflow_count <= 0;
else if (postddr_fifo_underflow && postddr_fifo_underflow_count < pMAX_UNDERFLOWS)
postddr_fifo_underflow_count <= postddr_fifo_underflow_count + 1;
end
end
// for debug: count FIFO reads
always @(posedge clk_usb) begin
if (fresh_start_usb) begin
fifo_read_count <= 0;
fifo_read_count_error_freeze <= 0;
end
else if (postddr_fifo_rd) begin
fifo_read_count <= fifo_read_count + 1;
if (!error_flag)
fifo_read_count_error_freeze <= fifo_read_count_error_freeze + 1;
end
end
cdc_pulse U_read_update_cdc (
.reset_i (reset),
.src_clk (ui_clk),
.src_pulse (read_update),
.dst_clk (clk_usb),
.dst_pulse (read_update_usb)
);
cdc_pulse U_fresh_start_cdc (
.reset_i (reset),
.src_clk (clk_usb),
.src_pulse (fresh_start_usb),
.dst_clk (ui_clk),
.dst_pulse (fresh_start_ui)
);
wire postddr_fifo_empty_ui;
cdc_simple U_postddr_fifo_empty_cdc (
.reset (reset),
.clk (ui_clk),
.data_in (postddr_fifo_empty),
.data_out (postddr_fifo_empty_ui),
.data_out_r ()
);
wire pre_read_flush_usb;
wire pre_read_flush_usb_r;
cdc_simple U_pre_read_flush_cdc (
.reset (reset),
.clk (clk_usb),
.data_in (pre_read_flush),
.data_out (pre_read_flush_usb),
.data_out_r (pre_read_flush_usb_r)
);
// track how many FIFO entries (roughly) are available to be read; tricky because of two clock domains!
always @(posedge ui_clk) begin
if (fresh_start_ui) begin
write_count <= 0;
write_count_to_usb <= 0;
read_update <= 1'b0;
end
else begin
write_cycle_count <= write_cycle_count + 1;
if (postddr_fifo_wr)
// TODO/NOTE: in Husky, write_count and read_count counted actual ADC samples; now they could 64-bit words
// (i.e. 5.33 samples); this needs to be accounted for in Python
write_count <= write_count + 1;
if (write_cycle_count == 0) begin
read_update <= 1'b1;
write_count_to_usb <= write_count;
end
else
read_update <= 1'b0;
end
end
// TODO: still need this?
always @(posedge clk_usb) begin
if (arm_pulse_usb) begin
read_count <= 0;
stream_segment_available <= 1'b0;
end
else begin
if (postddr_fifo_rd)
read_count <= read_count + 1;
if (read_update_usb) begin
if (write_count_to_usb > read_count)
stream_segment_available <= ( (write_count_to_usb - read_count > stream_segment_threshold) || (write_count_to_usb >= max_samples_i) );
else
stream_segment_available <= 1'b0;
end
end
end
reg [3:0] postddr_fifo_dout_r;
always @(posedge clk_usb) begin
if (reset || fresh_start_usb) begin
slow_read_count <= 0;
slow_fifo_rd_slow <= 1'b0;
end
else if (fifo_read_fifoen || first_read) begin
if (actual_low_res) begin
if (slow_read_count < 15)
slow_read_count <= slow_read_count + 1;
else
slow_read_count <= 0;
if ((slow_read_count == 4) || (slow_read_count == 9) || (slow_read_count == 15)) begin
slow_fifo_rd_slow <= 1;
postddr_fifo_dout_r <= postddr_fifo_dout[3:0];
end
else
slow_fifo_rd_slow <= 0;
end
else begin // hi_res, return all 12 bits per sample
if (slow_read_count < 7) begin
slow_fifo_rd_slow <= 0;
slow_read_count <= slow_read_count + 1;
end
else begin
slow_fifo_rd_slow <= 1;
slow_read_count <= 0;
end
end
end
else
slow_fifo_rd_slow <= 1'b0;
end
reg actual_low_res;
always @(posedge clk_usb) begin
if (start_adc_read_usb_pulse)
actual_low_res <= low_res;
else if (start_la_read_usb_pulse || start_trace_read_usb_pulse)
actual_low_res <= 1'b0;
end
assign slow_fifo_rd_fast = fifo_read_fifoen && (actual_low_res ? (slow_read_count == 2) : ((slow_read_count == 3) || (slow_read_count == 8))); // TODO!
assign postddr_fifo_rd = ((source_flushing || pre_read_flush_usb) && ~postddr_fifo_empty) ||
((fast_fifo_read_mode)? slow_fifo_rd_fast : slow_fifo_rd_slow);
reg [7:0] fifo_read_data_pre;
always @(*) begin
if (postddr_fifo_underflow_sticky)
fifo_read_data_pre = 0;
else if (actual_low_res) begin
if (low_res_lsb)
case (slow_read_count)
0: fifo_read_data_pre = postddr_fifo_dout[52+:8];
1: fifo_read_data_pre = postddr_fifo_dout[40+:8];
2: fifo_read_data_pre = postddr_fifo_dout[28+:8];
3: fifo_read_data_pre = postddr_fifo_dout[16+:8];
4: fifo_read_data_pre = postddr_fifo_dout[ 4+:8];
5: fifo_read_data_pre = postddr_fifo_dout[56+:8];
6: fifo_read_data_pre = postddr_fifo_dout[44+:8];
7: fifo_read_data_pre = postddr_fifo_dout[32+:8];
8: fifo_read_data_pre = postddr_fifo_dout[20+:8];
9: fifo_read_data_pre = postddr_fifo_dout[ 8+:8];
10: fifo_read_data_pre = {postddr_fifo_dout_r[3:0], postddr_fifo_dout[60+:4]};
11: fifo_read_data_pre = postddr_fifo_dout[48+:8];
12: fifo_read_data_pre = postddr_fifo_dout[36+:8];
13: fifo_read_data_pre = postddr_fifo_dout[24+:8];
14: fifo_read_data_pre = postddr_fifo_dout[12+:8];
15: fifo_read_data_pre = postddr_fifo_dout[0+:8];
default: fifo_read_data_pre = 8'h00;
endcase
else
case (slow_read_count)
// TODO: not validated in simulation; will have to get validated on actual target
0: fifo_read_data_pre = postddr_fifo_dout[56+:8];
1: fifo_read_data_pre = postddr_fifo_dout[44+:8];
2: fifo_read_data_pre = postddr_fifo_dout[32+:8];
3: fifo_read_data_pre = postddr_fifo_dout[20+:8];
4: fifo_read_data_pre = postddr_fifo_dout[ 8+:8];
5: fifo_read_data_pre = {postddr_fifo_dout_r[3:0], postddr_fifo_dout[60+:4]};
6: fifo_read_data_pre = postddr_fifo_dout[48+:8];
7: fifo_read_data_pre = postddr_fifo_dout[36+:8];
8: fifo_read_data_pre = postddr_fifo_dout[32+:8];
9: fifo_read_data_pre = postddr_fifo_dout[12+:8];
10: fifo_read_data_pre = postddr_fifo_dout[ 0+:8];
11: fifo_read_data_pre = postddr_fifo_dout[52+:8];
12: fifo_read_data_pre = postddr_fifo_dout[40+:8];
13: fifo_read_data_pre = postddr_fifo_dout[28+:8];
14: fifo_read_data_pre = postddr_fifo_dout[16+:8];
15: fifo_read_data_pre = postddr_fifo_dout[4+:8];
default: fifo_read_data_pre = 8'h00;
endcase
end
else
fifo_read_data_pre = postddr_fifo_dout[(7-slow_read_count)*8 +: 8];
end
// Register the FIFO output to help meet timing.
// FIFO is first-word-fall-through, so the intent of "first_read" is to
// register the FIFO output as soon as it's available, prior to executing
// a FIFO read.
// Originally "first_read" was triggered by the FIFO being not empty, but
// in some cases the FIFO isn't empty when the read command is issued (the
// FIFO gets flushed in this case), so instead we explicitely look at
// read_started
wire read_started_usb;
cdc_simple U_read_started_cdc (
.reset (reset),
.clk (clk_usb),
.data_in (read_started),
.data_out (read_started_usb),
.data_out_r ()
);
always @(posedge clk_usb) begin
// if ~postddr_fifo_empty when one of those events occur, we don't
// want to clear first_read_done yet, because there are conditions
// where we may falsely set first_read (too early); by waiting instead
// for the postddr flush to be done, we avoid those scenarios.
if ( (fresh_start_usb && postddr_fifo_empty) ||
(pre_read_flush_usb_r && ~pre_read_flush_usb) ) begin
first_read <= 1'b0;
first_read_done <= 1'b0;
end
else if (first_read) begin
first_read <= 1'b0;
first_read_done <= 1'b1;
end
// Note that when bypassing DDR, we don't bother with the read_started logic; it's assumed that
// FIFOs are already empty prior to starting a read when DDR is bypassed . This may or may not
// hold, but that's ok since DDR bypass is just for debugging.
else if (!postddr_fifo_empty && (read_started_usb || ~use_ddr) && !first_read_done) begin
first_read <= 1'b1;
end
if (fifo_read_fifoen || first_read) fifo_read_data <= fifo_read_data_pre;
end
cdc_simple U_single_write_cdc (
.reset (reset),
.clk (ui_clk),
.data_in (single_write),
.data_out (single_write_ui),
.data_out_r (single_write_ui_r)
);
cdc_simple U_single_read_cdc (
.reset (reset),
.clk (ui_clk),
.data_in (single_read),
.data_out (single_read_ui),
.data_out_r (single_read_ui_r)
);
wire single_write_go = single_write_ui && ~single_write_ui_r;
wire single_read_go = single_read_ui && ~single_read_ui_r;
assign start_read_ui_pulse = start_adc_read_ui_pulse || start_la_read_ui_pulse || start_trace_read_ui_pulse;
cdc_pulse U_first_read_cdc (
.reset_i (reset),
.src_clk (clk_usb),
.src_pulse (first_read),
.dst_clk (ui_clk),
.dst_pulse (first_read_pulse_ui)
);
////////////
// DDR logic
////////////
// DDR FSM:
always @(*) begin
// defaults:
main_app_en = 1'b0;
main_app_wdf_wren = 1'b0;
main_app_wdf_end = 1'b0;
incr_app_address = 1'b0;
reset_app_address = 1'b0;
preddr_fifo_rd = 1'b0;
next_ddr_state = pS_DDR_IDLE;
single_write_done = 1'b0;
pre_read_flush = 1'b0;
case (ddr_state)
pS_DDR_IDLE: begin
if ((capture_go_any || start_read_ui_pulse || single_write_go || single_read_go) && ~ddr_rwtest_en) begin
if (~use_ddr)
next_ddr_state = pS_DDR_ADC_BYPASS;
else if (init_calib_complete) begin
if (single_write_ui)
next_ddr_state = pS_DDR_WAIT_WRITE;
else if (single_read_ui)
next_ddr_state = pS_DDR_WAIT_READ;
else if (start_read_ui_pulse) begin
reset_app_address = 1'b1;
next_ddr_state = pS_DDR_WAIT_READ;
end
else begin
reset_app_address = 1'b1;
next_ddr_state = pS_DDR_WAIT_WRITE;
end
end
end
else
next_ddr_state = pS_DDR_IDLE;
end
pS_DDR_ADC_BYPASS: begin
// TODO: re-word DDR bypass with all sources in mind, as per note in Husky/Pro
if (use_ddr)
next_ddr_state = pS_DDR_IDLE;
else begin
next_ddr_state = pS_DDR_ADC_BYPASS;
// extra checks here to throttle the reads and ensure we don't overflow the FIFO;
// it's ok if we don't read back-to-back.
if (~preddr_adc_fifo_empty && ~postddr_fifo_full && ~preddr_fifo_rd_r && ~postddr_fifo_wr)
preddr_fifo_rd = 1'b1;
end
end
pS_DDR_WAIT_WRITE: begin
// This is where we arbitrate between the different preddr sources.
// ddr_state_r check because ddr_write_data_done goes low one cycle after getting here:
if (ddr_write_data_done && ~single_write_ui && (ddr_state_r == pS_DDR_WAIT_WRITE)) begin
reset_app_address = 1'b1;
next_ddr_state = pS_DDR_IDLE;
end
else if (app_rdy && app_wdf_rdy) begin
if (single_write_ui)
next_ddr_state = pS_DDR_WRITE1;
else if (~preddr_all_fifo_empty_r && (ddr_state_r == pS_DDR_WAIT_WRITE)) begin // wait extra cycle for selection mux to switch
preddr_fifo_rd = 1'b1;
next_ddr_state = pS_DDR_WRITE1;
end
else
next_ddr_state = pS_DDR_WAIT_WRITE;
end
else
next_ddr_state = pS_DDR_WAIT_WRITE;
end
pS_DDR_WRITE1: begin
main_app_en = 1'b1;
main_app_wdf_wren = 1'b1;
main_app_wdf_end = 1'b0;
if (app_rdy && app_wdf_rdy)
next_ddr_state = pS_DDR_WRITE2;
else
next_ddr_state = pS_DDR_WRITE1;
end
pS_DDR_WRITE2: begin
main_app_en = 1'b0;
main_app_wdf_wren = 1'b1;
main_app_wdf_end = 1'b1;
if (app_wdf_rdy) begin
if (single_write_ui) begin
next_ddr_state = pS_DDR_IDLE;
single_write_done = 1'b1;
end
else if (ddr_write_data_done) begin
reset_app_address = 1'b1;
next_ddr_state = pS_DDR_IDLE;
end
else begin
incr_app_address = 1'b1;
if (app_rdy && ~preddr_chosen_fifo_empty) begin
preddr_fifo_rd = 1'b1;
next_ddr_state = pS_DDR_WRITE1;
end
else
next_ddr_state = pS_DDR_WAIT_WRITE;
end
end
else
next_ddr_state = pS_DDR_WRITE2;
end
pS_DDR_WAIT_READ: begin
main_app_en = 1'b0;
if (ddr_rwtest_en || capture_go_any || ddr_read_change)
next_ddr_state = pS_DDR_IDLE;
else if (~postddr_fifo_empty_ui && ~read_started && ~single_read_ui) begin
pre_read_flush = 1'b1;
next_ddr_state = pS_DDR_WAIT_READ;
end
else if (app_rdy && (~postddr_fifo_prog_full || single_read_ui))
next_ddr_state = pS_DDR_READ1;
else
next_ddr_state = pS_DDR_WAIT_READ;
end
pS_DDR_READ1: begin
main_app_en = 1'b1;
if (app_rdy) begin
incr_app_address = 1'b1;
next_ddr_state = pS_DDR_READ2;
end
else
next_ddr_state = pS_DDR_READ1;
end
pS_DDR_READ2: begin
main_app_en = 1'b0;
if (ddr_read_data_done || single_read_ui || capture_go_any)
next_ddr_state = pS_DDR_IDLE;
else if (app_rdy && ~postddr_fifo_prog_full)
next_ddr_state = pS_DDR_READ1;
else
next_ddr_state = pS_DDR_WAIT_READ;
end
default: begin
next_ddr_state = pS_DDR_IDLE;
end
endcase
end
// NEW:
reg preddr_all_fifo_empty_r;
reg [1:0] source_select;
reg active_source_adc;
reg active_source_la;
reg active_source_trace;
wire [2:0] active_sources = {active_source_trace, active_source_la, active_source_adc};
localparam pADC_SOURCE = 2'b00;
localparam pLA_SOURCE = 2'b01;
localparam pTRACE_SOURCE = 2'b10;
wire preddr_all_fifo_empty = preddr_adc_fifo_empty &&
preddr_la_fifo_empty &&
preddr_trace_fifo_empty;
wire preddr_chosen_fifo_empty = (source_select == pADC_SOURCE)? preddr_adc_fifo_empty :
(source_select == pLA_SOURCE)? preddr_la_fifo_empty :
(source_select == pTRACE_SOURCE)? preddr_trace_fifo_empty : 1'b1;
reg preddr_fifo_rd;
assign preddr_adc_fifo_rd = preddr_fifo_rd && (source_select == pADC_SOURCE);
assign preddr_la_fifo_rd = preddr_fifo_rd && (source_select == pLA_SOURCE);
assign preddr_trace_fifo_rd = preddr_fifo_rd && (source_select == pTRACE_SOURCE);
wire [63:0] preddr_fifo_dout = (source_select == pADC_SOURCE)? preddr_adc_fifo_dout :
(source_select == pLA_SOURCE)? preddr_la_fifo_dout :
preddr_la_fifo_dout;
reg capture_go_any;
wire [2:0] ddr_active_read = {ddr_start_trace_read_ui, ddr_start_la_read_ui, ddr_start_adc_read_ui};
reg [2:0] ddr_active_read_r;
reg ddr_read_change;
wire ddr_start_adc_read_ui;
wire ddr_start_la_read_ui;
wire ddr_start_trace_read_ui;
cdc_simple U_ddr_start_adc_read_cdc (
.reset (reset),
.clk (ui_clk),
.data_in (ddr_start_adc_read),
.data_out (ddr_start_adc_read_ui),
.data_out_r ()
);
cdc_simple U_ddr_start_la_read_cdc (
.reset (reset),
.clk (ui_clk),
.data_in (ddr_start_la_read),
.data_out (ddr_start_la_read_ui),
.data_out_r ()
);
cdc_simple U_ddr_start_trace_read_cdc (
.reset (reset),
.clk (ui_clk),
.data_in (ddr_start_trace_read),
.data_out (ddr_start_trace_read_ui),
.data_out_r ()
);
always @(posedge ui_clk) begin
if (reset) begin
ddr_read_change <= 1'b0;
ddr_active_read_r <= 0;
end
else begin
ddr_active_read_r <= ddr_active_read;
if (ddr_active_read != ddr_active_read_r)
ddr_read_change <= 1'b1;
else if (ddr_state == pS_DDR_IDLE)
ddr_read_change <= 1'b0;
end
end
`ifdef __ICARUS__
// to help debug
reg [31:0] postddr_fifo_writes = 0;
reg [31:0] postddr_fifo_reads = 0;
always @ (posedge ui_clk) if (postddr_fifo_wr) postddr_fifo_writes <= postddr_fifo_writes + 1;
always @ (posedge clk_usb) if (postddr_fifo_rd) postddr_fifo_reads <= postddr_fifo_reads + 1;
`endif
// DDR FSM sequential control logic and slow FIFO writes:
always @(posedge ui_clk) begin
if (reset) begin
ddr_state <= pS_DDR_IDLE;
ddr_state_r <= pS_DDR_IDLE;
adc_app_addr <= 0;
la_app_addr <= 0;
trace_app_addr <= 0;
ddr_full_error <= 0;
postddr_fifo_wr <= 0;
preddr_fifo_rd_r <= 0;
adc_top_app_addr <= 0;
la_top_app_addr <= 0;
trace_top_app_addr <= 0;
single_read_done <= 0;
preddr_all_fifo_empty_r <= 0;
source_select <= pADC_SOURCE;
active_source_adc <= 0;
active_source_la <= 0;
active_source_trace <= 0;
ddr_write_data_done <= 0;
ddr_writing_r <= 0;
read_started <= 1'b0;
capture_go_any <= 0;
end
else begin
ddr_state <= next_ddr_state;
ddr_state_r <= ddr_state;
preddr_fifo_rd_r <= preddr_fifo_rd;
preddr_all_fifo_empty_r <= preddr_all_fifo_empty;
ddr_writing_r <= ddr_writing;
single_read_done <= 0;
if (capture_go_any)
ddr_full_error <= 1'b0;
// Convention is trace start address > LA start address, and ADC start address is 0.
else if ( (adc_app_addr >= ddr_la_start_address) ||
(la_app_addr >= ddr_trace_start_address) ||
(incr_app_address && (main_app_addr >= pDDR_MAX_ADDR)) )
ddr_full_error <= 1'b1;
if (single_write_ui || single_read_ui)
adc_app_addr <= single_address;
else if (reset_app_address) begin
adc_app_addr <= 0;
la_app_addr <= ddr_la_start_address;
trace_app_addr <= ddr_trace_start_address;
/*
if (ddr_state != pS_DDR_IDLE) begin
if (ddr_state == pS_DDR_WAIT_WRITE)
adc_top_app_addr <= adc_app_addr;
else
adc_top_app_addr <= adc_app_addr + pDDR_INC_ADDR;
end
*/
end
else if (incr_app_address) begin
if (source_select == pADC_SOURCE)
adc_app_addr <= adc_app_addr + pDDR_INC_ADDR;
else if (source_select == pLA_SOURCE)
la_app_addr <= la_app_addr + pDDR_INC_ADDR;
else
trace_app_addr <= trace_app_addr + pDDR_INC_ADDR;
end
if (write_done_adc)
adc_top_app_addr <= adc_app_addr + pDDR_INC_ADDR;
if (write_done_la)
la_top_app_addr <= la_app_addr + pDDR_INC_ADDR;
if (write_done_trace)
trace_top_app_addr <= trace_app_addr + pDDR_INC_ADDR;
if (single_read_ui && app_rd_data_valid) begin
if (app_rd_data_end) begin
single_read_data[63:32] <= app_rd_data;
single_read_done <= 1;
end
else
single_read_data[31:0] <= app_rd_data;
end
if (ddr_rwtest_en || single_write_ui || single_read_ui)
postddr_fifo_wr <= 1'b0;
else if (use_ddr && app_rd_data_valid) begin
app_rd_data_r <= app_rd_data;
if (app_rd_data_end) begin
postddr_fifo_wr <= 1'b1;
postddr_fifo_din <= {app_rd_data, app_rd_data_r};
end
else
postddr_fifo_wr <= 1'b0;
end
else if (~use_ddr) begin
// in this case DDR is bypassed: data is read from preddr and
// immediately written to postddr
postddr_fifo_wr <= preddr_fifo_rd_r;
postddr_fifo_din <= preddr_fifo_dout;
end
else
postddr_fifo_wr <= 1'b0;
// arbitrate DDR writing between the 3 input sources:
// (very simple arbitration: assume that all sources go idle at
// some point, to allow others their turn)
if (single_write_go || single_read_go)
source_select <= pADC_SOURCE;
else if ((ddr_state == pS_DDR_WAIT_WRITE) && ~preddr_all_fifo_empty && ~preddr_fifo_rd) begin
if (~preddr_adc_fifo_empty)
source_select <= pADC_SOURCE;
else if (~preddr_la_fifo_empty)
source_select <= pLA_SOURCE;
else if (~preddr_trace_fifo_empty)
source_select <= pTRACE_SOURCE;
end
else if (ddr_state == pS_DDR_IDLE) begin
if (start_adc_read_ui_pulse)
source_select <= pADC_SOURCE;
else if (start_la_read_ui_pulse)
source_select <= pLA_SOURCE;
else if (start_trace_read_ui_pulse)
source_select <= pTRACE_SOURCE;
end
// Track active sources, to know when DDR FSM is done writing.
// write_done_* refers to whether the associated front-end has
// finished writing to its input FIFO, whereas ddr_write_data_done
// (below) refers to whether the data from all active front-ends
// has been written to DDR.
if (write_done_adc)
active_source_adc <= 1'b0;
else if (capture_go_adc)
active_source_adc <= 1'b1;
if (write_done_la)
active_source_la <= 1'b0;