Documente Academic
Documente Profesional
Documente Cultură
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity counter is
port( clk,wr,cs,rd,cw: in std_logic;
crm,crl,sr:in std_logic_vector( 7 downto 0);
olm,oll: out std_logic_vector(7 downto 0);
ot: inout std_logic;
gate: in std_logic);
end counter;
architecture Behavioral of counter is
signal s: std_logic;
signal c: std_logic:='1';
signal temp: std_logic_vector(15 downto 0);
signal temp2: std_logic_vector(15 downto 0);
signal lm: std_logic_vector(1 downto 0);
signal mo: std_logic_vector(2 downto 0);
begin
process(clk,wr,cs,gate,cw,rd,sr,crl,crm,c,temp)
variable temp1: std_logic_vector(15 downto 0);
begin
if clk='1' and clk'event then
if cs='0' then
if cw='1' then
mo(2)<= sr(3);
--CHIP SELECT
--FOR CONTROL WORD
--FOR SELECTING THE MODES
mo(1)<= sr(2);
mo(0)<= sr(1);
lm(1)<=sr(5);
lm(0)<=sr(4);
--else
--------------------------------MODE 0-----------------------------------------case mo is
when"000"=>
--MODE SELECTOR
if rd='1' then
case wr is
when '0'=>
case lm is --LOADING OF VALUE
when "01" =>
temp1(7 downto 0):=crl(7 downto 0);
temp1(15 downto 8):="00000000";
ot<= '0';
when "10"=>
temp1(7 downto 0):="00000000";
temp1(15 downto 8):=crm(7 downto 0);
ot<= '0';
when "11" =>
temp1(7 downto 0):=crl(7 downto 0);
temp1(15 downto 8):=crm(7 downto 0);
ot<= '0';
when others =>
null;
end case;
when '1' =>--write 1
if sr(0)='0' then
--BINARY COUNTING
case gate is
when '1'=>
--GATE=1
--end if;
when '0'=>
--GATE=0
if c='1' then
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
else
null;
end if;
when others =>null;
end case;
else
null;
end if;
--BCD COUNTING
=>
when "001"=>
if rd ='1' then
if wr ='0' then
if gate='1' then--and gate'event then
case lm is --LOADING VALUE
when "01" => crl0(7 downto 0) := data;
temp1(7 downto 0):= crl(7 downto 0);
temp1(15 downto 8):="00000000";
when "10"=>crm0(7 downto 0) := data;
temp1(7 downto 0):="00000000";
temp1(15 downto 8):=crm(7 downto 0);
when "11" =>
temp1(7 downto 0):=crl(7 downto 0);
temp1(15 downto 8):=crm(7 downto 0);
when others=>
null;
end case;
if s='1' then
ot<='0';
else
ot<='1' ;
s<='0';
end if;
end if;
if gate='0' then
if sr(0)='0' then --BINARY COUNTING
if clk='1' and clk'event then
temp1:= temp1- '1';
if c='1' then
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
else
null;
end if;
if s='0' then
ot<= '0';
end if;
if temp1="0000000000000000" then
if c='1' then
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
else
null;
end if;
ot<= '1';
s<= '1';
end if;
--end if;
else
null;
-- BCD COUNTING
end if;
end if;
else
null;
end if;
else
case lm is
when "01" =>
--SIMPLE READ
=>
=>
null;
end if;
temp1:= temp1 - '1';
if gate='0' then
if c='1' then
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
else
null;
end if;
end if;
if temp1="0000000000000000" then
ot<= '0';
case lm is
when "01"
=>
=>
=>
=>
=>
end case;
when '1' =>
if sr(0)='0' then -- BINARY COUNTING
case temp(0) is
when '0' =>
--EVEN COUNT
=>
=>
end if;
--end if;
when '1' =>
--ODD COUNT
temp<=temp-"0000000000000001";
--if clk='1' and clk'event then
if c='1' then
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
else
null;
end if;
temp<= temp - "0000000000000010";
if temp="0000000000000000" then
s<='1';
if c='1' then
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
else
null;
end if;
ot <= not ot;-- after 10 ns ;
case lm is
when "01" =>
temp2(7 downto 0)<=crl(7 downto 0);
temp2(15 downto 8)<="00000000";
temp2<=temp2-"0000000000000001";
when "10" =>
temp2(7 downto 0)<="00000000";
temp2(15 downto 8)<=crm(7 downto 0);
temp2<=temp2-"0000000000000001";
when "11" =>
temp2(7 downto 0)<=crl(7 downto 0);
temp2(15 downto 8)<=crm(7 downto 0);
temp2<=temp2-"0000000000000001";
when others=>
null;
end case;
--if clk='1' and clk'event then
if c='1' then
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
else
null;
end if;
temp2<= temp2 - "0000000000000010";
if temp2="0000000000000010" then
if c='1' then
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
else
null;
end if;
ot <= not ot;-- after 50 ns ;
end if;
--end if;
end if;
--end if;
when others=>
null;
end case;
end if;
when others=>
null;
end case;
else
if ot<='0' then
ot<='1';
oll(7 downto 0)<=temp(7 downto 0);
=>
else
if wr='1' then
if c='1' then
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
else
null;
end if;
ot<='1';
case lm is
when "01" =>
temp1(7 downto 0):=crl(7 downto 0);
temp1(15 downto 8):="00000000";
when "10" =>
temp1(7 downto 0):="00000000";
temp1(15 downto 8):=crm(7 downto 0);
when "11" =>
temp1(7 downto 0):=crl(7 downto 0);
temp1(15 downto 8):=crm(7 downto 0);
when others =>
null;
end case;
end if;
end if;
else
case lm is
when "01" =>
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<="00000000";
when "10"
=>
=>
-- BCD COUNTING
end if;
end if;
else
null;
end if;
else
case lm is
when "01" =>
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<="00000000";
when "10" =>
oll(7 downto 0)<="00000000" ;
olm(7 downto 0)<=temp1(15 downto 8);
when "11" =>
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
when "00" =>
oll(7 downto 0)<=temp1(7 downto 0) ;
olm(7 downto 0)<=temp1(15 downto 8);
when others=>
null;
end case;
end if;
---------------------------------------------------------------------------------------------------------------when others =>
null;
end case;
end if;
else
null;
end if;
end if;
end process;
end Behavioral;
);
end component;
component counter
port(gate:in std_logic;clk,wr,cs,rd,cw:in std_logic;
crm,crl,sr:in std_logic_vector( 7 downto 0);
olm,oll:out std_logic_vector(7 downto 0);
ot:inout std_logic);
end component;
signal ztemp:std_logic_vector(3 downto 0);
signal writemp:std_logic_vector(2 downto 0);
signal redtemp:std_logic_vector(2 downto 0);
signal crmtemp0,crmtemp1,crmtemp2:std_logic_vector (7 downto 0);
signal crltemp0,crltemp1,crltemp2:std_logic_vector (7 downto 0);
signal srtemp0,srtemp1,srtemp2:std_logic_vector (7 downto 0);
signal timer_cs, lsb:std_logic:='0';
signal ctrl_word : std_logic_vector(7 downto 0);
signal sc, rw : std_logic_vector(1 downto 0);
begin
r0:rwlo port map (clk=>clock,rd=>rdbar,wr=>wrbar,cs=>csbar,a=>aa,z=>ztemp,
wri=>writemp,red=>redtemp);
c0:counter port map (gate=>gate(0),clk=>clock,wr=>writemp(0),cs=>ztemp(0),
rd=>redtemp(0),cw=>ztemp(3),crm=>crmtemp0,crl=>crltemp0,
sr=>ctrl_word,ot=>outt(0));
c1:counter port map (gate=>gate(1),clk=>clock,wr=>writemp(1),
cs=>ztemp(1),crm=>crmtemp1,crl=>crltemp1,sr=>ctrl_word,
rd=>redtemp(1),cw=>ztemp(3),ot=>outt(1));
c2:counter port map (gate=>gate(2),clk=>clock,wr=>writemp(2),cs=>
ztemp(2),crm=>crmtemp2,crl=>crltemp2,sr=>ctrl_word,
rd=>redtemp(2),cw=>ztemp(3),ot=>outt(2));
ctrl_word <= dd when aa="11" and rising_edge(clock);
sc <= ctrl_word(7 downto 6);
rw <= ctrl_word(5 downto 4);
process(wrbar, csbar, clock)
begin
if wrbar='0' and csbar='0' and rising_edge(clock) then
if aa="11" then
lsb <= '1';
else
lsb <= '0';
end if;
end if;
end process;
process(clock)
begin
if rising_edge(clock) then
if csbar='0' and wrbar='0' then
case aa is
when "00" => --counter0
case rw is
when "01" =>
crltemp0 <= dd;
when "10" =>
crmtemp0 <= dd;
when "11" =>
if lsb='1' then
crltemp0 <= dd;
else
crmtemp0 <= dd;
end if;
when others=>null;
end case;
end process;
end Behavioral;