Untitled

 avatar
unknown
plain_text
16 days ago
6.2 kB
4
Indexable
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.common_pack.all;

entity dataConsume is
      port (
          clk:            in std_logic;
            reset:        in std_logic; 
            start:        in std_logic;
            numWords_bcd: in BCD_ARRAY_TYPE(2 downto 0);
            ctrlIn:       in std_logic; 
            ctrlOut:      out std_logic; 
            data:         in std_logic_vector(7 downto 0);
            dataReady:    out std_logic := '0';
            byte:         out std_logic_vector(7 downto 0);
            seqDone:      out std_logic;
            maxIndex:     out BCD_ARRAY_TYPE(2 downto 0);
            dataResults:  out CHAR_ARRAY_TYPE(0 to RESULT_BYTE_NUM-1)
      );
end dataConsume;



architecture behavioural of dataConsume is
    --All signals declared
    type state_type is (IDLE, requestData, receiveData, processData, pulseDataReady);
    
    signal curState, nextState   : state_type;
    signal data_reg    : std_logic_vector(7 downto 0) := (others => '0');
    signal data_received : std_logic := '0';
    signal numWordsCounter : integer range 0 to 999;
    signal curPeak         : integer := 0;
    signal curMaxIndex     : integer range 0 to 999 := 0;
    signal dataResults_reg : CHAR_ARRAY_TYPE(0 to RESULT_BYTE_NUM-1) := (others => (others => '0'));
    signal prev3Bytes      : CHAR_ARRAY_TYPE(2 downto 0) := (others => (others => '0'));
    signal fillCount       : integer range 0 to 3;
    signal numWords_reg    : BCD_ARRAY_TYPE(2 downto 0);
    signal counter         : integer range 0 to 1000 := 0;
    signal maxIndex_reg    : BCD_ARRAY_TYPE(2 downto 0) := (others => (others => '0'));
    signal counter_reset   : std_logic := '0';
    
    -------------------------------------------------------------------------------
    function DecToBCD(input_dec : integer) return BCD_ARRAY_TYPE is
    variable bcd : BCD_ARRAY_TYPE(2 downto 0);
    begin
        bcd := (others => (others => '0')); -- Initialize bcd to 0
        -- Convert thousands digit
        bcd(2) := std_logic_vector(to_unsigned(input_dec / 100, 4));
        -- Convert hundreds digit
        bcd(1) := std_logic_vector(to_unsigned((input_dec mod 100) / 10, 4));
        -- Convert tens digit
        bcd(0) := std_logic_vector(to_unsigned((input_dec mod 100) mod 10, 4));
        return bcd;
    end DecToBCD;

    function bcd_to_decimal(bcd : BCD_ARRAY_TYPE(2 downto 0)) return integer is
        variable decimal : integer := 0;
    begin
        decimal := to_integer(unsigned(bcd(2))) * 100 +
                   to_integer(unsigned(bcd(1))) * 10 +
                   to_integer(unsigned(bcd(0)));
        return decimal;
    end function;
    -------------------------------------------------------------------------------

    begin--This is the begin statement for the whole process
    
    process(curState, clk, reset)
    begin
        if reset = '1' then
            curState <= IDLE;
        elsif rising_edge(clk) then
            curState <= nextState;
        end if;
    end process;

    process(clk, reset)
    begin
        if reset = '1' then
            numWords_reg <= (others => (others => '0'));
        elsif rising_edge(clk) then
            if start = '1' then
                numWords_reg <= numWords_bcd;
                numWordsCounter <= bcd_to_decimal(numWords_bcd);
            end if;
        end if;
    end process;

    process(clk, reset)
    begin
        if rising_edge(clk) then
            if reset = '1' then
                counter <= 0;
            elsif counter_reset = '1' then
                counter <= 0;           
            elsif ctrlIn = '1' and curState = receiveData then
                data_reg <= data;
                data_received <= '1';
                if to_integer(signed(data)) > curPeak and counter > 0 then
                    curPeak <= to_integer(signed(data));
                    maxIndex_reg <= DecToBCD(counter);

                    dataResults_reg(0 to 2) <= prev3Bytes;
                    dataResults_reg(3) <= data;
                    dataResults_reg(4 to 6) <= (others => (others => '0'));
                    fillCount <= 0;
                elsif fillCount < 3 then
                    dataResults_reg(4 + fillCount) <= data;
                    fillCount <= fillCount + 1;
                end if;
                counter <= counter + 1;
                prev3Bytes(2) <= prev3Bytes(1);
                prev3Bytes(1) <= prev3Bytes(0);
                prev3Bytes(0) <= data;
            end if;
            
            if curState = processData then
                data_received <= '0';
            end if;

        end if;
    end process;

    process(curState, data_received, start)
    begin
        
        ctrlOut <= '0';
        dataReady <= '0';
        seqDone <= '0';
        counter_reset <= '0';

        case curState is

            when IDLE =>
                if start = '1' then
                    ctrlOut <= '1';
                    nextState <= receiveData;
                else
                    nextState <= IDLE;
                end if;
            
            when receiveData =>
                if data_received = '1' then
                    nextState <= processData;
                else
                    nextState <= receiveData;
                end if;
            
            when processData =>
                    nextState <= pulseDataReady;

            when pulseDataReady =>
                dataReady <= '1';
                if counter = numWordsCounter then
                    seqDone <= '1';
                    nextState <= IDLE;
                    counter_reset <= '1';
                else
                    nextState <= IDLE;
                end if;
                
            when OTHERS =>
                nextState <= IDLE;
        end case;       
    end process;
    
    maxIndex <= maxIndex_reg;
    dataResults <= dataResults_reg;
    byte <= data_reg;

end behavioural;
Editor is loading...
Leave a Comment