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

后端 未结 3 571
一整个雨季
一整个雨季 2021-02-09 01:06
  • 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:57

    This one varies little for Morten's answer - it only uses one multiply, it copies the string instead of creating an alias, it uses an additional variable and it returns a standard logic vector with an ascending index range.

    From a package called string_utils:

    library ieee; 
    use ieee.numeric_std.all;
    -- ...
        function to_slv(s: string) return std_logic_vector is 
            constant ss: string(1 to s'length) := s; 
            variable answer: std_logic_vector(1 to 8 * s'length); 
            variable p: integer; 
            variable c: integer; 
        begin 
            for i in ss'range loop
                p := 8 * i;
                c := character'pos(ss(i));
                answer(p - 7 to p) := std_logic_vector(to_unsigned(c,8)); 
            end loop; 
            return answer; 
        end function; 
    

    You could add an argument with a default specifying ascending/descending index range for the return value. You'd only need to provided the argument for the non default.

提交回复
热议问题