include  "inc:util.imp"
include  "inc:fs.imp"

begin ; !CSD142B checkout aid, RWT Jan 86

@16_7fffc byte  status
@16_7fffd byte  data
@16_7ffff byte  control
constinteger  tbe=8,rd=4,rc=2,rbf=1
string (127)file=""
integer  hexflag=¬0

routine  load; !Load Station's Z80 (cutting filestore umbilical)
integer  zstart,zsize
integer  sym
  connectfile(file.".obj",0,zstart,zsize)
{}printsymbol('*')
  for  sym = 1,1,10000 cycle ; repeat 
{}printsymbol('*')
  status = 16_40; !Reset station
  for  sym = 1,1,1000 cycle ; repeat 
  sym = 0; status = sym; !Unreset (interrupts off)
  control = 16_03;       !Load command
  cycle 
  repeatuntil  status&tbe#0
  while  zsize>0 cycle 
    zsize = zsize-1
    sym = byteinteger(zstart); zstart = zstart+1
    data = sym
    cycle 
    repeatuntil  status&tbe#0
  repeat 
  control = 16_0b
  cycle 
  repeatuntil  status&tbe#0
  newline
end 

routine  restart
integer  i
  status = 16_40
  for  i = 1,1,1000 cycle ; repeat 
  status = 6
  etheropen(lsap,rdte<<8+rsap)
end 

routine  run
integer  sym

  integerfn  getnibble
  integer  n
    n = testsymbol until  n>=0
    n = n-32 if  n>='a'
    n = n-7 if  n>'9'
    result  = n&15
  end 

  integerfn  getbyte
  integer  n
    n = getnibble<<4
    result  = getnibble+n
  end 

  cycle 
    sym = testsymbol
    if  sym>=0 start 
      returnif  sym=0
if  sym=1 start 
  phex2(status);newline
finish 
      printstring("*Busy") and  newline andcontinue  if  status&tbe=0
      if  sym='_' start 
        data = getbyte
      elseif  sym='^'
        control = getbyte
      else 
        data = sym
      finish 
    finish 
    sym = status&(rc+rd+rbf)
    if  sym#0 start 
      if  sym=rc+rbf start 
        newline; printsymbol('^'); phex2(control)
      elseif  sym=rd+rbf
        sym = data
        if  hexflag=0 and  (sym=nl or  ' '<=sym<=126) start 
          printsymbol(sym)
        else 
          printsymbol('_'); phex2(sym)
        finish 
      else 
        newline; printstring("Inconsistent status "); phex2(sym)
      finish 
    finish 
  repeat 
end 

defineparam("File",file,pamnodefault)
definebooleanparams("Hex",hexflag,0)
processparameters(cliparam)

set terminal mode(nopage)
load
run
restart

endofprogram