
                  org   40000

start             di

                  call  3435
                  ld    a,2
                  call  #1601

                  ld    (oldSP+1),sp

                  ld    hl,#c000
                  ld    a,#c1
_loop             ld    (hl),a
                  inc   l
                  jr    nz,_loop
                  inc   h
                  ld    (hl),a

                  ld    a,195
                  ld    (#c1c1),a

                  ld    bc,#00ff

_test_loop        ld    sp,temp_stack
                  push  bc
                  call  Test_IN_FF
                  pop   bc
                  cp    #7d         ; INs expected during frame with no contention applied on T1 of the IO
                  call  nz,out_Port ; output the port address high byte for ports where additional contention was applied by the ULA
                  djnz  _test_loop

                  di
oldSP             ld    sp,0
                  ld    iy,23610
                  ld    bc,0
                  ld    a,63
                  ld    i,a
                  im    1
                  ld    hl,#2758
                  exx
                  ei
                  ret

out_Port          proc
                  ld    a,b
                  call  print_hex_8
                  ld    a," "
                  rst   16
                  ld    a," "
                  rst   16
                  ret
                  endp


Test_IN_FF        ld    (stack),sp

                  ld    a,#c0
                  ld    i,a
                  im    2

                  exx

                  ld    hl,syncint1
                  ld    (#c1c2),hl

                  ei
                  halt

                ; enter int retrigger loop to sync with interrupt
syncint1          ei
                  ld    hl,0
                  ld    de,0
                  ld    bc,3325
                  ldir
                  nop

                  di

                  ; we have synchronised to framestart when we reach here

                  ld    hl,mainint
                  ld    (#c1c2),hl

                  ld    ix,mainint_1

                  ld    a,r   ; ensure remainder of frame mod 4 = 0
                  ei
                  halt

                  ; we're exactly 29 t-states into a frame upon reaching here (19 for INTACK, 10 for JP)
mainint           ei
                  jp    (ix)

mainint_1         ld    ix,mainint_2      ; exit point on next INTACK

                  exx
                  ld    e,0

_in_loop          in    a,(c)
                  inc   e                 ; count INs during this frame
                  jp    _in_loop

mainint_2         ld    sp,(stack)
                  ld    a,e
                  ret

print_hex_8       proc
                  push  af
                  rra
                  rra
                  rra
                  rra
                  call  print_hex_char
                  pop   af
                  call  print_hex_char
                  ret
                  endp

print_hex_char    proc
                  and   15
                  cp    10
                  sbc   a,#69
                  daa
                  rst   16
                  ret
                  endp

stack             dw    0

                  ds    64
temp_stack        dw    0

                  end   start
