Advertisement
Guest User

Untitled

a guest
May 23rd, 2025
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 9.42 KB | Source Code | 0 0
  1.      
  2.            
  3.            
  4.             library IEEE;
  5.             use IEEE.STD_LOGIC_1164.ALL;
  6.             use IEEE.STD_LOGIC_UNSIGNED.ALL;
  7.            
  8.            
  9.             entity CHANGE_ALG is
  10.                 Port ( CLK : in STD_LOGIC;
  11.                        
  12.             --           CASH: in integer;
  13.             --           COST: in integer;
  14.             --           CASH_VECTOR : in STD_LOGIC_VECTOR (23 downto 0);
  15.             --           MONEY_VECTOR : in STD_LOGIC_VECTOR (47 downto 0);
  16.             --           REST_VECTOR: out STD_LOGIC_VECTOR (23 downto 0);
  17.                        NR : out STD_LOGIC;
  18.                        
  19.                        LAN: out std_logic_vector(3 downto 0);
  20.                        LSEG: out std_logic_vector(6 downto 0));
  21.             end CHANGE_ALG;
  22.            
  23.             architecture BEHAVIORAL of CHANGE_ALG is
  24.            
  25.             signal CASH: integer := 100;
  26.             signal COST: integer := 60;
  27.             signal CASH_VECTOR: std_logic_vector (23 downto 0) := "000000000000000001011000";
  28.             signal MONEY_VECTOR: std_logic_vector (47 downto 0) := "111111111111111111111111111111111111111111111111";
  29.             signal REST_VECTOR: std_logic_vector (23 downto 0);
  30.            
  31.             signal COSTX: integer := 0;
  32.             signal DONE: std_logic := '0'; -- this will signal when the algorithm is done
  33.             signal CASH_BACK: std_logic := '0';
  34.            
  35.             -- store the amount of each type of bull in another auxiliary signal
  36.             signal bill_1: std_logic_vector (7 downto 0) := MONEY_VECTOR(7 downto 0);
  37.             signal bill_2: std_logic_vector (7 downto 0) := MONEY_VECTOR(15 downto 8);
  38.             signal bill_5: std_logic_vector (7 downto 0) := MONEY_VECTOR(23 downto 16);
  39.             signal bill_10: std_logic_vector (7 downto 0) := MONEY_VECTOR(31 downto 24);
  40.             signal bill_20: std_logic_vector (7 downto 0) := MONEY_VECTOR(39 downto 32);
  41.             signal bill_50: std_logic_vector (7 downto 0) := MONEY_VECTOR(47 downto 40);
  42.            
  43.             signal REST: integer := 0;
  44.             type integer_vector is array (0 to 5) of integer;
  45.             constant BILL_VALUES : integer_vector := (1, 2, 5, 10, 20, 50);
  46.             -- store the amount of each type of bill that is given as rest
  47.             signal rest_1: std_logic_vector (3 downto 0) := "0000";
  48.             signal rest_2: std_logic_vector (3 downto 0) := "0000";
  49.             signal rest_5: std_logic_vector (3 downto 0) := "0000";
  50.             signal rest_10: std_logic_vector (3 downto 0) := "0000";
  51.             signal rest_20: std_logic_vector (3 downto 0):= "0000";
  52.             signal rest_50: std_logic_vector (3 downto 0) := "0000";
  53.            
  54.             signal HUN, TEN, UNIT: integer;
  55.             component INT_TO_CAT_CONVERTER is
  56.                 Port ( INT : in integer;
  57.                        CAT : out STD_LOGIC_VECTOR (6 downto 0));
  58.             end component;
  59.             signal HC, TC, UC: std_logic_vector(6 downto 0);
  60.            
  61.             component THREE_DIGIT_SSD is
  62.                 Port ( CLK : in STD_LOGIC;
  63.                
  64.                        DIGI1: in std_logic_vector (6 downto 0);
  65.                        DIGI2: in std_logic_vector (6 downto 0);
  66.                        DIGI3: in std_logic_vector (6 downto 0);
  67.                        
  68.                        AN: out std_logic_vector (3 downto 0);
  69.                        SEG: out std_logic_vector (6 downto 0)
  70.                        );
  71.             end component;
  72.            
  73.             signal STATE, NEXT_STATE: std_logic_vector (2 downto 0);
  74.             begin
  75.            
  76.             process (CLK)
  77.             begin
  78.                 if rising_edge (CLK) then
  79.                     STATE <= NEXT_STATE;
  80.                 end if;
  81.             end process;
  82.            
  83.             process (STATE)
  84.             begin
  85.             COSTX <= COST;
  86.                 if DONE = '0' then
  87.                          case STATE is
  88.                             when "000" => -- init
  89.                                 COSTX <= COST;
  90.                                 NEXT_STATE <= "001";
  91.                             when "001" =>
  92.                                 if (COSTX + 50 <= CASH) and (bill_50 > 0) then
  93.                                     COSTX <= COSTX + 50;
  94.                                     bill_50 <= bill_50 - 1;
  95.                                     rest_50 <= rest_50 + 1;
  96.                                     NEXT_STATE <= "001";
  97.                                 else
  98.                                     NEXT_STATE <= "010";
  99.                                 end if;
  100.                             when "010" =>
  101.                                 if (COSTX + 20 <= CASH) and (bill_20 > 0) then
  102.                                     COSTX <= COSTX + 20;
  103.                                     bill_20 <= bill_20 - 1;
  104.                                     rest_20 <= rest_20 + 1;
  105.                                     NEXT_STATE <= "010";
  106.                                 else
  107.                                     NEXT_STATE <= "011";
  108.                                 end if;
  109.                             when "011" =>
  110.                                 if (COSTX + 10 <= CASH) and (bill_10 > 0) then
  111.                                     COSTX <= COSTX + 10;
  112.                                     bill_10 <= bill_10 - 1;
  113.                                     rest_10 <= rest_10 + 1;
  114.                                     NEXT_STATE <= "011";
  115.                                 else
  116.                                     NEXT_STATE <= "100";
  117.                                 end if;
  118.                             when "100" =>
  119.                                 if (COSTX + 5 <= CASH) and (bill_5 > 0) then
  120.                                     COSTX <= COSTX + 5;
  121.                                     bill_5 <= bill_5 - 1;
  122.                                     rest_5 <= rest_5 + 1;
  123.                                     NEXT_STATE <= "100";
  124.                                 else
  125.                                     NEXT_STATE <= "101";
  126.                                 end if;
  127.                             when "101" =>
  128.                                 if (COSTX + 2 <= CASH) and (bill_2 > 0) then
  129.                                     COSTX <= COSTX + 2;
  130.                                     bill_2 <= bill_2 - 1;
  131.                                     rest_2 <= rest_2 + 1;
  132.                                     NEXT_STATE <= "101";
  133.                                 else
  134.                                     NEXT_STATE <= "110";
  135.                                 end if;
  136.                             when "110" =>
  137.                                 if (COSTX + 1 <= CASH) and (bill_1 > 0) then
  138.                                     COSTX <= COSTX + 1;
  139.                                     bill_1 <= bill_1 - 1;
  140.                                     rest_1 <= rest_1 + 1;
  141.                                     NEXT_STATE <= "110";
  142.                                 else
  143.                                     NEXT_STATE <= "111";
  144.                                 end if;
  145.                             when "111" =>
  146.                                 DONE <= '1';
  147.                                 if COSTX /= CASH then
  148.                                     CASH_BACK <= '1';
  149.                                     NR <= '1';
  150.                                 end if;
  151.                                 NEXT_STATE <= "111";
  152.                         end case;
  153.                 end if;
  154.             end process;
  155.        
  156.                    
  157.                     -- compose the rest vector by concatenating the rest subvectors
  158.                     REST_VECTOR <= rest_50 & rest_20 & rest_10 & rest_5 & rest_2 & rest_1;
  159.                    
  160.                     process(CLK)
  161.                     begin
  162.                         if rising_edge (CLK) then
  163.                             if DONE = '1' then
  164.                                 if CASH_BACK = '1' then
  165.                                     REST <= COSTX; -- give back the money (here should be CASH)
  166.                                 else
  167.                                     REST <= conv_integer(rest_1) * BILL_VALUES(0) +
  168.                                         conv_integer(rest_2) * BILL_VALUES(1) +
  169.                                         conv_integer(rest_5) * BILL_VALUES(2) +
  170.                                         conv_integer(rest_10) * BILL_VALUES(3) +
  171.                                         conv_integer(rest_20) * BILL_VALUES(4) +
  172.                                         conv_integer(rest_50) * BILL_VALUES(5);
  173.                                 end if;
  174.                        
  175.                                 -- break the number in parts
  176.                                 HUN <= REST/100;
  177.                                 TEN <= REST/10 mod 10;
  178.                                 UNIT <= REST mod 10;
  179.                             end if;
  180.                         end if;  
  181.                     end process;
  182.                    
  183.                     -- convert the digits of the number in their SSD equivalent
  184.                     ITCC1: INT_TO_CAT_CONVERTER port map (INT => HUN, CAT => HC);
  185.                     ITCC2: INT_TO_CAT_CONVERTER port map (INT => TEN, CAT => TC);
  186.                     ITCC3: INT_TO_CAT_CONVERTER port map (INT => UNIT, CAT => UC);
  187.                
  188.                     -- display the number
  189.                     TDSSD: THREE_DIGIT_SSD port map (CLK => CLK, DIGI1 => UC, DIGI2 => TC, DIGI3 => HC, AN => LAN, SEG => LSEG);
  190.             end BEHAVIORAL;
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement