Re-structure ucore user libs, add getchar

Signed-off-by: Harry Chen <i@harrychen.xyz>
master
Harry Chen 6 years ago
parent 3230c06ce0
commit 7ee8b35ad4

@ -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

@ -4,6 +4,10 @@
#include <defs.h>
#include <stdarg.h>
#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);

@ -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;
}
}

@ -0,0 +1,114 @@
#include <stdio.h>
#include <ulib.h>
#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;
}
Loading…
Cancel
Save