Advertisement
Guest User

Untitled

a guest
May 26th, 2017
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 6.08 KB | None | 0 0
  1. --Francesco Cervellera [francesco.cervellera@gmail.com (+39)3346169924]
  2. --SX2 Register Byte Reader
  3.  
  4. library ieee;
  5. use ieee.std_logic_1164.all;
  6.  
  7. entity SX2RegisterByteReader is
  8.  
  9.     port(
  10.     --Sync port
  11.         CLK      : in   std_logic; --Clock for the FSM
  12.         RESET    : in   std_logic; --Async Reset
  13.    
  14.     --In port
  15.         READREQ  : in   std_logic; --REQUEST a read from a register of SX2. The FSM will take WORKING active untill the process is running.
  16.         READLAST : in   std_logic; --put in output the last readed byte from the register
  17.        
  18.         ADDR     : in   std_logic_vector(5 downto 0); --The Register Address from which FSM will read
  19.  
  20.     --Out port
  21.         OUTLAST  : out  std_logic; --Rised when the last requested data is held by the fsm
  22.         DATA     : out  std_logic_vector(7 downto 0); --The Data That FSM will READ
  23.        
  24.         WORKING  : out  std_logic; --This signal is actived when the FSM is running
  25.  
  26.         USB_READY   : in std_logic;  --Ref SX2 datasheet
  27.         USB_FIFOADR : out std_logic_vector(2 downto 0); --Nedded to choice the command functionality
  28.        
  29.         CMDW_DATA   : out std_logic_vector(7 downto 0); --Data read from the SX2CommandReader
  30.         CMDW_WREQ   : out std_logic; --Signal
  31.         CMDW_WRITING: in std_logic;
  32.        
  33.         CMDR_DATA   : in std_logic_vector(7 downto 0); --Data read from the SX2CommandReader
  34.         CMDR_DATATYPE: in std_logic; --Data type read;
  35.         CMDR_RLREQ  : out std_logic;
  36.         CMDR_RREQ   : out std_logic; --Signal
  37.         CMDR_OUTL   : in std_logic;
  38.         CMDR_READING: in std_logic  
  39.     );
  40.  
  41. end entity;
  42.  
  43. architecture rtl of SX2RegisterByteReader is
  44.  
  45.     type state_type is (s_IDLE, s_SETCMD, s_WRITEADR, s_WAITADR, s_READREQ, s_WAITREADREQ, s_READ, s_STOREDATA, s_OUTLAST);
  46.     signal state    : state_type;
  47.     signal QRunning : std_logic;
  48.    
  49.     signal QOutLast : std_logic;
  50.     signal QReadRequest : std_logic;
  51.     signal QReadLastRequest : std_logic;
  52.     signal QDataReadOUT : std_logic_vector(7 downto 0);
  53.    
  54.     signal QWriteRequest : std_logic;
  55.     signal QDataWriteOUT    : std_logic_vector(7 downto 0);
  56.    
  57. begin
  58.  
  59.     -- Logic to advance to the next state
  60.     process (CLK, RESET)
  61.  
  62.     begin
  63.         if RESET = '0' then
  64.             state <= s_IDLE;
  65.         elsif (rising_edge(CLK)) then
  66.             case state is
  67.                 when s_IDLE=>
  68.                 --Wait until the external logic dosen't request for a write
  69.                     if READREQ = '1' then
  70.                         state <= s_SETCMD;
  71.                     elsif READLAST = '1' then
  72.                         state <= s_OUTLAST;
  73.                     else
  74.                         state <= s_IDLE;
  75.                     end if;
  76.                 when s_OUTLAST =>
  77.                     if READLAST = '0' then
  78.                         state <= s_IDLE;
  79.                     else
  80.                         state <= s_OUTLAST;
  81.                     end if;
  82.                 when s_SETCMD=>
  83.                 --Set USB_FIFOADR to [100] and wait for the ack from the SX2
  84.                     if USB_READY = '1' then
  85.                         state <= s_WRITEADR;
  86.                     else
  87.                         state <= s_SETCMD;
  88.                     end if;
  89.                 when s_WRITEADR=>
  90.                 --Write register's address using SX2CmdXXXXWrite
  91.                     state <= s_WAITADR;
  92.                 when s_WAITADR =>
  93.                 --Wait until SX2CmdXXXXWrite finish the write sequence
  94.                     if CMDW_WRITING = '0' then
  95.                         state <= s_READREQ;
  96.                     else
  97.                         state <= s_WAITADR;
  98.                     end if;
  99.                 when s_READREQ =>
  100.                 --Post a request of read to SX2CmdXXXXRead
  101.                     state <= s_WAITREADREQ;
  102.                 when s_WAITREADREQ =>
  103.                 --Wait until SX2CmdXXXXRead finish the read sequence
  104.                     if CMDR_READING = '0' then
  105.                         state <= s_READ;
  106.                     else
  107.                         state <= s_WAITREADREQ;
  108.                     end if;
  109.                 when s_READ =>
  110.                 --Read the last read byte from SX2CmdXXXXRead
  111.                     if CMDR_OUTL = '1' then
  112.                         if CMDR_DATATYPE = '1' then
  113.                             state <= s_STOREDATA;
  114.                         else
  115.                             state <= s_WRITEADR;
  116.                         --!!!!!!!!!!!!!!ATTENZIONE !!!!!!!!!!!!!!!
  117.                         --PERDITA DATI INTERRUPT
  118.                         end if;
  119.                     else
  120.                         state <= s_READ;
  121.                     end if;
  122.                 when s_STOREDATA =>
  123.                 --Store the data read into internal register
  124.                     state <= s_IDLE;
  125.             end case;
  126.         end if;
  127.     end process;
  128.  
  129.     -- Output depends solely on the current state
  130.     process (state)
  131.     variable QAddrIn : std_logic_vector(5 downto 0);
  132.     variable QData   : std_logic_vector(7 downto 0);
  133.     begin
  134.         case state is
  135.             when s_IDLE =>
  136.                 --Set this flag to 0
  137.                 QRunning <= '0';
  138.                 QOutLast <= '0';
  139.                 QDataWriteOUT <= (others => 'X');
  140.                
  141.                 QWriteRequest <= 'X';
  142.                 QReadRequest <= 'X';
  143.             when s_OUTLAST =>
  144.                 QRunning <= '0';
  145.                 QOutLast <= '1';
  146.                
  147.                 QDataReadOUT <= QData;
  148.                
  149.                 QWriteRequest <= 'X';
  150.                 QReadRequest <= 'X';
  151.             when s_SETCMD =>
  152.                 QRunning <= '1';
  153.                 QOutLast <= '0';
  154.                 --Record The Addr to be used
  155.                 QAddrIn := ADDR;
  156.                 QDataWriteOUT <= (others => 'X');
  157.                 QWriteRequest <= '0';
  158.                 QReadRequest <= '0';
  159.             when s_WRITEADR =>
  160.                 QRunning <= '1';
  161.                 QOutLast <= '0';
  162.                 --Query for read the QAddrIn
  163.                 QDataWriteOUT(7) <= '1';
  164.                 QDataWriteOUT(6) <= '1';
  165.                 QDataWriteOUT(5 downto 0) <= QAddrIn;
  166.                 QReadRequest <= '0';
  167.                 QWriteRequest <= '1'; --Submit the request to the SX2CmdXXXXWrite
  168.             when s_WAITADR =>
  169.                 QRunning <= '1';
  170.                 QOutLast <= '0';
  171.                 --Query for read the QAddrIn
  172.                 QDataWriteOUT(7) <= '1';
  173.                 QDataWriteOUT(6) <= '1';
  174.                 QDataWriteOUT(5 downto 0) <= QAddrIn;
  175.                 QReadRequest <= '0';
  176.                 QWriteRequest <= '0';
  177.             when s_READREQ =>
  178.                 QRunning <= '1';
  179.                 QOutLast <= '0';
  180.                 QDataWriteOUT <= (others => 'Z');
  181.                 QReadRequest <= '1';
  182.                 QWriteRequest <= '0';
  183.             when s_WAITREADREQ =>
  184.                 QRunning <= '1';
  185.                 QOutLast <= '0';
  186.                 QDataWriteOUT <= (others => 'Z');
  187.                 QReadRequest <= '0';
  188.                 QWriteRequest <= '0';
  189.             when s_READ =>
  190.                 QRunning <= '1';
  191.                 QOutLast <= '0';
  192.                 CMDR_RLREQ <= '1';
  193.                 QDataWriteOUT <= (others => 'Z');
  194.                 QReadRequest <= '0';
  195.                 QWriteRequest <= '0';
  196.             when s_STOREDATA =>
  197.                 QRunning <= '1';
  198.                 QOutLast <= '0';
  199.                 CMDR_RLREQ <= '0';
  200.                 QData := CMDR_DATA;
  201.                 QReadRequest <= '0';
  202.                 QWriteRequest <= '0';
  203.         end case;
  204.     end process;
  205.    
  206.    
  207.    
  208.     --TriState Port !
  209.     DATA <= (others => 'Z') when (QOutLast = '0') else QDataReadOUT;
  210.     OUTLAST <= QOutLast;
  211.    
  212.     USB_FIFOADR <= (others => 'Z') when (QRunning = '0') else "100";
  213.     CMDW_DATA <= (others => 'Z') when (QRunning = '0') else QDataWriteOUT;
  214.     CMDW_WREQ <= 'Z' when (QRunning = '0') else QWriteRequest;
  215.     CMDR_RREQ <= 'Z' when (QRunning = '0') else QReadRequest;
  216.     WORKING <= QRunning;
  217. end rtl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement