|
|
|
|
|
|
|
|
|
|
|
|
string data types
|
|
|
string data type is used for storing strings, the size is dynamic as shown below. String data types come with build in methods. |
|
|
|
|
|
String Operators
|
|
|
|
|
|
String
|
Description
|
Str1 == Str2
|
Equality. Checks if the two strings are equal. Result is 1 if they are equal and 0 if they are not. Both strings can be of type string. Or one of them can be a string literal. If both operands are string literals, the expression is the same Verilog equality operator for integer types. The special value " " is allowed.
|
Str1 != Str2
|
Inequality. Logical Negation of ==
|
<,>,<=,=>
|
Comparison. Relational operators return 1 if the corresponding condition is true using the lexicographical ordering of the two strings Str1 and Str2. The comparison behaves like the ANSI C strcmp function (or the compare string method) (with regard to the lexical ordering) and embedded null bytes are included. Both operands can be of type string, or one of them can be a string literal.
|
{Str1,Str2,...,Strn}
|
Concatenation. Each operand can be of type string or a string literal (it shall be implicitly converted to type string). If at least one operand is of type string, then the expression evaluates to the concatenated string and is of type string. If all the operands are string literals, then the expression behaves like a Verilog concatenation of integral types; if the result is then used in an expression involving string types, it is implicitly converted to the string type.
|
{multiplier{Str}}
|
Replication. Str can be of type string or a string literal. Multiplier must be of integral type and can be non-constant. If multiplier is non-constant or Str is of type string, the result is a string containing N concatenated copies of Str, where N is specified by the multiplier. If Str is a literal and the multiplier is constant, the expression behaves like numeric replication in Verilog (if the result is used in another expression involving string types, it is implicitly converted to the string type).
|
Str.method(...)
|
The dot (.) operator is used to invoke a specified method on strings.
|
Str[index]
|
Indexing. Returns a byte, the ASCII code at the given index. Indexes range from 0 to N-1, where N is the number of characters in the string. If given an index out of range, returns 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
String Tasks
|
|
|
String data type contains built in tasks to make the life earier when using string data types. |
|
|
|
|
|
Function
|
Description
|
len()
|
Returns length of string.
|
putc()
|
Used to assign one character of string.
|
getc()
|
Returns a character.
|
toupper()
|
Returns the uppercase of string.
|
tolower()
|
Returns the lowercase of string.
|
compare()
|
Returns the string compare result .
|
icompare()
|
Returns caseless string compare result
|
substr()
|
Returns the sub string of main string.
|
|
|
|
|
|
|
Function
|
Description
|
atoi()
|
Returns integer value of the decimal represenation in ASCII string.
|
atohex()
|
Returns hex value of the hex represenation in ASCII string.
|
atooct()
|
Returns octal value of the octal represenation in ASCII string.
|
atobin()
|
Returns binary value of the binary represenation in ASCII string.
|
atoreal()
|
Returns real value of the real represenation in ASCII string.
|
itoa()
|
Stores the ASCII decimal representation of i into str (inverse of atoi).
|
hextoa()
|
Stores the ASCII hex representation of i into str (inverse of atohex).
|
octtoa()
|
Stores the ASCII octal representation of i into str (inverse of atooct).
|
bintoa()
|
Stores the ASCII binary representation of i into str (inverse of atobin).
|
realtoa()
|
Stores the ASCII real representation of i into str (inverse of atoreal).
|
|
|
|
|
|
|
Example - String
|
|
|
|
|
|
1 module string_ex ();
2
3 string my_string = "This is a orginal string";
4 string my_new_string;
5
6 initial begin
7 $display ("My String = %s",my_string);
8 // Assign new string of different size
9 my_string = "This is new string of different length";
10 $display ("My String = %s",my_string);
11 // Change to uppercase and assign to new string
12 my_new_string = my_string.toupper();
13 $display ("My New String = %s",my_new_string);
14 // Get the length of sting
15 $display ("Length of new string %0d",my_new_string.len());
16 // Compare variable to another variable
17 if (my_string.tolower() == my_new_string.tolower()) begin
18 $display("String Compare matches");
19 end
20 // Compare variable to variable
21 if (my_string.toupper() == my_new_string) begin
22 $display("String Variable Compare matches");
23 end
24 #1 $finish;
25 end
26
27 endmodule
You could download file string.sv here
|
|
|
|
|
|
Simulation Output - String
|
|
|
|
|
|
My String = This is a orginal string
My String = This is new string of different length
My New String = THIS IS NEW STRING OF DIFFERENT LENGTH
Length of new string 38
String Compare matches
String Variable Compare matches
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright © 1998-2014 |
Deepak Kumar Tala - All rights reserved |
Do you have any Comment? mail me at:deepak@asic-world.com
|
|