             .title        "Mapper 227 reader"


;Written by YW
;Version 1.0
;This is an OVERDUMPER for Mapper 227. You must modify the final NES file to
;remove the extraneous banks, if there are any.
             
             ;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 0000h
temp1_lo:    .equ 0000h
temp1_hi:    .equ 0001h
temp2:       .equ 06e2h
temp2_lo:    .equ 06e2h
temp2_hi:    .equ 06e3h
temp3:       .equ 06e4h
temp3_lo:    .equ 06e4h
temp3_hi:    .equ 06e5h


             ;plugin header that describes what it does
             
             .org 0380h
             
             .db "Mapper 227 overdumper " 
             .db "YW",0

             .fill 03ffh-*,00h    ;all plugins must reside at 400h
             .db 227


;mirroring is mapper-controlled
             lda #0
             jsr send_byte   ;send byte


;dump PRG data
             lda #000h
             jsr send_byte
             lda #010h       ;send size
             jsr send_byte
             lda #001h
             jsr send_byte   ;send PRG 
             
;init
             lda #0
             sta temp2
             tay
             sta 08083h,y

prg_loop_a:
             jsr prg
             lda #8
             clc
             adc temp2
             cmp #80h
             beq prg_done_a
             sta temp2
             tay
             sta 08083h,y
             jmp prg_loop_a
prg_done_a:

             lda #0
             sta temp2
             tay
             sta 08183h,y

prg_loop_b:
             jsr prg
             lda #8
             clc
             adc temp2
             cmp #80h
             beq prg_done_b
             sta temp2
             tay
             sta 08183h,y
             jmp prg_loop_b
prg_done_b:
             
             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:
             ldx #020h
             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
