Prev: 54751 Up: Map Next: 54914
54855: Test Overlap
Used by the routine at TestEntityPair.
Tests whether two ranges overlap. The carry is set when they do, and L is the amount of the overlap. No overlap leaves the carry clear.
Input
HL The start of the first range
DE The start of the second range
B The size of the first range
C The size of the second range
Output
F The carry flag is set if they overlap
L The amount of the overlap
B is the size of the first range and C the size of the second. The result is the smaller amount by which they cross.
TestOverlap 54855 XOR A Subtract the second start from the first.
54856 SBC HL,DE
54858 JR C,TestOverlap_Before The second range starts first.
54860 OR H No overlap when the starts are a byte or more apart.
54861 RET NZ
54862 LD A,L No overlap when the gap is at least the first size.
54863 SUB B
54864 RET NC
54865 NEG The overlap is the first size less that gap.
54867 ADD HL,DE
54868 LD B,0
54870 ADD HL,BC
54871 SBC HL,DE
54873 LD H,0
54875 CP L Keep the smaller of the two amounts.
54876 JR NC,TestOverlap_Overlap
54878 LD L,A Return that overlap.
54879 RET
TestOverlap_Overlap 54880 LD H,255 Return the overlap that reached the other size.
54882 SCF
54883 RET
TestOverlap_Before 54884 LD A,B Take the first size and clear the carry.
54885 LD B,0
54887 AND A
54888 PUSH HL Remember where the ranges stood.
54889 ADC HL,BC Add the second size to the first start.
54891 LD C,A
54892 LD A,L
54893 POP HL
54894 RET NC No overlap when the second size falls short.
54895 JP Z,TestRangeOverlap_Reset The ranges meet on an edge.
54898 ADD HL,DE Measure how far the first range reaches back.
54899 EX DE,HL
54900 ADD HL,BC
54901 SBC HL,DE
54903 LD H,255
54905 CP L Keep the smaller of the two amounts.
54906 JR NC,TestOverlap_Inside
54908 LD L,A Return that overlap.
54909 RET
TestOverlap_Inside 54910 LD H,0 Return the overlap that reached the other size.
54912 SCF
54913 RET
Prev: 54751 Up: Map Next: 54914