OpenCores
URL https://opencores.org/ocsvn/t48/t48/trunk

Subversion Repositories t48

[/] [t48/] [tags/] [rel_1_0/] [rtl/] [vhdl/] [timer.vhd] - Diff between revs 4 and 59

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 4 Rev 59
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
--
--
-- The Timer/Counter unit.
-- The Timer/Counter unit.
--
--
-- $Id: timer.vhd,v 1.1 2004-03-23 21:31:53 arniml Exp $
-- $Id: timer.vhd,v 1.2 2004-04-15 22:05:13 arniml Exp $
--
--
-- All rights reserved
-- All rights reserved
--
--
-- Redistribution and use in source and synthezised forms, with or without
-- Redistribution and use in source and synthezised forms, with or without
-- modification, are permitted provided that the following conditions are met:
-- modification, are permitted provided that the following conditions are met:
--
--
-- Redistributions of source code must retain the above copyright notice,
-- Redistributions of source code must retain the above copyright notice,
-- this list of conditions and the following disclaimer.
-- this list of conditions and the following disclaimer.
--
--
-- Redistributions in synthesized form must reproduce the above copyright
-- Redistributions in synthesized form must reproduce the above copyright
-- notice, this list of conditions and the following disclaimer in the
-- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution.
-- documentation and/or other materials provided with the distribution.
--
--
-- Neither the name of the author nor the names of other contributors may
-- Neither the name of the author nor the names of other contributors may
-- be used to endorse or promote products derived from this software without
-- be used to endorse or promote products derived from this software without
-- specific prior written permission.
-- specific prior written permission.
--
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.
-- POSSIBILITY OF SUCH DAMAGE.
--
--
-- Please report bugs to the author, but before you do so, please
-- Please report bugs to the author, but before you do so, please
-- make sure that this is not a derivative work and that
-- make sure that this is not a derivative work and that
-- you have the latest version of this file.
-- you have the latest version of this file.
--
--
-- The latest version of this file can be found at:
-- The latest version of this file can be found at:
--      http://www.opencores.org/cvsweb.shtml/t48/
--      http://www.opencores.org/cvsweb.shtml/t48/
--
--
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
 
 
library ieee;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_1164.all;
 
 
use work.t48_pack.word_t;
use work.t48_pack.word_t;
use work.t48_pack.mstate_t;
use work.t48_pack.mstate_t;
 
 
entity timer is
entity timer is
 
 
  generic (
  generic (
    -- state in which T1 is sampled (3 or 4)
    -- state in which T1 is sampled (3 or 4)
    sample_t1_state_g : integer := 4
    sample_t1_state_g : integer := 4
  );
  );
 
 
  port (
  port (
    -- Global Interface -------------------------------------------------------
    -- Global Interface -------------------------------------------------------
    clk_i         : in  std_logic;
    clk_i         : in  std_logic;
    res_i         : in  std_logic;
    res_i         : in  std_logic;
    en_clk_i      : in  boolean;
    en_clk_i      : in  boolean;
    t1_i          : in  std_logic;
    t1_i          : in  std_logic;
    clk_mstate_i  : in  mstate_t;
    clk_mstate_i  : in  mstate_t;
    -- T48 Bus Interface ------------------------------------------------------
    -- T48 Bus Interface ------------------------------------------------------
    data_i        : in  word_t;
    data_i        : in  word_t;
    data_o        : out word_t;
    data_o        : out word_t;
    read_timer_i  : in  boolean;
    read_timer_i  : in  boolean;
    write_timer_i : in  boolean;
    write_timer_i : in  boolean;
    -- Decoder Interface ------------------------------------------------------
    -- Decoder Interface ------------------------------------------------------
    start_t_i     : in  boolean;
    start_t_i     : in  boolean;
    start_cnt_i   : in  boolean;
    start_cnt_i   : in  boolean;
    stop_tcnt_i   : in  boolean;
    stop_tcnt_i   : in  boolean;
    overflow_o    : out std_logic
    overflow_o    : out std_logic
  );
  );
 
 
end timer;
end timer;
 
 
 
 
library ieee;
library ieee;
use ieee.numeric_std.all;
use ieee.numeric_std.all;
 
 
use work.t48_pack.all;
use work.t48_pack.all;
 
 
architecture rtl of timer is
architecture rtl of timer is
 
 
  -- the 8 bit counter core
  -- the 8 bit counter core
  signal counter_q   : unsigned(word_t'range);
  signal counter_q   : unsigned(word_t'range);
  signal overflow_q  : boolean;
  signal overflow_q  : boolean;
 
 
  -- increment signal for the counter core
  -- increment signal for the counter core
  type   inc_type_t is (NONE, TIMER, COUNTER);
  type   inc_type_t is (NONE, TIMER, COUNTER);
  signal increment_s : boolean;
  signal increment_s : boolean;
  signal inc_sel_q   : inc_type_t;
  signal inc_sel_q   : inc_type_t;
 
 
  -- T1 edge detector
  -- T1 edge detector
  signal t1_q        : std_logic;
  signal t1_q        : std_logic;
  signal t1_inc_s    : boolean;
  signal t1_inc_s    : boolean;
 
 
  -- timer prescaler
  -- timer prescaler
  signal prescaler_q : unsigned(4 downto 0);
  signal prescaler_q : unsigned(4 downto 0);
  signal pre_inc_s   : boolean;
  signal pre_inc_s   : boolean;
 
 
begin
begin
 
 
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
  -- Verify the generics
  -- Verify the generics
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
 
 
  -- pragma translate_off
  -- pragma translate_off
  assert (sample_t1_state_g = 3) or (sample_t1_state_g = 4)
  assert (sample_t1_state_g = 3) or (sample_t1_state_g = 4)
    report "sample_t1_state_g must be either 3 or 4!"
    report "sample_t1_state_g must be either 3 or 4!"
    severity failure;
    severity failure;
  -- pragma translate_on
  -- pragma translate_on
 
 
 
 
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
  -- Process t1_edge
  -- Process t1_edge
  --
  --
  -- Purpose:
  -- Purpose:
  --   Implements the edge detector for T1.
  --   Implements the edge detector for T1.
  --
  --
  t1_edge: process (t1_i,
  t1_edge: process (t1_i,
                    t1_q,
                    t1_q,
                    clk_mstate_i)
                    clk_mstate_i)
  begin
  begin
    t1_inc_s     <= false;
    t1_inc_s     <= false;
 
 
    -- sample in state according to generic
    -- sample in state according to generic
    -- Old devices: sample at the beginning of state 3
    -- Old devices: sample at the beginning of state 3
    -- New devices: sample in state 4
    -- New devices: sample in state 4
    if (sample_t1_state_g = 3 and clk_mstate_i = MSTATE3) or
    if (sample_t1_state_g = 3 and clk_mstate_i = MSTATE3) or
       (sample_t1_state_g = 4 and clk_mstate_i = MSTATE3) then
       (sample_t1_state_g = 4 and clk_mstate_i = MSTATE3) then
      -- detect falling edge
      -- detect falling edge
      if t1_q = '1' and t1_i = '0' then
      if t1_q = '1' and t1_i = '0' then
        t1_inc_s <= true;
        t1_inc_s <= true;
      end if;
      end if;
    end if;
    end if;
 
 
  end process t1_edge;
  end process t1_edge;
  --
  --
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
 
 
 
 
  pre_inc_s <= clk_mstate_i = MSTATE3 and prescaler_q = 31;
  pre_inc_s <= clk_mstate_i = MSTATE4 and prescaler_q = 31;
 
 
 
 
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
  -- Process inc_sel
  -- Process inc_sel
  --
  --
  -- Purpose:
  -- Purpose:
  --   Select increment source (timer, counter or none).
  --   Select increment source (timer, counter or none).
  --
  --
  inc_sel: process (inc_sel_q,
  inc_sel: process (inc_sel_q,
                    pre_inc_s,
                    pre_inc_s,
                    t1_inc_s)
                    t1_inc_s)
  begin
  begin
    -- default assignment
    -- default assignment
    increment_s     <= false;
    increment_s     <= false;
 
 
    case inc_sel_q is
    case inc_sel_q is
      when NONE =>
      when NONE =>
        increment_s <= false;
        increment_s <= false;
      when TIMER =>
      when TIMER =>
        increment_s <= pre_inc_s;
        increment_s <= pre_inc_s;
      when COUNTER =>
      when COUNTER =>
        increment_s <= t1_inc_s;
        increment_s <= t1_inc_s;
      when others =>
      when others =>
        null;
        null;
    end case;
    end case;
 
 
  end process inc_sel;
  end process inc_sel;
  --
  --
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
 
 
 
 
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
  -- Process regs
  -- Process regs
  --
  --
  -- Purpose:
  -- Purpose:
  --   Implements the counter, the prescaler and other registers.
  --   Implements the counter, the prescaler and other registers.
  --
  --
  regs: process (res_i, clk_i)
  regs: process (res_i, clk_i)
  begin
  begin
    if res_i = res_active_c then
    if res_i = res_active_c then
      counter_q      <= (others => '0');
      counter_q      <= (others => '0');
      overflow_q     <= false;
      overflow_q     <= false;
      t1_q           <= '0';
      t1_q           <= '0';
      prescaler_q    <= (others => '0');
      prescaler_q    <= (others => '0');
      inc_sel_q      <= NONE;
      inc_sel_q      <= NONE;
 
 
    elsif clk_i'event and clk_i = clk_active_c then
    elsif clk_i'event and clk_i = clk_active_c then
      if en_clk_i then
      if en_clk_i then
 
 
        -- Counter Core and overflow ------------------------------------------
        -- Counter Core and overflow ------------------------------------------
        overflow_q     <= false;
        overflow_q     <= false;
 
 
        if write_timer_i then
        if write_timer_i then
          counter_q    <= unsigned(data_i);
          counter_q    <= unsigned(data_i);
 
 
        elsif increment_s then
        elsif increment_s then
          counter_q    <= counter_q + 1;
          counter_q    <= counter_q + 1;
 
 
          if counter_q = 255 then
          if counter_q = 255 then
            overflow_q <= true;
            overflow_q <= true;
          end if;
          end if;
 
 
        end if;
        end if;
 
 
        -- T1 edge detector ---------------------------------------------------
        -- T1 edge detector ---------------------------------------------------
        if (sample_t1_state_g = 3 and clk_mstate_i = MSTATE3) or
        if (sample_t1_state_g = 3 and clk_mstate_i = MSTATE3) or
          (sample_t1_state_g = 4 and clk_mstate_i = MSTATE4) then
           (sample_t1_state_g = 4 and clk_mstate_i = MSTATE4) then
          t1_q <= t1_i;
          t1_q <= t1_i;
        end if;
        end if;
 
 
        -- Prescaler ----------------------------------------------------------
        -- Prescaler ----------------------------------------------------------
        if start_t_i then
        if start_t_i then
          prescaler_q  <= (others => '0');
          prescaler_q  <= (others => '0');
 
 
        elsif clk_mstate_i = MSTATE3 then
        elsif clk_mstate_i = MSTATE3 then
          prescaler_q  <= prescaler_q + 1;
          prescaler_q  <= prescaler_q + 1;
 
 
        end if;
        end if;
 
 
        -- Increment Selector -------------------------------------------------
        -- Increment Selector -------------------------------------------------
        if start_t_i then
        if start_t_i then
          inc_sel_q <= TIMER;
          inc_sel_q <= TIMER;
        elsif start_cnt_i then
        elsif start_cnt_i then
          inc_sel_q <= COUNTER;
          inc_sel_q <= COUNTER;
        elsif stop_tcnt_i then
        elsif stop_tcnt_i then
          inc_sel_q <= NONE;
          inc_sel_q <= NONE;
        end if;
        end if;
 
 
      end if;
      end if;
 
 
    end if;
    end if;
 
 
  end process regs;
  end process regs;
  --
  --
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
 
 
 
 
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
  -- Output Mapping.
  -- Output Mapping.
  -----------------------------------------------------------------------------
  -----------------------------------------------------------------------------
  data_o     <=   std_logic_vector(counter_q)
  data_o     <=   std_logic_vector(counter_q)
                when read_timer_i else
                when read_timer_i else
                  (others => bus_idle_level_c);
                  (others => bus_idle_level_c);
  overflow_o <= to_stdLogic(overflow_q);
  overflow_o <= to_stdLogic(overflow_q);
 
 
end rtl;
end rtl;
 
 
 
 
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- File History:
-- File History:
--
--
-- $Log: not supported by cvs2svn $
-- $Log: not supported by cvs2svn $
 
-- Revision 1.1  2004/03/23 21:31:53  arniml
 
-- initial check-in
 
--
--
--
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.