;copySoftwareVectors
;-------------------
;Copies the software vector table, so we can track changes to the IO vectors
;
;Entry:
; R3 = pointer to VecPtrTab
;
;Exit:
; R0, R1 - corrupt

.copySoftwareVectors
 STMFD   R13!, {R10-R12, R14}
 ADR     R1, softwareVectors
 MOV     R0, #FirstVectorToTrack	;last vector to track
 ._L1
   LDR     R14, [R3, R0, LSL #2]	;get vector pointer
   LDMIA   R14, {R10, R11, R12}		;get vector values
   STR     R12, [R1], #4		;PC value
   STR     R11, [R1], #4		;R12 value

   ADD     R0, R0, #1
   CMP     R0, #LastVectorToTrack
 BLS     _L1
LDMFD   R13!, {R10-R12, PC}




;resetVectors
;------------
;Resets key vectors during image loading

.resetVectors
 STMFD   R13!, {R0-R7, R10-R12, R14}

 LDR     R5, VecPtrTab
 TEQ     R5, #0				;are we tracking vectors?
 LDMEQFD R13!, {R0-R7, R10-R12, PC}	;NO, exit

 ADR     R6, softwareVectorBitmap
 ADR     R4, softwareVectors - (8 * FirstVectorToTrack)

 MOV     R0, #Vector_CLIV
 BL      checkVector
 MOV     R0, #Vector_ByteV
 BL      checkVector
 MOV     R0, #Vector_WordV
 BL      checkVector
 MOV     R0, #Vector_FileV
 BL      checkVector
 MOV     R0, #Vector_ArgsV
 BL      checkVector
 MOV     R0, #Vector_BGetV
 BL      checkVector
 MOV     R0, #Vector_BPutV
 BL      checkVector
 MOV     R0, #Vector_GBPBV
 BL      checkVector
 MOV     R0, #Vector_FindV
 BL      checkVector

LDMFD   R13!, {R0-R7, R10-R12, PC}



;checkVector
;-----------
;checks if a vector has change and points it back to ths OS if it has
;
;Entry:
; R0 = vector
; R4 = pointer to our vector table copy
; R5 = pointer to vector table
; R6 = pointer to softwareVectorBitmap
;
;Exit:
; R1-R3   - corrupt
; R7      - corrupt
; R10-R12 - corrupt

.checkVector
 ADD     R7, R4, R0, LSL #3		;R7 = our copy of the vector in question

 LDRB    R1, [R6, R0, LSR #3]		;load relevant word from bitmap table
 AND     R2, R0, #%111			;bitmask
 MOV     R3, #1
 MOV     R3, R3, LSL R2			;R3 = bit to check
 TST     R1, R3				;have we already claimed the vector
 BNE     _Release			;YES, release it

 LDR     R3, [R5, R0, LSL #2]		;get pointer to vector values
 LDMIA   R3, {R10, R11, R12}		;read R10, R12, PC values

 LDMIA   R7, {R1, R2}			;load R12, PC values into registers
 TEQ     R1, R12			;is the PC value the same?
 MOVEQ   PC, R14			;YES, exit

 STR     R14, [R13, #-4]!
 SWI     XOS_AddToVector		;redirect it back to RISC OS

 LDRB    R1, [R6, R0, LSR #3]		;load relevant word from bitmap table
 AND     R2, R0, #%111			;bitmask
 MOV     R3, #1
 MOV     R3, R3, LSL R2			;R3 = bit to set
 ORR     R1, R1, R3
 STRB    R1, [R6, R0, LSR #3]		;update so we know we've claimed it
LDR     PC, [R13], #4


 ._Release
 LDMIA   R7, {R1, R2}			;load R12, PC values into registers

 STR     R14, [R13, #-4]!
 SWI     XOS_Release			;hand it back

 LDRB    R1, [R6, R0, LSR #3]		;load relevant word from bitmap table
 AND     R2, R0, #%111			;bitmask
 MOV     R3, #1
 MOV     R3, R3, LSL R2			;R3 = bit to clear
 BIC     R1, R1, R3
 STRB    R1, [R6, R0, LSR #3]		;update so we know we've claimed it
LDR     PC, [R13], #4
