Updated Makefile, started splitting code in to multiple pieces.
This commit is contained in:
parent
a4d1679723
commit
03e7647fc6
32
Makefile
32
Makefile
@ -1,7 +1,13 @@
|
|||||||
CFLAGS = -m32 -nostdlib -fno-builtin -fno-exceptions -fno-leading-underscore -fno-pie -fno-stack-protector
|
CFLAGS = -m32 -nostdlib -fno-builtin -fno-exceptions -fno-leading-underscore -fno-pie -fno-stack-protector
|
||||||
LDFLAGS =
|
LDFLAGS =
|
||||||
|
|
||||||
disk.img: boot.sector kernel.bin hello.bin print.bin hello.txt boot32.bin kernel32.bin
|
DISK_IMG_FILES = kernel.bin hello.bin print.bin boot32.bin kernel32.bin
|
||||||
|
KERNEL32_OBJS = screenstuff.o io.o kernel32_strap.o kernel32.o
|
||||||
|
|
||||||
|
run: disk.img
|
||||||
|
qemu-system-x86_64 disk.img
|
||||||
|
|
||||||
|
disk.img: clean boot.sector $(DISK_IMG_FILES)
|
||||||
dd if=/dev/zero of=disk.img count=43 bs=100k
|
dd if=/dev/zero of=disk.img count=43 bs=100k
|
||||||
dd if=boot.sector of=disk.img conv=notrunc
|
dd if=boot.sector of=disk.img conv=notrunc
|
||||||
mount disk.img img.d
|
mount disk.img img.d
|
||||||
@ -10,26 +16,20 @@ disk.img: boot.sector kernel.bin hello.bin print.bin hello.txt boot32.bin kernel
|
|||||||
umount img.d
|
umount img.d
|
||||||
chmod 777 disk.img
|
chmod 777 disk.img
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm $(DISK_IMG_FILES) $(KERNEL32_OBJS) boot.sector disk.img || true
|
||||||
|
|
||||||
boot.sector: boot.asm
|
boot.sector: boot.asm
|
||||||
nasm $< -o $@
|
nasm $< -o $@
|
||||||
|
|
||||||
kernel.bin: kernel.asm
|
%.bin: %.asm
|
||||||
nasm $< -o $@
|
nasm $< -o $@
|
||||||
|
|
||||||
hello.bin: hello.asm
|
kernel32.bin: kernel32.ld $(KERNEL32_OBJS)
|
||||||
nasm $< -o $@
|
ld $(LDFLAGS) -T $< $(KERNEL32_OBJS)
|
||||||
|
|
||||||
print.bin: print.asm
|
|
||||||
nasm $< -o $@
|
|
||||||
|
|
||||||
boot32.bin: boot32.asm
|
|
||||||
nasm $< -o $@
|
|
||||||
|
|
||||||
kernel32.bin: kernel32.ld kernel32_strap.o kernel32.o
|
|
||||||
ld $(LDFLAGS) -T $<
|
|
||||||
|
|
||||||
kernel32.o: kernel32.c
|
|
||||||
gcc $(CFLAGS) -o $@ -c $<
|
|
||||||
|
|
||||||
kernel32_strap.o: kernel32_strap.asm
|
kernel32_strap.o: kernel32_strap.asm
|
||||||
nasm -felf32 $< -o $@
|
nasm -felf32 $< -o $@
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
gcc $(CFLAGS) -o $@ -c $<
|
10
io.c
Normal file
10
io.c
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "io.h"
|
||||||
|
|
||||||
|
void outb(uint16_t portnumber, uint8_t data) {
|
||||||
|
asm volatile("outb %0, %1" : : "a" (data), "Nd" (portnumber));
|
||||||
|
}
|
||||||
|
uint8_t inb(uint16_t portnumber) {
|
||||||
|
uint8_t result;
|
||||||
|
asm volatile("inb %1, %0" : "=a" (result) : "Nd" (portnumber));
|
||||||
|
return result;
|
||||||
|
}
|
9
io.h
Normal file
9
io.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef IO_H
|
||||||
|
#define IO_H
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
void outb(uint16_t portnumber, uint8_t data);
|
||||||
|
uint8_t inb(uint16_t portnumber);
|
||||||
|
|
||||||
|
#endif
|
155
kernel32.c
155
kernel32.c
@ -1,155 +1,6 @@
|
|||||||
#include <stdarg.h>
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
#include "screenstuff.h"
|
||||||
uint16_t* VMEM_ADDR = (uint16_t*)0xb8000;
|
#include "io.h"
|
||||||
const int TERM_WIDTH = 80;
|
|
||||||
const int TERM_HEIGHT = 25;
|
|
||||||
|
|
||||||
int cursor_x = 0;
|
|
||||||
int cursor_y = 0;
|
|
||||||
|
|
||||||
void outb(uint16_t portnumber, uint8_t data) {
|
|
||||||
asm volatile("outb %0, %1" : : "a" (data), "Nd" (portnumber));
|
|
||||||
}
|
|
||||||
uint8_t inb(uint16_t portnumber) {
|
|
||||||
uint8_t result;
|
|
||||||
asm volatile("inb %1, %0" : "=a" (result) : "Nd" (portnumber));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t get_curpos() {
|
|
||||||
uint16_t cursor_position = 0;
|
|
||||||
uint8_t* cursor_position_split = (uint8_t*)&cursor_position;
|
|
||||||
outb(0x3D4, 0x0F);
|
|
||||||
cursor_position_split[0] = inb(0x3D5);
|
|
||||||
outb(0x3D4, 0x0E);
|
|
||||||
cursor_position_split[1] = inb(0x3D5);
|
|
||||||
return cursor_position;
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_term() {
|
|
||||||
uint16_t cursor_position = get_curpos();
|
|
||||||
|
|
||||||
cursor_y = cursor_position / TERM_WIDTH;
|
|
||||||
cursor_x = cursor_position % TERM_WIDTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear_screen() {
|
|
||||||
for (int i=0; i<TERM_WIDTH*TERM_HEIGHT; i++) {
|
|
||||||
VMEM_ADDR[i] = 0x0720;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear_line(int line) {
|
|
||||||
for (int x=0; x<TERM_WIDTH; x++) {
|
|
||||||
VMEM_ADDR[TERM_WIDTH*line + x] = 0x0720;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_curpos_raw(int curpos) {
|
|
||||||
uint8_t* cursor_position_split = (uint8_t*)&curpos;
|
|
||||||
outb(0x3D4, 0x0F);
|
|
||||||
outb(0x3D5, cursor_position_split[0]);
|
|
||||||
outb(0x3D4, 0x0E);
|
|
||||||
outb(0x3D5, cursor_position_split[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_curpos(int x, int y) {
|
|
||||||
set_curpos_raw(y * TERM_WIDTH + x);
|
|
||||||
|
|
||||||
cursor_x = x;
|
|
||||||
cursor_y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
int int_to_decimal(unsigned int number, char* string_buffer) {
|
|
||||||
for (int i=0; i<11; i++)
|
|
||||||
string_buffer[i] = 0;
|
|
||||||
|
|
||||||
int index = 9;
|
|
||||||
unsigned int acc = number;
|
|
||||||
while (acc != 0) {
|
|
||||||
string_buffer[index--] = 0x30+(acc%10);
|
|
||||||
acc /= 10;
|
|
||||||
}
|
|
||||||
return (index+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
char dec_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
|
||||||
|
|
||||||
int int_to_hex(unsigned int number, char* string_buffer) {
|
|
||||||
for (int i=0; i<9; i++)
|
|
||||||
string_buffer[i] = 0;
|
|
||||||
|
|
||||||
int index = 7;
|
|
||||||
unsigned int acc = number;
|
|
||||||
while (acc != 0) {
|
|
||||||
string_buffer[index--] = dec_to_hex[acc%0x10];
|
|
||||||
acc /= 0x10;
|
|
||||||
}
|
|
||||||
return (index+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void printf(const char* string, ...) {
|
|
||||||
va_list ptr;
|
|
||||||
va_start(ptr, string);
|
|
||||||
|
|
||||||
int index = 0;
|
|
||||||
int count = 0;
|
|
||||||
char current;
|
|
||||||
while (current=string[index++]) {
|
|
||||||
count++;
|
|
||||||
if (current == '\n') {
|
|
||||||
cursor_x = 0;
|
|
||||||
cursor_y++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cursor_x == TERM_WIDTH) {
|
|
||||||
cursor_x = 0;
|
|
||||||
cursor_y++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cursor_y == TERM_HEIGHT) {
|
|
||||||
for (int i=1; i<TERM_HEIGHT; i++) {
|
|
||||||
for (int x=0; x<TERM_WIDTH; x++) {
|
|
||||||
int from = i * TERM_WIDTH + x;
|
|
||||||
int to = (i-1) * TERM_WIDTH + x;
|
|
||||||
VMEM_ADDR[to] = VMEM_ADDR[from];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
clear_line(24);
|
|
||||||
cursor_y--;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == '%') {
|
|
||||||
int type = string[index++];
|
|
||||||
int offset;
|
|
||||||
switch (type) {
|
|
||||||
case 'd':
|
|
||||||
char decimal_buffer[11];
|
|
||||||
offset = int_to_decimal(va_arg(ptr, int), decimal_buffer);
|
|
||||||
printf(decimal_buffer + offset);
|
|
||||||
break;
|
|
||||||
case 'x':
|
|
||||||
char hex_buffer[8];
|
|
||||||
offset = int_to_hex(va_arg(ptr, int), hex_buffer);
|
|
||||||
printf(hex_buffer + offset);
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
printf(va_arg(ptr, const char*));
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current != '\n') {
|
|
||||||
int mem_pos = cursor_y * TERM_WIDTH + cursor_x++;
|
|
||||||
VMEM_ADDR[mem_pos] = current + (0x07<<8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
set_curpos(cursor_x, cursor_y);
|
|
||||||
|
|
||||||
va_end(ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
init_term();
|
init_term();
|
||||||
@ -158,5 +9,5 @@ int main() {
|
|||||||
|
|
||||||
printf("Hello, World!\n\nWe are running XnoeOS Code in C now, Protected Mode has been achieved and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~\n");
|
printf("Hello, World!\n\nWe are running XnoeOS Code in C now, Protected Mode has been achieved and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~\n");
|
||||||
|
|
||||||
while (1) {}
|
while (1);
|
||||||
}
|
}
|
@ -1,7 +1,6 @@
|
|||||||
OUTPUT_FORMAT(binary)
|
OUTPUT_FORMAT(binary)
|
||||||
OUTPUT_ARCH(i386:i386)
|
OUTPUT_ARCH(i386:i386)
|
||||||
|
|
||||||
INPUT(kernel32_strap.o kernel32.o)
|
|
||||||
OUTPUT(kernel32.bin)
|
OUTPUT(kernel32.bin)
|
||||||
|
|
||||||
SECTIONS {
|
SECTIONS {
|
||||||
@ -10,5 +9,6 @@ SECTIONS {
|
|||||||
.text : {
|
.text : {
|
||||||
kernel32_strap.o(.text)
|
kernel32_strap.o(.text)
|
||||||
kernel32.o(.text)
|
kernel32.o(.text)
|
||||||
|
*(.text)
|
||||||
}
|
}
|
||||||
}
|
}
|
141
screenstuff.c
Normal file
141
screenstuff.c
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
#include "screenstuff.h"
|
||||||
|
|
||||||
|
uint16_t* VMEM_ADDR = (uint16_t*)0xb8000;
|
||||||
|
const int TERM_WIDTH = 80;
|
||||||
|
const int TERM_HEIGHT = 25;
|
||||||
|
|
||||||
|
int cursor_x = 0;
|
||||||
|
int cursor_y = 0;
|
||||||
|
|
||||||
|
uint16_t get_curpos() {
|
||||||
|
uint16_t cursor_position = 0;
|
||||||
|
uint8_t* cursor_position_split = (uint8_t*)&cursor_position;
|
||||||
|
outb(0x3D4, 0x0F);
|
||||||
|
cursor_position_split[0] = inb(0x3D5);
|
||||||
|
outb(0x3D4, 0x0E);
|
||||||
|
cursor_position_split[1] = inb(0x3D5);
|
||||||
|
return cursor_position;
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_term() {
|
||||||
|
uint16_t cursor_position = get_curpos();
|
||||||
|
|
||||||
|
cursor_y = cursor_position / TERM_WIDTH;
|
||||||
|
cursor_x = cursor_position % TERM_WIDTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_screen() {
|
||||||
|
for (int i=0; i<TERM_WIDTH*TERM_HEIGHT; i++) {
|
||||||
|
VMEM_ADDR[i] = 0x0720;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_line(int line) {
|
||||||
|
for (int x=0; x<TERM_WIDTH; x++) {
|
||||||
|
VMEM_ADDR[TERM_WIDTH*line + x] = 0x0720;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_curpos_raw(int curpos) {
|
||||||
|
uint8_t* cursor_position_split = (uint8_t*)&curpos;
|
||||||
|
outb(0x3D4, 0x0F);
|
||||||
|
outb(0x3D5, cursor_position_split[0]);
|
||||||
|
outb(0x3D4, 0x0E);
|
||||||
|
outb(0x3D5, cursor_position_split[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_curpos(int x, int y) {
|
||||||
|
set_curpos_raw(y * TERM_WIDTH + x);
|
||||||
|
|
||||||
|
cursor_x = x;
|
||||||
|
cursor_y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
int int_to_decimal(unsigned int number, char* string_buffer) {
|
||||||
|
for (int i=0; i<11; i++)
|
||||||
|
string_buffer[i] = 0;
|
||||||
|
|
||||||
|
int index = 9;
|
||||||
|
unsigned int acc = number;
|
||||||
|
while (acc != 0) {
|
||||||
|
string_buffer[index--] = 0x30+(acc%10);
|
||||||
|
acc /= 10;
|
||||||
|
}
|
||||||
|
return (index+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
char dec_to_hex[16] = "0123456789abcdef";
|
||||||
|
int int_to_hex(unsigned int number, char* string_buffer) {
|
||||||
|
for (int i=0; i<9; i++)
|
||||||
|
string_buffer[i] = 0;
|
||||||
|
|
||||||
|
int index = 7;
|
||||||
|
unsigned int acc = number;
|
||||||
|
while (acc != 0) {
|
||||||
|
string_buffer[index--] = dec_to_hex[acc%0x10];
|
||||||
|
acc /= 0x10;
|
||||||
|
}
|
||||||
|
return (index+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printf(const char* string, ...) {
|
||||||
|
va_list ptr;
|
||||||
|
va_start(ptr, string);
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
int count = 0;
|
||||||
|
char current;
|
||||||
|
while (current=string[index++]) {
|
||||||
|
count++;
|
||||||
|
if (current == '\n') {
|
||||||
|
cursor_x = 0;
|
||||||
|
cursor_y++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cursor_x == TERM_WIDTH) {
|
||||||
|
cursor_x = 0;
|
||||||
|
cursor_y++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cursor_y == TERM_HEIGHT) {
|
||||||
|
for (int i=1; i<TERM_HEIGHT; i++) {
|
||||||
|
for (int x=0; x<TERM_WIDTH; x++) {
|
||||||
|
int from = i * TERM_WIDTH + x;
|
||||||
|
int to = (i-1) * TERM_WIDTH + x;
|
||||||
|
VMEM_ADDR[to] = VMEM_ADDR[from];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clear_line(24);
|
||||||
|
cursor_y--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current == '%') {
|
||||||
|
int type = string[index++];
|
||||||
|
int offset;
|
||||||
|
switch (type) {
|
||||||
|
case 'd':
|
||||||
|
char decimal_buffer[11];
|
||||||
|
offset = int_to_decimal(va_arg(ptr, int), decimal_buffer);
|
||||||
|
printf(decimal_buffer + offset);
|
||||||
|
break;
|
||||||
|
case 'x':
|
||||||
|
char hex_buffer[8];
|
||||||
|
offset = int_to_hex(va_arg(ptr, int), hex_buffer);
|
||||||
|
printf(hex_buffer + offset);
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
printf(va_arg(ptr, const char*));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current != '\n') {
|
||||||
|
int mem_pos = cursor_y * TERM_WIDTH + cursor_x++;
|
||||||
|
VMEM_ADDR[mem_pos] = current + (0x07<<8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
set_curpos(cursor_x, cursor_y);
|
||||||
|
|
||||||
|
va_end(ptr);
|
||||||
|
}
|
18
screenstuff.h
Normal file
18
screenstuff.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef SCREENSTUFF_H
|
||||||
|
#define SCREENSTUFF_H
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "io.h"
|
||||||
|
|
||||||
|
uint16_t get_curpos();
|
||||||
|
void init_term();
|
||||||
|
void clear_screen();
|
||||||
|
void clear_line(int line);
|
||||||
|
void set_curpos_raw(int curpos);
|
||||||
|
void set_curpos(int x, int y);
|
||||||
|
int int_to_decimal(unsigned int number, char* string_buffer);
|
||||||
|
int int_to_hex(unsigned int number, char* string_buffer);
|
||||||
|
void printf(const char* string, ...);
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user