dosgame1/util/Asm.h

68 lines
1.7 KiB
C++

#ifndef ASM_H
#define ASM_H
#include <cstdint>
static inline uint8_t inb(uint16_t port) {
uint8_t value;
asm volatile ("inb %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
static inline uint16_t inw(uint16_t port) {
uint16_t value;
asm volatile ("inw %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
static inline uint32_t inl(uint16_t port) {
uint32_t value;
asm volatile ("inl %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
static inline void outb(uint16_t port, uint8_t value) {
asm volatile ("outb %0, %1" : : "a"(value), "Nd"(port));
}
static inline void outw(uint16_t port, uint16_t value) {
asm volatile ("outw %0, %1" : : "a"(value), "Nd"(port));
}
static inline void outl(uint16_t port, uint32_t value) {
asm volatile ("outl %0, %1" : : "a"(value), "Nd"(port));
}
static inline void enableInterrupts() {
asm volatile ("sti": : :"memory");
}
static inline void disableInterrupts() {
asm volatile ("cli": : :"memory");
}
static inline uint32_t eflags() {
uint32_t value;
asm volatile ( "pushf\n\t"
"popl %0"
: "=g"(value));
return value;
}
static inline bool interruptsEnabled() {
return (eflags() & (1u << 9u)) != 0;
}
static void pmInterruptCall(unsigned short selector, unsigned long offset) {
__asm__ __volatile__ (
"pushf\n" // Push EFLAGS
"mov %0, %%ax\n" // Load the segment selector into AX
"mov %%ax, %%ds\n" // Move it to DS (or another segment register)
"call *%1\n" // Perform the indirect far call using the offset
: // No output operands
: "r" (selector), "r" (offset) // Input operands
: "%ax" // Clobbered register
);
}
#endif