-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfmt.vhd
1874 lines (1649 loc) · 70.2 KB
/
fmt.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
-- Package: colors
-- Description
-- A package containing the strings for producing ANSI colored output on a
-- terminal.
package colors is
type colors_t is record
BLACK : string ;
RED : string ;
GREEN : string ;
YELLOW : string ;
BLUE : string ;
PURPLE : string ;
CYAN : string ;
WHITE : string ;
end record ;
type ansi_t is record
-- Foreground colors, typical
BLACK : string ;
RED : string ;
GREEN : string ;
YELLOW : string ;
BLUE : string ;
PURPLE : string ;
CYAN : string ;
WHITE : string ;
-- More styles
bold : colors_t ;
underline : colors_t ;
intense : colors_t ;
boldintense : colors_t ;
-- Background colors
background : colors_t ;
intensebg : colors_t ;
-- Control sequence
RESET : string ;
end record ;
constant FOREGROUND_COLORS : colors_t := (
BLACK => (ESC & "[30m"),
RED => (ESC & "[31m"),
GREEN => (ESC & "[32m"),
YELLOW => (ESC & "[33m"),
BLUE => (ESC & "[34m"),
PURPLE => (ESC & "[35m"),
CYAN => (ESC & "[36m"),
WHITE => (ESC & "[37m")
) ;
constant BOLD_COLORS : colors_t := (
BLACK => (ESC & "[1;30m"),
RED => (ESC & "[1;31m"),
GREEN => (ESC & "[1;32m"),
YELLOW => (ESC & "[1;33m"),
BLUE => (ESC & "[1;34m"),
PURPLE => (ESC & "[1;35m"),
CYAN => (ESC & "[1;36m"),
WHITE => (ESC & "[1;37m")
) ;
constant UNDERLINE_COLORS : colors_t := (
BLACK => (ESC & "[4;30m"),
RED => (ESC & "[4;31m"),
GREEN => (ESC & "[4;32m"),
YELLOW => (ESC & "[4;33m"),
BLUE => (ESC & "[4;34m"),
PURPLE => (ESC & "[4;35m"),
CYAN => (ESC & "[4;36m"),
WHITE => (ESC & "[4;37m")
) ;
constant BACKGROUND_COLORS : colors_t := (
BLACK => (ESC & "[40m"),
RED => (ESC & "[41m"),
GREEN => (ESC & "[42m"),
YELLOW => (ESC & "[43m"),
BLUE => (ESC & "[44m"),
PURPLE => (ESC & "[45m"),
CYAN => (ESC & "[46m"),
WHITE => (ESC & "[47m")
) ;
constant INTENSE_COLORS : colors_t := (
BLACK => (ESC & "[0;90m"),
RED => (ESC & "[0;91m"),
GREEN => (ESC & "[0;92m"),
YELLOW => (ESC & "[0;93m"),
BLUE => (ESC & "[0;94m"),
PURPLE => (ESC & "[0;95m"),
CYAN => (ESC & "[0;96m"),
WHITE => (ESC & "[0;97m")
) ;
constant BOLD_INTENSE_COLORS : colors_t := (
BLACK => (ESC & "[1;90m"),
RED => (ESC & "[1;91m"),
GREEN => (ESC & "[1;92m"),
YELLOW => (ESC & "[1;93m"),
BLUE => (ESC & "[1;94m"),
PURPLE => (ESC & "[1;95m"),
CYAN => (ESC & "[1;96m"),
WHITE => (ESC & "[1;97m")
) ;
constant INTENSE_BACKGROUND_COLORS : colors_t := (
BLACK => (ESC & "[0;100m"),
RED => (ESC & "[0;101m"),
GREEN => (ESC & "[0;102m"),
YELLOW => (ESC & "[0;103m"),
BLUE => (ESC & "[0;104m"),
PURPLE => (ESC & "[0;105m"),
CYAN => (ESC & "[0;106m"),
WHITE => (ESC & "[0;107m")
) ;
constant ansi : ansi_t := (
BLACK => FOREGROUND_COLORS.BLACK,
RED => FOREGROUND_COLORS.RED,
GREEN => FOREGROUND_COLORS.GREEN,
YELLOW => FOREGROUND_COLORS.YELLOW,
BLUE => FOREGROUND_COLORS.BLUE,
PURPLE => FOREGROUND_COLORS.PURPLE,
CYAN => FOREGROUND_COLORS.CYAN,
WHITE => FOREGROUND_COLORS.WHITE,
bold => BOLD_COLORS,
underline => UNDERLINE_COLORS,
intense => INTENSE_COLORS,
boldintense => BOLD_INTENSE_COLORS,
background => BACKGROUND_COLORS,
intensebg => INTENSE_BACKGROUND_COLORS,
RESET => (ESC & "[0m")
) ;
end package ;
-- Package: string_list
-- Description
-- A string_list is a dynamic array of strings which can efficiently be passed
-- between procedures such that the string does not need to be the same length.
-- NOTE
-- In the future, I'd like this to be a generic list package. If this is the
-- case then string_list can just be an instantiation of the generic list.
-- The only extra procedures to write are then sumlength and concatenate_list.
use std.textio.line ;
package string_list is
type string_list ;
type string_list_item ;
type string_list_item_ptr is access string_list_item ;
type string_list is record
root : string_list_item_ptr ;
length : natural ;
end record ;
type string_list_item is record
str : line ;
next_item : string_list_item_ptr ;
end record ;
-- Procedures for manipulating string_list
procedure append(variable list : inout string_list ; s : string) ;
procedure clear(variable list : inout string_list) ;
procedure get(variable list : in string_list ; index : integer ; variable l : out line) ;
procedure length(variable list : string_list; variable len : out natural) ;
procedure sumlength(variable list : string_list ; rv : out natural) ;
procedure concatenate_list(variable parts : string_list ; variable rv : inout line) ;
end package ;
package body string_list is
procedure append(variable list : inout string_list ; s : string) is
variable l : line := new string'(s) ;
variable new_item : string_list_item_ptr := new string_list_item ;
variable item : string_list_item_ptr := list.root ;
begin
new_item.str := l ;
new_item.next_item := null ;
if list.length = 0 then
list.root := new_item ;
else
while item.next_item /= null loop
item := item.next_item ;
end loop ;
item.next_item := new_item ;
end if ;
list.length := list.length + 1 ;
end procedure ;
procedure clear(variable list : inout string_list) is
variable item : string_list_item_ptr := list.root ;
variable next_item : string_list_item_ptr := null ;
begin
if item /= null then
next_item := item.next_item ;
end if ;
while item /= null loop
next_item := item.next_item ;
deallocate(item) ;
item := next_item ;
end loop ;
list.root := null ;
list.length := 0 ;
end procedure ;
procedure get(variable list : in string_list ; index : integer ; variable l : out line) is
variable item : string_list_item_ptr := list.root ;
begin
if index >= list.length then
report "Cannot retrieve item, index out of bounds"
severity warning ;
l := null ;
end if ;
for i in 1 to index loop
item := item.next_item ;
end loop ;
l := item.str ;
end procedure ;
procedure length(variable list : string_list; variable len : out natural) is
begin
len := list.length ;
end procedure ;
procedure sumlength(variable list : string_list ; rv : out natural) is
variable l : line := null ;
variable len : natural := 0 ;
variable count : natural := 0 ;
begin
length(list, len) ;
for i in 0 to len-1 loop
get(list, i, l) ;
count := count + l.all'length ;
end loop ;
rv := count ;
end procedure ;
procedure concatenate_list(variable parts : string_list ; variable rv : inout line) is
variable start : positive := 1 ;
variable stop : positive := 1 ;
variable l : line := null ;
variable len : natural := 0 ;
begin
sumlength(parts, len) ;
rv := new string(1 to len) ;
for i in 0 to parts.length-1 loop
get(parts, i, l) ;
stop := start + l.all'length - 1 ;
rv(start to stop) := l.all ;
start := stop + 1 ;
end loop ;
end procedure ;
end package body ;
-- Package: fmt
-- Description
-- A string formatting package that is based on the Python format specifier.
-- See this website for some information:
--
-- https://realpython.com/python-formatted-output/#the-format_spec-component
--
-- Currently supported is:
--
-- :[fill][align][sign][width][.precision][class]
--
-- fill: Any character
-- The character to fill any extra space when the string does not fit the
-- requested width.
--
-- align: '<', '>', '^', '='
-- < Left alignment
-- > Right alignment
-- ^ Center alignment
-- = Sign alignment (d, e, f, u classes only)
--
-- sign: '+'
-- Ensures the sign is always printed for a number.
--
-- width: A number
-- The minimum number of characters to write.
--
-- .precision: A point then a number.
-- For the (e, f) classes, prints the number of points to the right of the decimal.
-- For the t class, determines which timebase to utilize for conversion.
-- Precision Time Unit
-- .0 1 second
-- .3 1 millisecond
-- .6 1 microsecond
-- .9 1 nanosecond
-- .12 1 picosecond
-- .15 1 femtosecond
--
-- class: 'b', 'c', 'd', 'e', 'f', 'o', 's', 't', 'u', 'x'
-- Character Class
-- b Binary
-- c Character
-- d Signed integer
-- e Floating point (exp notation - i.e. 3.14159e+00)
-- f Floating point (fixed notation - i.e. 3.14159)
-- o Octal
-- s String
-- t Time value
-- u Unsigned integer
-- x Hexadecimal
-- Note: Both lowercase and uppercase class values are accepetd.
use std.textio.line ;
use std.textio.side ;
use std.textio.read ;
use std.textio.bread ;
use std.textio.hread ;
use std.textio.oread ;
use std.textio.write ;
use std.textio.bwrite ;
use std.textio.hwrite ;
use std.textio.owrite ;
use work.string_list.all ;
library ieee ;
use ieee.std_logic_1164.std_logic ;
use ieee.std_logic_1164.std_logic_vector ;
use ieee.std_logic_1164.hwrite ;
use ieee.std_logic_1164.owrite ;
use ieee.std_logic_1164.bwrite ;
use ieee.std_logic_1164.write ;
use ieee.numeric_std.signed ;
use ieee.numeric_std.unsigned ;
use ieee.numeric_std.to_integer ;
use ieee.numeric_std.hwrite ;
use ieee.numeric_std.owrite ;
use ieee.numeric_std.bwrite ;
use ieee.fixed_pkg.sfixed ;
use ieee.fixed_pkg.ufixed ;
use ieee.fixed_pkg.hwrite ;
use ieee.fixed_pkg.owrite ;
use ieee.fixed_pkg.bwrite ;
use ieee.fixed_pkg.to_real ;
use ieee.fixed_pkg.to_slv ;
package fmt is
---------------------------------------------------------------------------
-- VHDL-2008 Generic Function
---------------------------------------------------------------------------
---- TODO: Generic f() function which utilizes the type'image to get the string and just pass to fstr()?
---- Useful for custom enumerated types?
--function fstring
-- generic(type t; function to_string(x : t) return string is <>)
-- parameter(value : t ; sfmt : string := "s")
-- return string ;
--
--function fimage
-- generic(type t)
-- parameter(value : t ; sfmt : string := "s")
-- return string;
-- Format string building function using a string_list
procedure f(sfmt : string ; variable args : inout string_list ; variable l : inout line) ;
alias fmt is f[string, string_list, line] ;
-- Format string building function using up to 16 arguments
function f(sfmt : string ; a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 : in string := "") return string ;
alias fmt is f[string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string return string] ;
-- Single argument formatting
function f(sfmt : string ; value : bit) return string ;
function f(sfmt : string ; value : bit_vector) return string ;
function f(sfmt : string ; value : boolean) return string ;
function f(sfmt : string ; value : character) return string ;
function f(sfmt : string ; value : integer) return string ;
function f(sfmt : string ; value : real) return string ;
function f(sfmt : string ; value : time) return string ;
-- Functions to format standard types
-- NOTE: Aliases added for ambiguous types
function f(value : bit ; sfmt : string := "b") return string ;
alias fbit is f[bit, string return string] ;
function f(value : bit_vector ; sfmt : string := "b") return string ;
alias fbv is f[bit_vector, string return string] ;
function f(value : boolean ; sfmt : string := "s") return string ;
function f(value : character ; sfmt : string := "c") return string ;
alias fchar is f[character, string return string] ;
function f(value : integer ; sfmt : string := "d") return string ;
function f(value : real ; sfmt : string := "f") return string ;
function f(value : string ; sfmt : string := "s") return string ;
alias fstr is f[string, string return string] ;
function f(value : time ; sfmt : string := ".9t") return string ;
-- Functions to format fixed point types
function f(value : sfixed ; sfmt : string := "f") return string ;
function f(value : ufixed ; sfmt : string := "f") return string ;
function f(value : signed ; sfmt : string := "d") return string ;
function f(value : unsigned ; sfmt : string := "d") return string ;
function f(value : std_logic ; sfmt : string := "b") return string ;
function f(value : std_logic_vector ; sfmt : string := "b") return string ;
-- Printing to output
procedure p(x : string) ;
alias print is p[string];
end package ;
package body fmt is
use std.textio.write ;
use std.textio.writeline ;
use std.textio.output ;
procedure p(x : string) is
variable l : line ;
begin
write(l, x) ;
writeline(output, l) ;
end procedure ;
-- Internal private types
-- Format Alignment
-- LEFT 'example '
-- RIGHT ' example'
-- CENTERED ' example '
-- SIGN_EDGE '+ 3'
type align_t is (LEFT, RIGHT, CENTERED, SIGN_EDGE) ;
-- Format Class
-- b Binary
-- c Character
-- d Signed integer
-- e Floating point (exp notation)
-- f Floating point (fixed notation)
-- o Octal
-- s String
-- t Time value
-- u Unsigned integer
-- x Hexadecimal
type class_t is (BINARY, CHAR, INT, FLOAT_EXP, FLOAT_FIXED, OCTAL, STR, TIMEVAL, UINT, HEX) ;
function f(sfmt : string ; value : align_t) return string ;
function f(sfmt : string ; value : class_t) return string ;
function f(value : align_t ; sfmt : string := "s") return string ;
function f(value : class_t ; sfmt : string := "s") return string ;
-- [fill][align][sign][width][.precision][class]
-- NOTE: # after sign might be good for prefixes (0b, 0o, 0x) and might be easy to implement.
-- NOTE: Grouping might be good, but python only limits to [,_] and doesn't allow for arbitrary
-- grouping size. Could be arbitrary character like fill, and how many digits? Sounds complicated, though.
type fmt_spec_t is record
fill : character ;
align : align_t ;
sign : boolean ;
width : natural ;
precision : natural ;
class : class_t ;
end record ;
constant DEFAULT_FMT_SPEC : fmt_spec_t := (
fill => ' ',
align => LEFT,
sign => false,
width => 0,
precision => 0,
class => STR
) ;
-- Private Helper functions
function parse(sfmt : string ; default_class : class_t := STR) return fmt_spec_t ;
-- Collapse align_t to be side (LEFT, RIGHT)
function to_side(value : align_t) return side ;
-- Helper functions for line manipulation for custom f-functions
procedure fill(variable l : inout line ; variable fmt_spec : fmt_spec_t ; variable fillcount : inout natural) ;
procedure shift(variable l : inout line ; count : in natural) ;
---------------------------------------------------------------------------
-- VHDL-2008 Generic Function
---------------------------------------------------------------------------
--function f
-- generic(type t; function to_string(x : t) return string is <>)
-- parameter(value : t ; sfmt : string := "s")
-- return string
--is
--begin
-- return fstr(to_string(value), sfmt) ;
--end function ;
--function f
-- generic(type t)
-- parameter(value : t ; sfmt : string := "s")
-- return string
--is
--begin
-- return fstr(t'image(value), sfmt) ;
--end function ;
function parse(sfmt : string ; default_class : class_t := STR) return fmt_spec_t is
type fsm_t is (START, FILL, ALIGN, SIGN, WIDTH, DOT, PRECISION, CLASS, EXTRA) ;
alias fn : string(1 to sfmt'length) is sfmt ;
variable l : line := null ;
variable fsm : fsm_t := START ;
variable rv : fmt_spec_t := DEFAULT_FMT_SPEC ;
variable idx : positive := 1 ;
variable numstart : natural := 0 ;
variable numstop : natural := 0 ;
variable precision_present : boolean := false ;
begin
assert fn'length > 0
report "Format string must not be empty"
severity warning ;
rv.class := default_class ;
while idx <= fn'length loop
case fsm is
when START =>
if fn'length = 1 then
-- Only a single character
case fn(idx) is
when '<'|'>'|'^'|'=' =>
-- Alignment but it doesn't matter since no width
null ;
when '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' =>
-- Single character width
fsm := WIDTH ;
when '+' =>
-- Sign
rv.sign := true ;
when 'b'|'B'|'c'|'C'|'d'|'D'|'e'|'E'|'f'|'F'|'o'|'O'|'s'|'S'|'t'|'T'|'u'|'U'|'x'|'X' =>
-- Class
fsm := CLASS ;
when '.' =>
-- Illegal precision
report "Format specifier missing precision"
severity warning ;
exit ;
when others =>
report fstr("Unknown format code: {}", f(fn(idx)))
severity warning ;
exit ;
end case ;
else
-- Guaranteed to be at least 2 characters
case fn(idx) is
-- Check the first character class
when '<'|'>'|'^'|'=' =>
-- Alignment character first, but could also be a fill character
case fn(idx+1) is
when '<'|'>'|'^'|'=' =>
-- 2 alignment characters in a row, so one must be for filling
fsm := FILL ;
when others =>
-- Alignment character is first, followed by a non-alignment character
fsm := ALIGN ;
end case ;
when '+' =>
-- Sign character first, but might be fill, check for alignment character next
case fn(idx+1) is
when '<'|'>'|'^'|'=' =>
-- Alignment character second, so consume FILL character
fsm := FILL ;
when others =>
-- Second character is not an alignment character
-- Assume first character is alignment and not fill
fsm := SIGN ;
end case ;
when '0' =>
-- With a leading zero, either FILL or WIDTH
case fn(idx+1) is
when '+'|'.' =>
fsm := WIDTH ;
when others =>
fsm := FILL ;
end case ;
when '1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' =>
case fn(idx+1) is
when '<'|'>'|'^'|'='|'+' =>
-- Non-Zero number followed by alignment character or sign, so consume as fill character
fsm := FILL ;
when others =>
-- Non-Zero number followed by something else, so assume width
fsm := WIDTH ;
end case ;
when '.' =>
-- Start with DOT precision
fsm := DOT ;
when others =>
case fn(idx+1) is
when '<'|'>'|'^'|'='|'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' =>
-- Alignment character is second, so fill character is first
fsm := FILL ;
when others =>
report fmt("Invalid format specifier: {}", fstr(fn))
severity warning ;
exit ;
end case ;
end case ;
end if ;
next ;
when FILL =>
rv.fill := fn(idx) ;
idx := idx + 1 ;
fsm := ALIGN ;
next ;
when ALIGN =>
case fn(idx) is
when '<' =>
rv.align := LEFT ;
idx := idx + 1 ;
when '>' =>
rv.align := RIGHT ;
idx := idx + 1 ;
when '^' =>
rv.align := CENTERED ;
idx := idx + 1 ;
when '=' =>
rv.align := SIGN_EDGE ;
idx := idx + 1 ;
when others =>
null ;
end case ;
fsm := SIGN ;
when SIGN =>
case fn(idx) is
when '+' =>
rv.sign := true ;
idx := idx + 1 ;
when others =>
null ;
end case ;
fsm := WIDTH ;
when WIDTH =>
case fn(idx) is
when '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' =>
if numstart = 0 then
numstart := idx ;
numstop := idx ;
elsif numstart > 0 then
numstop := idx ;
end if ;
idx := idx + 1 ;
when others =>
if numstart > 0 then
l := new string'(fn(numstart to numstop)) ;
read(l, rv.width) ;
numstart := 0 ;
numstop := 0 ;
end if ;
fsm := DOT ;
end case ;
when DOT =>
case fn(idx) is
when '.' =>
idx := idx + 1 ;
fsm := PRECISION ;
when others =>
fsm := CLASS ;
end case ;
when PRECISION =>
case fn(idx) is
when '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' =>
if numstart = 0 then
numstart := idx ;
numstop := idx ;
elsif numstart > 0 then
numstop := idx ;
end if ;
idx := idx + 1 ;
when others =>
if numstart > 0 then
l := new string'(fn(numstart to numstop)) ;
read(l, rv.precision) ;
precision_present := true ;
numstart := 0 ;
numstop := 0 ;
else
report "Format specifier missing precision"
severity warning ;
end if ;
fsm := CLASS ;
end case ;
when CLASS =>
case fn(idx) is
when 'b'|'B' =>
rv.class := BINARY ;
when 'c'|'C' =>
rv.class := CHAR ;
when 'd'|'D' =>
rv.class := INT ;
when 'e'|'E' =>
-- Normalized d[.precision]e[+-]dd notation
rv.class := FLOAT_EXP ;
if precision_present = false then
-- Precision isn't mentioned, so default to 6
rv.precision := 6 ;
end if ;
when 'f'|'F' =>
if precision_present = true then
if rv.precision = 0 then
-- Precision was specified, so change the class to int to cut off
-- any decimal representation
rv.class := INT ;
else
rv.class := FLOAT_FIXED ;
end if ;
else
-- Precision isn't present, so default the precision to 6 places
rv.precision := 6 ;
rv.class := FLOAT_FIXED ;
end if ;
when 'o'|'O' =>
rv.class := OCTAL ;
when 's'|'S' =>
rv.class := STR ;
when 't'|'T' =>
rv.class := TIMEVAL ;
when 'u'|'U' =>
rv.class := UINT ;
when 'x'|'X' =>
rv.class := HEX ;
when others =>
rv.class := BINARY ;
report fmt("Unknown class: {} is not [bcdofsux] - defaulting to BINARY", f(fn(idx)))
severity warning ;
end case ;
idx := idx + 1 ;
fsm := EXTRA ;
when EXTRA =>
report fmt("Extra characters in format specifier ignored : {}", fstr(fn(idx to fn'length)))
severity warning ;
exit ;
end case ;
end loop ;
-- Parse the last bit of data
case fsm is
when WIDTH =>
l := new string'(sfmt(numstart to numstop)) ;
read(l, rv.width) ;
when PRECISION =>
l := new string'(sfmt(numstart to numstop)) ;
read(l, rv.precision) ;
when others =>
null ;
end case ;
return rv ;
end function ;
procedure shift(variable l : inout line ; count : in natural) is
variable newl : line := new string'(l.all) ;
variable dest : positive := count + 1 ;
begin
if count > 0 then
for idx in l'range loop
newl(dest) := l(idx) ;
dest := dest + 1 ;
if dest = l'length + 1 then
dest := 1 ;
end if ;
end loop ;
l := newl ;
end if ;
end procedure ;
function to_side(value : align_t) return side is
begin
case value is
when RIGHT|SIGN_EDGE =>
return right ;
when others =>
return left ;
end case ;
end function ;
procedure fill(variable l : inout line ; variable fmt_spec : fmt_spec_t ; variable fillcount : inout natural) is
variable inc : integer ;
variable idx : integer ;
begin
fillcount := 0 ;
case fmt_spec.align is
when RIGHT|SIGN_EDGE =>
-- Start on the left side to fill in
idx := 1 ;
inc := 1 ;
when others =>
-- Start on the right side to fill in
idx := l'length ;
inc := -1 ;
end case ;
while true loop
if l(idx) = ' ' then
fillcount := fillcount + 1 ;
l(idx) := fmt_spec.fill ;
idx := idx + inc ;
else
exit ;
end if ;
end loop ;
end procedure ;
function f(value : string ; sfmt : string := "s") return string is
alias s : string(1 to value'length) is value ;
variable fmt_spec : fmt_spec_t := parse(sfmt, STR) ;
variable l : line ;
variable fillcount : integer := fmt_spec.width - value'length ;
constant static_fill : string(1 to fillcount) := (others => fmt_spec.fill) ;
begin
if (fmt_spec.precision > 0) and (value'length > fmt_spec.precision) then
-- Limiting the string size based on precision
return s(1 to fmt_spec.precision) ;
else
-- The string might have spaces included, so lets create
fillcount := fmt_spec.width - value'length ;
case fmt_spec.align is
when LEFT|CENTERED =>
write(l, s & static_fill, to_side(fmt_spec.align)) ;
when others =>
write(l, static_fill & s, to_side(fmt_spec.align)) ;
end case ;
end if ;
if fillcount > 0 and fmt_spec.align = CENTERED then
shift(l, fillcount/2) ;
end if ;
return l.all ;
end function ;
function f(value : align_t ; sfmt : string := "s") return string is
constant s : string := align_t'image(value) ;
begin
return fstr(s, sfmt) ;
end function ;
function f(value : class_t ; sfmt : string := "s") return string is
constant s : string := class_t'image(value) ;
begin
return fstr(s, sfmt) ;
end function ;
function f(value : bit ; sfmt : string := "b") return string is
variable fmt_spec : fmt_spec_t := parse(sfmt, BINARY) ;
variable l : line ;
variable fillcount : natural ;
begin
write(l, value, to_side(fmt_spec.align), fmt_spec.width) ;
fill(l, fmt_spec, fillcount) ;
if fmt_spec.align = CENTERED then
shift(l, fillcount/2) ;
end if ;
return l.all ;
end function ;
function f(value : bit_vector ; sfmt : string := "b") return string is
variable fmt_spec : fmt_spec_t := parse(sfmt, BINARY) ;
variable l : line ;
variable fillcount : natural ;
begin
case fmt_spec.class is
when BINARY =>
bwrite(l, value, to_side(fmt_spec.align), fmt_spec.width) ;
when OCTAL =>
owrite(l, value, to_side(fmt_spec.align), fmt_spec.width) ;
when HEX =>
hwrite(l, value, to_side(fmt_spec.align), fmt_spec.width) ;
when others =>
report f("Unsupported class for bit_vector ({}), using binary: {}", fmt_spec.class)
severity warning ;
bwrite(l, value, to_side(fmt_spec.align), fmt_spec.width) ;
end case ;
fill(l, fmt_spec, fillcount) ;
if fmt_spec.align = CENTERED then
shift(l, fillcount/2) ;
end if ;
return l.all ;
end function ;
function f(value : boolean ; sfmt : string := "s") return string is
variable fmt_spec : fmt_spec_t := parse(sfmt, BINARY) ;
variable l : line ;
variable bit_arg : bit := '0' ;
variable fillcount : natural ;
begin
case fmt_spec.class is
when STR =>
write(l, value, to_side(fmt_spec.align), fmt_spec.width) ;
when BINARY =>
if value = true then
bit_arg := '1' ;
end if ;
l := new string'(f(bit_arg, sfmt)) ;
return l.all ;
when others =>
report fstr("Unsupported class for boolean - {}, using STR", f(fmt_spec.class))
severity warning ;
write(l, value, to_side(fmt_spec.align), fmt_spec.width) ;
end case ;
fill(l, fmt_spec, fillcount) ;
if fmt_spec.align = CENTERED then
shift(l, fillcount/2) ;
end if ;
return l.all ;
end function ;
function f(value : character ; sfmt : string := "c") return string is
variable fmt_spec : fmt_spec_t := parse(sfmt, CHAR) ;
variable l : line ;
variable fillcount : natural ;
begin
write(l, value, to_side(fmt_spec.align), fmt_spec.width) ;
fill(l, fmt_spec, fillcount) ;
if fmt_spec.align = CENTERED then
shift(l, fillcount/2) ;
end if ;
return l.all ;
end function ;
function f(value : time ; sfmt : string := ".9t" ) return string is
variable fmt_spec : fmt_spec_t := parse(sfmt, TIMEVAL) ;
variable l : line ;
variable unit : time := 1 ns ;
variable fillcount : natural ;
begin
case fmt_spec.precision is
when 0 => unit := 1 sec ;
when 3 => unit := 1 ms ;
when 6 => unit := 1 us ;
when 9 => unit := 1 ns ;
when 12 => unit := 1 ps ;
when 15 => unit := 1 fs ;
when others =>
report fmt("Time precision unknown: {}, using default", f(fmt_spec.precision))
severity warning ;
end case ;
write(l, value, to_side(fmt_spec.align), fmt_spec.width, unit) ;
fill(l, fmt_spec, fillcount) ;
if fmt_spec.align = CENTERED then
shift(l, fillcount/2) ;
end if ;
return l.all ;
end function ;
procedure add_sign(variable l : inout line ; s : character ; fmt_fill : character ) is
variable idx : natural := 1 ;
begin
while l(idx) = fmt_fill loop
idx := idx + 1 ;
end loop ;
l(idx-1) := s ;
end procedure ;
function f(value : integer ; sfmt : string := "d") return string is
variable fmt_spec : fmt_spec_t := parse(sfmt, INT) ;
variable l : line := null ;
variable temp : line := null ;
variable fillcount : natural := 0 ;
variable sign : character ;
begin
write(l, value, to_side(fmt_spec.align), fmt_spec.width) ;
fill(l, fmt_spec, fillcount) ;
if fmt_spec.align = CENTERED then
shift(l, fillcount/2) ;
end if ;
if fmt_spec.sign = true then