Thursday, January 08, 2009     Register | Login | Search | Contact Us
     

Many of you already received communications about the move of the Cadence user community into cadence.com. And many of you have already joined, with over 4000 registrations in the first two weeks.

The new Cadence Community enhances the ability of Cadence users to connect and collaborate. In addition to moving the community into cadence.com -- enabling single sign-on for community, Sourcelink and Cadence events -- the new site is organized around nine technology segments, giving you easy access to product information, training, forums and blogs. Some of the new features include:
  • Ability to respond to posts via e-mail
  • Technology-specific blogs
  • Latest Web 2.0 social networking capabilities
  • Public profile options
  • Private messaging
  • Friends lists
Visit the new Cadence Community today at www.cadence.com/community and join the discussions!

Registration note: Due to the scope of the enhancements and the new SSO registration system, we were not able to migrate existing cdnusers.org member accounts. So new registrations are required, but this enables a broader set of functionality we think you'll enjoy.

Forum note: Under the guidance of forum moderators, we have taken the 20+ cdnusers.org forums and consolidated them into 11 forums on the new site. Posts have been brought over so you can leverage that posting history. CDNusers forums will be set to read only starting 7/30, and cdnusers.org will be redirected to the new community on 8/4.

Best regards,
Mike and Tom

Michael A. Catrambone - Steering Committee Chairman
Distinguished Engineer
PCB/Mechanical
UTStarcom, Inc.

Tom Diederich
Cadence Community Manager
Home
Forums
Subject: SystemVerilog Data-types (code)
Posting to forums is available to community members only.
Login or Register
Rate this topic:
   
Author Messages
prasad_vc
Posts: 0
Online: User is Offline
9/20/2006 2:12 AM  

Hi,
I am sharing the little work i did on SystemVerilog. This can be the starting point for anyone new to the language. If you all can enhance this by adding your part, will really make an practical guide for the language.

// Program for Data-Types of SystemVerilog
// Author : Vivek Prasad
// Date   : 15 Sep 2006
// Covers : shortint, int, real, time, reg, bit, logic, string, enum, struct
//          class, functions, tasks.

module data_types;

//**************************Data-Types**************************************//
  time dt_time         = 1ms;    // dt_time is defined as time variable.
  shortint dt_decimal  = 10;     // 16-bit representation of decimal 10.
  shortint dt_octal    = 8'o17;  // Octal "317" assigned to dt_octal.
  bit Ε:0]dt_binary   = 8'd42;  // dt_binary will be "00101010".
  logic Ε:0]dt_hex    = 8'hAX;  // 16-base-AB
  byte     dt_byte     = "V";    // ASCII "V" character.
  int      dt_signed   = -5;     // 32-bit represen'n of -5 in one's compl.
  real     dt_real     = 3.141;  // Real number representation.
  real     dt_exp      = 1.0e6;  // Floating point representation.
  reg Α:0]dt_reg      = 4'b01XZ;// 4-value "reg" data-type.
//  string   dt_name     = "{Vivek Prasad\n}";  // Not supported by ncsim5.6
//  int dt_cast;   // could not obtain the reult.
//************************Data-Types****************************************//

//****************************Enum********************************************//
  typedef enum bit {NO,YES} boolean;
  boolean  dt_enum;      // To use boolean as variable-type, use typedef.

  assign dt_enum = YES;
//****************************Enum********************************************//

//****************************Struct******************************************//
  typedef struct packed {
                 bit Ε:0] opcode;
                 bit ⎣:0]addr;
                 } IR;   // To use IR as variable-type, use typedef.
  IR dt_instr;
//****************************Struct******************************************//
 
//****************************Class*******************************************//
  class adder;
    int a;
    int b;
    int sum;
   
    task add();
      sum = a + b;
    endtask

    function void init();
      a = 2;
      b = 5;
      $display("a=%d & b=%d",a,b);
    endfunction
  endclass

  adder dt_add = new();
//****************************Class*******************************************//
     
//****************************Casting*****************************************//
//  dt_cast = int'(3.141);
//****************************Casting*****************************************//

//****************************Initialization**********************************//
  initial
  begin
    $display("Time Literal : %t\n",dt_time);
    $display("Decimal Literal : %d\n",dt_decimal);
    $display("Octal Literal : %o\n",dt_octal);
    $display("Binary Literal : %b\n",dt_binary);
    $display("Hex Literal : %h\n",dt_hex);
    $display("Byte Literal : %h\n",dt_byte);
    $display("Signed Literal : %d\n",dt_signed);
    $display("Real Literal : %f\n",dt_real);
    $display("Float Literal : %f\n",dt_exp);
    $display("Reg Literal : %b\n",dt_reg);
//    $display("String Literal : %s\n",dt_string);
    $display("Enum Data-type : %b\n",dt_enum);
    $display("Structure Example");
    dt_instr.opcode = 8'h12;
    dt_instr.addr   = 24'habcdef;
    $display("Struct Member1 : %h",dt_instr.opcode);
    $display("Struct Member2 : %h\n",dt_instr.addr);
    $display("Class Example");
    dt_add.init();
    dt_add.add();
    $display("Sum = %d\n",dt_add.sum);
//    $display("Casting = %d\n",dt_cast);
    $finish;
  end
//****************************Initialization**********************************//
endmodule

// Output, as on ncsim5.6

// Time Literal    :  1000000 
// Decimal Literal :  10
// Octal Literal   :  000017
// Binary Literal  :  00101010
// Hex Literal     :  ax
// Byte Literal    :  56      // ASCII Value of "V"
// Signed Literal  :  -5
// Real Literal    :  3.141000
// Float Literal   :  1000000.000000
// Reg Literal     :  01XZ
// Enum Data_type  :  0
// Structure Example
// Struct Member1  :  12
// Struct Member2  :  abcdef
// Class Example
// a = 2 & b = 5
// Sum = 7


Vivek Prasad

tpylant
Posts: 87
Online: User is Offline
9/20/2006 9:45 AM  
This is a great showing of all the different data types in SV. In actual usage, I would recommend putting the typedefs into packages. This cleans up your code and makes it easy to reuse the data types since they are in a stand alone package/file.

Example:

[u]types_pkg.sv[/u]
package types_pkg;
typedef enum bit {NO,YES} boolean_t; // adding _t extension makes it easier to identifiy as a typedef
typedef struct packed {
bit Ε:0] opcode;
bit ⎣:0]addr;
} IR_s; // To use IR as variable-type, use typedef.
...
endpackage : types_pkg

Then you "import" the package(s) into whatever modules you need those typedefs.

Example:

module data_types;
import types_pkg::*;
...
boolean_t flag; // boolean is a typedef from types_pkg
...
endmodule

You could also put the display statements into a general purpose task and put that task into the package as well.

Thanks for the examples,
Tim
tmackett
Posts: 34
Online: User is Offline
1/23/2007 3:21 PM  
Just to complete this examples for strings (partially works in IUS5.83 and complete in IUS6.0)

----file: top.v-----------
module top ();
/import "DPI-C" context pass_string_c= task pass_string_sv(input string a);

import "DPI-C" context string_c2v_c= function string string_c2v_sv();

string some_string;

// This doesnt work in IUS583, will work in IUS6.0
export "DPI-C" print_string_c = function print_string_sv;

function void print_string_sv(input string aaa);
$display("Print_string from Verilog % s", aaa);
endfunction

initial
begin
some_string = "pass it ON";
// comment in line below when running IUS6.0
pass_string_sv(some_string); // pass string to C
$display("Verilog: % s \ n", string_c2v_sv() ); // get string from C
$finish;
end

endmodule

----file: main_task.c----------
#include
#include

// to use io_printf
#include

void pass_string_c(const char* a) {
printf("C: %s \n", a);
print_string_c("string passed from C"); // This doesnt work in IUS583
}

const char* string_c2v_c(void) {
io_printf("C: give up a string \n"); // io_printf from vpi will print to ncsim.log file, can use printf()
return "Gimme String";
}

-------File: RUN_NC (script)-------------
rm -r INCA_libs
rm *.so
rm *.log
rm *.h

# Create .h file for Exported tasks/functions only
# (imported function do NOT need .h file):
ncverilog +sv top.v +ncdpiheader+dpi.h +elaborate +ncelabargs+-messages

# ncvlog -sv top.v -mess
# ncelab top -sv -dpiheader dpi.h -mess

gcc -fPIC -shared -o main_task.so main_task.c -I/`ncroot`/tools/inca/include

ncverilog +sv top.v +sv_lib=main_task.so +access+r +ncsimargs+"-sv_root ./"

-----simulation results-----
ncsim> run
C: pass it ON
Print_string from Verilog string passed from C
Verilog: C: give up a string
Gimme String

Simulation complete via $finish(1) at time 0 FS + 0

Todd Mackett
Cadence Incisive
Posting to forums is available to community members only.
Login or Register

Forums > Functional Verification > SystemVerilog > SystemVerilog Data-types (code)


ActiveForums 3.6
     
Copyright 2006 Cadence Design Systems, Inc.