;----------------------------------------------------------
; level_X_load (26 lines)
level_X_load_00X:
   PUSH AF                     ;

   CALL VDP_off                ; Stop VDP

   CALL load_tile_data_00X     ; Load tiles
   CALL load_bg_data_00X       ; Load background
   CALL load_palette_data_00X  ; Load palette
   CALL intro_load_sprites     ; Load intro sprites
   CALL sprites_to_VRAM        ;
   CALL copy_score_bgX         ; Set the score in RAM

   LD a, STATE_GAME_OVER_WAIT  ; Go to next state
   LD (RAM_GAME_STATE), a      ;

   LD a, $00                   ; Clear
   LD (RAM_TIME_1), a          ; Time 1
   LD a, $03                   ; Set
   LD (RAM_TIME_2), a          ; Time 2
   LD (RAM_HEALTH_1), a        ; Health

   LD a, $00                  ; Prevent light gun from being read 
   LD (RAM_END_TRIGGER_READY), a ; until timer ends.

   LD a, 0                     ; Clear
   LD (RAM_FOE_1_COUNT), a     ;
   LD (RAM_FOE_2_COUNT), a     ;
   LD (RAM_FOE_3_COUNT), a     ;
   LD (RAM_FOE_4_COUNT), a     ;
   LD (RAM_FOE_5_COUNT), a     ;
   LD (RAM_FOE_6_COUNT), a     ;
   LD (RAM_FOE_7_COUNT), a     ;
   LD (RAM_FOE_8_COUNT), a     ;

   LD a, $00                   ; Enable scrolling
   LD (RAM_ENABLE_SCROLLING), a ;

   CALL VDP_on                 ; Start VDP

   POP AF                      ;
   RET                         ; End subroutine 
;----------------------------------------------------------

;----------------------------------------------------------
; level_X_run_00X (17 lines)
;
; Run game over.
; * Wait for player to pull trigger again.
;----------------------------------------------------------
level_X_run_00X:

; Wait for player to press trigger to 
; return to title screen.
   CALL run_timer_level_Xend   ;
   LD a, (RAM_END_TRIGGER_READY) ; Wait 3 seconds
   CP $7F                      ;
   JP NZ, level_X_run_00X_end  ;
   LD a, (RAM_LIGHT_GUN_READ)  ; Light gun trigger pressed?
   CP $7F                      ;
   JP NZ, level_X_run_00X_end  ;

   CALL VDP_off                ; Stop VDP

   LD hl, $C000                ;
   LD b, 255                   ;
   CALL tool_clear_RAM_loop    ; Clear working RAM
   CALL clear_vram             ; Clear VRAM
   CALL clear_palette_data     ; Clear palette data
   CALL intro_load_sprites     ; Load intro sprites
   CALL load_tile_data_000     ; Load tile data
   CALL load_bg_data_000       ;
   CALL load_palette_data_000  ; Load colors

   LD a, STATE_INTRO           ; Go to next state
   LD (RAM_GAME_STATE), a      ;

   CALL copy_song_to_RAM       ; Load sound

   CALL VDP_on                 ; Start VDP

level_X_run_00X_end:
   RET                         ; End subroutine 
;----------------------------------------------------------

;----------------------------------------------------------
; load_tile_data_00X (12 lines)
;
; Load tile data to $0000.
;----------------------------------------------------------
load_tile_data_00X: 
   LD a, $00                   ; Low address byte
   OUT (VDP_ADDR),a            ;
   LD a, $00                   ; High address byte
   OUT (VDP_ADDR),a            ;

   LD hl, tiles_00X + 1        ; tile_data is defined below
   LD c, VDP_DATA              ; C <- $BE;   LD b, 128  

   LD e, 64                   ; Set up counter (127 tiles, 64 sets of 2 tiles)

tile_load_looptX:
   LD b, 64                    ; Write 2 tiles (64 characters)               
   CALL copy_to_vdp_loop       ; HL -> VDP, until B goes to 0

   DEC e                       ;
   JR NZ, tile_load_looptX     ; Loop until de is empty.

   RET                         ; End subroutine 
;---------------------------------------

;----------------------------------------------------------
; load_bg_data_00X (12 lines)
;
; Load background data
;----------------------------------------------------------
load_bg_data_00X: 
   LD a, $FF                   ; Low address byte
   OUT (VDP_ADDR),a            ;
   LD a, $37                   ; High address byte
   OUT (VDP_ADDR),a            ;

   LD hl, background_00X       ; background_000 is defined below
   LD c, VDP_DATA              ; C <- $BE;   LD b, 128  

   LD e, 24                    ; Set up counter (24 rows)

tile_load_loopbgX:
   LD b, 64                    ; Write 1 row (64 characters)               
   CALL copy_to_vdp_loop       ; HL -> VDP, until B goes to 0

   DEC e                       ;
   JR NZ, tile_load_loopbgX    ; Loop until de is empty.

   RET                         ; End subroutine 
;---------------------------------------

;----------------------------------------------------------
; load_palette_data_00X (12 lines)
;
; Load palette data.
;----------------------------------------------------------
load_palette_data_00X:
   LD a, $00                   ; Low address byte
   OUT (VDP_ADDR),a            ;
   LD a, $C0                   ; High address byte
   OUT (VDP_ADDR),a            ;

   LD hl, palette_001          ; 
   LD b, 32                    ;
   CALL copy_to_vdp_loop       ; HL -> VDP, until B goes to 0

   LD hl, palette_001          ; Copy restore color
   LD de, RAM_RESTORE_COLOR    ;
   LD bc, 32                   ;
   LDIR                        ;

   RET                         ; End subroutine  
;---------------------------------------

;---------------------------------------
; copy_score_bgX (17 lines)
;
; Copy score data from RAM to background
;---------------------------------------
copy_score_bgX:
; Row 1
   LD a, $55                   ; Low address byte
   OUT (VDP_ADDR),a            ;
   LD a, $3C                   ; High address byte
   OUT (VDP_ADDR),a            ;

   LD hl, RAM_SCORE_1          ; Read tile data
   LD b, 7                     ; Copy 7 digits

csbgX_loop_000:
   LD a, (hl)                  ; Load score
   CP $00                      ; $00 -> $0A
   JP NZ, csbgX_skip_000       ;
   LD a, $0A                   ; $0A is digit 0
csbgX_skip_000:
   OUT (VDP_DATA), a           ; Write to VRAM
   LD a, $00                   ;
   OUT (VDP_DATA), a           ; Write to VRAM

   INC hl                      ; Increment
   DEC b                       ; Decrement
   JP NZ, csbgX_loop_000       ;

   RET                         ; End subroutine 

;---------------------------------------

;---------------------------------------
; run_timer_level_Xend (18 lines)
;
; Count down the timer digits.
;---------------------------------------
run_timer_level_Xend:
   LD a, (RAM_FRAME_CTR)      ; Frame counter
   CP 59                      ; 60 frames per second
   JP NZ, rtlvXe_skip_000     ;

   LD a, (RAM_TIME_2)         ; Time 2
   DEC a                      ;
   LD (RAM_TIME_2), a         ;

   CP $FF                     ; at -1? 
   JP NZ, rtlvXe_skip_000     ;

   LD a, $09                  ;
   LD (RAM_TIME_2), a         ;

   LD a, (RAM_TIME_1)         ; Time 2
   DEC a                      ;
   LD (RAM_TIME_1), a         ;

   CP $FF                     ; at -1? 
   JP NZ, rtlvXe_skip_000     ;

   LD a, $7F                  ; Prevent light gun from being read 
   LD (RAM_END_TRIGGER_READY), a ; until timer ends.

rtlvXe_skip_000:
   RET                        ; End subroutine 
;---------------------------------------

background_00X:
.include "ppolis_gameover_BG.inc"

tiles_00X:
.include "ppolis_gameover_T.inc"

