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 zcmb<-^>JfjWMqH=CI&kO5bpzv16T+`GBCW51arZJ1A_$vCxZin9D^(a8v_FaD+2=q zOq~Oi1*3m}3}awmfYBUa6Brnn85kH?7#J8Vm>>ccOb`=bvW&O34bunWg7kfW>iYoI2csE4 z3P2`7X_)^&egokUsCWp}e=ynwq=$il0Y-z=f`kH|mZX5#J3y8)Ffi<3gMIpOcwnW}=^yqMMVMS6ZQ4 zVPU3gW};V|uV(~~SCIK2weEhQAm!j-0=W+q8z47}FhJ4(NWM`0%7>o&{|b!f7@oN2 zA96bLX)+5)FAReXAd&#t3$g=U`FsWj1|f7o1_lOs9O8aB#2IojlZs1HlC`x#el=r= zk5A9d&x`h{}0mmVbfoP9||Cl{GpL%qD@HqIw-h=U&$HjjoqHxo=VW#oRGsGTF z1KZzVJCRF)fuU3|)T8t1i@g8;|HmFaQ)VEhcWY5xmeki+a5N?Bh|=9g!H8}<>T z;s1r$msS7&{|B*yJsRIgFflNA^tL|u`~Sa3=kXWk|NZ~J8{}*xvD5$l|L?rv(Rc); zHahk&!Yg3+IEFihId=Yc40R0g=~bP^uD}rL(W?uhgFQRHd3Ij)(R>PytAl@;U%3DK z|KFqaZ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v`1qvaVwhu7K`v(i`H+D@uedU|Br%CWuehWLLTA8O znRz9tMFk9cdHE%&dX7$>x+RI}P+odosa{fPW=@K3W(tEIh?kLAoWY=%Qkhp=nG2yy ziXbv&sYS(^`FSXu_#y_qqSTy3kOnBLAg6>u5A0UGlGKV420c&;WY8*9NaBO&fU&cZi@?rE%*{+@&`ZxR0TX&)TOcMT6&Ew;CFkenrskD^av)_G z)Q$%g!LafY)_#M{-@w}OFk3)sVQdhs%)r0^YCFU9!`gYU`4Le43Q`Bdu<{YcH$>A9 zYY&z{4FrwH!PJ6ibp4=lwr~Ic=R=jl+l@1z0x8h( zRGp*fKF40R=b% z0|Ttz2vZL=7RF^*0M!o*e^`5e15`gO++iBf^@GO4K~99}hs`I#<{3d_=`g(@8eRW3 zH2Y!whYwKuzkoC$VOadZgbpw;Fz`Y107wkhj)ToRqN|7TVe}aW1_pj4{jmPX1Lyz* zOdZH<5Qgz#^bM%{q1grAZ+SsLKgcW)zRLhfdoca5{tawi7(BrVRRdBFvlo^Qo}t+f zn=ktS@)Xj%8b~h)!}P=G*J%3T{UxXc8X!$b7^V+KgW?Ai|1kH%`c)cG{ZI;S0!$xF z;1>e}14s;}AJ+e}fNBKE!PJ6ikX;}ehCv-LkQjPCgXu@_xq;Mz^di%+vR2 F0Ra5%dM^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