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,7 +6,16 @@ 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(
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_address = addr;
fb_pitch = pitch; fb_pitch = pitch;
fb_width = w; fb_width = w;
@ -18,14 +27,22 @@ void initfb(unsigned long addr, unsigned short w, unsigned short h, unsigned cha
fb_bpos = bpos; 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,
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a
){
if (x > fb_width || y > fb_height)
return;
unsigned long dest_addr = fb_address + y*fb_pitch + x*fb_bytespp;
if (fb_bpp == 32) { if (fb_bpp == 32) {
*((unsigned long *)(fb_address + y*fb_pitch + x*fb_bytespp)) = r<<fb_rpos | g<<fb_gpos | b<<fb_bpos; *((unsigned long *)(dest_addr)) = r<<fb_rpos | g<<fb_gpos | b<<fb_bpos;
} else { } else {
*((unsigned char *)(fb_address + y*fb_pitch + x*fb_bytespp + fb_rpos/8)) = r; *((unsigned char *)(dest_addr + fb_rpos/8)) = r;
*((unsigned char *)(fb_address + y*fb_pitch + x*fb_bytespp + fb_gpos/8)) = g; *((unsigned char *)(dest_addr + fb_gpos/8)) = g;
*((unsigned char *)(fb_address + y*fb_pitch + x*fb_bytespp + fb_bpos/8)) = b; *((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,17 +4,19 @@
#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; unsigned char ret;
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory"); asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory");
return ret; return ret;
} }
void kmain (unsigned int mbootmagick, multiboot_info_t* mbootinfo) { void kmain (unsigned int mbootmagick, multiboot_info_t* mbootinfo)
{
// Cursor disabling // Cursor disabling
outb(0x3D4, 0x0A); outb(0x3D4, 0x0A);
outb(0x3D5, 0x20); outb(0x3D5, 0x20);
@ -68,22 +70,27 @@ void kmain (unsigned int mbootmagick, multiboot_info_t* mbootinfo) {
serial_write_string(uitoa(mbootinfo->mem_upper/1024, 10)); serial_write_string(uitoa(mbootinfo->mem_upper/1024, 10));
serial_write_string(" M"); 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); 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; int x, y, i;
unsigned char c = 0; unsigned char c = 0;
for (;;) { for (;;) {
//serial_write_string("\n");
for (;c<255;c+=5) { for (;c<255;c+=5) {
//serial_write_string("/");
for(y=0; y < mbootinfo->framebuffer_height; y++) { for(y=0; y < mbootinfo->framebuffer_height; y++) {
for(x=0; x < mbootinfo->framebuffer_width; x++) { for(x=0; x < mbootinfo->framebuffer_width; x++) {
putpixel(x, y, 0, c, c, 0xff); putpixel(x, y, 0, c, c, 0xff);
} }
} }
} }
//serial_write_string("\n");
for (;c>0;c-=5) { for (;c>0;c-=5) {
//serial_write_string("\\");
for(y=mbootinfo->framebuffer_height; y>=0; y--) { for(y=mbootinfo->framebuffer_height; y>=0; y--) {
for(x=mbootinfo->framebuffer_width; x>=0; x--) { for(x=mbootinfo->framebuffer_width; x>=0; x--) {
putpixel(x, y, 0, c, c, 0xff); putpixel(x, y, 0, c, c, 0xff);

View file

@ -1,31 +1,33 @@
#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; unsigned char ret;
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory"); asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory");
return ret; return ret;
} }
int serial_init() { int serial_init()
{
outb(PORT + 1, 0x00); // Disable all interrupts outb(PORT + 1, 0x00); // Disable all interrupts
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(PORT + 1, 0x00); // (hi byte) outb(PORT + 1, 0x00); // (hi byte)
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit 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 + 2, 0xC7); // Enable FIFO, clear them, 14-byte threshold
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip 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 outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check
// returns same byte) // 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)
@ -33,18 +35,22 @@ int serial_init() {
return 0; return 0;
} }
static int serial_is_transmit_empty() { return inb(PORT + 5) & 0x20; } static int serial_is_transmit_empty()
{
return inb(PORT + 5) & 0x20;
}
static void serial_write_char(char chr) { static void serial_write_char(char chr)
{
while (serial_is_transmit_empty() == 0); while (serial_is_transmit_empty() == 0);
outb(PORT, chr); outb(PORT, chr);
} }
void serial_write_string(const char* text) { void serial_write_string(const char* text)
{
int i = 0; int i = 0;
while (text[i]) { while (text[i]) {
serial_write_char(text[i]); serial_write_char(text[i]);
i++; i++;
} }
} }

View file

@ -4,10 +4,10 @@
.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"
@ -21,8 +21,8 @@
.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

View file

@ -1,5 +1,6 @@
#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
@ -24,7 +25,9 @@ char* itoa(int value, int base) {
} }
return result; return result;
} }
char* uitoa(unsigned int value, int base) {
char* uitoa(unsigned int value, int base)
{
char* result; char* result;
// check that the base if valid // check that the base if valid
@ -39,7 +42,6 @@ char* uitoa(unsigned int value, int base) {
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value ); } while ( value );
*ptr-- = '\0'; *ptr-- = '\0';
while (ptr1 < ptr) { while (ptr1 < ptr) {
tmp_char = *ptr; tmp_char = *ptr;
@ -48,7 +50,9 @@ char* uitoa(unsigned int value, int base) {
} }
return result; return result;
} }
char* ltoa(long value, int base) {
char* ltoa(long value, int base)
{
char* result; char* result;
// check that the base if valid // check that the base if valid
@ -73,7 +77,9 @@ char* ltoa(long value, int base) {
} }
return result; return result;
} }
char* ultoa(unsigned long value, int base) {
char* ultoa(unsigned long value, int base)
{
char* result; char* result;
// check that the base if valid // check that the base if valid
@ -88,8 +94,6 @@ char* ultoa(unsigned long value, int base) {
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value ); } while ( value );
// Apply negative sign
//if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0'; *ptr-- = '\0';
while (ptr1 < ptr) { while (ptr1 < ptr) {
tmp_char = *ptr; tmp_char = *ptr;