; Sprite AND XOR Routine
; by Frank Schoep. Revision: 33

; Routine input:  D is the x coordinate
;                 E is the y coordinate
;                 BC as pointer to bit mask (8 bits) + sprite data (8 bits)
; Routine output: N/A
SPRANDXOR:
	push	de
	ld	a,d
	call	sprand
	inc	bc
	pop	de
	push	de
	ld	a,d
	add	a,8
	call	sprand                          ;display sprite AND XOR way                                        
        push	bc
        pop	de
        ld	l,31
        ld	h,0
        add	hl,de
        push	hl
        pop	bc
        pop	de
        push	de
        ld	a,d
        call	sprxor
        inc	bc
        pop	de
        ld	a,d
	add	a,8
	call	sprxor

        ret                                     ;return

SPRAND:
	push	bc
        push    bc              ; Save sprite address
        ld      hl,0            ; Do y*12
        ld      d,0
        add     hl,de
        add     hl,de
        add     hl,de
        add     hl,hl
        add     hl,hl
        ld      e,a
        srl     e
        srl     e
        srl     e
        add     hl,de
        ld      de,plotsscreen
        add     hl,de           ; Add address to graphbuf
        ld      b,00000111b     ; Get the remainder of x/8
        and     b
        or a
        jp      z,AALIGN
        pop     ix              ; ix->sprite
        ld      d,a             ; d=how many bits to shift each line
        ld      e,16             ; Line loop
ALILOP: ld      b,(ix+0)        ; Get sprite data
        ld      c,$ff           ; Changed into $FF because of AND bit mask!!!
        scf
        push    de
ASHLOP: rr      b               ;srl
        rr      c
        dec     d
        jp      nz,ASHLOP
        pop     de
        ld      a,b             ; Write line to graphbuf
        and     (hl)
        ld      (hl),a
        inc     hl
        ld      a,c
        and     (hl)
        ld      (hl),a
        ld      bc,11           ; Calculate next line address
        add     hl,bc
        inc     ix              ; Inc spritepointer
        inc	ix
        dec     e
        jp      nz,ALILOP       ; Next line
        jp      ADONE1
AALIGN:                         ; Blit an aligned sprite to graphbuf
        pop     de              ; de->sprite
        ld      b,16
AALOP1: ld      a,(de)
        and     (hl)
        ld      (hl),a
        inc     de
        inc	de		; Mod for 16*16
        push    bc
        ld      bc,12
        add     hl,bc
        pop     bc
        djnz    AALOP1
ADONE1:
	pop	bc
	ret
; Sprite XOR Routine
; originally written by MOVAX
; edited for SPRANDXOR by Frank Schoep
; Revision: 3

; Routine input:  A is the x coordinate
;                 E is the y coordinate
;                 BC as pointer to sprite data
; Routine output: N/A

SPRXOR:

        push bc ;for needed for SPRANDXOR

        push    bc              ; Save sprite address
        ld      hl,0            ; Do y*12
        ld      d,0
        add     hl,de
        add     hl,de
        add     hl,de
        add     hl,hl
        add     hl,hl
        ld      e,a
        srl     e
        srl     e
        srl     e
        add     hl,de
        ld      de,plotsscreen
        add     hl,de           ; Add address to graphbuf
        ld      b,00000111b     ; Get the remainder of x/8
        and     b
        or a
        jp      z,ALIGN
        pop     ix              ; ix->sprite
        ld      d,a             ; d=how many bits to shift each line
        ld      e,16             ; Line loop
LILOP:  ld      b,(ix+0)        ; Get sprite data
        ld      c,0             ; Shift loop
        push    de
SHLOP:  srl     b
        rr      c
        dec     d
        jp      nz,SHLOP
        pop     de
        ld      a,b             ; Write line to graphbuf
        xor     (hl)
        ld      (hl),a
        inc     hl

        ld      a,c
        xor     (hl)
        ld      (hl),a
        ld      bc,11           ; Calculate next line address
        add     hl,bc
        inc     ix              ; Inc spritepointer
	inc 	ix
        dec     e
        jp      nz,LILOP        ; Next line
        jp      DONE1
ALIGN:                          ; Blit an aligned sprite to graphbuf
        pop     de              ; de->sprite
        ld      b,16
ALOP1:  ld      a,(de)
        xor     (hl)
        ld      (hl),a
        inc     de
        inc	de
        push    bc
        ld      bc,12
        add     hl,bc
        pop     bc
        djnz    ALOP1
DONE1:
        pop bc                  ;this code is for the memory increment after
        ret
	
.end	