2020年4月22日 星期三

Blocking Procedural Assignment in Verilog

Blocking Procedural Assignment in Verilog

//--------------------------------------------------
//4-bit register for Blocking Procedural Assignment
//--------------------------------------------------
module Blocking( CLK, RESET, Din,Qout);

input CLK, RESET;
input Din;

output reg [3:0] Qout;

always @ (posedge CLK or posedge RESET)
//Positive edge CLK and asynchronous RESET
 if (RESET)
   Qout = 4'b0000;
 else
  begin
   Qout[0] = Din;
   Qout[1] = Qout[0];
   Qout[2] = Qout[1];
   Qout[3] = Qout[2];
  end
endmodule


// 時間單位 1ns, 時間精確度10 ps
`timescale 10ns/10ps 
module TB;
/*
module Blocking( CLK, RESET, Din,Qout);
input CLK, RESET;
input Din;
*/

// Inputs
    reg CLK;
    reg RESET;
    reg Din;


// Outputs
    wire [3:0] Qout;


// Instantiate the UUT
    Blocking UUT (
        .Qout(Qout), 
        .CLK(CLK), 
        .RESET(RESET), 
        .Din(Din)
        );

initial
 $monitor ($time, "Data in=%b,  CLK=%b,  RESET=%b, Qout=%b", Din, CLK, RESET, Qout);

initial //Initialize input signals
 begin
    CLK = 0;
    RESET = 1;
    Din = 0;
 end

initial 
 begin
   #35  RESET=0;          //Disable RESET at 35 ns
   #50  Din = 1;             //Set Din at different times
   #150 Din = 0;
   #75  Din = 1;
 end

always #20 CLK=~CLK;     //Set clock with a period 20 ns

initial #400 $finish;        //Complete simulation after 400 ns
  
endmodule






Blocking & Non Blocking

1.        Blocking的語法 =  //循序式的方式執行程式

Exp :

        always@(posedge clock)

        begin

                Data = A&B;                   // blocking會先執行第一行程式

                OUT = A+B;                   // 緊接著再執行第二行程式

        end

注意 : 電路都使用blocking的方式設計會造成電路串連的太長,導致延遲太多時間。



2.        Non blocking的語法 <=  //平行式的方式執行程式

Exp :

        always@(posedge clock)

        begin

                Data <= A&B;         // non blocking會同時執行

                OUT <= A+B;                 //

        end

注意 : 電路都使用non blocking的方式設計會造成電路面積加大(成本提高),因為並行處理的輸出都要額外給予一個暫存器來儲存。



        對於新手而言,該如何準確的判斷哪些時候該選用blocking,哪些時候又該選用non blocking來做處理,有相當程度的困難。因此通常會給予新手一些建議,避免設計電路上的錯誤。

1.        組合邏輯assign電路採用blocking,且必須搭配wire

2.        循序邏輯always電路採用non blocking,且必須搭配reg



組合邏輯à與時間無關,大多作為運算用。(如加、減法器)

循序邏輯à與時間有關,大多作為記憶資料,但不能運算。(如正反器)

沒有留言:

張貼留言

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

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