Embedding TEMU in a Simulator

Now that you know how to use TEMU in stand alone mode, we will look on how we can integrate TEMU in a simulator. We will do this by creating our own program for driving TEMU. It is easy enough to create the simulator that drives TEMU. In fact, the command line interface that was used in the previous section, is a relatively simple program that just invokes the TEMU API.

Create a new file in your tutorial directory named simulator.c This file will be compiled using the command:

$ cc simulator.c -I/opt/temu/2.2.0/include -L/opt/temu/2.2.0/lib \
     -lTEMUSupport -o simulator

And executed using:

$ LD_LIBRARY_PATH=/opt/temu/2.2.0/lib ./simulator

Then we can write the following program in that file:

#include <stdint.h>
#include "temu-c/Support/Init.h"
#include "temu-c/Support/CommandLine.h"
#include "temu-c/Support/Objsys.h"
#include "temu-c/Support/Loader.h"
#include "temu-c/Support/Cpu.h"

int
main(int argc, const char *argv[])
{
  // Initialise TEMU support library, this is mandatory, it will
  // among other things ensure you have a valid license.
  temu_initSupportLib();
  // Init path support will populate various TEMU paths based on the
  // location of the temu command. Note that this does not work in
  // case "temu" is a shell alias. Also "temu" must be visible in your
  // $PATH. If you do not want PATH to be modified, you can specify the
  // complete TEMU path here. The call to this function is optional but
  // it makes sure that the temu_execCommandFile() works without an
  // absolute path (e.g. /opt/temu/2.2.0/share/temu/sysconfig/leon3.temu)
  temu_initPathSupport("temu");
  // Create a system based on the bundled LEON3 configuration.
  temu_execCommandFile("leon3.temu");

  // cpu0 is created by the leon3.temu script
  void *cpu0 = temu_objectForName("cpu0");
  // You can load an image to any object implementing MemoryIface
  temu_loadImage(cpu0, "rtems-hello.prom");
  // Run CPU for the given number of cycles
  temu_cpuRun(cpu0, UINT64_C(1000000000));
  return 0;
}

When you run the program above, you should see the same output as in the previous section.

Now you know the basics of using the TEMU APIs and compiling. We will move on to plug-in development in the next couple of sections.