gipu/cpp/vmas.cpp

94 lines
2.1 KiB
C++
Raw Normal View History

2017-05-14 13:07:24 +01:00
#include "vmas.h"
#include "debug.h"
2017-05-14 21:10:58 +01:00
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
2017-05-14 13:07:24 +01:00
2017-05-14 21:10:58 +01:00
VMAddrSpace::VMAddrSpace() {
2017-05-14 22:01:11 +01:00
stack = NULL;
code = NULL;
data = NULL;
2017-05-14 21:10:58 +01:00
stacksize = DEFAULT_STACKSIZE;
codesize = DEFAULT_CODESIZE;
datasize = DEFAULT_DATASIZE;
return;
2017-05-14 13:07:24 +01:00
}
2017-05-14 21:10:58 +01:00
VMAddrSpace::VMAddrSpace(uint32_t ss, uint32_t cs, uint32_t ds) {
2017-05-14 22:01:11 +01:00
stack = NULL;
code = NULL;
data = NULL;
2017-05-14 21:10:58 +01:00
stacksize = ss;
codesize = cs;
datasize = ds;
return;
}
bool VMAddrSpace::allocate(void) {
DBG_INFO(("Allocating sections...\n"));
if (code == NULL) {
2017-05-14 22:01:11 +01:00
DBG_INFO(("\tcode...\n"));
2017-05-14 21:10:58 +01:00
code = (uint8_t *)malloc(codesize);
}
if (data == NULL) {
2017-05-14 22:01:11 +01:00
DBG_INFO(("\tdata...\n"));
2017-05-14 21:10:58 +01:00
data = (uint8_t *)malloc(datasize);
}
if (stack == NULL) {
2017-05-14 22:01:11 +01:00
DBG_INFO(("\tstack...\n"));
2017-05-14 21:10:58 +01:00
stack = (uint8_t *)malloc(stacksize);
}
if (code == NULL) {
DBG_ERROR(("Couldn't allocate code section.\n"));
return false;
}
data = (uint8_t *)malloc(datasize);
if (data == NULL) {
DBG_ERROR(("Couldn't allocate data section.\n"));
return false;
}
stack = (uint8_t *)malloc(stacksize);
if (stack == NULL) {
DBG_ERROR(("Couldn't allocate stack section.\n"));
return false;
}
2017-05-14 22:01:11 +01:00
memset(code, 0xff,
stacksize); // auto halt in case the assembly is not correct
memset(stack, 0x0, stacksize);
memset(data, 0x0, stacksize);
2017-05-14 21:10:58 +01:00
DBG_SUCC(("Done!\n"));
return true;
}
bool VMAddrSpace::insCode(uint8_t *buf, uint8_t size) {
if (code) {
2017-05-14 13:07:24 +01:00
DBG_INFO(("Copying buffer into code section.\n"));
2017-05-14 21:10:58 +01:00
memcpy(code, buf, size);
} else {
DBG_ERROR(("Couldn't write into code section.\n"));
return false;
}
return true;
}
bool VMAddrSpace::insStack(uint8_t *buf, uint8_t size) {
if (stack) {
DBG_INFO(("Copying buffer into stack section.\n"));
memcpy(stack, buf, size);
} else {
DBG_ERROR(("Couldn't write into stack section.\n"));
return false;
}
return true;
2017-05-14 13:07:24 +01:00
}
2017-05-14 21:10:58 +01:00
bool VMAddrSpace::insData(uint8_t *buf, uint8_t size) {
if (this->code) {
2017-05-14 13:07:24 +01:00
DBG_INFO(("Copying buffer into data section.\n"));
2017-05-14 21:10:58 +01:00
memcpy(data, buf, size);
} else {
DBG_ERROR(("Couldn't write into data section.\n"));
return false;
}
return true;
2017-05-14 13:07:24 +01:00
}