2020年2月27日 星期四

Verilog 8x3 編碼器( Encode )

Verilog  8x3 編碼器( Encode ) 

程式( 8 to 3編碼器):
module EnCoder( In, Out );

    input   [7:0] In;
    output  [2:0] Out;

    wire    [7:0] In;
    reg        [2:0] Out;

    always @( In ) begin
        case( In )
            8'b0000_0001:   Out <= 3'b000;
            8'b0000_0010:   Out <= 3'b001;
            8'b0000_0100:   Out <= 3'b010;
            8'b0000_1000:   Out <= 3'b011;
            8'b0001_0000:   Out <= 3'b100;
            8'b0010_0000:   Out <= 3'b101;
            8'b0100_0000:   Out <= 3'b110;
            8'b1000_0000:   Out <= 3'b111;
            default:        Out <= 3'bxxx;
        endcase
    end

endmodule

//  編碼器( Encode )
//程式( 8 to 3編碼器):
//======================================
module encoder_8x3(In, Out );

    input   [7:0] In;
    output  [2:0] Out;

    wire    [7:0] In;
    reg     [2:0] Out;

    always @( In ) begin
        case( In )
            8'b0000_0001:   Out <= 3'b000;
            8'b0000_0010:   Out <= 3'b001;
            8'b0000_0100:   Out <= 3'b010;
            8'b0000_1000:   Out <= 3'b011;
            8'b0001_0000:   Out <= 3'b100;
            8'b0010_0000:   Out <= 3'b101;
            8'b0100_0000:   Out <= 3'b110;
            8'b1000_0000:   Out <= 3'b111;
            default:        Out <= 3'bzzz;
        endcase
    end

endmodule

//======================================
// 時間單位 100ns, 時間精確度10 ps
`timescale 100ns/10ps
module Test_bench;
//    input   [7:0] In;
//    output  [2:0] Out;

    
wire [2:0] Out; 

reg  [7:0] In=8'b0000_0000;

encoder_8x3 DUT( .In(In), .Out(Out) );

// initial程序結構區塊, 產生A、B輸入信號波形
initial begin

$monitor(In, Out );
 
 #100;    // 100ns
 In=8'b0000_0001 ; 
 
 #100;    // 200ns
 In=8'b0000_0010 ; 

 #100;    // 300ns
 In=8'b0000_0100 ; 
 
 #100;    // 400ns
 In=8'b0000_1000 ; 
 
 #100;    // 500ns
 In=8'b0001_0000 ; 
 
 #100;    // 600ns
 In=8'b0010_0000 ; 
 
 #100;    // 700ns
 In=8'b0100_0000 ; 
  
 #100;    // 800ns
 In=8'b1000_0000 ; 
 
 #100;    // 900ns
 In=8'b1100_0010 ; 
 
 end

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

endmodule
//======================================


沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...