]> rtime.felk.cvut.cz Git - fpga/quadcount.git/blob - qcounter.vhdl
Removed quadcount_tb as it do nothing
[fpga/quadcount.git] / qcounter.vhdl
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
5 use ieee.numeric_std.all;
6
7 entity qcounter is
8   port (
9     clock: in std_logic;
10     reset: in std_logic;
11     a0, b0: in std_logic;
12     qcount: out std_logic_vector (31 downto 0);
13     a_rise, a_fall, b_rise, b_fall, ab_event: out std_logic;
14     ab_error: out std_logic
15   );
16 end qcounter;
17
18 architecture behavioral of qcounter is
19   component dff
20     port (
21       clock: in std_logic;
22       d: in std_logic;
23       q: out std_logic
24     );
25   end component;
26
27 subtype std_logic4 is std_logic_vector (3 downto 0);
28   signal a, b, a_prev, b_prev: std_logic;
29   signal count_prev: std_logic_vector (29 downto 0)
30     := "000000000000000000000000000000";
31   signal count: std_logic_vector (29 downto 0);
32 begin
33   dff_a: dff
34     port map (
35       clock => clock,
36       d => a0,
37       q => a
38     );
39   dff_b: dff
40     port map (
41       clock => clock,
42       d => b0,
43       q => b
44     );
45   
46   qcount(0) <= a xor b;
47   qcount(1) <= b;
48   qcount(31 downto 2) <= count;
49   
50   comb_event: process (a_prev, b_prev, a, b)
51   begin
52     a_rise <= '0';
53     a_fall <= '0';
54     b_rise <= '0';
55     b_fall <= '0';
56     ab_event <= '0';
57     ab_error <= '0';
58     if ((a xor a_prev) and (b xor b_prev)) = '1' then
59       -- forbidden double transition
60       ab_error <= '1';
61     else
62       a_rise <= (a xor a_prev) and a;
63       a_fall <= (a xor a_prev) and not a;
64       b_rise <= (b xor b_prev) and b;
65       b_fall <= (b xor b_prev) and not b;
66       ab_event <= (a xor a_prev) or (b xor b_prev);
67     end if;
68   end process;
69
70   comb_count: process (a_prev, b_prev, a, b, count)
71   begin
72     if (a_prev = '0') and (b_prev = '1') and (a = '0') and (b = '0') then
73       count <= count_prev + 1;
74     elsif (a_prev = '0') and (b_prev = '0') and (a = '0') and (b = '1') then
75       count <= count_prev - 1;
76     else
77       count <= count_prev;
78     end if;
79   end process;
80
81   seq: process
82   begin
83     wait until clock'event and clock = '1';
84     if reset = '1' then
85       count_prev <= "000000000000000000000000000000";
86     else
87       count_prev <= count;
88     end if;
89     a_prev <= a;
90     b_prev <= b;
91   end process;
92
93 end behavioral;