initial commit
This commit is contained in:
commit
f617842dae
6 changed files with 159 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.zig-cache
|
||||||
|
zig-out
|
28
boot.s
Normal file
28
boot.s
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
.section .multiboot
|
||||||
|
.align 4
|
||||||
|
.set ALIGN, 1<<0
|
||||||
|
.set MEMINFO, 1<<1
|
||||||
|
.set MAGIC, 0x1BADB002
|
||||||
|
.set FLAGS, ALIGN|MEMINFO
|
||||||
|
.long MAGIC
|
||||||
|
.long FLAGS
|
||||||
|
.long -(MAGIC + FLAGS)
|
||||||
|
|
||||||
|
.section .bss
|
||||||
|
.align 16
|
||||||
|
stack_bottom:
|
||||||
|
.skip 16384
|
||||||
|
stack_top:
|
||||||
|
|
||||||
|
.section .text
|
||||||
|
.global _start
|
||||||
|
.type _start, @function
|
||||||
|
_start:
|
||||||
|
mov $stack_top, %esp
|
||||||
|
|
||||||
|
call kernel_main
|
||||||
|
|
||||||
|
cli
|
||||||
|
1: hlt
|
||||||
|
jmp 1b
|
||||||
|
.size _start, . - _start
|
32
build.zig
Normal file
32
build.zig
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
pub fn build(b: *Build) !void {
|
||||||
|
const optimize = std.builtin.OptimizeMode.Debug;
|
||||||
|
const target = b.resolveTargetQuery(.{
|
||||||
|
.cpu_arch = .x86,
|
||||||
|
.os_tag = .freestanding,
|
||||||
|
.abi = .none,
|
||||||
|
});
|
||||||
|
const exe = b.addExecutable(.{
|
||||||
|
.root_source_file = b.path("kernel.zig"),
|
||||||
|
.name = "multiboot",
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
exe.addCSourceFile(.{
|
||||||
|
.file = b.path("boot.s"),
|
||||||
|
});
|
||||||
|
exe.setLinkerScript(b.path("linker.ld"));
|
||||||
|
|
||||||
|
const iso_dir = b.addWriteFiles();
|
||||||
|
_ = iso_dir.addCopyFile(exe.getEmittedBin(), "boot/os.bin");
|
||||||
|
_ = iso_dir.addCopyFile(b.path("grub.cfg"), "boot/grub/grub.cfg");
|
||||||
|
const make_iso = b.addSystemCommand(&.{"i686-elf-grub-mkrescue"});
|
||||||
|
make_iso.addArg("-o");
|
||||||
|
const iso = make_iso.addOutputFileArg("lab.iso");
|
||||||
|
make_iso.addDirectoryArg(iso_dir.getDirectory());
|
||||||
|
|
||||||
|
b.getInstallStep()
|
||||||
|
.dependOn(&b.addInstallFile(iso, "lab.iso").step);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const Build = std.Build;
|
3
grub.cfg
Normal file
3
grub.cfg
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
menuentry "experiment" {
|
||||||
|
multiboot /boot/os.bin
|
||||||
|
}
|
71
kernel.zig
Normal file
71
kernel.zig
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
const vga = struct {
|
||||||
|
const width = 80;
|
||||||
|
const height = 25;
|
||||||
|
const screen: *[width * height]Cell = @ptrFromInt(0xB8000);
|
||||||
|
|
||||||
|
const Cell = packed struct(u16) {
|
||||||
|
char: u8,
|
||||||
|
style: Style = .default,
|
||||||
|
|
||||||
|
const blank: Cell = .{ .char = ' ' };
|
||||||
|
};
|
||||||
|
|
||||||
|
const Style = packed struct(u8) {
|
||||||
|
fg: Color,
|
||||||
|
bg: Color,
|
||||||
|
|
||||||
|
pub const default: Style = .{
|
||||||
|
.fg = .black,
|
||||||
|
.bg = .light_cyan,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const Color = enum(u4) {
|
||||||
|
black = 0,
|
||||||
|
blue = 1,
|
||||||
|
green = 2,
|
||||||
|
cyan = 3,
|
||||||
|
red = 4,
|
||||||
|
magenta = 5,
|
||||||
|
brown = 6,
|
||||||
|
light_grey = 7,
|
||||||
|
dark_grey = 8,
|
||||||
|
light_blue = 9,
|
||||||
|
light_green = 10,
|
||||||
|
light_cyan = 11,
|
||||||
|
light_red = 12,
|
||||||
|
light_magenta = 13,
|
||||||
|
light_brown = 14,
|
||||||
|
white = 15,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn init() void {
|
||||||
|
@memset(screen, .blank);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set(x: u32, y: u32, cell: Cell) void {
|
||||||
|
screen[x + y * width] = cell;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var current_x: u32 = 0;
|
||||||
|
var current_y: u32 = 0;
|
||||||
|
|
||||||
|
fn writeString(slice: []const u8, style: vga.Style) void {
|
||||||
|
for (slice) |char| {
|
||||||
|
vga.set(current_x, current_y, .{ .char = char, .style = style });
|
||||||
|
current_x += 1;
|
||||||
|
if (current_x >= vga.width) {
|
||||||
|
current_x = 0;
|
||||||
|
current_y += 1;
|
||||||
|
if (current_y >= vga.height) {
|
||||||
|
current_y = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn kernel_main() callconv(.c) void {
|
||||||
|
vga.init();
|
||||||
|
writeString("Hello Kernel", .default);
|
||||||
|
}
|
23
linker.ld
Normal file
23
linker.ld
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
ENTRY(_start)
|
||||||
|
SECTIONS
|
||||||
|
{
|
||||||
|
. = 2M;
|
||||||
|
.text : ALIGN(4K)
|
||||||
|
{
|
||||||
|
*(.multiboot)
|
||||||
|
*(.text)
|
||||||
|
}
|
||||||
|
.rodata : ALIGN(4K)
|
||||||
|
{
|
||||||
|
*(.rodata)
|
||||||
|
}
|
||||||
|
.data : ALIGN(4K)
|
||||||
|
{
|
||||||
|
*(data)
|
||||||
|
}
|
||||||
|
.bss : ALIGN(4K)
|
||||||
|
{
|
||||||
|
*(COMMON)
|
||||||
|
*(.bss)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue