0% found this document useful (0 votes)
33 views10 pages

Queue, String and Enumerated Data Types

The document describes three data types in SystemVerilog: queues, strings, and enumerated types. Queues provide easy searching and sorting and can grow and shrink as elements are added or removed. Strings support operations like getting/setting characters, converting case, extracting substrings, and concatenation. Enumerated types assign symbolic names to a set of integer values for type safety.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views10 pages

Queue, String and Enumerated Data Types

The document describes three data types in SystemVerilog: queues, strings, and enumerated types. Queues provide easy searching and sorting and can grow and shrink as elements are added or removed. Strings support operations like getting/setting characters, converting case, extracting substrings, and concatenation. Enumerated types assign symbolic names to a set of integer values for type safety.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

QUEUE,STRING AND

ENUMERATED DATA TYPES

QUEUE
A data type that provides easy searching and
sorting in a structure.
Queue can grow and shrink
With a queue you can add and remove elements
anywhere.

Examle:
int j = 1,
b[$] = {3,4},
q[$] = {0,2,5}; // {0,2,5} Initial queue
initial begin
q.insert(1, j); // {0,1,2,5} Insert 1 before 2
q.insert(3, b); // {0,1,2,3,4,5} Insert whole q.
q.delete(1); // {0,2,3,4,5} Delete elem. #1
// The rest of these are fast
q.push_front(6); // {6,0,2,3,4,5} Insert at front
j = q.pop_back; // {6,0,2,3,4} j = 5
q.push_back(8); // {6,0,2,3,4,8} Insert at back
j = q.pop_front; // {0,2,3,4,8} j = 6
foreach (q[i])
$display(q[i]);
end

STRING
String operations:

getc(N): returns byte at location N


putc(M): writes a byte into a string at location M
toupper():returns an upper-case copy
tolower(): returns a lower-case copy
substr(start,end): function extracts character
from location start to end.

Example:
string s;
initial begin
s = "SystemVerilog";
$display(s.getc(0)); // Display: 83 (S)
$display(s.toupper()); // Display: SYSTEMVERILOG
s = {s, "3.1b"}; // "SystemVerilog3.1b"
s.putc(s.len()-1, "a"); // change b-> a
$display(s.substr(2, 5)); // Display: stem
// Create temporary string, note format
my_log($psprintf("%s %5d", s, 42));
end
task my_log(string message);
// Print a message to a log
$display("@%0d: %s", $time, message);
endtask

EUMERATED TYPE
Enumerated data type assigns a symbolic value to each legal
value taken by the data type.

Ex:
// Create data type for values 0, 1, 2
typedef enum {INIT, DECODE, IDLE} fsmstate_e;
fsmstate_e pstate, nstate; // declare typed variables
initial begin
case (pstate)
IDLE: nstate = INIT; // data assignment
INIT: nstate = DECODE;
default: nstate = IDLE;
endcase
$display("Next state is %0s",nstate.name); // Use name function
end

Defining enumerated types:


typedef enum {INIT, DECODE=2, IDLE} fsmtype_e;

Incorrectly specified enumerated type


typedef enum {FIRST=1, SECOND, THIRD} ordinal_e;
ordinal_e position;

Correctlyspecified enumerated type


typedef enum {ERR_O=0, FIRST=1, SECOND, THIRD}
ordinal_e;
ordinal_e position;

Routines:
first returns first member of the enumeration.
last returns last member of the enumeration.
next returns the next element of the
enumeration.
next(N) returns the Nth next element.
prev returns the previous element of the
enumeration.
prev(N) returns the Nth previous element.

Example:
enum {RED, BLUE, GREEN} color;
color = color.first;
do
begin
$display("Color = %0d/%0s", color, color.name);
color = color.next;
end
while (color != color.first); // Done at wraparound

Converting to and from enumerated type:


Ex:
typedef enum {RED, BLUE, GREEN} COLOR_E;
COLOR_E color, c2;
integer c;
initial begin
c = color; // Convert from enum to integer
c++; // Increment integer
if (!$cast(color, c)) // Cast integer back to enum
$display("Cast failed for c=%0d", c);
$display("Color is %0d / %0s", color, color.name);
c2 = COLOR_E(c); // No type checking done
end

You might also like