-
Notifications
You must be signed in to change notification settings - Fork 57
/
utils.vhd
133 lines (120 loc) · 2.81 KB
/
utils.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.NUMERIC_STD.all;
package utils is
function log2(
N : integer)
return integer;
function log2_f(
N : integer)
return integer;
function conditional (
constant a : boolean;
constant if_true : in integer;
constant if_false : in integer)
return integer;
function branch_pack_signal (
pc : std_logic_vector;
target : std_logic_vector;
taken : std_logic;
flush : std_logic;
enable : std_logic)
return std_logic_vector;
function branch_get_pc (
sig : std_logic_vector)
return std_logic_vector;
function branch_get_tgt (
sig : std_logic_vector)
return std_logic_vector;
function branch_get_taken (
sig : std_logic_vector)
return std_logic;
function branch_get_flush (
sig : std_logic_vector)
return std_logic;
function branch_get_enable (
sig : std_logic_vector)
return std_logic;
end utils;
package body utils is
function log2_f(
N : integer)
return integer is
variable i : integer := 0;
begin
while (2**i <= n) loop
i := i + 1;
end loop;
return i-1;
end log2_f;
function log2(
N : integer)
return integer is
variable i : integer := 0;
begin
while (2**i < n) loop
i := i + 1;
end loop;
return i;
end log2;
function conditional (
constant a : boolean;
constant if_true : in integer;
constant if_false : in integer)
return integer is
begin
if a then
return if_true;
else
return if_false;
end if;
end conditional;
function ceil_log2(i : natural) return integer is
variable temp : integer := i;
variable ret_val : integer := 0;
begin
while temp > 1 loop
ret_val := ret_val + 1;
temp := temp / 2;
end loop;
return ret_val;
end function;
function branch_pack_signal (
pc : std_logic_vector;
target : std_logic_vector;
taken : std_logic;
flush : std_logic;
enable : std_logic)
return std_logic_vector is
begin
return pc & target & taken & flush & enable;
end function;
function branch_get_pc (
sig : std_logic_vector)
return std_logic_vector is
begin
return sig(sig'left downto sig'left-((sig'length-3)/2)+1);
end function;
function branch_get_tgt (
sig : std_logic_vector)
return std_logic_vector is
begin
return sig(sig'left-(sig'length-3)/2 downto 3);
end function;
function branch_get_taken (
sig : std_logic_vector) return std_logic is
begin
return sig(2);
end function;
function branch_get_flush (
sig : std_logic_vector) return std_logic is
begin
return sig(1);
end function;
function branch_get_enable (
sig : std_logic_vector)
return std_logic is
begin
return sig(0);
end function;
end utils;