In your rtl design, you may have a processor core where firmware runs and controls other rtl blocks. When you sim your design, one way is to bypass the processor core and use a Verilog BFM to drive/force processor core output data bus. Another way is to keep processor core in your rtl and you can run software-hardware cosim. In this case, c code is part of your TB. Test c code is first compiled into a file. Then when sim is running, processor core RTL automatically finds this file based on file name.
Taking Xilinx Microblaze as an example, you can use below to compile your test code, test_main.c, into an elf file, test_main.elf.
mb-gcc -L$PRJDIR/sdk/bsp/microblaze0/lib -Wl,-T -Wl,$PRJDIR/sdk/code/src/lscript.ld -mlittle-endian -mxl-barrel-shift -mxl-pattern-compare -mno-xl-soft-div -mcpu=v9.4 -mno-xl-soft-mul -mxl-multiply-high -mhard-float -mxl-float-convert -mxl-float-sqrt -Wl,–no-relax -Wl,–gc-sections -o test_main.elf -I$PRJDIR/tb/c test_main.c
test_main.c resides in your test folder so it is test specific. Other test cases have their own test_main.c but normally with different content. test_main.c may include test_main.h. Since test_main.h is normally common to all test cases, you can put it in your $PRJDIR/tb/c folder so it can be shared across multiple test cases. (Please check my other two posts for TB folder arrangement) In this case, you need to tell mb-gcc where to find this include folder with “-I$PRJDIR/tb/c”.
Above mb-gcc command actually is not generated manually. When you generate microblaze core with an hello world c program, the Xilinx tool will generate makefile for you and this mb-gcc is pulled out from there.
Now you compiled c and h code into elf file. Next you need to run:
data2mem -bm $PRJDIR/design_ip/mpc/mpc.sim/sim_1/synth/func/mpc_sim.bmm -bd test_main.elf -bx . tag microblaze_0 -u -p “<your_fpga_part_number>”
to convert elf into mem files. There could be multiple mem files since your microblaze may use multiple brams as instruction memory.
That is it. When you launch sim, microblaze rtl will find above generated mem files based on file names and run the program.