Initial Commit

This commit is contained in:
Creeper Lv
2026-04-11 12:52:14 +08:00
commit 349c71e4ea
6 changed files with 263 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
.DS_Store
Binaries/
+43
View File
@@ -0,0 +1,43 @@
#ifndef __SAGITTARIUS_H_
#define __SAGITTARIUS_H_
#include "SagittariusInst.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef _WIN32
#define SAGITTARIUS_API __declspec(dllexport)
#else
#define SAGITTARIUS_API __attribute__((visibility("default")))
#endif
typedef struct SagittariusRegister{
//256 Usable + Overflow preventing 256 extra bytes.
uint8_t head[512];
}SagittariusRegister;
typedef struct SagittariusMemory{
uint8_t* data;
uint64_t size;
} SagittariusMemory;
typedef struct SagittariusCore{
SagittariusRegister reg;
SagittariusMemory* memory;
uint64_t pc;
}SagittariusCore;
typedef int32_t(* SagittariusSyscall)(SagittariusCore* core, uint64_t arg_start);
typedef struct SagittariusSyscallEntry{
uint64_t id;
SagittariusSyscall syscall;
}SagittariusSyscallEntry;
typedef struct SagittariusVM{
SagittariusSyscallEntry* SagittariusSyscallEntries;
uint64_t SagittariusSyscallCount;
uint64_t SagittariusSyscallCapacity;
SagittariusMemory Memory;
}SagittariusVM;
SAGITTARIUS_API SagittariusVM* sagittarius_vm_new(uint64_t memory_size);
SAGITTARIUS_API void sagittarius_vm_free(SagittariusVM* vm);
SAGITTARIUS_API void sagittarius_step(SagittariusVM* vm);
SAGITTARIUS_API void sagittarius_mem_resize(SagittariusVM* vm, uint64_t new_size);
SAGITTARIUS_API uint64_t sagittarius_mem_getsize(SagittariusVM* vm);
#endif
+164
View File
@@ -0,0 +1,164 @@
#ifndef __SAGITTARIUS_INST_H_
#define __SAGITTARIUS_INST_H_
#include <stdint.h>
/**
* Generally, an instruction is 8 bytes, with the first byte being the instruction definition, and the remaining 7 bytes being the instruction data.
* Some special instructions take another full 8 bytes as data.
*/
typedef enum SagittariusInstDef
{
/**
* Math function have 2 operands involved.
* Layout:
* |0|1|2|3|4|5|
* |math2|type|operation|L reg|R reg|Dest reg|
*/
math2,
/**
* Math function have 1 operand involved.
* Layout:
* |0|1|2|3|4|
* |math1|type|operation|L reg|Dest reg|
*/
math1,
/**
* Type conversion
* Layout:
* |0|1|2|3|4
* |cvt|Src Reg| Src Type| Dst Reg| Dst Type|
*/
cvt,
/**
* Set register to a value.
* Layout:
* |0|1|2..7|8..15(another instruction struct)|
* |set|length(1-8 bytes)|padding|payload|
*/
set,
/**
* Move data from one register to another.
* Layout:
* |0|1|2|3|
* |mv|length(1-8 bytes)|Src Reg|Dest Reg|
*
*/
mv,
/**
* Copy data from one register to another
* Layout:
* |0|1|2|3|
* |cp|length(1-8 bytes)|Src Reg|Dest Reg|
*/
cp,
/**
* Save data to destination memory
* Layout:
* |0|1|2|3|
* |save|length(1-8 bytes)|Src Reg|Dest Mem Ptr (Reg ID)|
*/
save,
/**
* Load data from target memory
* Layout:
* |0|1|2|3|
* |load|length(1-8 bytes)|Dest Reg|Src Mem Ptr (Reg ID)|
*/
load,
/**
* Jump to target PC.
* Layout:
* |0|1|2|3/3..6|
* |jmp|mode selector: absolute/relative|value mode selector: register/immeidate|reg id/int32|
*/
jmp,
/**
* Jump to target PC.
* Layout:
* |0|1|2|3|4/4..7|
* |jmp|mode selector: absolute/relative|value mode selector: register/immeidate|flag register|reg id/int32|
*/
jmp_if,
/**
* Call to PC.
* Register Mode:
* Layout:
* |0|1|2|3|4|
* |call|mode selector: absolute/relative|0|register to remember|register|
* Immeidate Value Mode:
*
* |0|1|2|3|4..7|8..15|
* |call|mode selector: absolute/relative|1|register to remember|padding|uint64_t|
*/
call,
/**
* Return
* Layout:
* |0|1|
* |ret|reg to restore (normally sp)|
*/
ret,
/**
* Compare
* Layout:
*
* |0|1|2|3|4|5|
* |math2|type|operation|L reg|R reg|Dest reg|
*/
cmp,
/**
* Vector Math
* Layout:
* |0|1|2|3|4|5|6|
* |math2|w|h|operation|L mem ptr reg|R mem ptr reg| Dest mem ptr reg|
*/
mathv,
/**
* Halt execution (Only resumeable from VM holder)
* Layout:
* |0|
* |halt|
*/
halt,
/**
* System Call
* Layout:
* |0|1|2|
* |syscall|namespace reg| function id reg|
*/
syscall,
/**
* Test System Call
* Layout:
* |0|1|2|3|
* |tsyscall|namespace reg| function id reg| result reg|
*/
tsyscall,
} SagittariusInstDef;
typedef enum math2op
{
sag_math2_add,
sag_math2_sub,
sag_math2_mul,
sag_math2_div,
sag_math2_mod,
sag_math2_pow,
} math2op;
typedef enum math1op{
sag_math1_sin,
sag_math1_cos,
sag_math1_tan,
sag_math1_sinh,
sag_math1_cosh,
sag_math1_tanh,
sag_math1_asin,
sag_math1_acos,
sag_math1_atan,
sag_math1_abs,
}math1op;
typedef struct SagittariusInst
{
uint64_t data;
} SagittariusInst;
#endif
+4
View File
@@ -0,0 +1,4 @@
int main(int ac,char** av){
return 0;
}
+23
View File
@@ -0,0 +1,23 @@
#include "../../Headers/Sagittarius.h"
#include <stdlib.h>
SAGITTARIUS_API SagittariusVM* sagittarius_vm_new(uint64_t memory_size){
SagittariusVM* vm=malloc(sizeof(SagittariusVM));
vm->Memory.data=malloc(memory_size);
vm->Memory.size=memory_size;
return vm;
}
SAGITTARIUS_API void sagittarius_vm_free(SagittariusVM* vm){
free(vm);
}
SAGITTARIUS_API void sagittarius_step(SagittariusVM* vm){
}
SAGITTARIUS_API void sagittarius_mem_resize(SagittariusVM* vm, uint64_t new_size){
vm->Memory.data=realloc(vm->Memory.data,new_size);
vm->Memory.size=new_size;
}
SAGITTARIUS_API uint64_t sagittarius_mem_getsize(SagittariusVM* vm){
return vm->Memory.size;
}
Executable
+27
View File
@@ -0,0 +1,27 @@
#!/bin/sh
if [ -z "$CC" ];
then
CC=cc
fi
INDEX=0
if [ -d ".git" ];
then
echo "Project is tracked by git!"
git_str=$(git log | head -n 1 | cut -c 8-16)
echo $git_str
else
echo "Project is not tracked by git!"
git_str=""
fi
if [ -z "$SKIP_STANDALONE"];
then
EXEC="$CC Source/Standalone/*.c Source/VM/*.c -DVER=\"$git_str\" -o Binaries/Sagittarius"
echo "[$INDEX]$EXEC"
INDEX=$((INDEX+1))
$EXEC
fi
echo "$INDEX Task(s) completed."