uh-oh I'm learning zig

main
Clyne 3 weeks ago
parent 19d9a04e36
commit 71eecf3849
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -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)

@ -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);

@ -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;
}

@ -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();
}
Loading…
Cancel
Save