Iros
 
Loading...
Searching...
No Matches
io_instructions.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/types/prelude.h"
4
5namespace iris::x86::amd64 {
6static inline void io_out(u16 port, di::concepts::OneOf<u8, u16, u32, byte> auto const value) {
7 if constexpr (sizeof(value) == 1) {
8 asm volatile("outb %0, %1" : : "a"(value), "Nd"(port));
9 } else if constexpr (sizeof(value) == 2) {
10 asm volatile("outw %0, %1" : : "a"(value), "Nd"(port));
11 } else {
12 asm volatile("outl %0, %1" : : "a"(value), "Nd"(port));
13 }
14}
15
16template<di::concepts::OneOf<u8, u16, u32, byte> Out>
17static inline auto io_in(u16 port) -> Out {
18 Out value;
19 if constexpr (sizeof(Out) == 1) {
20 asm volatile("inb %1, %0" : "=a"(value) : "Nd"(port));
21 } else if constexpr (sizeof(Out) == 2) {
22 asm volatile("inw %1, %0" : "=a"(value) : "Nd"(port));
23 } else {
24 asm volatile("inl %1, %0" : "=a"(value) : "Nd"(port));
25 }
26 return value;
27}
28
32static inline void io_wait() {
33 // NOTE: OSDEV says that port 0x80 will not be used after boot, citing Linux.
34 // https://wiki.osdev.org/Inline_Assembly/Examples#IO_WAIT
35 io_out(0x80, (u8) 0);
36}
37
41static inline void io_wait_us(u32 us) {
42 for (u32 i = 0; i < us; i++) {
43 io_wait();
44 }
45}
46}
constexpr auto value
Definition value.h:34
__UINT8_TYPE__ u8
Definition integers.h:9
__UINT32_TYPE__ u32
Definition integers.h:11
__UINT16_TYPE__ u16
Definition integers.h:10
Definition gdt.cpp:38