From e43326a2c0384eb17b6d31f706826a1e71e2f345 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Tue, 24 Dec 2013 14:19:42 +0100 Subject: [PATCH] Add support for displaying signed numbers --- kernel/include/console.hpp | 5 ++++ kernel/include/stl/types.hpp | 13 +++++++-- kernel/src/console.cpp | 53 +++++++++++++++++++++++++++++++++++- kernel/src/e820.cpp | 2 +- kernel/src/fat32.cpp | 7 ++--- kernel/src/interrupts.cpp | 4 +-- kernel/src/memory.cpp | 16 +++++------ kernel/src/shell.cpp | 16 +++++------ kernel/src/sysinfo.cpp | 24 ++++++++-------- 9 files changed, 100 insertions(+), 40 deletions(-) diff --git a/kernel/include/console.hpp b/kernel/include/console.hpp index f49b9443..8e701998 100644 --- a/kernel/include/console.hpp +++ b/kernel/include/console.hpp @@ -30,6 +30,11 @@ void k_print(uint16_t number); void k_print(uint32_t number); void k_print(uint64_t number); +void k_print(int8_t number); +void k_print(int16_t number); +void k_print(int32_t number); +void k_print(int64_t number); + void k_printf(const char* fmt, ...); template diff --git a/kernel/include/stl/types.hpp b/kernel/include/stl/types.hpp index fb3b37de..5cf1047d 100644 --- a/kernel/include/stl/types.hpp +++ b/kernel/include/stl/types.hpp @@ -11,19 +11,26 @@ typedef unsigned int uint8_t __attribute__((__mode__(__QI__))); typedef unsigned int uint16_t __attribute__ ((__mode__ (__HI__))); typedef unsigned int uint32_t __attribute__ ((__mode__ (__SI__))); -typedef unsigned int uint32_t __attribute__ ((__mode__ (__SI__))); typedef unsigned int uint64_t __attribute__ ((__mode__ (__DI__))); -typedef unsigned int int16_t __attribute__ ((__mode__ (__HI__))); +typedef int int8_t __attribute__((__mode__(__QI__))); +typedef int int16_t __attribute__ ((__mode__ (__HI__))); +typedef int int32_t __attribute__ ((__mode__ (__SI__))); +typedef int int64_t __attribute__ ((__mode__ (__DI__))); typedef uint64_t uintptr_t; typedef uint64_t size_t; static_assert(sizeof(uint8_t) == 1, "uint8_t must be 1 byte long"); static_assert(sizeof(uint16_t) == 2, "uint16_t must be 2 bytes long"); -static_assert(sizeof(int16_t) == 2, "int16_t must be 2 bytes long"); static_assert(sizeof(uint32_t) == 4, "uint32_t must be 4 bytes long"); static_assert(sizeof(uint64_t) == 8, "uint64_t must be 8 bytes long"); + +static_assert(sizeof(int8_t) == 1, "int8_t must be 1 byte long"); +static_assert(sizeof(int16_t) == 2, "int16_t must be 2 bytes long"); +static_assert(sizeof(int32_t) == 4, "int32_t must be 4 bytes long"); +static_assert(sizeof(int64_t) == 8, "int64_t must be 8 bytes long"); + static_assert(sizeof(size_t) == 8, "size_t must be 8 bytes long"); static_assert(sizeof(uintptr_t) == sizeof(uint64_t*), "uintptr_t must have the same size as a pointer"); diff --git a/kernel/src/console.cpp b/kernel/src/console.cpp index 56f8915a..310f79b8 100644 --- a/kernel/src/console.cpp +++ b/kernel/src/console.cpp @@ -84,6 +84,16 @@ void print_unsigned(D number){ } } +template +void print_signed(D number){ + if(number < 0){ + k_print('-'); + print_unsigned(static_cast(-1 * number)); + } else { + print_unsigned(static_cast(number)); + } +} + } //end of anonymous namespace void set_column(long column){ @@ -118,6 +128,22 @@ void k_print(uint64_t number){ print_unsigned<20>(number); } +void k_print(int8_t number){ + print_signed<3,uint8_t>(number); +} + +void k_print(int16_t number){ + print_signed<5,uint16_t>(number); +} + +void k_print(int32_t number){ + print_signed<10,uint32_t>(number); +} + +void k_print(int64_t number){ + print_signed<20,uint64_t,int64_t>(number); +} + void next_line(){ ++current_line; @@ -223,8 +249,33 @@ void k_printf(const char* fmt, ...){ auto prev = current_column; - //Unsigned Decimal + //Signed decimal if(ch == 'd'){ + auto arg = va_arg(va, int64_t); + + if(min_digits > 0){ + size_t d = digits(arg); + if(min_digits > d){ + min_digits -= d; + + if(arg < 0){ + arg *= -1; + k_print('-'); + } + + while(min_digits > 0){ + while(min_digits > 0){ + k_print('0'); + --min_digits; + } + } + } + } + + k_print(arg); + } + //Unsigned Decimal + else if(ch == 'u'){ auto arg = va_arg(va, uint64_t); if(min_digits > 0){ diff --git a/kernel/src/e820.cpp b/kernel/src/e820.cpp index 505bbe70..5ae2e17f 100644 --- a/kernel/src/e820.cpp +++ b/kernel/src/e820.cpp @@ -20,7 +20,7 @@ size_t _available_memory; void e820::finalize_memory_detection(){ if(bios_e820_entry_count > 0){ - for(uint64_t i = 0; i < bios_e820_entry_count; ++i){ + for(int64_t i = 0; i < bios_e820_entry_count; ++i){ auto& bios_entry = bios_e820_entries[i]; auto& os_entry = e820_mmap[i]; diff --git a/kernel/src/fat32.cpp b/kernel/src/fat32.cpp index fa982042..7c2472a8 100644 --- a/kernel/src/fat32.cpp +++ b/kernel/src/fat32.cpp @@ -454,8 +454,8 @@ bool fat32::mkdir(dd disk, const disks::partition_descriptor& partition, const s std::unique_heap_array directory_cluster(16 * fat_bs->sectors_per_cluster); if(read_sectors(disk, cluster_lba(cluster_number.second), fat_bs->sectors_per_cluster, directory_cluster.get())){ - auto end = -1; - auto free = -1; + int64_t end = -1; + int64_t free = -1; for(size_t i = 0; i < directory_cluster.size(); ++i){ auto& entry = directory_cluster[i]; @@ -469,9 +469,6 @@ bool fat32::mkdir(dd disk, const disks::partition_descriptor& partition, const s break; } } - - k_printf("end=%d\n", end); - k_printf("free=%d\n", free); } return false; diff --git a/kernel/src/interrupts.cpp b/kernel/src/interrupts.cpp index 013d9953..3ab53943 100644 --- a/kernel/src/interrupts.cpp +++ b/kernel/src/interrupts.cpp @@ -74,8 +74,8 @@ struct regs { extern "C" { void _fault_handler(regs regs){ - k_printf("Exception (%d) occured\n", regs.error_no); - k_printf("error_code=%d\n", regs.error_code); + k_printf("Exception (%u) occured\n", regs.error_no); + k_printf("error_code=%u\n", regs.error_code); k_printf("rip=%h\n", regs.rip); k_printf("rflags=%h\n", regs.rflags); k_printf("cs=%h\n", regs.cs); diff --git a/kernel/src/memory.cpp b/kernel/src/memory.cpp index 47640eb1..46be12b9 100644 --- a/kernel/src/memory.cpp +++ b/kernel/src/memory.cpp @@ -150,7 +150,7 @@ void debug_malloc(const char* point = nullptr){ k_print(" next: "); do { - k_printf("%d%h -> ", static_cast(it->is_free()), reinterpret_cast(it)); + k_printf("%u%h -> ", static_cast(it->is_free()), reinterpret_cast(it)); it = it->next(); } while(it != malloc_head); @@ -296,7 +296,7 @@ void* k_malloc(uint64_t bytes){ reinterpret_cast(current) + sizeof(malloc_header_chunk)); if(TRACE_MALLOC){ - k_printf("m %d(%d) %h ", bytes, current->size(), reinterpret_cast(b)); + k_printf("m %u(%u) %h ", bytes, current->size(), reinterpret_cast(b)); } return b; @@ -372,7 +372,7 @@ void k_free(void* block){ } if(TRACE_MALLOC){ - k_printf("f %d %h ", free_header->size(), reinterpret_cast(block)); + k_printf("f %u %h ", free_header->size(), reinterpret_cast(block)); } //Less memory is used @@ -406,7 +406,7 @@ void memory_debug(){ auto it = malloc_head; - k_printf("malloc overhead: %d\n", META_SIZE); + k_printf("malloc overhead: %u\n", META_SIZE); k_print("Free blocks:"); do { if(!it->is_free()){ @@ -418,14 +418,14 @@ void memory_debug(){ ++inconsistent; } - k_printf("b(%d) ", it->size()); + k_printf("b(%u) ", it->size()); memory_free += it->size(); it = it->next(); } while(it != malloc_head); k_print_line(); - k_printf("memory free in malloc chain: %m (%d)\n", memory_free, memory_free); - k_printf("There are %d non free blocks in the free list\n", non_free_blocks); - k_printf("There are %d inconsistent sized blocks in the free list\n", inconsistent); + k_printf("memory free in malloc chain: %m (%u)\n", memory_free, memory_free); + k_printf("There are %u non free blocks in the free list\n", non_free_blocks); + k_printf("There are %u inconsistent sized blocks in the free list\n", inconsistent); } diff --git a/kernel/src/shell.cpp b/kernel/src/shell.cpp index 08aa5330..43205522 100644 --- a/kernel/src/shell.cpp +++ b/kernel/src/shell.cpp @@ -228,7 +228,7 @@ void help_command(const std::vector&){ } void uptime_command(const std::vector&){ - k_printf("Uptime: %ds\n", timer_seconds()); + k_printf("Uptime: %us\n", timer_seconds()); } #define CURRENT_YEAR 2013 @@ -320,7 +320,7 @@ void date_command(const std::vector&){ year += 100; } - k_printf("%d.%d.%d %d:%.2d:%.2d\n", day, month, year, hour, minute, second); + k_printf("%u.%u.%u %u:%.2d:%.2d\n", day, month, year, hour, minute, second); } void sleep_command(const std::vector& params){ @@ -339,7 +339,7 @@ void mmap_command(const std::vector&){ if(e820::mmap_failed()){ k_print_line("The mmap was not correctly loaded from e820"); } else { - k_printf("There are %d mmap entry\n", e820::mmap_entry_count()); + k_printf("There are %u mmap entry\n", e820::mmap_entry_count()); k_print_line("Base End Size Type"); for(uint64_t i = 0; i < e820::mmap_entry_count(); ++i){ @@ -386,13 +386,13 @@ void partitions_command(const std::vector& params){ k_print_line("UUID Type Start Sectors"); for(auto& partition : partitions){ - k_printf("%10d %12s %10d %d\n", partition.uuid, + k_printf("%10d %12s %10d %u\n", partition.uuid, disks::partition_type_to_string(partition.type), partition.start, partition.sectors); } } } else { - k_printf("Disks %d does not exist\n", uuid); + k_printf("Disks %u does not exist\n", uuid); } } @@ -402,7 +402,7 @@ void mount_command(const std::vector& params){ auto mp = disks::mounted_partition(); if(md && mp){ - k_printf("%d:%d is mounted\n", md->uuid, mp->uuid); + k_printf("%u:%u is mounted\n", md->uuid, mp->uuid); } else { k_print_line("Nothing is mounted"); } @@ -415,10 +415,10 @@ void mount_command(const std::vector& params){ if(disks::partition_exists(disk, partition_uuid)){ disks::mount(disk, partition_uuid); } else { - k_printf("Partition %d does not exist\n", partition_uuid); + k_printf("Partition %u does not exist\n", partition_uuid); } } else { - k_printf("Disk %d does not exist\n", disk_uuid); + k_printf("Disk %u does not exist\n", disk_uuid); } } } diff --git a/kernel/src/sysinfo.cpp b/kernel/src/sysinfo.cpp index e35539dd..e6e87e64 100644 --- a/kernel/src/sysinfo.cpp +++ b/kernel/src/sysinfo.cpp @@ -277,12 +277,12 @@ void get_deterministic_cache_parameters(){ k_print("Unified Cache: "); } - k_printf( "Level %d: ", (eax & 0xE0)/32 ); - k_printf( "Max Threads %d: ", ((eax & 0x03FFC000)/(8192))+1 ); - k_printf( "Max Procs. %d: " , ((eax & 0xFC000000)/(4*256*65536))+1 ); - k_printf( "Line Size = %d: ", (ebx & 0xFFF ) + 1 ); - k_printf( "Associativity = %d: ", ((ebx & 0xFFC00000)/4*16*65536) + 1 ); - k_printf( "Sets = %d:\n", ecx + 1 ); + k_printf( "Level %u: ", (eax & 0xE0)/32 ); + k_printf( "Max Threads %u: ", ((eax & 0x03FFC000)/(8192))+1 ); + k_printf( "Max Procs. %u: " , ((eax & 0xFC000000)/(4*256*65536))+1 ); + k_printf( "Line Size = %u: ", (ebx & 0xFFF ) + 1 ); + k_printf( "Associativity = %u: ", ((ebx & 0xFFC00000)/4*16*65536) + 1 ); + k_printf( "Sets = %u:\n", ecx + 1 ); ++caches; } @@ -331,12 +331,12 @@ void get_base_info(){ native_cpuid(1, &eax, &ebx, &ecx, &edx); - k_printf("Stepping: %d\n", eax & 0xF); - k_printf("Model: %d\n", (eax >> 4) & 0xF); - k_printf("Family: %d\n", (eax >> 8) & 0xF); - k_printf("Processor Type: %d\n", (eax >> 12) & 0x3); - k_printf("Extended Model: %d\n", (eax >> 16) & 0xF); - k_printf("Extended Family: %d\n", (eax >> 20) & 0xFF); + k_printf("Stepping: %u\n", eax & 0xF); + k_printf("Model: %u\n", (eax >> 4) & 0xF); + k_printf("Family: %u\n", (eax >> 8) & 0xF); + k_printf("Processor Type: %u\n", (eax >> 12) & 0x3); + k_printf("Extended Model: %u\n", (eax >> 16) & 0xF); + k_printf("Extended Family: %u\n", (eax >> 20) & 0xFF); } } //end of anonymous namespace