adds more functionality

This commit is contained in:
2023-10-05 01:27:03 -07:00
parent 2b7ed92a07
commit ef2537720d
30 changed files with 425 additions and 30 deletions

85
src/cpu/cpu.c Normal file
View File

@@ -0,0 +1,85 @@
#include "cpu.h"
// Function to read a 32-bit value from a CPU register
uint32_t read_register(uint8_t reg)
{
uint32_t value;
__asm__ volatile("mov %0, %%" + reg : "=r"(value));
return value;
}
// Function to write a 32-bit value to a CPU register
void write_register(uint8_t reg, uint32_t value)
{
__asm__ volatile("mov %0, %%" + reg : : "r"(value));
}
// Function to read the value of the CR0 register
uint32_t read_cr0()
{
uint32_t value;
__asm__ volatile("mov %%cr0, %0" : "=r"(value));
return value;
}
// Function to write a value to the CR0 register
void write_cr0(uint32_t value)
{
__asm__ volatile("mov %0, %%cr0" : : "r"(value));
}
// Function to switch from real mode to protected mode
void switch_to_protected_mode()
{
uint32_t cr0 = read_cr0();
cr0 |= 0x1; // Set the PE bit to switch to protected mode
write_cr0(cr0);
}
// Write a byte to a port
void outb(uint16_t port, uint8_t value)
{
__asm__ volatile ("outb %0, %1" : : "a"(value), "Nd"(port));
}
// Read a byte from a port
uint8_t inb(uint16_t port)
{
uint8_t value;
__asm__ volatile ("inb %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
// Write a word to a port
void outw(uint16_t port, uint16_t value)
{
__asm__ volatile ("outw %0, %1" : : "a"(value), "Nd"(port));
}
// Read a word from a port
uint16_t inw(uint16_t port)
{
uint16_t value;
__asm__ volatile ("inw %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
// Write a double word to a port
void outl(uint16_t port, uint32_t value)
{
__asm__ volatile ("outl %0, %1" : : "a"(value), "Nd"(port));
}
// Read a double word from a port
uint32_t inl(uint16_t port)
{
uint32_t value;
__asm__ volatile ("inl %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
// Execute the CPUID instruction
void cpuid(uint32_t code, uint32_t *a, uint32_t *d)
{
__asm__ volatile ("cpuid" : "=a"(*a), "=d"(*d) : "a"(code) : "ecx", "ebx");
}

32
src/cpu/cpu.h Normal file
View File

@@ -0,0 +1,32 @@
#ifndef CPU_H
#define CPU_H
#include <stdint.h>
// Function to read a 32-bit value from a CPU register
uint32_t read_register(uint8_t reg);
// Function to write a 32-bit value to a CPU register
void write_register(uint8_t reg, uint32_t value);
// Function to read the value of the CR0 register
uint32_t read_cr0();
// Function to write a value to the CR0 register
void write_cr0(uint32_t value);
// Function to switch from real mode to protected mode
void switch_to_protected_mode();
void outb(uint16_t port, uint8_t value);
uint8_t inb(uint16_t port);
void outw(uint16_t port, uint16_t value);
uint16_t inw(uint16_t port);
void outl(uint16_t port, uint32_t value);
uint32_t inl(uint16_t port);
void cpuid(uint32_t code, uint32_t *a, uint32_t *d);
#endif

32
src/drivers/display/vga.c Normal file
View File

@@ -0,0 +1,32 @@
#include "vga.h"
#include <stddef.h>
#include <stdint.h>
#define VGA_MEMORY_ADDRESS 0xA0000
#define VGA_MEMORY_SIZE 0x60000
static uint8_t* vga_memory = (uint8_t*) VGA_MEMORY_ADDRESS;
static uint16_t vga_width = VGA_WIDTH;
static uint16_t vga_height = VGA_HEIGHT;
static uint8_t vga_depth = VGA_DEPTH;
void vga_init() {
// Initialize VGA driver here
}
void vga_set_resolution(uint16_t width, uint16_t height, uint8_t depth) {
// Set VGA resolution here
}
void vga_draw_pixel(uint16_t x, uint16_t y, uint8_t color) {
// Draw a pixel on the screen at (x, y) with the given color
uint32_t offset = y * vga_width + x;
vga_memory[offset] = color;
}
void vga_clear_screen(uint8_t color) {
// Clear the screen with the given color
for (uint32_t i = 0; i < vga_width * vga_height; i++) {
vga_memory[i] = color;
}
}

15
src/drivers/display/vga.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef VGA_H
#define VGA_H
#include <stdint.h>
#define VGA_WIDTH 640
#define VGA_HEIGHT 480
#define VGA_DEPTH 8
void vga_init();
void vga_set_resolution(uint16_t width, uint16_t height, uint8_t depth);
void vga_draw_pixel(uint16_t x, uint16_t y, uint8_t color);
void vga_clear_screen(uint8_t color);
#endif

46
src/drivers/io/serial.c Normal file
View File

@@ -0,0 +1,46 @@
#include "serial.h"
#include <stddef.h>
#include <stdint.h>
#define PORT_BASE 0x3F8
void init_serial(uint16_t com) {
uint16_t port = PORT_BASE + 8 * (com - 1);
outb(port + 1, 0x00); // Disable all interrupts
outb(port + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(port + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(port + 1, 0x00); // (hi byte)
outb(port + 3, 0x03); // 8 bits, no parity, one stop bit
outb(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
outb(port + 4, 0x0B); // IRQs enabled, RTS/DSR set
}
int serial_received(uint16_t com) {
uint16_t port = PORT_BASE + 8 * (com - 1);
return inb(port + 5) & 1;
}
char read_serial(uint16_t com) {
uint16_t port = PORT_BASE + 8 * (com - 1);
while (serial_received(com) == 0);
return inb(port);
}
int is_transmit_empty(uint16_t com) {
uint16_t port = PORT_BASE + 8 * (com - 1);
return inb(port + 5) & 0x20;
}
void write_serial(uint16_t com, char a) {
uint16_t port = PORT_BASE + 8 * (com - 1);
while (is_transmit_empty(com) == 0);
outb(port, a);
}

12
src/drivers/io/serial.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef SERIAL_H
#define SERIAL_H
#include <stdint.h>
void init_serial(uint16_t com);
int serial_received(uint16_t com);
char read_serial(uint16_t com);
int is_transmit_empty(uint16_t com);
void write_serial(uint16_t com, char a);
#endif

86
src/drivers/mouse/mouse.c Normal file
View File

@@ -0,0 +1,86 @@
#include "mouse.h"
#include "ps2.h"
#include "serial.h"
#include "usb.h"
#include <stddef.h>
#include <stdint.h>
#define MOUSE_COM 1
void init_mouse(uint16_t com)
{
if (usb_mouse_detected())
{
init_usb();
}
else if (ps2_mouse_detected())
{
init_ps2();
}
else
{
init_serial(com);
}
}
int mouse_received(uint16_t com)
{
if (usb_mouse_detected())
{
return usb_mouse_received();
}
else if (ps2_mouse_detected())
{
return ps2_mouse_received();
}
else
{
return serial_received(com);
}
}
mouse_data_t read_mouse(uint16_t com)
{
mouse_data_t data;
if (usb_mouse_detected())
{
data = usb_read_mouse();
}
else if (ps2_mouse_detected())
{
data = ps2_read_mouse();
}
else
{
data.x = 0;
data.y = 0;
data.buttons = 0;
while (mouse_received(com) == 0)
;
uint8_t status = inb(PORT_BASE + 8 * (com - 1) + 5);
if (status & 0x01)
{
data.buttons |= 0x01;
}
if (status & 0x02)
{
data.buttons |= 0x02;
}
if (status & 0x04)
{
data.buttons |= 0x04;
}
data.x = inb(PORT_BASE + 8 * (com - 1));
data.y = inb(PORT_BASE + 8 * (com - 1) + 1);
}
return data;
}

16
src/drivers/mouse/mouse.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef MOUSE_H
#define MOUSE_H
#include <stdint.h>
typedef struct {
int16_t x;
int16_t y;
uint8_t buttons;
} mouse_data_t;
void init_mouse(uint16_t com);
int mouse_received(uint16_t com);
mouse_data_t read_mouse(uint16_t com);
#endif

View File

View File

3
src/elf.c Normal file
View File

@@ -0,0 +1,3 @@
/*
elf functions go here
*/

3
src/elf.h Normal file
View File

@@ -0,0 +1,3 @@
/*
elf functions go here
*/

View File

@@ -0,0 +1,3 @@
/*
fat16 goes here
*/

View File

@@ -0,0 +1,3 @@
/*
fat16 goes here
*/

View File

@@ -0,0 +1,3 @@
/*
fat32 goes here
*/

View File

@@ -0,0 +1,3 @@
/*
fat32 goes here
*/

3
src/gui/gui.c Normal file
View File

@@ -0,0 +1,3 @@
/*
gui goes here
*/

4
src/gui/gui.h Normal file
View File

@@ -0,0 +1,4 @@
/*
gui goes here
*/

0
src/kernel/acpi.c Normal file
View File

0
src/kernel/acpi.h Normal file
View File

View File

View File

3
src/kernel/timer.c Normal file
View File

@@ -0,0 +1,3 @@
/*
timer functions go here
*/

3
src/kernel/timer.h Normal file
View File

@@ -0,0 +1,3 @@
/*
timer functions go here
*/