; == 12x12 kanji rom dumper ==
; for FS-4600, FS-CM1, FS-A1FM
; 2003/01/12  Martos

bdos    equ     0f37dh

        org     100h

        in      a,(40h)
        push    af
        ld      a,0f7h
        out     (40h),a         ; activate kanji rom ports

        ld      de,0800h
        in      a,(40h)
        cp      d
        jr      z,_jis1active
        set     0,e
_jis1active:
        in      a,(41h)
        cp      d
        jr      z,_jis2active
        set     1,e
_jis2active:
        ld      a,e
        or      a
        jr      z,nowchecking
        out     (42h),a         ; force activation in FS-4600

nowchecking:
        ld      de,m_checking
        call    message
        ld      a,0eh
        out     (47h),a
        ld      a,5fh
        out     (48h),a
        ld      bc,0800h
jis1test:
        in      a,(49h)
        add     a,c
        ld      c,a
        djnz    jis1test
        cp      4ch
        ld      de,m_notfound
        jr      nz,errorexit1
        ld      a,25h
        out     (47h),a
        ld      a,5eh
        out     (48h),a
        in      a,(49h)
        sub     41h
        ld      c,a
        in      a,(49h)
        sub     42h
        or      c
        ld      b,8             ; size of kanji rom in 16K blocks (=128K)
        ld      de,m_jis1found
        jr      nz,showfound
        ld      b,16            ; =256K
        ld      de,m_jis12found
showfound:
        push    bc
        call    message

        ld      de,m_saving
        call    message
        ld      de,dta
        ld      c,1ah           ; set dta
        call    bdos
        ld      c,16h           ; create file
        call    file
        or      a
        jr      nz,errorexit2
        ld      hl,4000h
        ld      (fcb+14),hl     ; 16K data blocks

; Since base address pointed by index ports (47h-48h) is 800h, range 0-7ffh
; of the rom can't be accessed by software. So this range is filled up with
; zeroes in the output file.

        xor     a
        out     (47h),a
        out     (48h),a
        ld      hl,dta
        ld      de,dta+1
        ld      bc,800h-1
        ld      (hl),a
        ldir
        ex      de,hl
        ld      a,38h
        jr      1stblock

16kblock:
        push    bc
        ld      hl,dta
        ld      a,40h
1stblock:
        ld      bc,0049h
readfont:
        inir
        dec     a
        jr      nz,readfont
        ld      c,26h           ; write blocks to file
        ld      hl,1            ; only one (=16K)
        call    file
        or      a
        jr      nz,errorexit3
        pop     bc
        djnz    16kblock
        call    closefile
        ld      de,m_done
errorexit1:
        call    message
        pop     af
        cpl
        out     (40h),a
        ret

errorexit3:
        call    closefile
        ld      c,13h           ; kill truncated file
        call    file
errorexit2:
        pop     bc
        ld      de,m_failed
        jr      errorexit1

message:
        ld      c,09h
        jr      tobdos

closefile:
        ld      c,10h
file:
        ld      de,fcb
tobdos:
        jp      bdos

m_checking:
        db      "12x12 dot kanji rom $"
m_notfound:
        db      "not found!$"
m_jis1found:
        db      "found, JIS level 1 only$"
m_jis12found:
        db      "found, JIS level 1 & 2$"
m_saving:
        db      13, 10, "Now saving... $"
m_failed:
        db      "failed!$"
m_done:
        db      "done!$"
fcb:
        db      0, "KANJI12 ROM", 25 dup(0)
dta:

        end

