I've got a very simple cat program working, which reads from stdin and
sends it to stdout.
This commit is contained in:
+75
-32
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
#
|
#
|
||||||
# A7out: user-mode simulator for PDP-7 Unix applications
|
# a7out: user-mode simulator for PDP-7 Unix applications
|
||||||
#
|
#
|
||||||
# (c) 2016 Warren Toomey, GPL3
|
# (c) 2016 Warren Toomey, GPL3
|
||||||
#
|
#
|
||||||
@@ -74,6 +74,7 @@ sub simulate {
|
|||||||
my %Oplist = (
|
my %Oplist = (
|
||||||
oct("004") => \&dac,
|
oct("004") => \&dac,
|
||||||
oct("020") => \&lac,
|
oct("020") => \&lac,
|
||||||
|
oct("060") => \&jmp,
|
||||||
oct("070") => \&iot,
|
oct("070") => \&iot,
|
||||||
oct("074") => \&special,
|
oct("074") => \&special,
|
||||||
);
|
);
|
||||||
@@ -86,16 +87,16 @@ sub simulate {
|
|||||||
my $opcode = ( $instruction >> 12 ) & 074;
|
my $opcode = ( $instruction >> 12 ) & 074;
|
||||||
my $indirect = ( $instruction >> 13 ) & 1;
|
my $indirect = ( $instruction >> 13 ) & 1;
|
||||||
my $addr = $instruction & 017777;
|
my $addr = $instruction & 017777;
|
||||||
#printf( "PC %06o: instruction %08o, op %03o, ind %o, addr %06o\n",
|
printf( STDERR
|
||||||
# $PC, $instruction, $opcode, $indirect, $addr )
|
"PC %06o: instruction %08o, op %03o, ind %o, addr %06o\n",
|
||||||
# if ($debug);
|
$PC, $instruction, $opcode, $indirect, $addr )
|
||||||
|
if ($debug);
|
||||||
|
|
||||||
# Simulate the instruction. Each subroutine updates the $PC
|
# Simulate the instruction. Each subroutine updates the $PC
|
||||||
if ( defined( $Oplist{$opcode} ) ) {
|
if ( defined( $Oplist{$opcode} ) ) {
|
||||||
$Oplist{$opcode}->( $instruction, $indirect, $addr );
|
$Oplist{$opcode}->( $instruction, $indirect, $addr );
|
||||||
}
|
} else {
|
||||||
else {
|
printf( STDERR "Unknown instruction 0%o at location 0%o\n",
|
||||||
printf( "Unknown instruction 0%o at location 0%o\n",
|
|
||||||
$instruction, $PC );
|
$instruction, $PC );
|
||||||
die("\n");
|
die("\n");
|
||||||
}
|
}
|
||||||
@@ -105,14 +106,14 @@ sub simulate {
|
|||||||
# Debug code: dump memory contents
|
# Debug code: dump memory contents
|
||||||
sub dump_memory {
|
sub dump_memory {
|
||||||
foreach my $i ( 0 .. 017777 ) {
|
foreach my $i ( 0 .. 017777 ) {
|
||||||
printf( "%06o: %08o\n", $i, $Mem[$i] ) if ( $Mem[$i] != 0 );
|
printf( STDERR "%06o: %08o\n", $i, $Mem[$i] ) if ( $Mem[$i] != 0 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Load AC
|
# Load AC
|
||||||
sub lac {
|
sub lac {
|
||||||
my ( $instruction, $indirect, $addr ) = @_;
|
my ( $instruction, $indirect, $addr ) = @_;
|
||||||
printf( "PC %06o: lac %05o (value %08o) into AC\n",
|
printf( STDERR "PC %06o: lac %05o (value %08o) into AC\n",
|
||||||
$PC, $addr, $Mem[$addr] )
|
$PC, $addr, $Mem[$addr] )
|
||||||
if ($debug);
|
if ($debug);
|
||||||
$AC = ($indirect) ? $Mem[ $Mem[$addr] & 017777 ] : $Mem[$addr];
|
$AC = ($indirect) ? $Mem[ $Mem[$addr] & 017777 ] : $Mem[$addr];
|
||||||
@@ -122,7 +123,7 @@ sub lac {
|
|||||||
# Deposit AC
|
# Deposit AC
|
||||||
sub dac {
|
sub dac {
|
||||||
my ( $instruction, $indirect, $addr ) = @_;
|
my ( $instruction, $indirect, $addr ) = @_;
|
||||||
printf( "PC %06o: dac AC (value %08o) into %05o into AC\n",
|
printf( STDERR "PC %06o: dac AC (value %08o) into %05o into AC\n",
|
||||||
$PC, $AC, $addr )
|
$PC, $AC, $addr )
|
||||||
if ($debug);
|
if ($debug);
|
||||||
if ($indirect) {
|
if ($indirect) {
|
||||||
@@ -133,6 +134,13 @@ sub dac {
|
|||||||
$PC++;
|
$PC++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Jump
|
||||||
|
sub jmp {
|
||||||
|
my ( $instruction, $indirect, $addr ) = @_;
|
||||||
|
printf( STDERR "PC %06o: jmp %06o\n", $PC, $addr ) if ($debug);
|
||||||
|
$PC = $addr;
|
||||||
|
}
|
||||||
|
|
||||||
# Special instructions
|
# Special instructions
|
||||||
sub special {
|
sub special {
|
||||||
my $instruction = shift;
|
my $instruction = shift;
|
||||||
@@ -140,10 +148,22 @@ sub special {
|
|||||||
# Deal with each one in turn
|
# Deal with each one in turn
|
||||||
# hlt
|
# hlt
|
||||||
if ( $instruction == 0740040 ) {
|
if ( $instruction == 0740040 ) {
|
||||||
printf( "PC %06o: program halted\n", $PC );
|
printf( STDERR "PC %06o: program halted\n", $PC );
|
||||||
dump_memory() if ($debug);
|
dump_memory() if ($debug);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
if ( $instruction == 0741100 ) { # spa: skip on positive AC
|
||||||
|
printf( STDERR "PC %06o: spa\n", $PC ) if ($debug);
|
||||||
|
$PC += ( $AC >= 0 ) ? 2 : 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ( $instruction == 0741200 ) { # sna: skip on non-zero AC
|
||||||
|
printf( STDERR "PC %06o: sna\n", $PC ) if ($debug);
|
||||||
|
$PC += ( $AC != 0 ) ? 2 : 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printf( STDERR "PC %06o: unknown instruction %08o\n", $PC, $instruction );
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
# I/O transfer: used for system calls
|
# I/O transfer: used for system calls
|
||||||
@@ -163,38 +183,42 @@ sub iot {
|
|||||||
if ( defined( $Syscallist{$addr} ) ) {
|
if ( defined( $Syscallist{$addr} ) ) {
|
||||||
$Syscallist{$addr}->();
|
$Syscallist{$addr}->();
|
||||||
} else {
|
} else {
|
||||||
printf( "PC %06o: Unknown syscall %d\n", $PC, $addr );
|
printf( STDERR "PC %06o: Unknown syscall %d\n", $PC, $addr );
|
||||||
die("\n");
|
die("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Exit system call
|
# Exit system call
|
||||||
sub sys_exit {
|
sub sys_exit {
|
||||||
printf("PC %06o: exit system call\n", $PC) if ($debug);
|
printf( STDERR "PC %06o: exit system call\n", $PC ) if ($debug);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
# Close system call
|
# Close system call
|
||||||
sub sys_close {
|
sub sys_close {
|
||||||
|
|
||||||
# AC is the file descriptor
|
# AC is the file descriptor
|
||||||
my $fd = $AC;
|
my $fd = $AC;
|
||||||
printf("PC %06o: close: closing fd %d\n", $PC, $fd) if ($debug);
|
printf( STDERR "PC %06o: close: closing fd %d\n", $PC, $fd ) if ($debug);
|
||||||
|
|
||||||
# Bump up the PC
|
# Bump up the PC
|
||||||
$PC += 1;
|
$PC += 1;
|
||||||
|
|
||||||
# That filehandle is not open, set an error -1 in octal
|
# That filehandle is not open, set an error -1 in octal
|
||||||
if ( !defined( $FD[$fd] ) ) {
|
if ( !defined( $FD[$fd] ) ) {
|
||||||
print("close: fd $fd is not open\n") if ($debug);
|
print( STDERR "close: fd $fd is not open\n") if ($debug);
|
||||||
$AC= 0777777; return;
|
$AC = 0777777;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
close( $FD[$fd] );
|
close( $FD[$fd] );
|
||||||
$FD[$fd] = undef;
|
$FD[$fd] = undef;
|
||||||
$AC=0; return;
|
$AC = 0;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Open system call
|
# Open system call
|
||||||
sub sys_open {
|
sub sys_open {
|
||||||
|
|
||||||
# Open seems to have arguments: PC+1 has a pointer to the filename,
|
# Open seems to have arguments: PC+1 has a pointer to the filename,
|
||||||
# PC+2 and PC+3 I don't know yet, probably read/write and mask?
|
# PC+2 and PC+3 I don't know yet, probably read/write and mask?
|
||||||
# AC is the opened fd on success, or -1 on error
|
# AC is the opened fd on success, or -1 on error
|
||||||
@@ -207,27 +231,32 @@ sub sys_open {
|
|||||||
|
|
||||||
# Convert this to a sensible ASCII filename
|
# Convert this to a sensible ASCII filename
|
||||||
my $filename = mem2string($start);
|
my $filename = mem2string($start);
|
||||||
printf("PC %06o: open: file %s\n", $PC, $filename) if ($debug);
|
printf( STDERR "PC %06o: open: file %s\n", $PC, $filename ) if ($debug);
|
||||||
|
|
||||||
# Open the file
|
# Open the file
|
||||||
if ( open( my $FH, "<", $filename ) ) {
|
if ( open( my $FH, "<", $filename ) ) {
|
||||||
|
|
||||||
# Find a place in the @FD array to store this filehandle. 99 is arbitrary
|
# Find a place in the @FD array to store this filehandle. 99 is arbitrary
|
||||||
my $fd;
|
my $fd;
|
||||||
foreach $fd ( 0 .. 99 ) {
|
foreach $fd ( 0 .. 99 ) {
|
||||||
if ( !defined( $FD[$fd] ) ) {
|
if ( !defined( $FD[$fd] ) ) {
|
||||||
$FD[$fd]= $FH; last;
|
$FD[$fd] = $FH;
|
||||||
|
last;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$AC=$fd; return;
|
$AC = $fd;
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
# No filehandle, so it's an error
|
# No filehandle, so it's an error
|
||||||
print("open failed: $!\n") if ($debug);
|
print( STDERR "open failed: $!\n") if ($debug);
|
||||||
$AC= 0777777; return;
|
$AC = 0777777;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Read system call
|
# Read system call
|
||||||
sub sys_read {
|
sub sys_read {
|
||||||
|
|
||||||
# Read seems to have arguments: AC is the file descriptor, PC+1 is
|
# Read seems to have arguments: AC is the file descriptor, PC+1 is
|
||||||
# the pointer to the buffer and PC+2 is the number of words to read.
|
# the pointer to the buffer and PC+2 is the number of words to read.
|
||||||
# Return the number of words read in AC on success, or -1 on error.
|
# Return the number of words read in AC on success, or -1 on error.
|
||||||
@@ -237,39 +266,46 @@ sub sys_read {
|
|||||||
my $start = $Mem[ $PC + 1 ];
|
my $start = $Mem[ $PC + 1 ];
|
||||||
my $count = $Mem[ $PC + 2 ];
|
my $count = $Mem[ $PC + 2 ];
|
||||||
my $end = $start + $count - 1;
|
my $end = $start + $count - 1;
|
||||||
printf("PC %06o: read: %d words from %o from fd %d\n", $PC, $count, $start, $fd) if ($debug);
|
printf( STDERR "PC %06o: read: %d words into %o from fd %d\n",
|
||||||
|
$PC, $count, $start, $fd )
|
||||||
|
if ($debug);
|
||||||
|
|
||||||
# Bump up the PC
|
# Bump up the PC
|
||||||
$PC += 3;
|
$PC += 3;
|
||||||
|
|
||||||
# That filehandle is not open, set an error -1 in octal
|
# That filehandle is not open, set an error -1 in octal
|
||||||
if ( !defined( $FD[$fd] ) ) {
|
if ( !defined( $FD[$fd] ) ) {
|
||||||
print("read: fd $fd is not open\n") if ($debug);
|
print( STDERR "read: fd $fd is not open\n") if ($debug);
|
||||||
$AC= 0777777; return;
|
$AC = 0777777;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Read each word in
|
# Read each word in
|
||||||
my $FH = $FD[$fd];
|
my $FH = $FD[$fd];
|
||||||
$count = 0;
|
$count = 0;
|
||||||
foreach my $addr ( $start .. $end ) {
|
foreach my $addr ( $start .. $end ) {
|
||||||
|
|
||||||
# It's a terminal, so convert from ASCII
|
# It's a terminal, so convert from ASCII
|
||||||
if ( -t $FH ) {
|
if ( -t $FH ) {
|
||||||
my $c1 = getc($FH);
|
my $c1 = getc($FH);
|
||||||
last if ( !defined($c1) ); # No character, leave the loop
|
last if ( !defined($c1) ); # No character, leave the loop
|
||||||
my $c2 = getc($FH) || ""; # No character, make it a NUL
|
my $c2 = getc($FH) || ""; # No character, make it a NUL
|
||||||
$Mem[$addr]= (ord($c1) << 9) | ord($c2); # Pack both into one word
|
$Mem[$addr] =
|
||||||
|
( ord($c1) << 9 ) | ord($c2); # Pack both into one word
|
||||||
$count++;
|
$count++;
|
||||||
} else {
|
} else {
|
||||||
# otherwise (for now) read in one line and convert to octal
|
# otherwise (for now) read in one line and convert to octal
|
||||||
my $line = <$FH>;
|
my $line = <$FH>;
|
||||||
chomp($line);
|
|
||||||
last if ( !defined($line) ); # No line, leave the loop
|
last if ( !defined($line) ); # No line, leave the loop
|
||||||
|
chomp($line);
|
||||||
$Mem[$addr] = oct($line) & 0777777;
|
$Mem[$addr] = oct($line) & 0777777;
|
||||||
$count++;
|
$count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# No error
|
# No error
|
||||||
$AC= $count; return;
|
$AC = $count;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Write system call
|
# Write system call
|
||||||
@@ -283,20 +319,24 @@ sub sys_write {
|
|||||||
my $start = $Mem[ $PC + 1 ];
|
my $start = $Mem[ $PC + 1 ];
|
||||||
my $count = $Mem[ $PC + 2 ];
|
my $count = $Mem[ $PC + 2 ];
|
||||||
my $end = $start + $count - 1;
|
my $end = $start + $count - 1;
|
||||||
printf("PC %06o: write: %d words from %o to fd %d\n", $PC, $count, $start, $fd) if ($debug);
|
printf( STDERR "PC %06o: write: %d words from %o to fd %d\n",
|
||||||
|
$PC, $count, $start, $fd )
|
||||||
|
if ($debug);
|
||||||
|
|
||||||
# Bump up the PC
|
# Bump up the PC
|
||||||
$PC += 3;
|
$PC += 3;
|
||||||
|
|
||||||
# That filehandle is not open, set an error -1 in octal
|
# That filehandle is not open, set an error -1 in octal
|
||||||
if ( !defined( $FD[$fd] ) ) {
|
if ( !defined( $FD[$fd] ) ) {
|
||||||
print("write: fd $fd is not open\n") if ($debug);
|
print( STDERR "write: fd $fd is not open\n") if ($debug);
|
||||||
$AC= 0777777; return;
|
$AC = 0777777;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Write each word out
|
# Write each word out
|
||||||
my $FH = $FD[$fd];
|
my $FH = $FD[$fd];
|
||||||
foreach my $addr ( $start .. $end ) {
|
foreach my $addr ( $start .. $end ) {
|
||||||
|
|
||||||
# It's a terminal, so convert to ASCII
|
# It's a terminal, so convert to ASCII
|
||||||
# otherwise (for now) print in octal
|
# otherwise (for now) print in octal
|
||||||
if ( -t $FH ) {
|
if ( -t $FH ) {
|
||||||
@@ -305,8 +345,10 @@ sub sys_write {
|
|||||||
printf( $FH "%06o\n", $Mem[$addr] );
|
printf( $FH "%06o\n", $Mem[$addr] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# No error
|
# No error
|
||||||
$AC= 0; return;
|
$AC = 0;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Convert an 18-bit word into two ASCII characters and return them.
|
# Convert an 18-bit word into two ASCII characters and return them.
|
||||||
@@ -329,6 +371,7 @@ sub mem2string {
|
|||||||
my $result = "";
|
my $result = "";
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
||||||
# Stop when the address leave the 8K word address space
|
# Stop when the address leave the 8K word address space
|
||||||
return ($result) if ( $addr > 017777 );
|
return ($result) if ( $addr > 017777 );
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
" Simple cat program: echo stdin to stdout until no more words to read
|
||||||
|
|
||||||
|
main:
|
||||||
|
" Read 5 words into the buffer from stdin
|
||||||
|
lac d0
|
||||||
|
sys read; buf; 5
|
||||||
|
spa " Skip if result was >= 0
|
||||||
|
jmp error " Result was -ve, so error result
|
||||||
|
sna " Skip if result was >0
|
||||||
|
jmp end " Result was zero, so nothing left to read
|
||||||
|
|
||||||
|
" Save the count of words read in
|
||||||
|
dac 1f
|
||||||
|
|
||||||
|
" Write 5 words from the buffer to stdout
|
||||||
|
lac d1
|
||||||
|
sys write; buf; 1:0
|
||||||
|
jmp main
|
||||||
|
|
||||||
|
error:
|
||||||
|
end:
|
||||||
|
" exit
|
||||||
|
sys exit
|
||||||
|
|
||||||
|
d0: 0
|
||||||
|
d1: 1
|
||||||
|
|
||||||
|
" Input buffer for read
|
||||||
|
buf: 0; 0; 0; 0; 0
|
||||||
Reference in New Issue
Block a user