From 7ee8b35ad4a8a2cc97904922eb9053bfb80b7ffd Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Mon, 13 May 2019 19:32:44 +0800 Subject: [PATCH] Re-structure ucore user libs, add getchar Signed-off-by: Harry Chen --- ucore/CMakeLists.txt | 5 +- ucore/src/{libs => include}/defs.h | 0 ucore/src/{ulibs => include}/dir.h | 0 ucore/src/{libs => include}/dirent.h | 0 ucore/src/{libs => include}/elf.h | 0 ucore/src/{libs => include}/error.h | 0 ucore/src/{ulibs => include}/file.h | 0 ucore/src/{libs => include}/list.h | 0 ucore/src/{ulibs => include}/lock.h | 0 ucore/src/{libs => include}/skew_heap.h | 0 ucore/src/{libs => include}/stat.h | 0 ucore/src/{libs => include}/stdarg.h | 0 ucore/src/{libs => include}/stdio.h | 4 + ucore/src/{libs => include}/stdlib.h | 0 ucore/src/{libs => include}/string.h | 0 ucore/src/{ulibs => include}/syscall.h | 0 ucore/src/{ulibs => include}/ulib.h | 0 ucore/src/{libs => include}/unistd.h | 0 ucore/src/{ulibs => lib}/dir.c | 0 ucore/src/{ulibs => lib}/file.c | 0 ucore/src/{libs => lib}/hash.c | 0 ucore/src/{ulibs => lib}/panic.c | 0 ucore/src/{libs => lib}/printfmt.c | 0 ucore/src/{libs => lib}/rand.c | 0 ucore/src/{ulibs => lib}/stdio.c | 13 ++- ucore/src/{libs => lib}/string.c | 0 ucore/src/{ulibs => lib}/syscall.c | 0 ucore/src/{ulibs => lib}/ulib.c | 0 ucore/src/{ulibs => lib}/umain.c | 0 ucore/src/mandelbrot.c | 114 ++++++++++++++++++++++++ 30 files changed, 132 insertions(+), 4 deletions(-) rename ucore/src/{libs => include}/defs.h (100%) rename ucore/src/{ulibs => include}/dir.h (100%) rename ucore/src/{libs => include}/dirent.h (100%) mode change 100755 => 100644 rename ucore/src/{libs => include}/elf.h (100%) rename ucore/src/{libs => include}/error.h (100%) rename ucore/src/{ulibs => include}/file.h (100%) rename ucore/src/{libs => include}/list.h (100%) rename ucore/src/{ulibs => include}/lock.h (100%) rename ucore/src/{libs => include}/skew_heap.h (100%) rename ucore/src/{libs => include}/stat.h (100%) rename ucore/src/{libs => include}/stdarg.h (100%) rename ucore/src/{libs => include}/stdio.h (93%) rename ucore/src/{libs => include}/stdlib.h (100%) rename ucore/src/{libs => include}/string.h (100%) rename ucore/src/{ulibs => include}/syscall.h (100%) rename ucore/src/{ulibs => include}/ulib.h (100%) rename ucore/src/{libs => include}/unistd.h (100%) rename ucore/src/{ulibs => lib}/dir.c (100%) rename ucore/src/{ulibs => lib}/file.c (100%) rename ucore/src/{libs => lib}/hash.c (100%) rename ucore/src/{ulibs => lib}/panic.c (100%) rename ucore/src/{libs => lib}/printfmt.c (100%) rename ucore/src/{libs => lib}/rand.c (100%) rename ucore/src/{ulibs => lib}/stdio.c (89%) rename ucore/src/{libs => lib}/string.c (100%) rename ucore/src/{ulibs => lib}/syscall.c (100%) rename ucore/src/{ulibs => lib}/ulib.c (100%) rename ucore/src/{ulibs => lib}/umain.c (100%) create mode 100644 ucore/src/mandelbrot.c diff --git a/ucore/CMakeLists.txt b/ucore/CMakeLists.txt index 6c53058..d75d16c 100644 --- a/ucore/CMakeLists.txt +++ b/ucore/CMakeLists.txt @@ -10,10 +10,9 @@ else() set(ARCH_DIR src/arch/${ARCH}) endif() aux_source_directory(src SRCS) -aux_source_directory(src/libs LIBS) -aux_source_directory(src/ulibs LIBS) +aux_source_directory(src/lib LIBS) set(LIBS ${ARCH_DIR}/initcode.S ${LIBS}) -include_directories(src/libs src/ulibs ${ARCH_DIR}) +include_directories(src/include ${ARCH_DIR}) set(EXECUTABLE_OUTPUT_PATH ${ARCH}) # Toolchain diff --git a/ucore/src/libs/defs.h b/ucore/src/include/defs.h similarity index 100% rename from ucore/src/libs/defs.h rename to ucore/src/include/defs.h diff --git a/ucore/src/ulibs/dir.h b/ucore/src/include/dir.h similarity index 100% rename from ucore/src/ulibs/dir.h rename to ucore/src/include/dir.h diff --git a/ucore/src/libs/dirent.h b/ucore/src/include/dirent.h old mode 100755 new mode 100644 similarity index 100% rename from ucore/src/libs/dirent.h rename to ucore/src/include/dirent.h diff --git a/ucore/src/libs/elf.h b/ucore/src/include/elf.h similarity index 100% rename from ucore/src/libs/elf.h rename to ucore/src/include/elf.h diff --git a/ucore/src/libs/error.h b/ucore/src/include/error.h similarity index 100% rename from ucore/src/libs/error.h rename to ucore/src/include/error.h diff --git a/ucore/src/ulibs/file.h b/ucore/src/include/file.h similarity index 100% rename from ucore/src/ulibs/file.h rename to ucore/src/include/file.h diff --git a/ucore/src/libs/list.h b/ucore/src/include/list.h similarity index 100% rename from ucore/src/libs/list.h rename to ucore/src/include/list.h diff --git a/ucore/src/ulibs/lock.h b/ucore/src/include/lock.h similarity index 100% rename from ucore/src/ulibs/lock.h rename to ucore/src/include/lock.h diff --git a/ucore/src/libs/skew_heap.h b/ucore/src/include/skew_heap.h similarity index 100% rename from ucore/src/libs/skew_heap.h rename to ucore/src/include/skew_heap.h diff --git a/ucore/src/libs/stat.h b/ucore/src/include/stat.h similarity index 100% rename from ucore/src/libs/stat.h rename to ucore/src/include/stat.h diff --git a/ucore/src/libs/stdarg.h b/ucore/src/include/stdarg.h similarity index 100% rename from ucore/src/libs/stdarg.h rename to ucore/src/include/stdarg.h diff --git a/ucore/src/libs/stdio.h b/ucore/src/include/stdio.h similarity index 93% rename from ucore/src/libs/stdio.h rename to ucore/src/include/stdio.h index ab03960..89fb45d 100644 --- a/ucore/src/libs/stdio.h +++ b/ucore/src/include/stdio.h @@ -4,6 +4,10 @@ #include #include +#define stdin 0 +#define stdout 1 +#define stderr 2 + /* kern/libs/stdio.c */ int cprintf(const char *fmt, ...); int vcprintf(const char *fmt, va_list ap); diff --git a/ucore/src/libs/stdlib.h b/ucore/src/include/stdlib.h similarity index 100% rename from ucore/src/libs/stdlib.h rename to ucore/src/include/stdlib.h diff --git a/ucore/src/libs/string.h b/ucore/src/include/string.h similarity index 100% rename from ucore/src/libs/string.h rename to ucore/src/include/string.h diff --git a/ucore/src/ulibs/syscall.h b/ucore/src/include/syscall.h similarity index 100% rename from ucore/src/ulibs/syscall.h rename to ucore/src/include/syscall.h diff --git a/ucore/src/ulibs/ulib.h b/ucore/src/include/ulib.h similarity index 100% rename from ucore/src/ulibs/ulib.h rename to ucore/src/include/ulib.h diff --git a/ucore/src/libs/unistd.h b/ucore/src/include/unistd.h similarity index 100% rename from ucore/src/libs/unistd.h rename to ucore/src/include/unistd.h diff --git a/ucore/src/ulibs/dir.c b/ucore/src/lib/dir.c similarity index 100% rename from ucore/src/ulibs/dir.c rename to ucore/src/lib/dir.c diff --git a/ucore/src/ulibs/file.c b/ucore/src/lib/file.c similarity index 100% rename from ucore/src/ulibs/file.c rename to ucore/src/lib/file.c diff --git a/ucore/src/libs/hash.c b/ucore/src/lib/hash.c similarity index 100% rename from ucore/src/libs/hash.c rename to ucore/src/lib/hash.c diff --git a/ucore/src/ulibs/panic.c b/ucore/src/lib/panic.c similarity index 100% rename from ucore/src/ulibs/panic.c rename to ucore/src/lib/panic.c diff --git a/ucore/src/libs/printfmt.c b/ucore/src/lib/printfmt.c similarity index 100% rename from ucore/src/libs/printfmt.c rename to ucore/src/lib/printfmt.c diff --git a/ucore/src/libs/rand.c b/ucore/src/lib/rand.c similarity index 100% rename from ucore/src/libs/rand.c rename to ucore/src/lib/rand.c diff --git a/ucore/src/ulibs/stdio.c b/ucore/src/lib/stdio.c similarity index 89% rename from ucore/src/ulibs/stdio.c rename to ucore/src/lib/stdio.c index 306a3b1..ca06f66 100644 --- a/ucore/src/ulibs/stdio.c +++ b/ucore/src/lib/stdio.c @@ -17,7 +17,7 @@ fputch(char c, int *cnt, int fd) { * */ static void cputch(int c, int *cnt) { - fputch(c, cnt, 1); + fputch(c, cnt, stdout); } /* * @@ -85,3 +85,14 @@ fprintf(int fd, const char *fmt, ...) { return cnt; } + +int +getchar(void) { + char c; + long result = read(stdin, &c, sizeof(char)); + if (result < 0) { + return -1; + } else { + return c; + } +} \ No newline at end of file diff --git a/ucore/src/libs/string.c b/ucore/src/lib/string.c similarity index 100% rename from ucore/src/libs/string.c rename to ucore/src/lib/string.c diff --git a/ucore/src/ulibs/syscall.c b/ucore/src/lib/syscall.c similarity index 100% rename from ucore/src/ulibs/syscall.c rename to ucore/src/lib/syscall.c diff --git a/ucore/src/ulibs/ulib.c b/ucore/src/lib/ulib.c similarity index 100% rename from ucore/src/ulibs/ulib.c rename to ucore/src/lib/ulib.c diff --git a/ucore/src/ulibs/umain.c b/ucore/src/lib/umain.c similarity index 100% rename from ucore/src/ulibs/umain.c rename to ucore/src/lib/umain.c diff --git a/ucore/src/mandelbrot.c b/ucore/src/mandelbrot.c new file mode 100644 index 0000000..5fe9b21 --- /dev/null +++ b/ucore/src/mandelbrot.c @@ -0,0 +1,114 @@ +#include +#include + +#define WIDTH 800 +#define HEIGHT 600 + +char buf[WIDTH]; + +volatile char* frame_buf = 0xA2000000; +int w = WIDTH, h = HEIGHT, x, y; +//each iteration, it calculates: newz = oldz*oldz + p, where p is the current pixel, and oldz stars at the origin +float pr, pi; //real and imaginary part of the pixel p +float newRe, newIm, oldRe, oldIm; //real and imaginary parts of new and old z + + +void plot(float moveX, float moveY, float zoom, int maxIterations, int skip) { + cprintf("plot\n"); + //loop through every pixel + char* line_addr = frame_buf; + for (y = 0; y < h; y++) { + if (y % skip) { + memcpy(line_addr, line_addr - WIDTH, WIDTH); + line_addr += WIDTH; + continue; + } + for (x = 0; x < w; x++) { + if (x % skip) { + buf[x] = buf[x - 1]; + continue; + } + // calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values + pr = 1.5f * (x - w / 2) / (0.5f * zoom * w) + moveX; + pi = (y - h / 2) / (0.5f * zoom * h) + moveY; + newRe = newIm = oldRe = oldIm = 0; //these should start at 0,0 + //"i" will represent the number of iterations + int i; + //start the iteration process + for (i = 0; i < maxIterations; i++) { + //remember value of previous iteration + oldRe = newRe; + oldIm = newIm; + //the actual iteration, the real and imaginary part are calculated + newRe = oldRe * oldRe - oldIm * oldIm + pr; + newIm = 2.0f * oldRe * oldIm + pi; + //if the point is outside the circle with radius 2: stop + if ((newRe * newRe + newIm * newIm) > 4) + break; + } + + int color = i * 255 / maxIterations; + + buf[x] = (color << 5) | (color << 2) | (color >> 1); + } + memcpy(line_addr, buf, WIDTH); + line_addr += WIDTH; + } +} + +int main(void) { + float zoom = 1, moveX = -0.5, moveY = 0; //you can change these to zoom and change position + int maxIterations = 255; //after how much iterations the function should stop + int skip = 4; + char c; + plot(moveX, moveY, zoom, maxIterations, skip); + + while ((c = getchar) > 0) { + cprintf(" %d\n", c); + if (c == 'x') { + skip = 1; + } else if (c == 'h') { + moveX -= 0.2f / zoom; + cprintf("moveX left\n"); + } else if (c == 'l') { + moveX += 0.2f / zoom; + cprintf("moveX right\n"); + } else if (c == 'j') { + moveY += 0.2f / zoom; + cprintf("moveX down\n"); + } else if (c == 'k') { + moveY -= 0.2f / zoom; + cprintf("moveX up\n"); + } else if (c == ']') { + zoom += 3; + } else if (c == '[') { + zoom -= 3; + if (zoom <= 1) + zoom = 1; + } else if (c == 'q') { + break; + } else if (c == 'r') { + zoom = 1; + moveX = -0.5; + moveY = 0; + } else if (c == 'p') { + cprintf("x - HD\n"); + cprintf("h - Move left\n"); + cprintf("j - Move down\n"); + cprintf("k - Move up\n"); + cprintf("l - Move right\n"); + cprintf("[ - Zoom out\n"); + cprintf("] - Zoom in\n"); + cprintf("q - Quit\n"); + cprintf("r - Reset\n"); + cprintf("p - Print this help\n"); + } else { + cprintf("not recognized.\n"); + } + plot(moveX, moveY, zoom, maxIterations, skip); + skip = 4; + c = 0; + cprintf(">"); + } + return 0; +}