summaryrefslogtreecommitdiff
path: root/src/virtualmachine/VirtualMachine.cpp
blob: a5f661e7749c2a1081a21e6dc18fe1d05b2ee62c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include "VirtualMachine.h"

namespace vm
{

	VirtualMachine::VirtualMachine(int hunkMbytes):
		bigEndian(false),
		hunk(NULL),
		hunkSize(1048576),
		hunkFree(0),
		rom(NULL),
		romSize(0),
		ram(NULL),
		ramSize(0),
		dataStack(0),
		returnStack(0),
		DP(0),        /* Datastack pointer. */
		RP(0),        /* Return stack pointer. */
		PC(0),
		cm(0),
		cycles(0)
	{
		hunk = new char[hunkSize];
		std::fill(hunk, hunk+hunkSize, 0);
	}

	VirtualMachine::~VirtualMachine()
	{
		delete[] hunk;
	}

	#define DEBUGTRACE(args...) printf(args);

	int VirtualMachine::opcodeParameterSize(int opcode)
	{
		#define OP(n) OP##n
		switch (opcode)
		{
		case OP(ENTER):
		case OP(LEAVE):
		case OP(LOCAL):
		case OP(EQ):
		case OP(NE):
		case OP(LTI):
		case OP(LEI):
		case OP(GTI):
		case OP(GEI):
		case OP(LTU):
		case OP(LEU):
		case OP(GTU):
		case OP(GEU):
		case OP(EQF):
		case OP(NEF):
		case OP(LTF):
		case OP(LEF):
		case OP(GTF):
		case OP(GEF):
		case OP(CONST):
		case OP(BLOCK_COPY):
			return sizeof(uint4_t);
			break;
		case OP(ARG):
			return sizeof(uint1_t);
			break;
		}
		return 0;
		#undef OP
	}

	/* Read one octet from file. */
	int VirtualMachine::readByte(FILE *qvmfile)
	{
		int o;
		o = fgetc(qvmfile);
		if (o < 0) o = 0;  /* EOF (hack) */
			return o;
	}

	/* Read little-endian 32-bit integer from file. */
	int VirtualMachine::readInt(FILE *qvmfile)
	{
		int a, b, c, d, n;

		a = readByte(qvmfile);
		b = readByte(qvmfile);
		c = readByte(qvmfile);
		d = readByte(qvmfile);
		n = (a) | (b << 8) | (c << 16) | (d << 24);
		return n;
	}

	int VirtualMachine::LoadProgram(char * filename)
	{
		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. */
		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);
		qvminfo.dataoff = readInt(qvmfile);
		qvminfo.datalen = readInt(qvmfile);
		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);
		while (romSize < qvminfo.inscount)
		{
			n = readByte(qvmfile);
			w.int4 = 0;
			if ((i = opcodeParameterSize(n)))
			{
				x[0] = x[1] = x[2] = x[3] = 0;
				fread(&x, 1, i, qvmfile);
				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", 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)
			readByte(qvmfile);
		for (n = 0; n < (qvminfo.datalen / sizeof(uint1_t)); n++)
		{
			i = fread(&x, 1, sizeof(x), qvmfile);
			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 = fread(&x, 1, sizeof(x), qvmfile);
			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;
			returnStack = ramSize;
			dataStack = ramSize - (stacksize / 2);
			RP = returnStack;
			DP = dataStack;
		}

		/* set up PC for return-to-termination. */
		PC = romSize + 1;

		return 1;
	}

	int VirtualMachine::Call(int address)
	{
		word w;
		int i, argCount = 0;

		/* Set up call. */
		OpPUSH(w);
		DEBUGTRACE("Starting with PC=%d, DP=%d, RP=%d to %d\n", PC, DP, RP, address);
		w.int4 = (argCount + 2) * sizeof(word);
		OpENTER(w);
		i = 8;
		/**w.int4 = arg0; Marshal(i, w); i += 4;
		w.int4 = arg1; Marshal(i, w); i += 4;
		w.int4 = arg2; Marshal(i, w); i += 4;
		w.int4 = arg3; Marshal(i, w); i += 4;
		w.int4 = arg4; Marshal(i, w); i += 4;
		w.int4 = arg5; Marshal(i, w); i += 4;
		w.int4 = arg6; Marshal(i, w); i += 4;
		w.int4 = arg7; Marshal(i, w); i += 4;
		w.int4 = arg8; Marshal(i, w); i += 4;
		w.int4 = arg9; Marshal(i, w); i += 4;
		w.int4 = arg10; Marshal(i, w); i += 4;
		w.int4 = arg11; Marshal(i, w); i += 4;
		w.int4 = arg12; Marshal(i, w); i += 4;*/
		w.int4 = address;
		Push(w);
		OpCALL(w);
		DEBUGTRACE("Upon running PC=%d, DP=%d, RP=%d\n", PC, DP, RP);
		Run();
		DEBUGTRACE("At finish PC=%d, DP=%d, RP=%d\n", PC, DP, RP);
		w.int4 = (argCount + 2) * sizeof(word);
		OpLEAVE(w);
		PC = romSize + 1;
		return 0;
	}

	int VirtualMachine::Run()
	{
		bool running = true;
		int operation;
		word parameter;
		while(running)
		{
			cycles++;
			if(PC > romSize)
			{
				running = false;
				continue;
			}
			if (PC < 0)
			{
				syscall(PC);
				continue;
			}
			operation = rom[PC].Operation;
			parameter = rom[PC].Parameter;
			PC++;
			(this->*operations[operation])(parameter);
		}
		return 1;
	}



	int VirtualMachine::syscall(int trap)
	{
		int retval;
		word w;

		retval = 0;
		switch (trap)
		{
		#define TRAPDEF(n, f) case n: retval = trap##f(); break;
			#include "Syscalls.inl"
		#undef TRAPDEF
		}

		w = Pop();
		PC = w.int4;
		w.int4 = retval;
		Push(w);
		return 1;
	}
}