summaryrefslogtreecommitdiff
path: root/src/pim/Machine.h
blob: a40dd51dc75f8947c644b1727d019279f6ec893b (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
#pragma once

#include <vector>
#include <string>

class Simulation;
namespace pim
{
	union Word
	{
		int Integer;
		float Decimal;

		Word(int integer) : Integer(integer) {}
		Word(float decimal) : Decimal(decimal) {}
		Word() {}
	};
	struct Instruction
	{
		int Opcode;
		Word Parameter;
	};
	class VirtualMachine
	{

		#define WORDSIZE 4

		//#define OPDEF(name) void op##name(int parameter);
		//#include "Opcodes.inl"
		//#undef OPDEF

		Simulation * sim;

		Instruction * rom;
		int romSize;
		int romMask;

		unsigned char * ram;
		int ramSize;
		int ramMask;

		#define CSA(argument) (*((Word*)&ram[framePointer-argument]))
		#define CS() (*((Word*)&ram[callStack]))
		#define PS() (*((Word*)&ram[programStack]))
		#define PPROP(index, property) (*((Word*)(&sim->parts[(index)]+property)))

		int programStack;	//Points to the item on top of the Program Stack
		int callStack;		//Points to the item on top of the call stack
		int framePointer;	//Points to the bottom (first item) on the current frame of the call stack

		//Instruction * instructions;

		int programCounter;


	public:
		VirtualMachine(Simulation * sim);
		int OpcodeArgSize(int opcode);
		void LoadProgram(std::vector<unsigned char> programData);
		void Run();
		void Call(std::string entryPoint);
		void Call(int entryPoint);
		inline void PSPush(Word word)
		{
			programStack -= WORDSIZE;
			PS() = word;
		}

		inline Word PSPop()
		{
			Word word = PS();
			programStack += WORDSIZE;
			return word;
		} 

		inline void CSPush(Word word)
		{
			callStack -= WORDSIZE;
			CS() = word;
		}

		inline Word CSPop()
		{
			Word word = CS();
			callStack += WORDSIZE;
			return word;
		} 
	};
}