diff options
author | Friedrich Beckmann <friedrich.beckmann@hs-augsburg.de> | 2022-07-25 17:55:39 +0200 |
---|---|---|
committer | Friedrich Beckmann <friedrich.beckmann@hs-augsburg.de> | 2022-07-25 17:55:39 +0200 |
commit | 3fff6023602822531efdae30bc8ebf862967f1ef (patch) | |
tree | 16028102b8d850f8ab3115d28a8539ca6bc5f51d /VexRiscvSocSoftware/projects/raw/dhrystone/src/stdlib.c |
Initial Commit
Diffstat (limited to 'VexRiscvSocSoftware/projects/raw/dhrystone/src/stdlib.c')
-rw-r--r-- | VexRiscvSocSoftware/projects/raw/dhrystone/src/stdlib.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/VexRiscvSocSoftware/projects/raw/dhrystone/src/stdlib.c b/VexRiscvSocSoftware/projects/raw/dhrystone/src/stdlib.c new file mode 100644 index 0000000..7ec3503 --- /dev/null +++ b/VexRiscvSocSoftware/projects/raw/dhrystone/src/stdlib.c @@ -0,0 +1,104 @@ +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. + +#include <stdarg.h> +#include <stdint.h> + + +#define TEST_COM_BASE ((volatile uint32_t*)(0xF00FFF00)) + +extern char *malloc(); +extern int printf(const char *format, ...); + +char heap_memory[1024]; +int heap_memory_used = 0; + +char *malloc(int size) +{ + char *p = heap_memory + heap_memory_used; + // printf("[malloc(%d) -> %d (%d..%d)]", size, (int)p, heap_memory_used, heap_memory_used + size); + heap_memory_used += size; + if (heap_memory_used > 1024) + asm volatile ("ebreak"); + return p; +} + +static void printf_c(int c) +{ + putchar(c); +} + +static void printf_s(char *p) +{ + while (*p) + putchar(*(p++)); +} + +static void printf_d(int val) +{ + char buffer[32]; + char *p = buffer; + if (val < 0) { + printf_c('-'); + val = -val; + } + while (val || p == buffer) { + *(p++) = '0' + val % 10; + val = val / 10; + } + while (p != buffer) + printf_c(*(--p)); +} + +int printf(const char *format, ...) +{ + int i; + va_list ap; + + va_start(ap, format); + + for (i = 0; format[i]; i++) + if (format[i] == '%') { + while (format[++i]) { + if (format[i] == 'c') { + printf_c(va_arg(ap,int)); + break; + } + if (format[i] == 's') { + printf_s(va_arg(ap,char*)); + break; + } + if (format[i] == 'd') { + printf_d(va_arg(ap,int)); + break; + } + } + } else + printf_c(format[i]); + + va_end(ap); +} + + +int puts(char *s){ + while (*s) { + putchar(*s); + s++; + } + putchar('\n'); + return 0; +} + +void putchar(char c){ + TEST_COM_BASE[0] = c; +} + +#include <time.h> +clock_t clock(){ + return TEST_COM_BASE[4]; +} + |