]> rtime.felk.cvut.cz Git - fpga/pwm.git/blob - wave_table.vhd
Wave_table impure function.
[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       read(file_line, table(i));
54     end loop;
55
56     return table;
57   end function init_table_from_file;
58
59   
60   signal ram : wave_table_t := init_table_from_file(INIT_FILE);
61
62   signal stb_delayed : std_logic;
63
64 --------------------------------------------------------------------------------
65
66 begin
67
68   MEM : process (CLK_I) is
69     variable address : integer;
70   begin
71     address := conv_integer(ADR_I);
72     
73     if rising_edge(CLK_I) then
74       stb_delayed <= STB_I;
75
76       if STB_I = '1' then
77         if WE_I = '0' then
78           DAT_O <= to_stdLogicVector(ram(address));
79         else
80           ram(address) <= to_bitvector(DAT_I);
81         end if;
82       end if;
83     end if;
84   end process;
85
86
87   ACK_O <= STB_I and (stb_delayed or WE_I);
88   
89 end architecture behavioral;
90