]> code.bitgloo.com Git - clyne/osdev.git/commitdiff
uh-oh I'm learning zig
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 3 Oct 2024 00:37:29 +0000 (20:37 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 3 Oct 2024 00:37:29 +0000 (20:37 -0400)
Makefile
src/kernel.cpp
src/memory.cpp
src/ziggie.zig [new file with mode: 0644]

index 2484913f5806d3e6d2c9725fc9670729bbb744c1..03fa2f113e6ec7b95f18ee8c4a4848bd1fa37cc9 100644 (file)
--- 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)
index ae3f153dd36fbb90c7a731d7379e68220cbf0f9c..f881143d8fb9bd4c3aabbad1b91311d3d355a378 100644 (file)
@@ -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);
index 108c240c27d56ef593b220dcfc7949b5da0ef728..69c944a014d17c83552cd3cb17c81fe618b75da0 100644 (file)
@@ -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<char *>(dst);
+    auto s = reinterpret_cast<const char *>(src);
+
+    while (sz--)
+        *d++ = *s++;
+
+    return dst;
+}
+
+extern "C"
+void *memset(void *dst, int val, std::size_t sz)
+{
+    auto d = reinterpret_cast<char *>(dst);
+
+    while (sz--)
+        *d++ = val;
+
+    return dst;
+}
+
diff --git a/src/ziggie.zig b/src/ziggie.zig
new file mode 100644 (file)
index 0000000..c6747b9
--- /dev/null
@@ -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();
+}
+