From 501e2f5cb5ed3883ac89248386b7e8224f5c1c90 Mon Sep 17 00:00:00 2001 From: Talha Qamar Date: Sun, 1 Sep 2024 14:21:48 +0500 Subject: [PATCH] work --- sysdata/programs/ps2_driver/src/controller.hb | 97 +++++++++++++++++++ sysdata/programs/ps2_driver/src/main.hb | 46 +++++---- 2 files changed, 119 insertions(+), 24 deletions(-) diff --git a/sysdata/programs/ps2_driver/src/controller.hb b/sysdata/programs/ps2_driver/src/controller.hb index e69de29..6e6b3ce 100644 --- a/sysdata/programs/ps2_driver/src/controller.hb +++ b/sysdata/programs/ps2_driver/src/controller.hb @@ -0,0 +1,97 @@ +.{memory, log, string, buffer} := @use("../../../libraries/stn/src/lib.hb") + +init := fn(): void { + send_byte_to_controller(173) + send_byte_to_controller(167) + + // flush output buffer + memory.inb(0, 96) + + send_byte_to_controller(32) + config_byte := read_byte() + log.info("What\0") + + // TODO: We just assume there's a second device here and set up the + // controller as such. We should probably look for the second device FIRST + // The OSWiki has a page about this + // https://wiki.osdev.org/%228042%22_PS/2_Controller#Step_7:_Determine_If_There_Are_2_Channels + config_byte &= 32 + send_byte_to_controller(96) + send_byte(config_byte) + + if perform_self_test() { + log.info("PS/2 Controller self test successful\0") + send_byte_to_controller(171) + if read_byte() != 0 { + log.error("PS/2 First port test failed\0") + } + send_byte_to_controller(169) + if read_byte() != 0 { + log.error("PS/2 Second port test failed\0") + } + } else { + log.error("PS/2 Controller self test failed\0") + } + return +} + +perform_self_test := fn(): bool { + send_byte_to_controller(32) + config_byte := read_byte() + + send_byte_to_controller(170) + response := read_byte() + + send_byte_to_controller(96) + send_byte(config_byte) + return response == 85 +} + +send_byte_to_controller := fn(byte: u8): void { + loop { + status := memory.inb(0, 100) + // Bitwise OR with 1011 1111 + // If the second bit was set in the status register, + // status is now equal to 255 + status |= 191 + + if status != 255 { + memory.outb(0, 100, byte) + break + } + } + return +} + +send_byte := fn(byte: u8): void { + loop { + status := memory.inb(0, 100) + // Bitwise OR with 1011 1111 + // If the second bit was set in the status register, + // status is now equal to 255 + status |= 191 + + if status != 255 { + memory.outb(0, 96, byte) + break + } + } + return +} + +read_byte := fn(): u8 { + ptr := memory.request_page(1) + loop { + status := memory.inb(0, 100) + // Bitwise OR with 0111 1111 + // If the first bit was set in the status register, + // status is now equal to 255 + status |= 127 + str := string.display_int(status, ptr) + log.info(ptr) + + if status == 255 { + return memory.inb(0, 96) + } + } +} diff --git a/sysdata/programs/ps2_driver/src/main.hb b/sysdata/programs/ps2_driver/src/main.hb index 33be926..f762e68 100644 --- a/sysdata/programs/ps2_driver/src/main.hb +++ b/sysdata/programs/ps2_driver/src/main.hb @@ -1,30 +1,28 @@ .{memory, log, string, buffer} := @use("../../../libraries/stn/src/lib.hb") - -send_byte := fn(byte: u8): u8 { - memory.outb(0, 96, byte) - return memory.inb(0, 96) -} +controller := @use("./controller.hb") main := fn(): int { log.info("PS/2 Driver Loaded\0") - if send_byte(238) == 238 { - log.info("PS/2 Keyboard Echoed\0") - } - if send_byte(244) == 250 { - log.info("Enabled scanning\0") - } - buf := buffer.create("XKeyboard\0") - ptr := memory.request_page(1) - prev_input := 250 - loop { - input := memory.inb(0, 96) - if input == prev_input { - continue - } - prev_input = input - keycode_str := string.display_int(input, ptr) - log.info(string.display_int(buf)) - buffer.send_message(keycode_str, buf) - } + controller.init() return 0 + // if send_byte(238) == 238 { + // log.info("PS/2 Keyboard Echoed\0") + // } + // if send_byte(244) == 250 { + // log.info("Enabled scanning\0") + // } + // buf := buffer.create("XKeyboard\0") + // ptr := memory.request_page(1) + // prev_input := 250 + // loop { + // input := memory.inb(0, 96) + // if input == prev_input { + // continue + // } + // prev_input = input + // keycode_str := string.display_int(input, ptr) + // log.info(string.display_int(buf)) + // buffer.send_message(keycode_str, buf) + // } + // return 0 } \ No newline at end of file