From d2511c36c0e88c9e9535266b3cca870444f689e1 Mon Sep 17 00:00:00 2001 From: Warren Toomey Date: Sat, 27 Feb 2016 08:05:52 +1000 Subject: [PATCH] I'm trying to bring my wktcat.s file into line with the real one. I've refactored tools/a7out to have a dprintf() function to printf only on debugging. I still need to rewrite the command-line arg storage structure. --- tools/a7out | 74 +++++++++++++++++++++++++------------------------- tools/wktcat.s | 26 +++++++++++------- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/tools/a7out b/tools/a7out index 986b645..b349f35 100755 --- a/tools/a7out +++ b/tools/a7out @@ -103,7 +103,7 @@ sub set_arguments { # No arguments, set the 017777 pointer to 017776 which is NULL if (@ARGV==0) { $Mem[ 017777 ] = 017776; - print( STDERR "No arguments, so NULL 017777 pointer\n") if ($debug); + dprintf("No arguments, so NULL 017777 pointer\n"); return; } @@ -151,10 +151,8 @@ sub simulate { # Work out what any indirect address would be my $indaddr= ($indirect) ? $Mem[$addr] & 017777 : $addr; - printf( STDERR - "PC %06o: instr %06o, op %03o, ind %o, addr %06o ind %06o\n", - $PC, $instruction, $opcode, $indirect, $addr, $indaddr ) - if ($debug); + dprintf( "PC %06o: instr %06o, op %03o, ind %o, addr %06o ind %06o\n", + $PC, $instruction, $opcode, $indirect, $addr, $indaddr ); # Simulate the instruction. Each subroutine updates the $PC if ( defined( $Oplist{$opcode} ) ) { @@ -162,7 +160,7 @@ sub simulate { } else { printf( STDERR "Unknown instruction 0%o at location 0%o\n", $instruction, $PC ); - die("\n"); + exit(1); } } } @@ -177,9 +175,8 @@ sub dump_memory { # Load AC sub lac { my ( $instruction, $addr, $indaddr ) = @_; - printf( STDERR "PC %06o: lac %05o (value %06o) into AC\n", - $PC, $indaddr, $Mem[$indaddr] ) - if ($debug); + dprintf( "PC %06o: lac %05o (value %06o) into AC\n", + $PC, $indaddr, $Mem[$indaddr] ); $AC = $Mem[$indaddr]; $PC++; } @@ -187,27 +184,25 @@ sub lac { # Deposit AC sub dac { my ( $instruction, $addr, $indaddr ) = @_; - printf( STDERR "PC %06o: dac AC (value %06o) into %05o\n", - $PC, $AC, $indaddr ) - if ($debug); - $Mem[$indaddr] = $AC; + dprintf( "PC %06o: dac AC (value %06o) into %05o\n", + $PC, $AC, $indaddr ); + $Mem[$indaddr] = $AC; $PC++; } # Add to AC sub tad { my ( $instruction, $addr, $indaddr ) = @_; - printf( STDERR "PC %06o: tac AC (value %06o) from addr %05o\n", - $PC, $AC, $indaddr ) - if ($debug); - $AC+= $Mem[$indaddr]; + dprintf( "PC %06o: tac AC (value %06o) from addr %05o\n", + $PC, $AC, $indaddr ); + $AC+= $Mem[$indaddr]; $PC++; } # Jump sub jmp { my ( $instruction, $addr, $indaddr ) = @_; - printf( STDERR "PC %06o: jmp %06o\n", $PC, $indaddr ) if ($debug); + dprintf( "PC %06o: jmp %06o\n", $PC, $indaddr ); $PC = $indaddr; } @@ -223,18 +218,18 @@ sub special { exit(1); } if ( $instruction == 0741100 ) { # spa: skip on positive AC - printf( STDERR "PC %06o: spa AC %06o\n", $PC, $AC ) if ($debug); + dprintf( "PC %06o: spa AC %06o\n", $PC, $AC ); # Because we are dealing with 18 bits, compare the range $PC += ( ($AC >= 0) && ($AC < 0400000) ) ? 2 : 1; return; } if ( $instruction == 0741200 ) { # sna: skip on non-zero AC - printf( STDERR "PC %06o: sna AC %06o\n", $PC, $AC ) if ($debug); + dprintf( "PC %06o: sna AC %06o\n", $PC, $AC ); $PC += ( $AC != 0 ) ? 2 : 1; return; } if ( $instruction == 0740200 ) { # sza: skip on zero AC - printf( STDERR "PC %06o: sza AC %06o\n", $PC, $AC ) if ($debug); + dprintf( "PC %06o: sza AC %06o\n", $PC, $AC ); $PC += ( $AC == 0 ) ? 2 : 1; return; } @@ -260,13 +255,13 @@ sub iot { $Syscallist{$addr}->(); } else { printf( STDERR "PC %06o: Unknown syscall %d\n", $PC, $addr ); - die("\n"); + exit(1); } } # Exit system call sub sys_exit { - printf( STDERR "PC %06o: exit system call\n", $PC ) if ($debug); + dprintf( "PC %06o: exit system call\n", $PC ); exit(0); } @@ -275,14 +270,14 @@ sub sys_close { # AC is the file descriptor my $fd = $AC; - printf( STDERR "PC %06o: close: closing fd %d\n", $PC, $fd ) if ($debug); + dprintf( "PC %06o: close: closing fd %d\n", $PC, $fd ); # Bump up the PC $PC += 1; # That filehandle is not open, set an error -1 in octal if ( !defined( $FD[$fd] ) ) { - print( STDERR "close: fd $fd is not open\n") if ($debug); + dprint( "close: fd $fd is not open\n"); $AC = 0777777; return; } @@ -307,7 +302,7 @@ sub sys_open { # Convert this to a sensible ASCII filename my $filename = mem2string($start); - printf( STDERR "PC %06o: open: file %s\n", $PC, $filename ) if ($debug); + dprintf( "PC %06o: open: file %s\n", $PC, $filename ); # Open the file if ( open( my $FH, "<", $filename ) ) { @@ -323,7 +318,7 @@ sub sys_open { return; } else { # No filehandle, so it's an error - print( STDERR "open failed: $!\n") if ($debug); + dprintf( "open failed: $!\n"); $AC = 0777777; return; } @@ -340,17 +335,17 @@ sub sys_read { my $fd = $AC; my $start = $Mem[ $PC + 1 ]; my $count = $Mem[ $PC + 2 ]; - my $end = $start + $count - 1; - printf( STDERR "PC %06o: read: %d words into %o from fd %d\n", - $PC, $count, $start, $fd ) - if ($debug); + my $end = ($start + $count - 1) & 017777; + die("sys_read: bad start/end addresses $start $end\n") if ($end < $start); + dprintf( "PC %06o: read: %d words into %o from fd %d\n", + $PC, $count, $start, $fd ); # Bump up the PC $PC += 3; # That filehandle is not open, set an error -1 in octal if ( !defined( $FD[$fd] ) ) { - print( STDERR "read: fd $fd is not open\n") if ($debug); + dprint( "read: fd $fd is not open\n"); $AC = 0777777; return; } @@ -393,17 +388,17 @@ sub sys_write { my $fd = $AC; my $start = $Mem[ $PC + 1 ]; my $count = $Mem[ $PC + 2 ]; - my $end = $start + $count - 1; - printf( STDERR "PC %06o: write: %d words from %o to fd %d\n", - $PC, $count, $start, $fd ) - if ($debug); + my $end = ($start + $count - 1) & 017777; + die("sys_write: bad start/end addresses $start $end\n") if ($end < $start); + dprintf( "PC %06o: write: %d words from %o to fd %d\n", + $PC, $count, $start, $fd ); # Bump up the PC $PC += 3; # That filehandle is not open, set an error -1 in octal if ( !defined( $FD[$fd] ) ) { - print( STDERR "write: fd $fd is not open\n") if ($debug); + dprint( "write: fd $fd is not open\n"); $AC = 0777777; return; } @@ -487,3 +482,8 @@ sub string2mem { } return($base); } + +# Print out debug messages +sub dprintf { + printf( STDERR @_) if ($debug); +} diff --git a/tools/wktcat.s b/tools/wktcat.s index a60c7ed..ef11a35 100644 --- a/tools/wktcat.s +++ b/tools/wktcat.s @@ -44,12 +44,12 @@ stdinout: " This section opens files, and copies their contents to standard output catfiles: " We start with AC pointing to an argument. Save it at label 1f - dac 1f + dac name " Open the file and get the fd into AC - sys open; 1:0; 0; 0 + sys open; name:0; 0; 0 spa - jmp noopen " Bad fd, exit with an error message + jmp badfile " Bad fd, exit with an error message dac fd " Save the file descriptor fileloop: @@ -91,14 +91,19 @@ end: " exit sys exit -noopen: - " Print an "err open" string and exit - lac d1 - sys write; noopenstr; 5 - sys exit -noopenstr: - ;;;012000 +" This code comes from the real cat.s +badfile: + lac name " Get the pointer to the filename + dac 1f " Store it in 1f below + lac d8 " Load fd 8 which is stderr + sys write; 1:0; 4 " Write the name, max 4 words + lac d8 " Then write " ?\n" + sys write; 1f; 2 + sys exit " and exit + +1: 040; 077012 + error: " Print an "err read" string and exit @@ -112,6 +117,7 @@ noreadstr: fd: 0 " fd of the open file d0: 0 " Constants 0 and 1 d1: 1 +d8: 8 " stderr seems to have fd 8 " Input buffer for read buf: 0; 0; 0; 0; 0