CLK, reset_n: in std_logic;

Một phần của tài liệu Bài tập giải sẵn về VHDL (Trang 24 - 32)

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;

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 gii.

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ộ)

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;

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 <= (others => '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 gii.

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;

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:

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 gii.

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á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;

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 gii.

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;

Một phần của tài liệu Bài tập giải sẵn về VHDL (Trang 24 - 32)

Tải bản đầy đủ (PDF)

(32 trang)