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

Arrays in SystemVerilog

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

An array is a collection of similar variables, accessible via indices and the array name. Verilog-2001 supports multi-dimensional arrays and net and variable type array declarations are valid. Verilog restricts access to only one element or a slice of an array at a time.

 

 

 

SV:
  1. reg r [0:63]; // 1 dimensional unpacked array
  2. wire [0:7] count [0:144]; // a 1-dimensional unpacked array 145 8-bit nets
  3. int i [0:7] [0:15]; // a 2-dimensional unpacked array
  4.  
  5. // Packed Arrays
  6. wire [3:0] count; // packed array of 4-bits
  7.  
  8. wire [3:0] [1:0] addr; // 2-dimensional packed array
  9. wire [7:0] addr_out = addr; //  entire packed array
  10.  
  11. // Initializing Packed array
  12. wire [7:0] [1:0] data = 16'h0;
  13. // Initializing Unpacked array
  14. wire data [0:1] [0:3] = '{ '{11, 12, 13, 14}, '{2, 3, 4, 5} }
  15. int addr [0:1] [0:15] = '{ default: 2'h3 } // initialize all elements to 2'b11

SV extends the unpacked arrays to new types like int, shortint, logic, bit, byte, real and shortreal. SV also allows accessing all elements of an array at a time unlike Verilog. For packed arrays, multi-dimensional arrays are supported by SV. The entire packed array is stored as contiguous order of bits and arithmetic/logical operations are supported.

Packed arrays can be assigned to another packed array even if their sizes do not match. However, unpacked arrays can be assigned to another unpacked array only if the vector and dimension size are equal. To assign an unpacked array to a packed array or vice versa, bit stream casting needs to be done. A bit-stream cast converts an unpacked array into a stream of bits using the SV static cast operator '. Both packed and unpacked arrays are synthesizable.

SV also supports dynamic arrays for higher levels of modeling (not synthesizable) - A dynamic array is one dimension of an unpacked array, the size of which is set or changed during runtime.

SV:
  1. bit [3:0] crc [];// Dynamic array of 4-bit vectors

SV supports new[], size() and delete() methods similar to C for dynamic arrays.

Associative arrays are also supported by SV - the array index is not restricted to integral values (like int, shortint) and can be any data type.

SV:
  1. integer assoc[*]; // * is wildcard indicates unspecified but integral value index
  2. bit [7:0] assoc [string]; // indexed by string

SV supports in-built methods for associative arrays as tabulated below.


Associative Methods

Description

<array>.num( )

 

Returns number of entries in the
associative array.

<array>.delete(N)

 

if index N is not specified, deletes all elements of the
array. If index N is specified, deletes only that particular element.

<array>.exists(N)

checks if element exists at that particular index of the array. returns
1 if true. index is mandatory

<array>.first(N)

assigns to the index variable N the value of the smallest
index in the array, returns 1 if array is not empty, 0 otherwise

<array>.last(N)

assigns to the index variable N the value of the largest index in the
array, returns 1 if array is not empty, 0 otherwise
<array>.next(N) finds the entry whose index is greater
than current index N and assigns the index variable to that next entry.
Retruns 1 if true
<array>.prev(N) finds the entry whose index is smaller
than current index N and assigns the index variable to that previous entry.
Retruns 1 if true
   
Sphere: Related Content

Structures and Unions in SystemVerilog

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

SystemVerilog supports C-like structures without the optional tag before braces using struct keyword to group several related signals together.

Both variable and net types can be defined as structures. However, net types cannot be used for members of structures. By default, all structures are variable types. Structures that are defined without typedef are known as anonymous structures.

SV:
  1. // Anonymous Structure
  2. struct {
  3.     pkt_type_t type;
  4.     bit [15:0] addr;
  5.     bit [ 31:0] data;
  6.     byte crc;
  7. } Packet_s;
  8. // To assign a value to a struct member
  9. Packet_s.type = ETHER_II;
  10.  
  11. // User defined struct
  12. typedef struct {
  13.    pkt_type_t type;
  14.    bit [15:0] addr;
  15.    bit [ 31:0] data;
  16.    byte crc;
  17. } Packet_s;
  18.  
  19. Packet_s pkt;
  20. // To assign a value to a struct member
  21. pkt.type = ETHER_II;
  22.  
  23. // Structures can be initialized using '{ and } operators
  24. Packet_s pkt = '{ETHER_II, 16'h40, 32'hffff, 8'b30};
  25.  
  26. // Alternate way - explicit
  27. Packet_s pkt = '{crc: 8'b30, addr:16'h40, data:32'hffff, type:ETHER_II};
  28.  
  29. // Default to zero using default keyword
  30. Packet_s pkt = '{default:0};

structures are unpacked by default but packed structures are also possible using packed keyword after the struct - the members are stored as a bit vector with the first member being the MSB. Packed structures can compass only integral values (byte, int, shortint, logic or bit) - real types, enum types are not allowed. This allows mathematical/logical operations on packed structures. packed structures can be either signed or unsigned.

SV:
  1. typedef struct packed signed {
  2.    logic [7:0] addr;
  3.    logic [31:0] data;
  4.    byte crc;
  5. } Packet_s;
  6.  
  7. Packet_s pkt_a, pkt_b;
  8.  
  9. always @(posedge clock)
  10.     if (pkt_a < pkt_b)  // Arithmetic signed operation
  11. ..

Structures can be passed through modules and also passed as arguments to tasks and functions if they are not anonymous. structs are also synthesizable.

Unions are supported in SV using union keyword and stores only a single value unlike structures. Unpacked unions can consist of any variable type like real, unpacked structs for abstract modeling and are not synthesizable.

SV:
  1. typedef union {
  2.    logic [3:0] data;
  3.    logic         data_valid;
  4. } data_u;
  5.  
  6. data_u data_ctrl_u;
  7.  
  8. //tagged union
  9. typedef union tagged {
  10.    logic [3:0] data;
  11.    logic         data_valid;
  12. } data_u;
  13.  
  14. data_u data_ctrl_u;
  15.  
  16. data_ctrl_u = tagged data_valid 1; // set data_valid in union data_ctrl_u to 1
  17.  
  18. data_valid_out = data_ctrl_u.data_valid;

unions can be tagged to include a member with implicit tag - this tag represents the name of the union member last written to. Values are stored in tagged unions using a tagged expression.

unions can also be packed - the members must have same number of bits making them synthesizable.

Sphere: Related Content

« Previous PageNext Page »

Close
E-mail It