organize source files into separate directories

This commit is contained in:
2025-04-04 14:28:33 +03:00
parent f660beb3a7
commit b7b33fefca
16 changed files with 16 additions and 16 deletions
+199
View File
@@ -0,0 +1,199 @@
#include "../include/disk.h"
#include "../include/utils.h"
#include "../include/stdio.h"
// Define the IDE registers
#define IDE_CMD 0x1F7
#define IDE_DATA 0x1F0
#define IDE_SEC_COUNT 0x1F2
#define IDE_SEC_NUM 0x1F3
#define IDE_CYL_LOW 0x1F4
#define IDE_CYL_HIGH 0x1F5
#define IDE_HEAD 0x1F6
#define IDE_STATUS 0x1F7
#define IDE_COMMAND 0x1F7
#define STATUS_ERR 0x01
#define STATUS_BUSY 0x80
#define STATUS_DRQ 0x08
#define STATUS_RDY 0x40
// Define the IDE commands
#define IDE_CMD_IDENTIFY 0xEC
void ide_send_command(unsigned char command) {
__asm__ __volatile__("outb %0, %1" : : "a" (command), "d" (IDE_COMMAND));
}
unsigned short ide_read_data() {
unsigned short data;
__asm__ __volatile__("inw %w1, %0" : "=a" (data) : "Nd" (IDE_DATA));
return data;
}
#define inw ide_read_data
void outw(uint16_t port, uint16_t value) //implemented in asm
{
__asm__ volatile(
"out %0, %1"
:
: "a" (value), "Nd" (port)
);
}
unsigned char ide_read_status() {
unsigned char status;
__asm__ __volatile__("inb %1, %0" : "=a" (status) : "d" (IDE_STATUS));
return status;
}
//https://wiki.osdev.org/ATA_PIO_Mode#IDENTIFY_command
void get_disk_info(disk_info *info) {
outportb(0x1F6, 0xA0);//select master drive, 0xA0 - master, 0xB0 - slave
//set sectorcount, LBAlo, LBAmid, LBAhi IO ports to 0
outportb(0x1F2, 0x0);
outportb(0x1F3, 0x0);
outportb(0x1F4, 0x0);
outportb(0x1F5, 0x0);
// Send the identify command
ide_send_command(IDE_CMD_IDENTIFY);//0xEC
uint8_t status = inportb(0x1F7);
printf("status is %X\n", status);
if(!status)
{
printf("!status\n");
return;
}
while((inportb(0x1F7) & STATUS_BUSY) != 0);
while(1){
status = inportb(0x1F7);
if(status & STATUS_ERR)
{
printf("ERROR! drive has err set. returning...\n");
return;
}
if(status & STATUS_DRQ)
break;
}
printf("disk is online\n");
uint8_t lbaMID, lbaHI;
lbaMID = inportb(0x1F4);
lbaHI = inportb(0x1F5);
if(lbaMID != 0 || lbaHI != 0)
{
printf("ERROR! lbaMID: %X, lbaHI: %X\n", lbaMID, lbaHI);
printf("The drive is not ATA\n");
return;
}
// Read the data
unsigned short data[256];
for (int i = 0; i < 256; i++) {
data[i] = ide_read_data();
}
// Extract the disk information
info->cylinders = (data[1] >> 8) | (data[2] << 8); // Cylinder count
info->heads = data[6]; // Heads per cylinder
info->sectors = data[3]; // Sectors per track
}
// Function to check if a disk is present
int ide_check_disk_present() {
// Send the identify command
ide_send_command(IDE_CMD_IDENTIFY);
// Wait for 1 second
for (int i = 0; i < 1000000; i++);
// Check the status register
unsigned char status;
__asm__ __volatile__("inb %1, %0" : "=a" (status) : "d" (IDE_STATUS));
// If the device is not busy and there's no error, it's likely a disk is present
if ((status & 0x80) == 0 && (status & 0x01) == 0) {
return 1; // Disk present
}
// If the device is busy, it might be a disk, so let's try again
if (status & 0x80) {
// Wait for the device to become ready
while ((ide_read_status() & 0x80)!= 0);
// Check the status register again
__asm__ __volatile__("inb %1, %0" : "=a" (status) : "d" (IDE_STATUS));
// If the device is not busy and there's no error, it's likely a disk is present
if ((status & 0x80) == 0 && (status & 0x01) == 0) {
return 1; // Disk present
}
}
return 0; // No disk present
}
void ata_400ns_delay()
{
for(int i = 0; i < 4; i++)
inb(0x1FC);
}
//polling
//just waiting for the drive to be ready to transfer data
void ata_poll()
{
while(inportb(0x1F7)&STATUS_BUSY);
while(!(inportb(0x1F7)&STATUS_RDY));
}
//https://wiki.osdev.org/ATA_PIO_Mode
//x86 directions section
bool ata_read(uint8_t *buffer, uint32_t lba)
{
outb(0x1F6, 0xE0 | ((lba >> 24) & 0x0F));//E0 is for master, F0 is for slave
//outb(0x1F1, 0x00);//TODO: this is probably ignored so could be removed maybe?
outb(0x1F2, 1);//1 is sector count, how many sectors are we reading
outb(0x1F3, (unsigned char) lba);
outb(0x1F4, (unsigned char)(lba >> 8));
outb(0x1F5, (unsigned char)(lba >> 16));
outb(0x1F7, 0x20); //Send the "READ SECTORS" command
ata_poll();
for(int i = 0; i < 256; i++)
{
uint16_t data = inw(0x1F0);
*(uint16_t*)(buffer+i*2) = data;
}
ata_400ns_delay();
return true;
}
bool ata_write(const uint8_t *buffer, uint32_t lba)
{
outb(0x1F6, 0xE0 | ((lba >> 24) & 0x0F));
outb(0x1F2, 1);//sector count
outb(0x1F3, (unsigned char) lba);
outb(0x1F4, (unsigned char) (lba >> 8));
outb(0x1F5, (unsigned char) (lba >> 16));
outb(0x1F7, 0x30); //Send the "WRITE SECTORS" command
ata_poll();
for(int i = 0; i < 256; i++)
{
uint16_t data = *(uint16_t*)(buffer + i * 2);
outw(0x1F0, data);
//ata_400ns_delay();
ata_poll();
}
return true;
}
+1863
View File
File diff suppressed because it is too large Load Diff
+134
View File
@@ -0,0 +1,134 @@
#include "../include/keyboard.h"
#include <stdbool.h>
unsigned char kbdus[128] =
{
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 19 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 29 - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`', 0, /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
unsigned char kbdus2[128] =
{
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', /* 9 */
'(', ')', '_', '+', '\b', /* Backspace */
'\t', /* Tab */
'Q', 'W', 'E', 'R', /* 19 */
'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', /* Enter key */
0, /* 29 - Control */
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* 39 */
'\'', '`', 0, /* Left shift */
'\\', 'Z', 'X', 'C', 'V', 'B', 'N', /* 49 */
'M', '<', '>', '?', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
bool shift_pressed;
bool caps_pressed;
void keyboard_wait();
void handle_keyboard()
{//3a
uint8_t scancode = inb(0x60);
if(scancode & 0x80)//key released
{
scancode = scancode-0x80;
if(scancode == 0x2a || scancode == 0x36)
shift_pressed = false;
//printf("(%X)%c released ", scancode, kbdus[scancode-0x80]);
}
else//key pressed
{
if(scancode == 0x2a || scancode == 0x36)
{
shift_pressed = true;
return;
}
if(scancode == 0x3a)
{
caps_pressed = !caps_pressed;
outb(0x60, 0xed);//set LED command
keyboard_wait();
outb(0x60, caps_pressed << 2);//0 is scroll lock, 1 is numlock, 2 is caps lock
return;
}
if(shift_pressed || caps_pressed)
printf("%c", kbdus2[scancode]);
else
printf("%c", kbdus[scancode]); //printf("(0x%X)%c", scancode, kbdus[scancode]);
}
}
void keyboard_wait()
{
int status;
while(1)
{
status = inb(0x60);
if(status == 0xfa || status == 0xfe)
return;
}
}
+93
View File
@@ -0,0 +1,93 @@
// screen.c
#include "../include/screen.h"
#include "../include/string.h"
static size_t terminal_row;
static size_t terminal_column;
static uint8_t terminal_color;
static uint16_t* terminal_buffer;
inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4;
}
inline uint16_t vga_entry(unsigned char uc, uint8_t color)
{
return (uint16_t) uc | (uint16_t) color << 8;
}
void terminal_initialize(void)
{
terminal_row = 0;
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
terminal_buffer = (uint16_t*) 0xB8000;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
}
}
void terminal_setcolor(uint8_t color)
{
terminal_color = color;
}
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_scroll(void)
{
for (size_t y = 0; y < VGA_HEIGHT - 1; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index_from = (y + 1) * VGA_WIDTH + x;
const size_t index_to = y * VGA_WIDTH + x;
terminal_buffer[index_to] = terminal_buffer[index_from];
}
}
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
terminal_row = VGA_HEIGHT - 1;
terminal_column = 0;
}
void terminal_putchar(char c)
{
if(c != '\n')
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column >= VGA_WIDTH){
terminal_column = 0;
if (++terminal_row >= VGA_HEIGHT)
terminal_scroll();
}
if(terminal_row >= VGA_HEIGHT){
terminal_scroll();
}
if(c == '\n')
{
terminal_column = 0;
terminal_row++;
}
}
void terminal_write(const char* data, size_t size)
{
for (size_t i = 0; i < size; i++)
terminal_putchar(data[i]);
}
void terminal_writestring(const char* data)
{
terminal_write(data, strlen(data));
}