THE VLSI HOMEPAGE

A Practical guide to VLSI Design and Verification..

Procedural Blocks in SystemVerilog

Posted in SystemVerilog by Nigam on the October 19th, 2007

Verilog supports the always block that is used to model combinatorial as well as sequential logic. In addition to this, the always block is used to model clock oscillators and other verification tasks that are not synthesizable. Synthesis tools add strict guidelines that need to be followed while using always block to infer latches, flops or combinatorial logic correctly.

SystemVerilog adds three new procedural blocks - always_ff, always_comb and always_latch for clear logic inference.

SV:
  1. always_comb // Not necessary to specify sensitivity list
  2.   if (sel)
  3.     out = in1;
  4.   else
  5.     out = in2;
  6.  
  7. always_latch       // infer latch
  8.     if (en) q <= d;
  9.  
  10. always_ff @(posedge clk, negedge rstn)
  11.    if (!rstn)       q <= 0;
  12.    else             q <= d;

There are three key differences between always_comb and always blocks :

  • the variables on the LHS side of assignments in always_comb blocks cannot be assigned in multiple procedural blocks for true combinatorial logic behavior and
  • the always_comb block is automatically triggered at simulation time 0 after all initial and always blocks are active. This is necessary to prevent simulation deadlock that would exist if one were to use 2-state types in the always block that default to zero.
  • when including function calls inside always_comb blocks, Verilog always @* can infer incomplete sensitivity lists since it only adds signals read directly in the block and not any inputs called by the function. SV infers the sensitivity list correctly and supports calling functions without arguments.

Tasks and Functions

SV adds several enhancements to Verilog tasks and functions that include:

  • A return keyword that returns the value of the function. In verilog, the value assigned to the function name is returned. In SV, the return takes precedence over this. In addition, the return keyword can be used to abort the function at any time whereas in Verilog the function is executed until the endfunction is reached.
  • SV adds a void type to declare functions that have no return value.
  • SV infers begin - end for grouping multiple statements in functions and tasks.
  • SV allows function formal arguments to be input, output, inout or ref unlike Verilog where function arguments can only be input. By default, all arguments are input in SV unless explicitly declared otherwise.
SV:
  1. function automatic int expo (input int a = 2, b); // arguments can have default values
  2.    expo = a * b;
  3.    return a ** b; // return overrides the value written to function name expo
  4. endfunction
  5.  
  6. always_comb
  7.     x = expo (.a (m), .b(n) ); // can pass function arguments by name in SV
  8.  
  9. // allows unpacked arrays, packed/unpacked structures in functions/tasks
  10. typedef struct {
  11.     logic [23:0] addr;
  12.     logic [127:0] data;
  13.     byte crc;
  14. } packet_s;
  15.  
  16. function void packet_construct ( input logic [127:0] data_in,   output packet_s pkt_s );
  17.     pkt_s.data = data_in;
  18. endfunction

SV allows passing arguments by value and by reference to a function or a task. Pass by argument works by copying the argument into the subroutine area and any changes to the copy are only locally visible. To pass by reference, SV uses the ref keyword and the argument is not duplicated within the function or task and any changes within the subroutine is also visible to the function call. In order to have ref arguments, the function or task must be automatic.

Sphere: Related Content

Leave a Reply


Close
E-mail It