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


沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...