summaryrefslogtreecommitdiffstats
path: root/fpga/common/remote_access.v
blob: 5a37e86724b24b1e62c2be9d78440bd252619633 (plain)
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
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 			Raptor Engineering
// Engineer: 			Timothy Pearson
// 
// Design Name:			Remote Access Driver
// Module Name:    		remote_access 
// Project Name: 		Remote Access Driver
// Target Devices: 		Any
// Description: 		Serial remote access driver and LCD display driver
//
// Dependencies: 
//
// (c) 2007-2013 Timothy Pearson, Raptor Engineering
// Released into the Public Domain
//
//////////////////////////////////////////////////////////////////////////////////
module remote_access(
	input main_fifty_clock,				// 50MHz clock in
	input [3:0] remote_access_4_bit_output,		// 4 bit output from the user program to remote access client
	output [3:0] remote_access_4_bit_input,		// 4 bit input from the remote access client to user program
	input [7:0] remote_access_8_bit_output,		// 8 bit output from the user program to remote access client
	output [7:0] remote_access_8_bit_input,		// 8 bit input from the remote access client to user program
	input [15:0] remote_access_16_bit_output,	// 16 bit output from the user program to the remote access client
	output [15:0] remote_access_16_bit_input,	// 16 bit input from the remote access client to the user program
	input serial_port_receiver,
	output serial_port_transmitter,
	input remote_access_input_enable,
	input [7:0] local_input,
	input seize_serial_tx,
	input [7:0] serial_tx_data,
	input serial_tx_strobe,
	output [7:0] serial_rx_data,
	output serial_rx_strobe,
	input [5:0] lcd_data_in_address,
	input [7:0] lcd_data_in_data,
	input lcd_data_in_enable,
	input sram_wren_in,
	input sram_clock_in,
	input [7:0] sram_data_in,
	input [(RAM_ADDR_BITS-1):0] sram_address_in,
	output [7:0] sram_data_out,
	output sram_available,
	input sram_processing_done,
	input [7:0] led_segment_bus,
	input [3:0] led_digit_select,

	// For use on Digilent Spartan 3E or compatible board only
	output [3:0] remote_access_lcd_data_out,
	output remote_access_lcd_rs_out,
	output remote_access_lcd_rw_out,
	output remote_access_lcd_enable_out);

	parameter RAM_ADDR_BITS = 14;
	
	reg [7:0] remote_access_4_bit_input_reg;
	reg [7:0] remote_access_8_bit_input_reg;
	reg [15:0] remote_access_16_bit_input_reg;
	reg [3:0] remote_access_lcd_data_out_reg;
	reg remote_access_lcd_rs_out_reg;
	reg remote_access_lcd_rw_out_reg;
	reg remote_access_lcd_enable_out_reg;
	reg [7:0] serial_rx_data_reg;
	reg serial_rx_strobe_reg;
	reg sram_available_reg;
	reg startup_needed = 1;
	
	assign remote_access_4_bit_input = remote_access_4_bit_input_reg[3:0];
	assign remote_access_8_bit_input = remote_access_8_bit_input_reg;
	assign remote_access_16_bit_input = remote_access_16_bit_input_reg;
	assign remote_access_lcd_data_out = remote_access_lcd_data_out_reg;
	assign remote_access_lcd_rs_out = remote_access_lcd_rs_out_reg;
	assign remote_access_lcd_rw_out = remote_access_lcd_rw_out_reg;
	assign remote_access_lcd_enable_out = remote_access_lcd_enable_out_reg;
	assign serial_rx_data = serial_rx_data_reg;
	assign serial_rx_strobe = serial_rx_strobe_reg;
	assign sram_available = sram_available_reg;
	
	//-----------------------------------------------------------------------------------
	//
	// Create a 4.16MHz clock for the LCD display driver and a 25MHz clock 
	// for the serial receiver.
	//
	//-----------------------------------------------------------------------------------
	
	reg four_mhz_clk;
	reg clk_div_by_two;
	reg clk_div_by_two_oneeighty;
	reg clk_div_by_four;
	reg clk_div_by_eight;
	reg [3:0] fifty_clock_divider = 0;
	
	always @(posedge main_fifty_clock) begin
		fifty_clock_divider = fifty_clock_divider + 1;
		if (fifty_clock_divider > 12) begin
			four_mhz_clk = !four_mhz_clk;
			fifty_clock_divider = 0;
		end
	end
	
	always @(posedge main_fifty_clock) begin
		clk_div_by_two = !clk_div_by_two;
	end

	always @(negedge main_fifty_clock) begin
		clk_div_by_two_oneeighty = !clk_div_by_two_oneeighty;
	end
	
	always @(posedge clk_div_by_two_oneeighty) begin
		clk_div_by_four = !clk_div_by_four;
	end
	
	always @(posedge clk_div_by_four) begin
		clk_div_by_eight = !clk_div_by_eight;
	end
	
	//-----------------------------------------------------------------------------------
	//
	// Keep track of what is on the LED display
	//
	//-----------------------------------------------------------------------------------
	
	reg [7:0] led_display_bytes [3:0];
	reg [17:0] digit_blanker_1 = 0;
	reg [17:0] digit_blanker_2 = 0;
	reg [17:0] digit_blanker_3 = 0;
	reg [17:0] digit_blanker_4 = 0;

	reg [7:0] led_segment_bus_latch;
	reg [3:0] led_digit_select_latch;
	
	always @(negedge clk_div_by_eight) begin
		led_segment_bus_latch = led_segment_bus;
		led_digit_select_latch = led_digit_select;

		if (led_digit_select_latch[0] == 0) begin
			led_display_bytes[0] = led_segment_bus_latch;
			digit_blanker_1 = 0;
			digit_blanker_2 = digit_blanker_2 + 1;
			digit_blanker_3 = digit_blanker_3 + 1;
			digit_blanker_4 = digit_blanker_4 + 1;
		end
		
		if (led_digit_select_latch[1] == 0) begin
			led_display_bytes[1] = led_segment_bus_latch;
			digit_blanker_1 = digit_blanker_1 + 1;
			digit_blanker_2 = 0;
			digit_blanker_3 = digit_blanker_3 + 1;
			digit_blanker_4 = digit_blanker_4 + 1;
		end
		
		if (led_digit_select_latch[2] == 0) begin
			led_display_bytes[2] = led_segment_bus_latch;
			digit_blanker_1 = digit_blanker_1 + 1;
			digit_blanker_2 = digit_blanker_2 + 1;
			digit_blanker_3 = 0;
			digit_blanker_4 = digit_blanker_4 + 1;
		end
		
		if (led_digit_select_latch[3] == 0) begin
			led_display_bytes[3] = led_segment_bus_latch;
			digit_blanker_1 = digit_blanker_1 + 1;
			digit_blanker_2 = digit_blanker_2 + 1;
			digit_blanker_3 = digit_blanker_3 + 1;
			digit_blanker_4 = 0;
		end

		if (digit_blanker_1 > 128000) begin
			led_display_bytes[0] = 255;
		end
		
		if (digit_blanker_2 > 128000) begin
			led_display_bytes[1] = 255;
		end
		
		if (digit_blanker_3 > 128000) begin
			led_display_bytes[2] = 255;
		end
		
		if (digit_blanker_4 > 128000) begin
			led_display_bytes[3] = 255;
		end
	end
	
	//-----------------------------------------------------------------------------------
	//
	// Instantiate the data storage RAM for signal processing
	//
	//-----------------------------------------------------------------------------------

	reg data_storage_remote_enable = 0;
	wire data_storage_clka;
	wire [7:0] data_storage_dina;
	wire [(RAM_ADDR_BITS-1):0] data_storage_addra;
	wire data_storage_write_enable;
	wire [7:0] data_storage_data_out;
	
	reg [7:0] data_storage_dina_reg;
	reg [(RAM_ADDR_BITS-1):0] data_storage_addra_reg;
	reg data_storage_write_enable_reg;

	data_storage #(RAM_ADDR_BITS) data_storage(.clka(data_storage_clka), .dina(data_storage_dina), .addra(data_storage_addra), 
		.wea(data_storage_write_enable), .douta(data_storage_data_out));
	
	assign data_storage_clka = (data_storage_remote_enable) ? main_fifty_clock : sram_clock_in;
	assign data_storage_dina = (data_storage_remote_enable) ? data_storage_dina_reg : sram_data_in;
	assign data_storage_addra = (data_storage_remote_enable) ? data_storage_addra_reg : sram_address_in;
	assign data_storage_write_enable = (data_storage_remote_enable) ? data_storage_write_enable_reg : sram_wren_in;
	
	assign sram_data_out = data_storage_data_out;
	
	// -----------------------------------------------------------------------------------------------
	//
	// Here is the serial receiver and transmitter
	//
	// -----------------------------------------------------------------------------------------------

	reg [7:0] transmit_all_data_state = 0;

	wire RxD_data_ready;
	wire [7:0] RxD_data;
	wire RxD_endofpacket;
	wire RxD_idle;

	reg TxD_start;
	reg [7:0] TxD_data;
	wire TxD_busy;
	wire [4:0] state;
	
	reg [7:0] transmitter_4_bit_state = 0;
	reg [7:0] transmitter_8_bit_state = 0;
	reg [15:0] transmitter_16_bit_state = 0;
	reg [7:0] transmitter_main_state = 0;
	reg [7:0] transmitter_input_state = 0;

	async_transmit asyncTX(.clk(clk_div_by_two), .TxD_start(TxD_start), .TxD_data(TxD_data), .TxD(serial_port_transmitter), .TxD_busy(TxD_busy), .state(state));
	async_receiver asyncRX(.clk(clk_div_by_two), .RxD(serial_port_receiver), .RxD_data_ready(RxD_data_ready), .RxD_data(RxD_data), .RxD_endofpacket(RxD_endofpacket), .RxD_idle(RxD_idle));

	reg tx_toggle = 0;
	
	reg transmit_4_bit_status = 0;
	reg transmit_4_bit_status_done = 0;
	
	reg transmit_8_bit_status = 0;
	reg transmit_8_bit_status_done = 0;
	
	reg transmit_16_bit_status = 0;
	reg transmit_16_bit_pass_two = 0;
	reg transmit_16_bit_status_done = 0;
	
	reg transmit_main_status = 0;
	reg transmit_main_status_done = 0;

	reg transmit_dsp_ram_size = 0;
	reg transmit_dsp_ram_size_done = 0;
	
	reg transmit_input_status = 0;
	reg transmit_input_status_done = 0;
	
	reg transmit_lcd_status = 0;
	reg transmit_lcd_status_done = 0;
	reg [7:0] transmit_lcd_status_counter = 0;
	
	reg enable_remote_access_input = 1;
	reg remote_access_input_enable_prev = 0;
	
	reg [7:0] lcd_display_string [31:0];
	
	reg transmit_dsp_status = 0;
	reg transmit_dsp_status_done = 0;
	reg transmit_dsp_status_holdoff = 0;
	reg [RAM_ADDR_BITS:0] transmit_dsp_status_counter = 0;
	
	reg transmit_led_status = 0;
	reg transmit_led_status_done = 0;
	reg [7:0] transmit_led_status_counter = 0;

	reg transmit_dsp_rx_complete = 0;
	reg transmit_dsp_rx_complete_done = 0;
	
	// Transmit!
	always @(posedge clk_div_by_two) begin
		transmitter_4_bit_state = remote_access_4_bit_output;
		transmitter_8_bit_state = remote_access_8_bit_output;
		transmitter_16_bit_state = remote_access_16_bit_output;
		
		transmitter_main_state = 0;
		transmitter_main_state[0] = enable_remote_access_input;
		
		transmitter_input_state = local_input;
	
		if (seize_serial_tx == 1) begin
			TxD_start = serial_tx_strobe;
			TxD_data = serial_tx_data;
		end else begin
			if (tx_toggle == 0) begin
				if ((transmit_4_bit_status == 1) && (transmit_4_bit_status_done == 0)) begin
					TxD_data = transmitter_4_bit_state;

					TxD_start = 1;
					tx_toggle = 1;
					
					transmit_4_bit_status_done = 1;
				end
				
				if ((transmit_8_bit_status == 1) && (transmit_8_bit_status_done == 0)) begin
					TxD_data = transmitter_8_bit_state;

					TxD_start = 1;
					tx_toggle = 1;
					
					transmit_8_bit_status_done = 1;
				end
				
				if ((transmit_16_bit_status == 1) && (transmit_16_bit_status_done == 0)) begin
					if (transmit_16_bit_pass_two == 0) begin
						TxD_data = transmitter_16_bit_state[15:8];
						
						TxD_start = 1;
						tx_toggle = 1;	
						
						transmit_16_bit_pass_two = 1;
					end else begin
						TxD_data = transmitter_16_bit_state[7:0];
						
						TxD_start = 1;
						tx_toggle = 1;	
						
						transmit_16_bit_status_done = 1;
					end
				end
				
				if ((transmit_main_status == 1) && (transmit_main_status_done == 0)) begin
					TxD_data = transmitter_main_state;

					TxD_start = 1;
					tx_toggle = 1;
					
					transmit_main_status_done = 1;
				end

				if ((transmit_dsp_ram_size == 1) && (transmit_dsp_ram_size_done == 0)) begin
					TxD_data = RAM_ADDR_BITS;

					TxD_start = 1;
					tx_toggle = 1;
					
					transmit_dsp_ram_size_done = 1;
				end
				
				if ((transmit_input_status == 1) && (transmit_input_status_done == 0)) begin
					TxD_data = transmitter_input_state;

					TxD_start = 1;
					tx_toggle = 1;
					
					transmit_input_status_done = 1;
				end
				
				if ((transmit_lcd_status == 1) && (transmit_lcd_status_done == 0)) begin
					TxD_data = lcd_display_string[transmit_lcd_status_counter];

					TxD_start = 1;
					tx_toggle = 1;
					
					transmit_lcd_status_counter = transmit_lcd_status_counter + 1;
					if (transmit_lcd_status_counter > 31) begin
						transmit_lcd_status_done = 1;
					end
				end
				
				if ((transmit_led_status == 1) && (transmit_led_status_done == 0)) begin
					TxD_data = led_display_bytes[transmit_led_status_counter];

					TxD_start = 1;
					tx_toggle = 1;
					
					transmit_led_status_counter = transmit_led_status_counter + 1;
					if (transmit_led_status_counter > 3) begin
						transmit_led_status_done = 1;
					end
				end

				if ((transmit_dsp_rx_complete == 1) && (transmit_dsp_rx_complete_done == 0)) begin
					TxD_data = 77;

					TxD_start = 1;
					tx_toggle = 1;
					
					transmit_dsp_rx_complete_done = 1;
				end
				
				if ((transmit_dsp_status == 1) && (transmit_dsp_rx_complete == 0) && (transmit_dsp_status_done == 0)) begin
					if (transmit_dsp_status_holdoff == 0) begin
						transmit_dsp_status_holdoff = 1;
						data_storage_write_enable_reg = 0;
						data_storage_addra_reg = 0;			// Initial data value
					end else begin
						data_storage_write_enable_reg = 0;
						TxD_data = data_storage_data_out;

						TxD_start = 1;
						tx_toggle = 1;
						
						transmit_dsp_status_counter = transmit_dsp_status_counter + 1;
						data_storage_addra_reg = transmit_dsp_status_counter[(RAM_ADDR_BITS-1):0];
						if (transmit_dsp_status_counter >= (2**RAM_ADDR_BITS)) begin
							transmit_dsp_status_done = 1;
							data_storage_write_enable_reg = 1'bz;
							data_storage_addra_reg = {(RAM_ADDR_BITS){1'bz}};
						end
					end
				end
			end else begin
				if (state == 5'b10000) begin	// Wait for transmission of byte to complete
					TxD_start = 0;
					tx_toggle = 0;
				end
			end
		end
		
		if (transmit_4_bit_status == 0) begin
			transmit_4_bit_status_done = 0;
		end
		
		if (transmit_8_bit_status == 0) begin
			transmit_8_bit_status_done = 0;
		end
		
		if (transmit_16_bit_status == 0) begin
			transmit_16_bit_pass_two = 0;
			transmit_16_bit_status_done = 0;
		end
		
		if (transmit_main_status == 0) begin
			transmit_main_status_done = 0;
		end

		if (transmit_dsp_ram_size == 0) begin
			transmit_dsp_ram_size_done = 0;
		end
		
		if (transmit_input_status == 0) begin
			transmit_input_status_done = 0;
		end
		
		if (transmit_lcd_status == 0) begin
			transmit_lcd_status_done = 0;
			transmit_lcd_status_counter = 0;
		end
		
		if (transmit_led_status == 0) begin
			transmit_led_status_done = 0;
			transmit_led_status_counter = 0;
		end

		if (transmit_dsp_rx_complete == 0) begin
			transmit_dsp_rx_complete_done = 0;
		end
		
		if (transmit_dsp_status == 0) begin
			transmit_dsp_status_done = 0;
			transmit_dsp_status_holdoff = 0;
			transmit_dsp_status_counter = 0;
		end
	end

	reg [7:0] lcd_display_initialization_state = 0;
	reg serial_character_received = 0;
	reg [7:0] serial_receiver_toggler = 0;
	reg [7:0] serial_command_buffer = 0;
	reg [2:0] next_byte_is_command = 0;
	reg [7:0] next_byte_is_command_prev_command = 0;
	reg [7:0] serial_command_timer = 0;
	reg update_lcd_display = 0;
	reg [7:0] serial_update_counter = 0;
	reg [RAM_ADDR_BITS:0] dsp_update_counter = 0;
	reg [7:0] received_lcd_display_string [31:0];
	reg data_write_timer = 0;
	reg waiting_on_dsp_processing = 0;
	
	// Receive serial commands
	always @(posedge clk_div_by_two) begin
		if (startup_needed == 1) begin
			startup_needed = 0;
			transmit_dsp_status = 1;
		end
	
		if (lcd_data_in_enable == 1) begin
			received_lcd_display_string[lcd_data_in_address] = lcd_data_in_data;
			update_lcd_display = 1;
			serial_command_timer = 255;
		end
	
		if ((remote_access_input_enable == 1) && (remote_access_input_enable_prev == 0)) begin
			enable_remote_access_input = !enable_remote_access_input;
		end
		remote_access_input_enable_prev = remote_access_input_enable;
	
		if (enable_remote_access_input == 0) begin
			// Enable local input
			remote_access_8_bit_input_reg = local_input;
		end
	
		if (serial_command_timer > 0) begin
			serial_command_timer = serial_command_timer - 1;
		end else begin
			update_lcd_display = 0;
		end
		
		if (transmit_4_bit_status_done == 1) begin
			transmit_4_bit_status = 0;
			if (transmit_all_data_state == 1) begin
				transmit_8_bit_status = 1;
			end
		end
		
		if (transmit_8_bit_status_done == 1) begin
			transmit_8_bit_status = 0;
			if (transmit_all_data_state == 1) begin
				transmit_16_bit_status = 1;
			end
		end
		
		if (transmit_16_bit_status_done == 1) begin
			transmit_16_bit_status = 0;
			if (transmit_all_data_state == 1) begin
				transmit_led_status = 1;
			end
		end
		
		if (transmit_led_status_done == 1) begin
			transmit_led_status = 0;
			if (transmit_all_data_state == 1) begin
				transmit_all_data_state = 0;
			end
		end

		if (transmit_dsp_rx_complete_done == 1) begin
			transmit_dsp_rx_complete = 0;
		end
		
		if (transmit_main_status_done == 1) begin
			transmit_main_status = 0;
			if (transmit_all_data_state == 1) begin
				transmit_dsp_ram_size = 1;
			end
		end

		if (transmit_dsp_ram_size_done == 1) begin
			transmit_dsp_ram_size = 0;
			if (transmit_all_data_state == 1) begin
				transmit_4_bit_status = 1;
			end
		end
		
		if (transmit_input_status_done == 1) begin
			transmit_input_status = 0;
		end
		
		if (transmit_lcd_status_done == 1) begin
			transmit_lcd_status = 0;
			if (transmit_all_data_state == 1) begin
				transmit_main_status = 1;
			end
		end
		
		if (transmit_dsp_status_done == 1) begin
			transmit_dsp_status = 0;
			data_storage_remote_enable = 0;
		end
		
		if (transmit_dsp_status == 1) begin
			data_storage_remote_enable = 1;
		end
		
		if (data_write_timer > 1) begin
			data_write_timer = data_write_timer - 1;
		end else begin
			if (data_write_timer == 1) begin
				data_storage_write_enable_reg = 0;
				data_write_timer = 0;
			end
		end
		
		if ((waiting_on_dsp_processing == 1) && (sram_processing_done == 1)) begin
			waiting_on_dsp_processing = 0;
			transmit_dsp_status = 1;
		end
	
		serial_rx_strobe_reg = 0;		// Make sure that this get reset!
		
		if ((sram_processing_done == 1) && (sram_available_reg == 1)) begin
			sram_available_reg = 0;
			transmit_dsp_status = 1;
		end
	
		if (RxD_data_ready == 1) begin
			if (serial_character_received == 0) begin
				serial_rx_data_reg = RxD_data;
				serial_rx_strobe_reg = 1;		// Signal new data...
				if (seize_serial_tx == 0) begin
					if (next_byte_is_command_prev_command == 77) begin
						// DSP input data
						if (dsp_update_counter < (2**RAM_ADDR_BITS)) begin
							data_storage_remote_enable = 1;
							data_storage_addra_reg = dsp_update_counter[(RAM_ADDR_BITS-1):0];
							data_storage_dina_reg = serial_rx_data_reg;
							data_storage_write_enable_reg = 1;
							data_write_timer = 3;
							dsp_update_counter = dsp_update_counter + 1;
							
							// TESTING ONLY!!!
							//if (dsp_update_counter < 17) begin
							//	received_lcd_display_string[dsp_update_counter - 1] = serial_command_buffer;
							//end

							if (dsp_update_counter >= (2**RAM_ADDR_BITS)) begin
								next_byte_is_command = 0;
								data_storage_write_enable_reg = 0;
								data_storage_remote_enable = 0;
								sram_available_reg = 1;
								data_storage_write_enable_reg = 1'bz;
								data_storage_addra_reg = {(RAM_ADDR_BITS){1'bz}};
								waiting_on_dsp_processing = 1;
								transmit_dsp_rx_complete = 1;
								next_byte_is_command_prev_command = 0;
								
								// TESTING ONLY!!!
								//transmit_dsp_status = 1;
							end
						end
					end else begin
						// Parse the command and see what it is
						serial_character_received = 1;
						if (serial_rx_data_reg == 13) begin
							// Carriage Return!  The serial_command_buffer holds the command!  Parse it!
							if (next_byte_is_command == 0) begin
								if (serial_command_buffer == 65) begin
									// Display update requested
									next_byte_is_command = 1;
									serial_update_counter = 0;
									next_byte_is_command_prev_command = 65;
								end
								
								if (serial_command_buffer == 66) begin
									// 8 bit input update
									if (enable_remote_access_input == 1) begin
										next_byte_is_command = 1;
										serial_update_counter = 0;
										next_byte_is_command_prev_command = 66;
									end
								end
								
								if (serial_command_buffer == 67) begin
									// 16 bit input update
									next_byte_is_command = 1;
									serial_update_counter = 0;
									next_byte_is_command_prev_command = 67;
								end
								
								if (serial_command_buffer == 68) begin
									// 8 bit output status
									transmit_8_bit_status = 1;
								end
								
								if (serial_command_buffer == 69) begin
									// 16 bit output status
									transmit_16_bit_status = 1;
								end
								
								if (serial_command_buffer == 70) begin
									// System status
									transmit_main_status = 1;
								end
								
								if (serial_command_buffer == 71) begin
									// Simulate center button press
									enable_remote_access_input = !enable_remote_access_input;
								end
								
								if (serial_command_buffer == 72) begin
									// Local input status
									transmit_input_status = 1;
								end
								
								if (serial_command_buffer == 73) begin
									// 4 bit input update
									if (enable_remote_access_input == 1) begin
										next_byte_is_command = 1;
										serial_update_counter = 0;
										next_byte_is_command_prev_command = 73;
									end
								end
								
								if (serial_command_buffer == 74) begin
									// 4 bit output status
									transmit_4_bit_status = 1;
								end
								
								if (serial_command_buffer == 75) begin
									// Transmit the contents of the LCD...
									transmit_lcd_status = 1;
								end
								
								if (serial_command_buffer == 76) begin
									// Transmit the contents of the LCD...
									transmit_all_data_state = 1;
									transmit_lcd_status = 1;
								end
								
								if (serial_command_buffer == 77) begin
									// Receive offline DSP data
									next_byte_is_command = 1;
									dsp_update_counter = 0;
									next_byte_is_command_prev_command = 77;
								end
								
								if (serial_command_buffer == 78) begin
									// Transmit the contents of RAM...
									transmit_dsp_status = 1;
								end

								if (serial_command_buffer == 79) begin
									// Transmit the DSP RAM size
									transmit_dsp_ram_size = 1;
								end
							end else begin
								if (next_byte_is_command == 1) begin
									// The previous byte was the command--now load in the data!
									if (next_byte_is_command_prev_command == 65) begin
										if (serial_update_counter < 32) begin
											received_lcd_display_string[serial_update_counter] = serial_command_buffer;
											serial_update_counter = serial_update_counter + 1;
										end else begin
											update_lcd_display = 1;
											serial_command_timer = 255;
											next_byte_is_command = 0;
										end
									end
									
									// 4 bit input update
									if (next_byte_is_command_prev_command == 73) begin
										remote_access_4_bit_input_reg = serial_command_buffer;
										next_byte_is_command = 0;
									end
									
									// 8 bit input update
									if (next_byte_is_command_prev_command == 66) begin
										remote_access_8_bit_input_reg = serial_command_buffer;
										next_byte_is_command = 0;
									end
									
									// 16 bit input update
									if (next_byte_is_command_prev_command == 67) begin
										if (serial_update_counter == 0) begin
											remote_access_16_bit_input_reg[15:8] = serial_command_buffer;
											serial_update_counter = 1;
										end else begin
											remote_access_16_bit_input_reg[7:0] = serial_command_buffer;
											next_byte_is_command = 0;
										end
									end
								end
							end
						end
					end
				end
				
				//if (RxD_data != 10) begin		// Ignore linefeeds
					serial_command_buffer = RxD_data;
				//end
			
				serial_receiver_toggler = serial_receiver_toggler + 1;
			end
		end
		
		if (RxD_data_ready == 0) begin
			serial_character_received = 0;
		end
	end
	
	//-----------------------------------------------------------------------------------
	//
	// This routine will display the contents of lcd_display_string on the LCD display
	//
	//-----------------------------------------------------------------------------------
	
	reg [15:0] lcd_display_wait_counter = 0;
	reg [7:0] lcd_display_current_character = 0;
	reg lcd_display_line_two = 0;		// Are we trying to write to line two?
	
	always @(posedge four_mhz_clk) begin	
		case (lcd_display_initialization_state)
			// Initialize the display according to the reference manual
			0:begin
				// Set up the default display...
				lcd_display_string[0] = 73;		// I
				lcd_display_string[1] = 110;		// n
				lcd_display_string[2] = 105;		// i
				lcd_display_string[3] = 116;		// t
				lcd_display_string[4] = 105;		// i
				lcd_display_string[5] = 97;		// a
				lcd_display_string[6] = 108;		// l
				lcd_display_string[7] = 105;		// i
				lcd_display_string[8] = 122;		// z
				lcd_display_string[9] = 97;		// a
				lcd_display_string[10] = 116;	// t
				lcd_display_string[11] = 105;	// i
				lcd_display_string[12] = 111;	// o
				lcd_display_string[13] = 110;	// n
				lcd_display_string[14] = 32;		// <blank>
				lcd_display_string[15] = 32;		// <blank>
				lcd_display_string[16] = 79;		// O
				lcd_display_string[17] = 75;		// k
				lcd_display_string[18] = 32;		// <blank>
				lcd_display_string[19] = 32;		// <blank>
				lcd_display_string[20] = 32;		// <blank>
				lcd_display_string[21] = 32;		// <blank>
				lcd_display_string[22] = 32;		// <blank>
				lcd_display_string[23] = 32;		// <blank>
				lcd_display_string[24] = 32;		// <blank>
				lcd_display_string[25] = 32;		// <blank>
				lcd_display_string[26] = 32;		// <blank>
				lcd_display_string[27] = 32;		// <blank>
				lcd_display_string[28] = 32;		// <blank>
				lcd_display_string[29] = 32;		// <blank>
				lcd_display_string[30] = 32;		// <blank>
				lcd_display_string[31] = 32;		// <blank>
			
				lcd_display_current_character = 0;
				
				lcd_display_line_two = 0;
				remote_access_lcd_data_out_reg = 3;
				remote_access_lcd_enable_out_reg = 1;
				remote_access_lcd_rs_out_reg = 0;
				remote_access_lcd_rw_out_reg = 0;
				lcd_display_wait_counter = 17083;	// Wait 15mS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			1:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			2:begin
				remote_access_lcd_data_out_reg = 3;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 417;		// Wait 100uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			3:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			4:begin
				remote_access_lcd_data_out_reg = 3;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 167;		// Wait 40uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			5:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			6:begin
				remote_access_lcd_data_out_reg = 2;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 167;		// Wait 40uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			7:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			// Display is now initialized
			
			// Send Function Set command
			8:begin
				remote_access_lcd_data_out_reg = 2;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 5;		// Wait 1uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			9:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			10:begin
				remote_access_lcd_data_out_reg = 8;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 167;		// Wait 40uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			11:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			
			// Send Entry Mode Set command
			12:begin
				remote_access_lcd_data_out_reg = 0;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 5;		// Wait 1uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			13:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			14:begin
				remote_access_lcd_data_out_reg = 6;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 167;		// Wait 40uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			15:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			
			// Send Display On command and disable cursors and blinking
			16:begin
				remote_access_lcd_data_out_reg = 0;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 5;		// Wait 1uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			17:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			18:begin
				remote_access_lcd_data_out_reg = 12;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 167;		// Wait 40uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			19:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			
			// Send Clear Display command
			20:begin
				remote_access_lcd_data_out_reg = 0;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 5;		// Wait 1uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			21:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			22:begin
				remote_access_lcd_data_out_reg = 1;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 6833;		// Wait 1.64uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			23:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			
			// Set DD RAM Address to 0 if lcd_display_line_two is 0, or 0x40 if it is 1
			24:begin
				remote_access_lcd_rs_out_reg = 0;
				if (lcd_display_line_two == 0) begin
					remote_access_lcd_data_out_reg = 8;
				end
				if (lcd_display_line_two == 1) begin
					remote_access_lcd_data_out_reg = 12;
				end
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 5;		// Wait 1uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			25:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			26:begin
				remote_access_lcd_data_out_reg = 0;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 167;		// Wait 40uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			27:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			
			// Display the characters
			28:begin
				remote_access_lcd_data_out_reg = lcd_display_string[lcd_display_current_character][7:4];
				remote_access_lcd_rs_out_reg = 1;
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 5;		// Wait 1uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			29:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			30:begin
				remote_access_lcd_data_out_reg = lcd_display_string[lcd_display_current_character][3:0];
				remote_access_lcd_enable_out_reg = 1;
				lcd_display_wait_counter = 167;		// Wait 40uS
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			31:begin
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_wait_counter = lcd_display_wait_counter - 1;
				if (lcd_display_wait_counter == 0) lcd_display_initialization_state = lcd_display_initialization_state + 1;
			end
			32:begin
				lcd_display_current_character = lcd_display_current_character + 1;
				lcd_display_initialization_state = lcd_display_initialization_state + 1;
				if (lcd_display_current_character < 32) begin
					lcd_display_initialization_state = 28;
				end
				if (lcd_display_current_character == 16) begin
					if (lcd_display_line_two == 0) begin
						lcd_display_line_two = 1;
						lcd_display_initialization_state = 24;
					end
				end
			end
			33:begin
				// End!
				remote_access_lcd_rs_out_reg = 0;
				remote_access_lcd_enable_out_reg = 0;
				lcd_display_line_two = 0;
				lcd_display_current_character = 0;
				if (update_lcd_display == 1) begin
					lcd_display_line_two = 0;
					lcd_display_current_character = 0;
					lcd_display_initialization_state = 24;
					lcd_display_string[0] = received_lcd_display_string[0];
					lcd_display_string[1] = received_lcd_display_string[1];
					lcd_display_string[2] = received_lcd_display_string[2];
					lcd_display_string[3] = received_lcd_display_string[3];
					lcd_display_string[4] = received_lcd_display_string[4];
					lcd_display_string[5] = received_lcd_display_string[5];
					lcd_display_string[6] = received_lcd_display_string[6];
					lcd_display_string[7] = received_lcd_display_string[7];
					lcd_display_string[8] = received_lcd_display_string[8];
					lcd_display_string[9] = received_lcd_display_string[9];
					lcd_display_string[10] = received_lcd_display_string[10];
					lcd_display_string[11] = received_lcd_display_string[11];
					lcd_display_string[12] = received_lcd_display_string[12];
					lcd_display_string[13] = received_lcd_display_string[13];
					lcd_display_string[14] = received_lcd_display_string[14];
					lcd_display_string[15] = received_lcd_display_string[15];
					lcd_display_string[16] = received_lcd_display_string[16];
					lcd_display_string[17] = received_lcd_display_string[17];
					lcd_display_string[18] = received_lcd_display_string[18];
					lcd_display_string[19] = received_lcd_display_string[19];
					lcd_display_string[20] = received_lcd_display_string[20];
					lcd_display_string[21] = received_lcd_display_string[21];
					lcd_display_string[22] = received_lcd_display_string[22];
					lcd_display_string[23] = received_lcd_display_string[23];
					lcd_display_string[24] = received_lcd_display_string[24];
					lcd_display_string[25] = received_lcd_display_string[25];
					lcd_display_string[26] = received_lcd_display_string[26];
					lcd_display_string[27] = received_lcd_display_string[27];
					lcd_display_string[28] = received_lcd_display_string[28];
					lcd_display_string[29] = received_lcd_display_string[29];
					lcd_display_string[30] = received_lcd_display_string[30];
					lcd_display_string[31] = received_lcd_display_string[31];
				end
			end
		endcase
	end
endmodule

module async_receiver(clk, RxD, RxD_data_ready, RxD_data, RxD_endofpacket, RxD_idle);
	input clk, RxD;
	output RxD_data_ready;  // onc clock pulse when RxD_data is valid
	output [7:0] RxD_data;
	
	parameter ClkFrequency = 25000000;	// 25MHz
	parameter Baud = 115200;
	
	// We also detect if a gap occurs in the received stream of characters
	// That can be useful if multiple characters are sent in burst
	//  so that multiple characters can be treated as a "packet"
	output RxD_endofpacket;  // one clock pulse, when no more data is received (RxD_idle is going high)
	output RxD_idle;  // no data is being received
	
	// Baud generator (we use 8 times oversampling)
	parameter Baud8 = Baud*8;
	parameter Baud8GeneratorAccWidth = 16;
	wire [Baud8GeneratorAccWidth:0] Baud8GeneratorInc = ((Baud8<<(Baud8GeneratorAccWidth-7))+(ClkFrequency>>8))/(ClkFrequency>>7);
	reg [Baud8GeneratorAccWidth:0] Baud8GeneratorAcc;
	always @(posedge clk) Baud8GeneratorAcc <= Baud8GeneratorAcc[Baud8GeneratorAccWidth-1:0] + Baud8GeneratorInc;
	wire Baud8Tick = Baud8GeneratorAcc[Baud8GeneratorAccWidth];
	
	////////////////////////////
	reg [1:0] RxD_sync_inv;
	always @(posedge clk) if(Baud8Tick) RxD_sync_inv <= {RxD_sync_inv[0], ~RxD};
	// we invert RxD, so that the idle becomes "0", to prevent a phantom character to be received at startup
	
	reg [1:0] RxD_cnt_inv;
	reg RxD_bit_inv;
	
	always @(posedge clk)
	if(Baud8Tick)
	begin
		if( RxD_sync_inv[1] && RxD_cnt_inv!=2'b11) RxD_cnt_inv <= RxD_cnt_inv + 2'h1;
		else 
		if(~RxD_sync_inv[1] && RxD_cnt_inv!=2'b00) RxD_cnt_inv <= RxD_cnt_inv - 2'h1;
	
		if(RxD_cnt_inv==2'b00) RxD_bit_inv <= 1'b0;
		else
		if(RxD_cnt_inv==2'b11) RxD_bit_inv <= 1'b1;
	end
	
	reg [3:0] state;
	reg [3:0] bit_spacing;
	
	// "next_bit" controls when the data sampling occurs
	// depending on how noisy the RxD is, different values might work better
	// with a clean connection, values from 8 to 11 work
	wire next_bit = (bit_spacing==4'd10);
	
	always @(posedge clk)
	if(state==0)
		bit_spacing <= 4'b0000;
	else
	if(Baud8Tick)
		bit_spacing <= {bit_spacing[2:0] + 4'b0001} | {bit_spacing[3], 3'b000};
	
	always @(posedge clk)
	if(Baud8Tick)
	case(state)
		4'b0000: if(RxD_bit_inv) state <= 4'b1000;  // start bit found?
		4'b1000: if(next_bit) state <= 4'b1001;  // bit 0
		4'b1001: if(next_bit) state <= 4'b1010;  // bit 1
		4'b1010: if(next_bit) state <= 4'b1011;  // bit 2
		4'b1011: if(next_bit) state <= 4'b1100;  // bit 3
		4'b1100: if(next_bit) state <= 4'b1101;  // bit 4
		4'b1101: if(next_bit) state <= 4'b1110;  // bit 5
		4'b1110: if(next_bit) state <= 4'b1111;  // bit 6
		4'b1111: if(next_bit) state <= 4'b0001;  // bit 7
		4'b0001: if(next_bit) state <= 4'b0000;  // stop bit
		default: state <= 4'b0000;
	endcase
	
	reg [7:0] RxD_data;
	always @(posedge clk)
	if(Baud8Tick && next_bit && state[3]) RxD_data <= {~RxD_bit_inv, RxD_data[7:1]};
	
	reg RxD_data_ready;
	always @(posedge clk)
	begin
		RxD_data_ready <= (Baud8Tick && next_bit && state==4'b0001 && ~RxD_bit_inv);  // ready only if the stop bit is received
	end
	
	reg [4:0] gap_count;
	always @(posedge clk) if (state!=0) gap_count<=5'h00; else if(Baud8Tick & ~gap_count[4]) gap_count <= gap_count + 5'h01;
	assign RxD_idle = gap_count[4];
	reg RxD_endofpacket; always @(posedge clk) RxD_endofpacket <= Baud8Tick & (gap_count==5'h0F);
endmodule

module async_transmit(clk, TxD_start, TxD_data, TxD, TxD_busy, state);
	input clk, TxD_start;
	input [7:0] TxD_data;
	output TxD, TxD_busy;
	output [4:0] state;
	parameter ClkFrequency = 25000000;	// 25MHz
	//parameter ClkFrequency = 50000000;	// 50MHz
	parameter Baud = 115200;
	parameter RegisterInputData = 1;	// in RegisterInputData mode, the input doesn't have to stay valid while the character is been transmitted
	
	// Baud generator
	parameter BaudGeneratorAccWidth = 16;
	reg [BaudGeneratorAccWidth:0] BaudGeneratorAcc;
	`ifdef DEBUG
	wire [BaudGeneratorAccWidth:0] BaudGeneratorInc = 17'h10000;
	`else
	wire [BaudGeneratorAccWidth:0] BaudGeneratorInc = ((Baud<<(BaudGeneratorAccWidth-4))+(ClkFrequency>>5))/(ClkFrequency>>4);
	`endif
	
	wire BaudTick = BaudGeneratorAcc[BaudGeneratorAccWidth];
	wire TxD_busy;
	always @(posedge clk) if(TxD_busy) BaudGeneratorAcc <= BaudGeneratorAcc[BaudGeneratorAccWidth-1:0] + BaudGeneratorInc;
	
	// Transmitter state machine
	reg [4:0] state;
	wire TxD_ready = (state==0);
	assign TxD_busy = ~TxD_ready;
	
	reg [7:0] TxD_dataReg;
	always @(posedge clk) if(TxD_ready & TxD_start) TxD_dataReg <= TxD_data;
	wire [7:0] TxD_dataD = RegisterInputData ? TxD_dataReg : TxD_data;
	
	always @(posedge clk) begin
		if (TxD_start == 0) state <= 5'b00000;
	
		case(state)
			5'b00000: if(TxD_start) state <= 5'b00001;
			5'b00001: if(BaudTick) state <= 5'b00100;
			5'b00100: if(BaudTick) state <= 5'b01000;  // start
			5'b01000: if(BaudTick) state <= 5'b01001;  // bit 0
			5'b01001: if(BaudTick) state <= 5'b01010;  // bit 1
			5'b01010: if(BaudTick) state <= 5'b01011;  // bit 2
			5'b01011: if(BaudTick) state <= 5'b01100;  // bit 3
			5'b01100: if(BaudTick) state <= 5'b01101;  // bit 4
			5'b01101: if(BaudTick) state <= 5'b01110;  // bit 5
			5'b01110: if(BaudTick) state <= 5'b01111;  // bit 6
			5'b01111: if(BaudTick) state <= 5'b00010;  // bit 7
			5'b00010: if(BaudTick) state <= 5'b00011;  // stop1
			//4'b0011: if(BaudTick) state <= 4'b0000;  // stop2
			5'b00011: if(BaudTick) state <= 5'b10000;  // stop2
			//default: if(BaudTick) state <= 4'b0000;
		endcase
	end
	
	// Output mux
	reg muxbit;
	always @( * )
	case(state[2:0])
		3'd0: muxbit <= TxD_dataD[0];
		3'd1: muxbit <= TxD_dataD[1];
		3'd2: muxbit <= TxD_dataD[2];
		3'd3: muxbit <= TxD_dataD[3];
		3'd4: muxbit <= TxD_dataD[4];
		3'd5: muxbit <= TxD_dataD[5];
		3'd6: muxbit <= TxD_dataD[6];
		3'd7: muxbit <= TxD_dataD[7];
	endcase
	
	// Put together the start, data and stop bits
	reg TxD;
	always @(posedge clk) TxD <= (state<4) | (state[3] & muxbit) | state[4];  // register the output to make it glitch free

endmodule