diff --git a/c-print-fibonacci/Makefile b/c-print-fibonacci/Makefile new file mode 100644 index 0000000..49d1aab --- /dev/null +++ b/c-print-fibonacci/Makefile @@ -0,0 +1 @@ +include ../common/common.mk diff --git a/c-print-fibonacci/main.c b/c-print-fibonacci/main.c new file mode 100644 index 0000000..6cd2b4c --- /dev/null +++ b/c-print-fibonacci/main.c @@ -0,0 +1,49 @@ +static void putc(int); +static void puti(int); +static int fib(int); + +/* TODO: this sample fails to compile on higher -O values */ + +void _start(void) +{ + asm volatile ("law 04000"); + asm volatile ("dac 209"); + asm volatile ("law 03000"); + asm volatile ("dac 208"); + + int n; + + for (int i = 0; i < 20; i++) { + puti(fib(i)); + putc(033); + } + + asm volatile ("hlt"); + __builtin_unreachable(); +} + +static void putc(int c) +{ + asm volatile ("lio %0" : : "r"(c) : "io"); + asm volatile ("tyo"); +} + +/* TODO: this is printing out digits in reverse order; fix it */ +static void puti(int i) +{ + int mod; + + do { + mod = i % 10; + putc((mod == 0) ? 020 : mod); + i /= 10; + } while (i != 0); +} + +static int fib(int n) +{ + if (n <= 1) + return n; + else + return fib(n - 1) + fib(n - 2); +} diff --git a/common/common.mk b/common/common.mk index 8fb7202..1132be7 100644 --- a/common/common.mk +++ b/common/common.mk @@ -1,4 +1,5 @@ APP_NAME ?= main +CCFLAGS ?= -O0 objects-asm := $(patsubst %.S,%.o,$(wildcard *.S)) objects-c := $(patsubst %.c,%.o,$(wildcard *.c)) objects := $(objects-asm) $(objects-c) @@ -6,7 +7,7 @@ objects := $(objects-asm) $(objects-c) default: $(APP_NAME).rim %.s: %.c - ../build-gcc/gcc/cc1 $< + ../build-gcc/gcc/cc1 $(CCFLAGS) $< %.o: %.S pdp1-elf-as $< -o $@ diff --git a/tests/test.exp b/tests/test.exp index f41adad..44126ab 100644 --- a/tests/test.exp +++ b/tests/test.exp @@ -2,6 +2,7 @@ set timeout 1 proc run_test { demo_name expected_output } { spawn "pdp1" + send "set cpu mdv\n" send "attach ptr $demo_name/main.rim\n" expect { "PTR: creating new file" { fail "$demo_name: Didn't find the RIM tape file\n" } @@ -31,3 +32,4 @@ run_test "c-print-for-loop" "xxxxyxxx" run_test "c-test-comparison" "aceg" run_test "c-test-arith" "xx" run_test "c-test-recursion" "xxxxxxxxxxxxxxxxxxxx" +run_test "c-print-fibonacci" "0,1,1,2,3,5,8,31,12,43,55,98,441,332,773,016,789,7951,4852,1814,"