Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active June 24, 2020 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/e81128eae5adc6293d5c1bbeef3a3380 to your computer and use it in GitHub Desktop.
Save peterhellberg/e81128eae5adc6293d5c1bbeef3a3380 to your computer and use it in GitHub Desktop.
HellOS patched to compile using Zig 0.6.0 https://github.com/andrewrk/HellOS
diff --git a/hellos.zig b/hellos.zig
index 8563852..506f2c9 100644
--- a/hellos.zig
+++ b/hellos.zig
@@ -20,8 +20,9 @@ export var multiboot align(4) linksection(".multiboot") = MultiBoot{
export var stack_bytes: [16 * 1024]u8 align(16) linksection(".bss") = undefined;
const stack_bytes_slice = stack_bytes[0..];
-export nakedcc fn _start() noreturn {
- @newStackCall(stack_bytes_slice, kmain);
+export fn _start() callconv(.Naked) noreturn {
+ @call(.{ .stack = stack_bytes_slice }, kmain, .{});
+
while (true) {}
}
@@ -34,7 +35,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn
fn kmain() void {
terminal.initialize();
- terminal.write("Hello, kernel World!");
+ terminal.write("Hello, Kernel World from Zig 0.6.0!");
}
// Hardware text mode color constants
@@ -61,23 +62,26 @@ fn vga_entry_color(fg: VgaColor, bg: VgaColor) u8 {
}
fn vga_entry(uc: u8, color: u8) u16 {
- return u16(uc) | (u16(color) << 8);
+ var c: u16 = color;
+
+ return uc | (c << 8);
}
const VGA_WIDTH = 80;
const VGA_HEIGHT = 25;
const terminal = struct {
- var row = usize(0);
- var column = usize(0);
+ var row: usize = 0;
+ var column: usize = 0;
+
var color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
const buffer = @intToPtr([*]volatile u16, 0xB8000);
fn initialize() void {
- var y = usize(0);
+ var y: usize = 0;
while (y < VGA_HEIGHT) : (y += 1) {
- var x = usize(0);
+ var x: usize = 0;
while (x < VGA_WIDTH) : (x += 1) {
putCharAt(' ', color, x, y);
}
const builtin = @import("builtin");
const MultiBoot = packed struct {
magic: i32,
flags: i32,
checksum: i32,
};
const ALIGN = 1 << 0;
const MEMINFO = 1 << 1;
const MAGIC = 0x1BADB002;
const FLAGS = ALIGN | MEMINFO;
export var multiboot align(4) linksection(".multiboot") = MultiBoot{
.magic = MAGIC,
.flags = FLAGS,
.checksum = -(MAGIC + FLAGS),
};
export var stack_bytes: [16 * 1024]u8 align(16) linksection(".bss") = undefined;
const stack_bytes_slice = stack_bytes[0..];
export fn _start() callconv(.Naked) noreturn {
@call(.{ .stack = stack_bytes_slice }, kmain, .{});
while (true) {}
}
pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
@setCold(true);
terminal.write("KERNEL PANIC: ");
terminal.write(msg);
while (true) {}
}
fn kmain() void {
terminal.initialize();
terminal.write("Hello, Kernel World from Zig 0.6.0!");
}
// Hardware text mode color constants
const VgaColor = u8;
const VGA_COLOR_BLACK = 0;
const VGA_COLOR_BLUE = 1;
const VGA_COLOR_GREEN = 2;
const VGA_COLOR_CYAN = 3;
const VGA_COLOR_RED = 4;
const VGA_COLOR_MAGENTA = 5;
const VGA_COLOR_BROWN = 6;
const VGA_COLOR_LIGHT_GREY = 7;
const VGA_COLOR_DARK_GREY = 8;
const VGA_COLOR_LIGHT_BLUE = 9;
const VGA_COLOR_LIGHT_GREEN = 10;
const VGA_COLOR_LIGHT_CYAN = 11;
const VGA_COLOR_LIGHT_RED = 12;
const VGA_COLOR_LIGHT_MAGENTA = 13;
const VGA_COLOR_LIGHT_BROWN = 14;
const VGA_COLOR_WHITE = 15;
fn vga_entry_color(fg: VgaColor, bg: VgaColor) u8 {
return fg | (bg << 4);
}
fn vga_entry(uc: u8, color: u8) u16 {
var c: u16 = color;
return uc | (c << 8);
}
const VGA_WIDTH = 80;
const VGA_HEIGHT = 25;
const terminal = struct {
var row: usize = 0;
var column: usize = 0;
var color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
const buffer = @intToPtr([*]volatile u16, 0xB8000);
fn initialize() void {
var y: usize = 0;
while (y < VGA_HEIGHT) : (y += 1) {
var x: usize = 0;
while (x < VGA_WIDTH) : (x += 1) {
putCharAt(' ', color, x, y);
}
}
}
fn setColor(new_color: u8) void {
color = new_color;
}
fn putCharAt(c: u8, new_color: u8, x: usize, y: usize) void {
const index = y * VGA_WIDTH + x;
buffer[index] = vga_entry(c, new_color);
}
fn putChar(c: u8) void {
putCharAt(c, color, column, row);
column += 1;
if (column == VGA_WIDTH) {
column = 0;
row += 1;
if (row == VGA_HEIGHT)
row = 0;
}
}
fn write(data: []const u8) void {
for (data) |c|
putChar(c);
}
};
ENTRY(_start)
SECTIONS {
. = 1M;
.text : ALIGN(4K) {
KEEP(*(.multiboot))
*(.text)
}
.rodata : ALIGN(4K) {
*(.rodata)
}
.data : ALIGN(4K) {
*(.data)
}
.bss : ALIGN(4K) {
*(COMMON)
*(.bss)
}
}
@peterhellberg
Copy link
Author

peterhellberg commented Jun 24, 2020

hellos

HellOS

Bare bones "hello world" i386 kernel written in Zig.

Building

zig build-exe hellos.zig -target i386-freestanding --linker-script linker.ld

Testing with qemu

qemu-system-i386 -kernel hellos

@peterhellberg
Copy link
Author

peterhellberg commented Jun 24, 2020

Changing the color used before each call to putCharAt

setColor(@intCast(u8, x));

Colorful HellOS

setColor(@intCast(u8, x + y));

Diagonal stripes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment