library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity spi is
port (
cs : in std_logic; --negativo
sclk : in std_logic:='0';
mosi : in std_logic:='0';
miso : out std_logic;
addr : in std_logic_vector(3 downto 0);
digit1 : out std_logic_vector(6 downto 0);
digit2 : out std_logic_vector(6 downto 0);
digit3 : out std_logic_vector(6 downto 0);
digit4 : out std_logic_vector(6 downto 0)
);
end entity spi;
architecture behavior of spi is
type tBuff is array (0 to 15) of std_logic_vector(7 downto 0);
signal buff:tBuff := (x"20",x"21",x"22",x"23",x"24",x"25",x"26",x"27",x"28",x"29",x"2a",x"2b",x"2c",x"2d",x"2e",x"2f");
signal word : std_logic_vector (7 downto 0):=x"00";
signal wIndex: integer range 0 to 15:=15;
signal qBuff:tBuff := (x"30",x"31",x"32",x"33",x"34",x"35",x"36",x"37",x"38",x"39",x"3a",x"3b",x"3c",x"3d",x"3e",x"3f");
signal qWord : std_logic_vector (7 downto 0);
signal rIndex : integer range 0 to 15:=0;
signal n : integer range 0 to 7:=7;
begin
--**************************************************************
--Receiver
--**************************************************************
--Reseting bit counter
process(cs)
begin
if falling_edge(cs) then
if wIndex=15 then
wIndex<=0;
else
wIndex<=wIndex+1;
end if;
end if;
end process;
--Recepcion
process (sclk)
begin
if rising_edge(sclk) then
if cs='0' then
word<=word(6 downto 0) & mosi;
end if;
end if;
end process;
--Escritura a buffer circular
process (cs)
begin
if rising_edge(cs) then
buff(wIndex)<=word;
end if;
end process;
--**************************************************************
--Transmiter
--**************************************************************
--Reading
miso<=qWord(n);
--Reseting bit counter
process(cs)
begin
if falling_edge(cs) then
qWord<=qBuff(rIndex);
end if;
end process;
--bit counter
process (sclk)
begin
if falling_edge(sclk) then
if cs='0' then
if n=0 then
n<=7;
else
n<=n-1;
end if;
end if;
end if;
end process;
--Indice al buffer circular
process (sclk)
begin
if rising_edge(cs) then
if rIndex=15 then
rIndex<=0;
else
rIndex<=rIndex+1;
end if;
end if;
end process;
---**************************************
u1: entity work.dec7seg (behavior) port map (buff(to_integer(unsigned(addr)))( 3 downto 0),digit1);
u2: entity work.dec7seg (behavior) port map (buff(to_integer(unsigned(addr)))( 7 downto 4),digit2);
u3: entity work.dec7seg (behavior) port map (buff(to_integer(unsigned(addr)))( 3 downto 0),digit3);
u4: entity work.dec7seg (behavior) port map (buff(to_integer(unsigned(addr)))( 7 downto 4),digit4);
end architecture behavior;
|