Specman string: How to split a string to a list of its chars?

1.2k Views Asked by At

I need to split a uint to a list of bits (list of chars, where every char is "0" or "1", is also Ok). The way I try to do it is to concatenate the uint into string first, using binary representation for numeric types - bin(), and then to split it using str_split_all():

var num : uint(bits:4) = 0xF; // Can be any number  
print str_split_all(bin(num), "/w");

("/w" is string match pattern that means any char). The output I expect:

"0"
"b"
"1"
"1"
"1"
"1"

But the actual output is:

0.      "0b1111"

Why doesn't it work? Thank you for your help.

1

There are 1 best solutions below

0
On BEST ANSWER

If you want to split an integer into a list of bits, you can use the %{...} operator:

var num_bits : list of bit = %{num};

You can find a working example on EDAPlayground.

As an extra clarification to your question, "/w" doesn't mean match any character. The string "/\w/" means match any single character in AWK Syntax. If you put that into your match expression, you'll get (almost) the output you want, but with some extra blanks interleaved (the separators).

Regardless, if you want to split a string into its constituting characters, str_split_all(...) isn't the way to go. It's easier to convert the string into ASCII characters and then convert those back to string again:

extend sys {
  run() is also {
    var num : uint(bits:4) = 0xF; // Can be any number  
    var num_bin : string = bin(num);
    var num_bin_chars := num_bin.as_a(list of byte);

    for each (char) in num_bin_chars {
      var char_as_string : string;
      unpack(packing.low, %{8'b0, char}, char_as_string);
      print char_as_string;
    };
  };
};

The unpack(...) syntax is directly from the e Reference Manual, Section 2.8.3 Type Conversion Between Strings and Scalars or Lists of Scalars