Data types part 2
Faculty
B.Mohan Reddy(ap/ece)
rgukt rkv
Type conversions
• 1.static casting:
module ExampleStaticCasting;
logic [3:0] a;
logic signed [3:0] b;
logic signed [7:0] c;
initial begin
// Initialize a to 4-bit binary value 1010
a = 4'b1010;
// Static casting from 4-bit unsigned to 4-bit signed
b = signed'(a);
// Static casting from 4-bit signed to 8-bit signed
c = signed'(b);
// Display the values
$display("a: %0d, b: %0d, c: %0d", a, b, c);
end
endmodule
Example 2:
int i;
real r;
i=int’(10.0-0.1);
r= real’(42);
Contd..
• Dynamic casting: using $cast
• 1.It allows to check for out of bound values
• 2.It can be used as a function or as a task
Enumerated Types
• Example 1:
enum fsm_state_e {
init,
read,
write,
brd,
bwr
};
fsm_state_e current_state; // Declaration of the variable 'current_state' of type
fsm_state_e next_state; // Declaration of the variable 'next_state' of type 'fsm_state_e'
int main() {
current_state = init; // Assigning the 'init' state to the variable 'current_state'
next_state = read; // Assigning the 'read' state to the variable 'next_state'
// ...
return 0;
}
Example 2
typedef enum{ red, green, blue, yellow } colors_e;
colors_e c= c.first;
forever
begin
$display("%s : %dn",c.name,c);
if( c== c.last) break;
c=c.next;
end
Enumerated types:example 3
typedef enum { red, green, blue, yellow , white} color_e;
color_e color;
int c;
Initial
begin
color =red;
c=color;
color=1 ; // can’t be done
color = color_e'(1);//static casting//it will not check for bound value
$cast(color,5); // dynamic casting// out of bound error
if(!$cast(color,5) ); //
$display("casting failure");
end
strings
string s;
Initial
begin
s="IEEE";
$display(s.getc(0)) ;
$display(s.tolower());
s.putc(s.len()-1,"-");
s={s,"F1800"};
$display(s.substr(2,3));
my_log($psprintf("%s %5d",s,42));
task my_log(string message);
$display("%0t: %s", $time,message);
endtask: my_log
1.Variable length- grows
automatically
2.Memory is dynamically
allocated
3.{} – used for concatenation
4.Built-in functions: toupper,
tolower,putc,getc,substr
Memories
Packed arrays
• Examples of packed array
bit [3:0] [7:0] byte_packed;
1. byte_packed
2. byte_packed[3]
3. Byte_packed[3][7]
Multidimensional Arrays
• bit[3:0][7:0] bytes[0:2]; // 3x32
• bit [7:0][3:0] array_4;
• bytes[0]=
• bytes[0][3]=
• bytes[0][1][6]
Dynamic Arrays
int da1[],da2[];
initial
begin
da1=new[5];
foreach(da1[j])
da[j]=j;
da2=da1;
da1=new[20](da1);
da1=new[100];
da1.delete();
end
-> variable size single
dimensional memory-
size changes dynamically
during run time.
Syntax: type_name[]
queues
• Flexible memory – variable sizing
• Single dimensional array with automatic sizing
• Many searching,sorting, and insertion
methods
Syntax: type_name[$]
example
int q1[$]={1,3,4,5,6};
int q2[$]={2,3};
int k=2;
q1.insert(1,k);
q1.delete(1);
q1.push_front(7);
k=q1.pop_back();
q2.push_back(4);
k=q1.pop_front();
foreach(q1[i])
$display(q1[i]);
q2.delete();
Associative Arrays
• Great for creating memory models-sparse
memories
• Dyanmically allocated, non-contiguous elements
• Accessed with integer or string index
• Great for sparse arrays with wide ranging index
• Built in funtions:exists, first,last,next,prev
• Ex: logic [7:0] mem_model[string];
Associative array example
module test();
initial
begin
int mem[int];
mem[2]=1;
mem[3]=5;
mem[5]=60;
mem[23]=89;
mem[1]=70;
if(mem.exitst(5))
$display(“entry exists in the array %d”,mem[5]);
else
$display(“%d:number of entries in array”, mem.num);
end
endmodule
Output- entry exists in the array 60
5 :number of entries in array
Array methods
• Array methods can be used only with unpacked
arrays
– Fixed, Dynamic,queue, and associative arrays
// array reduction methods
int count,total,i[]={9,1,8,3,4,4};
//sorting methods
int d[]={9,1,8,3,4,4};
d.reverse();// {4,4,3,8,1,9};
d.sort();//{1,3,4,4,8,9}
d.rsort();// {9,8,4,4,3,1}
d.shuffle();
Array locator methods:min,max,unique
int f[6]={1,6,2,6,8};
int tq[$];
tq=f.min();
tq=f.max();
tq=f.unique();
int f[6]= {1,6,2,6,8}
tq=f.find_first with {item >5};
tq=f.find_first_index with {item>5};
tq=f.find_last with (item>5);
t1=f.find_last_index with(item>5);

Data types part 2 . pdf

  • 1.
    Data types part2 Faculty B.Mohan Reddy(ap/ece) rgukt rkv
  • 2.
    Type conversions • 1.staticcasting: module ExampleStaticCasting; logic [3:0] a; logic signed [3:0] b; logic signed [7:0] c; initial begin // Initialize a to 4-bit binary value 1010 a = 4'b1010; // Static casting from 4-bit unsigned to 4-bit signed b = signed'(a); // Static casting from 4-bit signed to 8-bit signed c = signed'(b); // Display the values $display("a: %0d, b: %0d, c: %0d", a, b, c); end endmodule Example 2: int i; real r; i=int’(10.0-0.1); r= real’(42);
  • 3.
    Contd.. • Dynamic casting:using $cast • 1.It allows to check for out of bound values • 2.It can be used as a function or as a task
  • 4.
    Enumerated Types • Example1: enum fsm_state_e { init, read, write, brd, bwr }; fsm_state_e current_state; // Declaration of the variable 'current_state' of type fsm_state_e next_state; // Declaration of the variable 'next_state' of type 'fsm_state_e' int main() { current_state = init; // Assigning the 'init' state to the variable 'current_state' next_state = read; // Assigning the 'read' state to the variable 'next_state' // ... return 0; }
  • 5.
    Example 2 typedef enum{red, green, blue, yellow } colors_e; colors_e c= c.first; forever begin $display("%s : %dn",c.name,c); if( c== c.last) break; c=c.next; end
  • 6.
    Enumerated types:example 3 typedefenum { red, green, blue, yellow , white} color_e; color_e color; int c; Initial begin color =red; c=color; color=1 ; // can’t be done color = color_e'(1);//static casting//it will not check for bound value $cast(color,5); // dynamic casting// out of bound error if(!$cast(color,5) ); // $display("casting failure"); end
  • 7.
    strings string s; Initial begin s="IEEE"; $display(s.getc(0)) ; $display(s.tolower()); s.putc(s.len()-1,"-"); s={s,"F1800"}; $display(s.substr(2,3)); my_log($psprintf("%s%5d",s,42)); task my_log(string message); $display("%0t: %s", $time,message); endtask: my_log 1.Variable length- grows automatically 2.Memory is dynamically allocated 3.{} – used for concatenation 4.Built-in functions: toupper, tolower,putc,getc,substr
  • 8.
  • 9.
    Packed arrays • Examplesof packed array bit [3:0] [7:0] byte_packed; 1. byte_packed 2. byte_packed[3] 3. Byte_packed[3][7]
  • 10.
    Multidimensional Arrays • bit[3:0][7:0]bytes[0:2]; // 3x32 • bit [7:0][3:0] array_4; • bytes[0]= • bytes[0][3]= • bytes[0][1][6]
  • 11.
    Dynamic Arrays int da1[],da2[]; initial begin da1=new[5]; foreach(da1[j]) da[j]=j; da2=da1; da1=new[20](da1); da1=new[100]; da1.delete(); end ->variable size single dimensional memory- size changes dynamically during run time. Syntax: type_name[]
  • 12.
    queues • Flexible memory– variable sizing • Single dimensional array with automatic sizing • Many searching,sorting, and insertion methods Syntax: type_name[$]
  • 13.
    example int q1[$]={1,3,4,5,6}; int q2[$]={2,3}; intk=2; q1.insert(1,k); q1.delete(1); q1.push_front(7); k=q1.pop_back(); q2.push_back(4); k=q1.pop_front(); foreach(q1[i]) $display(q1[i]); q2.delete();
  • 14.
    Associative Arrays • Greatfor creating memory models-sparse memories • Dyanmically allocated, non-contiguous elements • Accessed with integer or string index • Great for sparse arrays with wide ranging index • Built in funtions:exists, first,last,next,prev • Ex: logic [7:0] mem_model[string];
  • 15.
    Associative array example moduletest(); initial begin int mem[int]; mem[2]=1; mem[3]=5; mem[5]=60; mem[23]=89; mem[1]=70; if(mem.exitst(5)) $display(“entry exists in the array %d”,mem[5]); else $display(“%d:number of entries in array”, mem.num); end endmodule Output- entry exists in the array 60 5 :number of entries in array
  • 16.
    Array methods • Arraymethods can be used only with unpacked arrays – Fixed, Dynamic,queue, and associative arrays // array reduction methods int count,total,i[]={9,1,8,3,4,4}; //sorting methods int d[]={9,1,8,3,4,4}; d.reverse();// {4,4,3,8,1,9}; d.sort();//{1,3,4,4,8,9} d.rsort();// {9,8,4,4,3,1} d.shuffle();
  • 17.
    Array locator methods:min,max,unique intf[6]={1,6,2,6,8}; int tq[$]; tq=f.min(); tq=f.max(); tq=f.unique(); int f[6]= {1,6,2,6,8} tq=f.find_first with {item >5}; tq=f.find_first_index with {item>5}; tq=f.find_last with (item>5); t1=f.find_last_index with(item>5);