-
Notifications
You must be signed in to change notification settings - Fork 0
/
usbHost.sv
1655 lines (1508 loc) · 43.2 KB
/
usbHost.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
`default_nettype none
//18-341 P5 Gun Charnmanee (gcharnma) Dylan Koenig (djkoenig)
// Write your usb host here. Do not modify the port list.
module usbHost
(input logic clk, rst_L,
usbWires wires);
/* Tasks needed to be finished to run testbenches */
task prelabRequest
// sends an OUT packet with ADDR=5 and ENDP=4
// packet should have SYNC and EOP too
(input bit [15:0] data);
usbHost.token = 11'b1010000_0010;
usbHost.data_out =64'hAB;
/*
usbHost.mode = 2'd0;
usbHost.start_send_token <=1'b1;
wait(usbHost.done_send_token);
usbHost.start_send_token<= 1'b0;
*/
/*
usbHost.mode = 2'd1;
usbHost.start_send_data <=1'b1;
wait(usbHost.done_send_data);
usbHost.start_send_data<= 1'b0;
*/
/*
usbHost.mode = 2'd2;
usbHost.start_send_hand <=1'b1;
wait(usbHost.done_send_hand);
usbHost.start_send_hand<= 1'b0;
*/
endtask: prelabRequest
task readData
// host sends memPage to thumb drive and then gets data back from it
// then returns data and status to the caller
(input bit [15:0] mempage, // Page to write
output bit [63:0] data, // array of bytes to write
output bit success);
data = 64'b0;
usbHost.token = 11'b1010000_0010; // ADDR 5 ENDP 4
usbHost.data_out ={48'h0 , mempage};
usbHost.start_top =1;
usbHost.read_write = 1;
repeat(4) @(posedge clk);
wait(usbHost.done_send_token);
$display("finished SEND_TOKEN");
wait(usbHost.done_send_data);
$display("finished SEND_DATA");
usbHost.token = 11'b1010000_0001; //ADDR5 ENDP 8
usbHost.read_write =0; // Do READ process
repeat(4) @(posedge clk);
wait(usbHost.receive||usbHost.system_done);
$display("finished receiving hand or failed");
wait(usbHost.done_send_token);
$display("finished SEND_TOKEN");
/*
wait(usbHost.r_data_finish|| r_data_fail);
$display("finished receiving data");
@(posedge clk);
if( );
*/
wait(usbHost.system_done);
@(posedge clk);
usbHost.start_top =0;
// din't fail, send out ACK
//wait(usbHost.done_send_hand);
if(usbHost.system_done && ~usbHost.process_success) begin
$display("failed trying");
success =0;
end
else if(usbHost.process_success) begin
$display("PROCESS SUCCEED");
data = usbHost.msg_data;
success =1;
end
else success =0;
@(posedge clk);
endtask: readData
task writeData
// Host sends memPage to thumb drive and then sends data
// then returns status to the caller
(input bit [15:0] mempage, // Page to write
input bit [63:0] data, // array of bytes to write
output bit success);
usbHost.token = 11'b1010000_0010; // ADDR 5 ENDP 4
usbHost.data_out = {48'h0 , mempage};
usbHost.start_top =1;
usbHost.read_write = 1;
repeat(4) @(posedge clk);
/*
usbHost.token <= 11'b1010000_0001; //ADDR5 ENDP 8
*/
wait(usbHost.done_send_token);
$display("finished SEND_TOKEN");
wait(usbHost.done_send_data);
$display("finished SEND_DATA");
usbHost.token = 11'b1010000_0001; //ADDR5 ENDP 8
usbHost.data_out = data;
wait(usbHost.receive||usbHost.system_done);
$display("finished receiving hand or failed");
@(posedge clk);
if(usbHost.system_done && ~usbHost.process_success) begin
$display("failed trying");
success =0;
return;
end
wait(usbHost.done_send_token);
$display("finished SEND_TOKEN");
wait(usbHost.done_send_data);
$display("finished SEND_DATA");
usbHost.start_top =0;
wait(usbHost.receive || usbHost.system_done);
@(posedge clk);
if(usbHost.system_done && ~usbHost.process_success) begin
$display("failed trying");
success =0;
return;
end
else if(usbHost.process_success) begin
$display("PROCESS SUCCEED");
success =1;
end
else success =0;
@(posedge clk);
/*
wait( system_done );
if (process_success)
*/
endtask: writeData
// usbHost starts here!!
logic nrzi_in, nrzi_out, clear, start, wiresDP, wiresDM;
logic stuffer_in, stuffer_out, pause, crc_in, crc16_in, crc_out, crc16_out, en_crc_L, sync_out, pid_out, sync_pid_out;
logic ld_tok, en_tok, ld_sync, en_sync, ld_pid, en_pid, enable_send, do_eop;
logic sel_1, sel_2, sel_3; //sel_1 for sync or pid, sel_2 for nrzi input, sel_3 for crc16 or crc5
logic [10:0] token;
logic [63:0] data_out;
logic [7:0] sync, pid;
assign sync = 8'b0000_0001; // a constant
logic clear_sender;
logic done_send_token,start_send_token;
logic clear_stuffer;
logic start_send_data, start_send_hand;
logic start_top, read_write;
////////////////////////////////////////////////////////////////
logic [1:0] mode; // 0 = SEND_TOKEN, 1 = SEND_DATA, 2 = SEND_HAND
////////////////////////////////////////////////////////////////
////////////////////////////////////////////
logic do_eop_token,en_sync_token,en_crc_L_token, en_pid_token, en_tok_token, clear_token, ld_sync_token, ld_pid_token,
ld_tok_token, sel_1_token,sel_2_token,enable_send_token,clear_stuffer_token;
logic do_eop_hand,en_sync_hand, en_pid_hand, clear_hand, ld_sync_hand, ld_pid_hand, sel_1_hand,sel_2_hand,enable_send_hand,
done_send_hand;
logic do_eop_data, en_sync_data ,en_crc_L_data, en_pid_data, en_data_data, clear_data, ld_sync_data, ld_pid_data,
ld_data_data, sel_1_data,sel_2_data,enable_send_data, clear_stuffer_data,
done_send_data;
send_token handle_token(clk, rst_L, start_send_token, pause,
do_eop_token,en_sync_token,en_crc_L_token, en_pid_token, en_tok_token, clear_token, ld_sync_token, ld_pid_token,
ld_tok_token, sel_1_token,sel_2_token,enable_send_token,clear_stuffer_token,
done_send_token); // done signal sends to above.
send_data handle_data(clk, rst_L, start_send_data, pause,
do_eop_data, en_sync_data ,en_crc_L_data, en_pid_data, en_data_data, clear_data, ld_sync_data, ld_pid_data,
ld_data_data, sel_1_data,sel_2_data,enable_send_data, clear_stuffer_data,
done_send_data); // done signal sends to above.
send_ack_nak handle_hand( clk, rst_L, start_send_hand, pause, // ack or nak is determined by the higher FSM
do_eop_hand,en_sync_hand, en_pid_hand, clear_hand, ld_sync_hand, ld_pid_hand, sel_1_hand,sel_2_hand,enable_send_hand,
done_send_hand); // done signal sends to above.
////////// RECEIVES MAGIC FLOATING BLOCK OF CODE //////////
logic data_in_valid;
always_comb begin
if( {wires.DP,wires.DM} == 2'b00) begin // if no info or doing eop
data_in_valid = 1'b0;
end
else begin //activate nrzi
data_in_valid = 1'b1;
end
end
// check eop
logic eop_count, eop_valid;
logic [2:0] counter_eop;
//////////////////////////////////////////////////////////////////////////
always_comb begin
eop_valid =1'b0;
if(({wires.DP,wires.DM} == 2'd0)&&(counter_eop<2'd2)) begin
eop_count =1'b1; // count 2 consecutive XX
end
else if (counter_eop < 2'd2) begin
eop_count = 1'd0;
end
else if ({wires.DP,wires.DM} == 2'b10) begin
eop_count =1'b1; // see 2 consecutive XX follow by 1.
eop_valid =1'b1;
end
else eop_count =1'b0;
end
always_ff @(posedge clk, negedge rst_L) begin
if(~rst_L) counter_eop <= 3'd0;
else if(eop_count) counter_eop <= counter_eop + 3'd1;
else counter_eop <= 3'd0;
end
/////////////////////////////////////////////////////////////////////
logic [63:0] msg_out, msg_data;
logic [3:0] check_pid_out;
logic ld_msg;
logic msg_ok, done, receive, receive_hand, r_acknak_fail, ack, nak;
logic r_data_start, r_data_finish, r_data_fail, r_data_success, pause_receive;
logic process_success, system_done;
logic reverse_nrzi_in, reverse_nrzi_out, unstuff_out;
assign reverse_nrzi_in = wires.DP;
reverse_nrzi takein(reverse_nrzi_in, clk, rst_L, ~data_in_valid , reverse_nrzi_out);
// if data not valid, clear.
logic clear_sync1, clear_sync2, valid_sync, pid_valid, clear_pid1, clear_pid2, clear_unstuff, clear_crc;
// check sync
check_sync checker(reverse_nrzi_out, clk, rst_L,data_in_valid, (clear_sync1&&clear_sync2), valid_sync);
////////////////////////////
check_pid testpid(reverse_nrzi_out, clk, rst_L, (clear_pid1&&clear_pid2), pid_valid, check_pid_out);
////////////////////////////
reverse_stuffer unstuff(reverse_nrzi_out, clk, rst_L, clear_unstuff, unstuff_out, pause_receive);
receiver takecrc16(unstuff_out, rst_L, clk, pause_receive, clear_unstuff, clear_crc, msg_out, msg_ok,done);
//receive_data fsm instantiation
receive_data r_data_fsm(clk, rst_L, pause_receive, r_data_start, valid_sync,
msg_ok, clear_sync1, r_data_finish, r_data_fail, r_data_success, clear_pid1,
clear_crc, clear_unstuff,ld_msg);
//receive_acknak fsm instantiation
receive_acknak r_acknak_fsm(clk, rst_L, receive_hand, valid_sync, check_pid_out, r_acknak_fail, clear_pid2, clear_sync2, ack, nak, receive);
////////// RECEIVES MAGIC BLOCK OF CODE ENDS HERE //////////
/***top_fsm***/
top_fsm topFSM(clk, rst_L, start_top, read_write, done_send_token, done_send_data, done_send_hand, receive, ack, nak, r_acknak_fail, r_data_finish,
r_data_fail, r_data_success, start_send_token, start_send_data, start_send_hand, mode,pid, r_data_start, process_success, system_done, receive_hand);
mux4ways#(1) mux1(mode, do_eop_token,do_eop_data,do_eop_hand, 1'b0 ,do_eop),
mux2(mode, en_sync_token, en_sync_data, en_sync_hand,1'b0, en_sync),
mux3(mode, en_crc_L_token, en_crc_L_data, 1'b1, 1'b0, en_crc_L),
mux4(mode, en_pid_token, en_pid_data, en_pid_hand, 1'b0, en_pid),
mux5(mode, en_tok_token, 1'b0, 1'b0, 1'b0, en_tok),
mux6(mode, clear_token, clear_data, clear_hand, 1'b0,clear),
mux7(mode, ld_sync_token, ld_sync_data, ld_sync_hand, 1'b0,ld_sync),
mux8(mode, ld_pid_token, ld_pid_data, ld_pid_hand, 1'b0,ld_pid),
mux9(mode, ld_tok_token, 1'b0, 1'b0,1'b0, ld_tok),
mux10(mode, sel_1_token, sel_1_data, sel_1_hand,1'bz, sel_1),
mux11(mode, sel_2_token, sel_2_data, sel_2_hand, 1'bz,sel_2),
mux12(mode, enable_send_token, enable_send_data, enable_send_hand,1'b0,enable_send),
mux13(mode, clear_stuffer_token, clear_stuffer_data, 1'b1,1'bz,clear_stuffer);
always_comb begin
case(mode)
2'd0: sel_3 = 1'b0;
2'd1: sel_3 = 1'b1;
2'd2: sel_3 = 1'bz;
2'd3: sel_3 = 1'bz;
endcase
end
////////////////////////////////////////////
assign clear_sender = en_crc_L;
//implement enable_send as output of protocol_fsm
assign wires.DP = enable_send ? wiresDP : 1'bz;
assign wires.DM = enable_send ? wiresDM : 1'bz;
///////////////////////////////CRC machines
sender crcSender(crc_in, rst_L, clk, clear_sender ,pause, crc_out);
sender16 crcSender16(crc16_in, rst_L, clk,clear_sender ,pause, crc16_out);
//for now: crcSender's output is tied to bitstuffer's input, but should implement mux with crc16's output later!
assign stuffer_in = sel_3 ? crc16_out : crc_out;
//shift register to hold the token as it's sent to crc
shiftRegister #(11) shiftRegToken(clk, rst_L, ld_tok, en_tok, pause, token, crc_in);
//shift register to hold sync
shiftRegister #(8) shiftRegSync(clk, rst_L, ld_sync, en_sync, 1'd0, sync, sync_out);
//shift register to hold pid
shiftRegister #(8) shiftRegPid(clk, rst_L, ld_pid, en_pid, 1'd0, pid, pid_out);
//shift register to hold DATA
shiftRegister #(64) shiftRegData(clk, rst_L, ld_data_data, en_data_data, pause, data_out, crc16_in);
always_ff@(posedge clk, negedge rst_L) begin
if(~rst_L) msg_data <= 64'b0;
else if(ld_msg) msg_data<=msg_out;
else msg_data<= msg_data;
end
///////////////////////////////////////////////////////////////
stuffer bitstuff(stuffer_in, clk, rst_L, clear_stuffer, stuffer_out, pause); // stuff addr, endp,crc5,crc16, and DATA
///////////////////////////////////////////////////////////////
//mux in sync, pid
//mux for selecting between sync or pid
assign sync_pid_out = sel_1 ? sync_out : pid_out;
//mux for NRZI
assign nrzi_in = sel_2 ? stuffer_out : sync_pid_out;
////////////////////////////////////////////////////////////////
nrzi flip(nrzi_in, start, clk, rst_L, clear, nrzi_out);
////////////////////////////////////////////////////////////////
// small handler to send out DP and DM. use do_eop to control between NRZI output and EOP.
logic [2:0] counter_dpdm;
always_comb begin // sending DP and DM
if (do_eop || (counter_dpdm != 3'b0)) begin
if (counter_dpdm == 3'd2) begin
{wiresDP,wiresDM} = 2'b10;
end
else {wiresDP,wiresDM} = 2'b00;
end
else {wiresDP,wiresDM} = {nrzi_out,~nrzi_out}; // go back to output from NRZI
end
always_ff @(posedge clk, negedge rst_L) begin
if(~rst_L) counter_dpdm <= 3'd0;
else if(do_eop) counter_dpdm <= counter_dpdm + 3'd1;
else counter_dpdm <= 3'd0;
end
endmodule: usbHost
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// modified CRC 5 from hw2 //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module sender(input logic bit_in, rst_L, clk, clear, pause, //pause for bit stuffing
output logic send_bit);
logic [4:0] Q;
logic go;
logic mux;
logic out_bit;
assign send_bit = (mux) ?out_bit : bit_in; // mux output between incoming bit and complement
senderFSM sendit(clk,rst_L,clear,pause,mux,go);
crcCal calcIt(send_bit,clk,rst_L,clear,pause, Q);
complementMake make(rst_L, go, clk,clear, pause,Q,out_bit);
endmodule: sender
module senderFSM( input logic clk, rst_L, clear, pause,
output logic mux,go);
logic [4:0] counter;
enum logic [2:0] { FIRST, DATA,COMP, DEAD} cs,ns;
always_comb begin
go =1'b0;
mux=1'b0; // mux =1 => shifting out COMP
case(cs)
FIRST: begin
ns = DATA;
end
DATA: begin
if(counter >= 5'd11) begin
ns = COMP;
go =1'b1;
mux =1'b1;
end
else
ns= DATA;
end
COMP:begin
if(counter>=5'd15) begin
ns = DEAD;
mux=1'b1;
end
else begin
mux =1'b1;
ns =COMP;
end
end
DEAD: ns = DEAD;
endcase
end
always_ff @(posedge clk, negedge rst_L) begin
if(~rst_L) begin
cs <= FIRST;
counter <= 5'b0;
end
else if(clear) begin
cs<=FIRST;
counter <= 5'b0;
end
else if(pause)begin
cs <= cs; // stall the process by one clock
counter<= counter;
end
else begin
cs<= ns;
counter <= counter + 5'b00001;
end
end
endmodule:senderFSM
module crcCal(input logic bit_in,clk,rst_L, clear, pause, output logic [4:0] Q);
always_ff @(posedge clk, negedge rst_L) begin
if(~rst_L)
Q <= 5'b11111;
else if(clear) Q<= 5'b11111;
else if(pause) Q <= Q; //stall for one clock.
else begin
Q[0] <= bit_in ^ Q[4];
Q[1] <= Q[0];
Q[2] <= (bit_in ^ Q[4] ) ^ Q[1];
Q[3] <= Q[2];
Q[4] <= Q[3];
end
end
endmodule: crcCal
module complementMake(input logic rst_L, go, clk, clear, pause,
input logic [4:0] Q,
output logic oneBit);
logic [3:0] remainder;
always_comb begin // the first clock just output inverted MSB value of remainder
if(go) oneBit = ~Q[4];
else oneBit = remainder[3]; // for subsequent clocks take output from shift register.
end
always_ff@(posedge clk, negedge rst_L) begin
if(~rst_L)
remainder <= 4'b0;
else if (clear ) remainder <=4'b0;
else if(pause) remainder <= remainder; //stall for one clock.
else if(go)
remainder <= ~(Q[3:0]); // need to hold 4 bits since the first one is sent out on the clock
else begin
remainder[3:1]<= remainder[2:0];
remainder[0] <= 1'b0;
end
end
endmodule: complementMake
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CRC16 //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module sender16(input logic bit_in, rst_L, clk, clear, pause, //pause for bit stuffing
output logic send_bit);
logic [15:0] Q;
logic go;
logic mux;
logic out_bit;
assign send_bit = (mux) ?out_bit : bit_in; // mux output between incoming bit and complement
senderFSM16 sendit(clk,rst_L, clear, pause,mux,go);
crcCal16 calcIt(send_bit,clk,rst_L, clear, pause,Q);
complementMake16 make(rst_L,go,clk, clear, pause,Q,out_bit);
endmodule: sender16
module senderFSM16( input logic clk, rst_L, clear, pause,
output logic mux,go);
logic [6:0] counter;
enum logic [2:0] {FIRST,DATA,COMP,DEAD} cs,ns;
always_comb begin
go =1'b0;
mux=1'b0; // mux =1 => shifting out COMP
case(cs)
FIRST: begin
ns = DATA;
end
DATA: begin
if(counter >= 7'd64) begin
ns = COMP;
go = 1'b1;
mux = 1'b1;
end
else
ns= DATA;
end
COMP:begin
if(counter >= 7'd79) begin
ns = DEAD;
mux = 1'b1;
end
else begin
mux = 1'b1;
ns = COMP;
end
end
DEAD: ns = DEAD;
endcase
end
always_ff @(posedge clk, negedge rst_L) begin
if(~rst_L) begin
cs <= FIRST;
counter <= 7'd0;
end
else if( clear) begin
cs<= FIRST;
counter<= 7'd0;
end
else if(pause)begin
cs <= cs; // stall the process by one clock
counter<= counter;
end
else begin
cs<= ns;
counter <= counter + 7'd1;
end
end
endmodule:senderFSM16
module crcCal16(input logic bit_in,clk,rst_L, clear, pause, output logic [15:0] Q);
always_ff @(posedge clk, negedge rst_L) begin
if(~rst_L)
Q <= 16'b1111_1111_1111_1111;
else if(clear) Q<= 16'hFFFF;
else if(pause) Q <= Q; //stall for one clock.
else begin
Q[0] <= bit_in^Q[15];
Q[1] <= Q[0];
Q[2] <= (bit_in^Q[15])^Q[1];
Q[3] <= Q[2];
Q[4] <= Q[3];
Q[5] <= Q[4];
Q[6] <= Q[5];
Q[7] <= Q[6];
Q[8] <= Q[7];
Q[9] <= Q[8];
Q[10] <= Q[9];
Q[11] <= Q[10];
Q[12] <= Q[11];
Q[13] <= Q[12];
Q[14] <= Q[13];
Q[15] <= (bit_in^Q[15])^Q[14];
end
end
endmodule: crcCal16
module complementMake16(input logic rst_L, go, clk, clear, pause,
input logic [15:0] Q,
output logic oneBit);
logic [14:0] remainder;
always_comb begin // the first clock just output inverted MSB value of remainder
if(go) oneBit = ~Q[15];
else oneBit = remainder[14]; // for subsequent clocks take output from shift register.
end
always_ff@(posedge clk, negedge rst_L) begin
if(~rst_L)
remainder <= 15'd0;
else if(clear) remainder <=15'd0;
else if(pause) remainder <= remainder; //stall for one clock.
else if(go)
remainder <= ~(Q[14:0]); // need to hold 15 bits since the first one is sent out on the clock
else begin
remainder[14:1] <= remainder[13:0];
remainder[0] <= 1'b0;
end
end
endmodule: complementMake16
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Bit Stuffing //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module stuffer(input logic bit_in, clk, rst_L, clear,
output logic bit_out, pause); // stuff addr, endp,crc5,crc16, and DATA
logic [2:0] count;
always_comb begin
if(count ==3'd6)begin // found 6 ones, stuff a 0, pause for 1 clock
pause =1'd1;
bit_out =1'd0;
end
else begin
pause =1'd0;
bit_out = bit_in;
end
end
always_ff @(posedge clk, negedge rst_L)begin
if(~rst_L) count <= 0; //reset
else if (clear) count<=0; // count up to 6 or found a zero. clear
else if(bit_in) count <= count +3'd1; // keep counting.
else count <=0; // found a 0, clear.
end
endmodule:stuffer
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NRZI //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// assume first one is 1;
module nrzi(input logic bit_in, start, clk, rst_L, clear,
output logic bit_out); // everything except EOP
logic past;
always_comb begin
if(bit_in) bit_out = past; // input is 1, don't invert
else bit_out =~past; // input is 0 invert.
end
always_ff @(posedge clk, negedge rst_L) begin
if(~rst_L) past <= 1'd1;
else if(clear) past <= 1'd1;
else past <= bit_out;
end
endmodule
module shiftRegister
#(parameter w = 11)
(input logic clk, rst_L, ld, en, pause,
input logic [w-1:0] in,
output logic out);
logic [w-1:0] val;
assign out = val[w-1];
always_ff @(posedge clk, negedge rst_L) begin
if (~rst_L) begin
val <= 'd0;
end
else if (ld) begin
val <= in;
end
else if (en && !pause) begin
val <= val << 1;
end
end
endmodule: shiftRegister
module mux4ways#(parameter w = 1) (input logic [1:0] sel,
input logic [w-1:0] inA,inB,inC,inD,
output logic [w-1:0] out);
always_comb begin
case(sel)
2'd0:out = inA;
2'd1:out = inB;
2'd2:out = inC;
2'd3:out = inD;
endcase
end
endmodule:mux4ways
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SEND TOKEN FSM //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
module send_token(input logic clk, rst_L, start, pause,
output logic do_eop,en_sync,en_crc_L, en_pid, en_tok, clear, ld_sync, ld_pid,ld_tok, sel_1,sel_2,enable_send, clear_stuffer,
output logic done); // done signal sends to above.
logic [4:0] sync_count, pid_count,token_count, eop_count;
logic [4:0] sync_add,pid_add, token_add, eop_add;
enum logic [2:0] {IDLE, SYNC, PID, TOKE, EOP} cs,ns;
logic clear_counter;
always_comb begin
done = 1'b0;
clear_stuffer = 1'b1;
sync_add = 5'b0;
pid_add =5'b0;
token_add = 5'b0;
eop_add = 5'b0;
clear_counter=1'b0;
do_eop = 1'b0;
en_sync =1'b0;
en_crc_L = 1'b1;// has CRC off.
en_pid = 1'b0;
en_tok = 1'b0;
clear = 1'b1;
ld_sync =1'b0; //
ld_pid = 1'b0; //
ld_tok = 1'b0; //
sel_1 =1'b0; //
sel_2 =1'b0; //
enable_send = 1'b0;
case(cs)
IDLE :begin
if(start) begin
ns = SYNC;
ld_sync = 1'b1;
ld_pid =1'b1;
ld_tok = 1'b1;
sel_1 =1'b1;
sel_2=1'b0;
end
else ns = IDLE; // wait for start signal.
end
SYNC: begin
clear = 1'b0;
en_sync = 1'b1;
sel_1 = 1'b1;
sel_2 = 1'b0;
sync_add =1'b1;
enable_send =1'b1;
if(sync_count < 5'd7)begin
ns = SYNC;
end
else ns = PID;
end
PID: begin
clear = 1'b0;
en_pid = 1'b1;
sel_1 = 1'b0;
sel_2 = 1'b0;
pid_add = 1'b1;
enable_send =1'b1;
if(pid_count<5'd7) begin
ns = PID;
end
else ns = TOKE;
end
TOKE:begin
clear_stuffer = 1'b0;
clear = 1'b0;
sel_1 = 1'b0;
sel_2 = 1'b1;
en_crc_L = 1'b0;
token_add =1'b1;
enable_send =1'b1;
if(token_count <5'd10) en_tok =1'b1;
else en_tok = 1'b0;
if(token_count < 5'd15) begin
ns = TOKE;
end
else ns = EOP;
end
EOP: begin
clear = 1'b1;
en_crc_L =1'b1;
do_eop = 1'b1;
eop_add =1'b1;
enable_send =1'b1;
if(eop_count <5'd2) begin
ns = EOP;
end
else begin
ns = IDLE;
done = 1'b1;
clear_counter =1'b1;
end
end
endcase
end
always_ff @(posedge clk, negedge rst_L) begin
if(~rst_L) begin
cs <=IDLE;
sync_count <= 5'b0;
pid_count <= 5'b0;
token_count <= 5'b0;
eop_count <= 5'b0;
end
else if(clear_counter) begin
cs <=ns;
sync_count <= 5'b0;
pid_count <= 5'b0;
token_count <= 5'b0;
eop_count <= 5'b0;
end
else if(pause) begin
cs <=cs;
sync_count <= sync_count;
pid_count <= pid_count;
token_count <= token_count;
eop_count <= eop_count;
end
else begin
cs <=ns;
sync_count <= sync_count + sync_add;
pid_count <= pid_count +pid_add;
token_count <= token_count +token_add;
eop_count <= eop_count + eop_add;
end
end
endmodule: send_token
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// SEND_HAND //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
module send_ack_nak(input logic clk, rst_L, start, pause, // ack or nak is determined by the higher FSM
output logic do_eop,en_sync, en_pid, clear, ld_sync, ld_pid, sel_1,sel_2,enable_send,
output logic done); // done signal sends to above.
logic [4:0] sync_count, pid_count,token_count, eop_count;
logic [4:0] sync_add,pid_add, token_add, eop_add;
enum logic [2:0] {IDLE, SYNC, PID, EOP} cs,ns;
logic clear_counter;
always_comb begin
done = 1'b0;
sync_add = 5'b0;
pid_add =5'b0;
eop_add = 5'b0;
clear_counter=1'b0;
do_eop = 1'b0;
en_sync =1'b0;
en_pid = 1'b0;
clear = 1'b1;
ld_sync =1'b0; //
ld_pid = 1'b0; //
sel_1 =1'b0; //
sel_2 =1'b0; //
enable_send = 1'b0;
case(cs)
IDLE :begin
if(start) begin
ns = SYNC;
ld_sync = 1'b1;
ld_pid =1'b1;
sel_1 =1'b1;
sel_2=1'b0;
end
else ns = IDLE; // wait for start signal.
end
SYNC: begin
clear = 1'b0;
en_sync = 1'b1;
sel_1 = 1'b1;
sel_2 = 1'b0;
sync_add =1'b1;
enable_send =1'b1;
if(sync_count < 5'd7)begin
ns = SYNC;
end
else ns = PID;
end
PID: begin
clear = 1'b0;
en_pid = 1'b1;
sel_1 = 1'b0;
sel_2 = 1'b0;
pid_add = 1'b1;
enable_send =1'b1;
if(pid_count<5'd7) begin
ns = PID;
end
else ns = EOP;
end
EOP: begin
clear = 1'b0;
do_eop = 1'b1;
eop_add =1'b1;
enable_send =1'b1;
if(eop_count <5'd2) begin
ns = EOP;
end
else begin
ns = IDLE;
done = 1'b1;
clear_counter =1'b1;
end
end
endcase
end
always_ff @(posedge clk, negedge rst_L) begin
if(~rst_L) begin
cs <=IDLE;
sync_count <= 5'b0;
pid_count <= 5'b0;
eop_count <= 5'b0;
end
else if(clear_counter) begin
cs <=ns;
sync_count <= 5'b0;
pid_count <= 5'b0;
eop_count <= 5'b0;
end
/* else if(pause) begin
cs <=cs;
sync_count <= sync_count;
pid_count <= pid_count;
eop_count <= eop_count;
end*/
else begin
cs <=ns;
sync_count <= sync_count + sync_add;
pid_count <= pid_count +pid_add;
eop_count <= eop_count + eop_add;
end
end
endmodule: send_ack_nak
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// SEND_DATA //
//////////////////////////////////////////////////////////////////////////////////////////////////////////
module send_data(input logic clk, rst_L, start, pause,
output logic do_eop,en_sync,en_crc_L, en_pid, en_data, clear, ld_sync, ld_pid,ld_data, sel_1,sel_2,enable_send, clear_stuffer,
output logic done); // done signal sends to above.
logic [4:0] sync_count, pid_count, eop_count;
logic [6:0] data_count;
logic [4:0] sync_add,pid_add, data_add, eop_add;
enum logic [2:0] {IDLE, SYNC, PID, DATA, EOP} cs,ns;
logic clear_counter;
always_comb begin
done = 1'b0;
clear_stuffer = 1'b1;
sync_add = 5'b0;
pid_add =5'b0;
data_add = 5'b0;
eop_add = 5'b0;
clear_counter=1'b0;
do_eop = 1'b0;
en_sync =1'b0;
en_crc_L = 1'b1;// has CRC off.
en_pid = 1'b0;
en_data = 1'b0;
clear = 1'b1;
ld_sync =1'b0; //
ld_pid = 1'b0; //
ld_data = 1'b0; //
sel_1 =1'b0; //
sel_2 =1'b0; //
enable_send = 1'b0;
case(cs)
IDLE :begin
if(start) begin
enable_send =1'b1;
ns = SYNC;
ld_sync = 1'b1;
ld_pid =1'b1;
ld_data = 1'b1;
sel_1 =1'b1;
sel_2=1'b0;
end
else ns = IDLE; // wait for start signal.
end
SYNC: begin
clear = 1'b0;
en_sync = 1'b1;
sel_1 = 1'b1;
sel_2 = 1'b0;
sync_add =1'b1;
enable_send =1'b1;
if(sync_count < 5'd7)begin
ns = SYNC;
end
else ns = PID;
end
PID: begin
clear = 1'b0;
en_pid = 1'b1;
sel_1 = 1'b0;
sel_2 = 1'b0;
pid_add = 1'b1;
enable_send =1'b1;
if(pid_count<5'd7) begin
ns = PID;
end
else ns = DATA;
end
DATA:begin
clear_stuffer = 1'b0;
clear = 1'b0;
sel_1 = 1'b0;
sel_2 = 1'b1;
en_crc_L = 1'b0;
data_add =1'b1;
enable_send =1'b1;
if(data_count <7'd63) en_data =1'b1;
else if(data_count < 7'd79) begin
ns = DATA;
en_data = 1'b0;
end
else begin
ns = EOP;