Search code examples
cvhdlprocessortest-bench

Write C statement in VHDL


I am new to VHDL, but I have managed to create a processor in VHDL with some help (it has ALU, multiplier, and a bus architecture to access SRAM memory). The processor decodes 32-bit instructions (which contains the type of operations and the memory addresses).

How do I write the following C code in the processor?

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]);

I'm guessing I would write a list of instructions for this C code in the testbench:

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

and repeat for 9 times.

Is this the right way? Is there a better way to implement the for-loop? Is there a way to write C-code directly in my tb_top.vhd?


Solution

  • Of course, you can manually translate the C code to assembler code, and then assemble this to binary code. But, a better option is to use a C compiler and / or assembler for your processor. If there is no one available and you plan to use your processor for longer / many programs, you can build a new compiler on top of gcc and a new assembler on top of binutils, for example.

    Then place the binary output into the instruction ROM. If you simulate the processor itself without memory and I/O devices, then emulate the instruction ROM in your testbench. An array of std_logic_vector should fit your needs:

    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
     );
    

    The example defines a ROM with 64 addresses, each storing a byte. You will have to update the ranges and the memory content to fit your needs.