You are on page 1of 21

- Cấu trúc của một thiết kế vhdl :

Gồm 3 phần:
+) khai báo thư viện
+)mô tả thực thể
+)mô tả kiến trúc ( kiểu hành vi, kiểu luồng dữ liệu,kiểu kiến trúc)
- Cú pháp khai báo thư viện,mô tả thực thể(in,out,hằng), ví dụ minh họa.
Khai báo thư viện
Library tên thư viện ;
Use tên thư viện . tên gói thư viện . thành phần của gói ;
Vd:
Library ieee;
Use iee.std_logic_1164.all;
Khai báo thực thể
Entity entity_name is
[ generic (generic_declaration);]
[port (port_declaration);]
{entity_declarative_item{constants,types,signals};}
End [entity_name];
[] tham số có thể lựa chọn
{} khai báo 1 hoặc nhiều đối tượng,mà các đối tượng này có thể được
định nghĩa bởi người dùng.

Vd: entity cong_and is


Port( a,b : in std_logic;
c : out std_logic);
End cong_and;
- Đối tượng dữ liệu trong vhdl : hằng, biến, tín hiệu ( ghi rõ đặc điểm, cú
pháp, lấy vd)
Một đối tượng dữ liệu giữ một giá trị của một kiểu nhất định. Trong
vhdl có 3 lớp đối tượng: các hằng(constants),các biến(variables),các tín
hiệu(signals). Lớp của một đối tượng được chỉ ra bởi một từ khóa và
nó được chỉ ra ở điểm bắt đầu của một khai báo.
*) các hằng (constants):
Hằng là một đối tượng mà nó được khởi tạo để chỉ ra một giá trị
cố định và không bị thay đổi.
Cú pháp : constant constant_name{,constant_name} : type[ := value];
Vd: constant yes : boolean := TRUE;
*) các biến ( variables)
Các biến được dùng để lưu dữ liệu tạm thời, chúng chỉ được phép khai
báo trong phát biểu process hoặc các chương trình con
Vd : variable x,y : bit;
*) các kiểu tín hiệu (signals)
Tín hiệu được dùng để kết nối các entity của thiết kế lại với nhau và
trao đổi các giá tri biến đổi ở trong phát biểu process.
Cú pháp signal signal_name {,signal_name} : type [:=value];
Vd signal beep : bit:=’0’;

- Phát biểu if,case : đặc điểm, cú pháp, ví dụ.


 Phát biểu if
Một phát biểu if được dùng để chọn lựa những phát biểu tuần tự cho việc
thực thi dựa trên giá trị của biểu thức điều kiện, biểu thức điều kiện ở đây
có thể là một biểu thức bất kì mà giá trị của chúng phải là kiểu lý luận
Cú pháp if boolean-expression then sequential-statements
{elsif boolean-expression then sequential-statements}
{else sequential-statements}
End if;
Ví dụ
If ( clk’event and clk=’1’) then
Dout<= din;
End if
 Phát biểu case
Phát biểu case lựa chọn một trong những nhánh cho việc thực thi dựa trên
giá trị của biểu thức, giá trị của biểu thức phải thuộc kiểu rời rạc hoặc
kiểu mảng một chiều. Các lựa chọn có thể được diễn tả như một giá trị
đơn , hoặc một dải giá trị bằng việc sử dụng dấu “ | “ hoặc sử dụng mệnh
đề khác.tất cả các giá trị có thể có của biểu thức phải được thể hiện trong
phát biểu case đúng một lần. Các mệnh đề khác có thể được sử dụng để
bao quát tất cả các giá trị và nếu có phải là nhánh cuối cùng trong phát
biểu case. Mỗi một lựa chọn phải có cùng kiểu với kiểu của biểu thức.
Cú pháp case expression is
When choices => sequetial-statements -- branch 1
When choices=> sequetial-statements -- branch 2
--có thể có nhiều nhánh
{when others => sequetial-statements} -- last branch
End case;
Vd : case a is
when “000” => b<=“00”;
when “100”|”010”|”001” => b<= “10”;
when “110”|”101”|”011” => b<= “01”;
when others => b<= “11”;
end case;
 Gán tín hiệu có điều kiện

Một phép gán tín hiệu có điều kiện chính là 1 phát biểu đồng thời và có
một đích gán nhất định, tuy nhiên phép gán này có thể có nhiều hơn 1
biểu thức cho một đích. Ngoại trừ biểu thức cuối cùng , các biểu thức còn
lại phải có 1 điều kiện chắc chắn, các điều kiện này được đánh giá theo
thứ tự. Nếu 1 điều kiện được đánh giá là true thì biểu thức tương ứng
được sử dụng, ngược lại các biểu thức còn lại sẽ được sử dụng, nhớ rằng
chỉ một biểu thức được sử dụng tại 1 thời điểm. Một phát biểu gán tín
hiệu có điều kiện có thể được mô tả bởi 1 phát biểu process, mà process
có chứa phát biểu if. Ta có thể sử dụng phát biểu gán tín hiệu có điều kiện
ở trong 2 process
Cú pháp
Target <= {expression [after time_ expression] when condition else}
expression [after time_ expression];
vd:
achitecture vd is
signal a,b,c,d : integer;
begin
a<=b when (d>5) else
c when (d> 3) else
d;
end vd;

Code
 Cổng logic and, or,xor,not

- Bộ cộng, bộ trừ
 Bộ cộng không đầy đủ
Library ieee;
Use ieee.std_logic_1164.all;
Entity bocongkhongdaydu is
Port ( a,b : in std_logic;
s, c: out std_logic);
end bocongkhongdaydu;
achitecture behavioral of bocongkhongdaydu is
begin
process (a,b)
begin
s<= a xor b;
c<= a and b;
end process;
end behavioral;

 Bộ cộng đầy đủ
Library ieee;
Use ieee.std_logic_1164.all;
Entity bocongdaydu is
Port ( a : in std_logic_vector ( 2 downto 0);
b: out std_logic_vector ( 1 downto 0));
end bocongdaydu;
achitecture behavioral of bocongdaydu is
begin
process (a)
begin
case a is
when “000” => b<=“00”;
when “100”|”010”|”001” => b<= “10”;
when “110”|”101”|”011” => b<= “01”;
when others => b<= “11”;
end case;
end process;
end behavioral;
mô phỏng
stim_proc: process
begin
a<="000";
wait for 100 ns;
a<="001";
wait for 100 ns;
a<="010";
wait for 100 ns;
a<="011";
wait for 100 ns;
a<="100";
wait for 100 ns;
a<="101";
wait for 100 ns;
a<="110";
wait for 100 ns;
a<="111";
wait for 100 ns;

end process;
END;

 Bộ nửa trừ
Library ieee;
Use ieee.std_logic_1164.all;
Entity bonuatru is
Port ( a,b : in std_logic;
d, r: out std_logic);
end bonuatru;
achitecture behavioral of bonuatru is
begin
process (a,b)
begin
d<= a xor b;
r<= not (a) and b;
end process;
end behavioral;
mô phỏng
stim_proc: process
begin
a<= '1';
b<= '1';
wait for 100 ns;
a<= '0';
b<= '0';
wait for 100 ns;
a<= '1';
b<= '0';
wait for 100 ns;
a<= '0';
b<= '1';
wait for 100 ns;
end process;
END;
 Bộ trừ đầ y đủ

Library ieee;
Use ieee.std_logic_1164.all;
Entity bocongtrudu is
Port ( a : in std_logic_vector ( 2 downto 0);
b: out std_logic_vector ( 1 downto 0));
end botrudaydu;
achitecture behavioral of botrudaydu is
begin
process (a)
begin
case a is
when "000" |"101"|"110"=> b<="00";
when "100" => b<= "10";
when "011" => b<= "01";
when others => b<= "11";
end case;
end process;
end behavioral;

mô phỏng
stim_proc: process
begin
a<="000";
wait for 100 ns;
a<="001";
wait for 100 ns;
a<="010";
wait for 100 ns;
a<="011";
wait for 100 ns;
a<="100";
wait for 100 ns;
a<="101";
wait for 100 ns;
a<="110";
wait for 100 ns;
a<="111";
wait for 100 ns;

end process;

END;

 Thanh ghi: pipo,piso,siso,sipo


 Thanh ghi 4 bit
Library ieee;
Use ieee.std_logic_1164.all;
Entity thanhghi is
Port ( din : in std_logic_vector ( 3 downto 0);
clk,rst: in std_logic;
dout: out std_logic_vector ( 3 downto 0));
end thanhghi;
achitecture behavioral of thanhghi is
begin
process (din,clk,rst)
begin
if( rst ='1') then dout<="0000";
elsif(clk'event and clk='1') then
dout<=din;
end if;
end process;
end Behavioral;

mô phỏng
clk_process :process
begin
clk <= '0';
wait for 100 ns;
clk <= '1';
wait for 100 ns;
end process;
stim_proc: process
begin
reset<='1';
wait for 100 ns;
reset<='0';
din<= "0101";
wait for 100 ns;
end process;
END;
 Trigger d

Port ( d : in STD_LOGIC;
clk : in STD_LOGIC;
q : inout STD_LOGIC:='0';
qdao : inout STD_LOGIC:='1');
end trigger_d;

architecture Behavioral of trigger is


begin
process(d,clk)
begin
if(clk'event and clk='1') then
q<=d;
qdao<=not d;
end if;
end process;

end Behavioral;

END;
 Trigger T
Port( d,clk : in std_logic;
q: inout std_logic:= '0';
qdao: inout std_logic:= '1');
end trigger_t;
achitecture behavioral of trigger_t is
begin
process (d,clk)
begin
if (clk’event and clk=’1’) then
if d=’0’ then q<=q;
qdao<= not q;
else q<= not q
qdao <= q;
end if;
end if;
end process;

 Trigger jk

Port(j,k,clk : in std_logic;
Q : inout std_logic:=’0’;
Qdao:inout std_logic:=’1’);
End trigger_jk;
Achitecture behavioral of trigger_jk is
Begin
Process(j,k,clk)
Begin
If( clk’event and clk=’1’) then
If( j=’0’ and k=’0’) then q<= q;
Qdao<= not q;
Elsif (j=’0’ and k=’1’) then q<=’0’;
Qdao<=’1’;
Elsif(j=’1’ and k=’0’) then q<=’1’;
Qdao<=’0’;
Else q<=not q;
Qdao<=q;
End if;
End if;
End process;
End behaivioral;

 Trigger rs
Port(r,s,clk : in std_logic;
Q : inout std_logic:=’0’;
Qdao:inout std_logic:=’1’);
End trigger_rs;
Achitecture behavioral of trigger_rs is
Begin
Process(r,s,clk)
Begin
If( clk’event and clk=’1’) then
If( r=’0’ and s=’0’) then q<= q;
Qdao<= not q;
Elsif (r=’0’ and s=’1’) then q<=’1’;
Qdao<=’0’;
Elsif(r=’1’ and s=’0’) then q<=’0’;
Qdao<=’1’;
Else q<=’-‘;
Qdao<=’-‘;
End if;
End if;
End process;
End behavioral;

 Siso

Library ieee;
Use ieee.std_logic_1164.all;
Entity siso is
Port(clk,sin : in std_logic;
Q : inout std_logic_vector(3 downto 0);
sout: out std_logic);
End siso;
Achitecture behavioral of siso is
Begin
Process(clk,sin)
Begin
If( clk’event and clk=’1’) then
Q(3)<= sin;
Q(2)<=q(3);
Q(1)<=q(2);
Q(0)<=q(1);
sout<=q(0);
end if;
end process;
end behavioral;

mô phỏng
clk_process: process
begin
clk<=’0’;
wait for 100ns;
clk<=’1’;
wait for 100ns;
end process;
stim_proc:process
begin
sin<=’0’;
wait for 100ns;
sin<=’1’;
wait for 100ns;
sin<=’1’;
wait for 100ns;
sin<=’0’;
wait for 100ns;
end process;
end;

 Sipo
Library ieee;
Use ieee.std_logic_1164.all;
Entity sipo is
Port(clk,sin : in std_logic;
Q : inout std_logic_vector(3 downto 0);
sout: out std_logic_vector(3 downto 0);
End sipo;
Achitecture behavioral of sipo is

Begin
Process(clk,sin)
Begin
If( clk’event and clk=’1’) then
Q(3)<= sin;
Q(2)<=q(3);
Q(1)<=q(2);
Q(0)<=q(1);
end if;
end process;
sout<=q;
end behavioral;

mo phong

clk_process :process
begin
clk <= '0';
wait for 100 ns;
clk <= '1';
wait for 100 ns;
end process;

stim_proc: process
begin
sin<='0';
wait for 100 ns;
sin<='1';
wait for 100 ns;
sin<='1';
wait for 100 ns;
sin<='0';
wait for 100 ns;
end process;

END;
 Pipo

Library ieee;
Use ieee.std_logic_1164.all;
Entity pipo is
Port(clk : in std_logic;
Sin,Q : inout std_logic_vector(3 downto 0);
sout: out std_logic_vector(3 downto 0);
End pipo;
Achitecture behavioral of pipo is
Begin
Process(clk,sin)
Begin
If(clk ‘event and clk=’1’) then
q<= sin;
End if;
End process;
Sout<= q;
End behavioral;

Mp

clk_process :process
begin
clk <= '0';
wait for 100 ns;
clk <= '1';
wait for 100 ns;
end process;

stim_proc: process
begin
sin<= "1001";
wait for 100 ns;
end process;

END;

 Piso

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity piso is
Port (
d : in STD_LOGIC_VECTOR (3 downto 0);
clk,rst,load : in STD_LOGIC;
so : out STD_LOGIC);
end piso;
architecture Behavioral of piso is
signal q : std_logic_vector (3 downto 0 );
begin
process(d,clk,rst,load)
begin
if (rst='1') then so<='0';
elsif load ='1' then q<=d;
elsif (clk' event and clk='1') then
so<= q(0);
q<= '0' & q(3 downto 1);
end if;
end process;
end Behavioral;

mp

clk_process :process
begin
clk <= '0';
wait for 100 ns;
clk <= '1';
wait for 100 ns;
end process;

stim_proc: process
begin

d<="0110";

rst<= '0';
load<='1';
wait for 100 ns;
rst<= '0';
load<='0';
wait for 100 ns;
rst<= '0';
load<='0';
wait for 100 ns;
rst<= '0';
load<='0';
wait for 100 ns;
end process;

END;

 alu
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;

entity alu is
Port ( i0,i1 : in STD_LOGIC_VECTOR (7 downto 0);
k : in STD_LOGIC_VECTOR (2 downto 0);
o : out STD_LOGIC_VECTOR (7 downto 0));
end alu;

architecture Behavioral of alu is


signal a,b,c : std_logic_vector (7 downto 0);
begin
a<= std_logic_vector ( signed(i0)+1);
b<= std_logic_vector ( signed(i0)+signed(i1) );
c<= std_logic_vector ( signed(i0)-signed(i1) );
o <= a when (k(2)='0') else
b when (k="100") else
c when (k="101") else
i0 and i1 when(k="110") else
(i0 or i1);
end Behavioral;

mp

stim_proc: process
begin
i0 <= "00001000";
i1<="01000100";
k<="000";
wait for 100 ns;
k<="100";
wait for 100 ns;
k<="101";
wait for 100 ns;
k<="110";
wait for 100 ns;
k<="111";
wait for 100 ns;
end process;
END;

 Mã hóa ưu tiên

entity nam10 is
Port ( r : in STD_LOGIC_VECTOR (3 downto 0);
code : out STD_LOGIC_VECTOR (1 downto 0);
ac : out STD_LOGIC);
end nam10;

architecture Behavioral of nam10 is

begin
process (r)
begin
case r is
when "1000"|"1001"|"1010"|"1011"|"1100"|"1101"|"1110"|"1111" =>
code <="11";
when "0100"|"0101"|"0110"|"0111" => code <="10";
when "0010"|"0011" => code<= "01";
when others => code<= "00";

end case ;
ac<=r(3) or r(2) or r(1) or r(0);
end process;
end Behavioral;

 đa hợ p

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity phatbieucodieukien is
Port ( a,b,c,d : in STD_LOGIC_VECTOR (7 downto 0);
s : in STD_LOGIC_VECTOR (1 downto 0);
x : out STD_LOGIC_VECTOR (7 downto 0));
end phatbieucodieukien;

architecture Behavioral of phatbieucodieukien is


begin

x <= a when (s="00") else


b when (s="01") else
c when (s="10") else
d;

end Behavioral;

 giải mã

Port ( s : in STD_LOGIC_VECTOR (1 downto 0);


x : out STD_LOGIC_VECTOR (3 downto 0));
end vao2ra4;

architecture Behavioral of vao2ra4 is

begin
process (s)
begin
case s is
when "00" => x<="0001";
when "01" => x<="0010";
when "10" => x<= "0100";
when others => x<= "1000
";
end case;
end process;
end Behavioral;

mp

You might also like