FMDetection:
ld a,($c000)
or $04 ; Disable I/O chip
out ($3E),a
ld bc $0700 ; Counter (7 -> b), plus 0 -> c
-: ld a,b
and $01
out ($f2),a ; Output data in the form %00000??1 FM chip lots of times
ld e,a
in a,($f2)
and $07 ; Ignore high 5 bits,
cp e ; check low 3 are the same as what was written
jr nz,+
inc c ; c = # of times the test was passed
+: djnz -
ld a,c
cp $07 ; Check test was passed 7 times out of 7
jr z,+
xor a ; If not, result is 0
+: and $01 ; If so, result is 1
out ($f2),a ; Output result to FM chip
ld (HasFM),a ; Store result in HasFM
ld a,($c000)
out ($3e),a ; Turn I/O chip back on
ret