diff --git a/Makefile b/Makefile index 2484913..03fa2f1 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,12 @@ CXXFLAGS := -m32 -ggdb -g3 -O1 -fno-pic -fno-rtti -fno-exceptions -std=c++23 \ -Wall -Wextra -pedantic -Werror LDFLAGS := $(CXXFLAGS) -T link.ld -static -nostdlib -fno-use-cxa-atexit -CXXFILES := $(wildcard src/*.cpp) +CXXFILES := $(wildcard src/*.cpp) $(wildcard src/*.zig) PROJECT := myos PRJISO := $(PROJECT).iso PRJBIN := $(PROJECT).bin -OBJS := $(patsubst src/%.cpp,out/%.o,$(CXXFILES)) +OBJS := $(patsubst src/%.cpp,out/%.o,$(patsubst src/%.zig,out/%.o,$(CXXFILES))) all: $(PRJISO) @@ -26,6 +26,11 @@ out/%.o: src/%.cpp @echo " CXX " $< @g++ $(CXXFLAGS) -c $< -o $@ +out/%.o: src/%.zig + @echo " ZIG " $< + @zig build-obj -target x86-freestanding -O ReleaseSmall $< + @mv $(patsubst src/%.zig,%.o,$<)* out/ + clean: @echo " CLEAN" @rm -f $(OBJS) $(PRJBIN) $(PRJISO) diff --git a/src/kernel.cpp b/src/kernel.cpp index ae3f153..f881143 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -34,6 +34,8 @@ void ata_probe(auto bus, ATA::Drive drv, const char *name) } } +extern "C" void zigit(); + void kernel_main(void) { term.write("Clyne's kernel, v2024\n\n"); @@ -41,6 +43,8 @@ void kernel_main(void) if (!multiboot_initialize()) for (;;); + zigit(); + idt_register_callback(14, [](auto& regs) { term.write("Page fault! eip="); term.write(regs.eip); diff --git a/src/memory.cpp b/src/memory.cpp index 108c240..69c944a 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -105,3 +105,26 @@ void operator delete[](void *, std::size_t) } +extern "C" +void *memcpy(void *dst, const void *src, std::size_t sz) +{ + auto d = reinterpret_cast(dst); + auto s = reinterpret_cast(src); + + while (sz--) + *d++ = *s++; + + return dst; +} + +extern "C" +void *memset(void *dst, int val, std::size_t sz) +{ + auto d = reinterpret_cast(dst); + + while (sz--) + *d++ = val; + + return dst; +} + diff --git a/src/ziggie.zig b/src/ziggie.zig new file mode 100644 index 0000000..c6747b9 --- /dev/null +++ b/src/ziggie.zig @@ -0,0 +1,49 @@ +pub inline fn outb(port: u16, value: u8) void { + asm volatile ("outb %[value], %[port]" + : + : [port] "N{dx}" (port), + [value] "{al}" (value), + ); +} + +const VGATerminal = struct { + const width = 80; + const height = 25; + const vram: *[width * height]u16 = @ptrFromInt(0xB8000); + + offset: u16 = 0, + foreground: u8 = 0x07, + background: u8 = 0x00, + + pub fn put(self: *VGATerminal, ch: u8) void { + const cell = ch + | (@as(u16, self.foreground) << 8) + | (@as(u16, self.background) << 12); + + vram[self.offset] = cell; + self.offset += 1; + } + + fn checkpos(self: *VGATerminal) void { + if (self.offset >= width * height) { + const onebefore = (width - 1) * height; + @memcpy(vram, vram[width..onebefore]); + @memset(vram[onebefore..], 0); + self.offset = onebefore; + } + } + + pub fn updatecursor(self: VGATerminal) void { + outb(0x03d4, 0x0f); + _ = self; + } +}; + +export fn zigit() void { + var vga = VGATerminal{}; + vga.foreground = 0x0f; + vga.put('H'); + vga.put('i'); + vga.updatecursor(); +} +