VM con istruzioni cifrate. Da sistemare ancora la classe VM e i vari VMComponent
This commit is contained in:
parent
1431f6e104
commit
96ff9873ff
@ -10,21 +10,21 @@ int main(int argc, char *argv[]) {
|
|||||||
std::streamsize bytecode_size;
|
std::streamsize bytecode_size;
|
||||||
uint8_t * bytecode;
|
uint8_t * bytecode;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 3) {
|
||||||
printf("Usage: %s <program>\n", argv[0]);
|
printf("Usage: %s <opcodes_key> <program>\n", argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
reading bytecode
|
reading bytecode
|
||||||
*/
|
*/
|
||||||
bytecode_if.open(argv[1], std::ios::binary | std::ios::ate);
|
bytecode_if.open(argv[2], std::ios::binary | std::ios::ate);
|
||||||
bytecode_size = bytecode_if.tellg();
|
bytecode_size = bytecode_if.tellg();
|
||||||
bytecode_if.seekg(0, std::ios::beg);
|
bytecode_if.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
bytecode = new uint8_t[bytecode_size];
|
bytecode = new uint8_t[bytecode_size];
|
||||||
bytecode_if.read((char*)bytecode, bytecode_size);
|
bytecode_if.read((char*)bytecode, bytecode_size);
|
||||||
VM vm(bytecode, bytecode_size);
|
VM vm((uint8_t*)argv[1], bytecode, bytecode_size);
|
||||||
vm.run();
|
vm.run();
|
||||||
vm.status();
|
vm.status();
|
||||||
return 0;
|
return 0;
|
||||||
|
55
cpp/vm.cpp
55
cpp/vm.cpp
@ -3,6 +3,36 @@
|
|||||||
#include "vmas.h"
|
#include "vmas.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
unsigned rol(unsigned x, int L, int N) {
|
||||||
|
unsigned lsbs = x & ((1 >> L) - 1);
|
||||||
|
return (x << L) | (lsbs >> (N - L));
|
||||||
|
}
|
||||||
|
|
||||||
|
void VM::defineOpcodes(uint8_t *key) {
|
||||||
|
uint32_t i, j, keysize;
|
||||||
|
keysize = strlen((char *)key);
|
||||||
|
for (i = 0; i < keysize; i++) {
|
||||||
|
for (j = 0; j < NUM_OPS; j++) {
|
||||||
|
if (key[i] % 2) {
|
||||||
|
ops[j].setValue(rol(key[i] ^ ops[j].getValue(), key[i] % 8, 8));
|
||||||
|
} else {
|
||||||
|
ops[j].setValue(rol(key[i] ^ ops[j].getValue(), (key[i] + 1) % 8, 8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i = 0; i < NUM_OPS; i++) {
|
||||||
|
for (j = 0; j < NUM_OPS; j++) {
|
||||||
|
ops[j].setValue(rol(ops[j].getValue(), ops[i].getValue() % 8, 8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#ifdef DBG
|
||||||
|
DBG_INFO(("OPCODES:\n"));
|
||||||
|
for (i = 0; i < NUM_OPS; i++) {
|
||||||
|
DBG_INFO(("%s: 0x%x\n", ops[i].getName(), ops[i].getValue()));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
DBG UTILS
|
DBG UTILS
|
||||||
*/
|
*/
|
||||||
@ -101,26 +131,31 @@ void VM::status(void) {
|
|||||||
/*
|
/*
|
||||||
CONSTRUCTORS
|
CONSTRUCTORS
|
||||||
*/
|
*/
|
||||||
VM::VM() {
|
VM::VM(uint8_t *key) {
|
||||||
DBG_SUCC(("Creating VM without code.\n"));
|
DBG_SUCC(("Creating VM without code.\n"));
|
||||||
as.allocate();
|
as.allocate();
|
||||||
init_regs();
|
initVariables();
|
||||||
|
defineOpcodes(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
VM::VM(uint8_t *code, uint32_t codesize) {
|
VM::VM(uint8_t *key, uint8_t *code, uint32_t codesize) {
|
||||||
DBG_SUCC(("Creating VM with code.\n"));
|
DBG_SUCC(("Creating VM with code.\n"));
|
||||||
if (as.allocate()) {
|
if (as.allocate()) {
|
||||||
as.insCode(code, codesize);
|
as.insCode(code, codesize);
|
||||||
}
|
}
|
||||||
init_regs();
|
initVariables();
|
||||||
|
defineOpcodes(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VM::init_regs(void) {
|
void VM::initVariables(void) {
|
||||||
uint8_t i;
|
uint32_t i;
|
||||||
|
|
||||||
for (i = R0; i <= SP; i++) {
|
for (i = R0; i < NUM_REGS; i++) {
|
||||||
this->regs[i] = 0;
|
this->regs[i] = 0;
|
||||||
}
|
}
|
||||||
|
for (i = MOVI; i < NUM_OPS; i++) {
|
||||||
|
ops[i].setValue(i);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,11 +252,11 @@ void VM::run(void) {
|
|||||||
exec_movr();
|
exec_movr();
|
||||||
regs[IP] += MOVR_SIZE;
|
regs[IP] += MOVR_SIZE;
|
||||||
break;
|
break;
|
||||||
case GETM:
|
case LOAD:
|
||||||
exec_getm();
|
exec_getm();
|
||||||
regs[IP] += GETM_SIZE;
|
regs[IP] += GETM_SIZE;
|
||||||
break;
|
break;
|
||||||
case PUTM:
|
case STOR:
|
||||||
exec_putm();
|
exec_putm();
|
||||||
regs[IP] += PUTM_SIZE;
|
regs[IP] += PUTM_SIZE;
|
||||||
break;
|
break;
|
||||||
@ -229,7 +264,7 @@ void VM::run(void) {
|
|||||||
exec_addi();
|
exec_addi();
|
||||||
regs[IP] += ADDI_SIZE;
|
regs[IP] += ADDI_SIZE;
|
||||||
break;
|
break;
|
||||||
case HALT:
|
case SHIT:
|
||||||
DBG_INFO(("HALT\n"));
|
DBG_INFO(("HALT\n"));
|
||||||
finished = true;
|
finished = true;
|
||||||
break;
|
break;
|
||||||
|
28
cpp/vm.h
28
cpp/vm.h
@ -1,6 +1,7 @@
|
|||||||
#ifndef VM_H
|
#ifndef VM_H
|
||||||
#define VM_H
|
#define VM_H
|
||||||
#include "vmas.h"
|
#include "vmas.h"
|
||||||
|
#include "vmcomp.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#define MOVI_SIZE 4
|
#define MOVI_SIZE 4
|
||||||
@ -8,7 +9,7 @@
|
|||||||
#define GETM_SIZE 4
|
#define GETM_SIZE 4
|
||||||
#define PUTM_SIZE 4
|
#define PUTM_SIZE 4
|
||||||
#define ADDI_SIZE 4
|
#define ADDI_SIZE 4
|
||||||
enum regs { R0, R1, R2, R3, S0, S1, S2, S3, IP, BP, SP };
|
enum regs { R0, R1, R2, R3, S0, S1, S2, S3, IP, BP, SP, NUM_REGS };
|
||||||
|
|
||||||
/*
|
/*
|
||||||
MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG
|
MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG
|
||||||
@ -16,8 +17,8 @@ MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG
|
|||||||
enum ins {
|
enum ins {
|
||||||
MOVI,
|
MOVI,
|
||||||
MOVR,
|
MOVR,
|
||||||
GETM,
|
LOAD,
|
||||||
PUTM,
|
STOR,
|
||||||
ADDI,
|
ADDI,
|
||||||
ADDR,
|
ADDR,
|
||||||
SUBI,
|
SUBI,
|
||||||
@ -33,19 +34,31 @@ enum ins {
|
|||||||
PUSH,
|
PUSH,
|
||||||
POOP,
|
POOP,
|
||||||
CALL,
|
CALL,
|
||||||
HALT,
|
SHIT,
|
||||||
NOPE
|
NOPE,
|
||||||
|
GERM,
|
||||||
|
NUM_OPS
|
||||||
};
|
};
|
||||||
|
|
||||||
class VM {
|
class VM {
|
||||||
private:
|
private:
|
||||||
|
////////////////////////
|
||||||
|
// VARIABLES
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
uint16_t regs[0xb];
|
uint16_t regs[0xb];
|
||||||
|
VMComponent ops[NUM_OPS];
|
||||||
struct flags {
|
struct flags {
|
||||||
uint8_t zf : 1;
|
uint8_t zf : 1;
|
||||||
uint8_t cf : 1;
|
uint8_t cf : 1;
|
||||||
};
|
};
|
||||||
VMAddrSpace as;
|
VMAddrSpace as;
|
||||||
|
|
||||||
|
////////////////////////
|
||||||
|
// FUNCTIONS
|
||||||
|
///////////////////////
|
||||||
|
void initVariables(void);
|
||||||
|
void defineOpcodes(uint8_t * key);
|
||||||
/*
|
/*
|
||||||
DBG UTILS
|
DBG UTILS
|
||||||
*/
|
*/
|
||||||
@ -61,9 +74,8 @@ private:
|
|||||||
bool exec_addi(void);
|
bool exec_addi(void);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VM();
|
VM(uint8_t * key);
|
||||||
VM(uint8_t *code, uint32_t codesize);
|
VM(uint8_t * key, uint8_t *code, uint32_t codesize);
|
||||||
void init_regs(void);
|
|
||||||
void status(void);
|
void status(void);
|
||||||
void run();
|
void run();
|
||||||
};
|
};
|
||||||
|
38
cpp/vmcomp.cpp
Normal file
38
cpp/vmcomp.cpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#include "debug.h"
|
||||||
|
#include "vmcomp.h"
|
||||||
|
|
||||||
|
VMComponent::VMComponent(void) {
|
||||||
|
name = NULL;
|
||||||
|
value = 0;
|
||||||
|
}
|
||||||
|
VMComponent::VMComponent(uint8_t * name, uint16_t value) {
|
||||||
|
this->name = name;
|
||||||
|
this->value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t * VMComponent::getName(void){
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t VMComponent::getValue(void) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void VMComponent::setName(uint8_t * name) {
|
||||||
|
this->name = name;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void VMComponent::setValue(uint16_t value) {
|
||||||
|
this->value = value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t VMComponent::toUint8(void) {
|
||||||
|
return (uint8_t) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t VMComponent::toUint16(void) {
|
||||||
|
return (uint16_t) value;
|
||||||
|
}
|
||||||
|
|
19
cpp/vmcomp.h
Normal file
19
cpp/vmcomp.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef VMCOMP_H
|
||||||
|
#define VMCOMP_H
|
||||||
|
#include <stdint.h>
|
||||||
|
class VMComponent {
|
||||||
|
private:
|
||||||
|
uint8_t * name;
|
||||||
|
uint16_t value;
|
||||||
|
public:
|
||||||
|
VMComponent();
|
||||||
|
VMComponent(uint8_t * name, uint16_t value);
|
||||||
|
uint8_t * getName(void);
|
||||||
|
uint8_t getValue(void);
|
||||||
|
void setName(uint8_t * name);
|
||||||
|
void setValue(uint16_t value);
|
||||||
|
uint8_t toUint8(void);
|
||||||
|
uint16_t toUint16(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user