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

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [rtl/] [commonlib/] [types_util.vhd] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sergeykhbr
-----------------------------------------------------------------------------
2
--! @file
3
--! @copyright Copyright 2015 GNSS Sensor Ltd. All right reserved.
4
--! @author    Sergey Khabarov - sergeykhbr@gmail.com
5
--! @brief          Package for common testbenches implementation.
6
------------------------------------------------------------------------------
7
library ieee;
8
use ieee.std_logic_1164.all;
9
use ieee.numeric_std.all;
10
library std;
11
use std.textio.all;
12
 
13
package types_util is
14
 
15
function strlen(s: in string) return integer;
16
function StringToUVector(inStr: string) return std_ulogic_vector;
17
function StringToSVector(inStr: string) return std_logic_vector;
18
function UnsignedToSigned(inUnsigned: std_ulogic_vector) return std_logic_vector;
19
function SignalFromString(inStr: string; ind : integer ) return std_logic;
20
function SymbolToSVector(inStr: string; idx: integer) return std_logic_vector;
21
 
22
function tost(v:std_logic_vector) return string;
23
function tost(v:std_logic) return string;
24
function tost(i : integer) return string;
25
procedure print(s : string);
26
 
27
end;
28
 
29
package body types_util is
30
 
31
  function strlen(s: in string) return integer is
32
    variable n: integer:=0; variable sj: integer:=s'left;
33
  begin
34
    loop
35
      if    sj>s'right then exit;
36
      elsif s(sj)=NUL  then exit; --sequential if protects sj > length
37
      else                  sj:=sj+1; n:=n+1;
38
      end if;
39
    end loop;
40
    return n;
41
  end strlen;
42
 
43
  function SignalFromString(inStr: string; ind : integer ) return std_logic is
44
    variable temp: std_logic := 'X';
45
  begin
46
    if(inStr(inStr'high-ind)='1')    then temp := '1';
47
    elsif(inStr(inStr'high-ind)='0') then temp := '0';
48
    end if;
49
    return temp;
50
  end function SignalFromString;
51
 
52
 
53
  function StringToUVector(inStr: string) return std_ulogic_vector is
54
    variable temp: std_ulogic_vector(inStr'range) := (others => 'X');
55
  begin
56
    for i in inStr'range loop --
57
      if(inStr(inStr'high-i+1)='1')    then temp(i) := '1';
58
      elsif(inStr(inStr'high-i+1)='0') then temp(i) := '0';
59
      end if;
60
    end loop;
61
    return temp(inStr'high downto 1);
62
  end function StringToUVector;
63
  -- conversion function
64
 
65
  function StringToSVector(inStr: string) return std_logic_vector is
66
    variable temp: std_logic_vector(inStr'range) := (others => 'X');
67
  begin
68
    for i in inStr'range loop --
69
      if(inStr(inStr'high-i+1)='1')    then temp(i) := '1';
70
      elsif(inStr(inStr'high-i+1)='0') then temp(i) := '0';
71
      end if;
72
    end loop;
73
    return temp(inStr'high downto 1);
74
  end function StringToSVector;
75
 
76
  function SymbolToSVector(inStr: string; idx: integer) return std_logic_vector is
77
    constant ss: string(1 to inStr'length) := inStr;
78
    variable c : integer;
79
    variable temp: std_logic_vector(7 downto 0) := (others => 'X');
80
  begin
81
    c := character'pos(ss(idx+1));
82
    for i in 0 to 7 loop --
83
      temp(i) := to_unsigned(c,8)(i);
84
    end loop;
85
    return temp;
86
  end function SymbolToSVector;
87
 
88
 
89
  function UnsignedToSigned(inUnsigned: std_ulogic_vector)
90
    return std_logic_vector is
91
    variable temp: std_logic_vector(inUnsigned'length-1 downto 0) := (others => 'X');
92
    variable i: integer:=0;
93
  begin
94
    while i < inUnsigned'length loop
95
      if(inUnsigned(i)='1')    then temp(i) := '1';
96
      elsif(inUnsigned(i)='0') then temp(i) := '0';
97
      end if;
98
      i := i+1;
99
    end loop;
100
    return temp;
101
  end function UnsignedToSigned;
102
 
103
 
104
  subtype nibble is std_logic_vector(3 downto 0);
105
 
106
  function todec(i:integer) return character is
107
  begin
108
    case i is
109
    when 0 => return('0');
110
    when 1 => return('1');
111
    when 2 => return('2');
112
    when 3 => return('3');
113
    when 4 => return('4');
114
    when 5 => return('5');
115
    when 6 => return('6');
116
    when 7 => return('7');
117
    when 8 => return('8');
118
    when 9 => return('9');
119
    when others => return('0');
120
    end case;
121
  end;
122
 
123
 
124
  function tohex(n:nibble) return character is
125
  begin
126
    case n is
127
    when "0000" => return('0');
128
    when "0001" => return('1');
129
    when "0010" => return('2');
130
    when "0011" => return('3');
131
    when "0100" => return('4');
132
    when "0101" => return('5');
133
    when "0110" => return('6');
134
    when "0111" => return('7');
135
    when "1000" => return('8');
136
    when "1001" => return('9');
137
    when "1010" => return('a');
138
    when "1011" => return('b');
139
    when "1100" => return('c');
140
    when "1101" => return('d');
141
    when "1110" => return('e');
142
    when "1111" => return('f');
143
    when others => return('X');
144
    end case;
145
  end;
146
 
147
 
148
  function tost(v:std_logic_vector) return string is
149
    constant vlen : natural := v'length; --'
150
    constant slen : natural := (vlen+3)/4;
151
    variable vv : std_logic_vector(0 to slen*4-1) := (others => '0');
152
    variable s : string(1 to slen);
153
    variable nz : boolean := false;
154
    variable index : integer := -1;
155
  begin
156
    vv(slen*4-vlen to slen*4-1) := v;
157
    for i in 0 to slen-1 loop
158
      if (vv(i*4 to i*4+3) = "0000") and nz and (i /= (slen-1)) then
159
        index := i;
160
      else
161
        nz := false;
162
        s(i+1) := tohex(vv(i*4 to i*4+3));
163
      end if;
164
    end loop;
165
    if ((index +2) = slen) then return(s(slen to slen));
166
    else return(string'("0x") & s(index+2 to slen)); end if; --'
167
  end;
168
 
169
 
170
  function tost(v:std_logic) return string is
171
  begin
172
    if to_x01(v) = '1' then return("1"); else return("0"); end if;
173
  end;
174
 
175
 
176
  function tost(i : integer) return string is
177
    variable L : line;
178
    variable s, x : string(1 to 128);
179
    variable n, tmp : integer := 0;
180
  begin
181
    tmp := i;
182
    if i < 0 then tmp := -i; end if;
183
    loop
184
      s(128-n) := todec(tmp mod 10);
185
      tmp := tmp / 10;
186
      n := n+1;
187
      if tmp = 0 then exit; end if;
188
    end loop;
189
    x(1 to n) := s(129-n to 128);
190
    if i < 0 then return "-" & x(1 to n); end if;
191
    return(x(1 to n));
192
  end;
193
 
194
  procedure print(s : string) is
195
    variable L : line;
196
  begin
197
    L := new string'(s); writeline(output, L);
198
  end;
199
 
200
end;

powered by: WebSVN 2.1.0

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