Ok so while I'm debugging I'm just going to feed the VM a vector of CInstruction's instead of writing an assembler right now. This function takes one such vector and writes the elements into the memory allocated by AllocateBlock (I took major_small & everyone elses advice about the memory allocation BTW).
Code:
void CVM::LoadProgramVec(vector<CInstruction>& program)
{
vector<CInstruction>::iterator iter = program.begin();
word* write = AllocateBlock(program.size());
if (! write) throw VMPLoadFailNoMem();
for (; iter != program.end(); iter++)
++*write = *iter->opcode;
DebugBlock(write, program.size(), false);
}
DebugBlock just dumps the memory to the screen so I can, you know, see it. This all works fine except the opcodes are getting written in backwards. Here's my main which gives it two instructions:
Code:
// Eww, but buggered if I'm writing this out once per instruction while debugging
#define INST(i, op1, op2) *inst.opcode = i; inst.operands[0] = op1; inst.operands[1] = op2; program.push_back(inst)
void Error(const char* msg);
int main(int argc, char* argv[])
{
CVM* vm = new CVM();
vector<CInstruction> program;
CInstruction inst;
try {
vm->Initialise();
} catch (VMInitFail) {
Error("vm->Initialise() threw VMInitFail");
return -1;
}
INST(0, 0, 0);
INST(1, 0, 2);
try {
vm->LoadProgramVec(program);
} catch (VMPLoadFailNoMem) {
Error("Program load failed - memory could not be allocated");
}
vm->Shutdown();
delete vm;
return 0;
}
Output:
Code:
1 0
Press any key to continue
Unless I'm misunderstanding something, shouldn't that be "0 1" ?
Oh, not sure how relevant this is, but:
Code:
class CInstruction {
public:
CInstruction(void);
word* opcode;
word* operands;
};
opcode and operands are new'd in the constructor.