?? sh_machine.vhd
字號:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity sh_machine is
port ( clk: in std_logic; --系統(tǒng)時鐘
set,sel,cancel,finish: in std_logic; --設定、買、選擇、完成信號
add,coin0: in bit; --增加貨物的數(shù)量及1元硬幣
get,choice_a,choice_b: in bit; --貨物類型的選擇跳轉按鈕及選A或選B按鈕
goods1,goods2: buffer std_logic_vector(3 downto 0); --貨物號的顯示
display: buffer std_logic_vector(15 downto 0); --十六種貨物的顯示燈
money0,money1 : buffer std_logic_vector(7 downto 0)); --放入的錢數(shù)、返回的錢數(shù)的顯示輸出端
end sh_machine;
architecture xiao of sh_machine is
type ram_type is array(15 downto 0)of std_logic_vector(7 downto 0);
signal ram :ram_type; --定義RAM
signal item: std_logic_vector(3 downto 0); --商品種類
signal coin: std_logic_vector(7 downto 0); --幣數(shù)計數(shù)器
signal pri,qua:std_logic_vector(3 downto 0);
begin
--數(shù)量和價格的初始化
init:process(set,clk,item)
begin
if clk 'event and clk='1' then
if set='1' then --設定商品的價格和數(shù)量
if add='1' then
qua <=qua+1;
else
if item<"1000" then
pri<="0010";
ram(conv_integer(item))<=pri & qua;
item<=item+1 ;
qua <="0000";
else
pri<="0101";
ram(conv_integer(item))<=pri & qua;
item<=item+1;
qua <="0000" ;
end if;
end if;
elsif (finish='1' and sel='0')then --在購買時相應的商品數(shù)量減少
if ( (display(conv_integer(goods1))='1' or display(conv_integer(goods2+8))='1')) then
if get='0' then
ram(conv_integer(goods1))(3 downto 0)<=ram(conv_integer(goods1))(3 downto 0)-1;
elsif get='1' then
ram(conv_integer(goods2+8))(3 downto 0)<=ram(conv_integer(goods2+8))(3 downto 0)-1;
end if;
end if;
end if;
end if;
end process;
--錢數(shù)及貨物的計算
caculate:process
begin
wait until rising_edge(clk);
if set='0' then
if (coin0='1' and coin<="01100100")then --投入的錢數(shù)。
coin<=coin+1;
if coin>"00010100" then
money0<="00010100"; --輸出投入的錢數(shù)
money1<=(coin - money0);
else
money0<=coin;
money1<=(others=>'0');
end if;
elsif sel='0' then
if cancel='1' then --取消交易
money1<=money0;
money0<=(others=>'0');
elsif finish='1' then ----選擇好進行購買時
if (get='0' and ram(conv_integer(goods1))(3 downto 0)>"0000" )then
money0<=money0-2;
if(money0<"00000010") then --當剩余的錢數(shù)小于最小的貨品單價時退出剩余的錢
money1<=money0;
money0<=(others=>'0');
end if;
elsif (get='1' and ram(conv_integer(goods2+8))(3 downto 0)>"0000") then
money0<=money0-5;
if(money0<"00000110") then --當剩余的錢數(shù)小于最小的貨品單價時退出剩余的錢
money1<=money0;
money0<=(others=>'0');
end if;
end if ;
end if;
end if;
end if;
end process;
--檢查指示燈是否滿足點亮的條件
check:process
begin
wait until rising_edge(clk) ;
if set='0' then
if money0<"00000010" then
display<=(others=>'0');
elsif (money0>="00000010" and money0<"00000110") then
for i in 0 to 7 loop
display(i+8)<='0' ;
if (ram(i)(3 downto 0)>"0000") then
display(i)<='1' ;
else
display(i)<='0';
end if;
end loop;
elsif money0>="00000101" then
for i in 0 to 15 loop
if (ram(i)(3 downto 0)>"0000") then
display(i)<='1';
else
display(i)<='0' ;
end if;
end loop;
end if;
end if;
end process;
--對商品的選擇
choose:process
begin
wait until rising_edge(clk) ;
if coin0='0' then
if (sel='1' ) then
if choice_a='1' then --選擇a類商品
goods1<=goods1+1;
if (display(conv_integer(goods1+1))='0') then
if goods1<="0111" then
goods1<=goods1+1;
else
goods1<="0000";
end if;
end if;
elsif choice_b='1' then --選擇b類商品
goods2<=goods2+1;
if(display(conv_integer(goods2+8))='0') then
if goods2<="0111" then
goods2<=goods2+1;
else
goods2<="0000";
end if;
end if;
end if;
end if;
end if;
end process ;
end xiao;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -