VM riconosce opcode cifrati
This commit is contained in:
parent
f8bb6252f0
commit
56b8a9c407
36
cpp/opcodes.h
Normal file
36
cpp/opcodes.h
Normal 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
|
63
cpp/vm.cpp
63
cpp/vm.cpp
@ -1,5 +1,6 @@
|
|||||||
#include "vm.h"
|
#include "vm.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
#include "opcodes.h"
|
||||||
#include "vmas.h"
|
#include "vmas.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -14,23 +15,24 @@ void VM::defineOpcodes(uint8_t *key) {
|
|||||||
for (i = 0; i < keysize; i++) {
|
for (i = 0; i < keysize; i++) {
|
||||||
for (j = 0; j < NUM_OPS; j++) {
|
for (j = 0; j < NUM_OPS; j++) {
|
||||||
if (key[i] % 2) {
|
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 {
|
} 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 (i = 0; i < NUM_OPS; i++) {
|
||||||
for (j = 0; j < NUM_OPS; j++) {
|
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
|
#ifdef DBG
|
||||||
//#TODO ASSEGNARE I NOMI AGLI OPCODES
|
//#TODO ASSEGNARE I NOMI AGLI OPCODES
|
||||||
DBG_INFO(("OPCODES:\n"));
|
DBG_INFO(("~~~~~~~~~~\nOPCODES:\n"));
|
||||||
for (i = 0; i < NUM_OPS; i++) {
|
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
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -154,8 +156,8 @@ void VM::initVariables(void) {
|
|||||||
for (i = R0; i < NUM_REGS; i++) {
|
for (i = R0; i < NUM_REGS; i++) {
|
||||||
this->regs[i] = 0;
|
this->regs[i] = 0;
|
||||||
}
|
}
|
||||||
for (i = MOVI; i < NUM_OPS; i++) {
|
for (i = 0; i < NUM_OPS; i++) {
|
||||||
ops[i].setValue(i);
|
OPS[i] = i;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -199,28 +201,28 @@ bool VM::exec_movr(void) {
|
|||||||
return true;
|
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;
|
uint8_t dst;
|
||||||
uint16_t src;
|
uint16_t src;
|
||||||
dst = as.code[regs[IP] + 1];
|
dst = as.code[regs[IP] + 1];
|
||||||
src = *((uint16_t *)&as.code[regs[IP] + 2]);
|
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]);
|
regs[dst] = *((uint16_t *)&as.data[src]);
|
||||||
return true;
|
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;
|
uint16_t dst;
|
||||||
uint8_t src;
|
uint8_t src;
|
||||||
dst = *((uint16_t *)&as.code[regs[IP] + 1]);
|
dst = *((uint16_t *)&as.code[regs[IP] + 1]);
|
||||||
src = as.code[regs[IP] + 3];
|
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];
|
*((uint16_t *)&as.data[dst]) = regs[src];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -244,35 +246,26 @@ void VM::run(void) {
|
|||||||
bool finished = false;
|
bool finished = false;
|
||||||
while (!finished) {
|
while (!finished) {
|
||||||
opcode = (uint8_t)as.code[regs[IP]];
|
opcode = (uint8_t)as.code[regs[IP]];
|
||||||
switch (opcode) {
|
if (opcode == OPS[MOVI]) {
|
||||||
case MOVI:
|
|
||||||
exec_movi();
|
exec_movi();
|
||||||
regs[IP] += MOVI_SIZE;
|
regs[IP] += MOVI_SIZE;
|
||||||
break;
|
} else if (opcode == OPS[MOVR]) {
|
||||||
case MOVR:
|
|
||||||
exec_movr();
|
exec_movr();
|
||||||
regs[IP] += MOVR_SIZE;
|
regs[IP] += MOVR_SIZE;
|
||||||
break;
|
} else if (opcode == OPS[LOAD]) {
|
||||||
case LOAD:
|
exec_load();
|
||||||
exec_getm();
|
regs[IP] += LOAD_SIZE;
|
||||||
regs[IP] += GETM_SIZE;
|
} else if (opcode == OPS[STOR]) {
|
||||||
break;
|
exec_stor();
|
||||||
case STOR:
|
regs[IP] += STOR_SIZE;
|
||||||
exec_putm();
|
} else if (opcode == OPS[ADDI]) {
|
||||||
regs[IP] += PUTM_SIZE;
|
|
||||||
break;
|
|
||||||
case ADDI:
|
|
||||||
exec_addi();
|
exec_addi();
|
||||||
regs[IP] += ADDI_SIZE;
|
regs[IP] += ADDI_SIZE;
|
||||||
break;
|
} else if (opcode == OPS[SHIT]) {
|
||||||
case SHIT:
|
|
||||||
DBG_INFO(("HALT\n"));
|
|
||||||
finished = true;
|
finished = true;
|
||||||
break;
|
} else {
|
||||||
default:
|
DBG_ERROR(("WAT\n"));
|
||||||
DBG_INFO(("WAT: 0x%x\n", opcode));
|
|
||||||
finished = true;
|
finished = true;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
41
cpp/vm.h
41
cpp/vm.h
@ -1,44 +1,10 @@
|
|||||||
#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 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 };
|
|
||||||
|
|
||||||
/*
|
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
|
|
||||||
};
|
|
||||||
|
|
||||||
class VM {
|
class VM {
|
||||||
private:
|
private:
|
||||||
@ -47,7 +13,6 @@ private:
|
|||||||
////////////////////////
|
////////////////////////
|
||||||
|
|
||||||
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;
|
||||||
@ -69,8 +34,8 @@ private:
|
|||||||
bool exec_movi(void);
|
bool exec_movi(void);
|
||||||
bool exec_movr(void);
|
bool exec_movr(void);
|
||||||
bool exec_movm(void);
|
bool exec_movm(void);
|
||||||
bool exec_getm(void);
|
bool exec_load(void);
|
||||||
bool exec_putm(void);
|
bool exec_stor(void);
|
||||||
bool exec_addi(void);
|
bool exec_addi(void);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
19
cpp/vmcomp.h
19
cpp/vmcomp.h
@ -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
|
|
Loading…
Reference in New Issue
Block a user