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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use core::arch::asm;
use core::fmt;
use core::marker::PhantomData;
pub use crate::structures::port::{PortRead, PortWrite};
impl PortRead for u8 {
#[inline]
unsafe fn read_from_port(port: u16) -> u8 {
let value: u8;
unsafe {
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack, preserves_flags));
}
value
}
}
impl PortRead for u16 {
#[inline]
unsafe fn read_from_port(port: u16) -> u16 {
let value: u16;
unsafe {
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack, preserves_flags));
}
value
}
}
impl PortRead for u32 {
#[inline]
unsafe fn read_from_port(port: u16) -> u32 {
let value: u32;
unsafe {
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack, preserves_flags));
}
value
}
}
impl PortWrite for u8 {
#[inline]
unsafe fn write_to_port(port: u16, value: u8) {
unsafe {
asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack, preserves_flags));
}
}
}
impl PortWrite for u16 {
#[inline]
unsafe fn write_to_port(port: u16, value: u16) {
unsafe {
asm!("out dx, ax", in("dx") port, in("ax") value, options(nomem, nostack, preserves_flags));
}
}
}
impl PortWrite for u32 {
#[inline]
unsafe fn write_to_port(port: u16, value: u32) {
unsafe {
asm!("out dx, eax", in("dx") port, in("eax") value, options(nomem, nostack, preserves_flags));
}
}
}
mod sealed {
pub trait Access {
const DEBUG_NAME: &'static str;
}
}
pub trait PortReadAccess: sealed::Access {}
pub trait PortWriteAccess: sealed::Access {}
#[derive(Debug)]
pub struct ReadOnlyAccess(());
impl sealed::Access for ReadOnlyAccess {
const DEBUG_NAME: &'static str = "ReadOnly";
}
impl PortReadAccess for ReadOnlyAccess {}
#[derive(Debug)]
pub struct WriteOnlyAccess(());
impl sealed::Access for WriteOnlyAccess {
const DEBUG_NAME: &'static str = "WriteOnly";
}
impl PortWriteAccess for WriteOnlyAccess {}
#[derive(Debug)]
pub struct ReadWriteAccess(());
impl sealed::Access for ReadWriteAccess {
const DEBUG_NAME: &'static str = "ReadWrite";
}
impl PortReadAccess for ReadWriteAccess {}
impl PortWriteAccess for ReadWriteAccess {}
pub struct PortGeneric<T, A> {
port: u16,
phantom: PhantomData<(T, A)>,
}
pub type Port<T> = PortGeneric<T, ReadWriteAccess>;
pub type PortReadOnly<T> = PortGeneric<T, ReadOnlyAccess>;
pub type PortWriteOnly<T> = PortGeneric<T, WriteOnlyAccess>;
impl<T, A> PortGeneric<T, A> {
#[inline]
pub const fn new(port: u16) -> PortGeneric<T, A> {
PortGeneric {
port,
phantom: PhantomData,
}
}
}
impl<T: PortRead, A: PortReadAccess> PortGeneric<T, A> {
#[inline]
pub unsafe fn read(&mut self) -> T {
unsafe { T::read_from_port(self.port) }
}
}
impl<T: PortWrite, A: PortWriteAccess> PortGeneric<T, A> {
#[inline]
pub unsafe fn write(&mut self, value: T) {
unsafe { T::write_to_port(self.port, value) }
}
}
impl<T, A: sealed::Access> fmt::Debug for PortGeneric<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PortGeneric")
.field("port", &self.port)
.field("size", &core::mem::size_of::<T>())
.field("access", &format_args!("{}", A::DEBUG_NAME))
.finish()
}
}
impl<T, A> Clone for PortGeneric<T, A> {
fn clone(&self) -> Self {
Self {
port: self.port,
phantom: PhantomData,
}
}
}
impl<T, A> PartialEq for PortGeneric<T, A> {
fn eq(&self, other: &Self) -> bool {
self.port == other.port
}
}
impl<T, A> Eq for PortGeneric<T, A> {}