No way it works...

This commit is contained in:
2025-11-18 22:27:45 +01:00
commit 78177c0a07
17 changed files with 109146 additions and 0 deletions

17
src/boot/kernel.asm Normal file
View File

@@ -0,0 +1,17 @@
bits 32 ;nasm directive
section .text
;multiboot spec
align 4
dd 0x1BADB002 ;magic
dd 0x00 ;flags
dd - (0x1BADB002 + 0x00) ;checksum. m+f+c should be zero
global start
extern kmain ;kmain is defined in the kernel.c file
start:
cli ; stop interrupts
call kmain
hlt ; halt the CPU

89
src/kernel/kernel.c Normal file
View File

@@ -0,0 +1,89 @@
#include <string.h>
#include <stdint.h>
#include "lib/duktape.h"
#include "embedded_js.h"
#define WHITE_TXT 0x0F
// Forward declaration
unsigned int k_printf(char *message, unsigned int line);
duk_context *ctx;
duk_ret_t native_print(duk_context *ctx)
{
const char *msg = duk_to_string(ctx, 0);
// k_clear_screen();
k_printf((char *)msg, 0);
return 0;
}
duk_ret_t native_write_memory(duk_context *ctx)
{
// Get the address (first argument)
uint32_t address = (uint32_t)duk_to_uint32(ctx, 0);
// Get the value to write (second argument)
uint8_t value = (uint8_t)duk_to_uint32(ctx, 1);
// Write the value to the address
uint8_t *ptr = (uint8_t *)address;
*ptr = value;
return 0;
}
void kmain()
{
// Initialize Duktape heap
ctx = duk_create_heap_default();
// Register native print function
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_global_string(ctx, "print");
// Register native memory write function
duk_push_c_function(ctx, native_write_memory, 2);
duk_put_global_string(ctx, "writeMemory");
// Execute embedded JavaScript code from build/index.js
duk_push_string(ctx, embedded_js_code);
duk_int_t returnCode = duk_peval(ctx);
if (returnCode != 0)
{
// Error occurred - display stack trace
duk_safe_to_stacktrace(ctx, -1);
k_printf((char *)duk_safe_to_string(ctx, -1), 1);
}
duk_pop(ctx);
}
unsigned int k_printf(char *message, unsigned int line)
{
char *vidmem = (char *)0xb8000;
unsigned int i = 0;
i = (line * 80 * 2);
while (*message != 0)
{
if (*message == '\n') // check for a new line
{
line++;
i = (line * 80 * 2);
*message++;
}
else
{
vidmem[i] = *message;
*message++;
i++;
vidmem[i] = WHITE_TXT;
i++;
};
};
return (1);
}

3779
src/lib/duk_config.h Normal file

File diff suppressed because it is too large Load Diff

1911
src/lib/duk_source_meta.json Normal file

File diff suppressed because it is too large Load Diff

101351
src/lib/duktape.c Normal file

File diff suppressed because it is too large Load Diff

1456
src/lib/duktape.h Normal file

File diff suppressed because it is too large Load Diff

33
src/lib/syscalls.c Normal file
View File

@@ -0,0 +1,33 @@
/*
* Minimal system stubs for freestanding picolibc
*/
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
/* Exit function - halt the system */
void _exit(int status)
{
(void)status;
/* Halt the CPU */
while (1)
{
__asm__ volatile("hlt");
}
}
/* Get time of day - not supported in freestanding */
int gettimeofday(struct timeval *tv, void *tz)
{
(void)tz;
if (tv)
{
tv->tv_sec = 0;
tv->tv_usec = 0;
}
return 0;
}
/* Sbrk - memory allocation (picolibc uses __heap_start and __heap_end from linker script) */
/* No need to implement sbrk as picolibc uses the linker symbols directly */

40
src/link.ld Normal file
View File

@@ -0,0 +1,40 @@
/*
* link.ld
*/
OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
SECTIONS
{
. = 0x100000;
.text : {
*(.text)
*(.text.*)
*(.rodata)
*(.rodata.*)
}
.data : {
*(.data)
*(.data.*)
}
.bss : {
*(.bss)
*(.bss.*)
*(COMMON)
}
/* Heap for malloc */
. = ALIGN(4096);
__heap_start = .;
. = . + 0x100000; /* 1MB heap */
__heap_end = .;
/* Stack grows downward from high memory */
. = ALIGN(4096);
__stack_bottom = .;
. = . + 0x10000; /* 64KB stack */
__stack_top = .;
}

36
src/os/index.js Normal file
View File

@@ -0,0 +1,36 @@
clearScreen();
print("Hello World!");
// writeMemory(0xb8000, 0x48);
// writeMemory(0xb8001, 0x0f);
// writeMemory(0xb8002, 0x65);
// writeMemory(0xb8003, 0x0f);
// writeMemory(0xb8004, 0x6c);
// writeMemory(0xb8005, 0x0f);
// writeMemory(0xb8006, 0x6c);
// writeMemory(0xb8007, 0x0f);
// writeMemory(0xb8008, 0x6f);
// writeMemory(0xb8009, 0x0f);
function clearScreen() {
var i = 0;
while (i < 80 * 25 * 2) {
writeMemory(0xb8000 + i, 0x00);
i++;
}
}
function printChar(char, offset) {
writeMemory(0xb8000 + offset * 2, char.charCodeAt(0));
writeMemory(0xb8000 + offset * 2 + 1, 0x0f); // White on black
}
function print(str) {
for (var i = 0; i < str.length; i++) {
printChar(str.charAt(i), i);
}
}