2020年2月28日 星期五

Verilog 比較器( Comparator )

Verilog 比較器( Comparator )

程式( 比較器 ):
module Comparator( In1, In2, Out );

    input   In1, In2;
    output  [2:0] Out;

    wire    In1, In2;
    reg     [2:0] Out;

    always @( In1, In2 ) begin
        Out[0] <= ( In1 > In2 );
        Out[1] <= ( In1 == In2 );
        Out[2] <= ( In1 < In2 );
    end

endmodule
修改成4bit 比較器
module Comparator( In1, In2 ,Out );

    input   [3:0] In1,In2;
    output  [2:0] Out;

    wire    [3:0]In1, In2;
    reg     [2:0] Out;

    always @( In1, In2 ) begin
        Out[0] <= ( In1 > In2 );
        Out[1] <= ( In1 == In2 );
        Out[2] <= ( In1 < In2 );
    end

endmodule
// 時間單位 100ns, 時間精確度10 ps
`timescale 100ns/10ps
module Test_bench;
//    input   [3:0] In1,In2;
//    output  [2:0] Out;

    
wire [2:0] Out; 
reg  [3:0] In1=4'b000;
reg  [3:0] In2=4'b000;

Comparator  DUT( .In1(In1), .In2(In2) ,.Out(Out));

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

$monitor(In1, In2 ,Out );
 
 #100;    // 100ns
 In1=4'b0001 ; 
 In2=4'b0010 ; 
 
 #100;    // 200ns
 In1=4'b1001 ; 
 In2=4'b0010 ; 
 
 #100;    // 300ns
 In1=4'b1011 ; 
 In2=4'b1011 ; 
 
 #100;    // 400ns
 In1=4'b0001 ; 
 In2=4'b0110 ; 
 
 end

initial
begin
  #500;   // 模擬終止時間  500 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...