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.
Emulator code - constant nil
Started by kenna, Nov 29 2007 09:25 PM
4 replies to this topic
#1
Posted 29 November 2007 - 09:25 PM
|
|
|
#2
Posted 30 November 2007 - 09:33 AM
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;
}
#3
Posted 30 November 2007 - 11:09 AM
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^
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
Posted 02 December 2007 - 02:19 PM
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]
[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
Posted 03 December 2007 - 01:48 AM
Thanks! That is even better! It greatly reduces the number of procedure calls ^____^


Sign In
Create Account


Back to top









