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

Subversion Repositories artificial_neural_network

[/] [artificial_neural_network/] [trunk/] [test_bench/] [src/] [ann_tb.vhd] - Blame information for rev 10

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 jstefanowi
-------------------------------------------------------------------------------
2
-- Title      : Testbench for design "ann". XOR solving neural network.
3
-- Project    : 
4
-------------------------------------------------------------------------------
5
-- File       : ann_tb.vhd
6
-- Author     : Jurek Stefanowicz
7
-- Company    : 
8
-- Created    : 2016-09-30
9
-- Last update: 2016-09-30
10
-- Platform   : 
11
-- Standard   : VHDL'87
12
-------------------------------------------------------------------------------
13
-- Description: 
14
-------------------------------------------------------------------------------
15
-- Copyright (c) 2016
16
-------------------------------------------------------------------------------
17
 
18
library ieee;
19
use ieee.std_logic_1164.all;
20
use ieee.numeric_std.all;
21
use ieee.math_real.all;
22
 
23
library std;
24
use std.textio.all;
25
 
26
library work;
27
use work.layers_pkg.all;
28
use work.support_pkg.all;
29
 
30
-------------------------------------------------------------------------------
31
 
32
entity ann_tb is
33
end ann_tb;
34
 
35
-------------------------------------------------------------------------------
36
 
37
architecture beh1 of ann_tb is
38
 
39
  -- testbech signals
40
  signal end_sim : boolean := false;
41
 
42
  file data_out        : text open write_mode is "data_out_tb.txt";
43
  file data_in         : text open read_mode is "data_in.txt";
44
 
45
  signal reset   : std_logic := '1';
46
  signal clk     : std_logic := '1';
47
 
48
  -- ann input sigs
49
  signal run_in  : std_logic := '0'; -- Start and input data validation
50
  signal inputs  : std_logic_vector(Nbit-1 downto 0) := (others => '0'); -- Input data
51
 
52
  -- weight&bias memory interface (not used)
53
  signal wdata   : std_logic_vector(NbitW-1 downto 0) := (others => '0');  -- Weight and bias memory write data
54
  signal addr    : std_logic_vector((calculate_addr_l(NumIn, NumN, Nlayer)+log2(Nlayer))-1 downto 0) := (others => '0'); -- Weight and bias memory address
55
  signal m_en    : std_logic := '0'; -- Weight and bias memory enable (external interface)
56
  signal m_we    : std_logic_vector(((NbitW+7)/8)-1 downto 0) := (others => '0');
57
 
58
  signal run_out : std_logic; -- Output data validation
59
  signal rdata   : std_logic_vector(NbitW-1 downto 0);  -- Weight and bias memory read data
60
  signal outputs : std_logic_vector(Nbit-1 downto 0); -- Output data
61
 
62
 
63
begin
64
  -- component instantiation
65
  ann0 : entity work.ann
66
    generic map (
67
      WBinit   => true,
68
      Nlayer   => Nlayer,
69
      NbitW    => NbitW,
70
      NumIn    => NumIn,
71
      NbitIn   => Nbit,
72
      NumN     => NumN,
73
      l_type   => l_type,
74
      f_type   => f_type,
75
      LSbit    => LSbit,
76
      NbitO    => NbitO,
77
      NbitOut  => NbitOut)
78
    port map (
79
      -- in
80
      reset    => reset,
81
      clk      => clk,
82
      run_in   => run_in,
83
      m_en     => m_en,
84
      m_we     => m_we,
85
      inputs   => inputs,
86
      wdata    => wdata,
87
      addr     => addr,
88
      -- out
89
      run_out  => run_out,
90
      rdata    => rdata,
91
      outputs  => outputs
92
    );
93
  -- clock generation
94
  Clk <= not Clk after 10 ns when end_sim = false else '0';
95
 
96
  -- xor wieghts:
97
  -- layer0:                          addresses:
98
  -- weights:                         layer bias  neuron input     
99
  --   neuron 1, input 1 : -3.7596     0     0     0      0  
100
  --   neuron 1, input 2 :  3.0396     0     0     0      1   
101
  --   neuron 2, input 1 :  2.3740     0     0     1      0    
102
  --   neuron 2, input 2 : -2.1895     0     0     1      1    
103
  -- bias:                            layer bias    x   neuron       
104
  --   neuron 1 : 2.20762               0     1     0      0              
105
  --   neuron 2 : 0.96043               0     1     0      1                
106
  -- layer1:                                
107
  -- weights:                         layer bias  neuron input            
108
  --   neuron 1, input 1 :  -2.2381     1     0     0      0                               
109
  --   neuron 1, input 2 :  -2.2888     1     0     0      1                                 
110
  -- bias:                            layer bias    x   neuron    
111
  --   neuron 1: 3.8896                 1     1     0      0              
112
  --
113
 
114
  DataSave : process(Clk)
115
    variable my_line : line;  -- type 'line' comes from textio
116
  begin
117
    if (Clk'event and Clk = '1' ) then
118
      if ( run_out = '1') then
119
        write(my_line,  to_integer(signed(outputs)));
120
        writeline(data_out, my_line);
121
      end if;
122
    end if;
123
  end process;
124
 
125
  DataLoad : process
126
    variable input_line  : line;
127
    variable din         : real;
128
  begin
129
    wait for 20 ns;
130
    reset <= '0';
131
    wait until clk = '0';
132
    wait until clk = '1';
133
    wait until clk = '0';
134
    wait until clk = '1';
135
 
136
    l1 : while not end_sim loop
137
      if not endfile(data_in) then
138
        readline(data_in, input_line);
139
        read(input_line, din);
140
      else
141
        end_sim <= true;
142
        exit l1;
143
      end if;
144
      run_in <= '1';
145
      inputs  <= std_logic_vector(to_signed(integer(din*(2.0**LSB_In)),NbitIn));
146
 
147
      wait until clk = '0';
148
      wait until clk = '1';
149
      run_in <= '0';
150
 
151
      -- We wait 4 clock cycles between run_ins because
152
      -- the network has a maximum layers size of 3 neurons
153
 
154
      wait until clk = '0';
155
      wait until clk = '1';
156
 
157
      wait until clk = '0';
158
      wait until clk = '1';
159
 
160
      wait until clk = '0';
161
      wait until clk = '1';
162
 
163
      --wait until clk = '0';
164
      --wait until clk = '1';
165
 
166
    end loop l1;
167
    wait ;
168
  end process;
169
 
170
end beh1;
171
 
172
 

powered by: WebSVN 2.1.0

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