0% found this document useful (0 votes)
23 views12 pages

Unit 2 Notes SV

Uploaded by

avpoojitha26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views12 pages

Unit 2 Notes SV

Uploaded by

avpoojitha26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

System Verilog

Different Data Types

User-Defined and Enumerated Types:

String Data Types:

In System Verilog, strings are represented using the string data type, which allows you to store
sequences of characters. Strings are typically used for working with text data and provide a
way to manipulate and process strings within your System Verilog code.

Here's a brief explanation of the string data type and some examples:

1. Declaration: You can declare a string variable like this:

string myString;

2. Initialization: Strings can be initialized with text data, like this:

System Verilog Copy code

string greeting = "Hello, World!";

3. String Concatenation: You can concatenate strings using the + operator:

System Verilog Copy code

string firstName = "John"; string lastName = "Doe"; string fullName = firstName + " "
+ lastName; // Concatenates "John Doe"

4. String Methods: SystemVerilog provides built-in methods to manipulate strings, such


as len(), substr(), find(), compare(), etc. For example:

System Verilog Copy code

string myString = "Hello, SystemVerilog!";

int length = myString.len(); // Returns the length of the string (19)

string subStr = myString.substr(6, 5); // Extracts "System" from the string

int position = myString.find("System"); // Returns the position of "System" in the string


(6)

int compareResult = myString.compare("Hello, World!"); // Compares two strings

5. String Formatting: You can format strings using the $sformat and $sformatf system
functions. These functions are similar to the printf family of functions in C/C++.

System Verilog Copy code


int num = 42; string formattedString;

$sformat(formattedString, "The answer is %0d.", num); // Creates "The answer is 42."

6. String Literals: String literals are enclosed in double quotes, and escape sequences are
supported. For example:

System Verilog Copy code

string escapedString = "This is a string with\na new line";

7. String Comparison: You can compare strings using ==, !=, <, >, <=, and >= operators.

System Verilog Copy code

string str1 = "apple"; string str2 = "banana"; if (str1 < str2) begin // This block will
execute end

String manipulation and processing are essential in various applications, including


parsing input data, generating reports, and interfacing with external systems. The string
data type in SystemVerilog provides a convenient way to work with text data within
your designs.

Event Data types:

In System Verilog, the event data type is used to represent events or conditions that can trigger
a change in the control flow of your code. Events are a fundamental concept in System Verilog
used primarily in simulation, enabling you to synchronize and control the execution of different
parts of your design or testbench. Here's a brief explanation of the event data type with an
example:

1. Declaration: You can declare an event variable like this:

System Verilog Copy code

event myEvent;

2. Event Wait: You can use the wait statement to wait for an event to occur. When the
event is triggered, the code following the wait statement continues execution:

System Verilog Copy code

event myEvent;

initial begin
$display("Before waiting for the event");
wait(myEvent); // Wait for the event to be triggered
$display("Event has been triggered");
end
In the above example, the initial block waits for myEvent to be triggered before proceeding
to display "Event has been triggered."

3. Event Trigger: You can trigger an event using the -> operator:

System Verilog Copy code

event myEvent;
initial begin
$display("Triggering the event");
->myEvent; // Trigger the event
end

In this example, the event myEvent is explicitly triggered, and any code waiting for it to
occur will continue execution.

4. Event Control Flow: Events are often used to control the flow of your simulation. For
instance, you can use events to synchronize the execution of multiple threads or to wait
for a specific condition to be met before proceeding.

System Verilog Copy code

event startEvent;
event stopEvent;
initial begin
$display("Waiting for the start event");
wait(startEvent); // Wait for the start event
$display("Start event has occurred. Waiting for stop event");
wait(stopEvent); // Wait for the stop event
$display("Stop event has occurred");
End

initial begin #10;


$display("Triggering the start event");
->startEvent; // Trigger the start event #10;
$display("Triggering the stop event");
->stopEvent; // Trigger the stop event end

In this example, one initial block waits for the start event, and another initial block
triggers the start and stop events at different time points.

Events are essential for controlling the timing and synchronization of simulations in
System Verilog, allowing you to model complex interactions and dependencies within
your designs and testbenches.

User-Defined Types

In System Verilog, you can define your own user-defined types to create custom data structures
and improve code readability. These user-defined types are typically based on existing data
types, and they help make your code more self-documenting and maintainable. There are two
main ways to create user-defined types: using typedef and enum. Here's a brief explanation of
user-defined types with examples:

1. User-Defined Types Using typedef:


The typedef keyword allows you to create custom names for existing data types, making
it easier to understand the purpose of variables. You can create typedefs for various
data types, including integers, logic, and user-defined structures.

Example using integer type:

System Verilog Copy code

typedef int Temperature; // Create a custom type 'Temperature' based on int

Temperature roomTemp = 25; // Use the custom type

Example using struct:

System Verilog Copy code

typedef struct { int x; int y; }

Point; // Create a custom struct type 'Point'

Point origin = {0, 0}; // Use the custom struct type

2. User-Defined Types Using enum:


Enums allow you to create custom types with a fixed set of symbolic values. This is
particularly useful for defining states, modes, or any set of related values.

Example:

systemverilogCopy code

typedef enum logic [1:0]

{ IDLE = 2'b00,

ACTIVE = 2'b01,

ERROR = 2'b10 } State; // Create a custom enum type 'State'

State currentStatus = IDLE; // Use the custom enum type

3. User-Defined Types for Improved Readability:


User-defined types can significantly improve code readability, especially when dealing
with complex data structures, state machines, or when you want to give descriptive
names to variables.

Example with a custom struct for RGB color:


System Verilog Copy code

typedef struct {

logic [7:0] red;

logic [7:0] green;

logic [7:0] blue; } RGBColor; // Create a custom struct type 'RGBColor'

RGBColor backgroundColor = '{8'hFF, 8'hCC, 8'h99}; // Use the custom struct


type

By creating user-defined types, you can make your code more intuitive and maintainable, as it
becomes easier to understand the purpose of variables and data structures. These types can also
help catch errors early in the development process and promote good coding practices.

Nets:

In SystemVerilog, "nets" refer to data types used for connecting signals within your hardware
design. Nets are used to represent physical connections, wires, or interconnections between
various logic gates and components. Unlike registers (variables), nets do not have storage
capabilities but represent the electrical connections and carry values throughout the design.

Here's a brief explanation of nets in System Verilog:

1. Types of Nets:
System Verilog provides different net types, including:
a. wire: A 4-state net type used for connecting signals. Wires carry values (0, 1, X, or Z)
and are commonly used to model interconnections in your design.
b. wand: A wired-AND net type. It is used to represent multiple drivers where the result
is 0 if any driver is 0; otherwise, it's X.
c. wor: A wired-OR net type. It is used to represent multiple drivers where the result is 1
if any driver is 1; otherwise, it's X.
d. tri: A 4-state tri-state net type used to model a bus or data path where multiple drivers
can actively drive the value onto the net.

2. Usage: Wires (and other net types) are used to connect inputs and outputs of various
hardware modules and components within your design. They are used to propagate
signals, such as data and control signals, from one part of your design to another.

3. Continuous Assignment: Wires are often used in continuous assignments to specify the
logical relationships between different signals in your design. For example:

System Verilog Copy code

wire a, b, c, result;
assign result = a & b | c; // Logical AND and OR operation using wires

In this example, the result wire is assigned the result of the logical AND and OR
operations of a, b, and c.

4. Connectivity: Wires are used to connect the output of one module to the input of
another. This interconnection between modules is vital for creating complex digital
designs.

System Verilog Copy code

module ModuleA(input wire inA, output wire outA);

endmodule

module ModuleB(input wire inB, output wire outB);

endmodule

// Connect the output of ModuleA to the input of ModuleB

ModuleA instanceA(.inA(wireA), .outA(wireB));

ModuleB instanceB(.inB(wireB), .outB(wireC));

In this example, wireA, wireB, and wireC are used to connect the modules together.

Nets, especially wires, are essential for modeling the connectivity and signal flow in your
digital hardware designs in SystemVerilog. They represent the physical wires that carry data
and control signals, allowing for accurate modeling and simulation of digital circuits.

Reg:

In System Verilog, the "reg" keyword is used to declare register variables, which represent data
storage elements or memory elements in your hardware design. Registers are used to store and
manipulate data within your digital circuits, and they are typically used for sequential logic
elements like flip-flops or latches. Here's a brief explanation of the "reg" keyword in System
Verilog:

1. Declaration:
The "reg" keyword is used to declare register variables. You can declare registers of
various widths (number of bits) based on your design requirements.

System Verilog Copy code

reg [7:0] counter; // Declares an 8-bit register named 'counter'


2. Usage: Registers are commonly used for storing data values and for creating sequential
logic in your hardware design. For example, you can use registers to implement state
machines, counters, data storage, and other sequential elements.

3. Assignment: You can assign values to registers using non-blocking assignments ("=")
within procedural blocks like always or initial blocks.

System Verilog Copy code

always_ff @(posedge clk or negedge rst_n) begin

if (!rst_n) // Asynchronous reset

counter <= 8'b0; // Reset the counter to 0

else

counter <= counter + 1; // Increment the counter

end

In this example, the "counter" register is assigned a new value based on clock edges and an
asynchronous reset signal.

4. Sequential Logic: Registers are integral to modeling sequential logic elements. They
are used to create flip-flops and latches, which store and propagate values based on
clock edges, allowing you to design state machines and other sequential circuits.

System Verilog Copy code

always_ff @(posedge clk)

begin

if (enable) // Synchronous operation

counter <= counter + 1;

end

In this case, "counter" is updated synchronously on the rising edge of the clock when the
"enable" condition is met.

Registers play a fundamental role in modeling sequential behavior within digital circuits. They
store and update data, enabling the design of complex sequential logic and state machines. It's
essential to understand how registers work and how to properly use them when designing
digital systems in System Verilog.
In System Verilog, the "logic" data type represents a 4-state logic value, which is used to model
and simulate digital circuits. The "logic" type is part of the System Verilog standard and is
commonly used to represent signals and logic values in digital designs. Here's a brief
explanation of the "logic" data type:

1. Declaration: You can declare logic variables to represent 4-state logic values using the
"logic" keyword:

System Verilog Copy code

logic mySignal; // Declares a logic variable

2. Usage: Logic variables are used to represent digital signals, which can take one of four
values: 0, 1, X (unknown or uninitialized), or Z (high impedance). This 4-state logic
system allows you to model various conditions in digital designs, including unknown
or high-impedance states that can occur in real hardware.

3. Operations: You can perform various logic operations on "logic" variables, including
AND, OR, NOT, XOR, etc. For example:

System Verilog Copy code

logic a = 1;

logic b = 0;

logic result = a & b; // result is 0 (logical AND)

4. Signal Types: The "logic" data type is often used to declare signal types within digital
modules, representing inputs, outputs, and internal signals in your design.

System Verilog Copy code

module MyModule(input logic clk, input logic reset, output logic dataOut);

// Module logic here

endmodule

In this example, "clk," "reset," and "dataOut" are declared as "logic" signals in the module.

5. Four-Valued Logic: The four-valued logic system in SystemVerilog provides a way to


model and simulate real-world digital circuits more accurately. It allows for the
representation of conditions where the value is unknown or where multiple drivers
might be in a high-impedance state, which is common in tri-state buses and other
hardware scenarios.
6. Simulation and Testbenches: In simulation, the "logic" data type allows for
comprehensive testing and verification of digital designs by modeling and tracking
signal behavior, including scenarios where signals might be in an unknown or high-
impedance state.

Overall, the "logic" data type is fundamental in System Verilog for representing digital signals
and logic values, enabling you to accurately model and simulate digital circuits, including
complex behavior and signal conditions that occur in real hardware.

Type casting:

Type casting in System Verilog refers to the process of converting a value from one data type
to another. It is a way to explicitly change the data type of a variable or an expression. Type
casting is useful when you need to perform operations or assignments involving different data
types. Here's a brief explanation of type casting in SystemVerilog:

1. Implicit and Explicit Casting:


• Implicit Casting: System Verilog performs implicit casting automatically when it is safe
to do so. For example, when assigning an integer to a larger integer type, the value is
implicitly cast without loss of information.
• Explicit Casting: Explicit casting is done by the designer to convert a value from one
data type to another. It is achieved by using type casting operators.

2. Type Casting Operators: SystemVerilog provides several type casting operators to


facilitate explicit type casting. Some of the common casting operators include:
• ‘(data_type)value: This operator is used to cast a value to the specified data type. For
example, (int)real_var casts a real value to an integer.

3. Examples:
a. Casting Between Numeric Types:

System Verilog Copy code

real real_var = 3.14159;

int int_var = (int)real_var; // Explicit casting from real to int

b. Casting to and from Logic and Bit Vectors:

System Verilog Copy code

logic [1:0] logic_var = 2'b10;

int int_value = (int)logic_var; // Explicit casting from logic to int

logic [1:0] converted_logic = (logic[1:0])int_value; // Explicit casting from int to


logic

c. Casting to Different Bit Width:

System Verilog Copy code


logic [3:0] input_data = 4'b1101;

logic [1:0] lower_bits = (logic[1:0])input_data; // Explicit casting to 2 LSBs

d. Casting Enumerated Types:

System Verilog Copy code

typedef enum logic [1:0] {RED = 2'b00, GREEN = 2'b01, BLUE = 2'b10 } Color;

int color_value = (int)RED; // Explicit casting from enum to int

e. Casting String to Integer:

System Verilog Copy code

string num_str = "42";

int num = int'(num_str); // Explicit casting from string to integer

4. Type Safety and Data Loss: When performing type casting, be aware of potential data
loss or truncation. For example, when casting from a larger data type to a smaller one,
you may lose bits if the value exceeds the range of the smaller data type.

Type casting in System Verilog allows you to work with different data types, facilitating
operations and assignments between them while giving you control over the conversion
process. However, it's essential to use type casting carefully to avoid unintended data loss or
issues in your design.

Constants:

In System Verilog, constants are values that do not change during the execution of a program
or during simulation. They are used to define fixed values that should not be modified.
Constants provide a way to make your code more readable, maintainable, and self-
documenting. Here's a brief explanation of constants in System Verilog:

1. Declaration: Constants are typically declared using the const keyword. You can declare
constants of various data types, including integers, logic, and custom data types.

System Verilog Copy code

const int MAX_COUNT = 100; // Declare an integer constant

const logic ENABLE = 1'b1; // Declare a logic constant

typedef enum logic [1:0] { RED = 2'b00, GREEN = 2'b01, BLUE = 2'b10 } Color;

const Color DEFAULT_COLOR = Color::GREEN; // Declare an enumerated type


constant
2. Initialization: Constants must be initialized at the time of declaration, and their values
cannot be changed once set. This ensures that the constant retains the same value
throughout the program's execution.

3. Benefits: Constants offer several advantages in SystemVerilog code:


• Readability: Constants make your code more self-documenting by providing
meaningful names for values.
• Maintainability: If you need to change a constant's value, you can update it in one place,
and the change is automatically reflected throughout your code.
• Error prevention: Using constants helps prevent accidental changes to critical values,
enhancing code reliability.

4. Use Cases: Constants are commonly used for:


• Defining parameters such as maximum values, constants for configuration, and
constants for control.
• Assigning specific values to enumerated types for state machines, flag settings, and
control signals.
• Giving descriptive names to values that are used multiple times in your code, making
it easier to understand and modify.
5. Examples: Here are some examples of using constants in SystemVerilog code:

System Verilog Copy code

module Counter #(parameter WIDTH = 8);

const int MAX_COUNT = (1 << WIDTH) - 1;

// Calculate the maximum count based on WIDTH logic [WIDTH-1:0] count;

// Module logic here

endmodule

module RGBLed;

typedef enum logic [1:0] { RED = 2'b00, GREEN = 2'b01, BLUE = 2'b10 } Color;

const Color DEFAULT_COLOR = Color::GREEN;

logic [1:0] currentColor = DEFAULT_COLOR;

// Module logic here

endmodule

In summary, constants in SystemVerilog are used to define fixed values that should not change
during the execution of a program or simulation. They enhance code readability and
maintainability while preventing unintended modifications to critical values in your design.

You might also like