Adds syscall and hwio instructions and mnemonics.

This commit is contained in:
Felix Queißner 2016-05-21 17:57:13 +02:00
parent f7bc2624a0
commit 88d44b4ca1
4 changed files with 94 additions and 46 deletions

View file

@ -35,6 +35,24 @@ void vm_assert(int assertion, const char *msg)
exit(1); exit(1);
} }
void vm_syscall(Process *p, CommandInfo *info)
{
printf("Some syscall: (%d, %d, %d, %d)\n",
info->input0,
info->input1,
info->argument,
info->additional);
}
void vm_hwio(Process *p, CommandInfo *info)
{
printf("Some hardware IO: (%d, %d, %d, %d)\n",
info->input0,
info->input1,
info->argument,
info->additional);
}
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
if(argc < 2) { if(argc < 2) {

View file

@ -84,8 +84,8 @@ An instruction is only executed when all conditions are met.
| 8 | MATH | output = input0 OP[info] input1 | | 8 | MATH | output = input0 OP[info] input1 |
| 9 | SPGET | output = SP + input0 | | 9 | SPGET | output = SP + input0 |
| 10 | SPSET | output = SP + input0 = input1 | | 10 | SPSET | output = SP + input0 = input1 |
| 11 | | | | 11 | SYSCALL | output = SysCall(input0, input1) |
| 12 | | | | 12 | HWIO | output = HardwareIO(input0, input1) |
| 13 | | | | 13 | | |
| 14 | | | | 14 | | |
| 15 | | | | 15 | | |
@ -115,37 +115,40 @@ by the `cmdinfo`.
## Assembler Mnemonics ## Assembler Mnemonics
| Mnemonic | Arg? | i0 | i1 | Cmd | CmdInfo | Output | | Mnemonic | Arg? | i0 | i1 | Cmd | CmdInfo | Output | Flags? |
|----------|------|------|------|-------|----------|---------| |----------|------|------|------|---------|----------|---------|--------|
| nop | no | zero | zero | copy | 0 | discard | | nop | no | zero | zero | copy | 0 | discard | no |
| push | yes | arg | zero | copy | 0 | push | | push | yes | arg | zero | copy | 0 | push | no |
| drop | no | pop | zero | copy | 0 | discard | | drop | no | pop | zero | copy | 0 | discard | no |
| dup | no | peek | zero | copy | 0 | push | | dup | no | peek | zero | copy | 0 | push | no |
| jmp | yes | arg | zero | copy | 0 | jump | | jmp | yes | arg | zero | copy | 0 | jump | no |
| jmpi | no | pop | zero | copy | 0 | jump | | jmpi | no | pop | zero | copy | 0 | jump | no |
| ret | no | pop | zero | copy | 0 | jump | | ret | no | pop | zero | copy | 0 | jump | no |
| load | yes | arg | zero | load | 0 | push | | load | yes | arg | zero | load | 0 | push | no |
| loadi | no | pop | zero | load | 0 | push | | loadi | no | pop | zero | load | 0 | push | no |
| store | yes | arg | pop | store | 0 | discard | | store | yes | arg | pop | store | 0 | discard | no |
| storei | no | pop | pop | store | 0 | discard | | storei | no | pop | pop | store | 0 | discard | no |
| get | yes | arg | zero | get | 0 | push | | get | yes | arg | zero | get | 0 | push | no |
| geti | no | pop | zero | get | 0 | push | | geti | no | pop | zero | get | 0 | push | no |
| set | yes | arg | pop | set | 0 | discard | | set | yes | arg | pop | set | 0 | discard | no |
| seti | no | pop | pop | set | 0 | discard | | seti | no | pop | pop | set | 0 | discard | no |
| bpget | no | zero | zero | bpget | 0 | push | | bpget | no | zero | zero | bpget | 0 | push | no |
| bpset | no | pop | zero | bpset | 0 | discard | | bpset | no | pop | zero | bpset | 0 | discard | no |
| add | no | pop | pop | math | 0 | push | | add | no | pop | pop | math | 0 | push | no |
| sub | no | pop | pop | math | 1 | push | | sub | no | pop | pop | math | 1 | push | no |
| mul | no | pop | pop | math | 2 | push | | cmp | no | pop | pop | math | 1 | discard | yes |
| div | no | pop | pop | math | 3 | push | | mul | no | pop | pop | math | 2 | push | no |
| mod | no | pop | pop | math | 4 | push | | div | no | pop | pop | math | 3 | push | no |
| and | no | pop | pop | math | 5 | push | | mod | no | pop | pop | math | 4 | push | no |
| or | no | pop | pop | math | 6 | push | | and | no | pop | pop | math | 5 | push | no |
| xor | no | pop | pop | math | 7 | push | | or | no | pop | pop | math | 6 | push | no |
| not | no | pop | zero | math | 8 | push | | xor | no | pop | pop | math | 7 | push | no |
| rol | no | pop | pop | math | 9 | push | | not | no | pop | zero | math | 8 | push | no |
| ror | no | pop | pop | math | 10 | push | | rol | no | pop | pop | math | 9 | push | no |
| asl | no | pop | pop | math | 11 | push | | ror | no | pop | pop | math | 10 | push | no |
| asr | no | pop | pop | math | 12 | push | | asl | no | pop | pop | math | 11 | push | no |
| shl | no | pop | pop | math | 13 | push | | asr | no | pop | pop | math | 12 | push | no |
| shr | no | pop | pop | math | 14 | push | | shl | no | pop | pop | math | 13 | push | no |
| shr | no | pop | pop | math | 14 | push | no |
| syscall | yes | zero | zero | syscall | 0 | discard | no |
| hwio | yes | zero | zero | hwio | 0 | discard | no |

View file

@ -2,16 +2,6 @@
#include <stdio.h> #include <stdio.h>
typedef struct
{
uint32_t input0;
uint32_t input1;
uint32_t argument;
uint32_t additional;
uint32_t output;
} CommandInfo;
static void cmd_copy(CommandInfo *info) static void cmd_copy(CommandInfo *info)
{ {
info->output = info->input0; info->output = info->input0;
@ -108,6 +98,8 @@ int vm_step_process(Process *process)
{ {
case VM_CMD_COPY: cmd_copy(&info); break; case VM_CMD_COPY: cmd_copy(&info); break;
case VM_CMD_MATH: cmd_math(&info); break; case VM_CMD_MATH: cmd_math(&info); break;
case VM_CMD_SYSCALL: vm_syscall(process, &info); break;
case VM_CMD_HWIO: vm_hwio(process, &info); break;
default: vm_assert(0, "Invalid instruction: command undefined."); default: vm_assert(0, "Invalid instruction: command undefined.");
} }

View file

@ -32,6 +32,8 @@ extern "C" {
#define VM_CMD_MATH 8 #define VM_CMD_MATH 8
#define VM_CMD_SPGET 9 #define VM_CMD_SPGET 9
#define VM_CMD_SPSET 10 #define VM_CMD_SPSET 10
#define VM_CMD_SYSCALL 11
#define VM_CMD_HWIO 12
#define VM_MATH_ADD 0 #define VM_MATH_ADD 0
#define VM_MATH_SUB 1 #define VM_MATH_SUB 1
@ -84,6 +86,7 @@ typedef struct
typedef struct typedef struct
{ {
Module *module; Module *module;
void *tag;
uint32_t codePointer; uint32_t codePointer;
uint32_t stackPointer; uint32_t stackPointer;
@ -93,6 +96,16 @@ typedef struct
uint32_t stack[VM_STACKSIZE]; uint32_t stack[VM_STACKSIZE];
} Process; } Process;
typedef struct
{
uint32_t input0;
uint32_t input1;
uint32_t argument;
uint32_t additional;
uint32_t output;
} CommandInfo;
/** /**
* @brief Steps a given process. * @brief Steps a given process.
* *
@ -118,8 +131,30 @@ uint32_t vm_pop(Process *process);
*/ */
uint32_t vm_peek(Process *process); uint32_t vm_peek(Process *process);
// The following functions need to be host-implemented.
/**
* An assertion the VM does.
* @param assertion If zero, the assertion failed.
* @param msg The message that should be shown when the assertion fails.
*/
void vm_assert(int assertion, const char *msg); 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.
*/
void vm_syscall(Process *process, CommandInfo *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.
*/
void vm_hwio(Process *process, CommandInfo *info);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif