用VHDL编写C语句

Write C statement in VHDL

我是 VHDL 的新手,但我在一些帮助下成功地在 VHDL 中创建了一个处理器(它具有 ALU、乘法器和访问 SRAM 内存的总线架构)。处理器解码 32 位指令(其中包含操作类型和内存地址)。

如何在处理器中编写以下 C 代码?

int i = 0;
int c = 0;

int a[10] = "0,1,2,3,4,5,6,7,8,9";
int b[10] = "1,0,-1,0,1,0,2,1,-1,1";

for (i = 0; i < 9; i++) c += (a[i]*b[i]);

我想我会在测试平台中为这个 C 代码写一个指令列表:

1st instruction: multiply a[0] with b[0]
2nd instruction: add the result to c

重复9次

这是正确的方法吗?有没有更好的方法来实现 for 循环?有没有办法直接在我的 tb_top.vhd?

中编写 C 代码

当然,您可以手动将 C 代码翻译成 assembler 代码,然后 assemble 将其翻译成二进制代码。但是,更好的选择是为您的处理器使用 C 编译器和/或 assembler。如果没有可用的,并且您打算将处理器用于更长/更多的程序,您可以在 gcc 之上构建一个新的编译器,并在其之上构建一个新的 assembler binutils,例如。

然后将二进制输出放入指令ROM。如果您在没有内存和 I/O 设备的情况下模拟处理器本身,则在您的测试台中模拟指令 ROM。 std_logic_vector 的数组应该符合您的需要:

type rom_t is array(natural range <>) of std_logic_vector(7 downto 0) ; 
constant rom : rom_t(0 to 63) := (
  0 => x"00", -- insert binary code of instruction at address 0 here
  1 => x"00", -- instruction at address 1
   -- and so on
   others => x"00" -- fill remaining ROM with zero
 );

例子定义了一个ROM,有64个地址,每个地址存储一个字节。您将必须更新范围和内存内容以满足您的需要。