DetectJapanesePaddle: ld bc,$0000 ; b = 0 for 256 loops; c = 0 for counting -: in a,($dc) bit 5,a ; Get TR input jr nz,+ ; If 1, don't increment c inc c +: djnz - ; repeat 256 times ld a,c ; inspect counter ; If the counter value is between $60 and $a0 (50% +/- 12.5% of 256) ; then set b=$ff else set b=0 ld b,$00 sub $60 jr c,_DetectionEnd cp $40 jr nc,_DetectionEnd dec b _DetectionEnd: ld a,b ret ; result in a ($ff if detected, 0 if not)