Advertisement
Guest User

Untitled

a guest
Jan 10th, 2021
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.76 KB | None | 0 0
  1. library ieee;
  2. use IEEE.std_logic_1164.all;
  3.  
  4. entity FSM is
  5. port (CLK : in std_logic; --Clock, active high
  6.  RSTn : in std_logic; --Async. Reset, active low
  7.  CoinIn : in std_logic_vector (1 downto 0); --Which coin was inserted
  8.  Soda : out std_logic; --Is Soda dispensed ?
  9.  CoinOut : out std_logic_vector (1 downto 0) --Which coin is dispensed?
  10.  );
  11.  
  12. end entity;
  13.  
  14. architecture behavior of FSM is
  15. -- add your code here
  16. type state_type is (idle, --start state/reset
  17.  put_money, --waiting to enter money
  18.  in_1,in_3,in_6,in_5, --represent the current sum of money after returning change
  19.  change_1, --should return change of 1$
  20.  soda_out --dispence soda can.
  21.  ); --type of state machine.
  22. signal current_s,next_s: state_type; --current and next state declaration.
  23.  
  24. begin
  25.  
  26. process(CLK,RSTn)
  27. begin
  28.  if(RSTn = '0') then
  29.  current_s <= idle; --defualt state is on RESET
  30.  elsif(clk'event and clk = '1') then
  31.  current_s <= next_s;
  32.  end if;
  33. end process;
  34. --------------------
  35. --FSM process:
  36. process(current_s,CoinIn)
  37. begin
  38. case current_s is
  39.  when idle => --state reset or idle
  40.  Soda <= '0';
  41.  CoinOut <= "00";
  42.  next_s <= put_money;
  43.  ------------------------------------------------------
  44.  when put_money => --wait for money to be entered
  45.  if(CoinIn = "00")then
  46.  Soda <= '0';
  47.  CoinOut <= "00";
  48.  next_s <= put_money;
  49.  elsif(CoinIn = "01")then --insert 1$
  50.  Soda <= '0';
  51.  CoinOut <= "00";
  52.  next_s <= in_1;
  53.  elsif(CoinIn = "10")then --insert 2$
  54.  Soda <= '0';
  55.  CoinOut <= "00";
  56.  next_s <= soda_out;
  57.  elsif(CoinIn = "11")then --insert 5$
  58.  Soda <= '0';
  59.  CoinOut <= "00";
  60.  next_s <= in_5;
  61.  end if;
  62.  ------------------------------------------------------
  63.  when in_1 =>
  64.  if(CoinIn = "00") then--stay on the same state
  65.  Soda <= '0';
  66.  CoinOut <= "00";
  67.  next_s <= in_1;
  68.  elsif(CoinIn = "01") then--inserted another 1$
  69.  Soda <= '0';
  70.  CoinOut <= "00";
  71.  next_s <= soda_out;
  72.  elsif(CoinIn = "10") then--inserted another 2$
  73.  Soda <= '0';
  74.  CoinOut <= "00";
  75.  next_s <= in_3;
  76.  elsif(CoinIn = "11") then
  77.  Soda <= '0';
  78.  CoinOut <= "10";
  79.  next_s <= in_6;
  80.  end if;
  81.  ------------------------------------------------------
  82.  when in_3 =>
  83.  Soda <= '0';
  84.  CoinOut <= "01";
  85.  next_s <= soda_out;
  86.  ------------------------------------------------------
  87.  when in_6 =>
  88.  Soda <= '0';
  89.  CoinOut <= "01";
  90.  next_s <= in_5;
  91.  ------------------------------------------------------
  92.  when in_5 => -- input = 5 coin
  93.  Soda <= '0';
  94.  CoinOut <= "10";
  95.  next_s <= change_1;
  96.  ------------------------------------------------------
  97.  when change_1 => -- input = 5 coin
  98.  Soda <= '0';
  99.  CoinOut <= "01";
  100.  next_s <= soda_out;
  101.  ------------------------------------------------------
  102.  when soda_out =>
  103.  Soda <= '1';
  104.  CoinOut <= "00";
  105.  next_s <= put_money;
  106. end case;
  107. end process;
  108.  
  109. end behavior;
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement