GCC Code Coverage Report
Directory: generated/vunit_out/preprocessed/ Exec Total Coverage
File: generated/vunit_out/preprocessed/common/types_pkg.vhd Lines: 41 47 87.2 %
Date: 2021-06-12 04:12:08 Branches: 74 145 51.0 %

Line Branch Exec Source
1




26511576
-- -------------------------------------------------------------------------------------------------
2
-- Copyright (c) Lukas Vik. All rights reserved.
3
--
4
-- This file is part of the tsfpga project.
5
-- https://tsfpga.com
6
-- https://gitlab.com/tsfpga/tsfpga
7
-- -------------------------------------------------------------------------------------------------
8
9
library ieee;
10
use ieee.numeric_std.all;
11
use ieee.std_logic_1164.all;
12
13
14
package types_pkg is
15
16
  type natural_vec_t is array (integer range <>) of natural;
17
  type positive_vec_t is array (integer range <>) of positive;
18
  type boolean_vec_t is array (integer range <>) of boolean;
19
20
  function get_maximum(values : positive_vec_t) return positive;
21
22
  function to_sl(value : boolean) return std_logic;
23
  function to_bool(value : std_logic) return boolean;
24
  function to_bool(value : natural) return boolean;
25
26
  subtype binary_integer_t is integer range 0 to 1;
27
  function to_int(value : boolean) return binary_integer_t;
28
  function to_int(value : std_logic) return binary_integer_t;
29
30
  function swap_byte_order(data : std_logic_vector) return std_logic_vector;
31
  function swap_bit_order(data : std_logic_vector) return std_logic_vector;
32
33
  function count_ones(data : std_logic_vector) return natural;
34
35
end package;
36
37
package body types_pkg is
38
39
  function get_maximum(values : positive_vec_t) return positive is
40
    -- Minimum possible value
41
4
    variable result : positive := 1;
42
  begin
43
4
    for value_idx in values'range loop
44



16
      result := maximum(result, values(value_idx));
45
    end loop;
46


4
    return result;
47
  end function;
48
49
  function to_sl(value : boolean) return std_logic is
50
  begin
51
15554524
    if value then
52
8103832
      return '1';
53
    end if;
54
7450692
    return '0';
55
  end function;
56
57
  function to_bool(value : std_logic) return boolean is
58
  begin
59
4
    if value = '1' then
60
2
      return true;
61
2
    elsif value = '0' then
62
2
      return false;
63
    end if;
64
    assert false report "Can not convert value " & to_string(value) severity failure;
65
    return false;
66
  end function;
67
68
  function to_bool(value : natural) return boolean is
69
  begin
70
4
    if value = 1 then
71
2
      return true;
72
    end if;
73
2
    if value = 0 then
74
2
      return false;
75
    end if;
76
77
    assert false report "Can not convert this value " & to_string(value) severity failure;
78
    return false;
79
  end function;
80
81
  function to_int(value : boolean) return binary_integer_t is
82
  begin
83
15590
    if value then
84
98
      return 1;
85
    else
86
15492
      return 0;
87
    end if;
88
  end function;
89
90
  function to_int(value : std_logic) return binary_integer_t is
91
  begin
92
10887348
    if value = '1' then
93
568956
      return 1;
94
10318392
    elsif value = '0' then
95
10318392
      return 0;
96
    end if;
97
98
    assert false report "Can not convert value " & to_string(value) severity warning;
99
    return 0;
100
  end function;
101
102
  function swap_byte_order(data : std_logic_vector) return std_logic_vector is
103

132
    variable result : std_logic_vector(data'range);
104
4
    constant num_bytes : integer := data'length / 8;
105
4
    variable result_byte_idx : integer;
106
  begin
107
    -- Swap endianness of the input word.
108
    -- I.e., while maintaining the range and vector direction, swap the location of the data bytes.
109
110
4
    assert data'left > data'right report "Only use with descending range" severity failure;
111
4
    assert data'length mod 8 = 0 report "Must be a whole number of bytes" severity failure;
112
113
4
    for input_byte_idx in 0 to num_bytes - 1 loop
114
16
      result_byte_idx := num_bytes - 1 - input_byte_idx;
115






16
      result(result'low + result_byte_idx * 8 + 7 downto result'low + result_byte_idx * 8) :=
116
        data(data'low + input_byte_idx * 8 + 7 downto data'low + input_byte_idx * 8);
117
    end loop;
118
119
4
    return result;
120
  end function;
121
122
  function swap_bit_order(data : std_logic_vector) return std_logic_vector is
123
8
    constant length : positive := data'length;
124

56
    variable result : std_logic_vector(data'range);
125
  begin
126
    -- While maintaining the range and vector direction, swap the location of the data bits.
127
128
8
    for idx in 0 to length - 1 loop
129



48
      result(result'low + idx) := data(data'high - idx);
130
    end loop;
131
132
8
    return result;
133
  end function;
134
135
  function count_ones(data : std_logic_vector) return natural is
136

53454
    variable result : integer range 0 to data'length := 0;
137
  begin
138
53454
    for bit_idx in data'range loop
139


102328
      result := result + to_int(data(bit_idx));
140
    end loop;
141

53772
    return result;
142
  end function;
143
144
end package body;