Verilog Mem File from Compiled ELF

Oct 22, 2019
I've seen many ways to convert a compiled ELF file (like a.out) to a Verilog Mem file.  Some of the ways to use Python or some custom converter program or shell script or ????.  What if you want to move between Linux and Window without have to recompile the converter program or install the heavyweight Python.

The trick is to use the SRecord program.  There are three steps

0. Install the SRecord program for Linux, Windows, etc.
1. Compile your program (make or whatever gets that ELF output)
2. Make a binary file with objcopy -O binary a.out aout.bin
3. Final convert is srec_cat aout.bin -binary -byte-swap 4 -O mem.init -Lattice_Memory_Initialization_Format  32 -Data_Only

Explanation of the srec_cat command
aout.bin: the binary from objcopy (use cross-compiler if needed)
-binary: format of the input file
-byte-swap 4: little-endian (leave out if big-endian)
-O mem.init: output file name (doesn't have to be mem.init)
-Lattice_Memory_Initialization_Format 32: seems to format the output correctly
-Data_Only: leave off the header information normally in the Lattice Mem format

This can all be done with a Makefile that can be used anywhere SRecord (srec_cat) command is available.