diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-09-14 21:03:14 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-09-14 21:03:14 (GMT) |
| commit | 486b34ebe581cf41b67145911a64ce2ea7b8932a (patch) | |
| tree | 6752bc0bb7245bbd5009061b504f03b02790b58b /src/virtualmachine/VirtualMachine.cpp | |
| parent | 685be24ffa1eba28aa378b8a9b4fd52dbe238756 (diff) | |
| download | powder-486b34ebe581cf41b67145911a64ce2ea7b8932a.zip powder-486b34ebe581cf41b67145911a64ce2ea7b8932a.tar.gz | |
Fixes invalid text pasting, Adds API to VM, allow program loading in Lua and assigning to update function
Diffstat (limited to 'src/virtualmachine/VirtualMachine.cpp')
| -rw-r--r-- | src/virtualmachine/VirtualMachine.cpp | 167 |
1 files changed, 142 insertions, 25 deletions
diff --git a/src/virtualmachine/VirtualMachine.cpp b/src/virtualmachine/VirtualMachine.cpp index 9de822d..7d1af8e 100644 --- a/src/virtualmachine/VirtualMachine.cpp +++ b/src/virtualmachine/VirtualMachine.cpp @@ -74,46 +74,167 @@ namespace vm } /* Read one octet from file. */ - int VirtualMachine::readByte(FILE *qvmfile) + int VirtualMachine::readByte(std::istream & input) { int o; - o = fgetc(qvmfile); + o = input.get(); if (o < 0) o = 0; /* EOF (hack) */ return o; } /* Read little-endian 32-bit integer from file. */ - int VirtualMachine::readInt(FILE *qvmfile) + int VirtualMachine::readInt(std::istream & input) { int a, b, c, d, n; - a = readByte(qvmfile); - b = readByte(qvmfile); - c = readByte(qvmfile); - d = readByte(qvmfile); + a = readByte(input); + b = readByte(input); + c = readByte(input); + d = readByte(input); n = (a) | (b << 8) | (c << 16) | (d << 24); return n; } + int VirtualMachine::readProgram(std::istream & input) + { + qvm_header_t qvminfo; + int i, n; + uint1_t x[4]; + word w; + + DEBUGTRACE("Loading file...\n"); + qvminfo.magic = readInt(input); /* magic. */ + if (qvminfo.magic != QVM_MAGIC) + { + DEBUGTRACE("Invalid magic"); + throw InvalidProgramException(); + //q3vm_error("Does not appear to be a QVM file."); + /* XXX: option to force continue. */ + return 0; + } + DEBUGTRACE("Magic OK\n"); + /* variable-length instructions mean instruction count != code length */ + qvminfo.inscount = readInt(input); + qvminfo.codeoff = readInt(input); + qvminfo.codelen = readInt(input); + qvminfo.dataoff = readInt(input); + qvminfo.datalen = readInt(input); + qvminfo.litlen = readInt(input); + qvminfo.bsslen = readInt(input); + + /* Code segment should follow... */ + /* XXX: use fseek with SEEK_CUR? */ + DEBUGTRACE("Searching for .code @ %d from %d\n", qvminfo.codeoff, input.tellg()); + + // rom = (q3vm_rom_t*)(hunk); /* ROM-in-hunk */ + rom = (Instruction*)calloc(qvminfo.inscount, sizeof(rom[0])); + while (input.tellg() < qvminfo.codeoff) + readByte(input); + while (romSize < qvminfo.inscount) + { + n = readByte(input); + w.int4 = 0; + if ((i = opcodeParameterSize(n))) + { + x[0] = x[1] = x[2] = x[3] = 0; + input.readsome((char*)x, 4); + w.uint4 = (x[0]) | (x[1] << 8) | (x[2] << 16) | (x[3] << 24); + } + rom[romSize].Operation = n; + rom[romSize].Parameter = w; + romSize++; + } + DEBUGTRACE("After loading code: at %d, should be %d\n", input.tellg(), qvminfo.codeoff + qvminfo.codelen); + + /* Then data segment. */ + // ram = hunk + ((romlen + 3) & ~3); /* RAM-in-hunk */ + ram = hunk; + DEBUGTRACE("Searching for .data @ %d from %d\n", qvminfo.dataoff, input.tellg()); + while (input.tellg() < qvminfo.dataoff) + readByte(input); + for (n = 0; n < (qvminfo.datalen / sizeof(uint1_t)); n++) + { + i = input.readsome((char*)x, sizeof(x)); + w.uint4 = (x[0]) | (x[1] << 8) | (x[2] << 16) | (x[3] << 24); + *((word*)(ram + ramSize)) = w; + ramSize += sizeof(word); + } + + /* lit segment follows data segment. */ + /* Assembler should have already padded properly. */ + DEBUGTRACE("Loading .lit\n"); + for (n = 0; n < (qvminfo.litlen / sizeof(uint1_t)); n++) + { + i = input.readsome((char*)x, sizeof(x)); + memcpy(&(w.uint1), &x, sizeof(x)); /* no byte-swapping. */ + *((word*)(ram + ramSize)) = w; + ramSize += sizeof(word); + } + /* bss segment. */ + DEBUGTRACE("Allocating .bss %d (%X) bytes\n", qvminfo.bsslen, qvminfo.bsslen); + /* huge empty chunk. */ + ramSize += qvminfo.bsslen; + + hunkFree = hunkSize - ((ramSize * sizeof(uint1_t)) + 4); + + DEBUGTRACE("VM hunk has %d of %d bytes free (RAM = %d B).\n", hunkFree, hunkSize, ramSize); + if (ramSize > hunkSize) + { + throw OutOfMemoryException(); + return 0; + } + + /* set up stack. */ + { + int stacksize = 0x10000; + dataStack = ramSize - (stacksize / 2); + returnStack = ramSize; + //returnStack = dataStack+4; + RP = returnStack; + DP = dataStack; + } + + /* set up PC for return-to-termination. */ + PC = romSize + 1; + + ramMask = ramSize; + + return 1; + } + + int VirtualMachine::LoadProgram(std::vector<char> data) + { + /*class vectorwrapbuf : public std::basic_streambuf<char, std::char_traits<char> > + { + public: + vectorwrapbuf(std::vector<char> &vec) { + setg(vec.data(), vec.data(), vec.data() + vec.size()); + } + }; + vectorwrapbuf databuf(data); + std::istream is(&databuf); + return readProgram(is);*/ + std::stringstream ss(std::string(data.begin(), data.end())); + return readProgram((std::istream &)ss); + } + int VirtualMachine::LoadProgram(char * filename) { - FILE * qvmfile = fopen(filename, "rb"); + /*FILE * qvmfile = fopen(filename, "rb"); qvm_header_t qvminfo; int i, n; uint1_t x[4]; word w; DEBUGTRACE("Loading file...\n"); - qvminfo.magic = readInt(qvmfile); /* magic. */ + qvminfo.magic = readInt(qvmfile); if (qvminfo.magic != QVM_MAGIC) { DEBUGTRACE("Invalid magic"); - //q3vm_error("Does not appear to be a QVM file."); - /* XXX: option to force continue. */ return 0; } DEBUGTRACE("Magic OK\n"); - /* variable-length instructions mean instruction count != code length */ + qvminfo.inscount = readInt(qvmfile); qvminfo.codeoff = readInt(qvmfile); qvminfo.codelen = readInt(qvmfile); @@ -122,10 +243,9 @@ namespace vm qvminfo.litlen = readInt(qvmfile); qvminfo.bsslen = readInt(qvmfile); - /* Code segment should follow... */ - /* XXX: use fseek with SEEK_CUR? */ + DEBUGTRACE("Searching for .code @ %d from %d\n", qvminfo.codeoff, ftell(qvmfile)); - // rom = (q3vm_rom_t*)(hunk); /* ROM-in-hunk */ + rom = (Instruction*)calloc(qvminfo.inscount, sizeof(rom[0])); while (ftell(qvmfile) < qvminfo.codeoff) readByte(qvmfile); @@ -145,8 +265,7 @@ namespace vm } DEBUGTRACE("After loading code: at %d, should be %d\n", ftell(qvmfile), qvminfo.codeoff + qvminfo.codelen); - /* Then data segment. */ - // ram = hunk + ((romlen + 3) & ~3); /* RAM-in-hunk */ + ram = hunk; DEBUGTRACE("Searching for .data @ %d from %d\n", qvminfo.dataoff, ftell(qvmfile)); while (ftell(qvmfile) < qvminfo.dataoff) @@ -159,19 +278,17 @@ namespace vm ramSize += sizeof(word); } - /* lit segment follows data segment. */ - /* Assembler should have already padded properly. */ + DEBUGTRACE("Loading .lit\n"); for (n = 0; n < (qvminfo.litlen / sizeof(uint1_t)); n++) { i = fread(&x, 1, sizeof(x), qvmfile); - memcpy(&(w.uint1), &x, sizeof(x)); /* no byte-swapping. */ + memcpy(&(w.uint1), &x, sizeof(x)); *((word*)(ram + ramSize)) = w; ramSize += sizeof(word); } - /* bss segment. */ + DEBUGTRACE("Allocating .bss %d (%X) bytes\n", qvminfo.bsslen, qvminfo.bsslen); - /* huge empty chunk. */ ramSize += qvminfo.bsslen; hunkFree = hunkSize - ((ramSize * sizeof(uint1_t)) + 4); @@ -183,7 +300,7 @@ namespace vm return 0; } - /* set up stack. */ + { int stacksize = 0x10000; dataStack = ramSize - (stacksize / 2); @@ -193,12 +310,12 @@ namespace vm DP = dataStack; } - /* set up PC for return-to-termination. */ + PC = romSize + 1; ramMask = ramSize; - return 1; + return 1;*/ } void VirtualMachine::End() |
