mirror of
https://github.com/ringtailsoftware/uvm32.git
synced 2026-06-05 22:43:39 +00:00
Recursive maze demo, causes big stack usage
This commit is contained in:
parent
428bd7cc6b
commit
9596838a4b
6 changed files with 116 additions and 1 deletions
|
|
@ -22,6 +22,7 @@ Although based on a fully fledged CPU emulator, uvm32 is intended for executing
|
||||||
* [apps/helloworld](apps/helloworld) C hello world program
|
* [apps/helloworld](apps/helloworld) C hello world program
|
||||||
* [apps/conio](apps/conio) C console IO demo
|
* [apps/conio](apps/conio) C console IO demo
|
||||||
* [apps/lissajous](apps/lissajous) C console lissajous curve (showing softfp, floating point)
|
* [apps/lissajous](apps/lissajous) C console lissajous curve (showing softfp, floating point)
|
||||||
|
* [apps/maze](apps/maze) C ASCII art recursive maze generation
|
||||||
* [apps/hello-asm](apps/hello-asm) Minimal hello world assembly
|
* [apps/hello-asm](apps/hello-asm) Minimal hello world assembly
|
||||||
* [apps/fib](apps/fib) C fibonacci series program (iterative and recursive)
|
* [apps/fib](apps/fib) C fibonacci series program (iterative and recursive)
|
||||||
* [apps/self](apps/self) host-mini with embedded mandelbrot generation program, compiled as an app (inception!)
|
* [apps/self](apps/self) host-mini with embedded mandelbrot generation program, compiled as an app (inception!)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ all:
|
||||||
(cd zigtris && make)
|
(cd zigtris && make)
|
||||||
(cd rust-hello && make)
|
(cd rust-hello && make)
|
||||||
(cd hello-asm && make)
|
(cd hello-asm && make)
|
||||||
|
(cd maze && make)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
(cd sketch && make clean)
|
(cd sketch && make clean)
|
||||||
|
|
@ -22,4 +23,5 @@ clean:
|
||||||
(cd zigtris && make clean)
|
(cd zigtris && make clean)
|
||||||
(cd rust-hello && make clean)
|
(cd rust-hello && make clean)
|
||||||
(cd hello-asm && make clean)
|
(cd hello-asm && make clean)
|
||||||
|
(cd maze && make clean)
|
||||||
|
|
||||||
|
|
|
||||||
27
apps/maze/Makefile
Normal file
27
apps/maze/Makefile
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
PROJECT:=maze
|
||||||
|
|
||||||
|
DOCKER_IMAGE=riscv-dev
|
||||||
|
DOCKER_CMD:=docker run --rm -v ${PWD}../../../:/data -w /data/apps/${PROJECT} ${DOCKER_IMAGE}
|
||||||
|
PREFIX:=${DOCKER_CMD} riscv64-unknown-elf-
|
||||||
|
CFLAGS+=-I../../common
|
||||||
|
CFLAGS+=-fno-stack-protector
|
||||||
|
CFLAGS+=-static-libgcc -fdata-sections -ffunction-sections
|
||||||
|
CFLAGS+=-g -Os -march=rv32ima_zicsr -mabi=ilp32 -static
|
||||||
|
LDFLAGS:= -T ../linker.ld -nostdlib -Wl,--gc-sections
|
||||||
|
LIBS:= #-lgcc # needed for softfp
|
||||||
|
|
||||||
|
SRCS=${PROJECT}.c ../crt0.S
|
||||||
|
|
||||||
|
all:
|
||||||
|
${PREFIX}gcc -o ${PROJECT}.elf ${CFLAGS} ${LDFLAGS} ${SRCS} ${LIBS}
|
||||||
|
$(PREFIX)objcopy ${PROJECT}.elf -O binary ${PROJECT}.bin
|
||||||
|
|
||||||
|
disasm: all
|
||||||
|
$(PREFIX)objdump -S -d -f ${PROJECT}.elf
|
||||||
|
|
||||||
|
test: all
|
||||||
|
../../host/host ${PWD}/${PROJECT}.bin
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f ${PROJECT}.o ${PROJECT}.elf ${PROJECT}.bin
|
||||||
|
|
||||||
85
apps/maze/maze.c
Normal file
85
apps/maze/maze.c
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
#include "uvm32_target.h"
|
||||||
|
|
||||||
|
// This example generates a random maze using the recursive backtracking algorithm.
|
||||||
|
// https://github.com/ccattuto/riscv-python/blob/main/tests/test_newlib_maze.c
|
||||||
|
|
||||||
|
#define WIDTH 79 // must be odd
|
||||||
|
#define HEIGHT 31 // must be odd
|
||||||
|
|
||||||
|
char maze[HEIGHT * WIDTH];
|
||||||
|
|
||||||
|
void* memcpy(void* dst, const void* src, int len) {
|
||||||
|
uint8_t* d = (uint8_t*)dst;
|
||||||
|
const uint8_t* s = (const uint8_t*)src;
|
||||||
|
while (len--) {
|
||||||
|
*(d++) = *(s++);
|
||||||
|
}
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* memset(void* buf, int c, int len) {
|
||||||
|
uint8_t* b = (uint8_t*)buf;
|
||||||
|
while (len--) {
|
||||||
|
*(b++) = c;
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
int dx[] = {0, 1, 0, -1};
|
||||||
|
int dy[] = {-1, 0, 1, 0};
|
||||||
|
|
||||||
|
int seed = 123456789;
|
||||||
|
|
||||||
|
int rand() {
|
||||||
|
uint32_t a = 1103515245;
|
||||||
|
uint32_t c = 12345;
|
||||||
|
uint32_t m = 0xFFFFFFFF;
|
||||||
|
seed = (a * seed + c) % m;
|
||||||
|
return seed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_maze() {
|
||||||
|
memset(maze, '#', sizeof(maze));
|
||||||
|
}
|
||||||
|
|
||||||
|
int in_bounds(int x, int y) {
|
||||||
|
return x > 0 && y > 0 && x < WIDTH - 1 && y < HEIGHT - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void carve(int x, int y) {
|
||||||
|
maze[y * WIDTH + x] = ' ';
|
||||||
|
|
||||||
|
int dirs[] = {0, 1, 2, 3};
|
||||||
|
// Fisher-Yates shuffle
|
||||||
|
for (int i = 3; i > 0; i--) {
|
||||||
|
int j = rand() % (i + 1);
|
||||||
|
int tmp = dirs[i];
|
||||||
|
dirs[i] = dirs[j];
|
||||||
|
dirs[j] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
int nx = x + dx[dirs[i]] * 2;
|
||||||
|
int ny = y + dy[dirs[i]] * 2;
|
||||||
|
|
||||||
|
if (in_bounds(nx, ny) && maze[ny * WIDTH + nx] == '#') {
|
||||||
|
maze[(y + dy[dirs[i]]) * WIDTH + (x + dx[dirs[i]])] = ' ';
|
||||||
|
carve(nx, ny);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_maze() {
|
||||||
|
for (int y = 0; y < HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
|
putc(maze[y * WIDTH + x]);
|
||||||
|
}
|
||||||
|
putc('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
init_maze();
|
||||||
|
carve(1, 1);
|
||||||
|
print_maze();
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
all:
|
all:
|
||||||
gcc -Wall -pedantic -std=c99 -O2 -DUVM32_MEMORY_SIZE=32768 -I../uvm32 -I../common -o host ../uvm32/uvm32.c host.c
|
gcc -Wall -pedantic -std=c99 -O2 -DUVM32_MEMORY_SIZE=65535 -I../uvm32 -I../common -o host ../uvm32/uvm32.c host.c
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f host
|
rm -f host
|
||||||
|
|
|
||||||
BIN
precompiled/maze.bin
Executable file
BIN
precompiled/maze.bin
Executable file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue