Improves vm.h: Changes style to C API, removes unnecessary overhead.

This commit is contained in:
Felix Queißner 2016-06-29 16:56:01 +02:00
parent 233a206329
commit 02bc1933ab
2 changed files with 50 additions and 54 deletions

View file

@ -25,7 +25,7 @@
struct PredefinedCmd
{
const char *name;
Instruction instr;
instruction_t instr;
};
struct PredefinedCmd namedCommands[] =
@ -103,12 +103,12 @@ const char *commandStrings[] =
int disassembleVerbose = 0;
void disassemble(Instruction *list, uint32_t count, uint32_t base, FILE *f)
void disassemble(instruction_t *list, uint32_t count, uint32_t base, FILE *f)
{
int v = disassembleVerbose;
for (int i = 0; i < count; i++)
{
Instruction instr = list[i];
instruction_t instr = list[i];
fprintf(f, "%8X: ", base + i);
@ -116,7 +116,7 @@ void disassemble(Instruction *list, uint32_t count, uint32_t base, FILE *f)
for (int j = 0; j < sizeof(namedCommands) / sizeof(struct PredefinedCmd); j++)
{
if (memcmp(&instr, &namedCommands[j].instr, sizeof(Instruction) - sizeof(uint32_t)) == 0) {
if (memcmp(&instr, &namedCommands[j].instr, sizeof(instruction_t) - sizeof(uint32_t)) == 0) {
knownInstruction = &namedCommands[j];
break;
}
@ -210,7 +210,6 @@ int main(int argc, char **argv)
int headers = 0;
int dumpSections = 0;
int dumpMetas = 0;
int disassembleSections = 0;
int c;
@ -235,6 +234,12 @@ int main(int argc, char **argv)
abort();
}
}
if(!headers && !dumpSections && !dumpMetas && !disassembleSections) {
headers = 1;
}
for (int index = optind; index < argc; index++)
{
const char *fileName = argv[index];
@ -256,9 +261,7 @@ int main(int argc, char **argv)
}
if (fileHeader.magicNumber != EXP_MAGIC)
{
fprintf(
stderr, "Invalid magic in %s\n",
fileHeader.majorVersion, fileHeader.minorVersion);
fprintf(stderr, "Invalid magic in %s\n", fileName);
continue;
}
if (fileHeader.majorVersion != 1 && fileHeader.minorVersion == 0)
@ -305,7 +308,7 @@ int main(int argc, char **argv)
else
fprintf(stdout, "; Section '%s'@0x%08X\n", section.name, section.base);
Instruction *buffer = malloc(section.length);
instruction_t *buffer = malloc(section.length);
fseek(f, section.start, SEEK_SET);
int len = fread(buffer, 1, section.length, f);
@ -314,7 +317,7 @@ int main(int argc, char **argv)
disassemble(
buffer,
section.length / sizeof(Instruction),
section.length / sizeof(instruction_t),
section.base,
stdout);

View file

@ -75,7 +75,7 @@ extern "C" {
#define VM_FLAG_Z (1<<0)
#define VM_FLAG_N (1<<1)
typedef struct
struct instruction
{
unsigned int execZ : 2;
unsigned int execN : 2;
@ -86,48 +86,39 @@ typedef struct
unsigned int flags : 1;
unsigned int output : 2;
uint32_t argument;
} PACKED Instruction;
} PACKED ;
static_assert(sizeof(Instruction) == 8, "Instruction must be 8 bytes large.");
static_assert(offsetof(Instruction, argument) == 4, "Argument must be must be 8 bytes large.");
typedef struct
struct spu
{
Instruction *code;
uint32_t length;
} Module;
struct instruction *code; // Pointer to the first instruction
uint32_t codeLength; // length of the code in instructions
typedef struct
uint32_t codePointer; // code pointer register
uint32_t stackPointer; // stack pointer register
uint32_t basePointer; // base pointer register
uint32_t flags; // flag register
uint32_t memoryBase; // Linear start address of the memory.
void *memory; // Point to the base linear address
uint32_t memorySize; // Size of the memory in bytes.
uint32_t stack[VM_STACKSIZE]; // The stack of the SPU.
};
struct cmdinput
{
uint32_t pageSize;
uint32_t length;
uint8_t **pages;
} VirtualMemoryMap;
uint32_t input0; // first input argument
uint32_t input1; // second input argument
uint32_t info; // command info
};
typedef struct
{
Module *module;
void *tag;
typedef struct instruction instruction_t;
typedef struct spu spu_t;
typedef struct cmdinput cmdinput_t;
uint32_t codePointer;
uint32_t stackPointer;
uint32_t basePointer;
uint32_t flags;
uint32_t stack[VM_STACKSIZE];
VirtualMemoryMap mmap;
} Process;
typedef struct
{
uint32_t input0;
uint32_t input1;
uint32_t argument;
uint32_t additional;
uint32_t output;
} CommandInfo;
/**
* @brief Steps a given process.
@ -137,36 +128,36 @@ typedef struct
* @param process The process to be stepped.
* @returns 1 if the process is still running or 0 if the process is terminated.
*/
int vm_step_process(Process *process);
int vm_step_process(spu_t *process);
/**
* @brief Pushes a value onto the process' stack.
*/
void vm_push(Process *process, uint32_t value);
void vm_push(spu_t *process, uint32_t value);
/**
* @brief Pops a value from the process' stack.
*/
uint32_t vm_pop(Process *process);
uint32_t vm_pop(spu_t *process);
/**
* @brief Returns the top value of the process' stack.
*/
uint32_t vm_peek(Process *process);
uint32_t vm_peek(spu_t *process);
/**
* Reads a byte from process memory.
* @arg process
* @arg address The address to read from.
*/
uint8_t vm_read_byte(Process *process, uint32_t address);
uint8_t vm_read_byte(spu_t *process, uint32_t address);
/**
* Writes a byte to process memory.
* @arg process
* @arg address The address to read from.
*/
void vm_write_byte(Process *process, uint32_t address, uint8_t value);
void vm_write_byte(spu_t *process, uint32_t address, uint8_t value);
// The following functions need to be host-implemented.
@ -180,16 +171,18 @@ void vm_assert(int assertion, const char *msg);
/**
* The hosts syscall implementation.
* @param process The process that calls the syscall.
* @param info Additional information for the syscall. Contains arguments and results.
* @param info The input values of the syscall.
* @returns Result the SPU receives from this command.
*/
void vm_syscall(Process *process, CommandInfo *info);
uint32_t vm_syscall(spu_t *process, cmdinput_t *info);
/**
* The hosts hardware IO implementation.
* @param process The process that wants to do IO.
* @param info Additional information for the HWIO. Contains arguments and results.
* @param info The input values for the hardware IO.
* @returns Result the SPU receives from this command.
*/
void vm_hwio(Process *process, CommandInfo *info);
uint32_t vm_hwio(spu_t *process, cmdinput_t *info);
#if defined(__cplusplus)
}