我正在尝试通过串行输出不同的(非常量)值。串行通信工作正常,但似乎没有一种优雅的、可综合的方式将任何大小和值的任何整数/自然/std_logic_vector/unsigned/signed 类型转换为基于 ASCII 表的 8 位 std_logic_vectors 数组。这非常奇怪,因为我想做的事情并不少见。
我可以做到这一点的一种方法是使用大型查找表或长而嵌套的 if-elsif-else 语句链,但这似乎非常低效且不优雅。
这不会合成:
eight_bit_result <= std_logic_vector(to_unsigned(character'pos(some_integer), 8));
ISE 14.7 因某些头文件引起的模糊错误而中断。即使它确实有效,它也不适用于 0-255 之外的值。这样做的正确方法是什么?
编辑:
我写了一个快速而肮脏的函数来覆盖整数值 0-9999。它不需要计时过程,不需要额外的实体等。到目前为止,建议的答案似乎过于复杂。
function get_ascii_array_from_int(i : integer range 0 to 9999) return char_array is
variable result : char_array(0 to 3) := (x"30", x"30", x"30", x"30"); -- 0000
begin
if i >= 0 then
if i < 1000 then
result(0) := x"30"; -- 0
result(1 to 3) := get_ascii_array_from_int_hundreds(i);
elsif i < 2000 then
result(0) := x"31"; -- 1
result(1 to 3) := get_ascii_array_from_int_hundreds(i-1000);
elsif i < 3000 then
result(0) := x"32"; -- 2
result(1 to 3) := get_ascii_array_from_int_hundreds(i-2000);
elsif i < 4000 then
result(0) := x"33"; -- 3
result(1 to 3) := get_ascii_array_from_int_hundreds(i-3000);
elsif i < 5000 then
result(0) := x"34"; -- 4
result(1 to 3) := get_ascii_array_from_int_hundreds(i-4000);
elsif i < 6000 then
result(0) := x"35"; -- 5
result(1 to 3) := get_ascii_array_from_int_hundreds(i-5000);
elsif i < 7000 then
result(0) := x"36"; -- 6
result(1 to 3) := get_ascii_array_from_int_hundreds(i-6000);
elsif i < 8000 then
result(0) := x"37"; -- 7
result(1 to 3) := get_ascii_array_from_int_hundreds(i-7000);
elsif i < 9000 then
result(0) := x"38"; -- 8
result(1 to 3) := get_ascii_array_from_int_hundreds(i-8000);
else
result(0) := x"39"; -- 9
result(1 to 3) := get_ascii_array_from_int_hundreds(i-9000);
end if;
else
result := (x"6e", x"65", x"67", x"23"); -- "neg#"
end if;
return result;
end get_ascii_array_from_int;
您可能已经猜到还有其他三种方法: get_ascii_array_from_int_hundreds get_ascii_array_from_int_tens get_ascii_array_from_int_ones
说出你对这个函数的看法,但请记住,当我需要它时,它会产生正确的结果,而不是几个周期后,而且简单明了。我可以轻松地扩展它以涵盖负数和更大的数字。