library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.NUMERIC_STD.ALL; Package Types is type cache_type is array (63 downto 0) of std_logic_vector(155 downto 0); type bloco is array (3 downto 0) of std_logic_vector(15 downto 0); -- BLOCO DE 4 PALAVRAS DE 16 BITS type TRam is array(0 to 4095) of std_logic_vector(15 downto 0); End Types; library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; use ieee.std_logic_arith.all; USE IEEE.STD_LOGIC_TEXTIO.ALL; USE STD.TEXTIO.ALL; use work.Types.all; entity controlMem is port (CLK : in std_logic; WR_MAR : in std_logic; --0 se for ler 1 se for escrever MARaddress: in std_logic_vector(11 downto 0); AUX: out bloco; ADDaux: out std_logic_vector(11 downto 0); DataProc: out std_logic_vector(15 downto 0)); --dado enviado ao processador end entity; architecture algoritmica of controlMem is signal cacheMemory: cache_type :=(others => (others=>'0')); -- ESSA É A CACHE !!! signal TAG1,TAG2:std_logic_vector(3 downto 0); signal Position:std_logic_vector(5 downto 0); signal V,Valid,Modif: std_logic:='0'; signal Palavra :std_logic_vector (1 downto 0); signal cont0,cont1 :integer; signal Cache_IN,Cache_OUT :std_logic_vector(155 downto 0); signal dadoMP:bloco:=(others => (others=>'0')); signal endereco_aux: std_logic_vector(11 downto 0); signal noobDelay ,noobFat7y: std_logic := '0'; signal dado: std_logic_vector(15 downto 0); impure function init_bram (ram_file_name : in string) return TRam is file ramfile : text open READ_MODE is ram_file_name; variable line_read : line; variable temp:bit_vector(15 downto 0); variable ram_to_return : TRam; begin for i in TRam'range loop readline(ramfile, line_read); read(line_read, temp); ram_to_return(i):=to_stdlogicvector(temp); end loop; return ram_to_return; end function; signal Ram : TRam := init_bram("MEMRAM.txt"); function validade(validBitIn:std_logic;validTag1,validTag2:std_logic_vector(3 downto 0)) return std_logic is begin return (validTag1(0) xnor validTag2(0)) and (validTag1(1) xnor validTag2(1)) and (validTag1(2) xnor validTag2(2)) and (validTag1(3) xnor validTag2(3)) and validBitIn; end validade; function mux4x1(muxIn1,muxIn2,muxIn3,muxIn4:STD_LOGIC_VECTOR(15 DOWNTO 0);muxControl:STD_LOGIC_VECTOR(1 DOWNTO 0)) return STD_LOGIC_VECTOR is begin if muxControl="00" then return muxIn1; elsif muxControl="01" then return muxIn2; elsif muxControl="10" then return muxIn3; else return muxIn4; end if; end mux4x1; begin process(CLK) variable tempMiss : std_logic; begin if rising_edge(CLK) then if (WR_MAR='0') then --leitura!! Position<=MARaddress(7 downto 2); Palavra<=MARaddress(1 downto 0); TAG1<=MARaddress(11 downto 8); Cache_OUT<=cacheMemory(conv_integer(Position)); --acessou a linha pedida TAG2<= Cache_OUT(67 downto 64); --primeiro tenta o primeiro conjunto V<=Cache_OUT(77); Valid<=validade(V,TAG1,TAG2); --testa se é valido if (Valid='1') then DataProc<=mux4x1(Cache_OUT(15 downto 0),Cache_OUT(31 downto 16), Cache_OUT(47 downto 32),Cache_OUT(63 downto 48),Palavra); cacheMemory(conv_integer(Position))(75 downto 68)<=cacheMemory(conv_integer(Position))(75 downto 68)+1; else --tenta o segundo conjunto TAG2<= Cache_OUT(145 downto 142); V<=Cache_OUT(155); Valid<=validade(V,TAG1,TAG2); if (Valid='1') then DataProc<=mux4x1(Cache_OUT(93 downto 78),Cache_OUT(109 downto 94), Cache_OUT(125 downto 110),Cache_OUT(141 downto 126),Palavra); cacheMemory(conv_integer(Position))(153 downto 146)<=cacheMemory(conv_integer(Position))(153 downto 146)+1; else --nao foi encontrado em nenhum dos dois conjuntos endereco_aux(11 downto 8)<=TAG1; endereco_aux(7 downto 2)<=Position; endereco_aux(1 downto 0)<="00"; dadoMP(0)<=Ram(conv_integer(endereco_aux)); -- bloco trazido da MP AUX(0)<=dadoMP(0); ADDaux<=endereco_aux; endereco_aux(1 downto 0)<="01"; dadoMP(1)<=Ram(conv_integer(endereco_aux)); -- bloco trazido da MP AUX(1)<=dadoMP(1); ADDaux<=endereco_aux; endereco_aux(1 downto 0)<="10"; dadoMP(2)<=Ram(conv_integer(endereco_aux)); -- bloco trazido da MP AUX(2)<=dadoMP(2); ADDaux<=endereco_aux; endereco_aux(1 downto 0)<="11"; dadoMP(3)<=Ram(conv_integer(endereco_aux)); -- bloco trazido da MP AUX(3)<=dadoMP(3); ADDaux<=endereco_aux; cont1<= conv_integer(cacheMemory(conv_integer(Position))(153 downto 146)); cont0<= conv_integer(cacheMemory(conv_integer(Position))(75 downto 68)); if (cont0 >= cont1) then --conjunto 0 foi acessado mais vezes, logo o 1 vai ser substituido cacheMemory(conv_integer(Position))(155)<='1'; cacheMemory(conv_integer(Position))(154)<='0'; cacheMemory(conv_integer(Position))(153 downto 146)<=conv_std_logic_vector(0,8); cacheMemory(conv_integer(Position))(145 downto 142)<=TAG1; cacheMemory(conv_integer(Position))(93 downto 78)<=dadoMP(0); cacheMemory(conv_integer(Position))(109 downto 94)<=dadoMP(1); cacheMemory(conv_integer(Position))(125 downto 110)<=dadoMP(2); cacheMemory(conv_integer(Position))(141 downto 126)<=dadoMP(3); DataProc<=mux4x1(cacheMemory(conv_integer(Position))(93 downto 78),cacheMemory(conv_integer(Position))(109 downto 94), cacheMemory(conv_integer(Position))(125 downto 110),cacheMemory(conv_integer(Position))(141 downto 126),Palavra); else --conjunto 1 foi acessado mais vezes,logo o 0 vai ser substituido cacheMemory(conv_integer(Position))(77)<='1'; cacheMemory(conv_integer(Position))(76)<='0'; cacheMemory(conv_integer(Position))(75 downto 68)<=conv_std_logic_vector(0,8); cacheMemory(conv_integer(Position))(67 downto 64)<=TAG1; cacheMemory(conv_integer(Position))(63 downto 48)<=dadoMP(3); cacheMemory(conv_integer(Position))(47 downto 32)<=dadoMP(2); cacheMemory(conv_integer(Position))(31 downto 16)<=dadoMP(1); cacheMemory(conv_integer(Position))(15 downto 0)<=dadoMP(0); DataProc<=mux4x1(cacheMemory(conv_integer(Position))(15 downto 0),cacheMemory(conv_integer(Position))(31 downto 16), cacheMemory(conv_integer(Position))(47 downto 32),cacheMemory(conv_integer(Position))(63 downto 48),Palavra); end if; end if; end if; else --escrita!! end if; end if; end process; end algoritmica;