%begin {SCSI initiator program}

%externalroutinespec phex(%integer x)
%externalroutinespec phex2(%integer x)

%include "moose:mouse.inc"
%include "oldscsi.inc"

%constinteger id=2,other=3
%recordformat f(%record(semaphorefm)wait,sync,%string(255)iwait,twait)
%record(f)%name sync
%integer x

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

%routine wait
  signalsemaphore(sync_sync)
  semaphorewait(sync_wait)
%end

! Set up

x = find entry("scsi_sync",poa_logdict)
sync == record(integer(x))
for == sync_iwait
chip == chip0

! Select target

select(id,other); printstring("Initiator selected target"); newline

! Command phase

chip_tcr = tcpcmd
for = "command phase"; wait %until ne(chip_bsr&bsrpha)
chip_icr = icrdata; offerbyte('1'); offerbyte('2')
printstring("Initiator sent command"); newline

! Output phase

chip_tcr = tcpout
for = "out phase"; wait %until ne(chip_bsr&bsrpha)
offerbyte('a'); offerbyte('b')
printstring("Initiator sent data"); newline

! Status phase

chip_tcr = tcpsta
for = "status phase"; wait %until ne(chip_bsr&bsrpha)
x = acceptbyte
printstring("Initiator received status "); printsymbol(x); newline

! Bus free phase

for = "bus free"; wait %until eq(chip_cbs&(cbsbsy!cbssel))
chip_mode = 0; chip_tcr = 0; x = chip_rpi

! Reselection phase

chip_icr = 0; chip_ser = 1<<(id+8)
for = "reselection"; wait %until ne(chip_bsr&bsrirq)
chip_icr = icrbsy
for = "Resel sel -"; wait %until eq(chip_cbs&cbssel)
chip_icr = 0
printstring("Initiator has been reselected"); newline
x = chip_rpi

! Input phase (DMA)

chip_tcr = tcpin
for = "input phase"; wait %until ne(chip_bsr&bsrpha)
chip_mode = modebsy!modedma; chip_sir = 255
x = dmaget %and printsymbol('R') %and printsymbol(x) %until x='9'; newline
chip_mode = modebsy; chip_icr = 0

! Status phase

chip_tcr = tcpsta
for = "status phase"; wait %until ne(chip_bsr&bsrpha)
x = acceptbyte
printstring("Initiator received status "); printsymbol(x); newline

! Bus free phase

for = "bus free"; wait %until eq(chip_cbs&(cbsbsy!cbssel))
chip_mode = 0; chip_tcr = 0; x = chip_rpi
printstring("Initiator finished"); newline

for = ""
wait %until 0=1

%end
