繁体   English   中英

将C宏转换为VHDL

[英]Converting C Macro to VHDL

我对VHDL很新,并试图将两个给定的C宏转换为我的FPGA上的单个指令。 宏是:

#define m_in_bits(buf, num) (buf) >> (24 - (num)) // buf is uint32_t
#define m_ext_bits(buf, i) ((buf) < (1<<((i)-1)) ? (buf) + (((-1)<<(i)) + 1) : (buf))

使用宏的C代码是:

m_ext_bits(m_in_bits(buffer, size), size);

我遇到了让m_ext_bits正确编译的问题。 这是我的VHDL:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity myEntity is
port(
  signal buffer: in std_logic_vector(31 downto 0);
  signal result: out std_logic_vector(31 downto 0)
);
end entity myEntity;

architecture myArch of myEntity  is
  signal size : signed (3 downto 0);
begin

size <= signed(buffer(27 downto 24));

result(15 downto 0) <= std_logic_vector(signed(buffer(23 downto 0)) srl (24 - to_integer(size))
                     + signed((-1 sll to_integer(size)) + 1)); -- the offending line

end architecture myArch ;

result(15 downto 0) <=开始的长行result(15 downto 0) <=实际编译没有错误(实现m_in_bits宏)。 但是,当我添加以下行时,以+开头,会发生错误。 我尝试使用std_logic_vectorsigned类型并且错误发生变化。

表达的类型是不明确的 - “SIGNED”或“UNSIGNED”是两种可能的匹配......
无法确定运算符的定义“”sll“” - 找到0个可能的定义......
非法签名表达......

我认为这是适当的铸造和使用正确的类型来完成所需的操作。

首先, buffer是一个保留的VHDL字,所以改变一下; 使用下面的argbuf

表达式-1 sll to_integer(size)未在VHDL中定义,因为整数值-1是一个数字表达式,只是没有VHDL指定的位表示,因此无法进行移位。 对于整数,也不像bitwise和/或等操作。 可以创建24位有signed类型的-1表示形式:

to_signed(-1, 24)

分配有一个长度问题,因为16位信号( result(15 downto 0) )被赋予24位值(基于右侧argbuf(23 downto 0) )。

然后在解决上述问题时编译srl

代码为:

result(15 downto 0) <= std_logic_vector(resize((signed(argbuf(23 downto 0)) srl (24 - to_integer(size)))
                                               + signed((to_signed(-1, 24) sll to_integer(size)) + 1), 16));

但是,VHDL移位运算符(例如srl )可能会产生意外结果,如本页所述“算术和逻辑移位和旋转是使用VHDL中的函数完成的,而非运算符” ,因此您可以考虑使用numeric_std中定义的移位函数例如shift_right ,作为一般编码风格。 代码功能如下:

result(15 downto 0) <= std_logic_vector(resize(shift_right(signed(argbuf(23 downto 0)), 24 - to_integer(size))
                                               + signed(shift_left(to_signed(-1, 24), to_integer(size)) + 1), 16));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM