diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0285b5d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "kmain.h": "c", + "io.h": "c" + } +} \ No newline at end of file diff --git a/Makefile b/Makefile index 769468c..5144219 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJECTS = loader.o kmain.o +OBJECTS = io.o loader.o kmain.o CC = gcc CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \ -nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c diff --git a/io.c b/io.c new file mode 100644 index 0000000..4bfcbed --- /dev/null +++ b/io.c @@ -0,0 +1,24 @@ +#include "kmain.h" +#include "io.h" + +void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned char bg) { + char *fb = (char *) 0x000B8000; + fb[i] = c; + fb[i + 1] = ((fg & 0x0F) << 4) | (bg & 0x0F); +} + +void fb_move_cursor(unsigned short pos) +{ + outb(FB_COMMAND_PORT, FB_HIGH_BYTE_COMMAND); + outb(FB_DATA_PORT, ((pos >> 8) & 0x00FF)); + outb(FB_COMMAND_PORT, FB_LOW_BYTE_COMMAND); + outb(FB_DATA_PORT, pos & 0x00FF); +} + +int write(char *buf, unsigned int len) { + for(int i = 0; i <= (int)len; i++) { + fb_write_cell((int)(i * 2), buf[i], 0, 15); + fb_move_cursor(i); + } + return 0; +} \ No newline at end of file diff --git a/io.h b/io.h new file mode 100644 index 0000000..545cd18 --- /dev/null +++ b/io.h @@ -0,0 +1,36 @@ +#ifndef INCLUDE_IO_H +#define INCLUDE_IO_H + +/* The I/O ports */ +#define FB_COMMAND_PORT 0x3D4 +#define FB_DATA_PORT 0x3D5 + +/* The I/O port commands */ +#define FB_HIGH_BYTE_COMMAND 14 +#define FB_LOW_BYTE_COMMAND 15 + +int write(char *buf, unsigned int len); + +void outb(unsigned short port, unsigned char data); + +/** fb_write_cell: + * Writes a character with the given foreground and background to position i + * in the framebuffer. + * + * @param i The location in the framebuffer + * @param c The character + * @param fg The foreground color + * @param bg The background color + * + * @example fb_write_cell(0, 'A', 2, 8); + */ +void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned char bg); + +/** fb_move_cursor: + * Moves the cursor of the framebuffer to the given position + * + * @param pos The new position of the cursor + */ +void fb_move_cursor(unsigned short pos); + +#endif /* INCLUDE_IO_H */ \ No newline at end of file diff --git a/io.s b/io.s new file mode 100644 index 0000000..c5fbc4d --- /dev/null +++ b/io.s @@ -0,0 +1,11 @@ + global outb ; make the label outb visible outside this file + + ; outb - send a byte to an I/O port + ; stack: [esp + 8] the data byte + ; [esp + 4] the I/O port + ; [esp ] return address + outb: + mov al, [esp + 8] ; move the data to be sent into the al register + mov dx, [esp + 4] ; move the address of the I/O port into the dx register + out dx, al ; send the data to the I/O port + ret ; return to the calling function \ No newline at end of file diff --git a/kmain.c b/kmain.c index bc5a824..6a5fc91 100644 --- a/kmain.c +++ b/kmain.c @@ -1,31 +1,11 @@ -void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned char bg); - - -int main() { - fb_write_cell(0, 'A', 2, 8); +#include "kmain.h" +#include "io.h" + +int main() +{ + fb_write_cell(0, 'A', 0, 15); + fb_write_cell(2, 'B', 0, 15); + fb_write_cell(4, 'C', 0, 15); + fb_move_cursor(6); return 0; -} - -/** fb_write_cell: - * Writes a character with the given foreground and background to position i - * in the framebuffer. - - * - * @param i The location in the framebuffer - * @param c The character - * @param fg The foreground color - * @param bg The background color - * - * @example fb_write_cell(0, 'A', 2, 8); - */ -void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned char bg) { - char *fb = (char *) 0x000B8000; - fb[i] = c; - fb[i + 1] = ((fg & 0x0F) << 4) | (bg & 0x0F); -} - -struct example { - unsigned char config; /* bit 0 - 7 */ - unsigned short address; /* bit 8 - 23 */ - unsigned char index; /* bit 24 - 31 */ -} __attribute__((packed)); \ No newline at end of file +} \ No newline at end of file diff --git a/kmain.h b/kmain.h new file mode 100644 index 0000000..ec107a6 --- /dev/null +++ b/kmain.h @@ -0,0 +1 @@ +int main(); \ No newline at end of file diff --git a/loader.s b/loader.s index 89899bf..1824fd8 100644 --- a/loader.s +++ b/loader.s @@ -25,5 +25,7 @@ loader: ; the loader label (defined as entry point in li mov esp, kernel_stack + KERNEL_STACK_SIZE ; point esp to the start of the ; stack (end of memory area) call main + .loop: + jmp .loop ; loop forever \ No newline at end of file