aboutsummaryrefslogtreecommitdiffstats
path: root/src/ziggie.zig
blob: a986a47b093f644e407ef56a0c8a1778540b794f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
pub inline fn outb(port: u16, value: u8) void {
    asm volatile ("outb %[value], %[port]"
        :: [port] "N{dx}" (port), [value] "{al}" (value),
    );
}

const VGACell = packed struct {
    char: u8,
    foreground: u4 = 0xf,
    background: u4 = 0x0,
};

const VGATerminal = struct {
    const width = 80;
    const height = 25;
    const vram: *[width * height]VGACell = @ptrFromInt(0xB8000);

    offset: u16 = 0,

    pub fn put(self: *VGATerminal, ch: u8) void {
        vram[self.offset] = VGACell{ .char = ch };
        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);
        outb(0x03d5, @intCast(self.offset & 0xFF));
        outb(0x03d4, 0x0e);
        outb(0x03d5, @intCast(self.offset >> 8));
    }
};

export fn zigit() void {
    var vga = VGATerminal{};
    vga.put('H');
    vga.put('i');
    vga.updatecursor();
}