Divise le classi, boilerplate

This commit is contained in:
Giulio De Pasquale 2017-05-14 14:06:17 +02:00
parent 16d57bf64b
commit bea0edfd10
6 changed files with 156 additions and 107 deletions

BIN
cpp/a.out

Binary file not shown.

View File

@ -1,32 +1,33 @@
#ifndef DBG_H #ifndef DBG_H
#define DBG_H #define DBG_H
#include <stdio.h>
#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 #endif

View File

@ -1,26 +1,26 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fstream>
#include "instructions.h"
#include "vm.h"
#include "debug.h" #include "debug.h"
#include "vm.h"
#include <fstream>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std; using namespace std;
int main() { int main() {
ifstream vmbytecode; ifstream vmbytecode;
VM * vm; VM vm;
uint8_t * data; uint8_t *data;
vm.status();
/* // vm.status();
if (vmbytecode != NULL) { /*
fread() if (vmbytecode != NULL) {
} else { fread()
fprintf(stderr, "Couldn't open bytecode!\n"); } else {
return 1; fprintf(stderr, "Couldn't open bytecode!\n");
} return 1;
*/ }
return 0; */
return 0;
} }

View File

@ -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

View File

@ -1,15 +1,71 @@
#include "vm.hh" #include "vm.h"
#include "debug.h" #include "debug.h"
#include "vmas.h"
#include <string.h>
void VM::VM(void) { VM::VM() {
DBG_INFO(("Creating VM without code.\n")); DBG_INFO(("Creating VM without code.\n"));
init_regs();
} }
void VM::VM(uint8_t * code, uint32_t codesize) { VM::VM(uint8_t *code, uint32_t codesize) {
DBG_INFO(("Creating VM with code.\n")); DBG_INFO(("Creating VM with code.\n"));
memcpy(&vm.as.code, code, codesize); init_regs();
as.insCode(code, codesize);
} }
void VM::run(uint8_t * code) { void VM::init_regs(void) {
return; 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; }

View File

@ -1,20 +1,53 @@
#ifndef VM_H #ifndef VM_H
#define VM_H #define VM_H
#include <stdint.h>
#include "vmas.h"
class VMAddrSpace { enum regs { R0, R1, R2, R3, S0, S1, S2, S3, IP, BP, SP };
uint8_t stack[0x100], code[0x300], data[0x500];
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 { class VM {
uint16_t regs[0xb]; uint16_t regs[0xb];
struct flags { struct flags {
uint8_t zf:1; uint8_t zf : 1;
uint8_t cf:1; uint8_t cf : 1;
}; };
VMAddrSpace as; VMAddrSpace as;
public: public:
void run(uint8_t * code); VM();
VM(uint8_t *code, uint32_t codesize);
void init_regs(void);
void status(void);
void run(uint8_t *code);
}; };
#endif #endif