-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathports.c
39 lines (35 loc) · 1.15 KB
/
ports.c
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
#include "ports.h"
/**
* Read a byte from the specified port
*/
uint8_t port_byte_in(uint16_t port) {
uint8_t result;
/* Inline assembler syntax
* !! Notice how the source and destination registers are switched from NASM
* !!
*
* '"=a" (result)'; set '=' the C variable '(result)' to the value of
* register e'a'x
* '"d" (port)': map the C variable '(port)' into e'd'x register
*
* Inputs and outputs are separated by colons
*/
asm("in %%dx, %%al" : "=a"(result) : "d"(port));
return result;
}
void port_byte_out(uint16_t port, uint8_t data) {
/* Notice how here both registers are mapped to C variables and
* nothing is returned, thus, no equals '=' in the asm syntax
* However we see a comma since there are two variables in the input area
* and none in the 'return' area
*/
asm volatile("out %%al, %%dx" : : "a"(data), "d"(port));
}
uint16_t port_word_in(uint16_t port) {
uint16_t result;
asm("in %%dx, %%ax" : "=a"(result) : "d"(port));
return result;
}
void port_word_out(uint16_t port, uint16_t data) {
asm volatile("out %%ax, %%dx" : : "a"(data), "d"(port));
}