; Use the GET directive to include register definitions as if typed here
        GET     h.RegNames

        AREA    |C$$Code|, CODE, READONLY


        EXPORT  compare_screens
        EXPORT  press_release_key


XOS_CallAVector        * &34


press_release_key ROUT
;on entry     r0  =  key no
;             r1  =  0 (release) or <>0 (press)
          STMFD     r13!, {r0-r12,r14}
          CMP       r1, #0
          MOV       r1, r0
          MOVEQ     r0, #1        ; release
          MOVNE     r0, #2        ; press
          MOV       r9, #&13      ; keyV
          SWI       XOS_CallAVector
          LDMFD     r13!, {r0-r12,pc}



compare_screens  ROUT
;compare two screenlines and find the first and last changed bytes
;on entry       r0 -> x0 on exit
;               r1 -> x1 on exit
;               r2 -> current screenline
;               r3 -> screenline in previous frame (updated on exit)
;             sp+0 =  bytes per line
;on exit        x0 == -1 if no diff
          STMFD     r13!, {r0-r12,r14}
          MVN       r0, #0
          MVN       r1, #0
          LDR       r4, [r13, #4*(14+0)]  ; bytes per line
          MOV       r5, r4
loop
          LDMIA    r2!,{r6-r9}
          LDMIA    r3,{r10-r12,r14}
          CMP      r6,r10
          CMPEQ    r7,r11
          CMPEQ    r8,r12
          CMPEQ    r9,r14
          BNE      not_eq
continue
          ADD       r3, r3, #16
          SUBS      r5, r5, #16
          BGT       loop

done
          CMP       r1, #0		; last not found
          ADDLT     r1, r0, #16		; so use end of 4 wrd blck at first
          BICLT     r1, r1, #15
          LDR       r2, [r13, #4*(14-14)]
          STR       r0,[r2]
          LDR       r2, [r13, #4*(14-13)]
          STR       r1,[r2]
          LDMFD     r13!, {r0-r12,pc}

not_eq
          STMIA     r3,{r6-r9}		; store it
          CMP       r0, #0              ; r0 < 0 -> first not found yet
          BLT       first_found

          SUB       r1, r4, r5
          ADD       r1, r1, #16

          CMP       r9, r14
          SUBEQ     r1, r1, #4
          CMPEQ     r8, r12
          SUBEQ     r1, r1, #4
          CMPEQ     r7, r11
          SUBEQ     r1, r1, #4

          ADD       r3, r3, #16
          SUBS      r5, r5, #16
          BGT       loop
          B         done


first_found
          SUB       r0, r4, r5

          CMP       r6, r10
          BNE       first_found2
          ADD       r0, r0, #4

          CMP       r7, r11
          BNE       first_found2
          ADD       r0, r0, #4

          CMP       r8, r12
          ADDEQ     r0, r0, #4

first_found2
          ADD       r3, r3, #16
          SUBS      r5, r5, #16
          BGT       loop
          B         done

        END
