cfenollosa_os-tutorial/23-fixes/cpu/ports.c

38 lines
1.2 KiB
C
Raw Normal View History

2015-08-17 16:41:38 +00:00
#include "ports.h"
/**
* Read a byte from the specified port
*/
2015-08-18 08:31:28 +00:00
uint8_t port_byte_in (uint16_t port) {
uint8_t result;
2015-08-17 16:41:38 +00:00
/* 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
*/
2015-08-18 09:44:24 +00:00
asm("in %%dx, %%al" : "=a" (result) : "d" (port));
2015-08-17 16:41:38 +00:00
return result;
}
2015-08-18 08:31:28 +00:00
void port_byte_out (uint16_t port, uint8_t data) {
2015-08-17 16:41:38 +00:00
/* 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
*/
2015-08-18 09:44:24 +00:00
asm volatile("out %%al, %%dx" : : "a" (data), "d" (port));
2015-08-17 16:41:38 +00:00
}
2015-08-18 08:31:28 +00:00
uint16_t port_word_in (uint16_t port) {
uint16_t result;
2015-08-18 09:44:24 +00:00
asm("in %%dx, %%ax" : "=a" (result) : "d" (port));
2015-08-17 16:41:38 +00:00
return result;
}
2015-08-18 08:31:28 +00:00
void port_word_out (uint16_t port, uint16_t data) {
2015-08-18 09:44:24 +00:00
asm volatile("out %%ax, %%dx" : : "a" (data), "d" (port));
2015-08-17 16:41:38 +00:00
}