ports.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "ports.h"
  2. /**
  3. * Read a byte from the specified port
  4. */
  5. uint8_t port_byte_in (uint16_t port) {
  6. uint8_t result;
  7. /* Inline assembler syntax
  8. * !! Notice how the source and destination registers are switched from NASM !!
  9. *
  10. * '"=a" (result)'; set '=' the C variable '(result)' to the value of register e'a'x
  11. * '"d" (port)': map the C variable '(port)' into e'd'x register
  12. *
  13. * Inputs and outputs are separated by colons
  14. */
  15. asm("in %%dx, %%al" : "=a" (result) : "d" (port));
  16. return result;
  17. }
  18. void port_byte_out (uint16_t port, uint8_t data) {
  19. /* Notice how here both registers are mapped to C variables and
  20. * nothing is returned, thus, no equals '=' in the asm syntax
  21. * However we see a comma since there are two variables in the input area
  22. * and none in the 'return' area
  23. */
  24. asm volatile("out %%al, %%dx" : : "a" (data), "d" (port));
  25. }
  26. uint16_t port_word_in (uint16_t port) {
  27. uint16_t result;
  28. asm("in %%dx, %%ax" : "=a" (result) : "d" (port));
  29. return result;
  30. }
  31. void port_word_out (uint16_t port, uint16_t data) {
  32. asm volatile("out %%ax, %%dx" : : "a" (data), "d" (port));
  33. }