CLOSE
// Custom stdint.h header

#ifndef _STDINT_H
#define _STDINT_H

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;

typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long long int64_t;

#endif /* _STDINT_H */
// kprintf implementation using the custom stdint.h header

#include "stdint.h"
#include <stdarg.h>

#define VGA_BUFFER_ADDR 0xB8000
#define VGA_WIDTH 80
#define VGA_HEIGHT 25

// Define your own va_list and va_arg macros
typedef char* va_list;
#define va_start(ap, arg) (ap = (va_list)&arg)
#define va_arg(ap, type) (*(type*)(ap += sizeof(type)))
#define va_end(ap) (ap = (va_list)0)

// Function to calculate the VGA text mode buffer index from row and column
static inline size_t vga_buffer_index(size_t row, size_t col) {
    return (row * VGA_WIDTH + col) * 2;
}

// Write a single character to the VGA text mode buffer
static inline void vga_buffer_write_char(char c, size_t row, size_t col, uint8_t color) {
    uint16_t* buffer = (uint16_t*)VGA_BUFFER_ADDR;
    size_t index = vga_buffer_index(row, col);
    buffer[index] = (uint16_t)c | ((uint16_t)color << 8);
}

// Function to print a formatted string to the VGA text mode buffer
void kprintf(const char* format, ...) {
    va_list args;
    va_start(args, format);

    size_t row = 0;
    size_t col = 0;

    while (*format != '\0') {
        if (*format == '\n') {
            // Move to the next line
            col = 0;
            row++;
        } else if (*format == '\t') {
            // Tab space
            col += 4;
        } else if (*format == '%') {
            // Handle format specifiers
            format++; // Move to the next character after '%'
            switch (*format) {
                case 'd': {
                    // Print integer
                    int num = va_arg(args, int);
                    // Convert integer to string and print
                    // (implementation of integer to string conversion is omitted for brevity)
                    break;
                }
                case 's': {
                    // Print string
                    char* str = va_arg(args, char*);
                    while (*str != '\0') {
                        vga_buffer_write_char(*str, row, col, 0x0F); // White text on black background
                        col++;
                        str++;
                    }
                    break;
                }
                // Add more format specifiers as needed
            }
        } else {
            // Regular character
            vga_buffer_write_char(*format, row, col, 0x0F); // White text on black background
            col++;
        }

        // Check if reached end of line
        if (col >= VGA_WIDTH) {
            col = 0;
            row++;
        }

        // Check if reached end of screen
        if (row >= VGA_HEIGHT) {
            // Scroll up
            // (implementation of scrolling is omitted for brevity)
            break;
        }

        format++;
    }

    va_end(args);
}

// Example usage
void kernel_main(void) {
    kprintf("Hello, world!\n");
    kprintf("This is a test.\n");
    kprintf("Printing integer: %d\n", 42);
    kprintf("Printing string: %s\n", "Hello from kprintf!");
}