Skip to content

Commit

Permalink
gcc: hide accumulator from the compiler
Browse files Browse the repository at this point in the history
Signed-off-by: Filip Kokosiński <[email protected]>
  • Loading branch information
fkokosinski committed Jul 8, 2024
1 parent c33516f commit 669911c
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 31 deletions.
70 changes: 51 additions & 19 deletions c-test-arith/main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
static void putc(int);

int x;
int y;
int z;
int x = 21;
int y = 37;

void _start(void)
{
Expand All @@ -12,22 +9,57 @@ void _start(void)
asm volatile ("law 04000");
asm volatile ("dac 0130");

x = 21;
y = 37;
/* test addition with two variables */
*(volatile int *)(01000) = x + y;

/* test addition with an immediate value */
*(volatile int *)(01001) = x + 20;

/* test addition with an immediate value equal 1 */
*(volatile int *)(01002) = x + 1;

/* test subtraction with two variables */
*(volatile int *)(01003) = y - x;

/* test subtraction with an immediate value */
*(volatile int *)(01004) = y - 10;

/* test multiplication with two variables */
*(volatile int *)(01005) = x * y;

/* test division with two variables */
*(volatile int *)(01006) = y / x;

/* test modulo with two variables */
*(volatile int *)(01007) = y % x;

/* test logical negation with a variable */
*(volatile int *)(01010) = ~x;

z = x << 3;
if (z == 168)
putc(027);

z = y >> 3;
if (z == 4)
putc(027);
/* test logical and with two variables */
*(volatile int *)(01011) = x & y;

/* test logical and with an immediate value */
*(volatile int *)(01012) = x & 12;

/* test logical or with two variables */
*(volatile int *)(01013) = x | y;

/* test logical or with an immediate value */
*(volatile int *)(01014) = x | 12;

/* test logical xor with two variables */
*(volatile int *)(01015) = x ^ y;

/* test logical xor with an immediate value */
*(volatile int *)(01016) = x ^ 12;

/* test shift left */
*(volatile int *)(01016) = x << 6;

/* test shift right */
*(volatile int *)(01017) = x >> 3;

asm volatile ("hlt");
__builtin_unreachable();
}

static void putc(int c) {
asm volatile ("lio %0" : : "r"(c) : "$io");
asm volatile ("tyo");
}
2 changes: 1 addition & 1 deletion pdp1-gcc
73 changes: 62 additions & 11 deletions tests/test.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set timeout 1

proc run_test { demo_name expected_output } {
proc run_print_test { demo_name expected_output } {
spawn "pdp1"
send "set cpu mdv\n"
send "attach ptr $demo_name/main.rim\n"
Expand All @@ -23,13 +23,64 @@ proc run_test { demo_name expected_output } {
send "quit\n"
}

run_test "asm-print-x" "x"
run_test "asm-hello-world" "hello, world"
run_test "c-print-x" "xx"
run_test "c-print-struct" "xyz"
run_test "c-print-array" "xyzxyzxyzxyz"
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,"
proc run_memory_test { demo_name expected_output expected_memory } {
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" }
"sim>" { pass "$demo_name: Successfully loaded the RIM tape file\n" }
default { fail "$demo_name: Test failed unexpectedly\n" }
}

send "boot ptr\n"
expect {
"$expected_output" { pass "$demo_name: Encountered expected output\n" }
default { fail "$demo_name: Didn't encounter expected output\n" }
}
expect {
-re "HALT instruction,.*" { pass "$demo_name: Program finished running normally\n" }
default { fail "$demo_name: Program halted unexpectedly\n" }
}

dict for {addr val} $expected_memory {
send "examine $addr\n"
expect {
-re "$addr\:.*$val" { pass "$demo_name: Correct value $val for address $addr\n" }
default { fail "$demo_name: Incorrect value for address $addr\n" }
}
}

send "quit\n"
}

# run tests that print something to stdout
run_print_test "asm-print-x" "x"
run_print_test "asm-hello-world" "hello, world"
run_print_test "c-print-x" "xx"
run_print_test "c-print-struct" "xyz"
run_print_test "c-print-array" "xyzxyzxyzxyz"
run_print_test "c-print-for-loop" "xxxxyxxx"
run_print_test "c-test-comparison" "aceg"
run_print_test "c-test-recursion" "xxxxxxxxxxxxxxxxxxxx"
run_print_test "c-print-fibonacci" "0,1,1,2,3,5,8,31,12,43,55,98,441,332,773,016,789,7951,4852,1814,"

# run tests that set values in memory
set mem [dict create "1000" "000072" \
"1001" "000051" \
"1002" "000026" \
"1003" "000020" \
"1004" "000033" \
"1005" "001411" \
"1006" "000001" \
"1007" "000020" \
"1010" "777752" \
"1011" "000005" \
"1012" "000004" \
"1013" "000065" \
"1014" "000035" \
"1015" "000060" \
"1016" "002500" \
"1017" "000002"]

run_memory_test "c-test-arith" "" $mem

0 comments on commit 669911c

Please sign in to comment.