VHDL: Is there a convenient way to assign ascii values to std_logic_vector?

后端 未结 3 1972
眼角桃花
眼角桃花 2021-02-09 00:56
  • In verilog, I can assign a string to a vector like:

    wire [39:0] hello;
    assign hello = \"hello\"; 
    
  • In VHDL, I\'m having diffic

3条回答
  •  既然无缘
    2021-02-09 01:44

    A small general function is one way to do it, with a suggestion below:

    library ieee;
    use ieee.numeric_std.all;
    ...
    -- String to std_logic_vector convert in 8-bit format using character'pos(c)
    --
    -- Argument(s):
    -- - str: String to convert
    --
    -- Result: std_logic_vector(8 * str'length - 1 downto 0) with left-most
    -- character at MSBs.
    function to_slv(str : string) return std_logic_vector is
      alias str_norm : string(str'length downto 1) is str;
      variable res_v : std_logic_vector(8 * str'length - 1 downto 0);
    begin
      for idx in str_norm'range loop
        res_v(8 * idx - 1 downto 8 * idx - 8) := 
          std_logic_vector(to_unsigned(character'pos(str_norm(idx)), 8));
      end loop;
      return res_v;
    end function;
    

提交回复
热议问题