Skip to content

Commit

Permalink
samples: add c-print-fibonacci
Browse files Browse the repository at this point in the history
Signed-off-by: Filip Kokosiński <[email protected]>
  • Loading branch information
fkokosinski committed Jun 22, 2024
1 parent 6bcae7e commit f81517b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions c-print-fibonacci/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../common/common.mk
49 changes: 49 additions & 0 deletions c-print-fibonacci/main.c
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 2 additions & 1 deletion common/common.mk
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
APP_NAME ?= main
CCFLAGS ?= -O0
objects-asm := $(patsubst %.S,%.o,$(wildcard *.S))
objects-c := $(patsubst %.c,%.o,$(wildcard *.c))
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 $@
Expand Down
2 changes: 2 additions & 0 deletions tests/test.exp
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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,"

0 comments on commit f81517b

Please sign in to comment.