VHDL:将字符串转换为 Std_Logic_Vector

VHDL: Convert String to Std_Logic_Vector

我正在 VHDL 中编写一个 sha-256 哈希函数,它接受一个 String。我需要将此字符串转换为 std_logic_vector 位。因此,我必须以某种方式从 String 字符 中提取 ,但我不确定最佳方法。据我所知,在任何库中都不存在执行此操作的内置函数。

我唯一的选择是遍历字符串的每个索引并使用 case 块将 字符 映射到它们各自的 8 位 ASCII 对应物吗?或者有什么方法可以将 character 转换为 bits?

您可以使用如下函数(未经测试)将字符串转换为位:

function to_std_logic_vector(a : string) return std_logic_vector is
    variable ret : std_logic_vector(a'length*8-1 downto 0);
begin
    for i in a'range loop
        ret(i*8+7 downto i*8) := std_logic_vector(to_unsigned(character'pos(a(i)), 8));
    end loop;
    return ret;
end function to_std_logic_vector;

我不认为 string 类型是可综合的,所以这个函数只对模拟或静态初始化有用。