#[derive(Copy, Clone)]
pub struct Pio<T> {
port: u16,
value: PhantomData<T>,
}
impl<T> Pio<T> {
pub const fn new(port: u16) -> Self {
return Pio{
port,
value: PhantomData
};
}
}
impl HardwareIo for Pio<u8> {
type Value = u8;
fn read(&self) -> Self::Value {
let value: u8;
unsafe{
#[cfg(any(target_arch="x86", target_arch="x86_64"))]
asm!("in al, dx", in("dx") self.port, out("al") value, options(nostack, nomem, preserves_flags));
}
return value;
}
fn write(&mut self, value: Self::Value) {
unsafe {
#[cfg(any(target_arch="x86", target_arch="x86_64"))]
asm!("out dx, al", in("dx") self.port, in("al") value, options(nostack, nomem, preserves_flags));
}
}
}
impl HardwareIo for Pio<u16> {
type Value = u16;
fn read(&self) -> Self::Value {
let value: u16;
unsafe{
#[cfg(any(target_arch="x86", target_arch="x86_64"))]
asm!("in ax, dx", in("dx") self.port, out("ax") value, options(nostack, nomem, preserves_flags));
}
return value;
}
fn write(&mut self, value: Self::Value) {
unsafe{
#[cfg(any(target_arch="x86", target_arch="x86_64"))]
asm!("out dx, ax", in("dx") self.port, in("ax") value, options(nostack, nomem, preserves_flags));
}
}
}
impl HardwareIo for Pio<u32> {
type Value = u32;
fn read(&self) -> Self::Value {
let value: u32;
unsafe{
#[cfg(target_arch="x86_64")]
asm!("in eax, dx", in("dx") self.port, out("eax") value, options(nostack, nomem, preserves_flags));
}
return value;
}
fn write(&mut self, value: Self::Value) {
unsafe{
#[cfg(target_arch="x86_64")]
asm!("out dx, eax", in("dx") self.port, in("eax") value, options(nostack, nomem, preserves_flags));
}
}
}
use {
crate::io::HardwareIo,
core::{
arch::asm,
marker::PhantomData,
},
};