OurOS 0.1
Operating System built by McGill Students
 
Loading...
Searching...
No Matches
port_io.h
Go to the documentation of this file.
1/**
2 * @file port_io.h
3 * @brief c wrappers for asm port operations
4 */
5#ifndef _HAL_PORT_IO_H
6#define _HAL_PORT_IO_H
7
8#include "stdint.h"
9
10/**
11 * @brief read byte from low level port
12 * @param port port to read from
13 * @return value in port
14 */
15static inline uint8_t inb(uint16_t port) {
16 uint8_t result;
17 __asm__ volatile(
18 "inb %1, %0"
19 : "=a" (result)
20 : "Nd" (port));
21 return result;
22}
23
24/**
25 * @brief write byte to low level port
26 * @param port port to write to
27 * @param val value to write to port
28 */
29static inline void outb(uint16_t port, uint8_t val) {
30 __asm__ volatile(
31 "outb %0, %1"
32 :
33 : "a" (val), "Nd" (port));
34}
35
36/**
37 * @brief read word from low level port
38 * @param port port to read from
39 * @return value in port
40 */
41static inline uint16_t inw(uint16_t port) {
42 uint16_t result;
43 __asm__ volatile(
44 "inw %1, %0"
45 : "=a" (result)
46 : "Nd" (port));
47 return result;
48}
49
50/**
51 * @brief write word to low level port
52 * @param port port to write to
53 * @param val value to write to port
54 */
55static inline void outw(uint16_t port, uint16_t val) {
56 __asm__ volatile(
57 "outw %0, %1"
58 :
59 : "a" (val), "Nd" (port));
60}
61
62/**
63 * @brief read double word from low level port
64 * @param port port to read from
65 * @return value in port
66 */
67static inline uint32_t inl(uint16_t port) {
68 uint32_t result;
69 __asm__ volatile(
70 "inl %1, %0"
71 : "=a" (result)
72 : "Nd" (port));
73 return result;
74}
75
76/**
77 * @brief write double word to low level port
78 * @param port port to write to
79 * @param val value to write to port
80 */
81static inline void outl(uint16_t port, uint32_t val) {
82 __asm__ volatile(
83 "outl %0, %1"
84 :
85 : "a" (val), "Nd" (port));
86}
87
88/**
89 * @brief perform a short wait (1-4 microseconds)
90 *
91 * This function outputs to a random unused port (0x80 is almost always unused after booting)
92 * Since output takes some time, this function can work as a small wait
93 */
94static inline void io_wait(void) {
95 outb(0x80, 0);
96}
97
98#endif
static uint8_t inb(uint16_t port)
read byte from low level port
Definition port_io.h:15
static void io_wait(void)
perform a short wait (1-4 microseconds)
Definition port_io.h:94
static void outl(uint16_t port, uint32_t val)
write double word to low level port
Definition port_io.h:81
static uint32_t inl(uint16_t port)
read double word from low level port
Definition port_io.h:67
static void outb(uint16_t port, uint8_t val)
write byte to low level port
Definition port_io.h:29
static void outw(uint16_t port, uint16_t val)
write word to low level port
Definition port_io.h:55
static uint16_t inw(uint16_t port)
read word from low level port
Definition port_io.h:41
unsigned short uint16_t
Definition stdint.h:5
unsigned int uint32_t
Definition stdint.h:6
unsigned char uint8_t
Definition stdint.h:4