io.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _IO_H
  20. #define _IO_H
  21. #include <kernel/types.h>
  22. typedef __uint16_t port_t;
  23. typedef port_t __io_t;
  24. #define inb(port) ({ \
  25. __uint8_t _r; \
  26. __asm__ volatile ("inb %%dx, %%al" : "=a" (_r) : "d" (port)); \
  27. _r; \
  28. })
  29. #define inw(port) ({ \
  30. __uint16_t _r; \
  31. __asm__ volatile ("inw %%dx, %%ax" : "=a" (_r) : "d" (port)); \
  32. _r; \
  33. })
  34. #define inl(port) ({ \
  35. __uint32_t _r; \
  36. __asm__ volatile ("inl %%dx, %%eax" : "=a" (_r) : "d" (port)); \
  37. _r; \
  38. })
  39. #define insw(port, buf, c) __asm__ volatile ("cld;rep;insw;"::"d" (port), "D" (buf), "c" (c))
  40. #define outsw(port, buf, c) __asm__ volatile ("cld;rep;outsw;" :: "d" (port), "D" (buf), "c" (c))
  41. #define outb(value, port) \
  42. __asm__ volatile ("outb %%al, %%dx" :: "a" (value), "d" (port));
  43. #define outw(value, port) \
  44. __asm__ volatile ("outw %%ax, %%dx" :: "a" (value), "d" (port));
  45. #define outl(value, port) \
  46. __asm__ volatile ("outl %%eax, %%dx" :: "a" (value), "d" (port));
  47. #endif