Datatype - Examples
Example for Data Types
(a)Ethernet
(i) Example using String
//String operations in system verilog
//The "String" contains few data, which stores the value of ethernet packet
program Eth_strings;
logic [7:0] Data[string];
string string_index;
initial
begin
Data["Kacper"] = 100;
Data["Sdh"] = 55;
Data["Sonet"]= 300;
Data["Mac"] = 227;
Data["Eth"] = 77;
$display("\nFirst operation, counting the number of entries:");
$display("\nNumber of entries = %0d",Data.num);
$display("\nSecond operation, check if the entry exists in string Data:");
if(Data.exists("Kacper"))
begin
$display("\nString \"Kacper\" is in Data");
$display("\nThird operation, display the first entry in alphabetical order:");
end
if(Data.first(string_index))
begin
$display("\nThe first entry is \"%s\",string_index);
end
if(Data.last(string_index))
begin
$display("\nFourth operation, display the last entry in alphabetical order:");
$display("\nThe last entry is \"%s\",string_index);
$display("\nFifth operation, display the value of all entries in ethernet frame:");
do
$display("\n%s : %0d",string_index, Data[string_index]);
while (Data.prev(string_index));
end
Data.delete;
$display("\nSixth operation, number of entries after deletion = %0d",Data.num);
#1 $display("\n********--------********--------********--------********------******");
end
endprogram
Output in VCS
First operation, counting the number of entries:
Number of entries = 5
Second operation, check if the entry exists in string Data:
String "Kacper" is in Data
Third operation, display the first entry in alphabetical order:
The first entry is "Eth"
Fourth operation, display the last entry in alphabetical order:
The last entry is "Sonet"
Fifth operation, display the value of all entries in ethernet frame:
Sonet : 44
Sdh : 55
Mac : 227
Kacper : 100
Eth : 77
Sixth operation, number of entries after deletion = 0
(ii) Example using typedef
module type_enumerated;
typedef enum {INIT, DECODE, ENCODE, IDLE} fsm_state;
fsm_state pstate, nstate; // Declare type variables
initial
begin
case (pstate)
IDLE: nstate = INIT; // Data assignment
INIT: nstate = DECODE;
DECODE: nstate = ENCODE;
default:nstate = IDLE;
endcase
$display("Present state is: %s",pstate.name()); // Display present state name
$display("Next state is: %s",nstate.name()); // Display next state name
end
endmodule
Output in VCS
Present state is: INIT
Next state is: DECODE
(b) Sonet
(i) Example using String
module string_packet;
string s;
initial
begin
s="SONET_FRAME";
$display(s.getc(0));
$display(s.tolower());
s.putc(s.len()-1,"-");
s={s,"first frame arrival"};
$display(s.substr(2,5));
tx_frame($psprintf("%s %0d",s,50));
end
task tx_frame(string transmitter);
$display("@%0t: %s",$time,transmitter);
endtask
endmodule
Output in VCS
83 sonet_frame
NET_
@0: SONET_FRAM-first frame arrival 50
(ii) Example using different data types
//SONET OCTETS: STS Path Access Point Identifier - J1, Path BIP(Bit Interleaved Parity) - B3,
//C2 - Sonet octet, Path Status - G1, example for sonet data types.
module data_types();
bit [ 7:0] J1_pathtrace = 8'b0101_01xz;
logic [ 7:0] B3_pathBIP = 8'b010z_01xz;
integer C2_octet = 32'b01x1_01xz_01xz_01xz;
int G1_pathstatus = 32'b01x0_01xz_01xz_01xz;
initial
begin
$display ("Showing outputs for different datatypes:\n");
$display ("Value of bit J1 Path Trace = %b\n", J1_pathtrace);
$display ("Value of logic B3 Path BIP = %b\n", B3_pathBIP);
$display ("Value of integer C2 Octet = %b\n", C2_octet);
$display ("Value of int G1 Path Status = %b\n", G1_pathstatus);
$display ("Output for bit + integer is = %b\n", J1_pathtrace + B3_pathBIP);
$display ("Output for logic + int is = %b\n", C2_octet + G1_pathstatus);
J1_pathtrace = 10;
B3_pathBIP = 20;
C2_octet = 30;
G1_pathstatus = 40;
$display ( "After changing values, output for bit + logic is = %b\n", J1_pathtrace + B3_pathBIP);
$display ( "After changing values, output for integer + int is = %b\n", C2_octet + G1_pathstatus);
end
endmodule
Output in VCS
Showing outputs for different datatypes:
Value of bit J1 Path Trace = 01010100
Value of logic B3 Path BIP = 010z01xz
Value of integer C2 Octet = 000000000000000001x101xz01xz01xz
Value of int G1 Path Status = 00000000000000000100010001000100
Output for bit + integer is = xxxxxxxx
Output for logic + int is = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
After changing values, output for bit + logic is = 00011110
After changing values, output for integer + int is = 00000000000000000000000001000110
(iii) Example using Typedef
typedef struct{
bit [7:0]H1Byte ;
bit [7:0]H2Byte ;
bit [7:0]H3Byte ;
} pointer_bytes;
//usage of the typedefs in the module
module typedef_ex();
typedef integer pointer_ind;
typedef pointer_ind;
pointer_ind H1Byte = 'd11;
pointer_bytes struct_instance='{8'b10011100,8'b11110000,8'b11110101};
initial
begin
$display ("H1Byte=%d",H1Byte);
$display ("H1Byte:%b,H2Byte:%b,H3Byte:%b",struct_instance.H1Byte,struct_instance.H2Byte,struct_instance.H3Byte);
# 1 $finish;
end
endmodule
Output in VCS
H1Byte= 11
H1Byte:10011100,H2Byte:11110000,H3Byte:11110101
No comments:
Post a Comment