Commit Diff


commit - a8ceaca4fff83dffd72f34c096e41d5a0cdfce5c
commit + e46639b37c8c6d9ceeab59634d61b1eccdef9357
blob - c0e9ed862854b37f31f7b51c55cc177ce45f5806
blob + 3f9f29c92857affc14e718a29487efa11effdaa8
--- .gitignore
+++ .gitignore
@@ -1,3 +1,6 @@
+examples/*.elf
+src/syscalls.h
+src/*.o
 tools/bin
 tools/build
 tools/include
@@ -6,12 +9,9 @@ tools/libexec
 tools/riscv64-unknown-linux-musl
 tools/share
 tools/src
-src/syscalls.h
-src/*.o
+microcoreutils
 *.swp
 *.core
-*.o
-*.elf
 *.pdf
 rootfs
 rvemu
blob - cc581bb65cc25b6023263787fb09d3492a7b8c5e
blob + e9573f14c36a97ee872016ce528b881e7c5e7ed0
--- Makefile
+++ Makefile
@@ -10,28 +10,31 @@ CROSS	= ./tools/bin/${TARGET}
 CFLAGS	= -std=c2x -fPIC -O0 -g
 LDFLAGS	= -pie -static -lpthread
 OBJ	= src/rvemu.o src/ecall.o src/cpu.o src/exec.o
-PROGS	= test.elf
+T	= test
+PROGS	= examples/test.elf	\
+	  examples/echo.elf	\
+	  examples/cat.elf
 
 all: rvemu ${PROGS}
 
-od: test.elf
-	${CROSS}-objdump -d test.elf | less
+od: examples/$T.elf
+	${CROSS}-objdump -d examples/$T.elf | less
 
+run: rvemu ${PROGS}
+	mkdir -p rootfs/bin
+	cp -f rvemu rootfs/bin
+	cp -f ${PROGS} rootfs/bin
+	${SUDO} chroot rootfs /bin/rvemu /bin/$T.elf
+
 clean:
-	rm -f rvemu src/*.o *.elf *.core src/syscalls.h
+	rm -f rvemu src/*.o examples/*.elf *.core src/syscalls.h
 	rm -rf rootfs
 
 install: rvemu
 	mkdir -p ${DESTDIR}${PREFIX}/bin
 	cp -f rvemu ${DESTDIR}${PREFIX}/bin/
 
-run: rvemu ${PROGS}
-	mkdir -p rootfs/bin
-	cp -f rvemu rootfs/bin
-	cp -f ${PROGS} rootfs
-	${SUDO} chroot rootfs /bin/rvemu /test.elf
 
-
 rvemu: ${OBJ}
 	${CC} -o $@ ${OBJ} ${LDFLAGS}
 
@@ -46,4 +49,4 @@ src/syscalls.h: src/syscalls.inc
 
 
 .c.elf:
-	${CROSS}-gcc -o $@ $<
+	${CROSS}-gcc -o $@ $< -O2
blob - a0d8b14d8d05941c2315f86aa3a6da4ba0551ba4
blob + 3f16fad3949a076ddca5b6547797a5a8685800f8
--- src/rvemu.c
+++ src/rvemu.c
@@ -166,12 +166,11 @@ int main (int argc, char *argv[], char *envp[])
 	if (strcmp (base, "rvemu") == 0) {
 		if (argc < 2)
 			errx (1, "usage: rvemu file");
-
-		filename = argv[1];
-	} else {
-		filename = argv[0];
+		++argv;
+		--argc;
 	}
 
+	filename = argv[0];
 	load_image (filename);
 
 	if (getrlimit (RLIMIT_STACK, &rl) == 0) {
blob - /dev/null
blob + 59f126f89644bd5a2f80bb454eb876f488386d09 (mode 644)
--- /dev/null
+++ examples/cat.c
@@ -0,0 +1,41 @@
+#include <string.h>
+#include <stdio.h>
+#include <err.h>
+
+static void cat (FILE* file) {
+	int ch;
+	while ((ch = fgetc(file)) != EOF)
+		putchar(ch);
+}
+
+int main (int argc, char* argv[]) {
+	FILE *file;
+	int ec = 0;
+	int ch;
+
+	if (argc == 1) {
+		cat (stdin);
+		return 0;
+	}
+
+	for (int i = 1; i < argc; ++i) {
+		if (strcmp (argv[i], "-") == 0) {
+			file = stdin;
+		} else {
+			file = fopen (argv[i], "r");
+		}
+
+		if (file == NULL) {
+			warn ("%s", argv[i]);
+			ec = 1;
+			continue;
+		}
+
+		cat (file);
+
+		if (file != stdin)
+			fclose (file);
+	}
+	return ec;
+}
+
blob - /dev/null
blob + 5ad7908c5d638a2c55983b57b997a0d82d67ad7e (mode 644)
--- /dev/null
+++ examples/echo.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main (int argc, char* argv[]) {
+	if (argc == 1)
+		goto end;
+
+	printf ("%s", argv[1]);
+	for (int i = 2; i < argc; ++i)
+		printf (" %s", argv[i]);
+
+end:
+	putchar('\n');
+	return 0;
+}
blob - /dev/null
blob + 57cb5e69d3c66c9307ecd7ca4a09c3a67be9208d (mode 644)
--- /dev/null
+++ examples/test.c
@@ -0,0 +1,23 @@
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+
+volatile int x = 42;
+
+__attribute__((always_inline))
+inline static void ebreak (void)
+{
+	__asm __volatile__ ("ebreak");
+}
+
+unsigned mul (unsigned a, unsigned b)
+{
+	return a * b;
+}
+
+int main (int argc, char *argv[]) {
+	printf ("argc = %d\n", argc);
+	for (int i = 1; i < argc; ++i)
+		puts (argv[i]);
+	return 0;
+}
blob - 03f581641727d6bf3a3b2bde53a69d2d7de35b27 (mode 644)
blob + /dev/null
--- test.c
+++ /dev/null
@@ -1,16 +0,0 @@
-#include <unistd.h>
-#include <string.h>
-#include <stdio.h>
-
-volatile int x = 42;
-
-__attribute__((always_inline))
-inline static void ebreak (void)
-{
-	__asm __volatile__ ("ebreak");
-}
-
-int main (void) {
-	printf ("Hello World, x = %d\n", x);
-	return 0;
-}
blob - 7c00c653c85d2f7a68c6ddec2ec894995f887fc2
blob + e777f65d1defd61013642483c67df9e3258a9bb5
--- tools/Makefile
+++ tools/Makefile
@@ -4,7 +4,7 @@ TOP != pwd
 STAMPS = ${TOP}/build/.stamps
 PREFIX = ${TOP}
 TARGET = riscv64-unknown-linux-musl
-ARCH = rv64ima
+ARCH = rv64ia
 
 BINUTILS_VER = 2.42
 GCC_VER = 13.2.0
@@ -189,7 +189,9 @@ ${STAMPS}/gcc-install: ${STAMPS}/gcc-build
 
 ${STAMPS}/musl-configure: ${STAMPS}/musl-extract ${STAMPS}/gcc-install
 	mkdir -p ${TOP}/build/musl/build
-	cd ${TOP}/build/musl/build && ../configure	\
+	cd ${TOP}/build/musl/build &&			\
+		CROSS_COMPILE=${PREFIX}/bin/${TARGET}-	\
+		../configure				\
 		--prefix=${PREFIX}/${TARGET}		\
 		--target=${TARGET}			\
 		--disable-shared			\