From bea0edfd10340eea0d759053bcf4188b4d7338ce Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Sun, 14 May 2017 14:06:17 +0200 Subject: [PATCH] Divise le classi, boilerplate --- cpp/a.out | Bin 8552 -> 0 bytes cpp/debug.h | 57 +++++++++++++++++----------------- cpp/emulator.cpp | 38 +++++++++++------------ cpp/instructions.h | 41 ------------------------- cpp/vm.cpp | 74 +++++++++++++++++++++++++++++++++++++++------ cpp/vm.h | 53 ++++++++++++++++++++++++++------ 6 files changed, 156 insertions(+), 107 deletions(-) delete mode 100755 cpp/a.out delete mode 100644 cpp/instructions.h diff --git a/cpp/a.out b/cpp/a.out deleted file mode 100755 index 2d21ebd3da6b9692e94187ae597da1cd70a92494..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8552 zcmeHMeQZo2fVZL#?UZB!K$`XyKG5JD>6Irp98 zJijjtVv!zF6`_YXjxna9M=a?TS+B@?LP6x2O8=;D^b9gS=nf%Ae255itM@!5 zSt|Q3nQ^M0A*YIlDrlSH0ECL(S5c2uSmNIkA2;{2zs1I-h2s}0^RQ(+Io#g1Wjx+G zo=oQ_TPHi(Tie^TTt?ff`c3vpc6Z-?<%!Hte13c==2iI68ld)Xeet#8<8R)W*!I)l z%Wv!*d?k4)Rzte-VD(cM^6Rf6{d2@tR>9XVg7+(I-dImhSFq>sXAtiuarB+C z++uAs7Bh0h1qqKyI8^6wQqg1@c1L|UcGSX@VYZ(p2BDvdVv~Bvm=e`A0ZHi&QTO2n(%+1_Wkqg47GMQy`g zL9B35KmH=gK8RtIyQ1O=-ygm&TquS2hX*4kH~y&JkDWW&N_3#NaJ9Gam&m3|YOcL2>lIhVfu_53?Dmk$nwzaAb69}FAko}733 z(K(Ec=3dP4h&^M5N3mVYPF{B+x_aW7=$J0nBk-m&{4ylh9C4G0OxQ_ON zYtrXhx4gdLNmsp(aQdyGq1j9VT&l|hY`}JsKZf!FlJ)v_d7JNPT=r1IG1$3w$CtKk zxSe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i!~Lbjy;l7DruqWBSc@?|fSfDsn3gIE{+wGLeL?Nrvj wUGxC^5AeX#cbt4kJo5da{Ux#6^}lzk#og~PE`CL}X%YQ5*+GLt!NJ160Q~KGF8}}l 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