From d474de82282fd6840ef5e54b801d7400e6c4ceb9 Mon Sep 17 00:00:00 2001 From: Jarkko Toivanen Date: Fri, 22 Sep 2023 18:17:00 +0300 Subject: [PATCH] Trying to somewhat move to 80char line limit and tabs as spaces --- .gitignore | 1 + src/framebuffer.c | 57 ++++++++++------ src/framebuffer.h | 20 +++++- src/kernel.c | 159 +++++++++++++++++++++++---------------------- src/serial.c | 80 ++++++++++++----------- src/start32.s | 68 +++++++++---------- src/xtoa.c | 162 ++++++++++++++++++++++++---------------------- src/xtoa.h | 8 +-- 8 files changed, 303 insertions(+), 252 deletions(-) diff --git a/.gitignore b/.gitignore index bc70311..770baf3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.elf *.o *.img +*.bin mnt/ build/ diff --git a/src/framebuffer.c b/src/framebuffer.c index 5fc0f6c..0040fe0 100644 --- a/src/framebuffer.c +++ b/src/framebuffer.c @@ -6,26 +6,43 @@ static unsigned short fb_width, fb_height; static unsigned char fb_bpp, fb_bytespp; static unsigned char fb_rpos, fb_bpos, fb_gpos; -void initfb(unsigned long addr, unsigned short w, unsigned short h, unsigned char bpp, unsigned long pitch, unsigned char rpos, unsigned char gpos, unsigned char bpos) { - fb_address = addr; - fb_pitch = pitch; - fb_width = w; - fb_height = h; - fb_bpp = bpp; - fb_bytespp = bpp/8; - fb_rpos = rpos; - fb_gpos = gpos; - fb_bpos = bpos; +void initfb( + unsigned long addr, + unsigned short w, + unsigned short h, + unsigned char bpp, + unsigned long pitch, + unsigned char rpos, + unsigned char gpos, + unsigned char bpos +){ + fb_address = addr; + fb_pitch = pitch; + fb_width = w; + fb_height = h; + fb_bpp = bpp; + fb_bytespp = bpp/8; + fb_rpos = rpos; + fb_gpos = gpos; + fb_bpos = bpos; } -void putpixel(unsigned short x, unsigned short y, unsigned char r, unsigned char g, unsigned char b, unsigned char a) { - if (x>fb_width || y> fb_height) return; - - if (fb_bpp == 32) { - *((unsigned long *)(fb_address + y*fb_pitch + x*fb_bytespp)) = r< fb_width || y > fb_height) + return; + unsigned long dest_addr = fb_address + y*fb_pitch + x*fb_bytespp; + if (fb_bpp == 32) { + *((unsigned long *)(dest_addr)) = r<flags, 2)); - serial_write_string("\nMultiboot flags: "); - serial_write_string(itoa(mbootinfo->flags, 2)); + // Check videomode + if (mbootinfo->flags & MULTIBOOT_INFO_VBE_INFO) { + serial_write_string("\nVBE Mode: 0x"); + serial_write_string(itoa(mbootinfo->vbe_mode, 16)); + } - // Check videomode - if (mbootinfo->flags & MULTIBOOT_INFO_VBE_INFO) { - serial_write_string("\nVBE Mode: 0x"); - serial_write_string(itoa(mbootinfo->vbe_mode, 16)); - } + if (!(mbootinfo->flags & MULTIBOOT_INFO_FRAMEBUFFER_INFO)) { + serial_write_string("\nVideo info not available"); + return; + } - if (!(mbootinfo->flags & MULTIBOOT_INFO_FRAMEBUFFER_INFO)) { - serial_write_string("\nVideo info not available"); - return; - } + serial_write_string("\nFramebuffer: "); + serial_write_string("\n Address: 0x"); + serial_write_string(ultoa(mbootinfo->framebuffer_addr, 16)); - serial_write_string("\nFramebuffer: "); - serial_write_string("\n Address: 0x"); - serial_write_string(ultoa(mbootinfo->framebuffer_addr, 16)); + serial_write_string("\n Dimensions: "); + serial_write_string(itoa(mbootinfo->framebuffer_width, 10)); + serial_write_string("X"); + serial_write_string(itoa(mbootinfo->framebuffer_height, 10)); + serial_write_string("x"); + serial_write_string(itoa(mbootinfo->framebuffer_bpp, 10)); + serial_write_string("\n Pitch: "); + serial_write_string(itoa(mbootinfo->framebuffer_pitch, 10)); + serial_write_string("\n RPos:"); + serial_write_string(itoa(mbootinfo->framebuffer_red_field_position, 10)); + serial_write_string("\n GPos:"); + serial_write_string(itoa(mbootinfo->framebuffer_green_field_position, 10)); + serial_write_string("\n BPos:"); + serial_write_string(itoa(mbootinfo->framebuffer_blue_field_position, 10)); - serial_write_string("\n Dimensions: "); - serial_write_string(itoa(mbootinfo->framebuffer_width, 10)); - serial_write_string("X"); - serial_write_string(itoa(mbootinfo->framebuffer_height, 10)); - serial_write_string("x"); - serial_write_string(itoa(mbootinfo->framebuffer_bpp, 10)); - serial_write_string("\n Pitch: "); - serial_write_string(itoa(mbootinfo->framebuffer_pitch, 10)); - serial_write_string("\n RPos:"); - serial_write_string(itoa(mbootinfo->framebuffer_red_field_position, 10)); - serial_write_string("\n GPos:"); - serial_write_string(itoa(mbootinfo->framebuffer_green_field_position, 10)); - serial_write_string("\n BPos:"); - serial_write_string(itoa(mbootinfo->framebuffer_blue_field_position, 10)); + serial_write_string("\nMemory: "); + serial_write_string("\n lower: "); + serial_write_string(uitoa(mbootinfo->mem_lower, 10)); + serial_write_string(" k"); + serial_write_string("\n upper: "); + serial_write_string(uitoa(mbootinfo->mem_upper/1024, 10)); + serial_write_string(" M"); - serial_write_string("\nMemory: "); - serial_write_string("\n lower: "); - serial_write_string(uitoa(mbootinfo->mem_lower, 10)); - serial_write_string(" k"); - serial_write_string("\n upper: "); - serial_write_string(uitoa(mbootinfo->mem_upper/1024, 10)); - serial_write_string(" M"); + initfb( + mbootinfo->framebuffer_addr, + mbootinfo->framebuffer_width, + mbootinfo->framebuffer_height, + mbootinfo->framebuffer_bpp, + mbootinfo->framebuffer_pitch, + mbootinfo->framebuffer_red_field_position, + mbootinfo->framebuffer_green_field_position, + mbootinfo->framebuffer_blue_field_position + ); + int x, y, i; + unsigned char c = 0; + for (;;) { + for (;c<255;c+=5) { + for(y=0; y < mbootinfo->framebuffer_height; y++) { + for(x=0; x < mbootinfo->framebuffer_width; x++) { + putpixel(x, y, 0, c, c, 0xff); + } + } + } + for (;c>0;c-=5) { + for(y=mbootinfo->framebuffer_height; y>=0; y--) { + for(x=mbootinfo->framebuffer_width; x>=0; x--) { + putpixel(x, y, 0, c, c, 0xff); + } + } + } + } - initfb(mbootinfo->framebuffer_addr, mbootinfo->framebuffer_width, mbootinfo->framebuffer_height, mbootinfo->framebuffer_bpp, mbootinfo->framebuffer_pitch, mbootinfo->framebuffer_red_field_position, mbootinfo->framebuffer_green_field_position, mbootinfo->framebuffer_blue_field_position); - int x, y, i; - unsigned char c = 0; - for(;;) { - //serial_write_string("\n"); - for (;c<255;c+=5) { - //serial_write_string("/"); - for(y=0; y < mbootinfo->framebuffer_height; y++) { - for(x=0; x < mbootinfo->framebuffer_width; x++) { - putpixel(x, y, 0, c, c, 0xff); - } - } - } - //serial_write_string("\n"); - for (;c>0;c-=5) { - //serial_write_string("\\"); - for(y=mbootinfo->framebuffer_height; y>=0; y--) { - for(x=mbootinfo->framebuffer_width; x>=0; x--) { - putpixel(x, y, 0, c, c, 0xff); - } - } - } - } - - serial_write_string("\nExecution finished, halting...\n"); + serial_write_string("\nExecution finished, halting...\n"); } diff --git a/src/serial.c b/src/serial.c index e4e8e27..d19762d 100644 --- a/src/serial.c +++ b/src/serial.c @@ -1,50 +1,56 @@ #include "serial.h" #define PORT 0x3f8 // COM1 -static inline void outb(unsigned short port, unsigned char val) { - asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port) : "memory"); +static inline void outb(unsigned short port, unsigned char val) +{ + asm volatile("outb %0, %1" : : "a"(val), "Nd"(port) : "memory"); } -static inline unsigned char inb(unsigned short port) { - unsigned char ret; - asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory"); - return ret; +static inline unsigned char inb(unsigned short port) +{ + unsigned char ret; + asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory"); + return ret; } -int serial_init() { - 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 - outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip - outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial - // returns same byte) +int serial_init() +{ + 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, 14-byte threshold + outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set + outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip + outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check + // if serial returns same byte) - // Check if serial is faulty (i.e: not same byte as sent) - if (inb(PORT + 0) != 0xAE) { - return 1; - } + // Check if serial is faulty (i.e: not same byte as sent) + if (inb(PORT + 0) != 0xAE) + return 1; - // If serial is not faulty set it in normal operation mode - // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) - outb(PORT + 4, 0x0F); - return 0; + // If serial is not faulty set it in normal operation mode + // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) + outb(PORT + 4, 0x0F); + return 0; } -static int serial_is_transmit_empty() { return inb(PORT + 5) & 0x20; } - -static void serial_write_char(char chr) { - while (serial_is_transmit_empty() == 0); - outb(PORT, chr); +static int serial_is_transmit_empty() +{ + return inb(PORT + 5) & 0x20; } -void serial_write_string(const char* text) { - int i = 0; - while(text[i]) { - serial_write_char(text[i]); - i++; - } - +static void serial_write_char(char chr) +{ + while (serial_is_transmit_empty() == 0); + outb(PORT, chr); +} + +void serial_write_string(const char* text) +{ + int i = 0; + while (text[i]) { + serial_write_char(text[i]); + i++; + } } diff --git a/src/start32.s b/src/start32.s index 32e185d..f03794a 100644 --- a/src/start32.s +++ b/src/start32.s @@ -1,48 +1,48 @@ -.set MULTIBOOT_PAGE_ALIGN, 1 << 0 -.set MULTIBOOT_MEMORY_INFO, 1 << 1 -.set MULTIBOOT_VIDEO_REQUEST, 1 << 2 -.set MULTIBOOT_AOUT_KLUDGE, 0 << 16 -.set MULTIBOOT_HEADER_MAGIC, 0x1BADB002 +.set MULTIBOOT_PAGE_ALIGN, 1 << 0 +.set MULTIBOOT_MEMORY_INFO, 1 << 1 +.set MULTIBOOT_VIDEO_REQUEST, 1 << 2 +.set MULTIBOOT_AOUT_KLUDGE, 0 << 16 +.set MULTIBOOT_HEADER_MAGIC, 0x1BADB002 .set MULTIBOOT_HEADER_FLAGS, \ - MULTIBOOT_PAGE_ALIGN |\ - MULTIBOOT_MEMORY_INFO |\ - MULTIBOOT_VIDEO_REQUEST |\ - MULTIBOOT_AOUT_KLUDGE -.set MULTIBOOT_CHECKSUM, -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) + MULTIBOOT_PAGE_ALIGN \ + | MULTIBOOT_MEMORY_INFO \ + | MULTIBOOT_VIDEO_REQUEST \ + | MULTIBOOT_AOUT_KLUDGE +.set MULTIBOOT_CHECKSUM, -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) #.section ".multiboottbabeee" .align 4 - .long MULTIBOOT_HEADER_MAGIC - .long MULTIBOOT_HEADER_FLAGS - .long MULTIBOOT_CHECKSUM - .long 0 # header address - .long 0 # load address - .long 0 # load end address - .long 0 # bss end address - .long 0 # entry address - .long 0 # video mode_type (0:fb, 1:txt) (set flags[2]!) - .long 640 # video width - .long 480 # video height - .long 32 # video depth + .long MULTIBOOT_HEADER_MAGIC + .long MULTIBOOT_HEADER_FLAGS + .long MULTIBOOT_CHECKSUM + .long 0 # header address + .long 0 # load address + .long 0 # load end address + .long 0 # bss end address + .long 0 # entry address + .long 0 # video mode_type (0:fb, 1:txt) (set flags[2]!) + .long 1024 # video width + .long 768 # video height + .long 32 # video depth .bss .align 16 - stack_bottom: - .skip 16384 # 16k - stack_top: + stack_bottom: + .skip 16384 # 16k + stack_top: .text .global _start _start: - # Setup stack - mov $stack_top, %esp + # Setup stack + mov $stack_top, %esp - # Call the main kernel function. - push %ebx - push %eax - call kmain + # Call the main kernel function. + push %ebx + push %eax + call kmain - cli -1: hlt - jmp 1b + cli +1: hlt + jmp 1b diff --git a/src/xtoa.c b/src/xtoa.c index 68c9520..2a7b9a5 100644 --- a/src/xtoa.c +++ b/src/xtoa.c @@ -1,100 +1,104 @@ #include "xtoa.h" -char* itoa(int value, int base) { - char* result; +char* itoa(int value, int base) +{ + char* result; - // check that the base if valid - if (base < 2 || base > 36) { *result = '\0'; return result; } + // check that the base if valid + if (base < 2 || base > 36) { *result = '\0'; return result; } - char* ptr = result, *ptr1 = result, tmp_char; - int tmp_value; + char* ptr = result, *ptr1 = result, tmp_char; + int tmp_value; - do { - tmp_value = value; - value /= base; - *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; - } while ( value ); - - // Apply negative sign - if (tmp_value < 0) *ptr++ = '-'; - *ptr-- = '\0'; - while(ptr1 < ptr) { - tmp_char = *ptr; - *ptr--= *ptr1; - *ptr1++ = tmp_char; - } - return result; + do { + tmp_value = value; + value /= base; + *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; + } while ( value ); + + // Apply negative sign + if (tmp_value < 0) *ptr++ = '-'; + *ptr-- = '\0'; + while (ptr1 < ptr) { + tmp_char = *ptr; + *ptr--= *ptr1; + *ptr1++ = tmp_char; + } + return result; } -char* uitoa(unsigned int value, int base) { - char* result; - // check that the base if valid - if (base < 2 || base > 36) { *result = '\0'; return result; } +char* uitoa(unsigned int value, int base) +{ + char* result; - char* ptr = result, *ptr1 = result, tmp_char; - int tmp_value; + // check that the base if valid + if (base < 2 || base > 36) { *result = '\0'; return result; } - do { - tmp_value = value; - value /= base; - *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; - } while ( value ); + char* ptr = result, *ptr1 = result, tmp_char; + int tmp_value; + do { + tmp_value = value; + value /= base; + *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; + } while ( value ); - *ptr-- = '\0'; - while(ptr1 < ptr) { - tmp_char = *ptr; - *ptr--= *ptr1; - *ptr1++ = tmp_char; - } - return result; + *ptr-- = '\0'; + while (ptr1 < ptr) { + tmp_char = *ptr; + *ptr--= *ptr1; + *ptr1++ = tmp_char; + } + return result; } -char* ltoa(long value, int base) { - char* result; - // check that the base if valid - if (base < 2 || base > 36) { *result = '\0'; return result; } +char* ltoa(long value, int base) +{ + char* result; - char* ptr = result, *ptr1 = result, tmp_char; - int tmp_value; + // check that the base if valid + if (base < 2 || base > 36) { *result = '\0'; return result; } - do { - tmp_value = value; - value /= base; - *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; - } while ( value ); + char* ptr = result, *ptr1 = result, tmp_char; + int tmp_value; - // Apply negative sign - if (tmp_value < 0) *ptr++ = '-'; - *ptr-- = '\0'; - while(ptr1 < ptr) { - tmp_char = *ptr; - *ptr--= *ptr1; - *ptr1++ = tmp_char; - } - return result; + do { + tmp_value = value; + value /= base; + *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; + } while ( value ); + + // Apply negative sign + if (tmp_value < 0) *ptr++ = '-'; + *ptr-- = '\0'; + while (ptr1 < ptr) { + tmp_char = *ptr; + *ptr--= *ptr1; + *ptr1++ = tmp_char; + } + return result; } -char* ultoa(unsigned long value, int base) { - char* result; - // check that the base if valid - if (base < 2 || base > 36) { *result = '\0'; return result; } +char* ultoa(unsigned long value, int base) +{ + char* result; - char* ptr = result, *ptr1 = result, tmp_char; - int tmp_value; + // check that the base if valid + if (base < 2 || base > 36) { *result = '\0'; return result; } - do { - tmp_value = value; - value /= base; - *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; - } while ( value ); + char* ptr = result, *ptr1 = result, tmp_char; + int tmp_value; - // Apply negative sign - //if (tmp_value < 0) *ptr++ = '-'; - *ptr-- = '\0'; - while(ptr1 < ptr) { - tmp_char = *ptr; - *ptr--= *ptr1; - *ptr1++ = tmp_char; - } - return result; + do { + tmp_value = value; + value /= base; + *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; + } while ( value ); + + *ptr-- = '\0'; + while (ptr1 < ptr) { + tmp_char = *ptr; + *ptr--= *ptr1; + *ptr1++ = tmp_char; + } + return result; } diff --git a/src/xtoa.h b/src/xtoa.h index 710c774..9ac39ee 100644 --- a/src/xtoa.h +++ b/src/xtoa.h @@ -1,9 +1,9 @@ #ifndef HEADER_XTOA #define HEADER_XTOA -char* itoa(int value, int base); -char* uitoa(unsigned int value, int base); -char* ltoa(long value, int base); -char* ultoa(unsigned long value, int base); +char *itoa(int value, int base); +char *uitoa(unsigned int value, int base); +char *ltoa(long value, int base); +char *ultoa(unsigned long value, int base); #endif