-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildingblocks.vhdl
93 lines (80 loc) · 2.47 KB
/
buildingblocks.vhdl
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
-- shift left by two
library IEEE; use IEEE.STD_LOGIC_1164.all;
entity sl2 is
port(a: in STD_LOGIC_VECTOR(31 downto 0);
y: out STD_LOGIC_VECTOR(31 downto 0));
end;
architecture behave of sl2 is
begin
y <= a(29 downto 0) & "00";
end;
-- sign extension
library IEEE; use IEEE.STD_LOGIC_1164.all; use IEEE.NUMERIC_STD.all;
entity signext is generic (width_in, width_out: integer);
port(a: in STD_LOGIC_VECTOR(width_in-1 downto 0);
y: out STD_LOGIC_VECTOR(width_out-1 downto 0));
end;
architecture behave of signext is
begin
y <= std_logic_vector(resize(signed(a), width_out));
end;
-- multiplexer
library IEEE; use IEEE.STD_LOGIC_1164.all; use IEEE.NUMERIC_STD.all;
entity mux2 is
generic(width: integer);
port(d0, d1: in STD_LOGIC_VECTOR(width-1 downto 0);
s: in STD_LOGIC;
y: out STD_LOGIC_VECTOR(width-1 downto 0));
end;
architecture behave of mux2 is
begin
y <= d0 when s = '0' else d1;
end;
library IEEE; use IEEE.STD_LOGIC_1164.all; use IEEE.NUMERIC_STD.all;
entity mux4 is
generic(width: integer);
port(d0, d1, d2, d3: in STD_LOGIC_VECTOR(width-1 downto 0);
s: in STD_LOGIC_VECTOR(1 downto 0);
y: out STD_LOGIC_VECTOR(width-1 downto 0));
end;
architecture behave of mux4 is
component mux2 generic(width: integer);
port(d0, d1: in STD_LOGIC_VECTOR(width-1 downto 0);
s: in STD_LOGIC;
y: out STD_LOGIC_VECTOR(width-1 downto 0));
end component;
signal res1, res2: STD_LOGIC_VECTOR(width-1 downto 0);
begin
my_mux1: mux2 generic map(width => width) port map(d0, d1, s(0), res1);
my_mux2: mux2 generic map(width => width) port map(d2, d3, s(0), res2);
my_mux3: mux2 generic map(width => width) port map(res1, res2, s(1), y);
end;
-- inverter
library IEEE; use IEEE.STD_LOGIC_1164.all; use IEEE.NUMERIC_STD.all;
entity inverter is
generic(width: integer);
port(input: in STD_LOGIC_VECTOR(width-1 downto 0);
output: out STD_LOGIC_VECTOR(width-1 downto 0)
);
end;
architecture behave of inverter is
begin
output <= not input;
end behave;
-- or
library IEEE; use IEEE.STD_LOGIC_1164.all; use IEEE.NUMERIC_STD.all;
entity or_gate is
generic(width: integer);
port(a: in STD_LOGIC_VECTOR(width-1 downto 0);
y: out STD_LOGIC
);
end entity;
architecture behave of or_gate is
signal temp: STD_LOGIC_VECTOR(width downto 0);
begin
temp(0) <= '0';
gate: for i in 0 to width-1 generate
temp(i+1) <= a(i) or temp(i);
end generate;
y <= temp(width);
end behave;