-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathhellos.zig
115 lines (94 loc) · 2.66 KB
/
hellos.zig
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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.7.1!");
}
// 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);
}
};