-- $Id: debounce_gen.vhd 418 2011-10-23 20:11:40Z mueller $
|
-- $Id: debounce_gen.vhd 418 2011-10-23 20:11:40Z mueller $
|
--
|
--
|
-- Copyright 2007-2011 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
|
-- Copyright 2007-2011 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
|
--
|
--
|
-- This program is free software; you may redistribute and/or modify it under
|
-- This program is free software; you may redistribute and/or modify it under
|
-- the terms of the GNU General Public License as published by the Free
|
-- the terms of the GNU General Public License as published by the Free
|
-- Software Foundation, either version 2, or at your option any later version.
|
-- Software Foundation, either version 2, or at your option any later version.
|
--
|
--
|
-- This program is distributed in the hope that it will be useful, but
|
-- This program is distributed in the hope that it will be useful, but
|
-- WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
|
-- WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
-- for complete details.
|
-- for complete details.
|
--
|
--
|
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
-- Module Name: debounce_gen - syn
|
-- Module Name: debounce_gen - syn
|
-- Description: Generic signal debouncer
|
-- Description: Generic signal debouncer
|
--
|
--
|
-- Dependencies: -
|
-- Dependencies: -
|
-- Test bench: tb/tb_debounce_gen
|
-- Test bench: tb/tb_debounce_gen
|
-- Target Devices: generic
|
-- Target Devices: generic
|
-- Tool versions: xst 8.2, 9.1, 9.2, 13.1; ghdl 0.18-0.29
|
-- Tool versions: xst 8.2, 9.1, 9.2, 13.1; ghdl 0.18-0.29
|
-- Revision History:
|
-- Revision History:
|
-- Date Rev Version Comment
|
-- Date Rev Version Comment
|
-- 2011-10-22 418 1.0.3 now numeric_std clean
|
-- 2011-10-22 418 1.0.3 now numeric_std clean
|
-- 2007-12-26 105 1.0.2 add default for RESET
|
-- 2007-12-26 105 1.0.2 add default for RESET
|
-- 2007-10-12 88 1.0.1 avoid ieee.std_logic_unsigned, use cast to unsigned
|
-- 2007-10-12 88 1.0.1 avoid ieee.std_logic_unsigned, use cast to unsigned
|
-- 2007-06-29 61 1.0 Initial version
|
-- 2007-06-29 61 1.0 Initial version
|
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
|
|
library ieee;
|
library ieee;
|
use ieee.std_logic_1164.all;
|
use ieee.std_logic_1164.all;
|
use ieee.numeric_std.all;
|
use ieee.numeric_std.all;
|
|
|
use work.slvtypes.all;
|
use work.slvtypes.all;
|
|
|
entity debounce_gen is -- debounce, generic vector
|
entity debounce_gen is -- debounce, generic vector
|
generic (
|
generic (
|
CWIDTH : positive := 2; -- clock interval counter width
|
CWIDTH : positive := 2; -- clock interval counter width
|
CEDIV : positive := 3; -- clock interval divider
|
CEDIV : positive := 3; -- clock interval divider
|
DWIDTH : positive := 8); -- data width
|
DWIDTH : positive := 8); -- data width
|
port (
|
port (
|
CLK : in slbit; -- clock
|
CLK : in slbit; -- clock
|
RESET : in slbit := '0'; -- reset
|
RESET : in slbit := '0'; -- reset
|
CE_INT : in slbit; -- clock interval enable (usec or msec)
|
CE_INT : in slbit; -- clock interval enable (usec or msec)
|
DI : in slv(DWIDTH-1 downto 0); -- data in
|
DI : in slv(DWIDTH-1 downto 0); -- data in
|
DO : out slv(DWIDTH-1 downto 0) -- data out
|
DO : out slv(DWIDTH-1 downto 0) -- data out
|
);
|
);
|
end entity debounce_gen;
|
end entity debounce_gen;
|
|
|
|
|
architecture syn of debounce_gen is
|
architecture syn of debounce_gen is
|
|
|
constant cntzero : slv(CWIDTH-1 downto 0) := (others=>'0');
|
constant cntzero : slv(CWIDTH-1 downto 0) := (others=>'0');
|
constant datazero : slv(dWIDTH-1 downto 0) := (others=>'0');
|
constant datazero : slv(dWIDTH-1 downto 0) := (others=>'0');
|
|
|
type regs_type is record
|
type regs_type is record
|
cecnt : slv(CWIDTH-1 downto 0); -- clock interval counter
|
cecnt : slv(CWIDTH-1 downto 0); -- clock interval counter
|
dref : slv(DWIDTH-1 downto 0); -- data reference
|
dref : slv(DWIDTH-1 downto 0); -- data reference
|
dchange : slv(DWIDTH-1 downto 0); -- data change flag
|
dchange : slv(DWIDTH-1 downto 0); -- data change flag
|
dout : slv(DWIDTH-1 downto 0); -- data output
|
dout : slv(DWIDTH-1 downto 0); -- data output
|
end record regs_type;
|
end record regs_type;
|
|
|
constant regs_init : regs_type := (
|
constant regs_init : regs_type := (
|
cntzero,
|
cntzero,
|
datazero,
|
datazero,
|
datazero,
|
datazero,
|
datazero
|
datazero
|
);
|
);
|
|
|
signal R_REGS : regs_type := regs_init; -- state registers
|
signal R_REGS : regs_type := regs_init; -- state registers
|
signal N_REGS : regs_type := regs_init; -- next value state regs
|
signal N_REGS : regs_type := regs_init; -- next value state regs
|
|
|
begin
|
begin
|
|
|
assert CEDIV<=2**CWIDTH report "assert(CEDIV<=2**CWIDTH)" severity failure;
|
assert CEDIV<=2**CWIDTH report "assert(CEDIV<=2**CWIDTH)" severity failure;
|
|
|
proc_regs: process (CLK)
|
proc_regs: process (CLK)
|
begin
|
begin
|
|
|
if rising_edge(CLK) then
|
if rising_edge(CLK) then
|
if RESET = '1' then
|
if RESET = '1' then
|
R_REGS.cecnt <= cntzero;
|
R_REGS.cecnt <= cntzero;
|
R_REGS.dref <= DI;
|
R_REGS.dref <= DI;
|
R_REGS.dchange <= datazero;
|
R_REGS.dchange <= datazero;
|
R_REGS.dout <= DI;
|
R_REGS.dout <= DI;
|
else
|
else
|
R_REGS <= N_REGS;
|
R_REGS <= N_REGS;
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
end process proc_regs;
|
end process proc_regs;
|
|
|
proc_next: process (R_REGS, CE_INT, DI)
|
proc_next: process (R_REGS, CE_INT, DI)
|
|
|
variable r : regs_type := regs_init;
|
variable r : regs_type := regs_init;
|
variable n : regs_type := regs_init;
|
variable n : regs_type := regs_init;
|
|
|
begin
|
begin
|
|
|
r := R_REGS;
|
r := R_REGS;
|
n := R_REGS;
|
n := R_REGS;
|
|
|
for i in DI'range loop
|
for i in DI'range loop
|
if DI(i) /= r.dref(i) then
|
if DI(i) /= r.dref(i) then
|
n.dchange(i) := '1';
|
n.dchange(i) := '1';
|
end if;
|
end if;
|
end loop;
|
end loop;
|
|
|
if CE_INT = '1' then
|
if CE_INT = '1' then
|
if unsigned(r.cecnt) = 0 then
|
if unsigned(r.cecnt) = 0 then
|
n.cecnt := slv(to_unsigned(CEDIV-1,CWIDTH));
|
n.cecnt := slv(to_unsigned(CEDIV-1,CWIDTH));
|
n.dref := DI;
|
n.dref := DI;
|
n.dchange := datazero;
|
n.dchange := datazero;
|
for i in DI'range loop
|
for i in DI'range loop
|
if r.dchange(i) = '0' then
|
if r.dchange(i) = '0' then
|
n.dout(i) := r.dref(i);
|
n.dout(i) := r.dref(i);
|
end if;
|
end if;
|
end loop;
|
end loop;
|
|
|
else
|
else
|
n.cecnt := slv(unsigned(r.cecnt) - 1);
|
n.cecnt := slv(unsigned(r.cecnt) - 1);
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
N_REGS <= n;
|
N_REGS <= n;
|
|
|
DO <= r.dout;
|
DO <= r.dout;
|
|
|
end process proc_next;
|
end process proc_next;
|
|
|
|
|
end syn;
|
end syn;
|
|
|
|
|