Trying to somewhat move to 80char line limit and tabs as spaces

This commit is contained in:
Jarkko Toivanen 2023-09-22 18:17:00 +03:00
parent 5e7356f58e
commit d474de8228
Signed by: jt
GPG key ID: 9151B109B73ECAD5
8 changed files with 303 additions and 252 deletions

1
.gitignore vendored
View file

@ -1,5 +1,6 @@
*.elf *.elf
*.o *.o
*.img *.img
*.bin
mnt/ mnt/
build/ build/

View file

@ -6,26 +6,43 @@ static unsigned short fb_width, fb_height;
static unsigned char fb_bpp, fb_bytespp; static unsigned char fb_bpp, fb_bytespp;
static unsigned char fb_rpos, fb_bpos, fb_gpos; 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) { void initfb(
fb_address = addr; unsigned long addr,
fb_pitch = pitch; unsigned short w,
fb_width = w; unsigned short h,
fb_height = h; unsigned char bpp,
fb_bpp = bpp; unsigned long pitch,
fb_bytespp = bpp/8; unsigned char rpos,
fb_rpos = rpos; unsigned char gpos,
fb_gpos = gpos; unsigned char bpos
fb_bpos = 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) { void putpixel(
if (x>fb_width || y> fb_height) return; unsigned short x,
unsigned short y,
if (fb_bpp == 32) { unsigned char r,
*((unsigned long *)(fb_address + y*fb_pitch + x*fb_bytespp)) = r<<fb_rpos | g<<fb_gpos | b<<fb_bpos; unsigned char g,
} else { unsigned char b,
*((unsigned char *)(fb_address + y*fb_pitch + x*fb_bytespp + fb_rpos/8)) = r; unsigned char a
*((unsigned char *)(fb_address + y*fb_pitch + x*fb_bytespp + fb_gpos/8)) = g; ){
*((unsigned char *)(fb_address + y*fb_pitch + x*fb_bytespp + fb_bpos/8)) = b; if (x > 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<<fb_rpos | g<<fb_gpos | b<<fb_bpos;
} else {
*((unsigned char *)(dest_addr + fb_rpos/8)) = r;
*((unsigned char *)(dest_addr + fb_gpos/8)) = g;
*((unsigned char *)(dest_addr + fb_bpos/8)) = b;
}
} }

View file

@ -1,7 +1,23 @@
#ifndef _FRAMEBUFFER_H #ifndef _FRAMEBUFFER_H
#define _FRAMEBUFFER_H 1 #define _FRAMEBUFFER_H 1
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); void initfb(
void putpixel(unsigned short x, unsigned short y, unsigned char r, unsigned char g, unsigned char b, unsigned char a); unsigned long addr,
unsigned short w,
unsigned short h,
unsigned char bpp,
unsigned long pitch,
unsigned char rpos,
unsigned char gpos,
unsigned char bpos
);
void putpixel(
unsigned short x,
unsigned short y,
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a
);
#endif #endif

View file

@ -4,93 +4,100 @@
#include "serial.h" #include "serial.h"
#include "xtoa.h" #include "xtoa.h"
static inline void outb(unsigned short port, unsigned char val) { static inline void outb(unsigned short port, unsigned char val)
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port) : "memory"); {
asm volatile("outb %0, %1" : : "a"(val), "Nd"(port) : "memory");
} }
static inline unsigned char inb(unsigned short port) { static inline unsigned char inb(unsigned short port)
unsigned char ret; {
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory"); unsigned char ret;
return ret; asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory");
return ret;
} }
void kmain (unsigned int mbootmagick, multiboot_info_t* mbootinfo) { void kmain (unsigned int mbootmagick, multiboot_info_t* mbootinfo)
{
// Cursor disabling
outb(0x3D4, 0x0A);
outb(0x3D5, 0x20);
// Cursor disabling int serial_initialized = serial_init();
outb(0x3D4, 0x0A); serial_write_string("\n=== rOSka ===\n");
outb(0x3D5, 0x20);
int serial_initialized = serial_init(); // Check multiboot header
serial_write_string("\n=== rOSka ===\n"); if (mbootmagick != MULTIBOOT_BOOTLOADER_MAGIC) {
return;
}
// Check multiboot header serial_write_string("\nMultiboot flags: ");
if (mbootmagick != MULTIBOOT_BOOTLOADER_MAGIC) { serial_write_string(itoa(mbootinfo->flags, 2));
return;
}
serial_write_string("\nMultiboot flags: "); // Check videomode
serial_write_string(itoa(mbootinfo->flags, 2)); 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_FRAMEBUFFER_INFO)) {
if (mbootinfo->flags & MULTIBOOT_INFO_VBE_INFO) { serial_write_string("\nVideo info not available");
serial_write_string("\nVBE Mode: 0x"); return;
serial_write_string(itoa(mbootinfo->vbe_mode, 16)); }
}
if (!(mbootinfo->flags & MULTIBOOT_INFO_FRAMEBUFFER_INFO)) { serial_write_string("\nFramebuffer: ");
serial_write_string("\nVideo info not available"); serial_write_string("\n Address: 0x");
return; serial_write_string(ultoa(mbootinfo->framebuffer_addr, 16));
}
serial_write_string("\nFramebuffer: "); serial_write_string("\n Dimensions: ");
serial_write_string("\n Address: 0x"); serial_write_string(itoa(mbootinfo->framebuffer_width, 10));
serial_write_string(ultoa(mbootinfo->framebuffer_addr, 16)); 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("\nMemory: ");
serial_write_string(itoa(mbootinfo->framebuffer_width, 10)); serial_write_string("\n lower: ");
serial_write_string("X"); serial_write_string(uitoa(mbootinfo->mem_lower, 10));
serial_write_string(itoa(mbootinfo->framebuffer_height, 10)); serial_write_string(" k");
serial_write_string("x"); serial_write_string("\n upper: ");
serial_write_string(itoa(mbootinfo->framebuffer_bpp, 10)); serial_write_string(uitoa(mbootinfo->mem_upper/1024, 10));
serial_write_string("\n Pitch: "); serial_write_string(" M");
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: "); initfb(
serial_write_string("\n lower: "); mbootinfo->framebuffer_addr,
serial_write_string(uitoa(mbootinfo->mem_lower, 10)); mbootinfo->framebuffer_width,
serial_write_string(" k"); mbootinfo->framebuffer_height,
serial_write_string("\n upper: "); mbootinfo->framebuffer_bpp,
serial_write_string(uitoa(mbootinfo->mem_upper/1024, 10)); mbootinfo->framebuffer_pitch,
serial_write_string(" M"); 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); serial_write_string("\nExecution finished, halting...\n");
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");
} }

View file

@ -1,50 +1,56 @@
#include "serial.h" #include "serial.h"
#define PORT 0x3f8 // COM1 #define PORT 0x3f8 // COM1
static inline void outb(unsigned short port, unsigned char val) { static inline void outb(unsigned short port, unsigned char val)
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port) : "memory"); {
asm volatile("outb %0, %1" : : "a"(val), "Nd"(port) : "memory");
} }
static inline unsigned char inb(unsigned short port) { static inline unsigned char inb(unsigned short port)
unsigned char ret; {
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory"); unsigned char ret;
return ret; asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory");
return ret;
} }
int serial_init() { int serial_init()
outb(PORT + 1, 0x00); // Disable all interrupts {
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) outb(PORT + 1, 0x00); // Disable all interrupts
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(PORT + 1, 0x00); // (hi byte) outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit outb(PORT + 1, 0x00); // (hi byte)
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set outb(PORT + 2, 0xC7); // Enable FIFO, clear them, 14-byte threshold
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
// returns same byte) 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) // Check if serial is faulty (i.e: not same byte as sent)
if (inb(PORT + 0) != 0xAE) { if (inb(PORT + 0) != 0xAE)
return 1; return 1;
}
// If serial is not faulty set it in normal operation mode // If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
outb(PORT + 4, 0x0F); outb(PORT + 4, 0x0F);
return 0; return 0;
} }
static int serial_is_transmit_empty() { return inb(PORT + 5) & 0x20; } static int serial_is_transmit_empty()
{
static void serial_write_char(char chr) { return inb(PORT + 5) & 0x20;
while (serial_is_transmit_empty() == 0);
outb(PORT, chr);
} }
void serial_write_string(const char* text) { static void serial_write_char(char chr)
int i = 0; {
while(text[i]) { while (serial_is_transmit_empty() == 0);
serial_write_char(text[i]); outb(PORT, chr);
i++; }
}
void serial_write_string(const char* text)
{
int i = 0;
while (text[i]) {
serial_write_char(text[i]);
i++;
}
} }

View file

@ -1,48 +1,48 @@
.set MULTIBOOT_PAGE_ALIGN, 1 << 0 .set MULTIBOOT_PAGE_ALIGN, 1 << 0
.set MULTIBOOT_MEMORY_INFO, 1 << 1 .set MULTIBOOT_MEMORY_INFO, 1 << 1
.set MULTIBOOT_VIDEO_REQUEST, 1 << 2 .set MULTIBOOT_VIDEO_REQUEST, 1 << 2
.set MULTIBOOT_AOUT_KLUDGE, 0 << 16 .set MULTIBOOT_AOUT_KLUDGE, 0 << 16
.set MULTIBOOT_HEADER_MAGIC, 0x1BADB002 .set MULTIBOOT_HEADER_MAGIC, 0x1BADB002
.set MULTIBOOT_HEADER_FLAGS, \ .set MULTIBOOT_HEADER_FLAGS, \
MULTIBOOT_PAGE_ALIGN |\ MULTIBOOT_PAGE_ALIGN \
MULTIBOOT_MEMORY_INFO |\ | MULTIBOOT_MEMORY_INFO \
MULTIBOOT_VIDEO_REQUEST |\ | MULTIBOOT_VIDEO_REQUEST \
MULTIBOOT_AOUT_KLUDGE | MULTIBOOT_AOUT_KLUDGE
.set MULTIBOOT_CHECKSUM, -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) .set MULTIBOOT_CHECKSUM, -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
#.section ".multiboottbabeee" #.section ".multiboottbabeee"
.align 4 .align 4
.long MULTIBOOT_HEADER_MAGIC .long MULTIBOOT_HEADER_MAGIC
.long MULTIBOOT_HEADER_FLAGS .long MULTIBOOT_HEADER_FLAGS
.long MULTIBOOT_CHECKSUM .long MULTIBOOT_CHECKSUM
.long 0 # header address .long 0 # header address
.long 0 # load address .long 0 # load address
.long 0 # load end address .long 0 # load end address
.long 0 # bss end address .long 0 # bss end address
.long 0 # entry address .long 0 # entry address
.long 0 # video mode_type (0:fb, 1:txt) (set flags[2]!) .long 0 # video mode_type (0:fb, 1:txt) (set flags[2]!)
.long 640 # video width .long 1024 # video width
.long 480 # video height .long 768 # video height
.long 32 # video depth .long 32 # video depth
.bss .bss
.align 16 .align 16
stack_bottom: stack_bottom:
.skip 16384 # 16k .skip 16384 # 16k
stack_top: stack_top:
.text .text
.global _start .global _start
_start: _start:
# Setup stack # Setup stack
mov $stack_top, %esp mov $stack_top, %esp
# Call the main kernel function. # Call the main kernel function.
push %ebx push %ebx
push %eax push %eax
call kmain call kmain
cli cli
1: hlt 1: hlt
jmp 1b jmp 1b

View file

@ -1,100 +1,104 @@
#include "xtoa.h" #include "xtoa.h"
char* itoa(int value, int base) { char* itoa(int value, int base)
char* result; {
char* result;
// check that the base if valid // check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; } if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char; char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value; int tmp_value;
do { do {
tmp_value = value; tmp_value = value;
value /= base; value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value ); } while ( value );
// Apply negative sign // Apply negative sign
if (tmp_value < 0) *ptr++ = '-'; if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0'; *ptr-- = '\0';
while(ptr1 < ptr) { while (ptr1 < ptr) {
tmp_char = *ptr; tmp_char = *ptr;
*ptr--= *ptr1; *ptr--= *ptr1;
*ptr1++ = tmp_char; *ptr1++ = tmp_char;
} }
return result; return result;
} }
char* uitoa(unsigned int value, int base) {
char* result;
// check that the base if valid char* uitoa(unsigned int value, int base)
if (base < 2 || base > 36) { *result = '\0'; return result; } {
char* result;
char* ptr = result, *ptr1 = result, tmp_char; // check that the base if valid
int tmp_value; if (base < 2 || base > 36) { *result = '\0'; return result; }
do { char* ptr = result, *ptr1 = result, tmp_char;
tmp_value = value; int tmp_value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
*ptr-- = '\0'; *ptr-- = '\0';
while(ptr1 < ptr) { while (ptr1 < ptr) {
tmp_char = *ptr; tmp_char = *ptr;
*ptr--= *ptr1; *ptr--= *ptr1;
*ptr1++ = tmp_char; *ptr1++ = tmp_char;
} }
return result; return result;
} }
char* ltoa(long value, int base) {
char* result;
// check that the base if valid char* ltoa(long value, int base)
if (base < 2 || base > 36) { *result = '\0'; return result; } {
char* result;
char* ptr = result, *ptr1 = result, tmp_char; // check that the base if valid
int tmp_value; if (base < 2 || base > 36) { *result = '\0'; return result; }
do { char* ptr = result, *ptr1 = result, tmp_char;
tmp_value = value; int tmp_value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign do {
if (tmp_value < 0) *ptr++ = '-'; tmp_value = value;
*ptr-- = '\0'; value /= base;
while(ptr1 < ptr) { *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
tmp_char = *ptr; } while ( value );
*ptr--= *ptr1;
*ptr1++ = tmp_char; // Apply negative sign
} if (tmp_value < 0) *ptr++ = '-';
return result; *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 char* ultoa(unsigned long value, int base)
if (base < 2 || base > 36) { *result = '\0'; return result; } {
char* result;
char* ptr = result, *ptr1 = result, tmp_char; // check that the base if valid
int tmp_value; if (base < 2 || base > 36) { *result = '\0'; return result; }
do { char* ptr = result, *ptr1 = result, tmp_char;
tmp_value = value; int tmp_value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign do {
//if (tmp_value < 0) *ptr++ = '-'; tmp_value = value;
*ptr-- = '\0'; value /= base;
while(ptr1 < ptr) { *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
tmp_char = *ptr; } while ( value );
*ptr--= *ptr1;
*ptr1++ = tmp_char; *ptr-- = '\0';
} while (ptr1 < ptr) {
return result; tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
} }

View file

@ -1,9 +1,9 @@
#ifndef HEADER_XTOA #ifndef HEADER_XTOA
#define HEADER_XTOA #define HEADER_XTOA
char* itoa(int value, int base); char *itoa(int value, int base);
char* uitoa(unsigned int value, int base); char *uitoa(unsigned int value, int base);
char* ltoa(long value, int base); char *ltoa(long value, int base);
char* ultoa(unsigned long value, int base); char *ultoa(unsigned long value, int base);
#endif #endif