first commit
This commit is contained in:
65
src/VM/VM.c
Normal file
65
src/VM/VM.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "VM.h"
|
||||
#include "../instructions/instructions.h"
|
||||
|
||||
void VM_init(VM* vm){
|
||||
memset(vm, 0, sizeof(VM));
|
||||
vm->state = VMState_Initialized;
|
||||
}
|
||||
|
||||
bool VM_loadProgram(VM* vm, u8* data, size_t size){
|
||||
if(data == NULL){
|
||||
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because data == NULL");
|
||||
return false;
|
||||
}
|
||||
if(size == 0){
|
||||
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because size == 0");
|
||||
return false;
|
||||
}
|
||||
|
||||
vm->data = data;
|
||||
vm->data_size = size;
|
||||
return true;
|
||||
}
|
||||
|
||||
i32 VM_executeProgram(VM* vm){
|
||||
if(vm->data == NULL){
|
||||
VM_setErrorMessage(vm, "[VM_executeProgram] data is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
while (pos < vm->data_size){
|
||||
u8 opcode = vm->data[pos];
|
||||
|
||||
const Instruction* instr = Instruction_getFromOpcode(opcode);
|
||||
if(instr == NULL){
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
pos++;
|
||||
i32 bytes_read = instr->implementation(vm, pos);
|
||||
if(bytes_read < 0)
|
||||
return -1;
|
||||
|
||||
pos += bytes_read;
|
||||
}
|
||||
|
||||
if(vm->state != VMState_Exited){
|
||||
VM_setErrorMessage(vm, "[%p] unexpected end of program", (void*)pos);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// exit code of the program should be in ax register
|
||||
return vm->ax.i32v;
|
||||
}
|
||||
|
||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
||||
if(pos + size >= vm->data_size){
|
||||
VM_setErrorMessage(vm, "[%p] unexpected end of data", (void*)vm->data_size);
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(dst, vm->data + pos, size);
|
||||
return true;
|
||||
}
|
||||
71
src/VM/VM.h
Normal file
71
src/VM/VM.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
typedef union Register {
|
||||
u32 u32v;
|
||||
i32 i32v;
|
||||
f32 f32v;
|
||||
struct {
|
||||
u16 u16v0;
|
||||
u16 u16v1;
|
||||
};
|
||||
struct {
|
||||
i16 i16v0;
|
||||
i16 i16v1;
|
||||
};
|
||||
struct {
|
||||
u8 u8v0;
|
||||
u8 u8v1;
|
||||
u8 u8v2;
|
||||
u8 u8v3;
|
||||
};
|
||||
struct {
|
||||
i8 i8v0;
|
||||
i8 i8v1;
|
||||
i8 i8v2;
|
||||
i8 i8v3;
|
||||
};
|
||||
} Register;
|
||||
|
||||
typedef enum VMState {
|
||||
VMState_Initialized,
|
||||
VMState_Executing,
|
||||
VMState_Exited,
|
||||
} VMState;
|
||||
|
||||
typedef struct VM {
|
||||
union {
|
||||
struct {
|
||||
Register ax;
|
||||
Register bx;
|
||||
Register cx;
|
||||
Register dx;
|
||||
};
|
||||
Register registers[4];
|
||||
};
|
||||
|
||||
VMState state;
|
||||
char* error_message;
|
||||
|
||||
u8* data;
|
||||
size_t data_size;
|
||||
} VM;
|
||||
|
||||
void VM_init(VM* vm);
|
||||
|
||||
/// @brief Loads a program from the buffer.
|
||||
/// @param data buffer with full program code
|
||||
/// @param size size of the program in bytes
|
||||
bool VM_loadProgram(VM* vm, u8* data, size_t size);
|
||||
|
||||
/// @brief Executes the program loaded into VM.
|
||||
/// @return program exit code or -1 on error (check vm.error_message)
|
||||
i32 VM_executeProgram(VM* vm);
|
||||
|
||||
#define VM_setErrorMessage(V, FORMAT, ...) {\
|
||||
char* buf = malloc(256);\
|
||||
sprintf(buf, FORMAT, ##__VA_ARGS__);\
|
||||
vm->error_message = buf;\
|
||||
}
|
||||
|
||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size);
|
||||
8
src/instructions/impl/EXIT.c
Normal file
8
src/instructions/impl/EXIT.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// EXIT
|
||||
/// ax - exit code
|
||||
i32 EXIT_impl(VM* vm, size_t pos){
|
||||
vm->state = VMState_Exited;
|
||||
return 0;
|
||||
}
|
||||
16
src/instructions/impl/MOV.c
Normal file
16
src/instructions/impl/MOV.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// MOV [dst_register] [src_register]
|
||||
i32 MOV_impl(VM* vm, size_t pos){
|
||||
u8 dst_register_i = 0;
|
||||
readRegisterVar(dst_register_i);
|
||||
u8 src_register_i = 0;
|
||||
readRegisterVar(src_register_i);
|
||||
if(dst_register_i == src_register_i){
|
||||
VM_setErrorMessage(vm, "[%p] dst_register_i == src_register_i (%x) ", (void*)pos, src_register_i);
|
||||
return -1;
|
||||
}
|
||||
|
||||
vm->registers[dst_register_i].u32v = vm->registers[src_register_i].u32v;
|
||||
return sizeof(dst_register_i) + sizeof(src_register_i);
|
||||
}
|
||||
6
src/instructions/impl/NOP.c
Normal file
6
src/instructions/impl/NOP.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// NOP
|
||||
i32 NOP_impl(VM* vm, size_t pos){
|
||||
return 0;
|
||||
}
|
||||
15
src/instructions/impl/PUSH.c
Normal file
15
src/instructions/impl/PUSH.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// PUSH [dst_register] [value_size] [value]
|
||||
i32 PUSH_impl(VM* vm, size_t pos){
|
||||
u8 dst_register_i = 0;
|
||||
readRegisterVar(dst_register_i);
|
||||
u8 value_size = 0;
|
||||
readValueSizeVar(value_size);
|
||||
|
||||
vm->registers[dst_register_i].u32v = 0;
|
||||
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, pos, value_size))
|
||||
return -1;
|
||||
|
||||
return sizeof(dst_register_i) + sizeof(value_size) + value_size;
|
||||
}
|
||||
44
src/instructions/impl/SYS.c
Normal file
44
src/instructions/impl/SYS.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
FILE* fileFromN(VM* vm, size_t pos, u32 file_n){
|
||||
FILE* f = NULL;
|
||||
switch(file_n){
|
||||
case 0: f = stdin; break;
|
||||
case 1: f = stdout; break;
|
||||
case 2: f = stderr; break;
|
||||
default:
|
||||
VM_setErrorMessage(vm, "[%p] invalid file_n (%x) ", (void*)pos, file_n);
|
||||
break;
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
/// SYS
|
||||
/// ax - func code
|
||||
i32 SYS_impl(VM* vm, size_t pos){
|
||||
u8 func_code = vm->ax.u8v0;
|
||||
size_t result_code = 0;
|
||||
switch(func_code){
|
||||
// sys_read
|
||||
// bx - file n
|
||||
// cx - buffer ptr
|
||||
// dx - buffer size
|
||||
case 0:;
|
||||
result_code = fread(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
|
||||
break;
|
||||
// sys_write
|
||||
// bx - file n
|
||||
// cx - buffer ptr
|
||||
// dx - buffer size
|
||||
case 1:;
|
||||
result_code = fwrite(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
|
||||
break;
|
||||
default:
|
||||
VM_setErrorMessage(vm, "[%p] invalid system call (%x) ", (void*)pos, func_code);
|
||||
return -1;
|
||||
}
|
||||
|
||||
vm->ax.u32v = result_code;
|
||||
return 0;
|
||||
}
|
||||
32
src/instructions/impl/impl_macros.h
Normal file
32
src/instructions/impl/impl_macros.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "../instructions.h"
|
||||
|
||||
#define readVar(VAR) {\
|
||||
if(!VM_dataRead(vm, &VAR, pos, sizeof(VAR))) \
|
||||
return -1;\
|
||||
pos += sizeof(VAR);\
|
||||
}
|
||||
|
||||
#define validateRegisterIndex(VAR) {\
|
||||
if(VAR > sizeof(vm->registers)){\
|
||||
VM_setErrorMessage(vm, "[%p] invalid register index (%x)", (void*)pos, VAR);\
|
||||
return -1;\
|
||||
}\
|
||||
}
|
||||
|
||||
#define readRegisterVar(VAR) {\
|
||||
readVar(VAR);\
|
||||
validateRegisterIndex(VAR);\
|
||||
}
|
||||
|
||||
#define validateValueSize(VAR) {\
|
||||
if(VAR < 1 || VAR > 4){\
|
||||
VM_setErrorMessage(vm, "[%p] invalid value_size (%x)", (void*)pos, VAR);\
|
||||
return -1;\
|
||||
}\
|
||||
}
|
||||
|
||||
#define readValueSizeVar(VAR) {\
|
||||
readVar(VAR);\
|
||||
validateValueSize(VAR);\
|
||||
}
|
||||
47
src/instructions/impl/math_operators.c
Normal file
47
src/instructions/impl/math_operators.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
#define mathOperatorImpl(OPERATOR){\
|
||||
u8 dst_register_i = 0, src_register_i = 0;\
|
||||
readRegisterVar(dst_register_i);\
|
||||
readRegisterVar(src_register_i);\
|
||||
u8 value_size = 0;\
|
||||
readValueSizeVar(value_size);\
|
||||
\
|
||||
switch(value_size){\
|
||||
case 1: \
|
||||
vm->registers[dst_register_i].u8v0 OPERATOR##= vm->registers[src_register_i].u8v0;\
|
||||
break;\
|
||||
case 2: \
|
||||
vm->registers[dst_register_i].u16v0 OPERATOR##= vm->registers[src_register_i].u16v0;\
|
||||
break;\
|
||||
case 4: \
|
||||
vm->registers[dst_register_i].u32v OPERATOR##= vm->registers[src_register_i].u32v;\
|
||||
break;\
|
||||
}\
|
||||
return sizeof(dst_register_i) + sizeof(src_register_i) + sizeof(value_size);\
|
||||
}
|
||||
|
||||
/// ADD [dst_register] [src_register]
|
||||
i32 ADD_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(+);
|
||||
}
|
||||
|
||||
/// SUB [dst_register] [src_register]
|
||||
i32 SUB_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(-);
|
||||
}
|
||||
|
||||
/// MUL [dst_register] [src_register]
|
||||
i32 MUL_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(*)
|
||||
}
|
||||
|
||||
/// DIV [dst_register] [src_register]
|
||||
i32 DIV_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(/)
|
||||
}
|
||||
|
||||
/// MOD [dst_register] [src_register]
|
||||
i32 MOD_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(%)
|
||||
}
|
||||
24
src/instructions/instructions.c
Normal file
24
src/instructions/instructions.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "instructions.h"
|
||||
|
||||
const Instruction instructions[] = {
|
||||
Instruction_construct(NOP),
|
||||
Instruction_construct(PUSH),
|
||||
Instruction_construct(MOV),
|
||||
Instruction_construct(ADD),
|
||||
Instruction_construct(SUB),
|
||||
Instruction_construct(MUL),
|
||||
Instruction_construct(DIV),
|
||||
Instruction_construct(MOD),
|
||||
Instruction_construct(SYS),
|
||||
Instruction_construct(EXIT),
|
||||
// Instruction_construct(JMP),
|
||||
// Instruction_construct(CALL),
|
||||
};
|
||||
const size_t instructions_count = sizeof(instructions)/sizeof(instructions[0]);
|
||||
|
||||
const Instruction* Instruction_getFromOpcode(u8 opcode){
|
||||
if(opcode >= instructions_count)
|
||||
return NULL;
|
||||
|
||||
return instructions + opcode;
|
||||
}
|
||||
34
src/instructions/instructions.h
Normal file
34
src/instructions/instructions.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include "../VM/VM.h"
|
||||
|
||||
///@param program_pos position in vm->program next afrer opcode
|
||||
///@returns number of bytes read
|
||||
typedef i32 (*InstructionImplFunc_t)(VM* vm, size_t program_pos);
|
||||
|
||||
typedef struct Instruction {
|
||||
const char* name;
|
||||
InstructionImplFunc_t implementation;
|
||||
} Instruction;
|
||||
|
||||
#define Instruction_construct(NAME) {\
|
||||
.name = #NAME, \
|
||||
.implementation = NAME##_impl \
|
||||
}
|
||||
|
||||
/// @brief get instruction info from table
|
||||
/// @param opcode any byte
|
||||
/// @return ptr to struct or NULL
|
||||
const Instruction* Instruction_getFromOpcode(u8 opcode);
|
||||
|
||||
i32 NOP_impl(VM* vm, size_t pos);
|
||||
i32 PUSH_impl(VM* vm, size_t pos);
|
||||
i32 MOV_impl(VM* vm, size_t pos);
|
||||
i32 ADD_impl(VM* vm, size_t pos);
|
||||
i32 SUB_impl(VM* vm, size_t pos);
|
||||
i32 MUL_impl(VM* vm, size_t pos);
|
||||
i32 DIV_impl(VM* vm, size_t pos);
|
||||
i32 MOD_impl(VM* vm, size_t pos);
|
||||
i32 SYS_impl(VM* vm, size_t pos);
|
||||
i32 EXIT_impl(VM* vm, size_t pos);
|
||||
i32 JMP_impl(VM* vm, size_t pos);
|
||||
i32 CALL_impl(VM* vm, size_t pos);
|
||||
77
src/main.c
Normal file
77
src/main.c
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "VM/VM.h"
|
||||
#include "instructions/instructions.h"
|
||||
|
||||
#define arg_is(STR) (strcmp(argv[argi], STR) == 0)
|
||||
|
||||
i32 main(const i32 argc, const char** argv){
|
||||
const char* filename = NULL;
|
||||
|
||||
for(i32 argi = 1; argi < argc; argi++){
|
||||
if(arg_is("-h") || arg_is("--help")){
|
||||
printf(
|
||||
"-h, --help Show this message\n"
|
||||
"-op, --opcodes Shows list of all instructions.\n"
|
||||
"-i, --image [FILE] Boot VM using image file\n"
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
else if(arg_is("-op") || arg_is("--opcodes")){
|
||||
for(u8 opcode = 0; opcode < 255; opcode++){
|
||||
const Instruction* instr = Instruction_getFromOpcode(opcode);
|
||||
if(instr != NULL){
|
||||
printf("%02x %s\n", opcode, instr->name);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else if(arg_is("-i") || arg_is("--image")){
|
||||
if(++argi >= argc){
|
||||
printfe("ERROR: no image file specified");
|
||||
return 1;
|
||||
}
|
||||
filename = argv[argi];
|
||||
}
|
||||
else {
|
||||
printfe("ERROR: unknown argument '%s'", argv[argi]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(filename == NULL){
|
||||
printfe("ERROR: no arguments provided. Use --help to know more.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
FILE* file = fopen(filename, "rb");
|
||||
if(file == NULL){
|
||||
printfe("ERROR: can't open file '%s'", filename);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const size_t buffer_size = 1024*1024;
|
||||
u8* buffer = malloc(buffer_size);
|
||||
memset(buffer, 0, buffer_size);
|
||||
|
||||
size_t bytes_read = fread(buffer, 1, buffer_size, file);
|
||||
fclose(file);
|
||||
if(bytes_read == (size_t)EOF){
|
||||
printfe("ERROR: can't read file '%s'", filename);
|
||||
free(buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
VM vm;
|
||||
VM_init(&vm);
|
||||
|
||||
i32 exit_code = 1;
|
||||
if(VM_loadProgram(&vm, buffer, bytes_read)){
|
||||
exit_code = VM_executeProgram(&vm);
|
||||
}
|
||||
if(vm.error_message != NULL){
|
||||
printfe("VM ERROR: %s", vm.error_message);
|
||||
free(vm.error_message);
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
return exit_code;
|
||||
}
|
||||
27
src/std.h
Normal file
27
src/std.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef uint8_t u8;
|
||||
typedef int16_t i16;
|
||||
typedef uint16_t u16;
|
||||
typedef int32_t i32;
|
||||
typedef uint32_t u32;
|
||||
typedef int64_t i64;
|
||||
typedef uint64_t u64;
|
||||
typedef float f32;
|
||||
typedef double f64;
|
||||
|
||||
typedef u8 bool;
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
|
||||
Reference in New Issue
Block a user