;************************************************************
; Procedure Name  : bgtile_mega
;
; Author          : Marc Gale (Xalthorn or Eldara)
;
; Description     : Fill the background with a 4x4 tile
;
; On Entry        : hl holds the start of the background data
;                   a holds the bank number of the tile
;                   b holds the starting sprite of the tile
;                   c holds the palette number (0-7)
;
; During          : a is used heavily
;                 : d is used as a loop counter
;                 : e is used as a loop counter
;
; On Exit         : Nothing intentional
;************************************************************
bgtile_mega:
 and    1               ; mask out all but the bit we want
 sla    a               ; move the bank
 sla    a               ; number to the
 sla    a               ; correct bit
 ld     d,a             ; stick it in d for a mo
 ld     a,c             ; get the palette number
 and    7               ; mask out all but the bits we want
 or     d               ; combine it with the bank number
 ld     c,a             ; store the result in c

; now we place the tile entries into the background map
 ld     a,0
 ldh    [$4F],a         ; make sure we're writing to bank 0

 push   hl              ; preserve the start address

 ld     e,8             ; initialise the y counter for 8 rows
.yloop
 ld     d,8             ; initialise the x counter for 8 mega tiles
.xloop1
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc    b               ; increase the sprite number
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc	b
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc	b
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 dec    b               ; decrease the sprite number ...
 dec	b		; ... back to the ...
 dec	b		; ... first tile ...

 dec    d               ; decrease the x counter 
 jr     nz,.xloop1      ; if we haven't hit 0, go back and carry on
 ld     d,8             ; reset the x counter

 inc    b               ; increase the sprite number...
 inc    b               ; ... to the next row
 inc	b
 inc	b
.xloop2
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc    b               ; increase the sprite number
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc	b
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc	b
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 dec    b               ; decrease the sprite number ...
 dec	b		; ... back to the ...
 dec	b		; ... first tile ...

 dec    d               ; decrease the x counter 
 jr     nz,.xloop2      ; if we haven't hit 0, go back and carry on
 ld     d,8             ; reset the x counter

 inc    b               ; increase the sprite number...
 inc    b               ; ... to the next row
 inc	b
 inc	b
.xloop3
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc    b               ; increase the sprite number
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc	b
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc	b
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 dec    b               ; decrease the sprite number ...
 dec	b		; ... back to the ...
 dec	b		; ... first tile ...

 dec    d               ; decrease the x counter 
 jr     nz,.xloop3      ; if we haven't hit 0, go back and carry on
 ld     d,8             ; reset the x counter

 inc    b               ; increase the sprite number...
 inc    b               ; ... to the next row
 inc	b
 inc	b
.xloop4
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc    b               ; increase the sprite number
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc	b
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 inc	b
 ld     a,b             ; get the sprite number
 ld     [hli],a         ; put it in the BG data and increase hl
 dec    b               ; decrease the sprite number ...
 dec	b		; ... back to the ...
 dec	b		; ... first tile ...

 dec    d               ; decrease the x counter 
 jr     nz,.xloop4      ; if we haven't hit 0, go back and carry on

 ld	a,b		; decrease the sprite number ...
 sub	a,12		; ... back to the start ...
 ld	b,a		; ... of the tile

 dec    e               ; decrease the y counter 
 jr     nz,.yloop       ; if we haven't hit 0, go back and carry on

; now that's done, let's set the bank and palette numbers

 pop    hl              ; restore the start address
 ld     a,1
 ldh    [$4F],a         ; make sure we're writing to bank 1

 ld     a,c             ; grab the bank 1 data

 ld     d,4             ; we want to write 4x256=1024 bytes
.small_loop
 ld     e,0             ; start at 0, which means we'll get 256
.large_loop
 ld     [hli],a         ; write the bank 1 data
 dec    e               ; drop the large counter by one
 jr     nz,.large_loop  ; if we haven't hit 0, carry on
 dec    d               ; drop the small counter by one
 jr     nz,.small_loop  ; if we haven't hit 0, carry on

 ret                    ; we're done here, return
