Micro Virtual Machine - A weekend project to build a virtual machine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

91 lines
2.0 KiB

// This file is part of uVM.
//
// uVM is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// uVM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with uVM. If not, see <http://www.gnu.org/licenses/>.
//
#include <vector>
#ifndef CPU_H_
#define CPU_H_
// CPU is a wrapper around our representation of a virtual CPU.
class CPU {
private:
int sp = -1; // Stack pointer
int ip = 0; // Instruction pointer
int fp; // Frame pointer
std::vector<int> code; // Our code in memory
int *stack = NULL;
// XXX: FIND A C++ TEST FRAMEWORK OMG LOL
public:
enum opcode {
IPUSH = 0x0,
ISAVE0 = 0x1,
ISAVE1 = 0x2,
ISAVE2 = 0x3,
ILOAD0 = 0x4,
ILOAD1 = 0x5,
ILOAD2 = 0x6,
CMP = 0x7,
IADD = 0x8,
ISUB = 0x9,
JMP = 0xA,
JC = 0xB,
HALT = 0xC,
PRINT = 0xD,
IMUL = 0xE,
CALL = 0xF, // XXX: IMPLEMENT
IDIV = 0x10,
IREM = 0x11,
};
// Temporary registers
int t0;
int t1;
int t2;
// Comparison flag register
int cf;
const char* opcode_map[18] = {
"IPUSH",
"ISAVE0",
"ISAVE1",
"ISAVE2",
"ILOAD0",
"ILOAD1",
"ILOAD2",
"CMP",
"IADD",
"ISUB",
"JMP",
"JC",
"HALT",
"PRINT",
"IMUL",
"CALL",
"IDIV",
"IREM",
};
CPU(unsigned int);
~CPU();
bool load(std::vector<int> code);
void run();
};
#endif // CPU_H_