Skip to main content

SW Compilation

In this section we will write a basic C program and compile the software part of the project.

  • Go to verif/mini_core/tests and create file named basics.c with the following content:
int main(){
int x = 1;
int y = 2;

int z = x + y;

return 0;
}
  • Type the following command in the terminal to compile the C program we just wrote:
./build.py -dut mini_core -test basics -app
  • Go to target/mini_core/tests/basics and observe the content of the folder. You should see the following files:

basics_sw_compile.png

WOW we got all the files we need to run the HW simulation. Let's try to understand what each file is doing: (those are the most important files)

  • basics_rv32i.c.s - This is an assembly file that was generated by the compiler. This file contains the assembly code of the C program we wrote.

  • basics_rv32i.elf - This is an ELF file that was generated by the compiler. This file contains the machine code of the C program we wrote. This file is not readable by humans so we wont use it at this point.

  • basics_rv32i_elf.txe - This is the converted version of the ELF file written in assembly code. This file is readable and we will use it allot while running and debugging the HW simulation.

  • inst_mem.sv - This is the instruction memory file that contains the machine code of the C program we wrote suitable for
    RISC-V ISA.

Hey, doesn't this seem like something we've seen before? Yes, you're correct. We did something similar when we were compiling a C program in the RISC-V GCC section in TFM. But now, the difference is that we're connecting everything to the MAFIA environment and doing it in a more automated way.

note: Please download the riscv-spec cause you will need it in the future. Please make sure you understand RV32I Base Instruction Set as shown in chapter 19 and RISC-V Assembly Programmer’s Handbook as shown in chapter 20 cause we will use the following table in the future:

RegisterABI nameDescription
x0zeroHard-wired zero
x1raReturn address
x2spStack pointer
x3gpGlobal pointer
x4tpThread pointer
x5t0Temporary/alternate link register
x6-7t1-2Temporaries
x8s0Saved register
x9s1Saved register
x10-11a0-1Function arguments/return values
x12-17a2-7Function arguments
x18-27s2-s11Saved registers
x28-31t3-6Temporaries

Background of SW Compilation command

In this section, we will outline the commands that run in the background when you use the
./build.py -dut mini_core -test basics -app command.

mkdir ./target/mini_core/tests/basics

mkdir ./target/mini_core/tests/basics/gcc_files

cd ./target/mini_core/tests/basics/gcc_files

riscv-none-embed-gcc.exe -S -ffreestanding -march=rv32i -I ../../../../../app/defines ../../../../.././verif/mini_core/tests/basics.c -o basics_rv32i.c.s

riscv-none-embed-gcc.exe -O3 -march=rv32i -T ../../../../../app/link.common.ld -I ../../../../../app/defines -Wl,--defsym=I_MEM_OFFSET=0 -Wl,--defsym=I_MEM_LENGTH=65536 -Wl,--defsym=D_MEM_OFFSET=65536 -Wl,--defsym=D_MEM_LENGTH=61440 -nostartfiles -D__riscv__ -Wl,-Map=basics.map ../../../../../app/crt0_default.S basics_rv32i.c.s -o basics_rv32i.elf

riscv-none-embed-objdump.exe -gd basics_rv32i.elf > basics_rv32i_elf.txt

riscv-none-embed-objcopy.exe --srec-len 1 --output-target=verilog basics_rv32i.elf inst_mem.sv

cd C:/workspace/fpga_mafia