OpenCores
URL https://opencores.org/ocsvn/spi_slave/spi_slave/trunk

Subversion Repositories spi_slave

[/] [spi_slave/] [trunk/] [bench/] [vhdl/] [images-body.vhd] - Blame information for rev 12

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 dkoethe
--------------------------------------------------------------------------
2
--
3
--  Copyright (C) 1993, Peter J. Ashenden
4
--  Mail:       Dept. Computer Science
5
--              University of Adelaide, SA 5005, Australia
6
--  e-mail:     petera@cs.adelaide.edu.au
7
--
8
--  This program is free software; you can redistribute it and/or modify
9
--  it under the terms of the GNU General Public License as published by
10
--  the Free Software Foundation; either version 1, or (at your option)
11
--  any later version.
12
--
13
--  This program is distributed in the hope that it will be useful,
14
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
--  GNU General Public License for more details.
17
--
18
--  You should have received a copy of the GNU General Public License
19
--  along with this program; if not, write to the Free Software
20
--  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
--
22
--------------------------------------------------------------------------
23
--
24
--  $RCSfile: images-body.vhd,v $  $Revision: 1.1 $  $Date: 2007-11-30 20:22:01 $
25
--
26
--------------------------------------------------------------------------
27
--
28
--  Images package body.
29
--
30
--  Functions that return the string image of values.
31
--  Each image is a correctly formed literal according to the
32
--  rules of VHDL-93.
33
--
34
--------------------------------------------------------------------------
35
library ieee;
36
use ieee.std_logic_1164.all;
37
 
38
package images is
39
  function image (
40
    constant bv : std_logic_vector)
41
    return string;
42
end images;
43
 
44
package body images is
45
 
46
 
47
  -- Image of bit vector as binary bit string literal
48
  -- (in the format B"...")
49
  -- Length of result is bv'length + 3
50
 
51
  function image (bv : in bit_vector) return string is
52
 
53
    alias bv_norm   : bit_vector(1 to bv'length) is bv;
54
    variable result : string(1 to bv'length + 3);
55
 
56
  begin
57
    result(1) := 'B';
58
    result(2) := '"';
59
    for index in bv_norm'range loop
60
      if bv_norm(index) = '0' then
61
        result(index + 2) := '0';
62
      else
63
        result(index + 2) := '1';
64
      end if;
65
    end loop;
66
    result(bv'length + 3) := '"';
67
    return result;
68
  end image;
69
-------------------------------------------------------------------------------
70
  -- Image of bit vector as binary bit string literal
71
  -- (in the format B"...")
72
  -- Length of result is bv'length + 3
73
 
74
  function image (bv : in std_logic_vector) return string is
75
 
76
    alias bv_norm   : std_logic_vector(1 to bv'length) is bv;
77
    variable result : string(1 to bv'length + 3);
78
 
79
  begin
80
    result(1) := 'B';
81
    result(2) := '"';
82
    for index in bv_norm'range loop
83
      if bv_norm(index) = '0' then
84
        result(index + 2) := '0';
85
      else
86
        result(index + 2) := '1';
87
      end if;
88
    end loop;
89
    result(bv'length + 3) := '"';
90
    return result;
91
  end image;
92
 
93
 
94
 
95
  ----------------------------------------------------------------
96
 
97
  -- Image of bit vector as octal bit string literal
98
  -- (in the format O"...")
99
  -- Length of result is (bv'length+2)/3 + 3
100
 
101
  function image_octal (bv : in bit_vector) return string is
102
 
103
    constant nr_digits  : natural                          := (bv'length + 2) / 3;
104
    variable result     : string(1 to nr_digits + 3);
105
    variable bits       : bit_vector(0 to 3*nr_digits - 1) := (others => '0');
106
    variable three_bits : bit_vector(0 to 2);
107
    variable digit      : character;
108
 
109
  begin
110
    result(1)                                      := 'O';
111
    result(2)                                      := '"';
112
    bits(bits'right - bv'length + 1 to bits'right) := bv;
113
    for index in 0 to nr_digits - 1 loop
114
      three_bits := bits(3*index to 3*index + 2);
115
      case three_bits is
116
        when b"000" =>
117
          digit := '0';
118
        when b"001" =>
119
          digit := '1';
120
        when b"010" =>
121
          digit := '2';
122
        when b"011" =>
123
          digit := '3';
124
        when b"100" =>
125
          digit := '4';
126
        when b"101" =>
127
          digit := '5';
128
        when b"110" =>
129
          digit := '6';
130
        when b"111" =>
131
          digit := '7';
132
      end case;
133
      result(index + 3) := digit;
134
    end loop;
135
    result(nr_digits + 3) := '"';
136
    return result;
137
  end image_octal;
138
 
139
  ----------------------------------------------------------------
140
 
141
  -- Image of bit vector as hex bit string literal
142
  -- (in the format X"...")
143
  -- Length of result is (bv'length+3)/4 + 3
144
 
145
  function image_hex (bv : in bit_vector) return string is
146
 
147
    constant nr_digits : natural                          := (bv'length + 3) / 4;
148
    variable result    : string(1 to nr_digits + 3);
149
    variable bits      : bit_vector(0 to 4*nr_digits - 1) := (others => '0');
150
    variable four_bits : bit_vector(0 to 3);
151
    variable digit     : character;
152
 
153
  begin
154
    result(1)                                      := 'X';
155
    result(2)                                      := '"';
156
    bits(bits'right - bv'length + 1 to bits'right) := bv;
157
    for index in 0 to nr_digits - 1 loop
158
      four_bits := bits(4*index to 4*index + 3);
159
      case four_bits is
160
        when b"0000" =>
161
          digit := '0';
162
        when b"0001" =>
163
          digit := '1';
164
        when b"0010" =>
165
          digit := '2';
166
        when b"0011" =>
167
          digit := '3';
168
        when b"0100" =>
169
          digit := '4';
170
        when b"0101" =>
171
          digit := '5';
172
        when b"0110" =>
173
          digit := '6';
174
        when b"0111" =>
175
          digit := '7';
176
        when b"1000" =>
177
          digit := '8';
178
        when b"1001" =>
179
          digit := '9';
180
        when b"1010" =>
181
          digit := 'A';
182
        when b"1011" =>
183
          digit := 'B';
184
        when b"1100" =>
185
          digit := 'C';
186
        when b"1101" =>
187
          digit := 'D';
188
        when b"1110" =>
189
          digit := 'E';
190
        when b"1111" =>
191
          digit := 'F';
192
      end case;
193
      result(index + 3) := digit;
194
    end loop;
195
    result(nr_digits + 3) := '"';
196
    return result;
197
  end image_hex;
198
 
199
 
200
end images;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.