
; set console -n log=log.txt 
; set debug -n debug.txt
; set debug stdout
; set cpu debug=cmd;data;detail


; params: %1 source program card deck to compile
;         %2 input program data card deck (if empty, do not attach input card)
;         %3 output program data card deck (if empty, attach deck_out.dck file) 

set cpu 2k
set cpu StorageUnit

att cdp0 -n -q print.txt

; print FORTRANSIT source program 

echo
carddeck -q print %1

; load FORTRANSIT translator but do not execute it

echo *** 
echo *** Load FORTRANSIT translator deck into drum
echo *** 

att cdr1 -q -l fortransit/fortransit_translator.dck

d csw 70 1951 9999
d ar 8000
go

; attach and load reconstructed add function title deck 

att cdr1 -q -l fortransit/fortransit_addfn.dck

d csw 70 1951 9999
d ar 8000
go

echo *** 
echo *** Run FORTRANSIT translator
echo *** 

; Now put source cards in reader and start translator (phase I)

att cdr1 -q %1
set cdr1 wiring=fortransit

att cdp1 -n -q deck_it.dck
set cdp1 echo, print, wiring=fortransit

d csw 00 0000 1999
d ar 8000
go

; check if programed stop because an error is detected
if not prop=01 goto translate_ok1

echo 
echo *** (translation error code in Address Register AR)
echo

ex ar

echo 
echo *** (show last three source fortransit cards processed)
echo
carddeck -q echolast 3 cdr1

goto end

:translate_ok1

; check if programed stop because normal termination 
if prop=70 if ar=1951 goto translate_ok2
goto end

:translate_ok2

; separate last card (header card), and put it in the 
; top of deck

carddeck -q split -1 cdp1 deck_it.dck deck_it_header.dck
carddeck -q join  deck_it_header.dck deck_it.dck as deck_it.dck
det cdr1

; load IT compiler but do not execute it

echo *** 
echo *** Load IT compiler deck into drum
echo *** 

att cdr1 -q -l fortransit/it_compiler.dck

d csw 70 1951 9999
d ar 8000
go

echo *** 
echo *** Run IT compiler
echo *** 

; Now put IT source cards in reader and start compiler (phase II)

att cdr1 -q deck_it.dck
att cdp1 -n -q deck_out.dck

d csw 00 0000 1999
d ar 8000
go

; check if programed stop because an error is detected
if not prop=01 if not ar=1234 goto compile_ok1

echo 
echo *** (compilation error code in Upper ACC)
echo

ex accup
goto end

:compile_ok1

; check if programed stop because normal termination 
if prop=70 if ar=1951 goto compile_ok2
goto end

:compile_ok2

; separate first card (reservation card), from 
; generated soap source code deck

carddeck -q split 1 cdp1 deck_res.dck deck_soap.dck
det cdr1

; Load soap deck into drum (1 word per card format), but does not execute it

echo *** 
echo *** Load SOAP deck into drum
echo *** 

att cdr1 -q -l fortransit/soapII.dck

d csw 7019519999
d ar 8000
go

; create the phase III deck with
; - entry point cards for subroutines
; - reservation cards from phase II 
; - package reservation cards 
; - subroutines in symbolic SOAP format (if any)
; - rest of compilation output from phase II

carddeck -q join fortransit/pack_entry_src.txt deck_res.dck fortransit/pack_res2.txt deck_soap.dck as deck_soap.dck 

echo *** 
echo *** Run SOAP assembler
echo *** 

att cdr1 deck_soap.dck
set cdr1 wiring=soap

att cdp1 -n -q deck_out.dck
set cdp1 echo, print, wiring=soap

d ar 1000
go

; check if programed stop because an error is detected
if not prop=01 goto assemble_ok1

echo 
echo *** (assembling error code in AR)
echo

ex ar
goto end

:assemble_ok1

; check if programed stop because normal termination 
if prop=70 if ar=1951 goto assemble_ok2
goto end

:assemble_ok2

det cdr1
det cdp1

; create object program deck with
; - package cards 
; - assembled program cards from phase III

carddeck -q join fortransit/pack.dck deck_out.dck as deck_out.dck

echo *** 
echo *** Read Object Program Deck 
echo *** 

; Load assembled deck into drum
; -l switch allows to load the symbolic info so debug cmd info will show 
; soap source code read from card.

set cdr1 wiring=8WORD
att cdr1 -l deck_out.dck

d csw 7019519999
d ar 8000
go

; attach input and output data deck

det cdr1

if "%2" == "" goto run1
att cdr1 -q %2
set cdr1 wiring=fortransit
:run1

if "%3" != "" att cdp1 -n -q %3
if "%3" == "" att cdp1 -n -q deck_out.dck
set cdp1 echo, print, wiring=fortransit

; Now execute the loaded program object deck

echo *** 
echo *** Run Program
echo *** 

; set csw negative to activate conditional punch statement
; normal program termination will halt cpu at AR=9999

d csw -01 0000 9999
d ar 1999
go

; clean up

del deck_it_header.dck
del deck_it.dck
del deck_res.dck
del deck_soap.dck

:end
