diff --git a/cpp/a.out b/cpp/a.out deleted file mode 100755 index 2d21ebd..0000000 Binary files a/cpp/a.out and /dev/null differ diff --git a/cpp/debug.h b/cpp/debug.h index c21572b..381411a 100644 --- a/cpp/debug.h +++ b/cpp/debug.h @@ -1,32 +1,33 @@ #ifndef DBG_H #define DBG_H +#include + +#if DBG +#define DBG_INFO(_x_) \ + do { \ + printf("\t[*] "); \ + printf _x_; \ + } while (0) +#define DBG_WARN(_x_) \ + do { \ + printf("[!] "); \ + printf _x_; \ + } while (0) +#define DBG_ERROR(_x_) \ + do { \ + printf("[-] "); \ + printf _x_; \ + } while (0) +#define DBG_SUCC(_x_) \ + do { \ + printf("[+] "); \ + printf _x_; \ + } while (0) +#else +#define DBG_INFO(_x_) +#define DBG_WARN(_x_) +#define DBG_ERROR(_x_) +#define DBG_SUCC(_x_) +#endif - #if DBG - #define DBG_INFO(_x_) \ - do { \ - printf("\t[*] "); \ - printf _x_; \ - } while (0) - #define DBG_WARN(_x_) \ - do { \ - printf("[!] "); \ - printf _x_; \ - } while (0) - #define DBG_ERROR(_x_) \ - do { \ - printf("[-] "); \ - printf _x_; \ - } while (0) - #define DBG_SUCC(_x_) \ - do { \ - printf("[+] "); \ - printf _x_; \ - } while (0) - #else - #define DBG_INFO(_x_) - #define DBG_WARN(_x_) - #define DBG_ERROR(_x_) - #define DBG_SUCC(_x_) - #endif - #endif \ No newline at end of file diff --git a/cpp/emulator.cpp b/cpp/emulator.cpp index 75f045d..7aed252 100644 --- a/cpp/emulator.cpp +++ b/cpp/emulator.cpp @@ -1,26 +1,26 @@ -#include -#include -#include -#include -#include "instructions.h" -#include "vm.h" #include "debug.h" +#include "vm.h" +#include +#include +#include +#include using namespace std; int main() { - ifstream vmbytecode; - VM * vm; - uint8_t * data; + ifstream vmbytecode; + VM vm; + uint8_t *data; - - /* - if (vmbytecode != NULL) { - fread() - } else { - fprintf(stderr, "Couldn't open bytecode!\n"); - return 1; - } - */ - return 0; + vm.status(); + // vm.status(); + /* + if (vmbytecode != NULL) { + fread() + } else { + fprintf(stderr, "Couldn't open bytecode!\n"); + return 1; + } + */ + return 0; } \ No newline at end of file diff --git a/cpp/instructions.h b/cpp/instructions.h deleted file mode 100644 index 0303886..0000000 --- a/cpp/instructions.h +++ /dev/null @@ -1,41 +0,0 @@ -#include "vm.h" -#ifndef INS_H -#define INS_H -enum regs { - R0, - R1, - R2, - R3, - S0, - S1, - S2, - S3, - IP, - BP, - SP -}; - -enum ins { - MOVI, - MOVR, - MOVM, - ADDI, - ADDR, - ADDM, - SUBI, - SUBR, - SUBM, - XORI, - XORR, - XORM, - MULI, - MULR, - MULM, - DIVI, - DIVR, - DIVM, - HALT, - NOPE -}; - -#endif \ No newline at end of file diff --git a/cpp/vm.cpp b/cpp/vm.cpp index daf23ad..2ec5f0e 100644 --- a/cpp/vm.cpp +++ b/cpp/vm.cpp @@ -1,15 +1,71 @@ -#include "vm.hh" +#include "vm.h" #include "debug.h" +#include "vmas.h" +#include -void VM::VM(void) { - DBG_INFO(("Creating VM without code.\n")); +VM::VM() { + DBG_INFO(("Creating VM without code.\n")); + init_regs(); } -void VM::VM(uint8_t * code, uint32_t codesize) { - DBG_INFO(("Creating VM with code.\n")); - memcpy(&vm.as.code, code, codesize); +VM::VM(uint8_t *code, uint32_t codesize) { + DBG_INFO(("Creating VM with code.\n")); + init_regs(); + as.insCode(code, codesize); } -void VM::run(uint8_t * code) { - return; -} \ No newline at end of file +void VM::init_regs(void) { + uint8_t i; + + for (i = R0; i <= SP; i++) { + this->regs[i] = 0; + } + return; +} + +void VM::status(void) { + uint8_t i; + DBG_INFO(("VM Status:\n")); + DBG_INFO(("~~~~~~~~~~\n")); + for (i = R0; i <= SP; i++) { + switch (i) { + case R0: + DBG_INFO(("R0:\t0x%x\n", this->regs[i])); + break; + case R1: + DBG_INFO(("R1:\t0x%x\n", this->regs[i])); + break; + case R2: + DBG_INFO(("R2:\t0x%x\n", this->regs[i])); + break; + case R3: + DBG_INFO(("R3:\t0x%x\n", this->regs[i])); + break; + case S0: + DBG_INFO(("S0:\t0x%x\n", this->regs[i])); + break; + case S1: + DBG_INFO(("S1:\t0x%x\n", this->regs[i])); + break; + case S2: + DBG_INFO(("S2:\t0x%x\n", this->regs[i])); + break; + case S3: + DBG_INFO(("S3:\t0x%x\n", this->regs[i])); + break; + case IP: + DBG_INFO(("IP:\t0x%x\n", this->regs[i])); + break; + case BP: + DBG_INFO(("BP:\t0x%x\n", this->regs[i])); + break; + case SP: + DBG_INFO(("SP:\t0x%x\n", this->regs[i])); + break; + } + } + DBG_INFO(("~~~~~~~~~~\n")); + return; +} + +void VM::run(uint8_t *code) { return; } \ No newline at end of file diff --git a/cpp/vm.h b/cpp/vm.h index 1bfdd35..0316583 100644 --- a/cpp/vm.h +++ b/cpp/vm.h @@ -1,20 +1,53 @@ #ifndef VM_H #define VM_H +#include +#include "vmas.h" -class VMAddrSpace { - uint8_t stack[0x100], code[0x300], data[0x500]; +enum regs { R0, R1, R2, R3, S0, S1, S2, S3, IP, BP, SP }; + +enum ins { + MOVI, + MOVR, + MOVM, + ADDI, + ADDR, + ADDM, + SUBI, + SUBR, + SUBM, + XORI, + XORR, + XORM, + NOTI, + NOTR, + NOTM, + MULI, + MULR, + MULM, + DIVI, + DIVR, + DIVM, + PUSH, + POOP, + CALL, + HALT, + NOPE }; class VM { - uint16_t regs[0xb]; - struct flags { - uint8_t zf:1; - uint8_t cf:1; - }; - VMAddrSpace as; + uint16_t regs[0xb]; + struct flags { + uint8_t zf : 1; + uint8_t cf : 1; + }; + VMAddrSpace as; - public: - void run(uint8_t * code); +public: + VM(); + VM(uint8_t *code, uint32_t codesize); + void init_regs(void); + void status(void); + void run(uint8_t *code); }; #endif \ No newline at end of file