os-workshop

same materials and sample source for RV32 OS projects
git clone http://frotz.net/git/os-workshop.git
Log | Files | Refs

mandelbrot.c (2132B)


      1 // Copyright 2022, Brian Swetland <swetland@frotz.net>
      2 // Licensed under the Apache License, Version 2.0
      3 
      4 #include <xos/syscalls.h>
      5 #include <hw/debug.h>
      6 #include <gfx/gfx.h>
      7 #include <string.h>
      8 
      9 uint16_t colors[12] = {
     10 	0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666,
     11 	0x7777, 0x8888, 0x9999, 0xAAAA, 0xBBBB, 0xCCCC,
     12 };
     13 
     14 int render(gfx_surface_t* _gs,
     15 	uint32_t px0, uint32_t py0,
     16 	uint32_t px1, uint32_t py1) {
     17 	gfx_surface_t gs;
     18 	memcpy(&gs, _gs, sizeof(gs));
     19 
     20 	for (int py = py0; py < py1; py++) {
     21 		int y0 = 650 - (1300 * py) / gs.height;
     22 		for (int px = px0; px < px1; px++) {
     23 			int x0 = -1250 + (1750 * px) / gs.width;
     24 			int x = 0, y = 0;
     25 			for (int i = 0; i < 500; i++) {
     26 				int x2 = x * x / 1000;
     27 				int y2 = y * y / 1000;
     28 				if ((x2 + y2) > 4000) {
     29 					gs.fgcolor = colors[(i > 11) ? 11 : i];
     30 					gfx_plot(&gs, px, py);
     31 					goto done;
     32 				}
     33 				y = 2 * x * y / 1000 + y0;
     34 				x = x2 - y2 + x0;
     35 			}
     36 			gs.fgcolor = 0;
     37 			gfx_plot(&gs, px, py);
     38 		done:
     39 			;
     40 		}
     41 	}
     42 
     43 	return 2;
     44 }
     45 
     46 #if 0
     47 int t1(void* arg) {
     48 	gfx_surface_t *gs = arg;
     49 	gfx_fill(gs, 0, 0, 320, 240, gfx_color(gs, C_WHITE));
     50 	gfx_puts(gs, 0, 240-16, "Thread One");
     51 	return render(gs, 0, 0, 320, 240);
     52 }
     53 int t2(void* arg) {
     54 	gfx_surface_t *gs = arg;
     55 	gfx_fill(gs, 320, 240, 640, 480, gfx_color(gs, C_GRAY75));
     56 	gfx_puts(gs, 320, 480-16, "Thread Two");
     57 	return render(gs, 320, 240, 640, 480);
     58 }
     59 int t3(void* arg) {
     60 	gfx_surface_t *gs = arg;
     61 	gfx_fill(gs, 320, 0, 640, 240, gfx_color(gs, C_GRAY25));
     62 	gfx_puts(gs, 320, 240-16, "Thread Three");
     63 	return render(gs, 320, 0, 640, 240);
     64 }
     65 int t4(void* arg) {
     66 	gfx_surface_t *gs = arg;
     67 	gfx_fill(gs, 0, 240, 320, 480, gfx_color(gs, C_GRAY50));
     68 	gfx_puts(gs, 0, 480-16, "Thread Four");
     69 	return render(gs, 0, 240, 320, 480);
     70 }
     71 #endif
     72 
     73 static gfx_surface_t screen;
     74 
     75 void start(void) {
     76 	xprintf("X/OS Example - Mandelbrot\n\n");
     77 
     78 	screen.width = 640;
     79 	screen.height = 480;
     80 	screen.stride = 640;
     81 	screen.pixels = (void*) 0x20000000;
     82 	screen.pixfmt = PIXFMT_RGB565;
     83 	screen.fgcolor = C_WHITE;
     84 	screen.bgcolor = C_BLUE;
     85 	gfx_init(&screen);
     86 
     87 	gfx_clear(&screen, C_BLUE);
     88 	render(&screen, 0, 0, 640, 480);
     89 
     90 	exit(0);
     91 }
     92