]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - hw/lx-fncapprox/rom_table.vhd
Approximated function block computes reciprocal, sine and cosine functions.
[fpga/lx-cpu1/lx-rocon.git] / hw / lx-fncapprox / rom_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 -- ROM based 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 rom_table is
23         generic (
24                 data_width : integer := 10;
25                 addr_width : integer := 9;
26                 init_file  : string  := "reci_tab_a.lut");
27         port (
28                 ack_o  : out std_logic;
29                 addr_i : in  std_logic_vector (addr_width-1 downto 0);
30                 clk_i  : in  std_logic;
31                 data_o : out std_logic_vector (data_width-1 downto 0);
32                 stb_i  : in  std_logic
33         );
34 end entity rom_table;
35
36 --------------------------------------------------------------------------------
37
38 architecture behavioral of rom_table is
39
40         constant table_size : integer := 2**addr_width;
41
42         type rom_table_t is array (0 to table_size-1) of bit_vector (data_width-1 downto 0);
43
44         impure function init_table_from_file (file_name : string) return rom_table_t is
45                 file table_file    : text open read_mode is file_name;
46                 variable file_line : line;
47                 variable table     : rom_table_t;
48         begin
49                 for i in rom_table_t'range loop
50                         loop
51                                 if (endfile(table_file)) and (i /= 0) then
52                                         -- Repeat file data when not enough data to fill
53                                         file_close(table_file);
54                                         file_open(table_file, file_name, read_mode);
55                                 end if;
56                                 readline(table_file, file_line);
57                                 exit when file_line'length /= 0;
58                         end loop;
59
60                         read(file_line, table(i));
61
62                 end loop;
63
64                 return table;
65         end function init_table_from_file;
66
67         constant ram : rom_table_t := init_table_from_file(init_file);
68
69         signal stb_delayed : std_logic;
70
71 --------------------------------------------------------------------------------
72
73 begin
74
75 mem_context : process (clk_i, addr_i) is
76         variable address : integer;
77         begin
78                 address := conv_integer(addr_i);
79
80                 if rising_edge(clk_i) then
81                         stb_delayed <= stb_i;
82
83                         if stb_i = '1' then
84                                 data_o <= to_stdLogicVector(ram(address));
85                         end if;
86                 end if;
87         end process;
88
89         ack_o <= stb_delayed;
90
91 end architecture behavioral;