1--  This -*- vhdl -*- file was generated from std_logic_1164-body.proto
2--  This is an implementation of -*- vhdl -*- ieee.std_logic_1164 based only
3--  on the specifications.  This file is part of GHDL.
4--  Copyright (C) 2015 Tristan Gingold
5--
6--  This program is free software: you can redistribute it and/or modify
7--  it under the terms of the GNU General Public License as published by
8--  the Free Software Foundation, either version 2 of the License, or
9--  (at your option) any later version.
10--
11--  This program is distributed in the hope that it will be useful,
12--  but WITHOUT ANY WARRANTY; without even the implied warranty of
13--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14--  GNU General Public License for more details.
15--
16--  You should have received a copy of the GNU General Public License
17--  along with this program.  If not, see <gnu.org/licenses>.
18
19--  This is a template file.  To avoid errors and duplication, the python
20--  script build.py generate most of the bodies.
21
22package body std_logic_1164 is
23
24  type table_1d is array (std_ulogic) of std_ulogic;
25  type table_2d is array (std_ulogic, std_ulogic) of std_ulogic;
26
27  constant resolution : table_2d :=
28  --  UX01ZWLH-
29    ("UUUUUUUUU",   --  U
30     "UXXXXXXXX",   --  X
31     "UX0X0000X",   --  0
32     "UXX11111X",   --  1
33     "UX01ZWLHX",   --  Z
34     "UX01WWWWX",   --  W
35     "UX01LWLWX",   --  L
36     "UX01HWWHX",   --  H
37     "UXXXXXXXX"    --  -
38     );
39
40  function resolved (s : std_ulogic_vector) return std_ulogic
41  is
42    variable res : std_ulogic := 'Z';
43  begin
44    for I in s'range loop
45      res := resolution (res, s (I));
46    end loop;
47    return res;
48  end resolved;
49
50
51  constant and_table : table_2d :=
52  --  UX01ZWLH-
53    ("UU0UUU0UU",   -- U
54     "UX0XXX0XX",   -- X
55     "000000000",   -- 0
56     "UX01XX01X",   -- 1
57     "UX0XXX0XX",   -- Z
58     "UX0XXX0XX",   -- W
59     "000000000",   -- L
60     "UX01XX01X",   -- H
61     "UX0XXX0XX"    -- -
62     );
63
64  constant nand_table : table_2d :=
65  --  UX01ZWLH-
66    ("UU1UUU1UU",   -- U
67     "UX1XXX1XX",   -- X
68     "111111111",   -- 0
69     "UX10XX10X",   -- 1
70     "UX1XXX1XX",   -- Z
71     "UX1XXX1XX",   -- W
72     "111111111",   -- L
73     "UX10XX10X",   -- H
74     "UX1XXX1XX"    -- -
75     );
76
77  constant or_table : table_2d :=
78  --  UX01ZWLH-
79    ("UUU1UUU1U",   -- U
80     "UXX1XXX1X",   -- X
81     "UX01XX01X",   -- 0
82     "111111111",   -- 1
83     "UXX1XXX1X",   -- Z
84     "UXX1XXX1X",   -- W
85     "UX01XX01X",   -- L
86     "111111111",   -- H
87     "UXX1XXX1X"    -- -
88     );
89
90  constant nor_table : table_2d :=
91  --  UX01ZWLH-
92    ("UUU0UUU0U",   -- U
93     "UXX0XXX0X",   -- X
94     "UX10XX10X",   -- 0
95     "000000000",   -- 1
96     "UXX0XXX0X",   -- Z
97     "UXX0XXX0X",   -- W
98     "UX10XX10X",   -- L
99     "000000000",   -- H
100     "UXX0XXX0X"    -- -
101     );
102
103  constant xor_table : table_2d :=
104  --  UX01ZWLH-
105    ("UUUUUUUUU",   -- U
106     "UXXXXXXXX",   -- X
107     "UX01XX01X",   -- 0
108     "UX10XX10X",   -- 1
109     "UXXXXXXXX",   -- Z
110     "UXXXXXXXX",   -- W
111     "UX01XX01X",   -- L
112     "UX10XX10X",   -- H
113     "UXXXXXXXX"    -- -
114     );
115
116  constant xnor_table : table_2d :=
117  --  UX01ZWLH-
118    ("UUUUUUUUU",   -- U
119     "UXXXXXXXX",   -- X
120     "UX10XX10X",   -- 0
121     "UX01XX01X",   -- 1
122     "UXXXXXXXX",   -- Z
123     "UXXXXXXXX",   -- W
124     "UX10XX10X",   -- L
125     "UX01XX01X",   -- H
126     "UXXXXXXXX"    -- -
127     );
128
129  constant not_table : table_1d :=
130  --  UX01ZWLH-
131     "UX10XX10X";
132
133
134  function "and" (l : std_ulogic; r : std_ulogic) return UX01 is
135  begin
136    return and_table (l, r);
137  end "and";
138
139  function "nand" (l : std_ulogic; r : std_ulogic) return UX01 is
140  begin
141    return nand_table (l, r);
142  end "nand";
143
144  function "or" (l : std_ulogic; r : std_ulogic) return UX01 is
145  begin
146    return or_table (l, r);
147  end "or";
148
149  function "nor" (l : std_ulogic; r : std_ulogic) return UX01 is
150  begin
151    return nor_table (l, r);
152  end "nor";
153
154  function "xor" (l : std_ulogic; r : std_ulogic) return UX01 is
155  begin
156    return xor_table (l, r);
157  end "xor";
158
159  function "xnor" (l : std_ulogic; r : std_ulogic) return UX01 is
160  begin
161    return xnor_table (l, r);
162  end "xnor";
163
164  function "not" (l : std_ulogic) return UX01 is
165  begin
166    return not_table (l);
167  end "not";
168
169  function "and" (l, r : std_ulogic_vector) return std_ulogic_vector
170  is
171    subtype res_type is std_ulogic_vector (1 to l'length);
172    alias la : res_type is l;
173    alias ra : std_ulogic_vector (1 to r'length) is r;
174    variable res : res_type;
175  begin
176    if la'length /= ra'length then
177      assert false
178        report "arguments of overloaded 'and' operator are not of the same length"
179        severity failure;
180    else
181      for I in res_type'range loop
182        res (I) := and_table (la (I), ra (I));
183      end loop;
184    end if;
185    return res;
186  end "and";
187
188  function "nand" (l, r : std_ulogic_vector) return std_ulogic_vector
189  is
190    subtype res_type is std_ulogic_vector (1 to l'length);
191    alias la : res_type is l;
192    alias ra : std_ulogic_vector (1 to r'length) is r;
193    variable res : res_type;
194  begin
195    if la'length /= ra'length then
196      assert false
197        report "arguments of overloaded 'nand' operator are not of the same length"
198        severity failure;
199    else
200      for I in res_type'range loop
201        res (I) := nand_table (la (I), ra (I));
202      end loop;
203    end if;
204    return res;
205  end "nand";
206
207  function "or" (l, r : std_ulogic_vector) return std_ulogic_vector
208  is
209    subtype res_type is std_ulogic_vector (1 to l'length);
210    alias la : res_type is l;
211    alias ra : std_ulogic_vector (1 to r'length) is r;
212    variable res : res_type;
213  begin
214    if la'length /= ra'length then
215      assert false
216        report "arguments of overloaded 'or' operator are not of the same length"
217        severity failure;
218    else
219      for I in res_type'range loop
220        res (I) := or_table (la (I), ra (I));
221      end loop;
222    end if;
223    return res;
224  end "or";
225
226  function "nor" (l, r : std_ulogic_vector) return std_ulogic_vector
227  is
228    subtype res_type is std_ulogic_vector (1 to l'length);
229    alias la : res_type is l;
230    alias ra : std_ulogic_vector (1 to r'length) is r;
231    variable res : res_type;
232  begin
233    if la'length /= ra'length then
234      assert false
235        report "arguments of overloaded 'nor' operator are not of the same length"
236        severity failure;
237    else
238      for I in res_type'range loop
239        res (I) := nor_table (la (I), ra (I));
240      end loop;
241    end if;
242    return res;
243  end "nor";
244
245  function "xor" (l, r : std_ulogic_vector) return std_ulogic_vector
246  is
247    subtype res_type is std_ulogic_vector (1 to l'length);
248    alias la : res_type is l;
249    alias ra : std_ulogic_vector (1 to r'length) is r;
250    variable res : res_type;
251  begin
252    if la'length /= ra'length then
253      assert false
254        report "arguments of overloaded 'xor' operator are not of the same length"
255        severity failure;
256    else
257      for I in res_type'range loop
258        res (I) := xor_table (la (I), ra (I));
259      end loop;
260    end if;
261    return res;
262  end "xor";
263
264  function "xnor" (l, r : std_ulogic_vector) return std_ulogic_vector
265  is
266    subtype res_type is std_ulogic_vector (1 to l'length);
267    alias la : res_type is l;
268    alias ra : std_ulogic_vector (1 to r'length) is r;
269    variable res : res_type;
270  begin
271    if la'length /= ra'length then
272      assert false
273        report "arguments of overloaded 'xnor' operator are not of the same length"
274        severity failure;
275    else
276      for I in res_type'range loop
277        res (I) := xnor_table (la (I), ra (I));
278      end loop;
279    end if;
280    return res;
281  end "xnor";
282
283  function "not" (l : std_ulogic_vector) return std_ulogic_vector
284  is
285    subtype res_type is std_ulogic_vector (1 to l'length);
286    alias la : res_type is l;
287    variable res : res_type;
288  begin
289    for I in res_type'range loop
290      res (I) := not_table (la (I));
291    end loop;
292    return res;
293  end "not";
294
295  function "and" (l, r : std_logic_vector) return std_logic_vector
296  is
297    subtype res_type is std_logic_vector (1 to l'length);
298    alias la : res_type is l;
299    alias ra : std_logic_vector (1 to r'length) is r;
300    variable res : res_type;
301  begin
302    if la'length /= ra'length then
303      assert false
304        report "arguments of overloaded 'and' operator are not of the same length"
305        severity failure;
306    else
307      for I in res_type'range loop
308        res (I) := and_table (la (I), ra (I));
309      end loop;
310    end if;
311    return res;
312  end "and";
313
314  function "nand" (l, r : std_logic_vector) return std_logic_vector
315  is
316    subtype res_type is std_logic_vector (1 to l'length);
317    alias la : res_type is l;
318    alias ra : std_logic_vector (1 to r'length) is r;
319    variable res : res_type;
320  begin
321    if la'length /= ra'length then
322      assert false
323        report "arguments of overloaded 'nand' operator are not of the same length"
324        severity failure;
325    else
326      for I in res_type'range loop
327        res (I) := nand_table (la (I), ra (I));
328      end loop;
329    end if;
330    return res;
331  end "nand";
332
333  function "or" (l, r : std_logic_vector) return std_logic_vector
334  is
335    subtype res_type is std_logic_vector (1 to l'length);
336    alias la : res_type is l;
337    alias ra : std_logic_vector (1 to r'length) is r;
338    variable res : res_type;
339  begin
340    if la'length /= ra'length then
341      assert false
342        report "arguments of overloaded 'or' operator are not of the same length"
343        severity failure;
344    else
345      for I in res_type'range loop
346        res (I) := or_table (la (I), ra (I));
347      end loop;
348    end if;
349    return res;
350  end "or";
351
352  function "nor" (l, r : std_logic_vector) return std_logic_vector
353  is
354    subtype res_type is std_logic_vector (1 to l'length);
355    alias la : res_type is l;
356    alias ra : std_logic_vector (1 to r'length) is r;
357    variable res : res_type;
358  begin
359    if la'length /= ra'length then
360      assert false
361        report "arguments of overloaded 'nor' operator are not of the same length"
362        severity failure;
363    else
364      for I in res_type'range loop
365        res (I) := nor_table (la (I), ra (I));
366      end loop;
367    end if;
368    return res;
369  end "nor";
370
371  function "xor" (l, r : std_logic_vector) return std_logic_vector
372  is
373    subtype res_type is std_logic_vector (1 to l'length);
374    alias la : res_type is l;
375    alias ra : std_logic_vector (1 to r'length) is r;
376    variable res : res_type;
377  begin
378    if la'length /= ra'length then
379      assert false
380        report "arguments of overloaded 'xor' operator are not of the same length"
381        severity failure;
382    else
383      for I in res_type'range loop
384        res (I) := xor_table (la (I), ra (I));
385      end loop;
386    end if;
387    return res;
388  end "xor";
389
390  function "xnor" (l, r : std_logic_vector) return std_logic_vector
391  is
392    subtype res_type is std_logic_vector (1 to l'length);
393    alias la : res_type is l;
394    alias ra : std_logic_vector (1 to r'length) is r;
395    variable res : res_type;
396  begin
397    if la'length /= ra'length then
398      assert false
399        report "arguments of overloaded 'xnor' operator are not of the same length"
400        severity failure;
401    else
402      for I in res_type'range loop
403        res (I) := xnor_table (la (I), ra (I));
404      end loop;
405    end if;
406    return res;
407  end "xnor";
408
409  function "not" (l : std_logic_vector) return std_logic_vector
410  is
411    subtype res_type is std_logic_vector (1 to l'length);
412    alias la : res_type is l;
413    variable res : res_type;
414  begin
415    for I in res_type'range loop
416      res (I) := not_table (la (I));
417    end loop;
418    return res;
419  end "not";
420
421  --  Conversion functions.
422  --  The result range (for vectors) is S'Length - 1 downto 0.
423  --  XMAP is return for values not in '0', '1', 'L', 'H'.
424  function to_bit (s : std_ulogic; xmap : bit := '0') return bit is
425  begin
426    case s is
427      when '0' | 'L' =>
428        return '0';
429      when '1' | 'H' =>
430        return '1';
431      when others =>
432        return xmap;
433    end case;
434  end to_bit;
435
436  type bit_to_std_table is array (bit) of std_ulogic;
437  constant bit_to_std : bit_to_std_table := "01";
438
439
440  function to_bitvector (s : std_ulogic_vector; xmap : bit := '0')
441    return bit_vector
442  is
443    subtype res_range is natural range s'length - 1 downto 0;
444    alias as : std_ulogic_vector (res_range) is s;
445    variable res : bit_vector (res_range);
446    variable b : bit;
447  begin
448    for I in res_range loop
449      --  Inline for efficiency.
450      case as (I) is
451        when '0' | 'L' =>
452          b := '0';
453        when '1' | 'H' =>
454          b := '1';
455        when others =>
456          b := xmap;
457      end case;
458      res (I) := b;
459    end loop;
460    return res;
461  end to_bitvector;
462
463  function to_bitvector (s : std_logic_vector; xmap : bit := '0')
464    return bit_vector
465  is
466    subtype res_range is natural range s'length - 1 downto 0;
467    alias as : std_logic_vector (res_range) is s;
468    variable res : bit_vector (res_range);
469    variable b : bit;
470  begin
471    for I in res_range loop
472      --  Inline for efficiency.
473      case as (I) is
474        when '0' | 'L' =>
475          b := '0';
476        when '1' | 'H' =>
477          b := '1';
478        when others =>
479          b := xmap;
480      end case;
481      res (I) := b;
482    end loop;
483    return res;
484  end to_bitvector;
485
486  function to_stdulogicvector (b : bit_vector) return std_ulogic_vector is
487    subtype res_range is natural range b'length - 1 downto 0;
488    alias ab : bit_vector (res_range) is b;
489    variable res : std_ulogic_vector (res_range);
490  begin
491    for I in res_range loop
492      res (I) := bit_to_std (ab (I));
493    end loop;
494    return res;
495  end to_stdulogicvector;
496
497  function to_stdlogicvector (b : bit_vector) return std_logic_vector is
498    subtype res_range is natural range b'length - 1 downto 0;
499    alias ab : bit_vector (res_range) is b;
500    variable res : std_logic_vector (res_range);
501  begin
502    for I in res_range loop
503      res (I) := bit_to_std (ab (I));
504    end loop;
505    return res;
506  end to_stdlogicvector;
507
508  function to_stdulogicvector (s : std_logic_vector) return std_ulogic_vector
509  is
510    subtype res_type is std_ulogic_vector (s'length - 1 downto 0);
511  begin
512    return res_type (s);
513  end to_stdulogicvector;
514
515  function to_stdlogicvector (s : std_ulogic_vector) return std_logic_vector
516  is
517    subtype res_type is std_logic_vector (s'length - 1 downto 0);
518  begin
519    return res_type (s);
520  end to_stdlogicvector;
521
522  function to_stdulogic (b : bit) return std_ulogic is
523  begin
524    return bit_to_std (b);
525  end to_stdulogic;
526
527  --  Normalization.
528  type table_std_x01 is array (std_ulogic) of X01;
529  constant std_to_x01 : table_std_x01 := ('U' | 'X' | 'Z' | 'W' | '-' => 'X',
530                                          '0' | 'L'                   => '0',
531                                          '1' | 'H'                   => '1');
532
533  type table_bit_x01 is array (bit) of X01;
534  constant bit_to_x01 : table_bit_x01 := ('0' => '0',
535                                          '1' => '1');
536
537
538  type table_std_x01z is array (std_ulogic) of X01Z;
539  constant std_to_x01z : table_std_x01z := ('U' | 'X' | 'W' | '-' => 'X',
540                                            '0' | 'L'             => '0',
541                                            '1' | 'H'             => '1',
542                                            'Z'                   => 'Z');
543
544  type table_std_ux01 is array (std_ulogic) of UX01;
545  constant std_to_ux01 : table_std_ux01 := ('U'                   => 'U',
546                                            'X' | 'Z' | 'W' | '-' => 'X',
547                                            '0' | 'L'             => '0',
548                                            '1' | 'H'             => '1');
549
550
551  function to_X01 (s : std_ulogic_vector) return std_ulogic_vector
552  is
553    subtype res_type is std_ulogic_vector (1 to s'length);
554    alias sa : res_type is s;
555    variable res : res_type;
556  begin
557    for i in res_type'range loop
558      res (i) := std_to_x01 (sa (i));
559    end loop;
560    return res;
561  end to_X01;
562
563  function to_X01 (s : std_logic_vector) return std_logic_vector
564  is
565    subtype res_type is std_logic_vector (1 to s'length);
566    alias sa : res_type is s;
567    variable res : res_type;
568  begin
569    for i in res_type'range loop
570      res (i) := std_to_x01 (sa (i));
571    end loop;
572    return res;
573  end to_X01;
574
575  function to_X01 (s : std_ulogic) return X01 is
576  begin
577    return std_to_x01 (s);
578  end to_X01;
579
580  function to_X01 (b : bit_vector) return std_ulogic_vector
581  is
582    subtype res_range is natural range 1 to b'length;
583    alias ba : bit_vector (res_range) is b;
584    variable res : std_ulogic_vector (res_range);
585  begin
586    for i in res_range loop
587      res (i) := bit_to_x01 (ba (i));
588    end loop;
589    return res;
590  end to_X01;
591
592  function to_X01 (b : bit_vector) return std_logic_vector
593  is
594    subtype res_range is natural range 1 to b'length;
595    alias ba : bit_vector (res_range) is b;
596    variable res : std_logic_vector (res_range);
597  begin
598    for i in res_range loop
599      res (i) := bit_to_x01 (ba (i));
600    end loop;
601    return res;
602  end to_X01;
603
604  function to_X01 (b : bit) return X01 is
605  begin
606    return bit_to_x01 (b);
607  end to_X01;
608
609  function to_X01Z (s : std_ulogic_vector) return std_ulogic_vector
610  is
611    subtype res_type is std_ulogic_vector (1 to s'length);
612    alias sa : res_type is s;
613    variable res : res_type;
614  begin
615    for i in res_type'range loop
616      res (i) := std_to_x01z (sa (i));
617    end loop;
618    return res;
619  end to_X01Z;
620
621  function to_X01Z (s : std_logic_vector) return std_logic_vector
622  is
623    subtype res_type is std_logic_vector (1 to s'length);
624    alias sa : res_type is s;
625    variable res : res_type;
626  begin
627    for i in res_type'range loop
628      res (i) := std_to_x01z (sa (i));
629    end loop;
630    return res;
631  end to_X01Z;
632
633  function to_X01Z (s : std_ulogic) return X01Z is
634  begin
635    return std_to_x01z (s);
636  end to_X01Z;
637
638  function to_X01Z (b : bit_vector) return std_ulogic_vector
639  is
640    subtype res_range is natural range 1 to b'length;
641    alias ba : bit_vector (res_range) is b;
642    variable res : std_ulogic_vector (res_range);
643  begin
644    for i in res_range loop
645      res (i) := bit_to_x01 (ba (i));
646    end loop;
647    return res;
648  end to_X01Z;
649
650  function to_X01Z (b : bit_vector) return std_logic_vector
651  is
652    subtype res_range is natural range 1 to b'length;
653    alias ba : bit_vector (res_range) is b;
654    variable res : std_logic_vector (res_range);
655  begin
656    for i in res_range loop
657      res (i) := bit_to_x01 (ba (i));
658    end loop;
659    return res;
660  end to_X01Z;
661
662  function to_X01Z (b : bit) return X01Z is
663  begin
664    return bit_to_x01 (b);
665  end to_X01Z;
666
667  function to_UX01 (s : std_ulogic_vector) return std_ulogic_vector
668  is
669    subtype res_type is std_ulogic_vector (1 to s'length);
670    alias sa : res_type is s;
671    variable res : res_type;
672  begin
673    for i in res_type'range loop
674      res (i) := std_to_ux01 (sa (i));
675    end loop;
676    return res;
677  end to_UX01;
678
679  function to_UX01 (s : std_logic_vector) return std_logic_vector
680  is
681    subtype res_type is std_logic_vector (1 to s'length);
682    alias sa : res_type is s;
683    variable res : res_type;
684  begin
685    for i in res_type'range loop
686      res (i) := std_to_ux01 (sa (i));
687    end loop;
688    return res;
689  end to_UX01;
690
691  function to_UX01 (s : std_ulogic) return UX01 is
692  begin
693    return std_to_ux01 (s);
694  end to_UX01;
695
696  function to_UX01 (b : bit_vector) return std_ulogic_vector
697  is
698    subtype res_range is natural range 1 to b'length;
699    alias ba : bit_vector (res_range) is b;
700    variable res : std_ulogic_vector (res_range);
701  begin
702    for i in res_range loop
703      res (i) := bit_to_x01 (ba (i));
704    end loop;
705    return res;
706  end to_UX01;
707
708  function to_UX01 (b : bit_vector) return std_logic_vector
709  is
710    subtype res_range is natural range 1 to b'length;
711    alias ba : bit_vector (res_range) is b;
712    variable res : std_logic_vector (res_range);
713  begin
714    for i in res_range loop
715      res (i) := bit_to_x01 (ba (i));
716    end loop;
717    return res;
718  end to_UX01;
719
720  function to_UX01 (b : bit) return UX01 is
721  begin
722    return bit_to_x01 (b);
723  end to_UX01;
724
725  function rising_edge (signal s : std_ulogic) return boolean is
726  begin
727    return s'event
728      and to_x01 (s'last_value) = '0'
729      and to_x01 (s) = '1';
730  end rising_edge;
731
732  function falling_edge (signal s : std_ulogic) return boolean is
733  begin
734    return s'event
735      and to_x01 (s'last_value) = '1'
736      and to_x01 (s) = '0';
737  end falling_edge;
738
739  type std_x_array is array (std_ulogic) of boolean;
740  constant std_x : std_x_array := ('U' | 'X' | 'Z' | 'W' | '-' => true,
741                                   '0' | '1' | 'L' | 'H' => false);
742
743
744  function is_X (s : std_ulogic_vector) return boolean is
745  begin
746    for i in s'range loop
747      if std_x (s (i)) then
748        return true;
749      end if;
750    end loop;
751    return false;
752  end is_X;
753
754  function is_X (s : std_logic_vector) return boolean is
755  begin
756    for i in s'range loop
757      if std_x (s (i)) then
758        return true;
759      end if;
760    end loop;
761    return false;
762  end is_X;
763
764  function is_X (s : std_ulogic) return boolean is
765  begin
766    return std_x (s);
767  end is_X;
768end std_logic_1164;
769