typedef class

A typedef declaration is used to create aliases or our own identifiers through which it can provide a new name for a type.That means it provides the new name to the already existing type and can be used interchangeably with the type specifiers like struct,union,int,bit,etc.

1. typedef int xyz
2. xyz xyz_1; // xyz is equivalent to int

3. typedef [15:0] word ; // create your own data type as per your requirement ;
4. word data1, data2;

Example of typedef using struct, union and enum data types.

syntax :

typedef enum type_declaration_identifier;
typedef struct type_declaration_identifier;
typedef union type_declaration_identifier;
typedef class type_declaration_identifier;
typedef type_declaration_identifier;



Advantages Of Using Typedef :

  • Makes a code portable,since changing a type name is easier at one place of the code rather than all the places.
  • Reduces the length of the complex declaration and hence improves readability.
  • Typecasting is easier.
  • Can be used through pointers and also useful in declaration of arrays.


  • Example 01: typedef_datatype

    module typedef_datatype();

    typedef bit [3:0] nibble; // Declare the userdeifine variable 4 bits called nibble
    typedef bit [7:0] result; // Declare the userdeifine variable 8 bits called result

    nibble Operand_A,Operand_B; // get instance of result as 2 inputs called Operand_A,Operand_B
    result sum,sub,div,mul; // get instance of result as 4 outputs called sum,sub,div,mul

    initial
    begin
    Operand_A = 4'b1001; //initilize 1001 value to Operand_A
    Operand_B = 4'b0110; //initilize 0000 value to Operand_A
    end

    initial
    begin
    $display("----------------OUTPUT RESULT -----------------------");
    sum = Operand_A + Operand_B;
    $display ("Operand_A + Operand_B sum = %0b", sum);

    sub = Operand_A - Operand_B;
    $display("Operand_A - Operand_B sub = %0b", sub);

    div= Operand_A / Operand_B;
    $display("Operand_A / Operand_B div = %0b", div);

    mul = Operand_A * Operand_B;
    $display ("Operand_A * Operand_B mul = %0b", mul);
    $display("------------------------------------------------------");
    end

    endmodule

    OUT PUT :

    # ----------------OUTPUT RESULT -----------------------
    # Operand_A + Operand_B sum = 1111
    # Operand_A - Operand_B sub = 11
    # Operand_A / Operand_B div = 1
    # Operand_A * Operand_B mul = 110110
    # ------------------------------------------------------