top

Wednesday, 28 November 2012

TLC controller code



library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-------------------------------------------line 513 to 520 must be disabled if input needs to be given from server also respective pibn config needs to be assigned wrt to these inpts
--------------------------------------------all inpts must be driven continuously
--------------------------------------------    for vip change mode to 010 and make vip_mode_on_off high and give vipdirectioninp 0 for northsouth 1 for ew
entity clkdiv2 is
Port (clk : in STD_LOGIC;
clk30 : in STD_LOGIC;
vipdirectioninp: inout STD_LOGIC:='0';-------------0 for northsouth 1 for ew
vip_mode_on_off : inout STD_LOGIC:='0';        ---------------manual operation only----------------this should be countinuously driven , drive 0 to disable
mode :inout std_logic_vector(2 downto 0):="000";-----------"000" for manual , "001" for auto,010 for vip; this should be countinuously driven
DRIVE_BUS :out std_logic_vector(15 downto 0):="0000000000000000";
CARD_DRIVE1 :out std_logic_vector(3 downto 0);
 righttime :inout std_logic_vector(13 downto 0):="00000001100100";------------------------time in binary centisecinds only
 walktime:inout std_logic_vector(13 downto 0):="00000001100100";
 amberbeforegreentime:inout std_logic_vector(13 downto 0):="00000001100100";
 greentime:inout std_logic_vector(13 downto 0):="00000001100100";
 amberaftergreentime:inout std_logic_vector(13 downto 0):="00000001100100"
);
end entity clkdiv2;
architecture Behavioral of clkdiv2 is
signal righttime1: std_logic_vector(13 downto 0):="00000000000000";
signal walktime1: std_logic_vector(13 downto 0):="00000000000000";
signal amberbeforegreentime1: std_logic_vector(13 downto 0):="00000000000000";
signal greentime1: std_logic_vector(13 downto 0):="00000000000000";
signal amberaftergreentime1: std_logic_vector(13 downto 0):="00000000000000";
signal CARD_DRIVE : std_logic_vector(3 downto 0);
signal vipdirection : STD_LOGIC:='1';----0 for northsouth 1 for e w
signal vip :STD_LOGIC;
signal vipdata :std_logic_vector(15 downto 0);
signal vipdatans :std_logic_vector(15 downto 0):="0000000100000100";
signal vipdataew :std_logic_vector(15 downto 0):="0000010000000001";
signal sum1 :std_logic_vector(15 downto 0):="0000000000000001";
signal statetime0:std_logic_vector(13 downto 0):="00000000000001";
signal statetime1:std_logic_vector(13 downto 0):="00000000000001";
signal statetime2:std_logic_vector(13 downto 0):="00000000000001";
signal statetime3:std_logic_vector(13 downto 0):="00000000000001";
signal statetime4:std_logic_vector(13 downto 0):="00000000000001";
signal statetime5:std_logic_vector(13 downto 0):="00000000000001";
signal statetime6:std_logic_vector(13 downto 0):="00000000000001";
signal statetime7:std_logic_vector(13 downto 0):="00000000000001";
signal statetime8:std_logic_vector(13 downto 0):="00000000000001";
signal statetime9:std_logic_vector(13 downto 0):="00000000000001";
signal statetime10:std_logic_vector(13 downto 0):="00000000000001";
signal statetime11:std_logic_vector(13 downto 0):="00000000000001";
signal statetime15:std_logic_vector(13 downto 0):="00000000000001";
signal statetime14:std_logic_vector(13 downto 0):="00000000000001";
signal statetime13:std_logic_vector(13 downto 0):="00000000000001";
signal statetime12:std_logic_vector(13 downto 0):="00000000000001";
signal statetime17:std_logic_vector(13 downto 0):="00000000000001";
signal statetime16:std_logic_vector(13 downto 0):="00000000000001";
signal statetime19:std_logic_vector(13 downto 0):="00000000000001";
signal statetime18:std_logic_vector(13 downto 0):="00000000000001";
signal statetime21:std_logic_vector(13 downto 0):="00000000000001";
signal statetime20:std_logic_vector(13 downto 0):="00000000000001";
signal statetime23:std_logic_vector(13 downto 0):="00000000000001";
signal statetime22:std_logic_vector(13 downto 0):="00000000000001";

signal clkcount :std_logic_vector(13 downto 0):="00000000000000";
signal state :std_logic_vector(4 downto 0):="00000";
signal clk_sig : std_logic:='0';
signal sum :std_logic_vector(15 downto 0):="0000000000000000";
signal clk_out :  STD_LOGIC;
begin
process(clk,mode)
begin
if rising_edge(clk) then
  if mode="000" then
         -------------------------------------------------------
---------------------------------------------------------------------
       statetime1<=walktime1;
       statetime0<="00000000000001";
       statetime3<=amberbeforegreentime1;
       statetime2<="00000000000001";
       statetime5<=greentime1; 
     statetime4<="00000000000001";
     statetime7<=amberaftergreentime1;
       statetime6<="00000000000001";
     statetime9<=walktime1;
     statetime8<="00000000000001";
       statetime11<=amberbeforegreentime1;
   statetime10<="00000000000001";
     statetime13<=greentime1;
   statetime12<="00000000000001";
     statetime15<=amberaftergreentime1;
   statetime14<="00000000000001";
       statetime17<=righttime1;
   statetime16<="00000000000001";
       statetime19<=righttime1;
   statetime18<="00000000000001";
          statetime21<=righttime1;
   statetime20<="00000000000001";
       statetime23<=righttime1;
   statetime22<="00000000000001";
  elsif mode="001" then
       statetime1<="00000111110100";--------------------500cs  walktime
       statetime0<="00000000000001";
       statetime3<="00000100101100";--------------------300cs  amb before green
       statetime2<="00000000000001";
       statetime5<="10000100110100";  -----------------8500cs
     statetime4<="00000000000001";
     statetime7<="00000011001000";--------------------200cs  amb after green
       statetime6<="00000000000001";
     statetime9<="00000111110100";-------------------
     statetime8<="00000000000001";
       statetime11<="00000100101100";----------------
   statetime10<="00000000000001";
      statetime13<="10000100110100";---------------------8500cs
   statetime12<="00000000000001";
     statetime15<="00000011001000";----------------------200cs
   statetime14<="00000000000001";
      statetime17<="00011111010000";----------------------
   statetime16<="00000000000001";
       statetime19<="00011111010000";----------------
   statetime18<="00000000000001";
  statetime21<="00011111010000";---------------------
   statetime20<="00000000000001";
    statetime23<="00011111010000";-------------------
   statetime22<="00000000000001";
  
   elsif mode="010" then
   --CARD_DRIVE<="0011";
  
  end if;
end if;
end process;
process(clk)
variable cnt : integer:=0;
begin
if rising_edge(clk) then
if (cnt=249999) then---------------24999999
clk_sig<=NOT(clk_sig);
cnt:=0;
else
cnt:=cnt+1;
end if;
end if;
end process;


process(clk_out)
begin
if rising_edge(clk_out) then

------------------------------------------------------------------
if state ="00000" then
            IF (clkcount = statetime0)then
            state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="00001" then
              IF (clkcount = statetime1)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="00010" then
              IF (clkcount = statetime2)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
   
elsif state ="00011" then
              IF (clkcount = statetime3)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="00100" then
              IF (clkcount = statetime4)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="00101" then
              IF (clkcount = statetime5)then
             state <=state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="00110" then
              IF (clkcount = statetime6)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="00111" then
              IF (clkcount = statetime7)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="01000" then
              IF (clkcount = statetime8)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="01001" then
              IF (clkcount = statetime9)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="01010" then
              IF (clkcount = statetime10)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="01011" then
              IF (clkcount = statetime11)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="01100" then
              IF (clkcount = statetime12)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="01101" then
              IF (clkcount = statetime13)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="01110" then
              IF (clkcount = statetime14)then
            state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="01111" then
              IF (clkcount = statetime15)then
             state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="10000" then
              IF (clkcount = statetime16)then
            state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="10001" then
              IF (clkcount = statetime17)then
            state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="10010" then
              IF (clkcount = statetime18)then
            state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="10011" then
              IF (clkcount = statetime19)then
            state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="10100" then
              IF (clkcount = statetime20)then
            state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="10101" then
              IF (clkcount = statetime21)then
            state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="10110" then
              IF (clkcount = statetime22)then
            state <= state + "00001";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
elsif state ="10111" then
              IF (clkcount = statetime23)then
            state <= "00000";
    clkcount <="00000000000000";
    ELSE
      clkcount <= clkcount + "00000000000001"  ;
             END iF;
end if;
-------------------------------------------------------------------
end if;
end process;
process(clk_out,state,vip)
begin
if rising_edge(clk_out) then
----------------------------------------------------
if mode="010" then
CARD_DRIVE<="0011";
else
------------------------------------------------- 
   if state<="00000" then
   CARD_DRIVE<="0111";
        sum<="0100000101000001";-----s0            all red try to walk here
       -- sum<="0000000000000001";

   elsif state<="00001" then
     
      CARD_DRIVE<="1011";
       sum<="0100000101000001";-----s0              all red try to walk here
       --sum<="0000000000000001";

   elsif state<="00010" then
      CARD_DRIVE<="0111";
     --sum<="0000000000000010";
   sum<="0010001000100001";-----s1            ns amber   ew red    
 elsif state<="00011" then
       CARD_DRIVE<="1011";
      sum<="0010001000100001";-----s1            ns amber   ew red 
   --sum<="0000000000000010";
  elsif state<="00100" then
        CARD_DRIVE<="0111";
     sum<="0010010000100001";-----s2              ns green    ew red
  --sum<="0000000000000011";
elsif state<="00101" then
        CARD_DRIVE<="1011";
     sum<="0010010000100001";-----s2              ns green    ew red
 --sum<="0000000000000011";
elsif state<="00110" then
        CARD_DRIVE<="0111";
    sum<="0010001000100010";-----s3               ns amber   ew amber
      --sum<="0000000000000100";
elsif state<="00111" then
         CARD_DRIVE<="1011";
     sum<="0010001000100010";-----s3               ns amber   ew amber
    --  sum<="0000000000000100";
elsif state<="01000" then
         CARD_DRIVE<="0111";
     sum<="0100000101000001";-----s4            all red try to walk here
 --sum<="0000000000000101";
elsif state<="01001" then
         CARD_DRIVE<="1011";
  sum<="0100000101000001";-----s4               all red try to walk here
 --sum<="0000000000000101";
elsif state<="01010" then
         CARD_DRIVE<="0111";
     --sum<="0000000000000111";
  sum<="0010000100100010";-----s5            ns red ew amber
 elsif state<="01011" then
         CARD_DRIVE<="1011";
     --sum<="0000000000000111";
      sum<="0010000100100010";-----s5            ns red ew amber
 elsif state<="01100" then
         CARD_DRIVE<="0111";
     --sum<="0000000000000111";
      sum<="0010000100100100";-----s6           ns red ew green
 elsif state<="01101" then
         CARD_DRIVE<="1011";
     --sum<="0000000000000111";
      sum<="0010000100100100";-----s6            ns red ew green
 elsif state<="01110" then
         CARD_DRIVE<="0111";
     --sum<="0000000000000111";
      sum<="0010000100100010";-----s7          ns red ew amber
 elsif state<="01111" then
         CARD_DRIVE<="1011";
     --sum<="0000000000000111";
      sum<="0010000100100010";-----s7            ns red ew amber
 elsif state<="10000" then
         CARD_DRIVE<="0111";
     --sum<="0000000000000111";
      sum<="0010000000101000";-----s8          right signal for east to north---------------------------------------
 elsif state<="10001" then
         CARD_DRIVE<="1011";
     --sum<="0000000000000111";
      sum<="0011000000100000";-----s8          left signal for south to east---------------------------------------
 elsif state<="10010" then
         CARD_DRIVE<="0111";
     --sum<="0000000000000111";
      sum<="0010100000110000";-----s9          right signal for north to west--------  left signal for east to south--------------
  
 elsif state<="10011" then
         CARD_DRIVE<="1011";
     --sum<="0000000000000111";
      sum<="0010000000100000";-----s9       
 elsif state<="10100" then
         CARD_DRIVE<="0111";
     --sum<="0000000000000111";
      sum<="0011000000100000";-----s10          left signal for n to e-------------------
 elsif state<="10101" then
         CARD_DRIVE<="1011";
     --sum<="0000000000000111";
      sum<="0010000000101000";-----s10          right signal for w to s--------------
 elsif state<="10110" then
         CARD_DRIVE<="0111";
     --sum<="0000000000000111";
      sum<="0010000000100000";-----s11        
 elsif state<="10111" then
         CARD_DRIVE<="1011";
     --sum<="0000000000000111";
      sum<="0010100000110000";-----s12          right signal for s to e-------------- left signal for w to n-------------------
end if;
end if;
end if;
end process;


vipdata(0)<=(vipdatans(0)and vipdirection )or (vipdataew(0)and not(vipdirection));
vipdata(1)<=(vipdatans(1)and vipdirection )or (vipdataew(1)and not(vipdirection));
vipdata(2)<=(vipdatans(2)and vipdirection )or (vipdataew(2)and not(vipdirection));
vipdata(3)<=(vipdatans(3)and vipdirection )or (vipdataew(3)and not(vipdirection));
vipdata(4)<=(vipdatans(4)and vipdirection )or (vipdataew(4)and not(vipdirection));
vipdata(5)<=(vipdatans(5)and vipdirection )or (vipdataew(5)and not(vipdirection));
vipdata(6)<=(vipdatans(6)and vipdirection )or (vipdataew(6)and not(vipdirection));
vipdata(7)<=(vipdatans(7)and vipdirection )or (vipdataew(7)and not(vipdirection));
vipdata(8)<=(vipdatans(8)and vipdirection )or (vipdataew(8)and not(vipdirection));
vipdata(9)<=(vipdatans(9)and vipdirection )or (vipdataew(9)and not(vipdirection));
vipdata(10)<=(vipdatans(10)and vipdirection )or (vipdataew(10)and not(vipdirection));
vipdata(11)<=(vipdatans(11)and vipdirection )or (vipdataew(11)and not(vipdirection));
vipdata(12)<=(vipdatans(12)and vipdirection )or (vipdataew(12)and not(vipdirection));
vipdata(13)<=(vipdatans(13)and vipdirection )or (vipdataew(13)and not(vipdirection));
vipdata(14)<=(vipdatans(14)and vipdirection )or (vipdataew(14)and not(vipdirection));
vipdata(15)<=(vipdatans(15)and vipdirection )or (vipdataew(15)and not(vipdirection));
-------------------------------------------------------------------------------------
sum1(0)<=(sum(0) and (not vip)) or (vip and vipdata(0));
sum1(1)<=(sum(1) and (not vip)) or (vip and vipdata(1));
sum1(2)<=(sum(2) and (not vip)) or (vip and vipdata(2));
sum1(3)<=(sum(3) and (not vip)) or (vip and vipdata(3));
sum1(4)<=(sum(4) and (not vip)) or (vip and vipdata(4));
sum1(5)<=(sum(5) and (not vip)) or (vip and vipdata(5));
sum1(6)<=(sum(6) and (not vip)) or (vip and vipdata(6));
sum1(7)<=(sum(7) and (not vip)) or (vip and vipdata(7));
sum1(8)<=(sum(8) and (not vip)) or (vip and vipdata(8));
sum1(9)<=(sum(9) and (not vip)) or (vip and vipdata(9));
sum1(10)<=(sum(10) and (not vip)) or (vip and vipdata(10));
sum1(11)<=(sum(11) and (not vip)) or (vip and vipdata(11));
sum1(12)<=(sum(12) and (not vip)) or (vip and vipdata(12));
sum1(13)<=(sum(13) and (not vip)) or (vip and vipdata(13));
sum1(14)<=(sum(14) and (not vip)) or (vip and vipdata(14));
sum1(15)<=(sum(15) and (not vip)) or (vip and vipdata(15));
DRIVE_BUS<=sum1;
-----------------------------------------------------------------------------------------
-----------------DISABLE THIS WHILE IMp LEMENTING
vip_mode_on_off<='1';
vip<=vip_mode_on_off;
mode<="000";
vipdirectioninp<='1';
vipdirection<=vipdirectioninp;
-----------------
----------------------------------------------------------------------------------------
CARD_DRIVE1<=CARD_DRIVE;
clk_out<=clk_sig;
walktime<="00000110010000";
amberbeforegreentime<="00000110010000";
amberaftergreentime<="00000110010000";
greentime<="00000110010000";
righttime<="00000110010000";
----------------------------------------------------------------
walktime1<=walktime;
righttime1<=righttime;
amberbeforegreentime1<=amberbeforegreentime;
amberaftergreentime1<=amberaftergreentime;
greentime1<=greentime;

end Behavioral

No comments:

Post a Comment