aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/gna/issue635/ram.vhdl
blob: e28225673b5ea5db60e22b86877643f611b5d9c1 (plain)
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
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity Ram is
	generic
	(
		addressWidth : in positive;
		busWidth : in positive;
		size : in positive
	);
	port
	(
		clk : in std_logic;
		address : in unsigned(addressWidth - 1 downto 0);
		writeEnable : in std_logic;
		dataIn : in std_logic_vector(busWidth - 1 downto 0);
		dataOut : out std_logic_vector(busWidth - 1 downto 0)
	);
end Ram;

architecture Behavioral of Ram is
	constant alignment : positive := busWidth / 8;
	constant ramSize : positive := size / alignment;

	type RamType is array(natural range <>) of std_logic_vector(busWidth - 1 downto 0);
	subtype RamRange is natural range 0 to ramSize;

	signal ram : RamType(RamRange);
begin
	process(clk)
		variable index : RamRange;
	begin
		if (rising_edge(clk))
		then
			index := to_integer(address) / alignment;

			if (writeEnable = '1')
			then
				ram(index) <= dataIn;
			end if;

			dataOut <= ram(index);
		end if;
	end process;
end Behavioral;