Tải bản đầy đủ (.pdf) (25 trang)

de thi mon dien tu so ngon ngu phan cung vhdl truong dai hoc dien luc co dap an

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (337.21 KB, 25 trang )

Câu 1 (2,5 điểm): cho đoạn chương trình viết bằng VHDL sau:
a, Tìm lỗi và sửa lỗi bằng cách viết lại chương trình
Các lỗi được làm nổi bật thành màu vàng



library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity RSFF is
port ( R;S;RST;CLK : in STD_LOGIC; -- Dùng dấu phẩy để ngăn cách khai
báo các chân vào/ra chứ không dùng chấm phẩy
Q : out STD_LOGIC; -- Khai báo các chân vào/ra cuối cùng của
entity không dùng chấm phẩy ở đây (thừa dấu chấm phẩy)
) -- Thiếu dấu chấm phẩy ở đây
end RSFF;
--}} End of automatically maintained section
architecture RSFF of RSFF is
signal q0: std_logic;
begin
process

(CLK,RST,R,S); -- Thừa dấu chấm phẩy ở đây
variable RS:std_logic_vector (1 downto 0);

begin
RS<=R&S; -- Lệnh gán biến dùng “:=” chứ không dùng “<=”
if (RST=="1") then -- Thừa một dấu bằng, nháy đơn chứ không dùng nháy kép
q0<='0';
else if (CLK'event and CLK='1') then
case RS is


1


when "00"=>q0=>q0; -- Dùng lệnh gán biến “<=” chứ không dùng
“=>”
when "01"=>q0=>'1'; ; -- Dùng lệnh gán biến “<=” chứ không dùng
“=>”
when "10"=>q0=>'0'; -- Dùng lệnh gán biến “<=” chứ không dùng
“=>”
when others=>q0=>'X'; -- Dùng lệnh gán biến “<=” chứ không dùng
“=>”
end case;
end if; end if;
end process;
Q=q0; -- Dùng lệnh gán biến “<=” chứ không dùng “=”
end RSFF;


Chương trình sau khi sửa lại ( các lỗi đã sửa được làm nổi bật thành màu
đỏ)

library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity RSFF is
port ( R,S,RST,CLK : in STD_LOGIC;
Q : out STD_LOGIC
);
end RSFF;
--}} End of automatically maintained section
architecture RSFF of RSFF is

signal q0: std_logic;

2


begin
process

(CLK,RST,R,S)
variable RS:std_logic_vector (1 downto 0);

begin
RS:=R&S;
if (RST=’1’) then
q0<='0';
else if (CLK'event and CLK='1') then
case RS is
when "00"=>q0<=q0;
when "01"=>q0<='1';
when "10"=>q0<='0';
when others=>q0<='X';
end case;
end if; end if;
end process;
Q<=q0;
end RSFF;
b, Đoạn mã đúng này mô tả phần tử số là Flip-Flop RS không đồng bộ (bất đồng bộ)
tín hiệu Reset, mô hình phần tử số đó là:

3



Hình 1: Mô hình RSFF và bảng sự thật
Câu 2 (2,5 điểm): Viết mã VHDL mô tả mạch như hình bên dưới:

Hình 2: Mạch logic số

4




Chương trình VHDL:

------------ Mo ta hoat dong cua component JKFF-------------library ieee;
use ieee.std_logic_1164.all;
entity JKFF is
port (J, K,CLK,RST: in std_logic;
y,ynot

:

out std_logic

);
end JKFF;
architecture JKFF of JKFF is
signal state: std_logic;
begin
process(CLK, RST)

variable JK: std_logic_vector(1 downto 0);
begin
JK:=J&K;
if (RST='1') then
state <= '0';
elsif (CLK'event and CLK='1') then
case JK is
when "11" => state <= not state;
when "10" => state <= '1';


when "01" => state <= '0';
when others => null;
end case;
end if;
end process;
y <= state;
ynot <= not state;
end JKFF;
------------ Mo ta hoat dong cua component NOT--------------library ieee;
use ieee.std_logic_1164.all;
entity inverter is
port ( x: in std_logic;
y: out std_logic
);
end inverter;
architecture inverter of inverter is
begin
y <= NOT x;
end inverter;

------------ Mo ta hoat dong cua component OR--------------library ieee;
use ieee.std_logic_1164.all;
entity OR_2 is


port ( x,y: in std_logic;
z: out std_logic
);
end OR_2;
architecture OR_2 of OR_2 is
begin
z <= x OR y;
end OR_2;
------------ Mo ta hoat dong cua component NOR--------------library ieee;
use ieee.std_logic_1164.all;
entity NOR_2 is
port ( x,y: in std_logic;
z: out std_logic
);
end NOR_2;
architecture NOR_2 of NOR_2 is
begin
z <= x NOR y;
end NOR_2;
------------ Mo ta hoat dong cua component AND--------------library ieee;
use ieee.std_logic_1164.all;
entity AND_2 is


port ( x,y: in std_logic;

z: out std_logic
);
end AND_2;
architecture AND_2 of AND_2 is
begin
z <= x AND y;
end AND_2;
------------ Chuong trinh chinh--------------library ieee; -- khai báo thư viện
use ieee.std_logic_1164.all;
entity bai_2 is
port ( x1,x2,CLK,RST: in std_logic; -- Khai báo các chân vào
z: out std_logic -- Khai báo các chân ra
);
end bai_2;
architecture bai_2 of bai_2 is
--------------khoi JKFF---------------component JKFF

-- Khai báo khối component JKFF cấp thấp

port( J,K,CLK,RST: in std_logic;
y,ynot

: out std_logic

);
end component;
--------------cong NOT----------------


component inverter -- Khai báo khối component cổng NOT cấp thấp

port( x: in std_logic;
y: out std_logic
);
end component;
--------------cong OR_2---------------component OR_2

-- Khai báo khối component cổng OR_2 cấp thấp

port( x,y: in std_logic;
z: out std_logic
);
end component;
--------------cong NOR_2---------------component NOR_2

-- Khai báo khối component cổng NOR_2 cấp thấp

port( x,y: in std_logic;
z: out std_logic
);
end component;
--------------cong AND_2---------------component AND_2

-- Khai báo khối component cổng AND_2 cấp thấp

port( x,y: in std_logic;
z: out std_logic
);
end component;



signal S1,S2,S3,S4,S5,S6,S7,S8: std_logic; -- Khai báo các tín hiệu để lưu trữ tạm thời
các giá trị trung gian (xem hình bên dưới)
begin
Cong_not: inverter port map(x1,S1);

-- Kết nối các khối component cấp thấp lại

với nhau port_name => signal_name
Cong_OR : OR_2 port map(x2,S8,S2);
JKFF_1 : JKFF port map(S1,S2,CLK,RST,S3,S4);
Cong_NOR: NOR_2 port map(S1,S3,S5);
JKFF_2 : JKFF port map(S5,S6,CLK,RST,S7,S8);
Cong_AND: AND_2 port map(S7,S4,z);
end bai_2;

Hình 3: Các tín hiệu tạm (Temp) S0 ÷ S8
Câu 3 (5 điểm): Vẽ sơ đồ chuyển trạng thái, sơ đồ khối, viết mô tả VHDL, cho
OTOMAT có nhớ với một đầu vào X và một đầu ra Z hoạt động theo yêu cầu sau:
-

Z=1
Z =

nếu như gặp dãy số vào là 0100
0
trong

mọi

trường


hợp

khác

 Phân tích bài toán ( Phần này không viết vào bài thi, chỉ để đọc hiểu vận

dụng để làm những bài khác):


-

Theo bài ra thì đây là thiết kế máy trạng thái hữu hạn FSM, ở đây đầu ra Z chỉ phụ
thuộc vào trạng thái hiện tại nên là FSM kiểu MOORE.

-

Theo bài ra ta xây dựng được bảng trạng thái ngõ ra theo ngõ vào:

Trạng thái hiện tại
(pr_state)
S0
S1
S2
S3
S4

Trạng thái tiếp theo
(nx_state)
X=0


X=1

S1
S1
S3
S4
S1

S0
S2
S0
S2
S2

Trạng thái S0 là trạng thái chờ 0 đầu tiên.
Trạng thái S1 là trạng thái đã có 0 và chờ 1.
Trạng thái S2 là trạng thái đã có 01 và đang chờ 0 thứ 2.
Trạng thái S3 là trạng thái đã có 010 và chờ 0 thứ 3.
Trạng thái S4 là trạng thái thu được chuỗi 0100.

Ngõ ra
(Z)
0
0
0
0
1



 Phần bài làm (viết vào bài thi):
-

-

Vẽ sơ đồ chuyển trạng thái:

Sơ đồ khối mạch:

-

Viết mã

VHDL mô tả mạch:

-------------------------------------------------------------------- phat hien chuoi "0100" FSM VHDL
-- by Khanh Hung, 08/06/2014
--------------------Khai bao thu vien-----------------------------library ieee;
use ieee.std_logic_1164.all;
--------------------Khai bao thuc the-----------------------------entity FSM is


port (
X :in std_logic; -- Input X
CLK :in std_logic; -- Input clock
RST :in std_logic; -- Input reset
Z :out std_logic -- Output
);
end FSM;
------------------------------------------------------------------architecture FSM of FSM is

type state is (S0,S1,S2,S3,S4); -- Khai báo state là dữ liệu kiểu liệt kê các trạng
thái
signal pr_state, nx_state: state; --Pr_state(trang thai hien tai), nx_state(trang thai
tiep theo)
begin
--------------------Phan mach tuan tu ben duoi--------------------process(RST,CLK)
begin
if (RST='1') then
pr_state <= S0;
elsif ( CLK'event and CLK='1') then -- Nếu có xung clock mức cạnh
lên thì
pr_state <= nx_state; -- Trạng thái hiện tại được gán bằng trạng
thái tiếp theo
end if;
end process;
--------------------Phan mach to hop ben duoi---------------------process(X,pr_state) -- Danh sách cảm nhận là trạng thái hiện tại và đầu vào X
begin
case pr_state is -- Với trạng thái hiện tại là:
when S0 => -- Khi trạng thái hiện tại là S0
Z <= '0'; -- Đầu ra Z=0
if(X='0') then nx_state <= S1; -- Nếu đầu vào X=0 thì trạng
thái tiếp theo là S1
else nx_state <= S0; -- Nếu đầu vào X=1 thì trạng thái tiếp
theo là S0
end if;
when S1 => -- Khi trạng thái hiện tại là S1
Z <= '0'; -- Đầu ra Z=0
if(X='1') then nx_state <= S2; -- Nếu đầu vào X=1 thì trạng
thái tiếp theo là S2
else nx_state <= S1; -- Nếu đầu vào X=0 thì trạng thái tiếp

theo là S1


end if;
when S2 => -- Khi trạng thái hiện tại là S2
Z <= '0'; -- Đầu ra Z=0
if(X='0') then nx_state <= S3; -- Nếu đầu vào X=0 thì trạng
thái tiếp theo là S3
else nx_state <= S0; -- Nếu đầu vào X=1 thì trạng thái tiếp
theo là S0
end if;
when S3 => -- Khi trạng thái hiện tại là S3
Z <= '0'; -- Đầu ra Z=0
if(X='0') then nx_state <= S4; -- Nếu đầu vào X=0 thì trạng
thái tiếp theo là S4
else nx_state <= S2; -- Nếu đầu vào X=1 thì trạng thái tiếp
theo là S2
when S4 =>

end if;
-- Khi trạng thái hiện tại là S4
Z <= '1'; -- Đầu ra Z=1
if(X='0') then nx_state <= S1; -- Nếu đầu vào X=0 thì

trạng thái tiếp theo là S1
else nx_state <= S2; -- Nếu đầu vào X=1 thì trạng thái tiếp
theo là S2
end if;
end case;
end process;

end FSM;
THE END


Mã Code

Bài 1
library IEEE;
use IEEE.STD_LOGIC_1164.all;

entity RSFF is
port ( R,S,RST,CLK : in STD_LOGIC;Q : out STD_LOGIC

);
end RSFF;
--}}
End of automatically maintained section
architecture RSFF of RSFF is
signal q0: std_logic;

begin

process

(CLK,RST,R,S)

variable RS:std_logic_vector (1 downto 0);

begin
RS:=R&S;

if (RST='1') then
q0<='0';
else if (CLK'event and CLK='1') then
case RS is


when "00"=>q0<=q0;
when "01"=>q0<='1';

when "10"=>q0<='0';
when others=>q0<='X';

end case;

end if;
end if;

end process;

Q<=q0;
end RSFF;
Bài 2
------------ Mo ta hoat dong cua component JKFF-------------library ieee;
use ieee.std_logic_1164.all;
entity JKFF is
port (J, K,CLK,RST: in std_logic;
y,ynot

:


out std_logic

);
end JKFF;
architecture JKFF of JKFF is
signal state: std_logic;
begin


process(CLK, RST)
variable JK: std_logic_vector(1 downto 0);
begin
JK:=J&K;
if (RST='1') then
state <= '0';
elsif (CLK'event and CLK='1') then
case JK is
when "11" => state <= not state;
when "10" => state <= '1';
when "01" => state <= '0';
when others => null;
end case;
end if;
end process;
y <= state;
ynot <= not state;
end JKFF;
------------ Mo ta hoat dong cua component NOT--------------library ieee;
use ieee.std_logic_1164.all;
entity inverter is

port ( x: in std_logic;
y: out std_logic
);
end inverter;
architecture inverter of inverter is
begin


y <= NOT x;
end inverter;
------------ Mo ta hoat dong cua component OR--------------library ieee;
use ieee.std_logic_1164.all;
entity OR_2 is
port ( x,y: in std_logic;
z: out std_logic
);
end OR_2;
architecture OR_2 of OR_2 is
begin
z <= x OR y;
end OR_2;
------------ Mo ta hoat dong cua component NOR--------------library ieee;
use ieee.std_logic_1164.all;
entity NOR_2 is
port ( x,y: in std_logic;
z: out std_logic
);
end NOR_2;
architecture NOR_2 of NOR_2 is
begin

z <= x NOR y;
end NOR_2;
------------ Mo ta hoat dong cua component AND--------------library ieee;


use ieee.std_logic_1164.all;
entity AND_2 is
port ( x,y: in std_logic;
z: out std_logic
);
end AND_2;

architecture AND_2 of AND_2 is
begin
z <= x AND y;
end AND_2;
------------ Chuong trinh chinh--------------library ieee;
use ieee.std_logic_1164.all;
entity bai_2 is
port ( x1,x2,CLK,RST: in std_logic;
z: out std_logic
);
end bai_2;
architecture bai_2 of bai_2 is
component JKFF
port( J,K,CLK,RST: in std_logic;
y,ynot

: out std_logic


);
end component;
--------------------------------------component inverter
port( x: in std_logic;


y: out std_logic
);
end component;
--------------------------------------component OR_2
port( x,y: in std_logic;
z: out std_logic
);
end component;
--------------------------------------component NOR_2
port( x,y: in std_logic;
z: out std_logic
);
end component;
--------------------------------------component AND_2
port( x,y: in std_logic;
z: out std_logic
);
end component;
--------------------------------------signal S1,S2,S3,S4,S5,S6,S7,S8: std_logic;
begin
Cong_not: inverter port map(x1,S1);
Cong_OR : OR_2 port map(x2,S8,S2);
JKFF_1 : JKFF port map(S1,S2,CLK,RST,S3,S4);
Cong_NOR: NOR_2 port map(S1,S3,S5);



JKFF_2 : JKFF port map(S5,S6,CLK,RST,S7,S8);
Cong_AND: AND_2 port map(S7,S4,z);
end bai_2;

Cách 2
library ieee; -- khai báo thư viện
use ieee.std_logic_1164.all;
entity bai_2 is
port ( x1,x2,CLK,RST: in std_logic; -- Khai báo các chân vào
z: out std_logic -- Khai báo các chân ra
);
end bai_2;
architecture bai_2 of bai_2 is
--------------khoi JKFF---------------component JKFF is

-- Khai báo khối component JKFF cấp thấp

port( J,K,CLK,RST: in std_logic;
y,ynot

: out std_logic

);
end component;
--------------cong NOT---------------component inverter is -- Khai báo khối component cổng NOT cấp thấp
port( x: in std_logic;
y: out std_logic
);

end component;
--------------cong OR_2---------------component OR_2 is

-- Khai báo khối component cổng OR_2 cấp thấp

port( x,y: in std_logic;


z: out std_logic
);
end component;
--------------cong NOR_2---------------component NOR_2 is

-- Khai báo khối component cổng NOR_2 cấp thấp

port( x,y: in std_logic;
z: out std_logic
);
end component;
--------------cong AND_2---------------component AND_2 is

-- Khai báo khối component cổng AND_2 cấp thấp

port( x,y: in std_logic;
z: out std_logic
);
end component;
signal S1,S2,S3,S4,S5,S6,S7,S8: std_logic; -- Khai báo các tín hiệu để lưu trữ tạm thời
các giá trị trung gian
begin

Cong_not: inverter port map(x1,S1);
với nhau port_name => signal_name

-- Kết nối các khối component cấp thấp lại

Cong_OR : OR_2 port map(x2,S8,S2);
JKFF_1 : JKFF port map(S1,S2,CLK,RST,S3,S4);
Cong_NOR: NOR_2 port map(S1,S3,S5);
JKFF_2 : JKFF port map(S5,S6,CLK,RST,S7,S8);
Cong_AND: AND_2 port map(S7,S4,z);
end bai_2;
Bài 3
-------------------------------------------------------------------- phat hien chuoi "0100" FSM VHDL


--------------------Khai bao thu vien-----------------------------library ieee;
use ieee.std_logic_1164.all;
--------------------Khai bao thuc the-----------------------------entity FSM is
port (
X :in std_logic;

-- Input X

CLK :in std_logic; -- Input clock
RST :in std_logic;

-- Input reset

Z :out std_logic -- Output
);

end FSM;
------------------------------------------------------------------architecture FSM of FSM is
type state is (S0,S1,S2,S3,S4); -- Khai báo state là dữ liệu kiểu liệt kê các trạng
thái
signal pr_state, nx_state: state; --Pr_state(trang thai hien tai), nx_state(trang thai
tiep theo)
begin
--------------------Phan mach tuan tu ben duoi--------------------process(RST,CLK)
begin
if (RST='1') then
pr_state <= S0;
elsif ( CLK'event and CLK='1') then

-- Nếu có xung clock mức cạnh

lên thì
pr_state <= nx_state; -- Trạng thái hiện tại được gán bằng trạng
thái tiếp theo
end if;
end process;


--------------------Phan mach to hop ben duoi---------------------process(X,pr_state) -- Danh sách cảm nhận là trạng thái hiện tại và đầu vào X
begin
case pr_state is -- Với trạng thái hiện tại là:
when S0 =>

-- Khi trạng thái hiện tại là S0

Z <= '0'; -- Đầu ra Z=0

if(X='0') then nx_state <= S1; -- Nếu đầu vào X=0 thì trạng
thái tiếp theo là S1
else nx_state <= S0; -- Nếu đầu vào X=1 thì trạng thái tiếp
theo là S0
end if;
when S1 =>

-- Khi trạng thái hiện tại là S1

Z <= '0'; -- Đầu ra Z=0
if(X='1') then nx_state <= S2; -- Nếu đầu vào X=1 thì trạng
thái tiếp theo là S2
else nx_state <= S1; -- Nếu đầu vào X=0 thì trạng thái tiếp
theo là S1
end if;
when S2 =>

-- Khi trạng thái hiện tại là S2

Z <= '0'; -- Đầu ra Z=0
if(X='0') then nx_state <= S3; -- Nếu đầu vào X=0 thì trạng
thái tiếp theo là S3
else nx_state <= S0; -- Nếu đầu vào X=1 thì trạng thái tiếp
theo là S0
end if;
when S3 =>

-- Khi trạng thái hiện tại là S3

Z <= '0'; -- Đầu ra Z=0

if(X='0') then nx_state <= S4; -- Nếu đầu vào X=0 thì trạng
thái tiếp theo là S4
else nx_state <= S2; -- Nếu đầu vào X=1 thì trạng thái tiếp
theo là S2


end if;
when S4 =>

-- Khi trạng thái hiện tại là S4
Z <= '1'; -- Đầu ra Z=1
if(X='0') then nx_state <= S1; -- Nếu đầu vào X=0 thì

trạng thái tiếp theo là S1
else nx_state <= S2; -- Nếu đầu vào X=1 thì trạng thái tiếp
theo là S2
end if;
end case;
end process;
end FSM;


×