             .title        "Nanjing"


;Written by YW
;Version 1.0
;This is an OVERDUMPER for Mapper 163. This always dumps 224 16k banks, even
;though no game is known to have that many. You must modify the final NES file
;to remove the extraneous banks.
             
             ;vectors for standard system calls

send_byte:   .equ 0200h
baton:       .equ 0203h
chk_vram:    .equ 0206h
chk_wram:    .equ 0209h
wr_ppu:      .equ 020ch
read_byte:   .equ 020fh
init_crc:    .equ 0212h
do_crc:      .equ 0215h
finish_crc:  .equ 0218h
crc0:        .equ 0080h
crc1:        .equ 0081h
crc2:        .equ 0082h
crc3:        .equ 0083h


temp1:       .equ 00e0h
temp1_lo:    .equ 00e0h
temp1_hi:    .equ 00e1h
temp2:       .equ 00e2h
temp2_lo:    .equ 00e2h
temp2_hi:    .equ 00e3h
temp3:       .equ 00e4h
temp3_lo:    .equ 00e4h
temp3_hi:    .equ 00e5h


             ;plugin header that describes what it does
             
             .org 0380h
             
             .db "Nanjing overdumper " 
             .db "YW",0

             .fill 03ffh-*,00h    ;all plugins must reside at 400h
             .db 163


;check mirroring             
             
             ldx #00h
             lda #020h
             jsr wr_ppu
             lda #055h
             sta 2007h
             lda #0aah
             sta 2007h
             lda #024h
             jsr wr_ppu
             lda 2007h
             lda 2007h
             cmp #055h
             bne horz_mir
             lda 2007h
             cmp #0aah
             bne horz_mir
             lda #020h
             jsr wr_ppu
             lda #0aah
             sta 2007h
             lda #024h
             jsr wr_ppu
             lda 2007h
             lda 2007h
             cmp #0aah
             bne horz_mir
             inx

horz_mir:    txa
             jsr send_byte   ;send byte
             
             
;dump PRG data
             lda #000h
             jsr send_byte
             lda #038h       ;send size
             jsr send_byte
             lda #001h
             jsr send_byte   ;send PRG 

;initialize
             lda #07h
             sta 05300h
             lda #0
             sta 05000h
             sta 05200h
             sta temp2_hi
             lda #80h
             sta 05000h
             sta temp2_lo

prg_loop:
             jsr prg

             inc temp2_lo
             lda temp2_lo
             cmp #090h
             beq prghi
prglo:
             sta 05000h
             jmp prg_loop
prghi:
             inc temp2_hi
             lda temp2_hi
             cmp #07h
             beq prgdone
             sta 05200h
             lda #080h
             sta temp2_lo
             jmp prglo
prgdone:

;no CHR data
             jmp end

             lda #020h
             tax
             jsr send_byte
             lda #000h
             jsr send_byte  ;send size
             lda #002h
             jsr send_byte  ;send CHR header

             jsr chr

end:
             lda #000h
             jsr send_byte  ;send end flag
             lda #000h
             jsr send_byte  ;send end flag
             lda #000h
             jsr send_byte  ;send end flag
             rts            ;done 


prg:
             ldy #0
             sty temp1_lo
             lda #080h
             sta temp1_hi    ;set pointer to 08000h
             
             ldx #080h       ;080h pages
             
dump_it:     lda (temp1),y   ;dump 1 byte to the PC
             jsr send_byte
             iny
             bne dump_it
             inc temp1_hi    ;dump 1 page
             dex
             bne dump_it
                             ;all pages dumped
             rts


chr:
             lda #000h
             sta 02006h
             lda #000h
             sta 02006h     ;set PPU address to 0000h
             lda 02007h     ;read garbage byte
             ldy #0
             
dump_chr:    lda 02007h     ;send 1 byte to the PC
             jsr send_byte
             iny
             bne dump_chr
             jsr baton      ;send 1 page
             dex
             bne dump_chr   ;dump 8K of CHR
             rts


             .fill 0800h-*,0ffh   ;fill rest to get 1K of data

             .end
