Fix mandelbrot on x86_64

master
Jiajie Chen 6 years ago
parent 4f90dd042b
commit bbcee244e8

@ -3,20 +3,13 @@
#include <defs.h> #include <defs.h>
#define do_div(n, base) ({ \ # define do_div(n,base) ({ \
unsigned long __upper, __low, __high, __mod, __base; \ uint32_t __base = (base); \
__base = (base); \ uint32_t __rem; \
asm ("" : "=a" (__low), "=d" (__high) : "A" (n)); \ __rem = ((uint64_t)(n)) % __base; \
__upper = __high; \ (n) = ((uint64_t)(n)) / __base; \
if (__high != 0) { \ __rem; \
__upper = __high % __base; \ })
__high = __high / __base; \
} \
asm ("divl %2" : "=a" (__low), "=d" (__mod) \
: "rm" (__base), "0" (__low), "1" (__upper)); \
asm ("" : "=A" (n) : "a" (__low), "d" (__high)); \
__mod; \
})
#define barrier() __asm__ __volatile__ ("" ::: "memory") #define barrier() __asm__ __volatile__ ("" ::: "memory")

@ -9,15 +9,17 @@
#if defined(__x86_64__) #if defined(__x86_64__)
#define WIDTH 1024 #define WIDTH 1024
#define HEIGHT 768 #define HEIGHT 768
#define BPP 3
#else #else
#define WIDTH 800 #define WIDTH 800
#define HEIGHT 600 #define HEIGHT 600
#define BPP 1
#endif #endif
char buf[WIDTH]; char buf[WIDTH * BPP];
volatile char* frame_buf = (volatile char*) 0xA2000000; volatile char* frame_buf = (volatile char*) 0xA2000000;
int w = WIDTH, h = HEIGHT, x, y; int w = WIDTH, h = HEIGHT, x, y, i;
//each iteration, it calculates: newz = oldz*oldz + p, where p is the current pixel, and oldz stars at the origin //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 pr, pi; //real and imaginary part of the pixel p
float newRe, newIm, oldRe, oldIm; //real and imaginary parts of new and old z float newRe, newIm, oldRe, oldIm; //real and imaginary parts of new and old z
@ -29,13 +31,15 @@ void plot(float moveX, float moveY, float zoom, int maxIterations, int skip) {
volatile char *line_addr = frame_buf; volatile char *line_addr = frame_buf;
for (y = 0; y < h; y++) { for (y = 0; y < h; y++) {
if (y % skip) { if (y % skip) {
memcpy(line_addr, line_addr - WIDTH, WIDTH); memcpy(line_addr, line_addr - WIDTH * BPP, WIDTH * BPP);
line_addr += WIDTH; line_addr += WIDTH * BPP;
continue; continue;
} }
for (x = 0; x < w; x++) { for (x = 0; x < w; x++) {
if (x % skip) { if (x % skip) {
buf[x] = buf[x - 1]; for (i = 0;i < BPP;i++) {
buf[x * BPP + i] = buf[(x - 1) * BPP + i];
}
continue; continue;
} }
// calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values // calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
@ -59,17 +63,19 @@ void plot(float moveX, float moveY, float zoom, int maxIterations, int skip) {
int color = i * 255 / maxIterations; int color = i * 255 / maxIterations;
buf[x] = (color << 5) | (color << 2) | (color >> 1); for (i = 0;i < BPP;i++) {
buf[x * BPP + i] = (color << 5) | (color << 2) | (color >> 1);
}
} }
memcpy(line_addr, buf, WIDTH); memcpy(line_addr, buf, WIDTH * BPP);
line_addr += WIDTH; line_addr += WIDTH * BPP;
} }
} }
int main(void) { int main(void) {
#if defined(__x86_64__) #if defined(__x86_64__)
int fd = sys_open("/dev/fb0", O_WRONLY); int fd = sys_open("/dev/fb0", O_WRONLY);
frame_buf = (volatile char *)sys_mmap(0, WIDTH * HEIGHT * 3, PROT_WRITE, 0, fd, 0); frame_buf = (volatile char *)sys_mmap(0, WIDTH * HEIGHT * BPP, PROT_WRITE, 0, fd, 0);
#endif #endif
float zoom = 1, moveX = -0.5, moveY = 0; //you can change these to zoom and change position 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 maxIterations = 255; //after how much iterations the function should stop

Loading…
Cancel
Save