diff --git a/kernel/src/x86_64.rs b/kernel/src/x86_64.rs index 319fc18..67595b6 100644 --- a/kernel/src/x86_64.rs +++ b/kernel/src/x86_64.rs @@ -12,3 +12,38 @@ pub fn halt_loop() -> ! { hlt(); } } + +#[repr(transparent)] +pub struct PortU16(pub u16); + +impl PortU16 { + pub fn offset(&self, offset: u16) -> Self { + Self(self.0 + offset) + } + + #[inline] + pub unsafe fn read(&self) -> u16 { + let value: u16; + unsafe { + core::arch::asm!( + "in ax, dx", + in("dx") self.0, + out("ax") value, + options(nomem, nostack, preserves_flags) + ); + } + value + } + + #[inline] + pub unsafe fn write(&self, value: u16) { + unsafe { + core::arch::asm!( + "out dx, ax", + in("dx") self.0, + in("ax") value, + options(nomem, nostack, preserves_flags) + ); + } + } +}