Bài tập giải sẵn về VHDL môn Kỹ thuật số - Hồ Trung Mỹ

Có nhiều cách giải cho phần này.

Từ bảng chân trị ta có biểu thức Boole cho các ngõ ra:

C1 = A1A0 + A0A2 + A1A2 + A0A1A2

C1 = A1A0 + A0A2 + A1A2 => cần AND 2 ngõ vào và OR 3 ngõ vào

C0 = A2’A1’A0 + A2’A1A0’ + A2A1’A0’ + A2A1A0

C0 = ((A2’A1’A0)’.( A2’A1A0’)’ .(A2A1’A0’)’.(A2A1A0)’)’

=> Cần NAND 3 ngõ vào, NAND 4 ngõ vào và cổng NOT

Mạch cho C1 được đặt tên là MAJ3 và mạch cho C0 được đặt tên là OPAR3.

pdf32 trang | Chuyên mục: Kỹ Thuật Số | Chia sẻ: tuando | Lượt xem: 471 | Lượt tải: 0download
Tóm tắt nội dung Bài tập giải sẵn về VHDL môn Kỹ thuật số - Hồ Trung Mỹ, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
 trình đặc tính cho các FF và ngõ ra Z : 
D1 = Q1+ = Q1’ + Q2 ; D2 = Q2+ = XQ2’ ; Z = Q1 + Q2’ 
Suy ra bảng chuyển trạng thái sau: 
PS 
Q1Q2 
NS (Q1+Q2+) Z X = 0 X =1 
00 10 01 1 
01 10 10 0 
11 00 10 1 
10 00 01 1 
b) Mã VHDL cho câu a): 
library ieee; 
use ieee.std_logic_1164.all; 
entity Q10_1 is 
port( X, CLK, reset_n: in std_logic; 
 Z: out std_logic); 
end Q10_1; 
architecture bg of Q10_1 is 
 signal state: std_logic_vector( 1 downto 0); 
 signal state_X: std_logic_vector( 2 downto 0); 
begin 
state_X <= state & X; 
Z <= '0' when state = "01" else '1'; 
process(CLK, reset_n) 
begin 
 if (reset_n = '0') then 
 state <= "00"; 
 elsif rising_edge(CLK) then 
 case state_X is 
 when "000" => state <= "10"; 
 when "001" => state <= "01"; 
Các BT giải sẵn về VHDL 2011 – trang 24 
 when "010" | "011" => state <= "10"; 
 when "110" => state <= "00"; 
 when "111" => state <= "10"; 
 when "100" => state <= "00"; 
 when "101" => state <= "01"; 
 when others => null; 
 end case; 
 end if; 
end process; 
end bg; 
Dạng sóng mô phỏng: 
Chú ý: 
Có nhiều cách viết khác để mô tả FSM, thí dụ sau đây là 1 cách viết khác: 
library ieee; 
use ieee.std_logic_1164.all; 
entity Q10_2 is 
port( 
 X, CLK, reset_n: in std_logic; 
 Z: out std_logic); 
end Q10_2; 
architecture bg of Q10_2 is 
 signal Present_state: std_logic_vector( 1 downto 0); -- 
PS 
 signal Next_state: std_logic_vector( 1 downto 0); -- 
NS 
begin 
Z <= '0' when Present_state = "01" else '1'; 
State_transition: 
process(CLK, reset_n) 
begin 
 if (reset_n = '0') then 
 Present_state <= "00"; 
 elsif rising_edge(CLK) then 
 Present_state <= Next_state ; 
 end if; 
end process; 
Các BT giải sẵn về VHDL 2011 – trang 25 
Find_Next_state: 
process(Present_state) 
begin 
 case Present_state is 
 when "00" => if X = '0' then 
 Next_state <= "10"; 
 else 
 Next_state <= "01"; 
 end if; 
 when "01" => Next_state <= "10"; 
 when "11" => if X = '0' then 
 Next_state <= "00"; 
 else 
 Next_state <= "10"; 
 end if; 
 when "10" => if X = '0' then 
 Next_state <= "00"; 
 else 
 Next_state <= "01"; 
 end if; 
 when others => null; 
 end case; 
end process; 
end bg; 
Dạng sóng mô phỏng: 
11. Thiết kế mạch phát hiện chuỗi bit vào nối tiếp có trị là "101". Viết mã VHDL với: 
a) Dùng FSM loại Mealy với mô tả FSM. 
b) FSM loại Mealy dùng thanh ghi dịch chứa 3 bit liên tiếp và so sánh với "101". 
Bài giải. 
a) FSM loại Mealy 
Ta có được giản đồ trạng thái và bảng chuyển trạng thái như sau (kết quả lấy từ bài 
giảng thiết kế hệ tuần tự đồng bộ) 
Các BT giải sẵn về VHDL 2011 – trang 26 
Ta định nghĩa thêm kiểu mới cho các trạng thái  không cần gán trạng thái mà CAD sẽ 
tự gán trị cho nó. 
library ieee; 
use ieee.std_logic_1164.all; 
entity Q11_1 is 
port( X, CLK, reset_n: in std_logic; 
 Z: out std_logic); 
end Q11_1; 
architecture bg of Q11_1 is 
 type state_type is (S0, S1, S2); 
 signal state: state_type; 
begin 
Z <= '1' when (state = S2 and X = '1') else '0'; 
process(CLK, reset_n) 
begin 
 if (reset_n = '0') then 
 state <= S0; 
 elsif rising_edge(CLK) then 
 case state is 
 when S0 => if X = '1' then state <= S1; end if; 
 when S1 => if X = '0' then state <= S2; end if; 
 when S2 => if X = '0' then state <= S0; 
 else state <= S1; end if; 
 when others => null; 
 end case; 
 end if; 
end process; 
end bg; 
Các BT giải sẵn về VHDL 2011 – trang 27 
b) Thanh ghi dịch 
library ieee; 
use ieee.std_logic_1164.all; 
entity Q11_2 is 
port( X, CLK, reset_n: in std_logic; 
 Z: out std_logic); 
end Q11_2; 
architecture bg of Q11_2 is 
 signal pattern: std_logic_vector(2 downto 0); 
begin 
Z <= '1' when (pattern = "101" and X = ‘1’) else '0'; 
process(CLK, reset_n) 
begin 
 if (reset_n = '0') then 
 pattern '0') ; 
 elsif rising_edge(CLK) then 
 pattern <= pattern(1 downto 0) & X; 
 end if; 
end process; 
end bg; 
12. Thiết kế mạch giải mã 3 sang 8 và mạch mã hóa ưu tiên 8 sang 3 (ưu tiên ngõ vào có trọng 
số thấp nhất khi có nhiều bit vào là 1). 
 Mạch giải mã có các ngõ vào là C, B, và A (LSB) và ra là Y. 
 Mạch mã hóa có ngõ vào 8 bit D_in và ngõ ra 3 bit D_out. 
Bài giải. 
library ieee; 
use ieee.std_logic_1164.all; 
use ieee.std_logic_arith.all; 
-- Dung cho ham conv_std_logic_vector(integer, number of bits) 
use ieee.std_logic_unsigned.all; 
-- Dung cho ham conv_integer(std_logic_vector) 
entity Q12_1 is 
port( C, B, A: in std_logic; -- A: LSB 
 Y: out std_logic_vector(0 to 7); 
 D_in: in std_logic_vector(0 to 7); 
 D_out: out std_logic_vector(0 to 2)); 
end Q12_1; 
Các BT giải sẵn về VHDL 2011 – trang 28 
architecture bg of Q12_1 is 
 signal CBA: std_logic_vector(0 to 2); 
 signal CBA_int: integer range 0 to 7; 
begin 
-- Decoder 3 to 8 
CBA <= C & B & A; 
CBA_int <= conv_integer(CBA); 
process (CBA_int) 
 variable Y_int: std_logic_vector(0 to 7); 
begin 
 Y_int := (others => '0'); 
 Y_int(CBA_int) := '1'; 
 Y <= Y_int; 
end process; 
-- 
-- Priority Encoder 8 to 3 
process(D_in) 
 variable index_in: integer; 
begin 
 for i in D_in'length -1 downto 0 loop 
 if D_in(i) = '1' then 
 index_in := i; 
 end if; 
 end loop; 
 D_out <= conv_std_logic_vector(index_in,3); 
end process; 
end bg; 
Dạng sóng mô phỏng của mạch giải mã: 
Dạng sóng mô phỏng của mạch mã hóa: 
Các BT giải sẵn về VHDL 2011 – trang 29 
13. Thiết kế bộ đếm lên 3 bit loại nối tiếp (còn gọi là bộ đếm gợn hay bất đồng bộ) với xung 
nhịp vào CLK (kích cạnh). Mạch có ngõ reset tích cực thấp reset_n. Hãy viết mã VHDL 
với 
a) Mô hình cấu trúc với component DFF có sẵn. 
b) Các lệnh tuần tự. 
Bài giải. 
a) Dùng component DFF có sẵn của Maxplus II 
library ieee; 
use ieee.std_logic_1164.all; 
entity Q13_1 is 
port( CLK, reset_n: in std_logic; 
 Q2, Q1, Q0: out std_logic); -- Q0: LSB 
end Q13_1; 
architecture bg of Q13_1 is 
COMPONENT DFF 
 PORT (d : IN STD_LOGIC; 
 clk : IN STD_LOGIC; 
 clrn: IN STD_LOGIC; 
 prn : IN STD_LOGIC; 
 q : OUT STD_LOGIC ); 
END COMPONENT; 
-- Inputs | Output 
--prn clrn CLK D | Q 
-- L H X X | H 
-- H L X X | L 
-- L L X X | Illegal 
-- H H  L | L 
-- H H  H | H 
-- H H L X | Qo* 
-- H H H X | Qo 
-- * Qo = level of Q before Clock pulse 
-- All flipflops are positive-edge-triggered. 
 signal D0, D1, D2, prn: std_logic; 
 signal Q2_int, Q1_int, Q0_int: std_logic; 
begin 
U1: DFF port map(D0, CLK, reset_n, prn, Q0_int); 
U2: DFF port map(D1, D0, reset_n, prn, Q1_int); 
U3: DFF port map(D2, D1, reset_n, prn, Q2_int); 
prn <= '1'; 
D0 <= not Q0_int; D1 <= not Q1_int; D2 <= not Q2_int; 
Q0 <= Q0_int; Q1 <= Q1_int; Q2 <= Q2_int; 
end bg; 
Các BT giải sẵn về VHDL 2011 – trang 30 
c) Các lệnh tuần tự: 
library ieee; 
use ieee.std_logic_1164.all; 
entity Q13_2 is 
port( CLK, reset_n: in std_logic; 
 Q2, Q1, Q0: out std_logic); -- Q0: LSB 
end Q13_2; 
architecture bg of Q13_2 is 
 signal Q2_int, Q1_int, Q0_int: std_logic; 
begin 
process(CLK, reset_n) 
begin 
if reset_n = '0' then 
 Q0_int <= '0'; 
elsif rising_edge(CLK) then 
 Q0_int <= not Q0_int; 
end if; 
end process; 
process(Q0_int) 
begin 
if reset_n = '0' then 
 Q1_int <= '0'; 
elsif falling_edge(Q0_int) then 
 Q1_int <= not Q1_int; 
end if; 
end process; 
process(Q1_int) 
begin 
if reset_n = '0' then 
 Q2_int <= '0'; 
elsif falling_edge(Q1_int) then 
 Q2_int <= not Q2_int; 
end if; 
end process; 
Q0 <= Q0_int; Q1 <= Q1_int; Q2 <= Q2_int; 
end bg; 
Các BT giải sẵn về VHDL 2011 – trang 31 
Chú ý: Ta có thể khai báo buffer cho các đối tượng ra Q2, Q1, và Q0, khi đó không cần dùng 
các tín hiệu Q2_int, Q1_int, Q0_int. 
14. Thiết kế bộ đếm lên 3 bit loại song song (còn gọi là bộ đếm đồng bộ) với xung nhịp vào 
CLK (kích cạnh). Mạch có ngõ reset tích cực thấp reset_n. Hãy viết mã VHDL với 
a) Mô hình cấu trúc với component JKFF có sẵn. 
b) Các lệnh tuần tự. 
c) Với dãy đếm 1, 3, 5, 7, 1, . . 
Bài giải. 
a) Mô hình cấu trúc với component JKFF có sẵn: 
library ieee; 
use ieee.std_logic_1164.all; 
entity Q14_1 is 
port( CLK, reset_n: in std_logic; 
 Q2, Q1, Q0: buffer std_logic); -- Q0: LSB 
end Q14_1; 
architecture bg of Q14_1 is 
COMPONENT JKFF 
 PORT (j : IN STD_LOGIC; 
 k : IN STD_LOGIC; 
 clk : IN STD_LOGIC; 
 clrn: IN STD_LOGIC; 
 prn : IN STD_LOGIC; 
 q : OUT STD_LOGIC); 
END COMPONENT; 
-- Inputs | Output 
-- PRN CLRN CLK J K | Q 
-- L H X X X | H 
-- H L X X X | L 
-- L L X X X | Illegal 
-- H H L X X | Qo* 
-- H H  L L | Qo* 
-- H H  H L | H 
-- H H  L H | L 
-- H H  H H | Toggle 
-- * Qo = level of Q before Clock pulse 
-- All flipflops are positive-edge-triggered. 
 signal J0, J1, J2, prn: std_logic; 
begin 
U1: JKFF port map(J0, J0, CLK, reset_n, prn, Q0); 
U2: JKFF port map(J1, J1, CLK, reset_n, prn, Q1); 
U3: JKFF port map(J2, J2, CLK, reset_n, prn, Q2); 
prn <= '1'; J0 <= '1'; J1 <= Q0; J2 <= Q1 and Q0; 
end bg; 
Các BT giải sẵn về VHDL 2011 – trang 32 
b) Các lệnh tuần tự: 
library ieee; 
use ieee.std_logic_1164.all; 
use ieee.std_logic_unsigned.all; -- De tinh cong so nhi phan 
voi so nguyen 
entity Q14_2 is 
port( CLK, reset_n: in std_logic; 
 Q2, Q1, Q0: out std_logic); -- Q0: LSB 
end Q14_2; 
architecture bg of Q14_2 is 
 signal Q: std_logic_vector(2 downto 0); 
begin 
process(CLK) 
begin 
if reset_n ='0' then 
 Q <= "000"; 
elsif rising_edge(CLK) then 
 Q <= Q + 1; 
end if; 
end process; 
Q2 <= Q(2); Q1 <= Q(1); Q0 <= Q(0); 
end bg; 
c) Với dãy đếm 1, 3, 5, 7, 1, . . 
Chỉ cần sửa lại trong phần process của b) như sau: 
if reset_n ='0' then 
 Q <= "001"; 
elsif rising_edge(CLK) then 
 if Q = "111" then 
 Q <= "001"; 
 else 
 Q <= Q + 2; 
 end if; 
end if; 

File đính kèm:

  • pdfbai_tap_giai_san_ve_vhdl_mon_ky_thuat_so_ho_trung_my.pdf