%begin {SCSI synchroniser program}
%include "moose:mouse.inc"
%option "-low"
%include "ncr5380.inc"
%externalintegerfnspec testsymbol
%externalroutinespec phex(%integer x)
%externalroutinespec phex2(%integer x)
%record(dictfm)%name d
%recordformat f(%record(semaphorefm)wait,sync,%string(255)iwait,twait)
%record(f)sync
%integer x
%integer c0i=0,c1i=0

%routine show(%integer n)
%record(ncr5380)%name chip

  %routine ph(%integer x)
    phex2(x>>8)
  %end

  chip == chip0
  n = n&1
  chip == chip1 %unless n=0
  printstring("Chip "); printsymbol(n+'0'); printstring(": ")
  ph(chip_csd); space; ph(chip_icr); space; ph(chip_mode); space
  ph(chip_tcr); space; ph(chip_cbs); space; ph(chip_bsr); newline
%end

  d == poa_logdict
  %stopif d==nil
  printstring("SCSI synchroniser starting"); newline
  d == d_alt %while d_alt##nil
  x = make entry("scsi_sync",d)
  setup semaphore(sync_wait); setup semaphore(sync_sync)
  sync_iwait = ""; sync_twait = ""
  integer(x) = addr(sync)
  run process("rwt_s:scsiinit.mob",20000,-1)
  run process("rwt_s:scsitarg.mob",20000,-1)
  printstring("Startup done"); newline
  semaphorewait(sync_sync)
  %cycle
    semaphorewait(sync_sync)
    x = (chip0_bsr&bsrirq)!!c0i
    %if x#0 %start
      c0i = c0i!!x
      printstring("Chip 0 interrupt")
      %if c0i=0 %then printstring(" cleared") %else printstring("ing")
      newline
    %finish
    x = (chip1_bsr&bsrirq)!!c1i
    %if x#0 %start
      c1i = c1i!!x
      printstring("Chip 1 interrupt")
      %if c1i=0 %then printstring(" cleared") %else printstring("ing")
      newline
    %finish
!   %cycle
      x = testsymbol
      %if x='Y'-64 %start
        *move.w #16_2700,d0
        *trap #0
        *reset
        *move.l 0,sp
        *move.l 4,-(sp)
        *rts
      %finish
      %if x='?' %start
        show(0); show(1)
        printstring("I: "); printstring(sync_iwait); newline
        printstring("T: "); printstring(sync_twait); newline
      %finish
!   %repeatuntil x>=0
    signalsemaphore(sync_wait)
  %repeat
%end
