Best way to modify strings in VHDL

2019-08-08 18:33发布

I'm currently writing a test bench for a VHDL design I made and I need to write a message to a text file. The message is of the format

[instance_name];[simulation_time] 

(i.e. U0;700 ns) and the filename must be [instance_name].log. Getting the instance name and simulation time is no problem, but writing to a custom filename has been problematic. Under simulation, the instance name will be given in the format:

"U0\ComponentX\test\" 

and I would like to replace the slashes with underscores. Is there an easy way to do this?

标签: vhdl
2条回答
老娘就宠你
2楼-- · 2019-08-08 19:19

Simple replace character function that is a bit more versatile and does the same job would be (nothing wrong with @Paebbels's answer)

      function fReplaceChar(
        a : character;
        x : character;
        s : string) return string
      is
        variable ret : string(s'range) := s;
      begin
        for i in ret'range loop
          if(ret(i) = a) then
            ret(i) := x;
          end if;
        end loop;
        return ret;
      end function fReplaceChar;

If there are more than one character to replace, one can always stack the function:

  function fReplaceChar(
    a : character;
    b : character;
    x : character;
    s : string) return string
  is
  begin
    return fReplaceChar(b, x, fReplaceChar(a, x, s));
  end function fReplaceChar;

or function call:

 fReplaceChar(')','_',fReplaceChar(':','(','_',tb'instance_name));

So for example:

 process 
 begin
     report lf & tb'instance_name & lf &
     fReplaceChar(')','_',fReplaceChar(':','(','_',tb'instance_name));
     wait;
 end process;

gives:

# ** Note:
# :tb(sim):
# _tb_sim__

查看更多
Evening l夕情丶
3楼-- · 2019-08-08 19:32

Our PoC Library has quite a big collection on string operations/functions. There is a str_replace function in PoC.strings that should solve your question. There is also the PoC.utils package with non string related functions, that could also be helpful in handling strings and file I/O.

A simple implementation:

function replace(str : STRING) return STRING
  variable Result : STRING(str'range) := str;
begin
  for i in str'range loop
    if (Result(i) = '\') then
      Result(i)  := '_';
    end if;
  loop;
  return Result;
end function;

Usage:

constant original : STRING := "U0\ComponentX\test\";
constant replaced : STRING := replace(original);
查看更多
登录 后发表回答