SystemVerilog Enum example

SystemVerilog enum example’s

enum data type

 Example-3 : Enumeration Type [DataTypes]

This example shows an error, in case of automatic increment-value, is the same as the value assigned to another enum member.

module enum_datatype;
  //declaration
  enum { red=0, green=1, blue=4, yellow, white=5, black=6 } Colors;
  
  initial begin
    Colors = Colors.first;
    for(int i=0;i<6;i++)
      $display("Colors  ::  Value of  %0s is = %0d",Colors.name(),Colors);
  end
endmodule

Simulator Output

Error-[ENUMDUPL] Duplicate labels in enum
testbench.sv, 9
The enum label 'white' has the value 5 which is duplicate of enum label
'yellow' in the declared enum.

1 error

Click to execute on   

Example-4 : Enumeration Type [DataTypes]

This example shows declaring new data types using the enumerated type.

module enum_datatype;
  //declaration
  typedef enum {GOOD, BAD} pkt_type;
  pkt_type  pkt_a; 
  pkt_type  pkt_b; 
  
  initial begin
    pkt_a = GOOD;
    pkt_b=BAD;
    
    if(pkt_a == GOOD)
      $display("pkt_a is  GOOD packet");
    else   
      $display("pkt_a is  BAD  packet");

    if(pkt_b == GOOD)
      $display("pkt_b is  GOOD packet");
    else   
      $display("pkt_b is  BAD  packet");
  end
endmodule

Simulator Output

pkt_a is  GOOD packet
pkt_b is  BAD  packet

Click to execute on   

Example-5 : Enumeration Type [DataTypes]

This example shows how to use enumeration methods.

module enum_datatype;
  //declaration
  typedef enum { red=0, green, blue=4, yellow, white=10, black } colors;
  
  enum { a, b, c, d, e, f, g } alphabets;
  colors first_set;
  colors second_set;
  
  initial begin
    
    first_set = first_set.first();
    $display("first_set first color is \t %0s, \t Value = %0d", first_set.name(),first_set);
    first_set = first_set.last();
    $display("first_set last color is \t %0s, \t Value = %0d", first_set.name(),first_set);

    second_set = first_set;
    $display("second_set color is \t %0s, \t Value = %0d", second_set.name(),second_set);
    second_set =second_set.prev(2);
    $display("second_set color is \t %0s, \t Value = %0d", second_set.name(),second_set);
    second_set =second_set.next(2);
    $display("second_set color is \t %0s, \t Value = %0d", second_set.name(),second_set);
  
    $display("Number of members in alphabets is \t %0d",alphabets.num());
    $display("Default First members in alphabets is \t %0s , \t value is %0d",alphabets.name(),alphabets);
    alphabets=alphabets.next;
    $display("Next members in alphabets is \t %0s , \t value is %0d",alphabets.name(),alphabets);
    alphabets=alphabets.last;
    $display("Last members in alphabets is \t %0s , \t value is %0d",alphabets.name(),alphabets);
    alphabets=alphabets.prev(3);
    $display("3rd members from last in alphabets is \t %0s , \t value is %0d",alphabets.name(),alphabets);
  end
endmodule

Simulator Output

first_set first color is red, Value = 0
first_set last color is black, Value = 11
second_set color is black, Value = 11
second_set color is yellow, Value = 5
second_set color is black, Value = 11
Number of members in alphabets is 7
Default First members in alphabets is a and value is 0
Next members in alphabets is b , value is 1
Last members in alphabets is g , value is 6
3rd members from last in alphabets is d , value is 3

Click to execute on   

class data type

A Class is a collection of data and a set of subroutines that operate on that data. The data in a class are referred to as class properties, and its subroutines are called methods.A Class is declared using the class…endclass keywords.

Class type example

class packet;
  // Properties
  bit [31:0] address;
  bit [31:0] data   ;
  
  // Method
  function new();
    $display("Inside new Function of packet");
  endfunction  
endclass : packet

A detailed explanation of Classes is explained in later chapters(SystemVerilog Classes).

❮ Previous Next ❯