2019年12月30日 星期一

Verilog 4-to-2 Priority Encoder

Verilog 4-to-2 Priority Encoder







module priority_4to2_encoder(Y,V,En,Din);
output [1:0]Y;
output V;
input En;
    input [3:0] Din;
reg V;
    reg [1:0]Y;
    
    always @ (En or Din)
    begin
        if (En)
        begin
            casex (Din)
4'b0001:
begin V = 1'b1  ; Y = 2'b00; end
4'b001x:    
begin V = 1'b1  ; Y = 2'b01; end
4'b01xx:    
begin V = 1'b1  ; Y = 2'b10; end
4'b1xxx:    
begin V = 1'b1  ; Y = 2'b11; end
default:    
begin V = 1'b0  ; Y = 2'bzz; end
            endcase
        end
        else
begin V = 1'b0  ; Y = 2'bzz; end
    end
endmodule


// 時間單位 100ns, 時間精確度100 ps
`timescale 100ns/100ps
module Test_bench;
reg [3:0]Din = 4'b0000;   //  暫存器資料初值為‘0000’
reg En;

wire [1:0]Y;
wire V;


integer i;

priority_4to2_encoder  DUT(.Y(Y),.V(V),.En(En),.Din(Din) );
// initial程序結構區塊, 產生輸入信號波形
initial begin
    $monitor(En,Din,Y,V);
    for (i=0; i<16; i=i+1) begin
        {Din} = i;
        En=1;
        #20;
    end
    for (i=0; i<16; i=i+1) begin
        {Din} = i;
        En=0;
        #20;
    end

end

initial
begin
  #680;   // 模擬終止時間  680 ns
    $stop;
end

endmodule


沒有留言:

張貼留言

  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...