Compare commits
4 Commits
7dedc0019b
...
c115dcd4f7
Author | SHA1 | Date | |
---|---|---|---|
c115dcd4f7 | |||
0256466f4c | |||
b87738ca47 | |||
72bab4a548 |
6
Makefile
6
Makefile
@ -2,7 +2,7 @@
|
|||||||
# or maybe add it to a bash script instead? im not sure
|
# or maybe add it to a bash script instead? im not sure
|
||||||
|
|
||||||
CC = i686-elf-gcc
|
CC = i686-elf-gcc
|
||||||
CFLAGS = -m32 -ffreestanding -Wall -Wextra -Werror -Wpedantic --sysroot=$(PWD)/sysroot -isystem=/usr/include -Iinclude -MD
|
CFLAGS = -m32 -ffreestanding -Wall -Wextra -Werror -Wpedantic --sysroot=$(PWD)/sysroot -isystem=/usr/include -Iinclude -MD -D__TESTING__
|
||||||
|
|
||||||
AS = nasm
|
AS = nasm
|
||||||
ASFLAGS = -f elf
|
ASFLAGS = -f elf
|
||||||
@ -46,7 +46,9 @@ os.iso: kernel.elf
|
|||||||
run: os.iso
|
run: os.iso
|
||||||
bochs -f util/bochsrc.txt -q
|
bochs -f util/bochsrc.txt -q
|
||||||
|
|
||||||
libc.a: CFLAGS:=$(CFLAGS) -D__is_libc # Target rule to define __is_libc
|
#TODO: when i inevitably do add libc, this is going to fail because they have the same name as the libk objects
|
||||||
|
#TODO: so we'll need to do a batch rename on one of the two object files,
|
||||||
|
libc.a: CFLAGS:=$(CFLAGS) -D__is_libc # Target rule to define __is_libc
|
||||||
libc.a: $(libc_objs)
|
libc.a: $(libc_objs)
|
||||||
$(AR) rcs $@ $(libc_objs)
|
$(AR) rcs $@ $(libc_objs)
|
||||||
|
|
||||||
|
@ -21,8 +21,6 @@ section .text
|
|||||||
loader:
|
loader:
|
||||||
mov esp, kernel_stack + KERNEL_STACK_SIZE
|
mov esp, kernel_stack + KERNEL_STACK_SIZE
|
||||||
|
|
||||||
;mov eax, 0xCAFEBABE
|
|
||||||
|
|
||||||
call kmain
|
call kmain
|
||||||
|
|
||||||
cli
|
cli
|
||||||
|
@ -16,4 +16,3 @@ uint64_t create_descriptor(uint32_t base, uint32_t limit, uint16_t flag)
|
|||||||
|
|
||||||
return descriptor;
|
return descriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#ifdef __TESTING__
|
||||||
|
#include <kernel/_kernel.h>
|
||||||
|
#endif
|
||||||
#include <kernel/x86/idt.h>
|
#include <kernel/x86/idt.h>
|
||||||
|
|
||||||
__attribute__((aligned(0x10)))
|
__attribute__((aligned(0x10)))
|
||||||
@ -29,6 +32,9 @@ void idt_set_descriptor(uint8_t vector, void *isr, uint8_t flags)
|
|||||||
extern void* isr_stub_table[];
|
extern void* isr_stub_table[];
|
||||||
void idt_init(void)
|
void idt_init(void)
|
||||||
{
|
{
|
||||||
|
#ifdef __TESTING__
|
||||||
|
kinfo("Initializing the IDT");
|
||||||
|
#endif
|
||||||
idtr.base = (uintptr_t)&idt[0];
|
idtr.base = (uintptr_t)&idt[0];
|
||||||
idtr.limit = (uint16_t) sizeof(idt_entry_t) * IDT_MAX_DESCRIPTORS - 1;
|
idtr.limit = (uint16_t) sizeof(idt_entry_t) * IDT_MAX_DESCRIPTORS - 1;
|
||||||
|
|
||||||
@ -37,7 +43,11 @@ void idt_init(void)
|
|||||||
vectors[vector] = true;
|
vectors[vector] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The "m" indicates actual data, not a pointer
|
||||||
__asm__ volatile("lidt %0" : : "m"(idtr)); // load the new IDT
|
__asm__ volatile("lidt %0" : : "m"(idtr)); // load the new IDT
|
||||||
__asm__ volatile("sti"); // set the interrupt flag
|
__asm__ volatile("sti"); // set the interrupt flag
|
||||||
|
#ifdef __TESTING__
|
||||||
|
kinfo("Initialized the IDT");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,3 +18,9 @@ inb:
|
|||||||
in al, dx ; read a byte from the I/O port and store it in the al register
|
in al, dx ; read a byte from the I/O port and store it in the al register
|
||||||
ret ; return the read byte
|
ret ; return the read byte
|
||||||
|
|
||||||
|
|
||||||
|
global io_wait
|
||||||
|
io_wait:
|
||||||
|
mov al, 0x0
|
||||||
|
out 0x80, al
|
||||||
|
ret
|
||||||
|
@ -3,9 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <kernel/serial.h>
|
#include <kernel/serial.h>
|
||||||
|
#include <kernel/x86/io.h>
|
||||||
|
|
||||||
#include "io.h"
|
|
||||||
|
|
||||||
/* I/O ports */
|
/* I/O ports */
|
||||||
|
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <kernel/tty.h>
|
#include <kernel/tty.h>
|
||||||
|
#include <kernel/x86/io.h>
|
||||||
|
|
||||||
#include "vga.h"
|
#include "vga.h"
|
||||||
#include "io.h"
|
|
||||||
|
|
||||||
/* I/O ports */
|
/* I/O ports */
|
||||||
#define VGA_COMMAND_PORT 0x3D4
|
#define VGA_COMMAND_PORT 0x3D4
|
||||||
|
114
kernel/arch/pic/pic.c
Normal file
114
kernel/arch/pic/pic.c
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
#ifdef __TESTING__
|
||||||
|
#include <kernel/_kernel.h>
|
||||||
|
#endif
|
||||||
|
#include <kernel/x86/io.h>
|
||||||
|
#include <kernel/x86/pic.h>
|
||||||
|
|
||||||
|
void PIC_sendEOI(uint8_t irq)
|
||||||
|
{
|
||||||
|
if (irq >= 8) // if we're over the PIC1 limit
|
||||||
|
outb(PIC2_COMMAND, PIC_EOI);
|
||||||
|
outb(PIC1_COMMAND, PIC_EOI); // if the IRQ came from the slave, it must go to both PICs
|
||||||
|
}
|
||||||
|
|
||||||
|
void PIC_remap(int offset1, int offset2)
|
||||||
|
{
|
||||||
|
#ifdef __TESTING__
|
||||||
|
kinfo("Remapping the PIC...");
|
||||||
|
#endif
|
||||||
|
// The io_wait calls are necessary for older machines, to give the PIC time to react
|
||||||
|
//
|
||||||
|
// After the init, the PIC requires 3 init words
|
||||||
|
// ICW2 // its vector offset
|
||||||
|
// ICW3 // how its wired to the master/slave
|
||||||
|
// ICW4 // additional info about the environment
|
||||||
|
|
||||||
|
outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); // starts the init sequence, in cascade mode
|
||||||
|
io_wait();
|
||||||
|
outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4);
|
||||||
|
io_wait();
|
||||||
|
outb(PIC1_DATA, offset1); // ICW2 - the offset for master
|
||||||
|
io_wait();
|
||||||
|
outb(PIC2_DATA, offset2); // same as above for slave
|
||||||
|
io_wait();
|
||||||
|
outb(PIC1_DATA, 4); // ICW3 - Tells master theres a slave at IRQ2
|
||||||
|
io_wait();
|
||||||
|
outb(PIC2_DATA, 2); // ICW3 - Tells slave the cascade identity
|
||||||
|
io_wait();
|
||||||
|
|
||||||
|
outb(PIC1_DATA, ICW4_8086); // ICW4 - Use 8086 mode (not 8080 mode)
|
||||||
|
io_wait();
|
||||||
|
outb(PIC2_DATA, ICW4_8086);
|
||||||
|
io_wait();
|
||||||
|
|
||||||
|
// Unmask the PICs
|
||||||
|
outb(PIC1_DATA, 0);
|
||||||
|
outb(PIC2_DATA, 0);
|
||||||
|
|
||||||
|
#ifdef __TESTING__
|
||||||
|
kinfo("Remapped the PIC!");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void pic_disable(void)
|
||||||
|
{ // Mask the PIC interrupts to disable them
|
||||||
|
outb(PIC1_DATA, 0xFF);
|
||||||
|
outb(PIC2_DATA, 0xFF);
|
||||||
|
#ifdef __TESTING__
|
||||||
|
kinfo("Disabled the PIC");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRQ_set_mask(uint8_t IRQline) // Masked IRQlines are ignored by the PIC, masked IRQ2 will fully ignore the slave
|
||||||
|
{
|
||||||
|
uint16_t port;
|
||||||
|
uint8_t value;
|
||||||
|
|
||||||
|
if (IRQline < 8) {
|
||||||
|
port = PIC1_DATA;
|
||||||
|
} else {
|
||||||
|
port = PIC2_DATA;
|
||||||
|
IRQline -= 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
value = inb(port) | (1 << IRQline);
|
||||||
|
outb(port, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRQ_clear_mask(uint8_t IRQline)
|
||||||
|
{
|
||||||
|
uint16_t port;
|
||||||
|
uint8_t value;
|
||||||
|
|
||||||
|
if (IRQline < 8) {
|
||||||
|
port = PIC1_DATA;
|
||||||
|
} else {
|
||||||
|
port = PIC2_DATA;
|
||||||
|
IRQline -= 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
value = inb(port) & ~(1 << IRQline);
|
||||||
|
outb(port, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t __pic_get_irq_reg(int ocw3)
|
||||||
|
{
|
||||||
|
/** OCW3 to PIC CMD to get the register values
|
||||||
|
* PIC2 is chained, and represents IRQs 8-1.
|
||||||
|
* PIC1 is IRQs 0-7, with 2 being the chain **/
|
||||||
|
outb(PIC1_COMMAND, ocw3);
|
||||||
|
outb(PIC2_COMMAND, ocw3);
|
||||||
|
|
||||||
|
return (inb(PIC2_COMMAND) << 8) | inb(PIC1_COMMAND);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t pic_get_irr(void)
|
||||||
|
{
|
||||||
|
return __pic_get_irq_reg(PIC_READ_IRR);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t pic_get_isr(void)
|
||||||
|
{
|
||||||
|
return __pic_get_irq_reg(PIC_READ_ISR);
|
||||||
|
}
|
||||||
|
|
@ -47,13 +47,15 @@
|
|||||||
SEG_PRIV(3) | SEG_DATA_RDWR
|
SEG_PRIV(3) | SEG_DATA_RDWR
|
||||||
|
|
||||||
|
|
||||||
|
#define GDT_SIZE 5
|
||||||
|
|
||||||
|
|
||||||
void setGdt(unsigned short limit, uint64_t* base);
|
void setGdt(unsigned short limit, uint64_t* base);
|
||||||
|
|
||||||
void reloadSegments();
|
void reloadSegments();
|
||||||
|
|
||||||
uint64_t create_descriptor(uint32_t base, uint32_t limit, uint16_t flag);
|
uint64_t create_descriptor(uint32_t base, uint32_t limit, uint16_t flag);
|
||||||
|
void gdt_init(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
* Sends the given data to the I/O port. Defined in io.s
|
* Sends the given data to the I/O port. Defined in io.s
|
||||||
*
|
*
|
||||||
* @param port The I/O port to send the data to
|
* @param port The I/O port to send the data to
|
||||||
* @param data TThe data to send to the I/O port
|
* @param data The data to send to the I/O port
|
||||||
*/
|
*/
|
||||||
void outb(unsigned short port, unsigned char data);
|
void outb(unsigned short port, unsigned char data);
|
||||||
|
|
||||||
@ -19,6 +19,15 @@ void outb(unsigned short port, unsigned char data);
|
|||||||
*/
|
*/
|
||||||
unsigned char inb(unsigned short port);
|
unsigned char inb(unsigned short port);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* io_wait:
|
||||||
|
* Wait for a very small amount of time (1 to 4 microseconds, generally)
|
||||||
|
* A simple imprecise wait.
|
||||||
|
*
|
||||||
|
* This performs an operation (sends 0) to port 0x80
|
||||||
|
*/
|
||||||
|
void io_wait(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
59
kernel/include/kernel/x86/pic.h
Normal file
59
kernel/include/kernel/x86/pic.h
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifndef ARCH_PIC_H
|
||||||
|
#define ARCH_PIC_H
|
||||||
|
|
||||||
|
/** PIC I/O ports **/
|
||||||
|
#define PIC1 0x20 /** Master PIC **/
|
||||||
|
#define PIC2 0xA0 /** Slave PIC **/
|
||||||
|
|
||||||
|
/** PIC helper defines **/
|
||||||
|
#define PIC1_COMMAND (PIC1)
|
||||||
|
#define PIC1_DATA (PIC1 + 1)
|
||||||
|
#define PIC2_COMMAND (PIC2)
|
||||||
|
#define PIC2_DATA (PIC2 + 1)
|
||||||
|
|
||||||
|
/** PIC Commands **/
|
||||||
|
#define ICW1_ICW4 0x01 /** Indicates ICW4 will be present **/
|
||||||
|
#define ICW1_SINGLE 0x02 /** Single (cascade mode) **/
|
||||||
|
#define ICW1_INTERVAL4 0x04 /** Call address interval 4 (8) **/
|
||||||
|
#define ICW1_LEVEL 0x08 /** Level triggered (edge) mode **/
|
||||||
|
#define ICW1_INIT 0x10 /** Initialization **/
|
||||||
|
|
||||||
|
#define ICW4_8086 0x01 /** 8086/88 (MCS-80/85) mode **/
|
||||||
|
#define ICW4_AUTO 0x02 /** Auto (normal) EOI **/
|
||||||
|
#define ICW4_BUF_SLAVE 0x08 /** Buffered mode/slave **/
|
||||||
|
#define ICW4_BUF_MASTER 0x0C /** Buffered mode/master **/
|
||||||
|
#define ICW4_SFNM 0x10 /** Special fully nested (not) **/
|
||||||
|
|
||||||
|
#define PIC_EOI 0x20 /** End-of-interrupt command code **/
|
||||||
|
#define PIC_READ_IRR 0x0a /** OCW3 irq ready next CMD read **/
|
||||||
|
#define PIC_READ_ISR 0x0b /** OCW3 irq service next CMD read **/
|
||||||
|
|
||||||
|
|
||||||
|
void PIC_sendEOI(uint8_t irq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PIC_remap:
|
||||||
|
*
|
||||||
|
* @param offset1 - Vector offset for master PIC
|
||||||
|
* @param offset2 - Vector offset for slave PIC
|
||||||
|
*/
|
||||||
|
void PIC_remap(int offset1, int offset2);
|
||||||
|
|
||||||
|
void pic_disable(void);
|
||||||
|
|
||||||
|
void IRQ_set_mask(uint8_t IRQline);
|
||||||
|
void IRQ_clear_mask(uint8_t IRQline);
|
||||||
|
|
||||||
|
/** Returns the combined value of the cascaded PICs irq request register **/
|
||||||
|
uint16_t pic_get_irr(void);
|
||||||
|
/** Returns the combined value of the cascaded PICs in-service register **/
|
||||||
|
uint16_t pic_get_isr(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: implement handling for Spurious IRQs
|
||||||
|
* https://wiki.osdev.org/8259_PIC#Spurious_IRQs
|
||||||
|
*/
|
||||||
|
|
||||||
|
#endif
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <kernel/_kernel.h>
|
#include <kernel/_kernel.h>
|
||||||
#include <kernel/serial.h>
|
#include <kernel/serial.h>
|
||||||
|
#include <kernel/tty.h>
|
||||||
|
|
||||||
enum log_mode {
|
enum log_mode {
|
||||||
LOG_ERR,
|
LOG_ERR,
|
||||||
@ -14,16 +15,22 @@ void klog(const char* buf, enum log_mode mode)
|
|||||||
switch(mode) {
|
switch(mode) {
|
||||||
case LOG_ERR:
|
case LOG_ERR:
|
||||||
serial_writestring("ERROR: ");
|
serial_writestring("ERROR: ");
|
||||||
|
terminal_writestring("ERROR: ");
|
||||||
break;
|
break;
|
||||||
case LOG_WARN:
|
case LOG_WARN:
|
||||||
serial_writestring("WARNING: ");
|
serial_writestring("WARNING: ");
|
||||||
|
terminal_writestring("WARNING: ");
|
||||||
break;
|
break;
|
||||||
case LOG_INFO:
|
case LOG_INFO:
|
||||||
serial_writestring("INFO: ");
|
serial_writestring("INFO: ");
|
||||||
|
terminal_writestring("INFO: ");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
serial_writestring(buf);
|
serial_writestring(buf);
|
||||||
|
terminal_writestring(buf);
|
||||||
|
|
||||||
serial_writestring("\n");
|
serial_writestring("\n");
|
||||||
|
terminal_writestring("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void kerror(const char* buf)
|
void kerror(const char* buf)
|
||||||
|
@ -1,18 +1,20 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <kernel/_kernel.h>
|
||||||
#include <kernel/tty.h>
|
#include <kernel/tty.h>
|
||||||
#include <kernel/serial.h>
|
#include <kernel/serial.h>
|
||||||
#include <kernel/x86/gdt.h>
|
#include <kernel/x86/gdt.h>
|
||||||
#include <kernel/x86/idt.h>
|
#include <kernel/x86/idt.h>
|
||||||
|
#include <kernel/x86/pic.h>
|
||||||
#define GDT_SIZE 5
|
|
||||||
|
|
||||||
uint64_t gdt[GDT_SIZE];
|
uint64_t gdt[GDT_SIZE];
|
||||||
|
|
||||||
|
void gdt_init()
|
||||||
void gdt_init(void)
|
|
||||||
{
|
{
|
||||||
|
#ifdef __TESTING__
|
||||||
|
kinfo("Initializing the GDT");
|
||||||
|
#endif
|
||||||
gdt[0] = create_descriptor(0, 0, 0); // null
|
gdt[0] = create_descriptor(0, 0, 0); // null
|
||||||
gdt[1] = create_descriptor(0, 0x000FFFFF, (GDT_CODE_PL0)); // kernel code
|
gdt[1] = create_descriptor(0, 0x000FFFFF, (GDT_CODE_PL0)); // kernel code
|
||||||
gdt[2] = create_descriptor(0, 0x000FFFFF, (GDT_DATA_PL0)); // kernel data
|
gdt[2] = create_descriptor(0, 0x000FFFFF, (GDT_DATA_PL0)); // kernel data
|
||||||
@ -21,22 +23,29 @@ void gdt_init(void)
|
|||||||
|
|
||||||
setGdt((sizeof(uint64_t) * GDT_SIZE) - 1, &(gdt[0])); // limit, base
|
setGdt((sizeof(uint64_t) * GDT_SIZE) - 1, &(gdt[0])); // limit, base
|
||||||
reloadSegments();
|
reloadSegments();
|
||||||
|
#ifdef __TESTING__
|
||||||
|
kinfo("Initialized the GDT");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef GDT_SIZE
|
|
||||||
|
|
||||||
void kmain(void)
|
void kmain(void)
|
||||||
{
|
{
|
||||||
gdt_init();
|
#ifdef __TESTING__ // important components should be declared first, but if we're testing we want to log all of that
|
||||||
idt_init();
|
|
||||||
|
|
||||||
terminal_initialize();
|
terminal_initialize();
|
||||||
serial_initialize();
|
serial_initialize();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
terminal_writestring("test");
|
gdt_init();
|
||||||
|
idt_init();
|
||||||
|
PIC_remap(0x20, 0x28);
|
||||||
|
|
||||||
serial_writestring("test!");
|
#ifndef __TESTING__
|
||||||
|
terminal_initialize();
|
||||||
|
serial_initialize();
|
||||||
|
#endif
|
||||||
|
|
||||||
printf("test..");
|
|
||||||
|
printf("Integer: %d\n", 10);
|
||||||
|
printf("Hex Int: %x\n", 2);
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,56 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <kernel/_kernel.h>
|
||||||
|
|
||||||
|
static void reverse(char* buf, int len)
|
||||||
|
{
|
||||||
|
int start = 0;
|
||||||
|
int end = len - 1;
|
||||||
|
while (start < end) {
|
||||||
|
char tmp = buf[start];
|
||||||
|
buf[start] = buf[end];
|
||||||
|
buf[end] = tmp;
|
||||||
|
end--;
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static char* itoa(int num, char* buf, int base)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
bool neg = false;
|
||||||
|
|
||||||
|
if (num == 0) { // Handle zero explicitly
|
||||||
|
buf[i++] = '0';
|
||||||
|
buf[i] = '\0';
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (num < 0 && base == 10) { // only handle negative on base10
|
||||||
|
neg = true;
|
||||||
|
num = -num;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (num != 0) {
|
||||||
|
int rem = num % base;
|
||||||
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
||||||
|
num = num / base;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (neg) // lets reapply the negative sign
|
||||||
|
buf[i++] = '-';
|
||||||
|
|
||||||
|
buf[i] = '\0';
|
||||||
|
|
||||||
|
reverse(buf, i); // reverse, since we did it backwards!
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
static bool print(const char* data, size_t length) {
|
static bool print(const char* data, size_t length) {
|
||||||
const unsigned char* bytes = (const unsigned char*) data;
|
const unsigned char* bytes = (const unsigned char*) data;
|
||||||
for (size_t i = 0; i < length; i++)
|
for (size_t i = 0; i < length; i++)
|
||||||
if (putchar(bytes[i]) == EOF)
|
if (putchar(bytes[i]) == EOF) return false;
|
||||||
return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,6 +63,8 @@ int printf(const char* restrict format, ...) {
|
|||||||
|
|
||||||
int written = 0;
|
int written = 0;
|
||||||
|
|
||||||
|
char buffer[512];
|
||||||
|
|
||||||
while (*format != '\0') {
|
while (*format != '\0') {
|
||||||
size_t maxrem = INT_MAX - written;
|
size_t maxrem = INT_MAX - written;
|
||||||
|
|
||||||
@ -61,6 +108,30 @@ int printf(const char* restrict format, ...) {
|
|||||||
if (!print(str, len))
|
if (!print(str, len))
|
||||||
return -1;
|
return -1;
|
||||||
written += len;
|
written += len;
|
||||||
|
} else if (*format == 'd') {
|
||||||
|
format++;
|
||||||
|
int i = (int) va_arg(parameters, int);
|
||||||
|
itoa(i, buffer, 10);
|
||||||
|
size_t len = strlen(buffer);
|
||||||
|
if (maxrem < len) {
|
||||||
|
// TODO: Set errno to EOVERFLOW.
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!print(buffer, len))
|
||||||
|
return -1;
|
||||||
|
written += len;
|
||||||
|
} else if (*format == 'x') {
|
||||||
|
format++;
|
||||||
|
int i = (int) va_arg(parameters, int);
|
||||||
|
itoa(i, buffer, 16);
|
||||||
|
size_t len = strlen(buffer);
|
||||||
|
if (maxrem < len) {
|
||||||
|
// TODO: Set errno to EOVERFLOW.
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!print(buffer, len))
|
||||||
|
return -1;
|
||||||
|
written += len;
|
||||||
} else {
|
} else {
|
||||||
format = format_begun_at;
|
format = format_begun_at;
|
||||||
size_t len = strlen(format);
|
size_t len = strlen(format);
|
||||||
|
@ -8,3 +8,4 @@ log: bochslog.txt
|
|||||||
clock: sync=realtime, time0=local
|
clock: sync=realtime, time0=local
|
||||||
cpu: count=1, ips=1000000
|
cpu: count=1, ips=1000000
|
||||||
com1: enabled=1, mode=file, dev=com1.out
|
com1: enabled=1, mode=file, dev=com1.out
|
||||||
|
keyboard: type=mf, serial_delay=150
|
||||||
|
Reference in New Issue
Block a user