VM – Registers

All CPU’s, even VM’s have a set of global variables referred to as “Registers”. These are the internal data used to execute instructions. Our VM is no exception, but the registers we need differ from instruction to instruction due to their high level nature.

“Registers” in this context is variables in our VM struct. The variables needed for an instruction is stored on the C stack. I will in general not use recursive calls, but it is an option that we use recursive calls on instructions like for, while etc since they otherwise needs to be decoded for every iteration. This is however optimization that I will look into later.

The capability to execute expressions require a math table. As this table will be of some size I need to minimize the footprint impact. One way of doing that is to take into account that we will be only executing one instruction at the time regardless of how many VM’s we have, so I can get away with a single, static table. This is not optional for performance, but it is required due to SRAM usage. The actual C code struct is listed below:

typedef struct _MathEntry
   unsigned mathOp:4;
   unsigned t1:4;
   unsigned t2:4;
   unsigned tix:4;
   unsigned do1:16;
   unsigned do2:16;

I am a bit unsure about this one as I have 48 bits per entry. It might very well be that performance will be better if I use 64 bits since we use a 32 bit computer – I will test this one and see if it matters.

Leave a Reply