-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathutil.vhd
4103 lines (3569 loc) · 120 KB
/
util.vhd
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
-------------------------------------------------------------------------------
--| @file util.vhd
--| @brief A collection of utilities and simple components. The components
--| should be synthesizable, and the functions can be used within synthesizable
--| components, unless marked with a "_tb" suffix (or if the function n_bits).
--|
--| Other modules to implement; CRC core (reuse LFSR), Count
--| Leading Zeros, Count Trailing Zeros, Manchester CODEC, Wishbone interface
--| types and Wishbone Bus Arbitrator. Also SPI, a H2 core with an eForth image
--| read to go, and UART.
--|
--| More exotic modules would include; encryption, compression, sorting networks,
--| switching networks, Reed-Solomon CODEC, Discrete Fourier Transform/Discrete
--| Cosine Transform, Pulse Width/Code/Position Modulation modules, so long as
--| they are fairly generic and synthesizable.
--|
--| Potential improvements to the library:
--| - Optional registers on either input or output, selectable by a generic
--| - Better timing models
--| - More assertions
--| - See 'A Structured VHDL design' by Jiri Gaisler,
--| <http://gaisler.com/doc/vhdl2proc.pdf> and apply methodology.
--|
--| @author Richard James Howe
--| @copyright Copyright 2017, 2019 Richard James Howe
--| @license MIT
--| @email howe.r.j.89@gmail.com
-------------------------------------------------------------------------------
library ieee, work;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use std.textio.all;
package util is
-- Not all modules will need every generic specified here, even so it
-- is easier to group the common generics in one structure.
type common_generics is record
clock_frequency: positive; -- clock frequency of module clock
delay: time; -- gate delay for simulation purposes
asynchronous_reset: boolean; -- use asynchronous reset if true
end record;
constant default_settings: common_generics := (
clock_frequency => 100_000_000,
delay => 10 ns,
asynchronous_reset => true
);
component util_tb is
generic (g: common_generics);
end component;
component clock_source_tb is
generic (g: common_generics; hold_rst: positive := 1);
port (
stop: in std_ulogic := '0';
clk: out std_ulogic;
clk_with_jitter: out std_ulogic := '0';
rst: out std_ulogic := '0');
end component;
component reg
generic (g: common_generics; N: positive);
port (
clk: in std_ulogic;
rst: in std_ulogic;
we: in std_ulogic;
di: in std_ulogic_vector(N - 1 downto 0);
do: out std_ulogic_vector(N - 1 downto 0));
end component;
component shift_register
generic (g: common_generics; N: positive);
port (
clk: in std_ulogic;
rst: in std_ulogic;
we: in std_ulogic;
di: in std_ulogic;
do: out std_ulogic;
-- optional
load_we: in std_ulogic := '0';
load_i: in std_ulogic_vector(N - 1 downto 0) := (others => '0');
load_o: out std_ulogic_vector(N - 1 downto 0));
end component;
component shift_register_tb
generic (g: common_generics);
end component;
component timer_us
generic (g: common_generics; timer_period_us: natural);
port (
clk: in std_ulogic;
rst: in std_ulogic;
co: out std_ulogic);
end component;
component timer_us_tb
generic (g: common_generics);
end component;
component rising_edge_detector is
generic (g: common_generics);
port (
clk: in std_ulogic;
rst: in std_ulogic;
di: in std_ulogic;
do: out std_ulogic);
end component;
component rising_edge_detector_tb is
generic (g: common_generics);
end component;
component rising_edge_detectors is
generic (g: common_generics; N: positive);
port (
clk: in std_ulogic;
rst: in std_ulogic;
di: in std_ulogic_vector(N - 1 downto 0);
do: out std_ulogic_vector(N - 1 downto 0));
end component;
-- NB. 'half_adder' test bench is folded in to 'full_adder_tb'
component half_adder is
generic (g: common_generics); -- simulation only
port (
a: in std_ulogic;
b: in std_ulogic;
sum: out std_ulogic;
carry: out std_ulogic);
end component;
component full_adder is
generic (g: common_generics); -- simulation only
port (
x: in std_ulogic;
y: in std_ulogic;
z: in std_ulogic;
sum: out std_ulogic;
carry: out std_ulogic);
end component;
component full_adder_tb is
generic (g: common_generics);
end component;
component fifo is
generic (g: common_generics;
data_width: positive;
fifo_depth: positive;
read_first: boolean := true);
port (
clk: in std_ulogic;
rst: in std_ulogic;
di: in std_ulogic_vector(data_width - 1 downto 0);
we: in std_ulogic;
re: in std_ulogic;
do: out std_ulogic_vector(data_width - 1 downto 0);
-- optional
full: out std_ulogic := '0';
empty: out std_ulogic := '1');
end component;
component fifo_tb is
generic (g: common_generics);
end component;
component counter is
generic (g: common_generics; N: positive);
port (
clk: in std_ulogic;
rst: in std_ulogic;
ce: in std_ulogic;
cr: in std_ulogic;
dout: out std_ulogic_vector(N - 1 downto 0);
-- optional
load_we: in std_ulogic := '0';
load_i: in std_ulogic_vector(N - 1 downto 0) := (others => '0'));
end component;
component counter_tb is
generic (g: common_generics);
end component;
component lfsr is
generic (g: common_generics; constant tap: std_ulogic_vector);
port (
clk: in std_ulogic;
rst: in std_ulogic;
ce: in std_ulogic := '1';
we: in std_ulogic;
di: in std_ulogic_vector(tap'high + 1 downto tap'low);
do: out std_ulogic_vector(tap'high + 1 downto tap'low));
end component;
component lfsr_tb is
generic (g: common_generics);
end component;
component io_pins is
generic (g: common_generics; N: positive);
port (
clk: in std_ulogic;
rst: in std_ulogic;
control: in std_ulogic_vector(N - 1 downto 0);
control_we: in std_ulogic;
din: in std_ulogic_vector(N - 1 downto 0);
din_we: in std_ulogic;
dout: out std_ulogic_vector(N - 1 downto 0);
pins: inout std_logic_vector(N - 1 downto 0)); -- NB!
end component;
component io_pins_tb is
generic (g: common_generics);
end component;
type file_format is (FILE_HEX, FILE_BINARY, FILE_NONE);
component dual_port_block_ram is
generic (g: common_generics;
addr_length: positive := 12;
data_length: positive := 16;
file_name: string := "memory.bin";
file_type: file_format := FILE_BINARY);
port (
-- port A of dual port RAM
a_clk: in std_ulogic;
a_dwe: in std_ulogic;
a_dre: in std_ulogic;
a_addr: in std_ulogic_vector(addr_length - 1 downto 0);
a_din: in std_ulogic_vector(data_length - 1 downto 0);
a_dout: out std_ulogic_vector(data_length - 1 downto 0) := (others => '0');
-- port B of dual port RAM
b_clk: in std_ulogic;
b_dwe: in std_ulogic;
b_dre: in std_ulogic;
b_addr: in std_ulogic_vector(addr_length - 1 downto 0);
b_din: in std_ulogic_vector(data_length - 1 downto 0);
b_dout: out std_ulogic_vector(data_length - 1 downto 0) := (others => '0'));
end component;
component single_port_block_ram is
generic (g: common_generics;
addr_length: positive := 12;
data_length: positive := 16;
file_name: string := "memory.bin";
file_type: file_format := FILE_BINARY);
port (
clk: in std_ulogic;
dwe: in std_ulogic;
dre: in std_ulogic;
addr: in std_ulogic_vector(addr_length - 1 downto 0);
din: in std_ulogic_vector(data_length - 1 downto 0);
dout: out std_ulogic_vector(data_length - 1 downto 0) := (others => '0'));
end component;
component data_source is
generic (g: common_generics;
addr_length: positive := 12;
data_length: positive := 16;
file_name: string := "memory.bin";
file_type: file_format := FILE_BINARY);
port (
clk: in std_ulogic;
rst: in std_ulogic;
ce: in std_ulogic := '1';
cr: in std_ulogic;
load: in std_ulogic_vector(addr_length - 1 downto 0) := (others => '0');
load_we: in std_ulogic := '0';
dout: out std_ulogic_vector(data_length - 1 downto 0));
end component;
component ucpu is
generic (
asynchronous_reset: boolean := true; -- use asynchronous reset if true, synchronous if false
delay: time := 0 ns; -- simulation only
width: positive range 8 to 32 := 8);
port (
clk, rst: in std_ulogic;
pc: out std_ulogic_vector(width - 3 downto 0);
op: in std_ulogic_vector(width - 1 downto 0);
adr: out std_ulogic_vector(width - 3 downto 0);
di: in std_ulogic_vector(width - 1 downto 0);
re, we: out std_ulogic;
do: out std_ulogic_vector(width - 1 downto 0));
end component;
component ucpu_tb is
generic (g: common_generics; file_name: string := "ucpu.bin");
end component;
component restoring_divider is
generic (g: common_generics; N: positive);
port (
clk: in std_ulogic;
rst: in std_ulogic := '0';
a: in std_ulogic_vector(N - 1 downto 0);
b: in std_ulogic_vector(N - 1 downto 0);
start: in std_ulogic;
done: out std_ulogic;
c: out std_ulogic_vector(N - 1 downto 0));
end component;
component restoring_divider_tb is
generic (g: common_generics);
end component;
component debounce_us is
generic (g: common_generics; timer_period_us: natural);
port (
clk: in std_ulogic;
di: in std_ulogic;
do: out std_ulogic);
end component;
component debounce_block_us is
generic (g: common_generics; N: positive; timer_period_us: natural);
port (
clk: in std_ulogic;
di: in std_ulogic_vector(N - 1 downto 0);
do: out std_ulogic_vector(N - 1 downto 0));
end component;
component debounce_us_tb is
generic (g: common_generics);
end component;
component state_changed is
generic (g: common_generics);
port (
clk: in std_ulogic;
rst: in std_ulogic;
di: in std_ulogic;
do: out std_ulogic);
end component;
component state_block_changed is
generic (g: common_generics; N: positive);
port (
clk: in std_ulogic;
rst: in std_ulogic;
di: in std_ulogic_vector(N - 1 downto 0);
do: out std_ulogic_vector(N - 1 downto 0));
end component;
component reset_generator is
generic (g: common_generics; reset_period_us: natural := 1);
port (
clk: in std_logic := 'X';
rst: out std_logic := '0'); -- reset out!
end component;
component reset_generator_tb is
generic (g: common_generics);
end component;
function n_bits(x: natural) return natural; -- Not synthesizable
function n_bits(x: std_ulogic_vector) return natural; -- Not synthesizable
component bit_count is
generic (g: common_generics; N: positive);
port (
bits: in std_ulogic_vector(N - 1 downto 0);
count: out std_ulogic_vector(n_bits(N) downto 0));
end component;
component bit_count_tb is
generic (g: common_generics);
end component;
component majority is
generic (g: common_generics; N: positive; even_wins: boolean := false);
port (
bits: in std_ulogic_vector(N - 1 downto 0);
vote: out std_ulogic;
tie: out std_ulogic);
end component;
component majority_tb is
generic (g: common_generics);
end component;
component delay_line is
generic (g: common_generics; width: positive; depth: natural);
port (
clk: in std_ulogic;
rst: in std_ulogic;
ce: in std_ulogic := '1';
di: in std_ulogic_vector(width - 1 downto 0);
do: out std_ulogic_vector(width - 1 downto 0));
end component;
component delay_line_tb is
generic (g: common_generics);
end component;
component gray_encoder is
generic (g: common_generics; N: positive);
port (di: in std_ulogic_vector(N - 1 downto 0);
do: out std_ulogic_vector(N - 1 downto 0));
end component;
component gray_decoder is
generic (g: common_generics; N: positive);
port (di: in std_ulogic_vector(N - 1 downto 0);
do: out std_ulogic_vector(N - 1 downto 0));
end component;
component gray_tb is
generic (g: common_generics);
end component;
component parity_module is
generic (g: common_generics; N: positive; even: boolean);
port (di: in std_ulogic_vector(N - 1 downto 0);
do: out std_ulogic);
end component;
component hamming_7_4_encoder is
generic (g: common_generics);
port (
di: in std_ulogic_vector(3 downto 0);
do: out std_ulogic_vector(6 downto 0);
parity: out std_ulogic -- parity over 'di'
);
end component;
component hamming_7_4_decoder is
generic (g: common_generics; secdec: boolean := true);
port (
di: in std_ulogic_vector(6 downto 0);
parity: in std_ulogic;
do: out std_ulogic_vector(3 downto 0);
single, double: out std_ulogic);
end component;
component hamming_7_4_tb is
generic (g: common_generics);
end component;
type vga_configuration is record
clock_frequency: positive; -- pixel clock frequency
h_pulse: integer; -- horizontal sync pulse width in pixels
h_back_porch: integer; -- horizontal back porch width in pixels
h_pixels: integer; -- horizontal display width in pixels
h_front_porch: integer; -- horizontal front porch width in pixels
h_polarity: std_ulogic; -- horizontal sync pulse polarity (1 = positive, 0 = negative)
v_pulse: integer; -- vertical sync pulse width in rows
v_back_porch: integer; -- vertical back porch width in rows
v_pixels: integer; -- vertical display width in rows
v_front_porch: integer; -- vertical front porch width in rows
v_polarity: std_ulogic; -- vertical sync pulse polarity (1 = positive, 0 = negative)
end record;
constant vga_640x480: vga_configuration := (
clock_frequency => 25_175_000,
h_pulse => 96, h_back_porch => 48, h_pixels => 640, h_front_porch => 16, h_polarity => '0',
v_pulse => 2, v_back_porch => 33, v_pixels => 480, v_front_porch => 10, v_polarity => '0');
constant vga_800x600: vga_configuration := (
clock_frequency => 40_000_000,
h_pulse => 128, h_back_porch => 88, h_pixels => 800, h_front_porch => 40, h_polarity => '1',
v_pulse => 4, v_back_porch => 23, v_pixels => 600, v_front_porch => 1, v_polarity => '1');
constant vga_1024x768: vga_configuration := (
clock_frequency => 44_900_000,
h_pulse => 176, h_back_porch => 56, h_pixels => 1024, h_front_porch => 8, h_polarity => '1',
v_pulse => 8, v_back_porch => 41, v_pixels => 800, v_front_porch => 0, v_polarity => '1');
constant vga_1920x1200: vga_configuration := (
clock_frequency => 193_160_000,
h_pulse => 208, h_back_porch => 336, h_pixels => 1920, h_front_porch => 128, h_polarity => '0',
v_pulse => 3, v_back_porch => 38, v_pixels => 1200, v_front_porch => 1, v_polarity => '1');
component vga_controller is
generic (
g: common_generics;
pixel_clock_frequency: positive := 25_000_000;
constant cfg: vga_configuration := vga_640x480);
port (
clk, rst: in std_ulogic; -- pixel clock, must run at configured frequency
h_sync, v_sync: out std_ulogic; -- sync pulses
h_blank, v_blank: out std_ulogic;
column, row: out integer); -- pixel coordinates
end component;
component vga_tb is
generic (g: common_generics; pixel_clock_frequency: positive := 25_000_000; simulation_us: time := 20000 us);
end component;
constant led_7_segment_character_length: positive := 4;
subtype led_7_segment_character is std_ulogic_vector(led_7_segment_character_length - 1 downto 0);
subtype led_7_segment is std_ulogic_vector(7 downto 0);
component led_7_segment_display is
generic (g: common_generics;
use_bcd_not_hex: boolean := true;
refresh_rate_us: natural := 1500;
number_of_led_displays: positive := 4);
port (
clk: in std_ulogic;
rst: in std_ulogic;
leds_we: in std_ulogic;
leds: in std_ulogic_vector((number_of_led_displays * led_7_segment_character_length) - 1 downto 0);
-- Physical outputs
an: out std_ulogic_vector(number_of_led_displays - 1 downto 0); -- anodes, controls on/off
ka: out std_ulogic_vector(7 downto 0)); -- cathodes, data on display
end component;
component led_7_segment_display_tb is
generic (g: common_generics);
end component;
component sine is
generic (g: common_generics; pipeline: boolean := true);
port (
clk, rst, xwe: in std_ulogic;
x: in std_ulogic_vector(15 downto 0);
s: out std_ulogic_vector(15 downto 0));
end component;
component cosine is
generic (g: common_generics; pipeline: boolean := true);
port (
clk, rst, xwe: in std_ulogic;
x: in std_ulogic_vector(15 downto 0);
s: out std_ulogic_vector(15 downto 0));
end component;
component sine_tb is
generic (g: common_generics);
end component;
function max(a: natural; b: natural) return natural;
function min(a: natural; b: natural) return natural;
function reverse (a: in std_ulogic_vector) return std_ulogic_vector;
function invert(slv:std_ulogic_vector) return std_ulogic_vector;
function parity(slv:std_ulogic_vector; even: boolean) return std_ulogic;
function parity(slv:std_ulogic_vector; even: std_ulogic) return std_ulogic;
function or_reduce(slv:std_ulogic_vector) return std_ulogic;
function and_reduce(slv:std_ulogic_vector) return std_ulogic;
function select_bit(indexed, selector: std_ulogic_vector) return std_ulogic;
function priority(order: std_ulogic_vector; high: boolean) return natural;
function priority(order: std_ulogic_vector; high: boolean) return std_ulogic_vector;
function mux(a: std_ulogic_vector; b: std_ulogic_vector; sel: std_ulogic) return std_ulogic_vector;
function mux(a: std_ulogic; b: std_ulogic; sel: std_ulogic) return std_ulogic;
function mux(a, b : std_ulogic_vector) return std_ulogic;
function decode(encoded: std_ulogic_vector) return std_ulogic_vector;
function to_std_ulogic_vector(s: string) return std_ulogic_vector;
function logical(b: boolean) return std_ulogic;
function bit_count_f(s: std_ulogic_vector) return integer;
function hex_char_to_std_ulogic_vector_tb(hc: character) return std_ulogic_vector;
type ulogic_string is array(integer range <>) of std_ulogic_vector(7 downto 0);
function to_std_ulogic_vector(s: string) return ulogic_string;
-- synthesis translate_off
subtype configuration_name is string(1 to 8);
type configuration_item is record
name: configuration_name;
value: integer;
end record;
type configuration_items is array(integer range <>) of configuration_item;
function search_configuration_tb(find_me: configuration_name; ci: configuration_items) return integer;
procedure read_configuration_tb(file_name: string; ci: inout configuration_items);
procedure write_configuration_tb(file_name: string; ci: configuration_items);
-- synthesis translate_on
end;
package body util is
function max(a: natural; b: natural) return natural is
begin
if (a > b) then return a; else return b; end if;
end function;
function min(a: natural; b: natural) return natural is
begin
if (a < b) then return a; else return b; end if;
end function;
function n_bits(x: natural) return natural is -- Not synthesizable
variable x1: natural := max(x, 1) - 1;
variable n: natural := 1;
begin
while x1 > 1 loop
x1 := x1 / 2;
n := n + 1;
end loop;
return n;
end function;
function n_bits(x: std_ulogic_vector) return natural is -- Not synthesizable
begin
return n_bits(x'high);
end function;
-- <https://stackoverflow.com/questions/13584307>
function reverse (a: in std_ulogic_vector) return std_ulogic_vector is
variable result: std_ulogic_vector(a'range);
alias aa: std_ulogic_vector(a'reverse_range) is a;
begin
for i in aa'range loop
result(i) := aa(i);
end loop;
return result;
end;
function invert(slv: std_ulogic_vector) return std_ulogic_vector is
variable z: std_ulogic_vector(slv'range);
begin
for i in slv'range loop
z(i) := not(slv(i));
end loop;
return z;
end;
function parity(slv: std_ulogic_vector; even: boolean) return std_ulogic is
variable z: std_ulogic := '0';
begin
if not even then
z := '1';
end if;
for i in slv'range loop
z := z xor slv(i);
end loop;
return z;
end;
function parity(slv:std_ulogic_vector; even: std_ulogic) return std_ulogic is
variable z: boolean := false;
begin
if even = '1' then
z := true;
end if;
return parity(slv, z);
end;
function or_reduce(slv:std_ulogic_vector) return std_ulogic is
variable z: std_ulogic := '0';
begin
for i in slv'range loop
z := z or slv(i);
end loop;
return z;
end;
function and_reduce(slv:std_ulogic_vector) return std_ulogic is
variable z: std_ulogic := '1';
begin
for i in slv'range loop
z := z and slv(i);
end loop;
return z;
end;
function select_bit(indexed, selector: std_ulogic_vector) return std_ulogic is
variable z: std_ulogic := 'X';
begin
assert n_bits(indexed) = selector'high + 1 severity failure;
for i in indexed'range loop
if i = to_integer(unsigned(selector)) then
z := indexed(i);
end if;
end loop;
return z;
end;
function priority(order: std_ulogic_vector; high: boolean) return natural is
variable p: natural := 0;
begin
if not high then
for i in order'high + 1 downto 1 loop
if order(i-1) = '1' then
p := i - 1;
end if;
end loop;
else
for i in 1 to order'high + 1 loop
if order(i-1) = '1' then
p := i - 1;
end if;
end loop;
end if;
return p;
end;
function priority(order: std_ulogic_vector; high: boolean) return std_ulogic_vector is
variable length: natural := n_bits(order'length);
begin
return std_ulogic_vector(to_unsigned(priority(order, high), length));
end;
function mux(a: std_ulogic_vector; b: std_ulogic_vector; sel: std_ulogic) return std_ulogic_vector is
variable m: std_ulogic_vector(a'range) := (others => 'X');
begin
if sel = '0' then m := a; else m := b; end if;
return m;
end;
function mux(a: std_ulogic; b: std_ulogic; sel: std_ulogic) return std_ulogic is
variable m: std_ulogic := 'X';
begin
if sel = '0' then m := a; else m := b; end if;
return m;
end;
function mux(a, b : std_ulogic_vector) return std_ulogic is
variable r: std_ulogic_vector(b'length - 1 downto 0) := (others => 'X');
variable i: integer;
begin
r := b;
i := to_integer(unsigned(a));
return r(i);
end;
function decode(encoded : std_ulogic_vector) return std_ulogic_vector is
variable r: std_ulogic_vector((2 ** encoded'length) - 1 downto 0) := (others => '0');
variable i: natural;
begin
i := to_integer(unsigned(encoded));
r(i) := '1';
return r;
end;
function logical(b: boolean) return std_ulogic is
begin
if b then return '1'; else return '0'; end if;
end;
function hex_char_to_std_ulogic_vector_tb(hc: character) return std_ulogic_vector is
variable slv: std_ulogic_vector(3 downto 0);
begin
case hc is
when '0' => slv := "0000";
when '1' => slv := "0001";
when '2' => slv := "0010";
when '3' => slv := "0011";
when '4' => slv := "0100";
when '5' => slv := "0101";
when '6' => slv := "0110";
when '7' => slv := "0111";
when '8' => slv := "1000";
when '9' => slv := "1001";
when 'A' => slv := "1010";
when 'a' => slv := "1010";
when 'B' => slv := "1011";
when 'b' => slv := "1011";
when 'C' => slv := "1100";
when 'c' => slv := "1100";
when 'D' => slv := "1101";
when 'd' => slv := "1101";
when 'E' => slv := "1110";
when 'e' => slv := "1110";
when 'F' => slv := "1111";
when 'f' => slv := "1111";
when others => slv := "XXXX";
end case;
assert (slv /= "XXXX") report " not a valid hex character: " & hc severity failure;
return slv;
end;
function bit_count_f(s : std_ulogic_vector) return integer is
variable count: natural := 0;
begin
for i in s'range loop
if s(i) = '1' then
count := count + 1;
end if;
end loop;
return count;
end;
-- <https://stackoverflow.com/questions/30519849/vhdl-convert-string-to-std-logic-vector>
function to_std_ulogic_vector(s: string) return std_ulogic_vector is
variable ret: std_ulogic_vector(s'length*8-1 downto 0);
begin
for i in s'range loop
ret(i*8+7 downto i*8) := std_ulogic_vector(to_unsigned(character'pos(s(i)), 8));
end loop;
return ret;
end;
function to_std_ulogic_vector(s: string) return ulogic_string is
variable ret: ulogic_string(s'range);
begin
for i in s'range loop
ret(i) := std_ulogic_vector(to_unsigned(character'pos(s(i)), 8));
end loop;
return ret;
end;
-- synthesis translate_off
-- Find a string in a configuration items array, or returns -1 on
-- failure to find the string.
function search_configuration_tb(find_me: configuration_name; ci: configuration_items) return integer is
begin
for i in ci'range loop
if ci(i).name = find_me then
return i;
end if;
end loop;
return -1;
end;
-- VHDL provides quite a limited set of options for dealing with
-- operations that are not synthesizeable but would be useful for
-- in test benches. This method provides a crude way of reading
-- in configurable options. It has a very strict format.
--
-- The format is line oriented, it expects a string on a line
-- with a length equal to the "configuration_name" type, which
-- is a subtype of "string". It finds the corresponding record
-- in configuration_items if it exists. It then reads in an
-- integer from the next line and sets the record for it.
--
-- Any deviation from this format causes an error and the simulation
-- to halt, whilst not a good practice to do error checking with asserts
-- there is no better way in VHDL in this case. The only sensible
-- action on an error would for the configuration file to be fixed
-- anyway.
--
-- Comment lines and variable length strings would be nice, but
-- are too much of a hassle.
--
-- The configuration function only deal with part of the configuration
-- process, it does not deal with deserialization into structures
-- more useful to the user - like into individual signals.
--
procedure read_configuration_tb(file_name: string; ci: inout configuration_items) is
file in_file: text is in file_name;
variable in_line: line;
variable d: integer;
variable s: configuration_name;
variable index: integer;
begin
while not endfile(in_file) loop
readline(in_file, in_line);
read(in_line, s);
index := search_configuration_tb(s, ci);
assert index >= 0 report "Unknown configuration item: " & s severity failure;
readline(in_file, in_line);
read(in_line, d);
ci(index).value := d;
report "Config Item: '" & ci(index).name & "' = " & integer'image(ci(index).value);
end loop;
file_close(in_file);
end procedure;
procedure write_configuration_tb(file_name: string; ci: configuration_items) is
file out_file: text is out file_name;
variable out_line: line;
begin
for i in ci'range loop
write(out_line, ci(i).name);
writeline(out_file, out_line);
write(out_line, ci(i).value);
writeline(out_file, out_line);
end loop;
end procedure;
-- synthesis translate_on
end;
------------------------- Utility Test Bench ----------------------------------------
library ieee, work;
use ieee.std_logic_1164.all;
use work.util.all;
entity util_tb is
generic (g: common_generics);
end entity;
architecture behav of util_tb is
begin
-- The "io_pins_tb" works correctly, however in GHDL 0.29, compiled under
-- Windows, fails to simulate this component correctly, resulting
-- in a crash. This does not affect the Linux build of GHDL. It has
-- something to do with 'Z' values for std_logic types.
uut_io_pins: work.util.io_pins_tb generic map (g => g);
uut_timer_us: work.util.timer_us_tb generic map (g => g);
uut_full_add: work.util.full_adder_tb generic map (g => g);
uut_fifo: work.util.fifo_tb generic map (g => g);
uut_counter: work.util.counter_tb generic map (g => g);
uut_ucpu: work.util.ucpu_tb generic map (g => g);
uut_rdivider: work.util.restoring_divider_tb generic map (g => g);
uut_debounce: work.util.debounce_us_tb generic map (g => g);
uut_rst_gen: work.util.reset_generator_tb generic map (g => g);
uut_bit_cnt: work.util.bit_count_tb generic map (g => g);
uut_majority: work.util.majority_tb generic map (g => g);
uut_delay_ln: work.util.delay_line_tb generic map (g => g);
uut_rising: work.util.rising_edge_detector_tb generic map (g => g);
uut_shiftReg: work.util.shift_register_tb generic map (g => g);
uut_lfsr: work.util.lfsr_tb generic map (g => g);
uut_gray: work.util.gray_tb generic map (g => g);
uut_ham: work.util.hamming_7_4_tb generic map (g => g); -- Oink!
uut_vga: work.util.vga_tb generic map (g => g, simulation_us => 1 us);
uut_sine: work.util.sine_tb generic map (g => g);
uut_7_seg: work.util.led_7_segment_display_tb generic map (g => g);
stimulus_process: process
begin
assert max(5, 4) = 5 severity failure;
assert work.util.min(5, 4) = 4 severity failure;
assert n_bits(1) = 1 severity failure;
assert n_bits(2) = 1 severity failure;
assert n_bits(7) = 3 severity failure;
assert n_bits(8) = 3 severity failure;
assert n_bits(9) = 4 severity failure;
assert reverse("1") = "1" severity failure;
assert reverse("0") = "0" severity failure;
assert reverse("10") = "01" severity failure;
assert reverse("11") = "11" severity failure;
assert reverse("0101") = "1010" severity failure;
assert invert("1") = "0" severity failure;
assert invert("0") = "1" severity failure;
assert invert("0101") = "1010" severity failure;
assert select_bit("01000", "01") = '1' severity failure;
assert parity("0", true) = '0' severity failure;
assert parity("1", true) = '1' severity failure;
assert parity("11", true) = '0' severity failure;
assert parity("1010001", true) = '1' severity failure;
assert parity("0", false) = '1' severity failure;
assert parity("1", false) = '0' severity failure;
assert parity("11", false) = '1' severity failure;
assert parity("1010001", false) = '0' severity failure;
assert or_reduce("0000") = '0' severity failure;
assert or_reduce("0") = '0' severity failure;
assert or_reduce("1") = '1' severity failure;
assert or_reduce("11") = '1' severity failure;
assert or_reduce("10") = '1' severity failure;
assert or_reduce("01") = '1' severity failure;
assert and_reduce("01") = '0' severity failure;
assert and_reduce("11") = '1' severity failure;
assert and_reduce("1") = '1' severity failure;
assert and_reduce("0") = '0' severity failure;
assert and_reduce("10") = '0' severity failure;
assert priority("01001", false) = 1 severity failure;
assert mux("1010", "0101", '0') = "1010" severity failure;
assert mux("1010", "0101", '1') = "0101" severity failure;
assert decode("00") = "0001" severity failure;
assert decode("01") = "0010" severity failure;
assert decode("10") = "0100" severity failure;
assert decode("11") = "1000" severity failure;
wait;
end process;
end architecture;
------------------------- Function Test Bench ---------------------------------------
------------------------- Test bench clock source -----------------------------------
library ieee, work;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.math_real.all;
use work.util.common_generics;
entity clock_source_tb is
generic (g: common_generics; hold_rst: positive);
port (
stop: in std_ulogic := '0';
clk: out std_ulogic;
clk_with_jitter: out std_ulogic := '0';
rst: out std_ulogic := '0');
end entity;
architecture rtl of clock_source_tb is
constant clock_period: time := 1000 ms / g.clock_frequency;
signal jitter_delay: time := 0 ns;
signal jitter_clk: std_ulogic := '0';
begin
jitter_clk_process: process
variable seed1, seed2: positive;
variable r: real;
variable jit_high, jit_low: time := 0 ns;
begin