Best way to modify strings in VHDL

南笙酒味 提交于 2019-12-24 14:15:27

问题


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?


回答1:


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);



回答2:


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__



来源:https://stackoverflow.com/questions/30631356/best-way-to-modify-strings-in-vhdl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!