Skip to content

Commit

Permalink
Merge pull request #4 from Kbz-8/kfs-1
Browse files Browse the repository at this point in the history
Kfs 1
  • Loading branch information
Kbz-8 authored Jul 13, 2024
2 parents 8e0b95b + 71b657d commit 349b96e
Show file tree
Hide file tree
Showing 10 changed files with 566 additions and 611 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ jobs:
run: |
sudo apt update
sudo apt install -y wget tar xz-utils mtools xorriso build-essential grub2-common
wget https://ziglang.org/download/0.12.0/zig-linux-x86_64-0.12.0.tar.xz
tar xf ./zig-linux-x86_64-0.12.0.tar.xz
sudo mv zig-linux-x86_64-0.12.0/* /bin
wget https://ziglang.org/download/0.13.0/zig-linux-x86_64-0.13.0.tar.xz
tar xf ./zig-linux-x86_64-0.13.0.tar.xz
sudo mv zig-linux-x86_64-0.13.0/* /bin
- name: Building
run: make
Expand Down
12 changes: 6 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ pub fn build(b: *std.Build) void
{
const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_source_file = .{ .path = "sources/kernel/kmain.zig" },
.root_source_file = .{ .src_path = .{ .owner = b, .sub_path = "sources/kernel/kmain.zig" } },
.target = b.resolveTargetQuery(.{
.cpu_arch = .x86,
.abi = .none,
.os_tag = .freestanding,
}),
.optimize = .ReleaseSmall,
// .strip = true,
.optimize = .Debug,
.strip = true,
.code_model = .kernel,
.pic = false,
.error_tracing = false,
});
kernel.setLinkerScriptPath(.{ .path = "linker.ld" });
kernel.setLinkerScriptPath(.{ .src_path = .{ .owner = b, .sub_path = "linker.ld" } });

const drivers_module = b.addModule("drivers", .{
.root_source_file = .{ .path = "sources/drivers/index.zig" }
.root_source_file = .{ .src_path = .{ .owner = b, .sub_path = "sources/drivers/index.zig" } }
});

drivers_module.addImport("kernel", &kernel.root_module);
Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn build(b: *std.Build) void
iso_step.dependOn(&iso_cmd.step);
b.default_step.dependOn(iso_step);

const run_cmd_str = &[_][]const u8{ "qemu-system-i386", "-cdrom", iso_path, "-machine", "type=pc-i440fx-3.1" };
const run_cmd_str = &[_][]const u8{ "qemu-system-i386", "-cdrom", iso_path };

const run_cmd = b.addSystemCommand(run_cmd_str);
run_cmd.step.dependOn(b.getInstallStep());
Expand Down
151 changes: 67 additions & 84 deletions sources/drivers/keyboard/keyboard.zig
Original file line number Diff line number Diff line change
@@ -1,125 +1,108 @@
const kernel = @import("kernel");
const vga = @import("../vga/vga.zig");
var capsOn : bool = false;
var capsLock : bool = false;

const UNKNOWN : u32 = 0xFFFFFFFF;
const ESC : u32 = 0xFFFFFFFF - 1;
const CTRL : u32 = 0xFFFFFFFF - 2;
const LSHFT : u32 = 0xFFFFFFFF - 3;
const RSHFT : u32 = 0xFFFFFFFF - 4;
const ALT : u32 = 0xFFFFFFFF - 5;
const F1 : u32 = 0xFFFFFFFF - 6;
const F2 : u32 = 0xFFFFFFFF - 7;
const F3 : u32 = 0xFFFFFFFF - 8;
const F4 : u32 = 0xFFFFFFFF - 9;
const F5 : u32 = 0xFFFFFFFF - 10;
const F6 : u32 = 0xFFFFFFFF - 11;
const F7 : u32 = 0xFFFFFFFF - 12;
const F8 : u32 = 0xFFFFFFFF - 13;
const F9 : u32 = 0xFFFFFFFF - 14;
const F10 : u32 = 0xFFFFFFFF - 15;
const F11 : u32 = 0xFFFFFFFF - 16;
const F12 : u32 = 0xFFFFFFFF - 17;
const SCRLCK : u32 = 0xFFFFFFFF - 18;
const HOME : u32 = 0xFFFFFFFF - 19;
const UP : u32 = 0xFFFFFFFF - 20;
const LEFT : u32 = 0xFFFFFFFF - 21;
const RIGHT : u32 = 0xFFFFFFFF - 22;
const DOWN : u32 = 0xFFFFFFFF - 23;
const PGUP : u32 = 0xFFFFFFFF - 24;
const PGDOWN : u32 = 0xFFFFFFFF - 25;
const END : u32 = 0xFFFFFFFF - 26;
const INS : u32 = 0xFFFFFFFF - 27;
const DEL : u32 = 0xFFFFFFFF - 28;
const CAPS : u32 = 0xFFFFFFFF - 29;
const NONE : u32 = 0xFFFFFFFF - 30;
const ALTGR : u32 = 0xFFFFFFFF - 31;
const NUMLCK : u32 = 0xFFFFFFFF - 32;
var caps_on: bool = false;
var caps_lock: bool = false;

const UNKNOWN: u32 = 0xFFFFFFFF;
const ESC: u32 = 0xFFFFFFFF - 1;
const CTRL: u32 = 0xFFFFFFFF - 2;
const LSHFT: u32 = 0xFFFFFFFF - 3;
const RSHFT: u32 = 0xFFFFFFFF - 4;
const ALT: u32 = 0xFFFFFFFF - 5;
const F1: u32 = 0xFFFFFFFF - 6;
const F2: u32 = 0xFFFFFFFF - 7;
const F3: u32 = 0xFFFFFFFF - 8;
const F4: u32 = 0xFFFFFFFF - 9;
const F5: u32 = 0xFFFFFFFF - 10;
const F6: u32 = 0xFFFFFFFF - 11;
const F7: u32 = 0xFFFFFFFF - 12;
const F8: u32 = 0xFFFFFFFF - 13;
const F9: u32 = 0xFFFFFFFF - 14;
const F10: u32 = 0xFFFFFFFF - 15;
const F11: u32 = 0xFFFFFFFF - 16;
const F12: u32 = 0xFFFFFFFF - 17;
const SCRLCK: u32 = 0xFFFFFFFF - 18;
const HOME: u32 = 0xFFFFFFFF - 19;
const UP: u32 = 0xFFFFFFFF - 20;
const LEFT: u32 = 0xFFFFFFFF - 21;
const RIGHT: u32 = 0xFFFFFFFF - 22;
const DOWN: u32 = 0xFFFFFFFF - 23;
const PGUP: u32 = 0xFFFFFFFF - 24;
const PGDOWN: u32 = 0xFFFFFFFF - 25;
const END: u32 = 0xFFFFFFFF - 26;
const INS: u32 = 0xFFFFFFFF - 27;
const DEL: u32 = 0xFFFFFFFF - 28;
const CAPS: u32 = 0xFFFFFFFF - 29;
const NONE: u32 = 0xFFFFFFFF - 30;
const ALTGR: u32 = 0xFFFFFFFF - 31;
const NUMLCK: u32 = 0xFFFFFFFF - 32;

const lowercase = [128]u32 {
UNKNOWN,ESC,'1','2','3','4','5','6','7','8',
'9','0','-','=',0x08,'\t','q','w','e','r',
't','y','u','i','o','p','[',']','\n',CTRL,
'a','s','d','f','g','h','j','k','l',';',
'\'','`',LSHFT,'\\','z','x','c','v','b','n','m',',',
'.','/',RSHFT,'*',ALT,' ',CAPS,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,NUMLCK,SCRLCK,HOME,UP,PGUP,'-',LEFT,UNKNOWN,RIGHT,
'+',END,DOWN,PGDOWN,INS,DEL,UNKNOWN,UNKNOWN,UNKNOWN,F11,F12,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN
UNKNOWN,ESC,'1','2','3','4','5','6','7','8',
'9','0','-','=',0x08,'\t','q','w','e','r',
't','y','u','i','o','p','[',']','\n',CTRL,
'a','s','d','f','g','h','j','k','l',';',
'\'','`',LSHFT,'\\','z','x','c','v','b','n','m',',',
'.','/',RSHFT,'*',ALT,' ',CAPS,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,NUMLCK,SCRLCK,HOME,UP,PGUP,'-',LEFT,UNKNOWN,RIGHT,
'+',END,DOWN,PGDOWN,INS,DEL,UNKNOWN,UNKNOWN,UNKNOWN,F11,F12,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN
};

const uppercase = [128]u32 {
UNKNOWN,ESC,'!','@','#','$','%','^','&','*','(',')','_','+',0x08,'\t','Q','W','E','R',
'T','Y','U','I','O','P','{','}','\n',CTRL,'A','S','D','F','G','H','J','K','L',':','"','~',LSHFT,'|','Z','X','C',
'V','B','N','M','<','>','?',RSHFT,'*',ALT,' ',CAPS,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,NUMLCK,SCRLCK,HOME,UP,PGUP,'-',
LEFT,UNKNOWN,RIGHT,'+',END,DOWN,PGDOWN,INS,DEL,UNKNOWN,UNKNOWN,UNKNOWN,F11,F12,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN
'T','Y','U','I','O','P','{','}','\n',CTRL,'A','S','D','F','G','H','J','K','L',':','"','~',LSHFT,'|','Z','X','C',
'V','B','N','M','<','>','?',RSHFT,'*',ALT,' ',CAPS,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,NUMLCK,SCRLCK,HOME,UP,PGUP,'-',
LEFT,UNKNOWN,RIGHT,'+',END,DOWN,PGDOWN,INS,DEL,UNKNOWN,UNKNOWN,UNKNOWN,F11,F12,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN
};


var keybuffer: [256]u8 = .{0} ** 256;

pub fn keyboardHandler(regs : *kernel.idt.IDT_Register) void
pub fn keyboardHandler(regs: *kernel.idt.IDTRegister) void
{
_ = regs;
const scanCode = kernel.ports.in(u8, 0x60) & 0x7F;
const scan_code = kernel.ports.in(u8, 0x60) & 0x7F;
const press = kernel.ports.in(u8, 0x60) & 0x80;
switch (scanCode)

switch(scan_code)
{
1, 29, 56, 59...68, 87, 88 => return,
42 =>
{
//shift key
if (press == 0)
{
capsOn = true;
}
else
{
capsOn = false;
}
caps_on = press == 0;
return;
},
58 =>
{
if (!capsLock and press == 0)
{
capsLock = true;
}
else if (capsLock and press == 0)
{
capsLock = false;
}
if(!caps_lock and press == 0)
caps_lock = true
else if(caps_lock and press == 0)
caps_lock = false;
return;
},
else =>
{
if (press == 0)
{
if (capsOn or capsLock)
{
vga.putChar(@truncate(uppercase[scanCode]));
}
else
{
vga.putChar(@truncate(lowercase[scanCode]));
}
}
if(press != 0)
return;
if(caps_on or caps_lock)
vga.putChar(@truncate(uppercase[scan_code]))
else
vga.putChar(@truncate(lowercase[scan_code]));
}
}

}

pub fn init() void
{
@setCold(true);
kernel.logs.klog("[PS/2 Keyboard Driver] loading...");
kernel.idt.irq_install_handler(1, &keyboardHandler);
kernel.idt.irqInstallHandler(1, &keyboardHandler);
kernel.logs.klog("[PS/2 Keyboard Driver] loaded");
}

85 changes: 36 additions & 49 deletions sources/drivers/vga/vga.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Screen = struct
row: usize,
column: usize,
color: u8,
var buffer = [2000]u16;
var buffer = [2000]u16;
};

const VGA = struct
Expand All @@ -36,8 +36,8 @@ const VGA = struct
column: usize,
color: u8,
buffer: [*]volatile u16 = @ptrFromInt(0xB8000),
screensArray: [8]Screen,
currentScreen: u8,
screensArray: [8]Screen,
currentScreen: u8,
};

fn computeColor(fg: Color, bg: Color) u8
Expand All @@ -55,30 +55,28 @@ var vga = VGA
.row = 0,
.column = 0,
.color = computeColor(Color.WHITE, Color.BLACK),
.screensArray =.{.{.row = 0, .column = 0, .color = computeColor(Color.WHITE, Color.BLACK)}} ** 8,
.currentScreen = 0,
.screensArray =.{.{.row = 0, .column = 0, .color = computeColor(Color.WHITE, Color.BLACK)}} ** 8,
.currentScreen = 0,
};

pub fn changeScreen(targetScreen: u8) void
{
if (targetScreen == vga.currentScreen or targetScreen < 0 or targetScreen >= 8)
return;
for (vga.buffer, 0..) |val, i|
vga.screensArray[vga.currentScreen].buffer[i] = val;
if(targetScreen == vga.currentScreen or targetScreen < 0 or targetScreen >= 8)
return;
for (vga.buffer, 0..) |val, i|
vga.screensArray[vga.currentScreen].buffer[i] = val;

vga.screensArray[vga.currentScreen].row = vga.row;
vga.screensArray[vga.currentScreen].column = vga.column;
vga.screensArray[vga.currentScreen].color = vga.color;

for (vga.screensArray[targetScreen].buffer, 0..) |val, i|
vga.buffer[i] = val;
vga.screensArray[vga.currentScreen].row = vga.row;
vga.screensArray[vga.currentScreen].column = vga.column;
vga.screensArray[vga.currentScreen].color = vga.color;

vga.row = vga.screensArray[targetScreen].row;
vga.column = vga.screensArray[targetScreen].column;
vga.color = vga.screensArray[targetScreen].color;
vga.currentScreen = targetScreen;
for (vga.screensArray[targetScreen].buffer, 0..) |val, i|
vga.buffer[i] = val;

return;
vga.row = vga.screensArray[targetScreen].row;
vga.column = vga.screensArray[targetScreen].column;
vga.color = vga.screensArray[targetScreen].color;
vga.currentScreen = targetScreen;
}

fn putEntry(c: u8, color: u8, x: usize, y: usize) void
Expand All @@ -88,50 +86,40 @@ fn putEntry(c: u8, color: u8, x: usize, y: usize) void

pub fn reverseScroll() void
{
for (0..(vga.height - 1)) |x|
{
for (0..vga.width) |y|
{
vga.buffer[x * vga.width + y] = vga.buffer[(x + 1) * vga.width + y];
}
}
for (0..vga.width) |y|
{
vga.buffer[y] = 0;
}
for (0..(vga.height - 1)) |x|
{
for (0..vga.width) |y|
vga.buffer[x * vga.width + y] = vga.buffer[(x + 1) * vga.width + y];
}
for (0..vga.width) |y|
vga.buffer[y] = 0;
}

pub fn scroll() void
{
for (1..vga.height) |x|
{
for (0..vga.width) |y|
{
vga.buffer[(x - 1) * vga.width + y] = vga.buffer[x * vga.width + y];
}
}
for (0..vga.width) |y|
{
vga.buffer[(vga.height - 1) * vga.width + y] = 0;
}
for (1..vga.height) |x|
{
for (0..vga.width) |y|
vga.buffer[(x - 1) * vga.width + y] = vga.buffer[x * vga.width + y];
}
for (0..vga.width) |y|
vga.buffer[(vga.height - 1) * vga.width + y] = 0;
vga.column -= 1;
}

pub fn putChar(c: u8) void
{
if (c == 0)
if(c == 0)
return;
if (c >= ' ')
if(c >= ' ')
putEntry(c, vga.color, vga.row, vga.column);
vga.row += 1;
if (vga.row == vga.width or c == '\n')
if(vga.row == vga.width or c == '\n')
{
vga.row = 0;
vga.column += 1;
if (vga.column == vga.height)
{
if(vga.column == vga.height)
scroll();
}
}
}

Expand Down Expand Up @@ -168,6 +156,5 @@ pub fn init() void
{
kernel.logs.klog("[VGA Driver] loading...");
clear(Color.BLACK);

kernel.logs.klog("[VGA Driver] loaded");
}
Loading

0 comments on commit 349b96e

Please sign in to comment.