initial commit

This commit is contained in:
klein panic
2025-02-14 18:02:53 -05:00
commit fecabb81a7
10 changed files with 1729 additions and 0 deletions

24
include/opcodes.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef OPCODES_H
#define OPCODES_H
// Define opcodes for the Virtual Machine.
// Each opcode is an integer constant used by the VM.
enum {
OP_HALT = 0, // Stop execution.
OP_PUSH, // Push an immediate value onto the stack. (Operand: value)
OP_POP, // Pop the top of the stack into a register. (Operand: register index)
OP_LOAD, // Load an immediate value into a register. (Operands: register, value)
OP_ADD, // Add two registers; store the result in the first register. (Operands: reg_dest, reg_src)
OP_SUB, // Subtract second register from first; store result in the first register. (Operands: reg_dest, reg_src)
OP_MUL, // Multiply two registers; store result in the first register. (Operands: reg_dest, reg_src)
OP_DIV, // Divide first register by second; store result in the first register. (Operands: reg_dest, reg_src)
OP_PRINT, // Print the value of a register. (Operand: register index)
OP_JMP, // Unconditional jump to a specified program address. (Operand: address)
OP_JMPZ, // Jump to a specified address if the given register is zero. (Operands: register, address)
OP_CALL, // Call a function: push return address and jump. (Operand: address)
OP_RET, // Return from a function call. (No operand)
OP_CALL_OS // Enter the nested OS shell. (No operand)
};
#endif // OPCODES_H

15
include/os.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef OS_H
#define OS_H
// Declare the global reload_path so that it can be used in other files.
extern char reload_path[256];
// Declaration for the nested OS shell.
void run_os(void);
// Functions to save and load the OS state (the in-memory file system).
// The state is saved in a custom file type (".osstate").
int save_state(const char *filename);
int load_state(const char *filename);
#endif // OS_H

27
include/vm.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef VM_H
#define VM_H
#include <stddef.h>
// Increase registers and memory sizes for advanced programs.
#define NUM_REGISTERS 16 // Number of general-purpose registers.
#define STACK_SIZE 512 // Maximum stack size.
#define PROGRAM_SIZE 2048 // Maximum program size (number of ints).
// VM structure definition.
typedef struct VM {
int registers[NUM_REGISTERS]; // General purpose registers.
int stack[STACK_SIZE]; // Stack memory.
int sp; // Stack pointer (index of next free slot).
int program[PROGRAM_SIZE]; // Bytecode program memory.
size_t program_size; // Number of ints in the loaded program.
size_t pc; // Program counter.
} VM;
// Function prototypes for the VM.
void init_vm(VM *vm);
int load_program(VM *vm, const int *program, size_t size);
void run_vm(VM *vm);
#endif // VM_H