Flushing out instruction read
This commit is contained in:
parent
67f974cb75
commit
d4690a2575
132
vm.c
132
vm.c
|
@ -16,10 +16,16 @@ struct lilith
|
||||||
|
|
||||||
struct Instruction
|
struct Instruction
|
||||||
{
|
{
|
||||||
|
uint64_t ip;
|
||||||
uint8_t opcode;
|
uint8_t opcode;
|
||||||
uint32_t XOP;
|
uint32_t XOP;
|
||||||
uint32_t Immediate;
|
uint32_t Immediate;
|
||||||
uint32_t HAL_CODE;
|
uint32_t HAL_CODE;
|
||||||
|
uint8_t reg0;
|
||||||
|
uint8_t reg1;
|
||||||
|
uint8_t reg2;
|
||||||
|
uint8_t reg3;
|
||||||
|
bool invalid;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Allocate and intialize memory/state */
|
/* Allocate and intialize memory/state */
|
||||||
|
@ -62,6 +68,10 @@ void read_instruction(struct lilith* vm, struct Instruction *current)
|
||||||
memset(current, 0, sizeof(struct Instruction));
|
memset(current, 0, sizeof(struct Instruction));
|
||||||
uint8_t opcode, segment1, segment2, segment3;
|
uint8_t opcode, segment1, segment2, segment3;
|
||||||
|
|
||||||
|
/* Store IP for debugging */
|
||||||
|
current->ip = vm->ip;
|
||||||
|
|
||||||
|
/* Read the actual bytes and increment the IP */
|
||||||
opcode = vm->memory[vm->ip];
|
opcode = vm->memory[vm->ip];
|
||||||
vm->ip = vm->ip + 1;
|
vm->ip = vm->ip + 1;
|
||||||
segment1 = vm->memory[vm->ip];
|
segment1 = vm->memory[vm->ip];
|
||||||
|
@ -71,43 +81,93 @@ void read_instruction(struct lilith* vm, struct Instruction *current)
|
||||||
segment3 = vm->memory[vm->ip];
|
segment3 = vm->memory[vm->ip];
|
||||||
vm->ip = vm->ip + 1;
|
vm->ip = vm->ip + 1;
|
||||||
|
|
||||||
/* Deal with NOPs */
|
/* The first byte is always the master opcode */
|
||||||
if (0x0 == opcode)
|
current->opcode = opcode;
|
||||||
{
|
current->invalid = false;
|
||||||
current->opcode = 0;
|
current->XOP = 0xFFFF;
|
||||||
} /* Deal with illegal instruction */
|
current->Immediate = 0xFFFF;
|
||||||
else if (0xFF == opcode)
|
current->HAL_CODE = 0xFFFF;
|
||||||
{
|
current->reg0 = 0xFF;
|
||||||
current->opcode = opcode;
|
current->reg1 = 0xFF;
|
||||||
current->XOP = 0xFFFF;
|
current->reg2 = 0xFF;
|
||||||
current->Immediate = 0xFFFF;
|
current->reg3 = 0xFF;
|
||||||
current->HAL_CODE = 0xFFFF;
|
|
||||||
} /* Deal with 4OP */
|
|
||||||
else if (0x1 == (opcode/32))
|
|
||||||
{
|
|
||||||
} /* Deal with 3OP */
|
|
||||||
else if (0x2 == (opcode/32))
|
|
||||||
{
|
|
||||||
} /* Deal with 2OP */
|
|
||||||
else if (0x3 == (opcode/32))
|
|
||||||
{
|
|
||||||
} /* Deal with 1OP */
|
|
||||||
else if (0x4 == (opcode/32))
|
|
||||||
{
|
|
||||||
} /* Deal with 2OPI */
|
|
||||||
else if (0x5 == (opcode/32))
|
|
||||||
{
|
|
||||||
} /* Deal with 1OPI */
|
|
||||||
else if (0x6 == (opcode/32))
|
|
||||||
{
|
|
||||||
} /* Deal with 0OPI */
|
|
||||||
else if (0x7 == (opcode/32))
|
|
||||||
{
|
|
||||||
} /* Deal with Halcode */
|
|
||||||
else if (0x8 == (opcode/32))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* Extract the fields from the instruction for easier evaluation */
|
||||||
|
switch(opcode)
|
||||||
|
{
|
||||||
|
case 0x00: /* Deal with NOPs */
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x01 ... 0x04: /* Deal with 4OP */
|
||||||
|
{
|
||||||
|
current->XOP = segment1;
|
||||||
|
current->Immediate = 0;
|
||||||
|
current->reg0 = segment2/16;
|
||||||
|
current->reg1 = segment2%16;
|
||||||
|
current->reg2 = segment3/16;
|
||||||
|
current->reg3 = segment3%16;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x05 ... 0x08: /* Deal with 3OP */
|
||||||
|
{
|
||||||
|
current->XOP = segment1*16 + segment2/16;
|
||||||
|
current->Immediate = 0;
|
||||||
|
current->reg0 = segment2%16;
|
||||||
|
current->reg1 = segment3/16;
|
||||||
|
current->reg2 = segment3%16;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x09 ... 0x0C: /* Deal with 2OP */
|
||||||
|
{
|
||||||
|
current->XOP = segment1*256 + segment2;
|
||||||
|
current->Immediate = 0;
|
||||||
|
current->reg0 = segment3/16;
|
||||||
|
current->reg1 = segment3%16;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x0D: /* Deal with 1OP */
|
||||||
|
{
|
||||||
|
current->XOP = segment1*4096 + segment2*16 + segment3/16;
|
||||||
|
current->Immediate = 0;
|
||||||
|
current->reg0 = segment3%16;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x0E ... 0x2B: /* Deal with 2OPI */
|
||||||
|
{
|
||||||
|
current->XOP = 0;
|
||||||
|
current->Immediate = segment2*256 + segment3;
|
||||||
|
current->reg0 = segment1/16;
|
||||||
|
current->reg1 = segment1%16;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x2C ... 0x3B: /* Deal with 1OPI */
|
||||||
|
{
|
||||||
|
current->XOP = 0;
|
||||||
|
current->Immediate = (segment1%16)*4096 + segment2*256 + segment3;
|
||||||
|
current->HAL_CODE = 0;
|
||||||
|
current->reg0 = segment1/16;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x3C: /* Deal with 0OPI */
|
||||||
|
{
|
||||||
|
current->XOP = 0;
|
||||||
|
current->Immediate = segment1*4096 + segment2*256 + segment3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x42: /* Deal with Halcode */
|
||||||
|
{
|
||||||
|
current->XOP = 0;
|
||||||
|
current->HAL_CODE = segment1*4096 + segment2*256 + segment3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0xFF: /* Deal with illegal instruction */
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
current->invalid = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute_vm(struct lilith* vm)
|
void execute_vm(struct lilith* vm)
|
||||||
|
|
Loading…
Reference in New Issue