Jump to content

Emulator code - constant nil

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
kenna

kenna

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
I have a small problem with some emulator code that I'm writing. I have 16 general purpose registers, but just like in MIPS, register 0 needs to be constant nil.

This is what I use for the registers, so the operand codes in the instruction translates directly into a certain register. However, r[0] needs to be constant nil, which I'm having problem accomplishing.

unsigned int r[16] = {
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
};

I could just have an array of 16 pointers, where 0 points to "const unsigned int" and the 15 others to "unsigned int". However, would this be considered bad programming?

I myself am not very fond of unnecessary warnings, and discarding qualifiers doesn't seem like a good idea. Anyway, I don't know exactly what happens if I use that method — i.e. what implications it has — so I don't want to use it right now.

EDIT: Nor do I want some horribly large procedure to handle this; which surely would be easy, just not very efficient.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What I would probably do is have a function for performing assignments to registers:
void update(int register,value)
{
  if register != 0 then
    r[register] = value
  else
    throw exception;
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
kenna

kenna

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Thank you! That would work like a charm ^_^

Must be because I've hardly had any sleep that I'm not able to think clearly...to imagine it was such a simple answer ^O^

#4
jbakid

jbakid

    Newbie

  • Members
  • Pip
  • 6 posts
Here's a useful macro: (for reads) (uses variable "trash" which must be defined);
[HIGHLIGHT="C"]
#define RREG(_rnum) (_rnum?register[_rnum]:0)[/HIGHLIGHT]

for writes:
[HIGHLIGHT="C"]
#define WREG(_rnum) (_rnum?register+rnum:trash)[/HIGHLIGHT]

used as:
[HIGHLIGHT="C"]
x = RREG(12); /* Assign register 12's value to x *;/
WREG(2) = x; /* Register 2 is now x */[/HIGHLIGHT]

#5
kenna

kenna

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Thanks! That is even better! It greatly reduces the number of procedure calls ^____^