Project

General

Profile

Download (4.43 KB) Statistics
| Branch: | Tag: | Revision:
1 d93979b7 Arnaud Dieumegard
2
-- Copyright (C) 1996 Morgan Kaufmann Publishers, Inc
3
4
-- This file is part of VESTs (Vhdl tESTs).
5
6
-- VESTs is free software; you can redistribute it and/or modify it
7
-- under the terms of the GNU General Public License as published by the
8
-- Free Software Foundation; either version 2 of the License, or (at
9
-- your option) any later version. 
10
11
-- VESTs is distributed in the hope that it will be useful, but WITHOUT
12
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
-- for more details. 
15
16
-- You should have received a copy of the GNU General Public License
17
-- along with VESTs; if not, write to the Free Software Foundation,
18
-- Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
19
20
-- ---------------------------------------------------------------------
21
--
22
-- $Id: bv_images_body.vhd,v 1.3 2001-10-26 16:29:33 paw Exp $
23
-- $Revision: 1.3 $
24
--
25
-- ---------------------------------------------------------------------
26
27
--------------------------------------------------------------------------
28
--
29
--  bv_images package body.
30
--
31
--  Functions that return the string image of values.
32
--  Each image is a correctly formed literal according to the
33
--  rules of VHDL-93.
34
--
35
--------------------------------------------------------------------------
36
37
package body bv_images is
38
39
40
  -- Image of bit vector as binary bit string literal
41
  -- (in the format B"...")
42
  -- Length of result is bv'length + 3
43
44
  function image (bv : in bit_vector) return string is
45
46
    alias bv_norm : bit_vector(1 to bv'length) is bv;
47
    variable result : string(1 to bv'length + 3);
48
49
  begin
50
    result(1) := 'B';
51
    result(2) := '"';
52
    for index in bv_norm'range loop
53
      if bv_norm(index) = '0' then
54
        result(index + 2) := '0';
55
      else
56
        result(index + 2) := '1';
57
      end if;
58
    end loop;
59
    result(bv'length + 3) := '"';
60
    return result;
61
  end image;
62
63
  ----------------------------------------------------------------
64
65
  -- Image of bit vector as octal bit string literal
66
  -- (in the format O"...")
67
  -- Length of result is (bv'length+2)/3 + 3
68
69
  function image_octal (bv : in bit_vector) return string is
70
71
    constant nr_digits : natural := (bv'length + 2) / 3;
72
    variable result : string(1 to nr_digits + 3);
73
    variable bits : bit_vector(0 to 3*nr_digits - 1) := (others => '0');
74
    variable three_bits : bit_vector(0 to 2);
75
    variable digit : character;
76
77
  begin
78
    result(1) := 'O';
79
    result(2) := '"';
80
    bits(bits'right - bv'length + 1 to bits'right) := bv;
81
    for index in 0 to nr_digits - 1 loop
82
      three_bits := bits(3*index to 3*index + 2);
83
      case three_bits is
84
	when b"000" =>
85
	  digit := '0';
86
	when b"001" =>
87
	  digit := '1';
88
	when b"010" =>
89
	  digit := '2';
90
	when b"011" =>
91
	  digit := '3';
92
	when b"100" =>
93
	  digit := '4';
94
	when b"101" =>
95
	  digit := '5';
96
	when b"110" =>
97
	  digit := '6';
98
	when b"111" =>
99
	  digit := '7';
100
      end case;
101
      result(index + 3) := digit;
102
    end loop;
103
    result(nr_digits + 3) := '"';
104
    return result;
105
  end image_octal;
106
107
  ----------------------------------------------------------------
108
109
  -- Image of bit vector as hex bit string literal
110
  -- (in the format X"...")
111
  -- Length of result is (bv'length+3)/4 + 3
112
113
  function image_hex (bv : in bit_vector) return string is
114
115
    constant nr_digits : natural := (bv'length + 3) / 4;
116
    variable result : string(1 to nr_digits + 3);
117
    variable bits : bit_vector(0 to 4*nr_digits - 1) := (others => '0');
118
    variable four_bits : bit_vector(0 to 3);
119
    variable digit : character;
120
121
  begin
122
    result(1) := 'X';
123
    result(2) := '"';
124
    bits(bits'right - bv'length + 1 to bits'right) := bv;
125
    for index in 0 to nr_digits - 1 loop
126
      four_bits := bits(4*index to 4*index + 3);
127
      case four_bits is
128
	when b"0000" =>
129
	  digit := '0';
130
	when b"0001" =>
131
	  digit := '1';
132
	when b"0010" =>
133
	  digit := '2';
134
	when b"0011" =>
135
	  digit := '3';
136
	when b"0100" =>
137
	  digit := '4';
138
	when b"0101" =>
139
	  digit := '5';
140
	when b"0110" =>
141
	  digit := '6';
142
	when b"0111" =>
143
	  digit := '7';
144
	when b"1000" =>
145
	  digit := '8';
146
	when b"1001" =>
147
	  digit := '9';
148
	when b"1010" =>
149
	  digit := 'A';
150
	when b"1011" =>
151
	  digit := 'B';
152
	when b"1100" =>
153
	  digit := 'C';
154
	when b"1101" =>
155
	  digit := 'D';
156
	when b"1110" =>
157
	  digit := 'E';
158
	when b"1111" =>
159
	  digit := 'F';
160
      end case;
161
      result(index + 3) := digit;
162
    end loop;
163
    result(nr_digits + 3) := '"';
164
    return result;
165
  end image_hex;
166
167
168
end bv_images;