From d4690a2575083f928b5e696c9f1329a6123eb3da Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Sun, 29 May 2016 08:38:54 -0400 Subject: [PATCH] Flushing out instruction read --- vm.c | 132 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 96 insertions(+), 36 deletions(-) diff --git a/vm.c b/vm.c index dc6fa6b..85892ca 100644 --- a/vm.c +++ b/vm.c @@ -16,10 +16,16 @@ struct lilith struct Instruction { + uint64_t ip; uint8_t opcode; uint32_t XOP; uint32_t Immediate; uint32_t HAL_CODE; + uint8_t reg0; + uint8_t reg1; + uint8_t reg2; + uint8_t reg3; + bool invalid; }; /* Allocate and intialize memory/state */ @@ -62,6 +68,10 @@ void read_instruction(struct lilith* vm, struct Instruction *current) memset(current, 0, sizeof(struct Instruction)); 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]; vm->ip = vm->ip + 1; segment1 = vm->memory[vm->ip]; @@ -71,43 +81,93 @@ void read_instruction(struct lilith* vm, struct Instruction *current) segment3 = vm->memory[vm->ip]; vm->ip = vm->ip + 1; - /* Deal with NOPs */ - if (0x0 == opcode) - { - current->opcode = 0; - } /* Deal with illegal instruction */ - else if (0xFF == opcode) - { - current->opcode = opcode; - current->XOP = 0xFFFF; - current->Immediate = 0xFFFF; - 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)) - { - } + /* The first byte is always the master opcode */ + current->opcode = opcode; + current->invalid = false; + current->XOP = 0xFFFF; + current->Immediate = 0xFFFF; + current->HAL_CODE = 0xFFFF; + current->reg0 = 0xFF; + current->reg1 = 0xFF; + current->reg2 = 0xFF; + current->reg3 = 0xFF; + /* 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)