Skip to content

CPU Library

nepx edited this page Dec 25, 2020 · 2 revisions

The CPU emulator was designed to be turned into its own standalone module. It's relatively easy to separate the CPU into its own separate shared library. Integrating that into your own programs is a different story, though.

I'm writing this page because I'm currently using the CPU library in another project and I'd like to detail the steps that were needed to get it to work.

Compiling

You can easily compile the library by running the following command, which will produce the file libhalfix.so.

node makefile.js libcpu --output libhalfix.so

I assume that you can easily do the same on Windows and convert it into a DLL file, but I haven't tested this.

If you'd like to integrate the library into your own project (i.e. libhalfix.a), use the following command:

node makefile.js libcpu release --output libhalfix.a --cc ar

This will produce an archive of all the CPU-related files.

API

The complete collection of functions that you can use is in include/cpu/libcpu.h. Copy (or symbolically link) the file to wherever it needs to be.

Linking

Add -L. -l:./libhalfix.so to your build linker. The ./libhalfix.so part is critical, or else ld.so can't find the file. This took me too long to find out.

Usage

Call libcpu_init first.

FAQ

Are instrumentation callbacks supported?

If you mean the instrumentation callbacks in src/cpu/instrument.h, no. There is a way to reroute data that would otherwise go to (non-existent) devices to you own callbacks, however. Of course, you can always compile with instrumentation enabled, but that isn't something that comes along with libcpu.

Static Library

You will have to implement a number of function calls yourself. src/cpu/libcpu-stubs.c has a few stubs that you can copy into your own program and implement yourself.

The benefit of using a static library is that calls are faster, there's one less dependency to worry about, and it integrates better since many of Halfix's functions call outwards into your own program.

Clone this wiki locally