From 1327ae7724220ada29823b5b1111c7e8a579d08f Mon Sep 17 00:00:00 2001 From: xiaoyi1212 Date: Sun, 1 Sep 2024 16:43:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0libc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/include/ctype.h | 5 ++ apps/include/math.h | 18 ++++++ apps/include/stdio.h | 4 +- apps/include/stdlib.h | 2 +- apps/include/string.h | 3 +- apps/include/syscall.h | 3 +- apps/libs/Makefile | 2 +- apps/libs/math.c | 78 ++++++++++++++++++++++++++ apps/libs/string.c | 2 +- apps/shell/shell.c | 5 ++ src/driver/serial.c | 2 +- src/driver/vbe.c | 9 +-- src/fs/iso9660.c | 8 +-- src/include/common.h | 6 ++ src/include/cpp.h | 4 -- src/include/proc.h | 29 ---------- src/kernel/kernel.c | 1 - src/kernel/proc.c | 5 -- src/util/base.cpp | 55 ------------------ src/util/common.c | 123 +++++++++++++++++++++++++++++++++++++++++ 20 files changed, 246 insertions(+), 118 deletions(-) delete mode 100644 src/include/cpp.h delete mode 100644 src/include/proc.h delete mode 100644 src/kernel/proc.c delete mode 100644 src/util/base.cpp diff --git a/apps/include/ctype.h b/apps/include/ctype.h index d3283bf..3904346 100644 --- a/apps/include/ctype.h +++ b/apps/include/ctype.h @@ -1,6 +1,11 @@ #ifndef CRASHPOWEROS_CTYPE_H #define CRASHPOWEROS_CTYPE_H +#include +#include +#include +#include + int ispunct(char ch); char toupper(char ch); char tolower(char ch); diff --git a/apps/include/math.h b/apps/include/math.h index 44ae5fb..284dd43 100644 --- a/apps/include/math.h +++ b/apps/include/math.h @@ -4,6 +4,16 @@ #define MIN(i, j) (((i) < (j)) ? (i) : (j)) #define MAX(i, j) (((i) > (j)) ? (i) : (j)) +#define F32_EPSILON 1e-5f +#define F64_EPSILON 1e-10 + +#define PI 3.14159265358979323846264338327950288 +#define E 2.718281828459045235360287 + +#define SQRT2 1.41421356237309504880168872420969807 + +#define PHI 1.61803398874989484820458683436563811772030917980576 + #include void srandlevel(unsigned short randlevel_); @@ -15,5 +25,13 @@ double pow(double a,long long b); unsigned long long ull_pow(unsigned long long a,unsigned long long b); double sqrt(double x); float q_sqrt(float number); +double mod(double x, double y); +double sin(double x); +double cos(double x); +double tan(double x); +double asin(double x); +double acos(double x); +double atan(double x); +double atan2(double y, double x); #endif diff --git a/apps/include/stdio.h b/apps/include/stdio.h index f32a25c..6aa4a15 100644 --- a/apps/include/stdio.h +++ b/apps/include/stdio.h @@ -10,9 +10,7 @@ #define BIN 0x0 #define PLUS 0x10 -#include -#include -#include +#include "ctype.h" typedef struct FILE { unsigned int mode; diff --git a/apps/include/stdlib.h b/apps/include/stdlib.h index 30515aa..b84a2b8 100644 --- a/apps/include/stdlib.h +++ b/apps/include/stdlib.h @@ -1,7 +1,7 @@ #ifndef CRASHPOWEROS_STDLIB_H #define CRASHPOWEROS_STDLIB_H -#include +#include "ctype.h" long long atoi(const char* s); void *malloc(size_t size); diff --git a/apps/include/string.h b/apps/include/string.h index 6b16c16..0a027ff 100644 --- a/apps/include/string.h +++ b/apps/include/string.h @@ -1,8 +1,7 @@ #ifndef CRASHPOWEROS_STRING_H #define CRASHPOWEROS_STRING_H -#include -#include +#include "ctype.h" const char* memchr(const char* buf,char c,unsigned long long count); void *memmove(void *dest, const void *src, size_t num); diff --git a/apps/include/syscall.h b/apps/include/syscall.h index 324023e..18c4154 100644 --- a/apps/include/syscall.h +++ b/apps/include/syscall.h @@ -13,8 +13,7 @@ #define SYSCALL_VFS_READFILE 10 #define SYSCALL_VFS_WRITEFILE 11 -#include -#include +#include "ctype.h" void syscall_print(char* c); void syscall_putchar(char c); diff --git a/apps/libs/Makefile b/apps/libs/Makefile index 7c4fb38..4d7b040 100644 --- a/apps/libs/Makefile +++ b/apps/libs/Makefile @@ -1,4 +1,4 @@ -OBJS_PACK = out/syscall.obj out/print.obj out/string.obj out/libc.obj +OBJS_PACK = out/syscall.obj out/print.obj out/string.obj out/libc.obj out/math.obj default : $(OBJS_PACK) diff --git a/apps/libs/math.c b/apps/libs/math.c index fa9a8d2..60ae24e 100644 --- a/apps/libs/math.c +++ b/apps/libs/math.c @@ -1,4 +1,5 @@ #include "../include/math.h" +#include "../include/ctype.h" static unsigned long long rand_seed = 1 ; static unsigned short max_bit = 32 ; @@ -75,3 +76,80 @@ float q_sqrt(float number){ y = y * (f - (x * y * y)) ; return number * y ; } + +double mod(double x, double y){ + return x - (int32_t)(x / y) * y; +} + +double sin(double x){ + x = mod(x, 2 * PI); + double sum = x; + double term = x; + int n = 1; + bool sign = true; + while (term > F64_EPSILON || term < -F64_EPSILON) { + n += 2; + term *= x * x / (n * (n - 1)); + sum += sign ? -term : term; + sign = !sign; + } + return sum; +} + +double cos(double x){ + x = mod(x, 2 * PI); + double sum = 1; + double term = 1; + int n = 0; + bool sign = true; + while (term > F64_EPSILON || term < -F64_EPSILON) { + n += 2; + term *= x * x / (n * (n - 1)); + sum += sign ? -term : term; + sign = !sign; + } + return sum; +} + +double tan(double x) { + return sin(x) / cos(x); +} + +double asin(double x){ + double sum = x; + double term = x; + int n = 1; + while (term > F64_EPSILON || term < -F64_EPSILON) { + term *= (x * x * (2 * n - 1) * (2 * n - 1)) / (2 * n * (2 * n + 1)); + sum += term; + n++; + } + return sum; +} + +double acos(double x) { + return PI / 2 - asin(x); +} + +double atan(double x){ + double sum = x; + double term = x; + int n = 1; + bool sign = true; + while (term > F64_EPSILON || term < -F64_EPSILON) { + term *= x * x * (2 * n - 1) / (2 * n + 1); + sum += sign ? -term : term; + sign = !sign; + n++; + } + return sum; +} + +double atan2(double y, double x){ + if (x > 0) return atan(y / x); + if (x < 0 && y >= 0) return atan(y / x) + PI; + if (x < 0 && y < 0) return atan(y / x) - PI; + if (x == 0 && y > 0) return PI / 2; + if (x == 0 && y < 0) return -PI / 2; + return 0; +} diff --git a/apps/libs/string.c b/apps/libs/string.c index 3348da0..3df42b7 100644 --- a/apps/libs/string.c +++ b/apps/libs/string.c @@ -211,4 +211,4 @@ char *strdup(const char *str) { while ((*ret++ = *strat++) != '\0') {} return ret - (len + 1); -} \ No newline at end of file +} diff --git a/apps/shell/shell.c b/apps/shell/shell.c index d963b1d..e60a753 100644 --- a/apps/shell/shell.c +++ b/apps/shell/shell.c @@ -1,6 +1,7 @@ #include #include "../include/syscall.h" #include "../include/string.h" +#include "../include/math.h" static int gets(char *buf, int buf_size) { int index = 0; @@ -49,6 +50,10 @@ int main(){ char *argv[50]; int argc = -1; char *buffer[255]; + + double x = sin(12); + printf("%08f",x); + while (1){ syscall_get_cd(buffer); printf("%s$ ",buffer); diff --git a/src/driver/serial.c b/src/driver/serial.c index 53dbe5c..e1b90b9 100644 --- a/src/driver/serial.c +++ b/src/driver/serial.c @@ -32,7 +32,7 @@ char read_serial() { int is_transmit_empty() { return io_in8(SERIAL_PORT + 5) & 0x20; } void write_serial(char a) { - return; + //return; while (is_transmit_empty() == 0); io_out8(SERIAL_PORT, a); } diff --git a/src/driver/vbe.c b/src/driver/vbe.c index 3f3e18e..131410c 100644 --- a/src/driver/vbe.c +++ b/src/driver/vbe.c @@ -62,11 +62,6 @@ void vbe_scroll() { void vbe_draw_char(char c, int32_t x, int32_t y) { if (c == ' ') { - for (int i = 0; i < 16; i++) { - for (int j = 0; j < 9; j++) { - //screen[(y + i) * width + x + j] = back_color; - } - } return; } @@ -79,7 +74,7 @@ void vbe_draw_char(char c, int32_t x, int32_t y) { for (int j = 0; j < 9; j++) { if (font[i] & (0x80 >> j)) { screen[(y + i) * width + x + j] = color; - } //else screen[(y + i) * width + x + j] = back_color; + } } } } @@ -187,6 +182,8 @@ void initVBE(multiboot_t *info) { c_width = width / 9; c_height = height / 16; + logkf("SCREEN BASS: %08x\n",screen); + vbe_clear(); Bmp *bmp = (Bmp*) &logo_bmp; diff --git a/src/fs/iso9660.c b/src/fs/iso9660.c index 7c27ce3..b3b2067 100644 --- a/src/fs/iso9660.c +++ b/src/fs/iso9660.c @@ -288,13 +288,7 @@ l9660_status l9660_read(l9660_file *f, void *buf, size_t size, size_t *read) { return L9660_OK; } -char *strdup(const char *s) { - size_t l = strlen(s); - char *d = kmalloc(l + 1); - if (!d) - return NULL; - return memcpy(d, s, l + 1); -} + bool read_sector(l9660_fs *fs, void *buf, uint32_t sector) { return CDROM_Read(sector, 1, buf, fs->disk_number); } diff --git a/src/include/common.h b/src/include/common.h index 2578464..13c84c4 100644 --- a/src/include/common.h +++ b/src/include/common.h @@ -32,6 +32,10 @@ typedef int bool; #define false 0 #define EOF -1 +char* replaceAll(char* src, char* find, char* replaceWith); +char *strstr(char *str1, char *str2); +char *strncpy(char *dest, const char *src, unsigned long long count); +char *strdup(const char *str); unsigned int rand(void); void srand(unsigned long seed); void insert_char(char* str, int pos, char ch); @@ -59,6 +63,8 @@ int vsprintf(char *buf, const char *fmt, va_list args); int sprintf(char *buf, const char *fmt, ...); long int strtol(const char *str,char **endptr,int base); +int getFindStrCount(char* src, char* find); + void reset_kernel(); void shutdown_kernel(); uint32_t memory_all(); diff --git a/src/include/cpp.h b/src/include/cpp.h deleted file mode 100644 index 702803f..0000000 --- a/src/include/cpp.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef CRASHPOWEROS_CPP_H -#define CRASHPOWEROS_CPP_H - -#endif diff --git a/src/include/proc.h b/src/include/proc.h deleted file mode 100644 index 99b43af..0000000 --- a/src/include/proc.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef CRASHPOWEROS_PROC_H -#define CRASHPOWEROS_PROC_H - -#include "common.h" - -struct stackframe { - uint32_t gs; - uint32_t fs; - uint32_t es; - uint32_t ds; - uint32_t edi; - uint32_t esi; - uint32_t ebp; - uint32_t kernel_esp; - uint32_t ebx; - uint32_t edx; - uint32_t ecx; - uint32_t eax; - uint32_t retaddr; - uint32_t eip; - uint32_t cs; - uint32_t eflags; - uint32_t esp; - uint32_t ss; -}; - -void proc_install(); - -#endif diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 7a28cf0..f7987a3 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -76,7 +76,6 @@ void kernel_main(multiboot_t *multiboot) { initVBE(multiboot); - char* cmdline = multiboot->cmdline; if(cmdline != NULL){ printf("Multiboot command line: %s\n",cmdline); diff --git a/src/kernel/proc.c b/src/kernel/proc.c deleted file mode 100644 index ec2850e..0000000 --- a/src/kernel/proc.c +++ /dev/null @@ -1,5 +0,0 @@ -#include "../include/proc.h" - -void proc_install(){ - -} \ No newline at end of file diff --git a/src/util/base.cpp b/src/util/base.cpp deleted file mode 100644 index 3e5bbfa..0000000 --- a/src/util/base.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "../include/cpp.h" -#include -#include - -extern "C" uint32_t kmalloc(size_t); -extern "C" void kfree(void*); - -extern "C" void __cxa_pure_virtual() -{ - // Do nothing or print an error message. -} - -void *operator -new(size_t -size) -{ -return -(void*) kmalloc(size); -} - -void *operator -new[]( -size_t size -) -{ -return (void*)kmalloc(size); -} - -void operator - -delete(void *p, unsigned int size) { - kfree(p); -} - -void operator -delete[]( -void *p, -unsigned int size -) -{ -kfree(p); -} -void operator - -delete(void *p) { - kfree(p); -} - -void operator -delete[]( -void *p -) -{ -kfree(p); -} diff --git a/src/util/common.c b/src/util/common.c index 32d1202..baff90f 100644 --- a/src/util/common.c +++ b/src/util/common.c @@ -9,6 +9,42 @@ uint32_t ALIGN_F(const uint32_t addr, const uint32_t _align) { return (uint32_t)((addr + _align - 1) & (~(_align - 1))); } +char *strstr(char *str1, char *str2) { + if (str1 == 0 || str2 == 0)return 0; + const char *temp = 0; + const char *res = 0; + while (*str1 != '\0') { + temp = str1; + res = str2; + while (*temp == *res)++temp, ++res; + if (*res == '\0')return str1; + ++str1; + } + return 0; +} + +char *strncpy(char *dest, const char *src, unsigned long long count) { + if (dest == 0 || src == 0)return 0; + char *ret = dest; + while (count)*dest = *src, ++dest, ++src, --count; + return ret; +} + +char *strdup(const char *str) { + if (str == NULL) + return NULL; + + char *strat = (char *) str; + int len = 0; + while (*str++ != '\0') + len++; + char *ret = (char *) kmalloc(len + 1); + + while ((*ret++ = *strat++) != '\0') {} + + return ret - (len + 1); +} + void insert_char(char *str, int pos, char ch) { int i; for (i = strlen(str); i >= pos; i--) { @@ -503,3 +539,90 @@ void trim(char *s) { while (*p && isspace(*p)) ++p, --len; memmove(s, p, len + 1); } + +char* replaceAll(char* src, char* find, char* replaceWith){ + //如果find或者replace为null,则返回和src一样的字符串。 + if(find == NULL || replaceWith == NULL){ + return strdup(src); + } + //指向替换后的字符串的head。 + char* afterReplaceHead = NULL; + //总是指向新字符串的结尾位置。 + char* afterReplaceIndex = NULL; + //find字符串在src字符串中出现的次数 + int count = 0; + int i,j,k; + + int srcLen = strlen(src); + int findLen = strlen(find); + int replaceWithLen = strlen(replaceWith); + + //指向src字符串的某个位置,从该位置开始复制子字符串到afterReplaceIndex,初始从src的head开始复制。 + char* srcIndex = src; + //src字符串的某个下标,从该下标开始复制字符串到afterReplaceIndex,初始为src的第一个字符。 + int cpStrStart = 0; + + //获取find字符串在src字符串中出现的次数 + count = getFindStrCount(src, find); + //如果没有出现,则返回和src一样的字符串。 + if(count == 0){ + return strdup(src); + } + + //为新字符串申请内存 + afterReplaceHead = afterReplaceIndex = (char*)kmalloc(srcLen + 1 + (replaceWithLen - findLen) * count); + //初始化新字符串内存 + memset(afterReplaceHead, '\0',sizeof(afterReplaceHead)); + + for(i = 0,j = 0,k = 0;i!=srcLen;i++){ + //如果find字符串的字符和src中字符串的字符是否相同。 + if(src[i] == find[j]){ + //如果刚开始比较,则将i的值先赋给k保存。 + if(j == 0){ + k = i; + } + //如果find字符串包含在src字符串中 + if(j == (findLen-1)){ + j = 0; + //拷贝src中find字符串之前的字符串到新字符串中 + strncpy(afterReplaceIndex, srcIndex, i - findLen - cpStrStart + 1); + //修改afterReplaceIndex + afterReplaceIndex = afterReplaceIndex + i - findLen - cpStrStart + 1; + //修改srcIndex + srcIndex = srcIndex + i - findLen - cpStrStart + 1; + //cpStrStart + cpStrStart = i + 1; + + //拷贝replaceWith字符串到新字符串中 + strncpy(afterReplaceIndex, replaceWith, replaceWithLen); + //修改afterReplaceIndex + afterReplaceIndex = afterReplaceIndex + replaceWithLen; + //修改srcIndex + srcIndex = srcIndex + findLen; + }else{ + j++; + } + }else{ + //如果find和src比较过程中出现不相等的情况,则将保存的k值还给i + if(j != 0){ + i = k; + } + j = 0; + } + } + //最后将src中最后一个与find匹配的字符串后面的字符串复制到新字符串中。 + strncpy(afterReplaceIndex, srcIndex, i - cpStrStart); + + return afterReplaceHead; +} + +int getFindStrCount(char* src, char* find){ + int count = 0; + char* position =src; + int findLen = strlen(find); + while((position = strstr(position, find)) != NULL){ + count++; + position = position + findLen; + } + return count; +}