-
Notifications
You must be signed in to change notification settings - Fork 4
/
tb_XNOR_net.vhd
137 lines (114 loc) · 3.32 KB
/
tb_XNOR_net.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
134
135
136
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
library ieee_proposed;
use IEEE_PROPOSED.fixed_pkg.all;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use work.Common.all;
entity tb_XNOR_net is
end tb_XNOR_net;
architecture Behavioral of tb_XNOR_net is
component xnor_popcount_32
Port ( weights : in matrix_bin_type;
nn_in : in std_logic_vector(7 downto 0);
nn_out : out vector_fixed_point_type);
end component;
component xnor_popcount_8
Port ( weights : in matrix_bin_type;
nn_in : in std_logic_vector(7 downto 0);
nn_out : out vector_fixed_point_type);
end component;
component scalar_multiplier_ard
Port ( scalar : in sfixed (8 downto -7);
nn_in : in vector_fixed_point_type;
nn_out : out sfixed_fixed_point_type);
end component;
component scalar_multiplier_last
Port ( scalar : in sfixed (8 downto -7);
nn_in : in sfixed_fixed_point_type;
nn_out : out sfixed_fixed_point_type);
end component;
--Inputs
signal weights : matrix_bin_type;
signal nn_in : std_logic_vector(7 downto 0);
signal alpha : sfixed (8 downto -7);
signal beta : sfixed (8 downto -7);
--Intermediate Nodes
signal nn_after_pop : vector_fixed_point_type;
signal nn_after_alpha : sfixed_fixed_point_type;
--Outputs
signal nn_out : sfixed_fixed_point_type;
--Clocks
signal clk : std_logic;
--Testbench Related
constant period : time := 10 ns;
BEGIN
-- feed in binary weights and binary input
pop: xnor_popcount_8 PORT MAP (
weights => weights,
nn_in => nn_in,
nn_out => nn_after_pop
);
-- multiply by alpha
sm0: scalar_multiplier_ard PORT MAP (
scalar => alpha,
nn_in => nn_after_pop,
nn_out => nn_after_alpha
);
-- multiply by beta
sm1: scalar_multiplier_last PORT MAP (
scalar => beta,
nn_in => nn_after_alpha,
nn_out => nn_out
);
-- process definitions
process1 :process
variable i : integer := 0;
begin
-- simulating RAM operation here
-- clock cycle 1
wait for period/2;
clk <= '0';
wait for period/2;
clk <= '1';
-- assign 8x8 weight matrix (binary)
weights <= (x"01",x"33",x"45",x"67",x"89",x"AB",x"CD",x"EF");
-- assign 1x8 input matrix (binary)
nn_in <= x"63";
-- scalar from weights normalization
alpha <= to_sfixed (2.5,7,-8);
beta <= to_sfixed (2.0,7,-8);
-- clock cycle 2
wait for period/2;
clk <= '0';
wait for period/2;
clk <= '1';
-- assign 8x8 weight matrix (binary)
weights <= (x"22",x"55",x"66",x"88",x"11",x"12",x"34",x"71");
-- assign 1x8 input matrix (binary)
nn_in <= x"A9";
-- scalar from weights normalization
alpha <= to_sfixed (-1.5,7,-8);
beta <= to_sfixed (1.0,7,-8);
-- clock cycle 3
wait for period/2;
clk <= '0';
wait for period/2;
clk <= '1';
-- assign 8x8 weight matrix (binary)
weights <= (x"55",x"33",x"45",x"64",x"89",x"AB",x"CD",x"EF");
-- assign 1x8 input matrix (binary)
nn_in <= x"78";
-- scalar from weights normalization
alpha <= to_sfixed (5.5,7,-8);
beta <= to_sfixed (8.0,7,-8);
end process;
-- Stimulus process
-- stim_proc: process
-- begin
-- wait for period/2;
-- CLK <= not CLK;
-- end process;
end Behavioral;