TSOS/src/kernel/drivers/ports/ports.c

53 lines
1.6 KiB
C

/*
Copyright 2022 Mattia Giambirtone & Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Utilities for writing to and reading from I/O ports
#include "kernel/drivers/ports/ports.h"
// Note: We use the volatile modifier everywhere because
// the compiler might try and optimize some of our code
// away, since its effects are not directly visible from
// the code itself, which would be a nightmare to debug!
byte readByte(u16 port) {
// Reads a byte from the specified I/O port
volatile byte result;
asm volatile ("in %%dx, %%al" : "=a" (result) : "d" (port));
return result;
}
void writeByte(u16 port, byte data) {
// Writes a byte to the specified I/O port
asm volatile ("out %%al, %%dx" : : "a" (data), "d" (port));
}
u16 readWord(u16 port) {
// Reads a word (16 bits) from the specified I/O port
volatile u16 result;
asm volatile ("in %%dx, %%ax" : "=a" (result) : "d" (port));
return result;
}
void writeWord(u16 port, u16 data) {
// Writes a word (16 bits) to the specified I/O port
asm volatile ("out %%ax, %%dx" : : "a" (data), "d" (port));
}