Skip to content

Commit

Permalink
- commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsoftware1 committed Sep 26, 2024
1 parent af97c38 commit 7a3279e
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 83 deletions.
136 changes: 83 additions & 53 deletions src/arch/i386/kernel/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,74 +182,104 @@ void console_putstr(const char *str)
}
}

void printf(const char *format, ...)
{
char **arg = (char **)&format;
int c;
char buf[32];
static void print_num(unsigned int num, int base, int width, char pad_char) {
char buf[MAX_BUFFER];
char *str = &buf[sizeof(buf) - 1];
*str = '\0';

arg++;
do {
*--str = "0123456789abcdef"[num % base];
num /= base;
} while (num > 0);

memset(buf, 0, sizeof(buf));
while ((c = *format++) != 0)
{
if (c != '%')
console_putchar(c);
else
{
char *p, *p2;
int pad0 = 0, pad = 0;

c = *format++;
if (c == '0')
{
pad0 = 1;
c = *format++;
}
int len = &buf[sizeof(buf) - 1] - str;
while (len < width) {
console_putchar(pad_char);
width--;
}

if (c >= '0' && c <= '9')
{
pad = c - '0';
c = *format++;
}
while (*str) {
console_putchar(*str++);
}
}

void printf(const char *format, ...) {
va_list args;
va_start(args, format);

while (*format) {
if (*format != '%') {
console_putchar(*format++);
continue;
}

format++;

int width = 0;
char pad_char = ' ';

if (*format == '0') {
pad_char = '0';
format++;
}

while (*format >= '0' && *format <= '9') {
width = width * 10 + (*format - '0');
format++;
}

switch (c)
{
switch (*format) {
case 'd':
case 'i': {
int num = va_arg(args, int);
if (num < 0) {
console_putchar('-');
num = -num;
}
print_num((unsigned int)num, 10, width, pad_char);
break;
}
case 'u':
print_num(va_arg(args, unsigned int), 10, width, pad_char);
break;
case 'x':
itoa(buf, c, *((int *)arg++));
p = buf;
goto string;
case 'X':
print_num(va_arg(args, unsigned int), 16, width, pad_char);
break;

case 's':
p = *arg++;
if (!p)
p = "(null)";

string:
for (p2 = p; *p2; p2++)
;
for (; p2 < p + pad; p2++)
console_putchar(pad0 ? '0' : ' ');
while (*p)
console_putchar(*p++);
case 'p':
console_putchar('0');
console_putchar('x');
print_num((unsigned int)va_arg(args, void*), 16, width, pad_char);
break;

case 'c':
{
wchar_t wc = (wchar_t)*arg++;
console_putchar(wc);
case 's': {
const char *str = va_arg(args, const char*);
if (!str) str = "(null)";
int len = 0;
while (str[len]) len++;
while (width > len) {
console_putchar(pad_char);
width--;
}
while (*str) {
console_putchar(*str++);
}
break;
}

case 'c':
console_putchar((char)va_arg(args, int));
break;
case '%':
console_putchar('%');
break;
default:
console_putchar(*((int *)arg++));
console_putchar('%');
console_putchar(*format);
break;
}
}
format++;
}

va_end(args);
}

void getstr(char *buffer)
Expand Down
108 changes: 104 additions & 4 deletions src/arch/i386/kernel/drivers/pic.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "../include/libc/include/types.h"
#include "../include/io.h"
#include "../include/drivers/pic.h"

void init_pic() {
void init_pic()
{
printf("Initiating PIC...\n");
u8 a1, a2;

Expand All @@ -23,10 +23,110 @@ void init_pic() {

outportb(PIC_MASTER_DATA, a1);
outportb(PIC_SLAVE_DATA, a2);

//! MASKING ALL INTERRUPTS
//outportb(PIC_MASTER_DATA, 0xFF);
//outportb(PIC_SLAVE_DATA, 0xFF);
}

void pic_disable()
{
outportb(PIC_MASTER_DATA, 0xFF);
outportb(PIC_SLAVE_DATA, 0xFF);
}

void pic_eoi(u8 irq) {
if(irq >= 0x28)
void pic_eoi(u8 irq)
{
if (irq >= 0x28)
outportb(PIC_SLAVE_COMMAND, PIC_EOI);
outportb(PIC_MASTER_COMMAND, PIC_EOI);
}

void mask(u8 irq)
{
u16 port;
u8 value;

if (irq < 8)
{
port = PIC_MASTER_DATA;
}
else
{
port = PIC_SLAVE_DATA;
irq -= 8;
}
value = inportb(port) &~ (1 << irq);
outportb(port, value);
}

void unmask(u8 irq)
{
u16 port;
u8 value;

if (irq < 8)
{
port = PIC_MASTER_DATA;
}
else
{
port = PIC_SLAVE_DATA;
irq -= 8;
}
value = inportb(port) | (1 << irq);
outportb(port, value);
}

u8 pic_read_irr(u8 pic)
{
if (pic == PIC_MASTER)
{
outportb(PIC_MASTER_COMMAND, PIC_READ_IRR);
return inportb(PIC_MASTER_COMMAND);
}
else if (pic == PIC_SLAVE)
{
outportb(PIC_SLAVE_COMMAND, PIC_READ_IRR);
return inportb(PIC_SLAVE_COMMAND);
}
return 0;
}

u8 pic_read_isr(u8 pic)
{
if (pic == PIC_MASTER)
{
outportb(PIC_MASTER_COMMAND, PIC_READ_ISR);
return inportb(PIC_MASTER_COMMAND);
}
else if (pic == PIC_SLAVE)
{
outportb(PIC_SLAVE_COMMAND, PIC_READ_ISR);
return inportb(PIC_SLAVE_COMMAND);
}
return 0;
}

void probe_pic()
{
printf("Probing PIC status:\n");

u8 master_imr = inportb(PIC_MASTER_DATA);
u8 slave_imr = inportb(PIC_SLAVE_DATA);

printf("Master IMR: 0x%02X\n", master_imr);
printf("Slave IMR: 0x%02X\n", slave_imr);

u8 master_irr = pic_read_irr(PIC_MASTER);
u8 slave_irr = pic_read_irr(PIC_SLAVE);

printf("Master IRR: 0x%02X\n", master_irr);
printf("Slave IRR: 0x%02X\n", slave_irr);

u8 master_isr = pic_read_isr(PIC_MASTER);
u8 slave_isr = pic_read_isr(PIC_SLAVE);

printf("Master ISR: 0x%02X\n", master_isr);
printf("Slave ISR: 0x%02X\n", slave_isr);
}
12 changes: 7 additions & 5 deletions src/arch/i386/kernel/include/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
#include "libc/include/types.h"
#include "./drivers/vga.h"

#define MAXIMUM_PAGES 16
#define MAXIMUM_PAGES 16
#define MAX_BUFFER 128

#define SCROLL_UP 1
#define SCROLL_DOWN 2
#define SCROLL_UP 1
#define SCROLL_DOWN 2

void console_clear(VGA_COLOR_TYPE fore_color, VGA_COLOR_TYPE back_color);
void console_init(VGA_COLOR_TYPE fore_color, VGA_COLOR_TYPE back_color);

void console_scroll(int line_count);
void console_putchar(char ch);
void console_ungetchar();
void console_ungetchar_bound(u8 n);
void console_gotoxy(u16 x, u16 y);

void console_putstr(const char *str);

void getstr(char *buffer);
void getstr_bound(char *buffer, u8 bound);

static void print_num(unsigned int num, int base, int width, char pad_char);
void printf(const char *format, ...);

#endif
28 changes: 18 additions & 10 deletions src/arch/i386/kernel/include/drivers/pic.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@

#include "../libc/include/types.h"

#define PIC_MASTER_COMMAND 0X20
#define PIC_MASTER_DATA 0X21
#define PIC_SLAVE_COMMAND 0XA0
#define PIC_SLAVE_DATA 0xA1

#define PIC_EOI 0x20

#define PIC_ICW1 0x11 /* interrupt control command word PIC for initialization */
#define PIC_ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */
#define PIC_MASTER_COMMAND 0X20
#define PIC_MASTER_DATA 0X21
#define PIC_SLAVE_COMMAND 0XA0
#define PIC_SLAVE_DATA 0xA1
#define PIC_READ_IRR 0x0A
#define PIC_READ_ISR 0x0B
#define PIC_MASTER 0
#define PIC_SLAVE 1
#define PIC_EOI 0x20
#define PIC_ICW1 0x11 /* interrupt control command word PIC for initialization */
#define PIC_ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */

void init_pic();

void pic_disable();
void pic_eoi(u8 irq);
void mask(u8 irq);
void unmask(u8 irq);

u8 pic_read_irr(u8 pic);
u8 pic_read_isr(u8 pic);
void probe_pic();

#endif
24 changes: 13 additions & 11 deletions src/arch/i386/kernel/include/libc/include/types.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
#ifndef TYPES_H
#define TYPES_H

#define NULL ((void*)0)

#ifdef __x86_64__
typedef unsigned long long size_t;
typedef signed long long ssize_t;
#else
typedef unsigned int size_t;
typedef signed int ssize_t;
#endif
#define NULL ((void *)0)

typedef unsigned char u8;
typedef unsigned short u16;
Expand All @@ -19,11 +11,17 @@ typedef signed char s8;
typedef signed short s16;
typedef signed int s32;
typedef char wchar_t;
typedef unsigned int size_t;
typedef unsigned int uintptr_t;
typedef signed int ssize_t;
typedef char *va_list;

typedef u8 byte;
typedef u16 word;
typedef u32 dword;

typedef enum {
typedef enum
{
false,
true
} bool;
Expand All @@ -37,6 +35,10 @@ typedef struct file
int fd;
int bufsize;
char *tmpfname;
}file_t;
} file_t;

#define va_start(ap, last) (ap = (va_list) & last + sizeof(last))
#define va_arg(ap, type) (*(type *)((ap += sizeof(type)) - sizeof(type)))
#define va_end(ap) (ap = (va_list)0)

#endif
Loading

0 comments on commit 7a3279e

Please sign in to comment.