_average:

;---------------------------------- save context

    PUSH    ACCAH
    PUSH    ACCAL
    PUSH    ACCBH
    PUSH    ACCBL
    PUSH    CORCON    

; averagedArrays = (1/BUFFER_SIZE) * ( sum arrayA[]     + sum arrayB[]     )
;             W0 = (1/W2)          * ( sum W0[0:(W2-1)] + sum W1[0:(W2-1)] )

;---------------------------------- configure, clear accumulators and prefetch X & Y addresses    

    MOV     #0x10F1,W3;             ;config accumulators for unsigned integer, 9.31 saturation, SATDW
    MOV     W3,CORCON                   

    CLR     A                       ;clear A for use as arrayA[] buffer
    CLR     B                       ;clear B for use as arrayB[] buffer

;---------------------------------- sum arrayA values into ACCA, sum arrayB values into ACCB

    DEC     W2,W2                   ;W2 = buffer size; adjust by -1 for use as DO loop counter

    DO      W2,end                  ;execute loop 64 times (0:64)
            ADD [W0++],A            ;W0 = arrayA[]; sum into A, post increment W0
    end:    ADD [W1++],B            ;W1 = arrayB[]; sum into B, post-increment W1

;---------------------------------- add the results together into A, divide by 64, round and store into W0

    ADD     A                       ;add A to B, save result in A
    SAC.R   A,#6,W0                 ;right shift A by #6 (divides by 64),store as rounded result into W0

;---------------------------------- restore context and return

    POP     CORCON
    POP     ACCBL
    POP     ACCBH
    POP     ACCAL
    POP     ACCAH

    RETURN

    .end