Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Filip Kokosiński <filip.kokosinski@gmail.com>
  • Loading branch information
fkokosinski committed Jun 5, 2024
0 parents commit 8947f85
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/pdp1-playground.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PDP-1 Playground
on: [push]
jobs:
Test-Toolchain:
runs-on: ubuntu-latest
steps:
- name: Checkout pdp1-playground
uses: actions/checkout@v3
with:
submodules: recursive

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y git build-essential flex bison texinfo libgmp3-dev libmpfr-dev libmpc-dev simh dejagnu
- name: Build binutils-gdb for PDP-1
run: |
cd pdp1-binutils
./configure --target=pdp1-elf --disable-sim
make -j $(nproc)
sudo make install
- name: Build gcc for PDP-1
run: |
cd pdp1-gcc
./configure --target=pdp1-elf --enable-languages=c --disable-bootstrap
make -j $(nproc) all-gcc
- name: Build samples
run: |
for sample in asm-*; do make -C $sample clean main.rim; done
for sample in c-*; do make -C $sample clean main.s main.rim; done
- name: Run tests
run: |
runtest --srcdir $(pwd)
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.bin
*.o
*.rim
*.s
*.log
*.sum
8 changes: 8 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[submodule "pdp1-binutils"]
path = pdp1-binutils
url = https://github.com/fkokosinski/binutils-gdb.git
branch = pdp1
[submodule "pdp1-gcc"]
path = pdp1-gcc
url = https://github.com/fkokosinski/gcc.git
branch = pdp1
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Filip Kokosiński

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# PDP-1 Playground

## Dependencies
```
sudo apt-get install git build-essential flex bison texinfo libgmp3-dev libmpfr-dev libmpc-dev simh dejagnu
```

## Building binutils
```
cd pdp1-binutils
./configure --target=pdp1-elf --disable-sim
make -j $(nproc)
sudo make install
```

## Building gcc
```
cd pdp1-gcc
./configure --target=pdp1-elf --enable-languages=c --disable-bootstrap
make -j $(nproc) all-gcc
```

## Building and running samples

```
cd c-print-for-loop
make main.s main.rim run
```

## Running tests
```
runtest --srcdir $(pwd)
```
1 change: 1 addition & 0 deletions asm-hello-world/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../common/common.mk
43 changes: 43 additions & 0 deletions asm-hello-world/main.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Simple asm program that prints out 'hello, world' on standard output */

.section .data
i:
.byte 0
ptr:
.byte str
len:
.byte end - str
str:
/* TOOD: use .ascii-derived FIODEC directive here */
.byte 070
.byte 065
.byte 043
.byte 043
.byte 046
.byte 033
.byte 000
.byte 026
.byte 046
.byte 051
.byte 043
.byte 064
end:

.section .text
.global _start
_start:
/* load IO register with *ptr and print it */
lio.i ptr
tyo

/* ptr++, i++ */
idx ptr
idx i

/* if (i == len) goto _start */
lac i
sas len
jmp _start

/* halt */
hlt
1 change: 1 addition & 0 deletions asm-print-x/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../common/common.mk
16 changes: 16 additions & 0 deletions asm-print-x/main.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Simple asm program that prints out 'x' on standard output */

.section .data
x:
/* 027 is 'x' in FIO DEC encoding */
.byte 027

.section .text
.global _start
_start:
/* load IO register with *x */
lio x
tyo

/* halt */
hlt
1 change: 1 addition & 0 deletions c-print-array/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../common/common.mk
23 changes: 23 additions & 0 deletions c-print-array/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
static void putc(int);
static int get_char(int);

static int x[] = {027, 030, 031};

void _start(void)
{
asm ("law 04000");
asm ("dac 209");
asm ("dac 208");

putc(x[0]);
putc(x[1]);
putc(x[2]);

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

static void putc(int c) {
asm ("lio %0" : : "r"(c));
asm ("tyo");
}
1 change: 1 addition & 0 deletions c-print-for-loop/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../common/common.mk
32 changes: 32 additions & 0 deletions c-print-for-loop/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
static void putc(int);
static int get_char(int);

void _start(void)
{
asm ("law 04000");
asm ("dac 209");
asm ("dac 208");

int c;

for (int i = 0; i != 8; i++) {
c = get_char(i);
putc(c);
}

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

static void putc(int c) {
asm ("lio %0" : : "r"(c));
asm ("tyo");
}

static int get_char(int c)
{
if (c != 4)
return 027;
else
return 030;
}
1 change: 1 addition & 0 deletions c-print-struct/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../common/common.mk
31 changes: 31 additions & 0 deletions c-print-struct/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
static void putc(int);

static struct {
int field1;
char field2;
short field3;
} x;

void _start(void)
{
asm ("law 04000");
asm ("dac 209");
asm ("dac 208");

x.field1 = 027;
x.field2 = 030;
x.field3 = 031;

putc(x.field1);
putc(x.field2);
putc(x.field3);


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

static void putc(int c) {
asm ("lio %0" : : "r"(c));
asm ("tyo");
}
1 change: 1 addition & 0 deletions c-print-x/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../common/common.mk
15 changes: 15 additions & 0 deletions c-print-x/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
int x = 027;

void _start(void)
{
asm ("law 04000");
asm ("dac 209");
asm ("dac 208");

asm ("lio %0" : : "r"(x));
asm ("tyo");

asm ("hlt");

__builtin_unreachable();
}
20 changes: 20 additions & 0 deletions common/common.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
OUTPUT_FORMAT("elf32-pdp1")
ENTRY(_start)

MEMORY
{
ram (rwx) : ORIGIN = 0x0, LENGTH = 64K
}

SECTIONS
{
.text :
{
*(.text)
} > ram

.data 0x400 :
{
*(.data)
} > ram
}
29 changes: 29 additions & 0 deletions common/common.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
APP_NAME ?= main
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
../pdp1-gcc/host-x86_64-pc-linux-gnu/gcc/cc1 $<

%.o: %.S
pdp1-elf-as $< -o $@

%.o: %.s
pdp1-elf-as $< -o $@

$(APP_NAME).bin: ../common/common.ld $(objects)
pdp1-elf-ld -o $@ -T $^

%.rim: %.bin
pdp1-elf-objcopy -O dec_rim $< $@

run: $(APP_NAME).rim
pdp1 ../common/startup.pdp1

clean:
rm -rf *.o *.s *.bin *.rim

.PHONY: default run clean
3 changes: 3 additions & 0 deletions common/startup.pdp1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
att ptr main.rim
boot ptr
quit
1 change: 1 addition & 0 deletions pdp1-binutils
Submodule pdp1-binutils added at 955d3f
1 change: 1 addition & 0 deletions pdp1-gcc
Submodule pdp1-gcc added at 6e88e4
30 changes: 30 additions & 0 deletions tests/test.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
set timeout 1

proc run_test { demo_name expected_output } {
spawn "pdp1"
send "attach ptr $demo_name/main.rim\n"
expect {
"PTR: creating new file" { fail "Didn't find the RIM tape file\n" }
"sim>" { pass "Successfully loaded the RIM tape file\n" }
default { fail "Test failed unexpectedly\n" }
}

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

send "quit\n"
}

run_test "asm-print-x" "x"
run_test "asm-hello-world" "hello, world"
run_test "c-print-x" "x"
run_test "c-print-struct" "xyz"
run_test "c-print-array" "xyz"
run_test "c-print-for-loop" "xxxxyxxx"

0 comments on commit 8947f85

Please sign in to comment.