📄 generic_counter.txt
字号:
---------------------------------------------------------------------------------
--Entity Declaration
--Generic_counter Component used to construct the counter
--Used to choose the count group, set jam , out put the count states
---------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity generic_counter is
generic(group_num:positive:=4);
port ( clk: in std_logic; --50Mhz:internal clock
newclk_en_g: in std_logic; --the newly get changable clock
bits_in: in std_logic_vector (7 downto 0);
--8-bit binary read in through 7 switches
--group_set_g: in std_logic; --select the right group
jam_enable_g: in std_logic; -- set the not-allowed states
--group_num_decimal: buffer integer range 1 to 20; --decimal person number
--to identify each group
output_buffer: buffer std_logic_vector (3 downto 0);
--store the highlight number
z_out_g : out std_logic; -- used to highlight the the state
--"1100" according to requirement in the project outline
q_g: out std_logic_vector (3 downto 0)); --out put the
--next counter state
end generic_counter;
architecture behaviour of generic_counter is
----------------------------------------
--Introduction of the signals used
----------------------------------------
type state_table20x16 is array(1 to 20,1 to 16)of std_logic_vector(3 downto 0);
--array used to store the states of every group
type state_table20x7 is array(1 to 20,1 to 7)of std_logic_vector (3 downto 0);
--array used to store the destinations of the transitions
-- from the not-allowed states
signal n:integer range 1 to 16;
--record the number of not-allowed states in every group
signal k:integer range 1 to 16;
--store the ID of the counter state of the selected group
signal s_id:integer range 1 to 16;
--store the ID of the counter state of the selected group
signal jam:std_logic_vector (3 downto 0);
-- jam is the entered not-allowed state
signal jam_change:std_logic;
--reflect the press of the button which was used to set the
-- not-allowed states
--signal group_change:std_logic;
--reflect the press of the button which was used to selecte
-- the group
--signal group_num:std_logic_vector(4 downto 0);
--store the number of selected group
signal state:std_logic_vector (3 downto 0);
--store the out put counter state
---------------------------------------------------
--Define the counter states as s0,s1,…,s15
---------------------------------------------------
constant s0:std_logic_vector(3 downto 0):="0000";
constant s1:std_logic_vector(3 downto 0):="0001";
constant s2:std_logic_vector(3 downto 0):="0010";
constant s3:std_logic_vector(3 downto 0):="0011";
constant s4:std_logic_vector(3 downto 0):="0100";
constant s5:std_logic_vector(3 downto 0):="0101";
constant s6:std_logic_vector(3 downto 0):="0110";
constant s7:std_logic_vector(3 downto 0):="0111";
constant s8:std_logic_vector(3 downto 0):="1000";
constant s9:std_logic_vector(3 downto 0):="1001";
constant s10:std_logic_vector(3 downto 0):="1010";
constant s11:std_logic_vector(3 downto 0):="1011";
constant s12:std_logic_vector(3 downto 0):="1100";
constant s13:std_logic_vector(3 downto 0):="1101";
constant s14:std_logic_vector(3 downto 0):="1110";
constant s15:std_logic_vector(3 downto 0):="1111";
------------------------------------------------------------------------------------------------------------
--Array used to store the states of every group
--The allowed states are arranged at the front of each group, not-allowed states are
-- arranged at the bank of each group
-------------------------------------------------------------------------------------------------------------
constant st:state_table20x16:=(
(s13,s12,s11,s9,s8,s7,s5,s4,s2,s0,s1,s3,s6,s10,s14,s15),--1
(s15,s14,s13,s12,s11,s10,s8,s7,s5,s3,s1,s0,s2,s4,s6,s9),--2
(s15,s14,s13,s12,s11,s10,s8,s7,s5,s4,s2,s1,s0,s3,s6,s9),--3
(s13,s12,s11,s9,s8,s7,s5,s4,s3,s0,s1,s2,s6,s10,s14,s15),--4
(s15,s14,s13,s12,s10,s9,s8,s6,s4,s2,s1,s0,s3,s5,s7,s11),--5
(s15,s14,s13,s12,s11,s9,s8,s6,s5,s3,s2,s1,s0,s4,s7,s10),--6
(s13,s12,s11,s9,s8,s7,s6,s5,s3,s0,s1,s2,s4,s10,s14,s15),--7
(s15,s14,s13,s11,s10,s9,s7,s5,s3,s2,s1,s0,s4,s6,s8,s12),--8
(s15,s14,s13,s12,s10,s9,s7,s6,s4,s3,s2,s1,s0,s5,s8,s11),--9
(s13,s12,s11,s10,s8,s7,s6,s5,s3,s0,s1,s2,s4,s9,s14,s15),--10
(s15,s14,s12,s11,s10,s8,s6,s4,s3,s2,s1,s0,s5,s7,s9,s13),--11
(s15,s14,s13,s11,s10,s8,s7,s5,s4,s3,s2,s1,s0,s6,s9,s12),--12
(s14,s12,s11,s10,s8,s7,s6,s5,s3,s0,s1,s2,s4,s9,s13,s15),--13
(s15,s13,s12,s10,s8,s6,s5,s4,s3,s2,s1,s0,s7,s9,s11,s14),--14
(s15,s14,s12,s11,s9,s8,s6,s5,s4,s3,s2,s1,s0,s7,s10,s13),--15
(s14,s13,s11,s10,s8,s7,s6,s5,s3,s0,s1,s2,s4,s9,s12,s15),--16
(s14,s13,s11,s9,s7,s6,s5,s4,s3,s2,s1,s0,s8,s10,s12,s15),--17
(s15,s13,s12,s10,s9,s7,s6,s5,s4,s3,s2,s1,s0,s8,s11,s14),--18
(s15,s14,s13,s11,s9,s7,s5,s3,s1,s0,s2,s4,s6,s8,s10,s12),--19
(s15,s14,s13,s12,s11,s9,s8,s7,s5,s3,s1,s0,s2,s4,s6,s10));--20
---------------------------------------------------------------------------------------------------
--Array used to store the destinations of the transitions from the not-allowed states
---------------------------------------------------------------------------------------------------
constant to_states:state_table20x7:=(
(s7,s5,s4,s12,s11,s11,s9), --1
(s15,s13,s3,s8,s7,s0,s0), --2
(s1,s2,s5,s7,s0,s0,s0), --3
(s13,s3,s5,s13,s12,s9,s8), --4
(s6,s15,s15,s15,s10,s0,s0), --5
(s8,s15,s15,s11,s0,s0,s0), --6
(s3,s5,s7,s13,s8,s7,s3), --7
(s10,s9,s7,s5,s7,s0,s0), --8
(s13,s15,s15,s10,s0,s0,s0), --9
(s13,s13,s13,s13,s13,s13,s13), --10
(s15,s15,s15,s15,s15,s0,s0), --11
(s15,s15,s15,s15,s0,s0,s0), --12
(s12,s10,s12,s1,s12,s5,s12), --13
(s13,s13,s13,s13,s13,s0,s0), --14
(s8,s8,s8,s8,s0,s0,s0), --15
(s7,s7,s7,s7,s7,s7,s7), --16
(s9,s9,s9,s9,s9,s0,s0), --17
(s4,s4,s4,s4,s0,s0,s0), --18
(s3,s3,s3,s3,s3,s3,s3), --19
(s1,s1,s1,s1,s1,s0,s0) ); --20
begin
---------------------------------------------------
--Read group number from the 8-bit switch
---------------------------------------------------
--process(clk)
--begin
--if (rising_edge(clk)) then
--group_change<=group_set_g;
--if group_change='1'then --button pressed
--group_num(4 downto 0)<=bits_in(4 downto 0);--get the group number
--end if;
--end if;
--end process;
----------------------------------------------------------------------
--Read not-allowed state (jam) entered from the 8 switches
----------------------------------------------------------------------
process(clk)
begin
if(rising_edge(clk))then
jam_change<=jam_enable_g;
if (jam_change='1')then
jam<=bits_in(3 downto 0); --set the not-allowed state
end if;
end if;
end process;
-----------------------------------------------------------------
--Select the required group according to "group_num"
-----------------------------------------------------------------
process(clk)
begin
--if((rising_edge(clk))and(group_change='1'))then
case group_num is
when 1=>
--group_num_decimal<=1; --the first group
n<=9; --there are 9 not-allowed states in this group
output_buffer<="1101"; --the highlight state is "1101"
when 2=>
--group_num_decimal<=2;
n<=11;
output_buffer<="1101";
when 3=>
--group_num_decimal<=3;
n<=12;
output_buffer<="1101";
when 4=>
--group_num_decimal<=4;
n<=9;
output_buffer<="1100";
when 5=>
--group_num_decimal<=5;
n<=11;
output_buffer<="1100";
when 6=>
--group_num_decimal<=6;
n<=12;
output_buffer<="1100";
when 7=>
--group_num_decimal<=7;
n<=9;
output_buffer<="1011";
when 8=>
--group_num_decimal<=8;
n<=11;
output_buffer<="1011";
when 9 =>
--group_num_decimal<=9;
n<=12;
output_buffer<="1010";
when 10=>
--group_num_decimal<=10;
n<=9;
output_buffer<="1010";
when 11=>
--group_num_decimal<=11;
n<=11;
output_buffer<="1010";
when 12=>
--group_num_decimal<=12;
n<=12;
output_buffer<="1010";
when 13=>
--group_num_decimal<=13;
n<=9;
output_buffer<="0111";
when 14=>
--group_num_decimal<=14;
n<=11;
output_buffer<="1000";
when 15=>
--group_num_decimal<=15;
n<=12;
output_buffer<="1000";
when 16=>
--group_num_decimal<=16;
n<=9;
output_buffer<="0110";
when 17=>
--group_num_decimal<=17;
n<=11;
output_buffer<="0110";
when 18=>
--group_num_decimal<=18;
n<=12;
output_buffer<="0110";
when 19=>
--group_num_decimal<=19;
n<=9;
output_buffer<="1101";
when 20=>
--group_num_decimal<=20;
n<=11;
output_buffer<="1100";
when others=>
--group_num_decimal<=5;
n<=11;
output_buffer<="1100";
end case;
--end if;
end process;
------------------------------------------------
--Get the ID of the state begin to count
------------------------------------------------
process(clk,newclk_en_g)
variable jam_id:integer range 1 to 16;
variable id:integer range 1 to 16;
begin
if(rising_edge(clk)and (jam_change='1'))then
gen1: for i in 1 to 16 loop
if (st(group_num,i)=jam)then --find the state set in array "st"
jam_id:=i; --jam_id :an integer to record
-- the id of the state vector
exit;
end if;
end loop gen1;
if jam_id>n then --it is a not-allocted state
state<=to_states(group_num,(jam_id-n)); --get the transition state
gen2: for i in 1 to 12 loop
id:=i; --find the id of the transition state
exit when(st(group_num,i)=state);
end loop gen2;
else --it is a allocated state
id:=jam_id ;
id:=id+1;
if(id=n+1)then --if the state is the last allocated
--state, it should return to beginning of the circle
id:=1;
end if;
end if;
k<=id; --get the ID of the state to begin the count
end if;
end process;
---------------------------------
-- Out put the counter state
---------------------------------
process(clk,newclk_en_g)
begin
if(jam_change='1')then --the button "jam_enable" was pressed
s_id<=k;
q_g<=jam; --out put the state
elsif((newclk_en_g'event and newclk_en_g='1')and(jam_change='0'))then
q_g<=st(group_num,s_id); --out put the next state
if(output_buffer=st(group_num,s_id))then z_out_g<='1';
--when meet the highlight state,
--set the "z_out_g" signal to '1'
else z_out_g<='0';
end if;
s_id<=s_id+1; --get the next out-put state
if(s_id=n)then --return to ID=1, count again
s_id<=1;
end if;
end if;
end process;
end behaviour;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -