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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
|
//======================================================================
//
// coretest.v
// ----------
// The Cryptech coretest testing module. Combined with an external
// interface that sends and receives bytes using a SYN-ACK
// handshake and a core to be tested, coretest can parse read
// and write commands needed to test the connected core.
//
//
// Author: Joachim Strombergson
// Copyright (c) 2014, NORDUnet A/S
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// - Neither the name of the NORDUnet nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//======================================================================
module coretest(
input wire clk,
input wire reset_n,
// Interface to communication core
input wire rx_syn,
input wire [7 : 0] rx_data,
output wire rx_ack,
output wire tx_syn,
output wire [7 : 0] tx_data,
input wire tx_ack,
// Interface to the core being tested.
output wire core_reset_n,
output wire core_cs,
output wire core_we,
output wire [15 : 0] core_address,
output wire [31 : 0] core_write_data,
input wire [31 : 0] core_read_data,
input wire core_error
);
//----------------------------------------------------------------
// Internal constant and parameter definitions.
//----------------------------------------------------------------
// Max elements in read and write buffers.
parameter BUFFER_MAX = 4'hf;
// Command constants.
parameter SOC = 8'h55;
parameter EOC = 8'haa;
parameter RESET_CMD = 8'h01;
parameter READ_CMD = 8'h10;
parameter WRITE_CMD = 8'h11;
// Response constants.
parameter SOR = 8'haa;
parameter EOR = 8'h55;
parameter UNKNOWN = 8'hfe;
parameter ERROR = 8'hfd;
parameter READ_OK = 8'h7f;
parameter WRITE_OK = 8'h7e;
parameter RESET_OK = 8'h7d;
// rx_engine states.
parameter RX_IDLE = 3'h0;
parameter RX_ACK = 3'h1;
parameter RX_NSYN = 3'h2;
// tx_engine states.
parameter TX_IDLE = 3'h0;
parameter TX_SYN = 3'h1;
parameter TX_NOACK = 3'h2;
parameter TX_NEXT = 3'h3;
parameter TX_SENT = 3'h4;
parameter TX_DONE = 3'h5;
// test_engine states.
parameter TEST_IDLE = 8'h00;
parameter TEST_GET_CMD = 8'h10;
parameter TEST_PARSE_CMD = 8'h11;
parameter TEST_GET_ADDR0 = 8'h20;
parameter TEST_GET_ADDR1 = 8'h21;
parameter TEST_GET_DATA0 = 8'h24;
parameter TEST_GET_DATA1 = 8'h25;
parameter TEST_GET_DATA2 = 8'h26;
parameter TEST_GET_DATA3 = 8'h27;
parameter TEST_GET_EOC = 8'h28;
parameter TEST_RST_START = 8'h30;
parameter TEST_RST_WAIT = 8'h31;
parameter TEST_RST_END = 8'h32;
parameter TEST_RD_START = 8'h50;
parameter TEST_RD_WAIT = 8'h51;
parameter TEST_RD_END = 8'h52;
parameter TEST_WR_START = 8'h60;
parameter TEST_WR_WAIT = 8'h61;
parameter TEST_WR_END = 8'h62;
parameter TEST_CMD_UNKNOWN = 8'h80;
parameter TEST_CMD_ERROR = 8'h81;
parameter TEST_SEND_RESPONSE = 8'hc0;
//----------------------------------------------------------------
// Registers including update variables and write enable.
//----------------------------------------------------------------
reg rx_syn_reg;
reg rx_ack_reg;
reg rx_ack_new;
reg rx_ack_we;
reg tx_syn_reg;
reg tx_syn_new;
reg tx_syn_we;
reg tx_ack_reg;
reg core_reset_n_reg;
reg core_reset_n_new;
reg core_reset_n_we;
reg core_cs_reg;
reg core_cs_new;
reg core_cs_we;
reg core_we_reg;
reg core_we_new;
reg core_we_we;
reg send_response_reg;
reg send_response_new;
reg send_response_we;
reg response_sent_reg;
reg response_sent_new;
reg response_sent_we;
reg [7 : 0] cmd_reg;
reg cmd_we;
reg [7 : 0] core_addr_byte0_reg;
reg core_addr_byte0_we;
reg [7 : 0] core_addr_byte1_reg;
reg core_addr_byte1_we;
reg [7 : 0] core_wr_data_byte0_reg;
reg core_wr_data_byte0_we;
reg [7 : 0] core_wr_data_byte1_reg;
reg core_wr_data_byte1_we;
reg [7 : 0] core_wr_data_byte2_reg;
reg core_wr_data_byte2_we;
reg [7 : 0] core_wr_data_byte3_reg;
reg core_wr_data_byte3_we;
reg [31 : 0] core_read_data_reg;
reg core_error_reg;
reg sample_core_output;
reg [3 : 0] rx_buffer_rd_ptr_reg;
reg [3 : 0] rx_buffer_rd_ptr_new;
reg rx_buffer_rd_ptr_we;
reg rx_buffer_rd_ptr_inc;
reg [3 : 0] rx_buffer_wr_ptr_reg;
reg [3 : 0] rx_buffer_wr_ptr_new;
reg rx_buffer_wr_ptr_we;
reg rx_buffer_wr_ptr_inc;
reg [3 : 0] rx_buffer_ctr_reg;
reg [3 : 0] rx_buffer_ctr_new;
reg rx_buffer_ctr_we;
reg rx_buffer_ctr_inc;
reg rx_buffer_ctr_dec;
reg rx_buffer_full;
reg rx_buffer_empty;
reg [7 : 0] rx_buffer [0 : 15];
reg rx_buffer_we;
reg [3 : 0] tx_buffer_ptr_reg;
reg [3 : 0] tx_buffer_ptr_new;
reg tx_buffer_ptr_we;
reg tx_buffer_ptr_inc;
reg tx_buffer_ptr_rst;
reg [7 : 0] tx_buffer [0 : 8];
reg tx_buffer_we;
reg [3 : 0] tx_msg_len_reg;
reg [3 : 0] tx_msg_len_new;
reg tx_msg_len_we;
reg [2 : 0] rx_engine_reg;
reg [2 : 0] rx_engine_new;
reg rx_engine_we;
reg [2 : 0] tx_engine_reg;
reg [2 : 0] tx_engine_new;
reg tx_engine_we;
reg [7 : 0] test_engine_reg;
reg [7 : 0] test_engine_new;
reg test_engine_we;
//----------------------------------------------------------------
// Wires.
//----------------------------------------------------------------
reg [7 : 0] tx_buffer_muxed0;
reg [7 : 0] tx_buffer_muxed1;
reg [7 : 0] tx_buffer_muxed2;
reg [7 : 0] tx_buffer_muxed3;
reg [7 : 0] tx_buffer_muxed4;
reg [7 : 0] tx_buffer_muxed5;
reg [7 : 0] tx_buffer_muxed6;
reg [7 : 0] tx_buffer_muxed7;
reg [7 : 0] tx_buffer_muxed8;
reg update_tx_buffer;
reg [7 : 0] response_type;
reg [7 : 0] rx_byte;
//----------------------------------------------------------------
// Concurrent connectivity for ports etc.
//----------------------------------------------------------------
assign rx_ack = rx_ack_reg;
assign tx_syn = tx_syn_reg;
assign tx_data = tx_buffer[tx_buffer_ptr_reg];
assign core_reset_n = core_reset_n_reg & reset_n;
assign core_cs = core_cs_reg;
assign core_we = core_we_reg;
assign core_address = {core_addr_byte0_reg, core_addr_byte1_reg};
assign core_write_data = {core_wr_data_byte0_reg, core_wr_data_byte1_reg,
core_wr_data_byte2_reg, core_wr_data_byte3_reg};
//----------------------------------------------------------------
// reg_update
// Update functionality for all registers in the core.
// All registers are positive edge triggered with synchronous
// active low reset. All registers have write enable.
//----------------------------------------------------------------
always @ (posedge clk)
begin: reg_update
if (!reset_n)
begin
rx_buffer[0] <= 8'h00;
rx_buffer[1] <= 8'h00;
rx_buffer[2] <= 8'h00;
rx_buffer[3] <= 8'h00;
rx_buffer[4] <= 8'h00;
rx_buffer[5] <= 8'h00;
rx_buffer[6] <= 8'h00;
rx_buffer[7] <= 8'h00;
rx_buffer[8] <= 8'h00;
rx_buffer[9] <= 8'h00;
rx_buffer[10] <= 8'h00;
rx_buffer[11] <= 8'h00;
rx_buffer[12] <= 8'h00;
rx_buffer[13] <= 8'h00;
rx_buffer[14] <= 8'h00;
rx_buffer[15] <= 8'h00;
tx_buffer[0] <= 8'h00;
tx_buffer[1] <= 8'h00;
tx_buffer[2] <= 8'h00;
tx_buffer[3] <= 8'h00;
tx_buffer[4] <= 8'h00;
tx_buffer[5] <= 8'h00;
tx_buffer[6] <= 8'h00;
tx_buffer[7] <= 8'h00;
tx_buffer[8] <= 8'h00;
rx_syn_reg <= 0;
rx_ack_reg <= 0;
tx_ack_reg <= 0;
tx_syn_reg <= 0;
rx_buffer_rd_ptr_reg <= 4'h0;
rx_buffer_wr_ptr_reg <= 4'h0;
rx_buffer_ctr_reg <= 4'h0;
tx_buffer_ptr_reg <= 4'h0;
tx_msg_len_reg <= 4'h0;
send_response_reg <= 0;
response_sent_reg <= 0;
cmd_reg <= 8'h00;
core_addr_byte0_reg <= 8'h00;
core_addr_byte1_reg <= 8'h00;
core_wr_data_byte0_reg <= 8'h00;
core_wr_data_byte1_reg <= 8'h00;
core_wr_data_byte2_reg <= 8'h00;
core_wr_data_byte3_reg <= 8'h00;
core_reset_n_reg <= 1;
core_cs_reg <= 0;
core_we_reg <= 0;
core_error_reg <= 0;
core_read_data_reg <= 32'h00000000;
rx_engine_reg <= RX_IDLE;
tx_engine_reg <= TX_IDLE;
test_engine_reg <= TEST_IDLE;
end
else
begin
rx_syn_reg <= rx_syn;
tx_ack_reg <= tx_ack;
if (rx_ack_we)
begin
rx_ack_reg <= rx_ack_new;
end
if (tx_syn_we)
begin
tx_syn_reg <= tx_syn_new;
end
if (rx_buffer_we)
begin
rx_buffer[rx_buffer_wr_ptr_reg] <= rx_data;
end
if (tx_buffer_we)
begin
tx_buffer[0] <= tx_buffer_muxed0;
tx_buffer[1] <= tx_buffer_muxed1;
tx_buffer[2] <= tx_buffer_muxed2;
tx_buffer[3] <= tx_buffer_muxed3;
tx_buffer[4] <= tx_buffer_muxed4;
tx_buffer[5] <= tx_buffer_muxed5;
tx_buffer[6] <= tx_buffer_muxed6;
tx_buffer[7] <= tx_buffer_muxed7;
tx_buffer[8] <= tx_buffer_muxed8;
end
if (cmd_we)
begin
cmd_reg <= rx_byte;
end
if (core_addr_byte0_we)
begin
core_addr_byte0_reg <= rx_byte;
end
if (core_addr_byte1_we)
begin
core_addr_byte1_reg <= rx_byte;
end
if (core_wr_data_byte0_we)
begin
core_wr_data_byte0_reg <= rx_byte;
end
if (core_wr_data_byte1_we)
begin
core_wr_data_byte1_reg <= rx_byte;
end
if (core_wr_data_byte2_we)
begin
core_wr_data_byte2_reg <= rx_byte;
end
if (core_wr_data_byte3_we)
begin
core_wr_data_byte3_reg <= rx_byte;
end
if (rx_buffer_rd_ptr_we)
begin
rx_buffer_rd_ptr_reg <= rx_buffer_rd_ptr_new;
end
if (rx_buffer_wr_ptr_we)
begin
rx_buffer_wr_ptr_reg <= rx_buffer_wr_ptr_new;
end
if (rx_buffer_ctr_we)
begin
rx_buffer_ctr_reg <= rx_buffer_ctr_new;
end
if (tx_buffer_ptr_we)
begin
tx_buffer_ptr_reg <= tx_buffer_ptr_new;
end
if (tx_msg_len_we)
begin
tx_msg_len_reg <= tx_msg_len_new;
end
if (core_reset_n_we)
begin
core_reset_n_reg <= core_reset_n_new;
end
if (core_cs_we)
begin
core_cs_reg <= core_cs_new;
end
if (core_we_we)
begin
core_we_reg <= core_we_new;
end
if (send_response_we)
begin
send_response_reg <= send_response_new;
end
if (response_sent_we)
begin
response_sent_reg <= response_sent_new;
end
if (sample_core_output)
begin
core_error_reg <= core_error;
core_read_data_reg <= core_read_data;
end
if (rx_engine_we)
begin
rx_engine_reg <= rx_engine_new;
end
if (tx_engine_we)
begin
tx_engine_reg <= tx_engine_new;
end
if (test_engine_we)
begin
test_engine_reg <= test_engine_new;
end
end
end // reg_update
//---------------------------------------------------------------
// read_rx_buffer
// Combinatinal read mux for the rx_buffer.
//---------------------------------------------------------------
always @*
begin : read_rx_buffer
rx_byte = rx_buffer[rx_buffer_rd_ptr_reg];
end // read_rx_buffer
//---------------------------------------------------------------
// tx_buffer_logic
//
// Update logic for the tx-buffer. Given the response type and
// the correct contents of the tx_buffer is assembled when
// and update is signalled by the test engine.
//---------------------------------------------------------------
always @*
begin: tx_buffer_logic
// Defafult assignments
tx_buffer_muxed0 = 8'h00;
tx_buffer_muxed1 = 8'h00;
tx_buffer_muxed2 = 8'h00;
tx_buffer_muxed3 = 8'h00;
tx_buffer_muxed4 = 8'h00;
tx_buffer_muxed5 = 8'h00;
tx_buffer_muxed6 = 8'h00;
tx_buffer_muxed7 = 8'h00;
tx_buffer_muxed8 = 8'h00;
tx_msg_len_new = 4'h0;
tx_msg_len_we = 0;
tx_buffer_we = 0;
if (update_tx_buffer)
begin
tx_buffer_we = 1;
tx_buffer_muxed0 = SOR;
case (response_type)
READ_OK:
begin
tx_buffer_muxed1 = READ_OK;
tx_buffer_muxed2 = core_addr_byte0_reg;
tx_buffer_muxed3 = core_addr_byte1_reg;
tx_buffer_muxed4 = core_read_data_reg[31 : 24];
tx_buffer_muxed5 = core_read_data_reg[23 : 16];
tx_buffer_muxed6 = core_read_data_reg[15 : 8];
tx_buffer_muxed7 = core_read_data_reg[7 : 0];
tx_buffer_muxed8 = EOR;
tx_msg_len_new = 4'h8;
tx_msg_len_we = 1;
end
WRITE_OK:
begin
tx_buffer_muxed1 = WRITE_OK;
tx_buffer_muxed2 = core_addr_byte0_reg;
tx_buffer_muxed3 = core_addr_byte1_reg;
tx_buffer_muxed4 = EOR;
tx_msg_len_new = 4'h4;
tx_msg_len_we = 1;
end
RESET_OK:
begin
tx_buffer_muxed1 = RESET_OK;
tx_buffer_muxed2 = EOR;
tx_msg_len_new = 4'h2;
tx_msg_len_we = 1;
end
ERROR:
begin
tx_buffer_muxed1 = ERROR;
tx_buffer_muxed2 = cmd_reg;
tx_buffer_muxed3 = EOR;
tx_msg_len_new = 4'h3;
tx_msg_len_we = 1;
end
default:
begin
// Any response type not explicitly defined is treated as UNKNOWN.
tx_buffer_muxed1 = UNKNOWN;
tx_buffer_muxed2 = cmd_reg;
tx_buffer_muxed3 = EOR;
tx_msg_len_new = 4'h3;
tx_msg_len_we = 1;
end
endcase // case (response_type)
end
end // tx_buffer_logic
//----------------------------------------------------------------
// rx_buffer_rd_ptr
//
// Logic for the rx buffer read pointer.
//----------------------------------------------------------------
always @*
begin: rx_buffer_rd_ptr
// Default assignments
rx_buffer_rd_ptr_new = 4'h0;
rx_buffer_rd_ptr_we = 1'b0;
rx_buffer_ctr_dec = 0;
if (rx_buffer_rd_ptr_inc)
begin
rx_buffer_ctr_dec = 1;
rx_buffer_rd_ptr_new = rx_buffer_rd_ptr_reg + 1'b1;
rx_buffer_rd_ptr_we = 1'b1;
end
end // rx_buffer_rd_ptr
//----------------------------------------------------------------
// rx_buffer_wr_ptr
//
// Logic for the rx buffer write pointer.
//----------------------------------------------------------------
always @*
begin: rx_buffer_wr_ptr
// Default assignments
rx_buffer_wr_ptr_new = 4'h0;
rx_buffer_wr_ptr_we = 1'b0;
rx_buffer_ctr_inc = 0;
if (rx_buffer_wr_ptr_inc)
begin
rx_buffer_ctr_inc = 1;
rx_buffer_wr_ptr_new = rx_buffer_wr_ptr_reg + 1'b1;
rx_buffer_wr_ptr_we = 1'b1;
end
end // rx_buffer_wr_ptr
//----------------------------------------------------------------
// rx_buffer_ctr
//
// Logic for the rx buffer element counter.
//----------------------------------------------------------------
always @*
begin: rx_buffer_ctr
// Default assignments
rx_buffer_ctr_new = 4'h0;
rx_buffer_ctr_we = 1'b0;
rx_buffer_empty = 1'b0;
rx_buffer_full = 1'b0;
if (rx_buffer_ctr_inc)
begin
rx_buffer_ctr_new = rx_buffer_ctr_reg + 1'b1;
rx_buffer_ctr_we = 1'b1;
end
else if (rx_buffer_ctr_dec)
begin
rx_buffer_ctr_new = rx_buffer_ctr_reg - 1'b1;
rx_buffer_ctr_we = 1'b1;
end
if (rx_buffer_ctr_reg == 4'h0)
begin
rx_buffer_empty = 1'b1;
end
if (rx_buffer_ctr_reg == BUFFER_MAX)
begin
rx_buffer_full = 1'b1;
end
end // rx_buffer_ctr
//----------------------------------------------------------------
// tx_buffer_ptr
//
// Logic for the tx buffer pointer. Supports reset and
// incremental updates.
//----------------------------------------------------------------
always @*
begin: tx_buffer_ptr
// Default assignments
tx_buffer_ptr_new = 4'h0;
tx_buffer_ptr_we = 1'b0;
if (tx_buffer_ptr_inc)
begin
tx_buffer_ptr_new = tx_buffer_ptr_reg + 1'b1;
tx_buffer_ptr_we = 1'b1;
end
else if (tx_buffer_ptr_rst)
begin
tx_buffer_ptr_new = 4'h0;
tx_buffer_ptr_we = 1'b1;
end
end // tx_buffer_ptr
//----------------------------------------------------------------
// rx_engine
//
// FSM responsible for handling receiving message bytes from the
// host interface and storing them in the receive buffer.
//----------------------------------------------------------------
always @*
begin: rx_engine
// Default assignments
rx_ack_new = 1'b0;
rx_ack_we = 1'b0;
rx_buffer_we = 1'b0;
rx_buffer_wr_ptr_inc = 1'b0;
rx_engine_new = RX_IDLE;
rx_engine_we = 1'b0;
case (rx_engine_reg)
RX_IDLE:
begin
if (rx_syn_reg)
begin
if (!rx_buffer_full)
begin
rx_buffer_we = 1'b1;
rx_engine_new = RX_ACK;
rx_engine_we = 1'b1;
end
end
end
RX_ACK:
begin
rx_ack_new = 1'b1;
rx_ack_we = 1'b1;
rx_buffer_wr_ptr_inc = 1'b1;
rx_engine_new = RX_NSYN;
rx_engine_we = 1'b1;
end
RX_NSYN:
begin
if (!rx_syn_reg)
begin
rx_ack_new = 1'b0;
rx_ack_we = 1'b1;
rx_engine_new = RX_IDLE;
rx_engine_we = 1;
end
end
default:
begin
end
endcase // case (rx_engine_reg)
end // rx_engine
//----------------------------------------------------------------
// tx_engine
//
// FSM responsible for handling transmitting message bytes
// to the host interface.
//----------------------------------------------------------------
always @*
begin: tx_engine
// Default assignments
tx_buffer_ptr_inc = 0;
tx_buffer_ptr_rst = 0;
response_sent_new = 0;
response_sent_we = 0;
tx_syn_new = 0;
tx_syn_we = 0;
tx_engine_new = TX_IDLE;
tx_engine_we = 0;
case (tx_engine_reg)
TX_IDLE:
begin
if (send_response_reg)
begin
tx_syn_new = 1;
tx_syn_we = 1;
tx_engine_new = TX_SYN;
tx_engine_we = 1;
end
end
TX_SYN:
begin
if (tx_ack_reg)
begin
tx_syn_new = 0;
tx_syn_we = 1;
tx_engine_new = TX_NOACK;
tx_engine_we = 1;
end
end
TX_NOACK:
begin
if (!tx_ack_reg)
begin
tx_engine_new = TX_NEXT;
tx_engine_we = 1;
end
end
TX_NEXT:
begin
if (tx_buffer_ptr_reg == tx_msg_len_reg)
begin
tx_engine_new = TX_SENT;
tx_engine_we = 1;
end
else
begin
tx_buffer_ptr_inc = 1;
tx_syn_new = 1;
tx_syn_we = 1;
tx_engine_new = TX_SYN;
tx_engine_we = 1;
end
end
TX_SENT:
begin
response_sent_new = 1;
response_sent_we = 1;
tx_engine_new = TX_DONE;
tx_engine_we = 1;
end
TX_DONE:
begin
response_sent_new = 0;
response_sent_we = 1;
tx_buffer_ptr_rst = 1;
tx_engine_new = TX_IDLE;
tx_engine_we = 1;
end
default:
begin
tx_engine_new = TX_IDLE;
tx_engine_we = 1;
end
endcase // case (tx_engine_reg)
end // tx_engine
//----------------------------------------------------------------
// test_engine
//
// Test engine FSM logic. Parses received commands, tries to
// execute the commands and assmbles the response to the
// given commands.
//----------------------------------------------------------------
always @*
begin: test_engine
// Default assignments.
core_reset_n_new = 1;
core_reset_n_we = 0;
core_cs_new = 0;
core_cs_we = 0;
core_we_new = 0;
core_we_we = 0;
sample_core_output = 0;
update_tx_buffer = 0;
response_type = 8'h00;
rx_buffer_rd_ptr_inc = 0;
cmd_we = 0;
core_addr_byte0_we = 0;
core_addr_byte1_we = 0;
core_wr_data_byte0_we = 0;
core_wr_data_byte1_we = 0;
core_wr_data_byte2_we = 0;
core_wr_data_byte3_we = 0;
send_response_new = 0;
send_response_we = 0;
test_engine_new = TEST_IDLE;
test_engine_we = 0;
case (test_engine_reg)
TEST_IDLE:
begin
if (!rx_buffer_empty)
begin
rx_buffer_rd_ptr_inc = 1;
if (rx_byte == SOC)
begin
test_engine_new = TEST_GET_CMD;
test_engine_we = 1;
end
end
end
TEST_GET_CMD:
begin
if (!rx_buffer_empty)
begin
rx_buffer_rd_ptr_inc = 1;
cmd_we = 1;
test_engine_new = TEST_PARSE_CMD;
test_engine_we = 1;
end
end
TEST_PARSE_CMD:
begin
case (cmd_reg)
RESET_CMD:
begin
test_engine_new = TEST_GET_EOC;
test_engine_we = 1;
end
READ_CMD:
begin
test_engine_new = TEST_GET_ADDR0;
test_engine_we = 1;
end
WRITE_CMD:
begin
test_engine_new = TEST_GET_ADDR0;
test_engine_we = 1;
end
default:
begin
test_engine_new = TEST_CMD_UNKNOWN;
test_engine_we = 1;
end
endcase // case (cmd_reg)
end
TEST_GET_ADDR0:
begin
if (!rx_buffer_empty)
begin
rx_buffer_rd_ptr_inc = 1;
core_addr_byte0_we = 1;
test_engine_new = TEST_GET_ADDR1;
test_engine_we = 1;
end
end
TEST_GET_ADDR1:
begin
if (!rx_buffer_empty)
begin
rx_buffer_rd_ptr_inc = 1;
core_addr_byte1_we = 1;
case (cmd_reg)
READ_CMD:
begin
test_engine_new = TEST_GET_EOC;
test_engine_we = 1;
end
WRITE_CMD:
begin
test_engine_new = TEST_GET_DATA0;
test_engine_we = 1;
end
default:
begin
test_engine_new = TEST_CMD_UNKNOWN;
test_engine_we = 1;
end
endcase // case (cmd_reg)
end
end
TEST_GET_DATA0:
begin
if (!rx_buffer_empty)
begin
rx_buffer_rd_ptr_inc = 1;
core_wr_data_byte0_we = 1;
test_engine_new = TEST_GET_DATA1;
test_engine_we = 1;
end
end
TEST_GET_DATA1:
begin
if (!rx_buffer_empty)
begin
rx_buffer_rd_ptr_inc = 1;
core_wr_data_byte1_we = 1;
test_engine_new = TEST_GET_DATA2;
test_engine_we = 1;
end
end
TEST_GET_DATA2:
begin
if (!rx_buffer_empty)
begin
rx_buffer_rd_ptr_inc = 1;
core_wr_data_byte2_we = 1;
test_engine_new = TEST_GET_DATA3;
test_engine_we = 1;
end
end
TEST_GET_DATA3:
begin
if (!rx_buffer_empty)
begin
rx_buffer_rd_ptr_inc = 1;
core_wr_data_byte3_we = 1;
test_engine_new = TEST_GET_EOC;
test_engine_we = 1;
end
end
TEST_GET_EOC:
begin
if (!rx_buffer_empty)
begin
rx_buffer_rd_ptr_inc = 1;
if (rx_byte == EOC)
begin
case (cmd_reg)
RESET_CMD:
begin
test_engine_new = TEST_RST_START;
test_engine_we = 1;
end
READ_CMD:
begin
test_engine_new = TEST_RD_START;
test_engine_we = 1;
end
WRITE_CMD:
begin
test_engine_new = TEST_WR_START;
test_engine_we = 1;
end
default:
begin
test_engine_new = TEST_CMD_UNKNOWN;
test_engine_we = 1;
end
endcase // case (cmd_reg)
end
else
begin
test_engine_new = TEST_CMD_ERROR;
test_engine_we = 1;
end
end
end
TEST_RST_START:
begin
core_reset_n_new = 0;
core_reset_n_we = 1;
test_engine_new = TEST_RST_WAIT;
test_engine_we = 1;
end
TEST_RST_WAIT:
begin
test_engine_new = TEST_RST_END;
test_engine_we = 1;
end
TEST_RST_END:
begin
core_reset_n_new = 1;
core_reset_n_we = 1;
update_tx_buffer = 1;
response_type = RESET_OK;
test_engine_new = TEST_SEND_RESPONSE;
test_engine_we = 1;
end
TEST_RD_START:
begin
core_cs_new = 1;
core_cs_we = 1;
test_engine_new = TEST_RD_WAIT;
test_engine_we = 1;
end
TEST_RD_WAIT:
begin
sample_core_output = 1;
test_engine_new = TEST_RD_END;
test_engine_we = 1;
end
TEST_RD_END:
begin
core_cs_new = 0;
core_cs_we = 1;
sample_core_output = 0;
if (core_error_reg)
begin
update_tx_buffer = 1;
response_type = ERROR;
end
else
begin
update_tx_buffer = 1;
response_type = READ_OK;
end
test_engine_new = TEST_SEND_RESPONSE;
test_engine_we = 1;
end
TEST_WR_START:
begin
core_cs_new = 1;
core_cs_we = 1;
core_we_new = 1;
core_we_we = 1;
test_engine_new = TEST_WR_WAIT;
test_engine_we = 1;
end
TEST_WR_WAIT:
begin
sample_core_output = 1;
test_engine_new = TEST_WR_END;
test_engine_we = 1;
end
TEST_WR_END:
begin
core_cs_new = 0;
core_cs_we = 1;
core_we_new = 0;
core_we_we = 1;
sample_core_output = 0;
if (core_error_reg)
begin
update_tx_buffer = 1;
response_type = ERROR;
end
else
begin
update_tx_buffer = 1;
response_type = WRITE_OK;
end
test_engine_new = TEST_SEND_RESPONSE;
test_engine_we = 1;
end
TEST_CMD_UNKNOWN:
begin
update_tx_buffer = 1;
response_type = UNKNOWN;
test_engine_new = TEST_SEND_RESPONSE;
test_engine_we = 1;
end
TEST_CMD_ERROR:
begin
update_tx_buffer = 1;
response_type = ERROR;
test_engine_new = TEST_SEND_RESPONSE;
test_engine_we = 1;
end
TEST_SEND_RESPONSE:
begin
send_response_new = 1;
send_response_we = 1;
if (response_sent_reg)
begin
send_response_new = 0;
send_response_we = 1;
test_engine_new = TEST_IDLE;
test_engine_we = 1;
end
end
default:
begin
// If we encounter an unknown state we move
// back to idle.
test_engine_new = TEST_IDLE;
test_engine_we = 1;
end
endcase // case (test_engine_reg)
end // test_engine
endmodule // coretest
//======================================================================
// EOF coretest.v
//======================================================================
|