String tasks in verilog

Note : If you are planning to use these Tasks with SystemVerilog then Please change byte to byte1 & char to char1 because both are keywords in system verilog .

Calculate the length(length) of string(str) in verilog .


Usage : strlen(str, length);
[sourcecode language='python']
// get the number of char in the string
task strlen;
input [1024*8-1:0] str;
output [10:0] length;
reg [7:0] byte;
reg yes;
reg [10:0] i;
begin
length = 0;
i = 0;
get_byte(str, i, byte);
ischar(byte, yes);
while(yes&&(i<1024)) i =" i" length =" i;" style="font-weight:bold;">Calculate the length(length) of string(str) in verilog .
[/sourcecode]

Usage : strclr(str);

// clear string
task strclr;
output [1024*8-1:0] str;
reg [10:0] i;
begin
for(i = 0; i<1024; i =" i" style="font-weight:bold;">Copy the first numbers(length) of input string(str_in) to output string(str_out) in verilog.


Usage : strcpy(str_out, str_in, length);

// copy part of input string to output string
task strcpy;
output [1024*8-1:0] str_out;
input [1024*8-1:0] str_in;
input [9:0] length;
reg [10:0] str_length;
reg [10:0] i;
reg [7:0] byte;
reg [10:0] diff;
begin
strclr(str_out);
strlen(str_in, str_length);

if(length > str_length)
diff = 0;
else
diff = str_length - length;

for(i = str_length; i > diff; i = i - 1)
begin
get_byte(str_in, i-1, byte);
put_byte(str_out, i-1-diff, byte);
end
end
endtask

Cascade two input strings(str_1, str_2) to one output string(str_out)in verilog

Usage : strcat2(str_out, str_1, str_2);

// cascade two strings
task strcat2;
output [1024*8-1:0] str_out;
input [1024*8-1:0] str_1;
input [1024*8-1:0] str_2;
reg [10:0] str_1_length;
reg [10:0] str_2_length;
reg [10:0] str_3_length;
reg [10:0] length;
reg [9:0] index;
reg [7:0] byte;
begin
strclr(str_out);
strlen(str_1, str_1_length);
strlen(str_2, str_2_length);
str_3_length = str_1_length + str_2_length;
if( str_3_length > 1024 )
begin
$display("The length of cascade string is larger than 1024");
$display("%s", str_1);
$display("%s", str_2);
end
else
begin
for(length = str_1_length; length > 0; length = length -1)
begin
index = length - 1;
get_byte(str_1, index, byte);
put_byte(str_out, index+str_2_length, byte);
end
for(length = str_2_length; length > 0; length = length -1)
begin
index = length - 1;
get_byte(str_2, index, byte);
put_byte(str_out, index, byte);
end
end
end
endtask

cascade three input strings(str_1, str_2, str_3) to one output string(str_out)in verilog

Usage : strcat2(str_out, str_1, str_2, str_3 );


//Author : Jarod, jarod@benz.ee.nthu.edu.tw