VM riconosce opcode cifrati

This commit is contained in:
Giulio De Pasquale 2017-05-17 18:32:05 +02:00
parent f8bb6252f0
commit 56b8a9c407
5 changed files with 67 additions and 130 deletions

36
cpp/opcodes.h Normal file
View File

@ -0,0 +1,36 @@
/*
MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG
*/
enum ops_starting_values {
MOVI,
MOVR,
LOAD,
STOR,
ADDI,
ADDR,
SUBI,
SUBR,
XORI,
XORR,
NOTI,
NOTR,
MULI,
MULR,
DIVI,
DIVR,
PUSH,
POOP,
JUMP,
SHIT,
NOPE,
GERM,
NUM_OPS
};
uint8_t OPS[NUM_OPS];
#define MOVI_SIZE 4
#define MOVR_SIZE 2
#define LOAD_SIZE 4
#define STOR_SIZE 4
#define ADDI_SIZE 4

View File

@ -1,5 +1,6 @@
#include "vm.h"
#include "debug.h"
#include "opcodes.h"
#include "vmas.h"
#include <string.h>
@ -14,23 +15,24 @@ void VM::defineOpcodes(uint8_t *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));
OPS[j] = rol(key[i] ^ OPS[j], key[i] % 8, 8);
} else {
ops[j].setValue(rol(key[i] ^ ops[j].getValue(), (key[i] + 1) % 8, 8));
OPS[j] = rol(key[i] ^ OPS[j], (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));
OPS[j] = rol(OPS[j], OPS[i] % 8, 8);
}
}
#ifdef DBG
//#TODO ASSEGNARE I NOMI AGLI OPCODES
DBG_INFO(("OPCODES:\n"));
DBG_INFO(("~~~~~~~~~~\nOPCODES:\n"));
for (i = 0; i < NUM_OPS; i++) {
DBG_INFO(("%s: 0x%x\n", ops[i].getName(), ops[i].getValue()));
DBG_INFO(("0x%x: 0x%x\n", i, OPS[i]));
}
DBG_INFO(("~~~~~~~~~~\n"));
#endif
return;
}
@ -154,8 +156,8 @@ void VM::initVariables(void) {
for (i = R0; i < NUM_REGS; i++) {
this->regs[i] = 0;
}
for (i = MOVI; i < NUM_OPS; i++) {
ops[i].setValue(i);
for (i = 0; i < NUM_OPS; i++) {
OPS[i] = i;
}
return;
}
@ -199,28 +201,28 @@ bool VM::exec_movr(void) {
return true;
}
bool VM::exec_getm(void) {
bool VM::exec_load(void) {
/*
GETM R0, 0x1000 | R0 = data[0x1000]
LOAD R0, 0x1000 | R0 = data[0x1000]
*/
uint8_t dst;
uint16_t src;
dst = as.code[regs[IP] + 1];
src = *((uint16_t *)&as.code[regs[IP] + 2]);
DBG_INFO(("GETM %s, 0x%x\n", reg_name(dst), src));
DBG_INFO(("LOAD %s, 0x%x\n", reg_name(dst), src));
regs[dst] = *((uint16_t *)&as.data[src]);
return true;
}
bool VM::exec_putm(void) {
bool VM::exec_stor(void) {
/*
PUTM 0x1000, R0 | data[0x1000] = R0
STOR 0x1000, R0 | data[0x1000] = R0
*/
uint16_t dst;
uint8_t src;
dst = *((uint16_t *)&as.code[regs[IP] + 1]);
src = as.code[regs[IP] + 3];
DBG_INFO(("PUTM 0x%x, %s\n", dst, reg_name(src)));
DBG_INFO(("STOR 0x%x, %s\n", dst, reg_name(src)));
*((uint16_t *)&as.data[dst]) = regs[src];
return true;
}
@ -244,35 +246,26 @@ void VM::run(void) {
bool finished = false;
while (!finished) {
opcode = (uint8_t)as.code[regs[IP]];
switch (opcode) {
case MOVI:
if (opcode == OPS[MOVI]) {
exec_movi();
regs[IP] += MOVI_SIZE;
break;
case MOVR:
} else if (opcode == OPS[MOVR]) {
exec_movr();
regs[IP] += MOVR_SIZE;
break;
case LOAD:
exec_getm();
regs[IP] += GETM_SIZE;
break;
case STOR:
exec_putm();
regs[IP] += PUTM_SIZE;
break;
case ADDI:
} else if (opcode == OPS[LOAD]) {
exec_load();
regs[IP] += LOAD_SIZE;
} else if (opcode == OPS[STOR]) {
exec_stor();
regs[IP] += STOR_SIZE;
} else if (opcode == OPS[ADDI]) {
exec_addi();
regs[IP] += ADDI_SIZE;
break;
case SHIT:
DBG_INFO(("HALT\n"));
} else if (opcode == OPS[SHIT]) {
finished = true;
break;
default:
DBG_INFO(("WAT: 0x%x\n", opcode));
} else {
DBG_ERROR(("WAT\n"));
finished = true;
break;
}
}
return;

View File

@ -1,44 +1,10 @@
#ifndef VM_H
#define VM_H
#include "vmas.h"
#include "vmcomp.h"
#include <stdint.h>
#define MOVI_SIZE 4
#define MOVR_SIZE 2
#define GETM_SIZE 4
#define PUTM_SIZE 4
#define ADDI_SIZE 4
enum regs { R0, R1, R2, R3, S0, S1, S2, S3, IP, BP, SP, NUM_REGS };
/*
MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG
*/
enum ins {
MOVI,
MOVR,
LOAD,
STOR,
ADDI,
ADDR,
SUBI,
SUBR,
XORI,
XORR,
NOTI,
NOTR,
MULI,
MULR,
DIVI,
DIVR,
PUSH,
POOP,
CALL,
SHIT,
NOPE,
GERM,
NUM_OPS
};
enum regs { R0, R1, R2, R3, S0, S1, S2, S3, IP, BP, SP, NUM_REGS };
class VM {
private:
@ -47,7 +13,6 @@ private:
////////////////////////
uint16_t regs[0xb];
VMComponent ops[NUM_OPS];
struct flags {
uint8_t zf : 1;
uint8_t cf : 1;
@ -69,8 +34,8 @@ private:
bool exec_movi(void);
bool exec_movr(void);
bool exec_movm(void);
bool exec_getm(void);
bool exec_putm(void);
bool exec_load(void);
bool exec_stor(void);
bool exec_addi(void);
public:

View File

@ -1,38 +0,0 @@
#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;
}

View File

@ -1,19 +0,0 @@
#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