目錄
1.設計一個選擇器(IF語句)
代碼:
--設計一個選擇器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_1 is
port (A,B,C: in boolean;
Y: out boolean);
end entity CSDN_1;
architecture bhv of CSDN_1 is
begin
process(A,B,C) is
variable N: boolean;
begin
if A then N:=B;
else N:=C;
end if;
Y<=N;
end process;
end architecture bhv;
邏輯圖:
2.設計8-3線優先編碼器(IF語句)
代碼:
--8線-3線優先編碼器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_2 is
port(SR: in std_logic_vector(0 to 7);
SC: out std_logic_vector(0 to 2));
end entity CSDN_2;
architecture bhv of CSDN_2 is
begin
process(SR) is
begin
if(SR(7)='0') then SC<="000"; --優先級最高
elsif(SR(6)='0') then SC<="100";
elsif(SR(5)='0') then SC<="010";
elsif(SR(4)='0') then SC<="110";
elsif(SR(3)='0') then SC<="001";
elsif(SR(2)='0') then SC<="101";
elsif(SR(1)='0') then SC<="011";
else SC<="111";
end if;
end process;
end architecture bhv;
驗證仿真波形圖:
即有波形仿真可以看出,SR(7)的優先級最大
3.設計4選1多路信號選擇器(case語句)
代碼:
--設計4選1多路信號選擇器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_3 is
port(S1,S2: in std_logic;--選擇控制信號
A,B,C,D: in std_logic;--輸入信號
Z: out std_logic);--選擇器的輸出信號
end entity CSDN_3;
architecture bhv of CSDN_3 is
signal S: std_logic_vector(1 downto 0);
begin
S<=S1&S2;--合併,方便選擇控制
process(S,A,B,C,D) is
begin
case S is
when "00"=>Z<=A;
when "01"=>Z<=B;
when "10"=>Z<=C;
when "11"=>Z<=D;
when others=>Z<='X';
end case;
end process;
end architecture bhv;
邏輯圖:
4.十進制加法計數器(組合邏輯電路)
代碼:
--十進制加法計數器
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity CSDN_4 is
port(CLR: in std_logic;--清零
DIN: in std_logic_vector(3 downto 0);--輸入
CNT: out std_logic_vector(3 downto 0));--輸出
end entity CSDN_4;
architecture bhv of CSDN_4 is
begin
process(DIN,CLR) is
begin
if(CLR='1' or DIN="1001") then
CNT<="0000";
else
CNT<=DIN+1;
end if;
end process;
end architecture bhv;
驗證仿真波形圖:
5.十進制加法計數器(時序邏輯電路)
代碼:
--十進制加法計數器(時序邏輯電路)
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity CSDN_5 is
port(CLR: in std_logic;
CLK: in std_logic;
CNT: buffer std_logic_vector(3 downto 0));
end entity CSDN_5;
architecture bhv of CSDN_5 is
begin
process is
begin
wait until CLK'event AND CLK= '1';--等待CLK的上升沿
if (CLR= '1' or CNT=9) then
CNT<="0000";
else
CNT<=CNT+1;
end if;
end process;
end architecture bhv;
驗證仿真波形圖:
6.八線-三線優先編碼器(條件賦值語句)
代碼:
--8線-3線優先編碼器(條件賦值語句)
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
entity CSDN_6 is
port(A,B,C,D,E,F,G,H: in std_logic;
Y0,Y1,Y2: out std_logic);
end entity CSDN_6;
architecture bhv of CSDN_6 is
signal SY: std_logic_vector(2 downto 0);
begin
SY(2 downto 0)<="111" when H='1' else --優先級最高
"110" when G='1' else
"101" when F='1' else
"100" when E='1' else
"011" when D='1' else
"010" when C='1' else
"001" when B='1' else
"000" when A='1' else
"XXX";
Y0<=SY(0);
Y1<=SY(1);
Y2<=SY(2);
end architecture bhv;
驗證仿真波形圖:
7.設計指令譯碼器(選擇信號控制語句)
代碼:
--指令譯碼器
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity CSDN_7 is
port(A,B,C: in std_logic;--輸入控制信號
data1,data2: in std_logic;--輸入操作數據
dataout: out std_logic);--輸出結果
end entity CSDN_7;
architecture bhv of CSDN_7 is
signal Q :std_logic_vector(2 downto 0);
begin
Q<=C&B&A;
with Q select
dataout<=data1 AND data2 when "000",
data1 OR data2 when "001",
data1 NAND data2 when "010",
data1 NOR data2 when "011",
data1 XOR data2 when "100",
data1 XNOR data2 when "101",
'Z' when others; --Z爲高阻態
end architecture bhv;
邏輯圖:
8.設計三個相同與非門構成的電路(元件例化)
代碼:
--設計三個相同與非門構成的電路(元件例化)
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_8 is
port(A,B: in std_logic;
C: out std_logic);
end entity CSDN_8;
architecture bhv of CSDN_8 is
begin
C<=A NAND B; --與非
end architecture bhv;
library ieee;
use ieee.std_logic_1164.all;
entity OR3 is
port(A1,B1,C1,D1: in std_logic;
Z1: out std_logic);
end entity OR3;
architecture art of OR3 is
component CSDN_8 is --元件調用聲明
port(A,B: in std_logic;
C: out std_logic);
end component;
signal S1,S2: std_logic;
begin
U1: CSDN_8 port map(A1,B1,S1);--位置關聯
U2: CSDN_8 port map(A=>C1,C=>S2,B=>D1);--名字關聯方式
U3: CSDN_8 port map(S1,S2,C=>Z1);--混合關聯方式
end architecture art;
邏輯圖:
10.設計3線-8線譯碼器(高電平有效)
代碼:
--3線-8線譯碼器(高電平有效)
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity CSDN_10 is
port (A,B,C: in std_logic;
Y: out bit_vector(7 downto 0));
end entity CSDN_10;
--SLL邏輯左移
architecture bhv1 of CSDN_10 is
signal SR: std_logic_vector(2 downto 0);
begin
SR<=C&B&A;
Y<="00000001" SLL(conv_integer(SR));--SLL邏輯左移
--標準邏輯位矢量類型數據強制轉換成整數類型數據
end architecture bhv1;
--使用進程process語句
architecture bhv2 of CSDN_10 is
signal SR: std_logic_vector(2 downto 0);
begin
SR<=C&B&A;
process(SR) is
begin
Y<=(others=>'0'); --Y的非指定位都賦值給'0'
Y(conv_integer(SR))<='1'; --由SR所確定的Y的指定位賦值爲'1'
end process;
end architecture bhv2;
--when-else語句
architecture bhv3 of CSDN_10 is
signal SR: std_logic_vector(2 downto 0);
begin
SR<=C&B&A;
Y(0)<='1' when SR="000" else '0';--並行執行,當只有一個條件成立時,其他條件不成立
Y(1)<='1' when SR="001" else '0';
Y(2)<='1' when SR="010" else '0';
Y(3)<='1' when SR="011" else '0';
Y(4)<='1' when SR="100" else '0';
Y(5)<='1' when SR="101" else '0';
Y(6)<='1' when SR="110" else '0';
Y(7)<='1' when SR="111" else '0';
end architecture bhv3;
--case-when語句
architecture bhv4 of CSDN_10 is
signal SR: std_logic_vector(2 downto 0);
begin
SR<=C&B&A;
process(SR) is
begin
case SR is
when "000"=>Y<="00000001";
when "001"=>Y<="00000010";
when "010"=>Y<="00000100";
when "011"=>Y<="00001000";
when "100"=>Y<="00010000";
when "101"=>Y<="00100000";
when "110"=>Y<="01000000";
when "111"=>Y<="10000000";
when others=>Y<="00000000";
end case;
end process;
end architecture bhv4;
11.設計8線-3線優先編碼器(LOOP語句)
代碼:
--設計8線-3線優先編碼器(LOOP語句)
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
entity CSDN_11 is
port(A,B,C,D,E,F,G,H: in std_logic;
Y0,Y1,Y2: out std_logic);
end entity CSDN_11;
--loop循環語句
architecture bhv of CSDN_11 is
begin
process(A,B,C,D,E,F,G,H) is
variable V: std_logic_vector(7 downto 0);
variable I: integer;
begin
V:=(H,G,F,E,D,C,B,A);--H優先級最高
I:=7;
while I>=0 AND V(I)/='1' loop
I:=I-1;
end loop;
(Y2,Y1,Y0)<=conv_std_logic_vector(I,3);--轉換爲位寬爲3
end process;
end architecture bhv;
12.設計4選1信號選擇器(IF_ELSE語句)
代碼:
--4選1信號選擇器(IF_ELSE語句)
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_12 is
port(X: in std_logic_vector(3 downto 0);--輸入信號
A,B: in std_logic;--控制信號
Y: out std_logic);--輸出信號
end entity CSDN_12;
architecture bhv of CSDN_12 is
signal SEL : std_logic_vector(1 downto 0);
begin
SEL<=B&A;
process(X,SEL) is
begin
if(SEL="00") then Y<=X(0);
elsif (sel="01") then Y<=X(1);
elsif (sel="10") then Y<=X(2);
else Y<=X(3);
end if;
end process;
end architecture bhv;
13.設計8位單向總線驅動器
代碼:
--8位單向總線驅動器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_13 is
port(DIN: in std_logic_vector(7 downto 0);--輸入
EN: in std_logic;--使能端
DOUT: out std_logic_vector(7 downto 0));--輸出
end entity CSDN_13;
architecture bhv of CSDN_13 is
begin
process(EN, DIN) is
begin
if(EN='1') then
DOUT<=DIN;
else
DOUT<="ZZZZZZZZ";
end if;
end process;
end architecture bhv;
參考圖:
14.設計8位雙向總線緩衝器
代碼:
--設計8位雙向總線緩衝器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_14 is
port(A,B: inout std_logic_vector(7 downto 0);--雙向輸入輸出端口
EN,DIR: in std_logic);--使能端,方向控制端口
end entity CSDN_14;
architecture bhv of CSDN_14 is
signal AOUT,BOUT: std_logic_vector(7 downto 0);
begin
process(A,EN,DIR) is --A輸入B輸出
begin
if((EN='0') AND (DIR='1')) then
BOUT<=A;
else
BOUT<="ZZZZZZZZ";
end if;
B<=BOUT;
end process;
process(B,EN,DIR) is --B輸入A輸出
begin
if((EN='0') AND (DIR='0')) then
AOUT<=B;
else
AOUT<="ZZZZZZZZ";
end if;
A<=AOUT;
end process;
end architecture bhv;
參考圖:
15.設計異步復位/置位的D觸發器
代碼:
--異步復位/置位的D觸發器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_15 is
port(CLK,D,PRESET,CLR: in std_logic;
Q: out std_logic);
end entity CSDN_15;
architecture bhv of CSDN_15 is
begin
process(CLK, PRESET, CLR) is
begin
if(PRESET='1') then --置位信號爲1,觸發器被置位
Q<='1';
elsif(CLR='1') then --復位信號爲1,觸發器被複位
Q<='0';
elsif(CLK'EVENT AND CLK='1') then
Q<=D;
end if;
end process;
end architecture bhv;
參考圖:
16.設計同步復位的D觸發器
代碼:
--同步復位的D觸發器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_16 is
port(D,CLK,RESET: in std_logic;
Q: out std_logic);
end entity CSDN_16;
architecture bhv of CSDN_16 is
begin
process(CLK) is
begin
if(CLK'EVENT AND CLK='1') then
if(RESET='0') then --時鐘上升沿到來且復位信號有效時,觸發器被複位
Q<='0';
else
Q<=D;
end if;
end if;
end process;
end architecture bhv;
參考圖:
17.設計JK觸發器
代碼:
--JK觸發器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_17 is
port(J,K,CLK: in std_logic;
Q,QB: buffer std_logic);
end entity CSDN_17;
architecture bhv of CSDN_17 is
signal Q_S, QB_S: std_logic;--保持信號變化的中間結果
begin
process(CLK,J,K) is
begin
if(CLK'EVENT AND CLK='1') then
if(J='0' AND K='1') then
Q_S<='0';
QB_S<='1';
elsif(J='1' AND K='0') then
Q_S<='1';
QB_S<='0';
elsif(J='1' AND K='1') then --反轉
Q_S<=NOT Q_S;
QB_S<=NOT QB_S;--輸出翻轉
end if;
end if;
Q<=Q_S;
QB<=QB_S;
end process;
end architecture bhv;
參考圖:
18.設計並行輸入,同步復位的8位移位寄存器
代碼:
--並行輸入,同步復位的8位移位寄存器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_18 is
port(CLK, RESET: in std_logic; --時鐘信號,復位信號
LSFT, REFT: in std_logic; --左移信號,右移信號
DATA: in std_logic_vector(7 downto 0); --數據輸入
MODE: in std_logic_vector(1 downto 0); --模式控制
QOUT: buffer std_logic_vector(7 downto 0)); --輸出端口
end entity CSDN_18;
architecture bhv of CSDN_18 is
begin
process is
begin
wait until (rising_edge(CLK));
if (RESET='1') then --同步復位
QOUT<="00000000";
else
case MODE is
when "01"=>QOUT<=REFT&QOUT(7 downto 1); --右移一位
when "10"=>QOUT<=QOUT(6 downto 0)&LSFT; --左移一位
when "11"=>QOUT<=DATA; --並行輸入
when others=>NULL;
end case;
end if;
end process;
end architecture bhv;
參考圖:
19.設計帶時鐘使能的十進制同步計數器
代碼:
--帶時鐘使能的十進制同步計數器
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_19 is
port(CLK: in std_logic; --計數時鐘信號
CLR: in std_logic; --清零信號
ENA: in std_logic; --計數使能信號
CQ: out integer range 0 to 15; --4位計數結果輸出
CO: out std_logic); --計數進位
end entity CSDN_19;
architecture bhv of CSDN_19 is
signal CQ1: integer range 0 to 15;--保持計數過程的中間結果
begin
process(CLK,CLR,ENA) is --計數控制進程
begin
if CLR='1' then --異步清零
CQ1<=0;
elsif CLK'EVENT AND CLK='1' then
if ENA='1' then
if CQ1=9 then --等於9,計數器清零
CQ1<=0;
else
CQ1<=CQ1+1;
end if;
end if;
end if;
end process;
process(CLK,CQ1) is --控制進位輸出
begin
if CLK'EVENT AND CLK='1' then --利用同步時鐘可以去毛刺
if CQ1=9 then
CO<='1';
else
CO<='0';
end if;
end if;
end process;
CQ<=CQ1;
end architecture bhv;
驗證仿真波形圖:
20.設計8421 BCD碼60進制計數器
代碼:
--8421 BCD碼60進制計數器
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity CSDN_20 is
port(EN: in std_logic; --使能信號
RESET: in std_logic; --異步復位
LOAD: in std_logic; --置數
D: in std_logic_vector(7 downto 0); --預置數據
CLK: in std_logic; --時鐘信號
CO: out std_logic; --進位輸出
QH: buffer std_logic_vector(3 downto 0); --輸出高四位
QL: buffer std_logic_vector(3 downto 0)); --輸出低四位
end entity CSDN_20;
architecture bhv of CSDN_20 is
begin
CO<='1' when (QH="0101" AND QL="1001" AND EN='1' ) --進位輸出
else '0';
process (CLK,RESET) is
begin
if(RESET='0') then --異步復位
QH<="0000";
QL<="0000";
elsif(CLK'EVENT AND CLK='1') then --同步置數
if(LOAD='1') then
QH<=D(7 downto 4);
QL<=D(3 downto 0);
elsif(EN='1') then --計數控制
if(QL=9) then
QL<="0000";
if(QH<=5) then
QH<="0000";
else
QH<=QH+1;
end if;
else
QL<=QL+1;
end if;
end if;
end if;
end process;
end architecture bhv;
參考圖:
21.設計1khz方波轉換爲50hz信號的非均勻分頻電路
代碼:
--1khz方波轉換爲50hz信號的非均勻分頻電路
library ieee;
use ieee.std_logic_1164.all;
entity CSDN_21 is
port(CLK: in std_logic; --1khz信號輸入
NEWCLK: out std_logic); --50hz信號輸出
end entity CSDN_21;
architecture bhv of CSDN_21 is
signal CNT: integer range 0 to 10#19#; --保持計數中間結果
begin
process(CLK) is --分頻計數器,1khz時鐘信號產生50hz信號
begin
if CLK'EVENT AND CLK='1' then
if CNT=10#19# then --1khz轉換50hz,分頻常數爲1000/50=20
CNT<=0;
else
CNT<=CNT+1;
end if;
end if;
end process;
process(CLK,CNT) is --分頻輸出
begin
if CLK'EVENT AND CLK='1' then
if CNT=10#19# then
NEWCLK<='1';
else
NEWCLK<='0';
end if;
end if;
end process;
end architecture bhv;
驗證仿真波形圖:
22.設計1khz方波轉換爲50hz信號的均勻分頻電路
代碼:
--1khz方波轉換爲50hz信號的均勻分頻電路
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity CSDN_22 is
port(CLK_IN: in std_logic;
RESET: in std_logic;
CLK_OUT: out std_logic);
end entity CSDN_22;
architecture bhv of CSDN_22 is
constant Q: integer:=20;
begin
process(CLK_IN,RESET) is
variable CNT: integer range 0 to 19;
begin
if(RESET='1') then
CNT:=0;
CLK_OUT<='0';
elsif rising_edge(CLK_IN) then
if(CNT<(Q/2)) then --前一半週期輸出高電平
CLK_OUT<='1';
CNT:=CNT+1;
elsif(CNT<(Q-1)) then --後一半週期輸出低電平
CLK_OUT<='0';
CNT:=CNT+1;
else --計數值大於20,則清零
CNT:=0;
end if;
end if;
end process;
end architecture bhv;
驗證仿真波形圖:
23.