1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Access to I/O space

use core::arch::asm;

/// Read an 8-bit value from the port at the given address in I/O space.
///
/// # Safety
/// This is an extremely low-level operation that can do anything: trigger an interrupt,
/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
/// caution.
pub unsafe fn in_u8(addr: u16) -> u8 {
    let data: u8;
    asm!(
        "in al, dx",
        out("al") data,
        in("dx") addr,
    );
    data
}

/// Read a 16-bit value from the port at the given address in I/O space.
///
/// # Safety
/// This is an extremely low-level operation that can do anything: trigger an interrupt,
/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
/// caution.
pub unsafe fn in_u16(addr: u16) -> u16 {
    let data: u16;
    asm!(
        "in ax, dx",
        out("ax") data,
        in("dx") addr,
    );
    data
}

/// Read a 32-bit value from the port at the given address in I/O space.
///
/// # Safety
/// This is an extremely low-level operation that can do anything: trigger an interrupt,
/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
/// caution.
pub unsafe fn in_u32(addr: u16) -> u32 {
    let data: u32;
    asm!(
        "in eax, dx",
        out("eax") data,
        in("dx") addr,
    );
    data
}

/// Write an 8-bit value to the port at the given address in I/O space.
///
/// # Safety
/// This is an extremely low-level operation that can do anything: trigger an interrupt,
/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
/// caution.
pub unsafe fn out_u8(addr: u16, data: u8) {
    asm!(
        "out dx, al",
        in("al") data,
        in("dx") addr,
    );
}

/// Write a 16-bit value to the port at the given address in I/O space.
///
/// # Safety
/// This is an extremely low-level operation that can do anything: trigger an interrupt,
/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
/// caution.
pub unsafe fn out_u16(addr: u16, data: u16) {
    asm!(
        "out dx, ax",
        in("ax") data,
        in("dx") addr,
    );
}

/// Write a 16-bit value to the port at the given address in I/O space.
///
/// # Safety
/// This is an extremely low-level operation that can do anything: trigger an interrupt,
/// overwrite arbitrary memory with DMA, shut down the system, launch a missile. Use with
/// caution.
pub unsafe fn out_u32(addr: u16, data: u32) {
    asm!(
        "out dx, eax",
        in("eax") data,
        in("dx") addr,
    );
}