]> rtime.felk.cvut.cz Git - fpga/pwm.git/blob - wave_table.vhd
Wave_table initialization data format modified.
[fpga/pwm.git] / wave_table.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
5
6 library std;
7 use std.textio.all;
8
9 --------------------------------------------------------------------------------
10 -- Waveform Look Up Table
11 --
12 -- It's based on behavioral description of full synchronous RAM, so it should be
13 -- mapped into FPGA BRAM. Interface is Wishbone like. Data and address bus width
14 -- is configurable.
15 --
16 -- Table is initialized from file specified by 'INIT_FILE' parameter. This is a
17 -- text file which contains one value per line. Values are typed in binary
18 -- format.  Sample file 'sin.lut' together with Matlab generation function
19 -- 'gen_lut_sin.m' is enclosed.
20 --------------------------------------------------------------------------------
21
22 entity wave_table is
23   generic (
24     DAT_W     : integer := 10;
25     ADR_W     : integer := 9;
26     INIT_FILE : string  := "sin.lut");
27   port (
28     ACK_O : out std_logic;
29     ADR_I : in  std_logic_vector (ADR_W-1 downto 0);
30     CLK_I : in  std_logic;
31     DAT_I : in  std_logic_vector (DAT_W-1 downto 0);
32     DAT_O : out std_logic_vector (DAT_W-1 downto 0);
33     STB_I : in  std_logic;
34     WE_I  : in  std_logic);
35 end entity wave_table;
36
37 --------------------------------------------------------------------------------
38
39 architecture behavioral of wave_table is
40
41   constant SIZE : integer := 2**ADR_W;
42   
43   type wave_table_t is array (0 to SIZE-1) of bit_vector (DAT_W-1 downto 0);
44
45   
46   impure function init_table_from_file (file_name : string) return wave_table_t is
47     file table_file    : text open read_mode is file_name;
48     variable file_line : line;
49     variable table     : wave_table_t;
50   begin
51     for i in wave_table_t'range loop
52       readline(table_file, file_line);
53
54       if endfile(table_file) then
55         file_close(table_file);
56         file_open(table_file, file_name, read_mode);
57
58         readline(table_file, file_line);
59       end if;
60
61       read(file_line, table(i));
62     end loop;
63
64     return table;
65   end function init_table_from_file;
66
67   
68   signal ram : wave_table_t := init_table_from_file(INIT_FILE);
69
70   signal stb_delayed : std_logic;
71
72 --------------------------------------------------------------------------------
73
74 begin
75
76   MEM : process (CLK_I) is
77     variable address : integer;
78   begin
79     address := conv_integer(ADR_I);
80     
81     if rising_edge(CLK_I) then
82       stb_delayed <= STB_I;
83
84       if STB_I = '1' then
85         if WE_I = '0' then
86           DAT_O <= to_stdLogicVector(ram(address));
87         else
88           ram(address) <= to_bitvector(DAT_I);
89         end if;
90       end if;
91     end if;
92   end process;
93
94
95   ACK_O <= STB_I and (stb_delayed or WE_I);
96   
97 end architecture behavioral;
98