27
Các bài tập VHDL Hiệutú_HY ^_^ Bài 1: m ạch so s ánh 4 bit: +mô tả: -mạch gồm có 2 đầu vào là a,b và 3 đầu ra là y1,y2,y3. với mỗi đầu vào chứa 4bit. mạch sẽ thực hiện so sánh giá trị của 2 đầu vào a và b và sẽ hiện thì ra tại các đầu ra y. đầu ra y hiện thị giá trị logic 1 và logic 0. +chương trình: library IEEE; use IEEE.STD_LOGIC_1164.ALL; Entity sosanh is Port (a,b: in std_logic_vector (3 downto 0); Y1: out std_logic; Y2:out std_logic; Y3: out std_logic); End sosanh; Architecture Behavioral of sosanh is Begin Process(a,b) Begin If (a>b) then y1<=’1’; Elsif (y1=’0’); End if; If (a<b) then y2=’1’; Elsif (y2=’0’); End if; If (a=b) then y3=’1’; Elsif (y3=’0’);

Bai Tap Vhdl

Embed Size (px)

Citation preview

Page 1: Bai Tap Vhdl

Các bài tập VHDL

Hiệutú_HY ^_^

Bài 1: m ạch so s ánh 4 bit:

+mô tả:-mạch gồm có 2 đầu vào là a,b và 3 đầu ra là y1,y2,y3. với mỗi đầu vào chứa 4bit. mạch sẽ thực hiện so sánh giá trị của 2 đầu vào a và b và sẽ hiện thì ra tại các đầu ra y. đầu ra y hiện thị giá trị logic 1 và logic 0.+chương trình:library IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity sosanh isPort (a,b: in std_logic_vector (3 downto 0); Y1: out std_logic; Y2:out std_logic; Y3: out std_logic);End sosanh;Architecture Behavioral of sosanh isBegin Process(a,b) Begin If (a>b) then y1<=’1’; Elsif (y1=’0’); End if; If (a<b) then y2=’1’; Elsif (y2=’0’); End if; If (a=b) then y3=’1’; Elsif (y3=’0’); End if; End process; End Behavioral;

Page 2: Bai Tap Vhdl

Bài 2: Mạch chia tần 16:

+mô tả hoạt động:-mạch chia tần 16 gồm có đầu vào clock & reset,và 1 đầu ra out.-Nhưng thực chất của mạch chia tần 16 thì nó có cấu tạo gồm có mạch chia tần 2,4,8,16 vì vậy ta cần khai báo thêm các tín hiệu: div2.div4,div8 & div16.-tín hiệu vào của bộ div2 là clock & reset; của div4 là đầu ra của bộ div2 & reset; của div8 là đầu ra của bộ div4 & reset; cuối cùng là bộ div16 là đầu ra của bộ div8 & reset. T lấy tín hiệu ra là từ bộ div16.+ch ương trìnhlibrary IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity Div16 isPort (ck: in std_logic; Reset: in std_logic; Y: out std_logic);End div16;Architecture Behavioral of Div16 is Signal div2,div4,div8,div16: std_logic;Begin Process(ck,reset,div2,div4,div8) Begin If (reset=’1’) then div2<=’0’; Elsif (ck’event and ck=’1’) then div2<= not div2; End if; If (reset=’1’) then div4=’0’; Elsif (div2’event and div2=’1’) then div4= not div4; End if; If (reset=’1’) then div8=’0’; Elsif (div4’event and div4=’1’) then div8= not div8; End if; If (reset=’1’) then div16=’0’; Elsif (div8’event and div8=’1’) then div16= not div16; End if; If (reset=’1’) then y<=’0’; Elsif (ck’event and ck=’1’) then y<=div16; End if; End process;End Behavioral;

Page 3: Bai Tap Vhdl

Bài 3: Bộ đếm ko đồng bộ,thiết kế bộ đếm lùi sử dụng trigơ T:

+mô tả hoạt động:-mạch đếm ko đồng bộ gồm có 2 đầu vào là clock & reset và có 4 đầu ra từ count(0) đến count(3).-khi chưa có tín hiệu vào thì ck=’0’ và khi đó count(0)=’0’; và khi có tín hiệu vào thì ck sẽ thay đổi từ 0 lên 1 và lúc này count(0) cũng sẽ thay đổi và count(0)= not count(0). Tương tự với các bộ count(1),count(2) và count(3).-nhưng đầu vào của count(0) là clock & reset,của count(1) là đầu ra của count(0) & reset,của count(2) là đầu ra của count(1) & reset,của count(3) là đầu ra của count(2) và reset.+chương trình:library IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity dem_dongbo is Port (ck:in std_logic; Reset:in std_logic; Count: inout std_logic_vector(3 downto 0)); End dem_dongbo;Architecture Behavioral of dem_dongbo isBegin Process (ck,reset.count) Begin If (reset=’1’) then count<=”0000”; Elsif (ck’event and ck=’1’) then count(0)<= not count(0); End if; If (count(0)’event and count(0)=’1’) then count(1)= not count(1); End if; If (count(1)’event and count(1)=’1’) then count(2)= not count(2); End if; If (count(2)’event and count(2)=’1’) then count(3)= not count(3); End if; End if; End process; End Behavioral;

Page 4: Bai Tap Vhdl

Bài 4: Bộ chốt 4bit(hay thanh ghi 4 bit):

+mô t ả hoạt động: -bộ chốt 4 bit :gồn có đầu vào Din,reset và clock và 1 đầu ra Dout. Đầu và Din & đầu ra Dout đều chứa 4 bit.-khi reset=1 thì đầu ra sẽ k có tín hiệu. Sau đó khi ta thay đổi giá trị của xung clock thì đầu ra bắt đầu có tín hiệu và Dout=Din.+chương trìnhlibrary IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity bochot_4bit is Port (Din:in std_logic_vector (3 downto 0); Ck,R: in std_logic; Dout: out std_logic_vector(3 downto 0)); End bochot_4bit;Architecture behavioral of bochot_4bit isBegin Process(ck,R) Begin If (R=’1’) then Dout <=”0000”; Elsif (ck’event and ck=’1’) then Dout<= Din; End if; End process; End Behavioral;

Page 5: Bai Tap Vhdl

Bài 5: M ạch giải m ã BCD 4bit sang led 7 thanh k chung:

+mô tả hoạt động:-mạch gồm có 3 đầu vào là Din,ck 7 reset và 1 đầu ra Dout. đầu vào Din chứa 4 bit và đầu ra Dout chứa 7 bit. Khi reset=1 thì đầu ra sẽ ko có tín hiệu,khí thay đổi giá trị của reset và ck thì có tín hiệu tại đầu ra.+chương trìnhlibrary IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity BCD_led7 is Port (Din: in std_logic_vector(3 downto 0); Ck: in std_logic; Reset: in std_logic; Dout: out std_logic_vector(6 downto 0)); End BCD_led7;Architecture Behavioral of BCD_led7 isBegin Process (ck,reset,Din) Begin If reset=’1’ then Dout<=”0000000”; Elsif (ck’event and ck=’1’) then Case Din is when "0000" => Dout <= "1111110"; --0 when "0001" => Dout <= "0110000"; --1 when "0010" => Dout <= "1101101"; --2 when "0011" => Dout <= "1111001"; --3 when "0100" => Dout <= "0111011"; --4 when "0101" => Dout <= "1011011"; --5 when "0110" => Dout <= "1011111"; --6 when "0111" => Dout <= "1110000"; --7 when "1000" => Dout <= "1111111"; --8 when "1001" => Dout <= "1111011"; --9 when others => Dout <= null; End case; End if; End process;End Behavioral;

số Led 70 11111101 01100002 11011013 11110014 01110115 10110116 10111117 11100008 11111119 1111011

10->15 0000000

Page 6: Bai Tap Vhdl

Bài 6: Mạch mã hoá 8:3 sử dụng case:

+mô tả hoạt động:-mạch gồm có đầu vào input chứa 8 bit và đầu ra output chứa 3 bit.Khi ta thay đổi giá trị tại đầu vào của mạch thì mạch sẽ mã hoá cho ta các ra giá trị ra tương ứng tại đầu ra.+chương trình:library IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity mahoa_83 is Port(input:in std_logic_vector(7 downto 0); Output : out std_logic_vector(2 downto 0)); End mahoa_83;Architecture Behavioral of mahoa_83 is Begin Process(input) Begin Case input is When “00000001” => output<=”000”; When “00000010” => output<=”001”; When “00000100” => output<=”010”; When “00001000” => output<=”011”; When “00010000” => output<=”100”; When “00100000” => output<=”101”; When “01000000” => output<=”110”; When “10000000” => output<=”111”; When others => output<=null; End case; End process; End behavioral;

A7

A6

A5

A4

A3

A2

A1

A0

Y2

Y1

Y0

0 0 0 0 0 0 0 1 0 0 00 0 0 0 0 0 1 0 0 0 10 0 0 0 0 1 0 0 0 1 00 0 0 0 1 0 0 0 0 1 10 0 0 1 0 0 0 0 1 0 00 0 1 0 0 0 0 0 1 0 10 1 0 0 0 0 0 0 1 1 01 0 0 0 0 0 0 0 1 1 1

Page 7: Bai Tap Vhdl

Bài 7: B ộ gh ép kênh 8:1:

+mô t ả hoạt động: bộ ghép kênh 8 lối vào 1 lối ra gồm có 8 lối vào từ x0 đến x7,với mỗi lối vào là 8 bít x0(7:0) đến x7(7:0),1 lối ra y(7:0). S(2:0) là các đầu vào điều khiển, để thay đổi lần lượt từ x0(7:0) đến x7(7:0)nphải có đầu vào điều khiển. Vậy đối với mạch ghép kênh 8 lối vào 1 lối ra cần đầu vào điều khiển là s.+ các Chương trình:1.s ử dụng if: library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity Mux_81 isPort (s : in std_logic_vector(2 downto 0); X0,x1,x2,x3.x4.x5,x6,x7: in std_logic_vector (7 downto 0); y : out std_logic_vector (7 downto 0));end Mux_81;architecture Behavioral of Mux_81 isBegin Process(s) If (s=”000”) then y<= x0; Elsif (s=”001”) then y<=x1; Elsif (s=”010”) then y<=x2; Elsif (s=”011”) then y<=x3; Elsif (s=”100”) then y<=x4; Elsif (s=”101”) then y<=x5; Elsif (s=”110”) then y<=x6; Else y<=x7; End if; End process;End Behavioral;

bảng chân lý:s y

0 0 0 X00 0 1 X10 1 0 X20 1 1 X31 0 0 X41 0 1 X51 1 0 X61 1 1 X7

Page 8: Bai Tap Vhdl

2. s ử dụng case library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity Mux_81 isPort (s : in std_logic_vector(2 downto 0); X0,x1,x2,x3.x4.x5,x6,x7: in std_logic_vector (7 downto 0); y : out std_logic_vector (7 downto 0));end Mux_81;architecture Behavioral of Mux_81 isBegin Process(s) Case input is When “000” => y<=x0; When “001” => y<=x1; When “010” => y<=x2; When “011” => y<=x3; When “100” => y<=x4; When “101” => y<=x5; When “110” => y<=x6; When “111” => y<=x7; End case; End process; End behavioral;

Page 9: Bai Tap Vhdl

Bài 8: thiết kế mạch mã hoá 4 đường sang 2 đường với ngõ vào tích hợp ở mức cao, sử dụng phát biểu case.

+mô tả:-mạch gồm có 4 đầu vào từ I0 đến I3 và có 2 đầu ra là Q0 & Q1. với mỗi giá tại đầu vào thì mạch sẽ mã hoá cho ta 1 giá trị tại đầu ra của mạch.+chương trình:library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity mahoa_42 is Port (I : in std_logic_vector(3 downto 0); Q : out std_logic_vector (1 downto 0));end mahoa_42;architecture Behavioral of mahoa_42 isBegin Process(I) Begin Case I is When “0001” => Q<=”00”; When “0010” => Q<=”01”; When “0100” => Q<=”10”; When “1000” => Q<=”11”; When others => null; End case; End process; End behavioral;

bảng chân lý

Ngõ vào Ngõ raI3 I2 I1 I0 Q1 Q00 0 0 1 0 00 0 1 0 0 10 1 0 0 1 01 0 0 0 1 1

Page 10: Bai Tap Vhdl

Bài 9: M ạch m ã hoá 8:3

+mô t ả hoạt động: Mạch mã hoá dùng để biến đổi dữ liệu rời rạc thành dạng nhị phân. Mạch mã hoá có 2n hoặc ít hơn ngõ vào sẽ mã hoá dữ liệu ngõ vào để cung cấp n ngõ ra được mã hoá. Cần giẩ định chỉ có 1 ngõ vào tích cực (‘1’) ở 1 thời điểm cho trước, nếu ko ngõ ra sẽ ko có giá trị xác định nào & mạch trở nên vô nghĩa.+Các chương trình: 1.s ử dụng if: library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity mahoa_83 is Port (A : in std_logic_vector(7 downto 0); y : out std_logic_vector (2 downto 0)); end mahoa_83;architecture Behavioral of mahoa_83 isBegin Process(A) Begin If (A=”00000001”) then y<=”000”; elsIf (A=”00000010”) then y<=”001”; elsIf (A=”00000100”) then y<=”010”; elsIf (A=”00001000”) then y<=”011”; elsIf (A=”00010000”) then y<=”100”; elsIf (A=”00100000”) then y<=”101”; elsIf (A=”01000000”) then y<=”110”; elsIf (A=”10000000”) then y<=”111”; else y<=”xxx”; End if; End process; End behavioral;2.s ử dụng ph át bi ểu case: library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity mahoa_83 is Port (A : in std_logic_vector(7 downto 0); y : out std_logic_vector (2 downto 0));

A7

A6

A5

A4

A3

A2

A1

A0

Y2

Y1

Y0

0 0 0 0 0 0 0 1 0 0 00 0 0 0 0 0 1 0 0 0 10 0 0 0 0 1 0 0 0 1 00 0 0 0 1 0 0 0 0 1 10 0 0 1 0 0 0 0 1 0 00 0 1 0 0 0 0 0 1 0 10 1 0 0 0 0 0 0 1 1 01 0 0 0 0 0 0 0 1 1 1

end mahoa_83;architecture Behavioral of mahoa_83 isBegin Case A is When “00000001” => y<=”000”; When “00000010” => y<=”001”; When “00000100” => y<=”010”; When “00001000” => y<=”011”; When “00010000” => y<=”100”; When “00100000” => y<=”101”; When “01000000” => y<=”110”; When “10000000” => y<=”111”; When others =>y<=null; End case; End process; End behavioral;

Page 11: Bai Tap Vhdl

Bài 10: M ạch m ã hoá ưu tiên:

+Mô t ả hoạt động: nếu có 2 hay nhiều ngõ vào ở mức logic tích cực (‘1’),ngõ vào có mức ưu tiên cao nhất sẽ được ưu tiên hơn và giá trị mã hoá cụ thể của ngõ vào này sẽ xuất hiện ở các ngõ ra.+ Các chương trình:1.s ử dụng ph át bi ểu if: library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity 83_uutien is Port (A : in std_logic_vector(7 downto 0); y : out std_logic_vector (2 downto 0)); end 83_uutien;architecture logic of 83_uutien isBegin Process(A) Begin If (A(7)=’1’) then y<=”111”; elsIf (A(6)=’1’) then y<=”110”; elsIf (A(5)=’1’) then y<=”101”; elsIf (A(4)=’1’)then y<=”110”; elsIf (A(3)=’1’) then y<=”100”; elsIf (A(2)=’1’) then y<=”011”; elsIf (A(1)=’1’) then y<=”001”; elsIf (A(0)=’1’) then y<=”000”; else valid<=’0’; y<=”xxx”; End if; End process; End logic;2.Phát bi ểu case: library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity 83_uutien is Port (A : in std_logic_vector(7 downto 0); y : out std_logic_vector (2 downto 0)); end 83_uutien;architecture logic of 83_uutien is

A7

A6

A5

A4

A3

A2

A1

A0

Y2

Y1

Y0

valid

0 0 0 0 0 0 0 1 0 0 0 10 0 0 0 0 0 1 X 0 0 1 10 0 0 0 0 1 X X 0 1 0 10 0 0 0 1 X X X 0 1 1 10 0 0 1 X X X X 1 0 0 10 0 1 X X X X X 1 0 1 10 1 X X X X X X 1 1 0 11 X X X X X X X 1 1 1 10 0 0 0 0 0 0 0 X X x 0

Begin Process(A) Begin A_int:=to_integer (A); Valid <=’1’; Case (A) is When 128 to 255 => y<=”111”; When 64 to 127 => y<=”110”; When 32 to 63 => y<=”101”; When 16 to 31 => y<=”100”; When 8 to 15 => y<=”011”; When 4 to 7 => y<=”010”; When 2 to 3 => y<=”001”; When 1 => y<=”000”; When others =>y<=valid; Y<=”xxx”; End case; End process; End logic;

Page 12: Bai Tap Vhdl

Bài 11: M ạch giải m ã 3:8:

+mô t ả hoạt động: Các mạch giải mã được sử dụng để giải mã dữ liệu,dữ liệu này đc mã hoá trước đây bằng cách dùng dạng mã hoá nhị phân hoặc có thể 1 dạng mã hoá khác. 1 mã n bit có thể biểu diễn đén 2n bit phân biệt của thông tin mã hoá, do vậy 1 mạch giải mã với n ngõ vào có thể giải mã đến 2n ngõ ra.+ Các phương trình:1.s ử dụng if: library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity giaima_38 is Port (A : in std_logic_vector(2 downto 0); y : out std_logic_vector (7 downto 0)); end giaima_38;architecture logic of giaima_38 isBegin Process(A) Begin If (A=”000”) then y<=”00000001”; elsIf (A=”001”) then y<=”00000010”; elsIf (A=”010”) then y<=”00000100”; elsIf (A=”011”) then y<=”00001000”; elsIf (A=”100”) then y<=”00010000”; elsIf (A=”101”) then y<=”00100000”; elsIf (A=”110”) then y<=”01000000”; else y<=”10000000”; End if; End process; End logic;2.s ử dụng case: library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity giaima_38 is Port (A : in std_logic_vector(2 downto 0); y : out std_logic_vector (7 downto 0)); end giaima_38;architecture logic of giaima_38 is

sơ đồ:

bảng chân lý:A2

A1

A0

Y7

Y6

Y5

Y4

Y3

Y2

Y1

Y0

0 0 0 0 0 0 0 0 0 0 10 0 1 0 0 0 0 0 0 1 00 1 0 0 0 0 0 0 1 0 00 1 1 0 0 0 0 1 0 0 01 0 0 0 0 0 1 0 0 0 01 0 1 0 0 1 0 0 0 0 01 1 0 0 1 0 0 0 0 0 01 1 1 1 0 0 0 0 0 0 0

Begin Case (A) 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<=null; End case; End process; End logic;

Page 13: Bai Tap Vhdl

Bài 12: Thiết kế mạch đa hợp 4 ngõ vào,1 ngõ ra,2 ngõ lựa chọn:

+mô tả hoạt động:-mạch gồm có 4 đầu vào từ I0 đến I3 và 1 đầu ra Q.s0,s1 là các đầu vào điều khiển. để thay đổi lần lượt từ I0 đến I3 thì phải có điều khiển,việc đó đc thực hiện bởi 2 đầu vào điều khiển s0 & s1.+ch ương trìnhlibrary IEEE;use IEEE.STD_LOGIC_1164.ALL;entity mux_41 is Port (I : in std_logic_vector(3 downto 0); S: in std_logic_vector (1 downto 0); Q : out std_logic);end mux_41;architecture Behavioral of mux_41 isBegin Process(I,s) Begin Case s is When “00” => Q<=I0; When “10” => Q<=I1; When “10” => Q<=I2; When “11” => Q<=I3; When others => Q <=null; End case; End process; End Behavioral;

S1 S0 I3 I2 I1 I0 Q0 0 X X X I0 I00 1 X X I1 X I11 0 X I2 X X I21 1 I3 X X X I3

Bảng chân lí

sơ đồ mạch

Page 14: Bai Tap Vhdl

Bài 13: Các cách mô t ả mạch gh ép kênh 4:1, 1 bit:

+mô t ả hoạt động: bộ ghép kênh 4 lối vào 1 lối ra gồm có 4 lối vào A,B,C,D,với mỗi lối vào là 1 bit và 1 lối ra y. S1,s2 là các đầu vào điều khiển, để thay đổi lần lượt các đầu vào từ A đến D phải có điều khiển, vậy đối với mạch này thì cần có đầu vào điều khiển là s1,s2.+ các chương trình: 1.s ử dụng ph át bi ểu if: library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity Mux_41 isPort (A,B,C,D,s1,s2: in std_logic; Y: out std_logic);end Mux_41;architecture Behavioral of Mux_41 isBegin Process(A,B,C,D,s1,s2) If (s1s2=”00”) then y<= A; Elsif (s1s2=”01”) then y<=B; Elsif (s=”10”) then y<=C; Else y<=D; End if; End process;End Behavioral; 2.s ử dụng ph ép gán tín hi ệu c ó đi ều kiện library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity Mux_41 isPort (A,B,C,D,s1,s2: in std_logic; Y: out std_logic);end Mux_41;architecture Behavioral of Mux_41 is Begin Y<=A When s1s2=“00” else B When s1s2=“10” else C When s1s2=“10 else Y<=D; End behavioral;

Bảng chân lý:

S1 S2 A B C D Y0 0 1 0 0 0 10 1 0 1 0 0 11 0 0 0 1 0 11 1 0 0 0 1 1

2.s ử dụng ph át bi ểu case: library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity Mux_41 isPort (A,B,C,D,s1,s2: in std_logic; Y: out std_logic);end Mux_41;architecture Behavioral of Mux_41 isBegin Process(A,B,C,D,s1,s2) Begin Case s1 s2 is When “00” => y<=A; When “10” => y<=B; When “10” => y<=C; When “11” => y<=D; End case; End process; End behavioral;

Page 15: Bai Tap Vhdl

Bài 14: Mạch cộng ko đầy đủ 1bit:

+mô tả:-mạch gồm có 2 đầu vào là A,B và 2 đầu ra là Cout, sum. với sum là tổng của 2 đầu vào và Cout là bit nhớ.+chương trìnhlibrary IEEE;use IEEE.STD_LOGIC_1164.ALL;entity HALF_ADD is Port ( A,B : in STD_LOGIC; Sum, Cout : out STD_LOGIC);end HALF_ADD;architecture Behavioral of HALF_ADD isbegin Sum <= A xor B; Cout <= A and B;end Behavioral;

Sơ đồ:

bảng chân lý:A B Cout Sum0 0 0 00 1 0 11 0 0 11 1 1 0

Page 16: Bai Tap Vhdl

Bài 11: Mạch cộng đầy đủ 1bit:

library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity FULL_ADD is Port ( A,B,Cin : in STD_LOGIC; Cout,Sum : out STD_LOGIC);end FULL_ADD;architecture Behavioral of FULL_ADD is COMPONENT HALF_ADD PORT(A : IN std_logic; B : IN std_logic; Sum : OUT std_logic; Cout : OUT std_logic); END COMPONENT; signal AplusB,CoutHA1,CoutHA2: STD_LOGIC; begin HA1: HALF_ADD PORT MAP ( A => A, B => B, Sum => APlusB, Cout => CoutHA1); HA2: HALF_ADD PORT MAP( A => AplusB, B => Cin, Sum => Sum, Cout => CoutHA2); Cout <= CoutHA1 or CoutHA2;end Behavioral;

+mô tả:-mạch gồm có 3 đầu vào A,B,Cin và 2 đầu ra Cout,sum.-với sum là tổng của 3 đầu vào và Cout là bit nhớ+Bảng chân lý:

A B Cin Sum Cout0 0 0 0 00 0 1 1 00 1 0 1 00 1 1 0 11 0 0 1 01 0 1 0 11 1 0 0 11 1 1 1 1

Page 17: Bai Tap Vhdl

Bài 15: Mạch ghép kênh 2:1, độ rộng 1 bit:

+ mô tả hoạt động:mạch ghép kênh có lựa chọn 1,2 hoặc nhiều tín hiệu ngõ vào đến ngõ ra. Một or nhiều tín hiệu điều khiển sẽ điều khiển giá trị của tín hiệu ngõ vào nào được truyền đến ngõ ra. Mỗi tín hiệu ngõ vào và tín hiệu ngõ ra có thể là 1 bít đơn or bus nhiều bit. Các ngõ vào lựa chọn thường được mã hoá nhị phân sap cho n ngõ vào lựa chọn có thể chọn từ 1 đến 2n ngõ vào. + chương trình:library IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity mux_21 is Port (A,B:in std_logic; s: in std_logic; y: out std_logic); End mux_21;Architecture behavioral of mux_21 isBegin Process(A,B) Begin If (s=’0’) then y<=A; Else y<=B; End if; End process; End Behavioral;

bảng chân lý: S A B Y0 0 X 00 1 X 11 X 0 01 X 1 1

Page 18: Bai Tap Vhdl

Bài 16: mạch giải mã địa chỉ 4bit:

library IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity giaima_4bit is Port (x:in integer range 0 to 15; A,C,D: out std_logic; B: out std_logic_vector(3 downto 0)); End giaima_4bit;Architecture logic of giaima_4bit isBegin Process(x) Begin A<=’0’; B<=(others=>’0’); C<=’0’; D<=’0’; Case (x) is When 0 to 3 => A<=’1’; When 4 => B(0)<=’1’; When 5 => B(1)<=’1’; When 6 => B(2)<=’1’; When 7 => B(3)<=’1’; When 8 to 11 => C<=’1’; When 12 to 15 => D<=’1’; End case; End process; End logic;

Page 19: Bai Tap Vhdl

Bài 17: C ổng 3 trạng th ái:

Lo ại 1: sử dụng when: +mô t ả hoạt động: cổng 3 trạng thái có đầu ra output=input khi ena=0 và có bằng trở kháng cao khi ena=1.+chương trình:library IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity 3trangthai is Port (ena:in std_logic; input:in std_logic; output: out std_logic); End 3trangthai;Architecture Behavioral of 3trangthai isBegin Output<=input when (ena=’0’) Else (others => ‘z’);End Behavioral;Lo ại 2: sử dụng if: library IEEE;use IEEE.STD_LOGIC_1164.ALL;Entity 3trangthai is Port (A,E: out std_logic; y: out std_logic); End 3trangthai;Architecture Behavioral of 3trangthai isBegin If (E=’0’) then y=’A’; Else y=’z’;End Behavioral;

sơ đồ:

bảng chân lý:Ena In Out0 1 11 Z z

Sơ đồ:

bảng chân lý:

E A y0 1 11 Z z