Queue, String and Enumerated Data Types
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:
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
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