use core::arch::asm;
#[inline]
pub unsafe fn outb(port: u16, val: u8) {
asm!("outb %al, %dx", in("al") val, in("dx") port, options(att_syntax));
}
#[inline]
pub unsafe fn inb(port: u16) -> u8 {
let ret: u8;
asm!("inb %dx, %al", in("dx") port, out("al") ret, options(att_syntax));
ret
}
#[inline]
pub unsafe fn outw(port: u16, val: u16) {
asm!("outw %ax, %dx", in("ax") val, in("dx") port, options(att_syntax));
}
#[inline]
pub unsafe fn inw(port: u16) -> u16 {
let ret: u16;
asm!("inw %dx, %ax", in("dx") port, out("ax") ret, options(att_syntax));
ret
}
#[inline]
pub unsafe fn outl(port: u16, val: u32) {
asm!("outl %eax, %dx", in("eax") val, in("dx") port, options(att_syntax));
}
#[inline]
pub unsafe fn inl(port: u16) -> u32 {
let ret: u32;
asm!("inl %dx, %eax", out("eax") ret, in("dx") port, options(att_syntax));
ret
}
#[cfg(all(test, feature = "vmtest"))]
mod x86testing {
use super::*;
use x86test::*;
#[x86test(ioport(0x0, 0xaf))]
fn check_outb() {
unsafe {
outb(0x0, 0xaf);
}
}
#[x86test(ioport(0x0, 0xaf))]
#[should_panic]
fn check_outb_wrong_value() {
unsafe {
outb(0x0, 0xff);
}
}
#[x86test(ioport(0x1, 0xad))]
fn check_inb() {
unsafe {
kassert!(
inb(0x1) == 0xad,
"`inb` instruction didn't read the correct value"
);
}
}
#[x86test(ioport(0x2, 0xad))]
#[should_panic]
fn check_inb_wrong_port() {
unsafe {
kassert!(
inb(0x1) == 0xad,
"`inb` instruction didn't read the correct value"
);
}
}
#[x86test(ioport(0x2, 0x99))]
fn check_outw() {
unsafe {
super::outw(0x2, 0x99);
}
}
#[x86test(ioport(0x3, 0xfefe))]
fn check_inw() {
unsafe {
kassert!(
inw(0x3) == 0xfefe,
"`inw` instruction didn't read the correct value"
);
}
}
#[x86test(ioport(0x5, 0xbeefaaaa))]
fn check_outl() {
unsafe {
outl(0x5, 0xbeefaaaa);
}
}
#[x86test(ioport(0x4, 0xdeadbeef))]
fn check_inl() {
unsafe {
kassert!(
inl(0x4) == 0xdeadbeef,
"`inl` instruction didn't read the correct value"
);
}
}
}