BỘ GIẢI MÃ LẬP TRÌNH BẰNG
VHDL & VERILOG

Ở đây mình sẽ ví dụ bộ giải mã 4-16 có một ngõ cho phép và lập trình cả bằng ngôn ngữ mô tả phần cứng HDL ( VHDL & Verilog )

Sơ đồ khối:



Hình  1: SƠ ĐỒ KHỐI MẠCH GIẢI MÃ

Bảng trạng thái:
Enable
INPUT
OUTPUT
0
XXXX
0000000000000000
1
0000
0000000000000001
1
0001
0000000000000010
1
0010
0000000000000100
1
0011
0000000000001000
1
0100
0000000000010000
1
0101
0000000000100000
1
0110
0000000001000000
1
0111
0000000010000000
1
1000
0000000100000000
1
1001
0000001000000000
1
1010
0000010000000000
1
1011
0000100000000000
1
1100
0001000000000000
1
1101
0010000000000000
1
1110
0100000000000000
1
1111
1000000000000000

Code bằng verilog

Cách 1: Dùng Case

`timescale 1ns / 1ps
module BOGIAIMA(
    input [3:0] binary_in,
    input enable,
    output [15:0] decoder_out
    );

reg [15:0] decoder_out;
always @ ( enable or binary_in )
begin
    decoder_out = 0;
    if (enable)
        begin
            case (binary_in)
            4'h0: decoder_out = 16'h0001; // 0000 0000 0000 0001
            4'h1: decoder_out = 16'h0002; // 0000 0000 0000 0010
            4'h2: decoder_out = 16'h0004; // 0000 0000 0000 0100
            4'h3: decoder_out = 16'h0008; // 0000 0000 0000 1000
            4'h4: decoder_out = 16'h0010; // 0000 0000 0001 0000
            4'h5: decoder_out = 16'h0020; // 0000 0000 0010 0000
            4'h6: decoder_out = 16'h0040; // 0000 0000 0100 0000
            4'h7: decoder_out = 16'h0080; // 0000 0000 1000 0000
            4'h8: decoder_out = 16'h0100; // 0000 0001 0000 0000
            4'h9: decoder_out = 16'h0200; // 0000 0010 0000 0000
            4'hA: decoder_out = 16'h0400; // 0000 0100 0000 0000
            4'hB: decoder_out = 16'h0800; // 0000 1000 0000 0000
            4'hC: decoder_out = 16'h1000; // 0001 0000 0000 0000
            4'hD: decoder_out = 16'h2000; // 0010 0000 0000 0000
            4'hE: decoder_out = 16'h4000; // 0100 0000 0000 0000
            4'hF: decoder_out = 16'h8000; // 1000 0000 0000 0000
            endcase
    end
end
endmodule



Cách 2: Dùng miêu tả assign

`timescale 1ns / 1ps
module DECODER_CACH2(
    input [3:0] binary_in,
    input enable,
    output [15:0] decoder_out
    );

wire [15:0] decoder_out;
assign decoder_out = (enable) ? (1 << binary_in) : 16'b0;

endmodule



Code bằng VHDL

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity DECODER4_16 is
    Port ( I : in  STD_LOGIC_VECTOR (3 downto 0);
           O : out  STD_LOGIC_VECTOR (15 downto 0);
           ENA : in  STD_LOGIC);
end DECODER4_16;

architecture Behavioral of DECODER4_16 is

begin
PROCESS ( ENA, I )
BEGIN
    IF  ENA = '1' THEN
        CASE I IS
            WHEN "0000" => O <= "0000000000000001";
            WHEN "0001" => O <= "0000000000000010";
            WHEN "0010" => O <= "0000000000000100";
            WHEN "0011" => O <= "0000000000001000";
            WHEN "0100" => O <= "0000000000010000";
            WHEN "0101" => O <= "0000000000100000";
            WHEN "0110" => O <= "0000000001000000";
            WHEN "0111" => O <= "0000000010000000";
            WHEN "1000" => O <= "0000000100000000";
            WHEN "1001" => O <= "0000001000000000";
            WHEN "1010" => O <= "0000010000000000";
            WHEN "1011" => O <= "0000100000000000";
            WHEN "1100" => O <= "0001000000000000";
            WHEN "1101" => O <= "0010000000000000";
            WHEN "1110" => O <= "0100000000000000";
            WHEN OTHERS => O <= "1000000000000000";
        END CASE;
    END IF;
END PROCESS;

end Behavioral;








Continue Reading