1use core::{
4 cmp::PartialEq,
5 ops::{BitAnd, BitOr, Not},
6};
7
8pub use self::{mmio::*, mmio_ptr::*};
9
10#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
11pub use self::pio::*;
12
13mod mmio;
14mod mmio_ptr;
15
16#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
17mod pio;
18
19pub trait Io {
20 type Value: Copy
21 + PartialEq
22 + BitAnd<Output = Self::Value>
23 + BitOr<Output = Self::Value>
24 + Not<Output = Self::Value>;
25
26 fn read(&self) -> Self::Value;
27 fn write(&mut self, value: Self::Value);
28
29 #[inline(always)]
30 fn readf(&self, flags: Self::Value) -> bool {
31 (self.read() & flags) as Self::Value == flags
32 }
33
34 #[inline(always)]
35 fn writef(&mut self, flags: Self::Value, value: bool) {
36 let tmp: Self::Value = match value {
37 true => self.read() | flags,
38 false => self.read() & !flags,
39 };
40 self.write(tmp);
41 }
42}
43
44pub struct ReadOnly<I> {
45 inner: I,
46}
47
48impl<I> ReadOnly<I> {
49 pub const fn new(inner: I) -> ReadOnly<I> {
50 ReadOnly { inner }
51 }
52}
53
54impl<I: Io> ReadOnly<I> {
55 #[inline(always)]
56 pub fn read(&self) -> I::Value {
57 self.inner.read()
58 }
59
60 #[inline(always)]
61 pub fn readf(&self, flags: I::Value) -> bool {
62 self.inner.readf(flags)
63 }
64}
65
66pub struct WriteOnly<I> {
67 inner: I,
68}
69
70impl<I> WriteOnly<I> {
71 pub const fn new(inner: I) -> WriteOnly<I> {
72 WriteOnly { inner }
73 }
74}
75
76impl<I: Io> WriteOnly<I> {
77 #[inline(always)]
78 pub fn write(&mut self, value: I::Value) {
79 self.inner.write(value)
80 }
81
82 #[inline(always)]
83 pub fn writef(&mut self, flags: I::Value, value: bool) {
84 self.inner.writef(flags, value)
85 }
86}