;*************************************
;       VRAM Direct Access Program
;*************************************

extrn   page_ofs:dword

bitptn  segment 'DATA'

;*************************************
;       16色VRAMビットパタ－ン
;*************************************

ankfnt  dd      16*256 dup(?)
fntcol  dd      256 dup(?)
fntptn  dd      000000000h,0F0000000h,00F000000h,0FF000000h
        dd      000F00000h,0F0F00000h,00FF00000h,0FFF00000h
        dd      0000F0000h,0F00F0000h,00F0F0000h,0FF0F0000h
        dd      000FF0000h,0F0FF0000h,00FFF0000h,0FFFF0000h
        dd      00000F000h,0F000F000h,00F00F000h,0FF00F000h
        dd      000F0F000h,0F0F0F000h,00FF0F000h,0FFF0F000h
        dd      0000FF000h,0F00FF000h,00F0FF000h,0FF0FF000h
        dd      000FFF000h,0F0FFF000h,00FFFF000h,0FFFFF000h
        dd      000000F00h,0F0000F00h,00F000F00h,0FF000F00h
        dd      000F00F00h,0F0F00F00h,00FF00F00h,0FFF00F00h
        dd      0000F0F00h,0F00F0F00h,00F0F0F00h,0FF0F0F00h
        dd      000FF0F00h,0F0FF0F00h,00FFF0F00h,0FFFF0F00h
        dd      00000FF00h,0F000FF00h,00F00FF00h,0FF00FF00h
        dd      000F0FF00h,0F0F0FF00h,00FF0FF00h,0FFF0FF00h
        dd      0000FFF00h,0F00FFF00h,00F0FFF00h,0FF0FFF00h
        dd      000FFFF00h,0F0FFFF00h,00FFFFF00h,0FFFFFF00h
        dd      0000000F0h,0F00000F0h,00F0000F0h,0FF0000F0h
        dd      000F000F0h,0F0F000F0h,00FF000F0h,0FFF000F0h
        dd      0000F00F0h,0F00F00F0h,00F0F00F0h,0FF0F00F0h
        dd      000FF00F0h,0F0FF00F0h,00FFF00F0h,0FFFF00F0h
        dd      00000F0F0h,0F000F0F0h,00F00F0F0h,0FF00F0F0h
        dd      000F0F0F0h,0F0F0F0F0h,00FF0F0F0h,0FFF0F0F0h
        dd      0000FF0F0h,0F00FF0F0h,00F0FF0F0h,0FF0FF0F0h
        dd      000FFF0F0h,0F0FFF0F0h,00FFFF0F0h,0FFFFF0F0h
        dd      000000FF0h,0F0000FF0h,00F000FF0h,0FF000FF0h
        dd      000F00FF0h,0F0F00FF0h,00FF00FF0h,0FFF00FF0h
        dd      0000F0FF0h,0F00F0FF0h,00F0F0FF0h,0FF0F0FF0h
        dd      000FF0FF0h,0F0FF0FF0h,00FFF0FF0h,0FFFF0FF0h
        dd      00000FFF0h,0F000FFF0h,00F00FFF0h,0FF00FFF0h
        dd      000F0FFF0h,0F0F0FFF0h,00FF0FFF0h,0FFF0FFF0h
        dd      0000FFFF0h,0F00FFFF0h,00F0FFFF0h,0FF0FFFF0h
        dd      000FFFFF0h,0F0FFFFF0h,00FFFFFF0h,0FFFFFFF0h
        dd      00000000Fh,0F000000Fh,00F00000Fh,0FF00000Fh
        dd      000F0000Fh,0F0F0000Fh,00FF0000Fh,0FFF0000Fh
        dd      0000F000Fh,0F00F000Fh,00F0F000Fh,0FF0F000Fh
        dd      000FF000Fh,0F0FF000Fh,00FFF000Fh,0FFFF000Fh
        dd      00000F00Fh,0F000F00Fh,00F00F00Fh,0FF00F00Fh
        dd      000F0F00Fh,0F0F0F00Fh,00FF0F00Fh,0FFF0F00Fh
        dd      0000FF00Fh,0F00FF00Fh,00F0FF00Fh,0FF0FF00Fh
        dd      000FFF00Fh,0F0FFF00Fh,00FFFF00Fh,0FFFFF00Fh
        dd      000000F0Fh,0F0000F0Fh,00F000F0Fh,0FF000F0Fh
        dd      000F00F0Fh,0F0F00F0Fh,00FF00F0Fh,0FFF00F0Fh
        dd      0000F0F0Fh,0F00F0F0Fh,00F0F0F0Fh,0FF0F0F0Fh
        dd      000FF0F0Fh,0F0FF0F0Fh,00FFF0F0Fh,0FFFF0F0Fh
        dd      00000FF0Fh,0F000FF0Fh,00F00FF0Fh,0FF00FF0Fh
        dd      000F0FF0Fh,0F0F0FF0Fh,00FF0FF0Fh,0FFF0FF0Fh
        dd      0000FFF0Fh,0F00FFF0Fh,00F0FFF0Fh,0FF0FFF0Fh
        dd      000FFFF0Fh,0F0FFFF0Fh,00FFFFF0Fh,0FFFFFF0Fh
        dd      0000000FFh,0F00000FFh,00F0000FFh,0FF0000FFh
        dd      000F000FFh,0F0F000FFh,00FF000FFh,0FFF000FFh
        dd      0000F00FFh,0F00F00FFh,00F0F00FFh,0FF0F00FFh
        dd      000FF00FFh,0F0FF00FFh,00FFF00FFh,0FFFF00FFh
        dd      00000F0FFh,0F000F0FFh,00F00F0FFh,0FF00F0FFh
        dd      000F0F0FFh,0F0F0F0FFh,00FF0F0FFh,0FFF0F0FFh
        dd      0000FF0FFh,0F00FF0FFh,00F0FF0FFh,0FF0FF0FFh
        dd      000FFF0FFh,0F0FFF0FFh,00FFFF0FFh,0FFFFF0FFh
        dd      000000FFFh,0F0000FFFh,00F000FFFh,0FF000FFFh
        dd      000F00FFFh,0F0F00FFFh,00FF00FFFh,0FFF00FFFh
        dd      0000F0FFFh,0F00F0FFFh,00F0F0FFFh,0FF0F0FFFh
        dd      000FF0FFFh,0F0FF0FFFh,00FFF0FFFh,0FFFF0FFFh
        dd      00000FFFFh,0F000FFFFh,00F00FFFFh,0FF00FFFFh
        dd      000F0FFFFh,0F0F0FFFFh,00FF0FFFFh,0FFF0FFFFh
        dd      0000FFFFFh,0F00FFFFFh,00F0FFFFFh,0FF0FFFFFh
        dd      000FFFFFFh,0F0FFFFFFh,00FFFFFFFh,0FFFFFFFFh
;**********************************************
;       漢字コ－ドチェックテ－ブル
;**********************************************
ISKAN1  equ     1
ISKAN2  equ     2
;               0 1 2 3 4 5 6 7 8 9 A B C D E F
kantbl  db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;1
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;2
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;3
        db      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 ;4
        db      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 ;5
        db      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 ;6
        db      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0 ;7
        db      2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 ;8
        db      3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 ;9
        db      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 ;A
        db      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 ;B
        db      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 ;C
        db      2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 ;D
        db      3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 ;E
        db      3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0 ;F
bitptn  ends

VRAM    segment 'CODE'
        assume  cs:VRAM,ds:bitptn
;**************************************
;       String to VRAM Display
;**************************************
;   putstr(char *vram, char *str)
;               +8          +12
;**************************************
        align   4                               ; align set
        public  putstr
putstr  proc    near
        push    ebp
        mov     ebp,esp
        xor     eax,eax
        push    eax                             ; work push

        push    gs
        push    fs
        push    es
        push    esi
        push    edi
        push    edx
        push    ecx
        push    ebx
        cld
        mov     ax,104h                         ; VRAM Selecter
        mov     es,ax                           ; es = VRAM Selecter
        mov     ax,0110h                        ; TBIOS Selecter
        mov     fs,ax                           ; fs = TBIOS
        mov     ax,ds
        mov     gs,ax                           ; gs = ds

        align   4                               ; align set
putlop:                                         ; Put Loop
        mov     esi,[ebp+12]                    ; char *str
        lodsb                                   ; Get String    ※
        or      al,al
        je      Left_clear                      ; End of String ?

        movzx   eax,al
        mov     bl,[eax+offset kantbl]
        and     bl,ISKAN1
        jz      putank                          ; Not iskanji1 ?
        movzx   ebx,byte ptr [esi]
        mov     bh,[ebx+offset kantbl]
        and     bh,ISKAN2
        jz      putank                          ; Not iskanji2 ?
        mov     bh,al
        inc     esi                             ; double_word ?????     ※
        mov     [ebp+12],esi                    ; Save *str

        shl     bh,1                            ; Shift Jis to Jis
        cmp     bl,080h
        adc     bx,1f61h
        add     bl,7fh
        jc      short knf1
        add     bl,0a2h
knf1:   and     bh,7fh                          ; StoJ end

        mov     ax,0100h
        mov     dx,1010h

        call    pword ptr fs:[00a0h]            ; Get Kanji Font

        mov     edi,ss:[ebp+8]                  ; *vram
        add     edi,ss:[ebp-4]                  ; work add
        or      edi,gs:[page_ofs]               ; or Page Offset
        mov     ebx,offset fntcol
        xor     edx,edx                         ; edx=0
        xor     ecx,ecx                         ; ecx=0
        REPT    16
        lodsw                                   ; Get Kanji Font Pattern
        mov     cl,al                           ; Left Font Pattern
        mov     dl,ah                           ; Right Font Pattern
        mov     eax,gs:[ebx+ecx*4]              ; Get Vram Bit Patan
        stosd                                   ; Write Left Font 
        mov     eax,gs:[ebx+edx*4]              ; Get Vram Bit Patan
        stosd                                   ; Write Right Font
        add     edi,512-4*2                     ; 1 Line 512 Byte
        endm
        mov     ax,gs
        mov     ds,ax
        add     dword ptr [ebp-4],8             ; Next vram Address

        jmp     putlop                          ; Next Put String Loop

        align   4                               ; align set
putank:                                         ; Write Ank Font
        mov     [ebp+12],esi                    ; Save *str
        movzx   esi,al
        shl     esi,6                           ; 4x16=64
        add     esi,offset ankfnt               ; ANK Font Table Offset

        mov     edi,[ebp+8]                     ; *vram
        add     edi,[ebp-4]                     ; work add
        or      edi,[page_ofs]                  ; or Page Offset
        mov     ebx,512-4
        REPT    15
        movsd                                   ; Trans Font
        add     edi,ebx                         ; Line add
        endm
        movsd                                   ; Trans Font
        add     dword ptr [ebp-4],4             ; Next vram Address

        jmp     putlop                          ; Next Put String Loop

        align   4
Left_clear:
        ; VRAM Clear
        mov     ebx,320
        sub     ebx,[ebp-4]                     ; nokori dword
        je      nonclr

        mov     edi,[ebp+8]                     ; *vram
        add     edi,[ebp-4]                     ; work add
        or      edi,[page_ofs]                  ; or Page Offset
        shr     ebx,2                           ; / 4
        mov     ebp,[ebp-4]
        add     ebp,512-320
        mov     eax,[fntcol]
        mov     edx,03FFFFh
        mov     esi,[page_ofs]
        REPT    16
        mov     ecx,ebx                         ; nokori dword
        rep     stosd
        add     edi,ebp
        and     edi,edx
        or      edi,esi
        endm
nonclr:
        ; end of VRAM Clear
        pop     ebx
        pop     ecx
        pop     edx
        pop     edi
        pop     esi
        pop     es
        pop     fs
        pop     gs

        pop     eax                             ; work pop
        pop     ebp
        ret                                     ; end of putstr

putstr  endp
;***************************************************
;       void    ctblset(int Chr_Col,int Bak_Col);
;***************************************************
        public  ctblset
ctblset proc    near
        push    ebp
        mov     ebp,esp
        push    esi
        push    edi
        push    fs

        mov     ecx,256
        mov     esi,offset fntptn
        mov     edi,offset fntcol
ctblp:  lodsd
        mov     edx,eax
        not     edx
        and     eax,[ebp+8]
        and     edx,[ebp+12]
        or      eax,edx
        stosd
        loop    ctblp

        mov     ax,138h                         ; ANK Font Selecter
        mov     fs,ax
        mov     esi,3D800h
        mov     edi,offset ankfnt

        mov     ecx,16*256
aktb1:  movzx   eax,byte ptr fs:[esi]
        inc     esi
        mov     eax,[eax*4+fntcol]
        stosd
        loop    aktb1

        pop     fs
        pop     edi
        pop     esi
        pop     ebp
        ret
ctblset endp
        public  inport
;****************************************************************************
;   wrtstr(char *str, int page, char *vram, int forcol, int bakcol, int font)
;               +8        +12        +16        +20         +24         +28
;****************************************************************************
        align   4                               ; align set
        public  wrtstr
wrtstr  proc    near
        push    ebp
        mov     ebp,esp
        push    gs
        push    fs
        push    es
        push    esi
        push    edi
        push    ebx

        mov     ax,104h                         ; VRAM Selecter
        mov     gs,ax                           ; gs = VRAM Selecter
        mov     ax,110h                         ; TBIOS Selecter
        mov     fs,ax                           ; fs = TBIOS

        align   4                               ; align set
wrtlop:                                         ; Put Loop
        mov     esi,[ebp+8]                     ; char *str
        movzx   eax,byte ptr [esi]
        inc     si
        or      al,al
        jnz     short wrtnxt                    ; End of String ?

        pop     ebx
        pop     edi
        pop     esi
        pop     es
        pop     fs
        pop     gs
        pop     ebp
        ret                                     ; end of putstr

        align   4                               ; align set
wrtnxt:
        mov     bl,[eax+offset kantbl]
        and     bl,ISKAN1
        jz      wrtank                          ; Not iskanji1 ?
        movzx   ebx,byte ptr [esi]
        mov     bh,[ebx+offset kantbl]
        and     bh,ISKAN2
        jz      wrtank                          ; Not iskanji2 ?
        mov     bh,al
        inc     si
        mov     [ebp+8],esi                     ; Save *str

        shl     bh,1                            ; Shift Jis to Jis
        cmp     bl,080h
        adc     bx,1f61h
        add     bl,7fh
        jc      short wrtst1
        add     bl,0a2h
wrtst1: and     bh,7fh                          ; StoJ end

        mov     ax,0100h
        mov     dx,1010h
        push    ds
        call    pword ptr fs:[00a0h]            ; Get Kanji Font
        push    ds                              ; ds:esi = Kanji Font
        pop     es                              ; cheng ds<->es
        pop     ds

        mov     edi,[ebp+16]                    ; *vram
        or      edi,[ebp+12]                    ; page
        mov     ecx,[ebp+28]                    ; Y = 16 Dot Font
        align   4                               ; align set

wtkn1:  mov     bx,es:[esi]
        add     esi,2

        cmp     byte ptr [ebp+28],16
        je      short wtkn2
        or      bx,es:[esi]
        add     esi,2
wtkn2:
        movzx   eax,bl                          ; Get Kanji Left Font Pattan
        mov     eax,[eax*4+fntptn]              ; Get Vram Bit Patan
        mov     edx,eax
        not     edx
        and     eax,[ebp+20]
        and     edx,[ebp+24]
        or      eax,edx
        mov     gs:[edi],eax                    ; Write Kanji Left Font

        movzx   eax,bh                          ; Get Kanji Right Font Pattan
        mov     eax,[eax*4+fntptn]              ; Get Vram Bit Patan
        mov     edx,eax
        not     edx
        and     eax,[ebp+20]
        and     edx,[ebp+24]
        or      eax,edx
        mov     gs:[edi+4],eax                  ; Write Kanji Right Font

        add     edi,512                         ; 1 Line 512 Byte
        and     edi,03FFFFh
        or      edi,[ebp+12]                    ; Page
        loop    wtkn1

        add     dword ptr [ebp+16],8
        jmp     wrtlop                          ; Next Put String Loop

        align   4                               ; align set
wrtank:                                         ; Write Ank Font
        cmp     al,1Bh                          ; Esc Code
        jne     short wtak0

        movzx   eax,byte ptr [esi]
        inc     esi
wtak0:  mov     [ebp+8],esi                     ; Save *str
        mov     esi,eax

        cmp     byte ptr [ebp+28],16
        jne     short wrtft1
        shl     esi,4                           ; x16
        add     esi,3D800h                      ; 8x16 ANK Font Offset
        jmp     short wrtft2
wrtft1: shl     esi,3                           ; x8
        add     esi,3D000h                      ; 8x8 ANK Font Offset

wrtft2: mov     ax,138h                         ; ANK Font Selecter
        mov     es,ax

        mov     edi,[ebp+16]                    ; *vram
        or      edi,[ebp+12]                    ; Page
        mov     ecx,[ebp+28]                    ; Y = 16 Dot Font
        align   4                               ; align set

wtak1:  movzx   eax,byte ptr es:[esi]           ; Get Font Pattan
        inc     esi
        mov     eax,[eax*4+fntptn]              ; Get Vram Bit Patan
        mov     edx,eax
        not     edx
        and     eax,[ebp+20]
        and     edx,[ebp+24]
        or      eax,edx
        mov     gs:[edi],eax                    ; Write Ank Font

        add     edi,512                         ; 1 Line 512 Byte
        and     edi,03FFFFh
        or      edi,[ebp+12]                    ; Page
        loop    wtak1

        add     dword ptr [ebp+16],4

        jmp     wrtlop                          ; Next Put String Loop

wrtstr  endp

;**************************************
;       int     inport(int portaddr)
;**************************************
inport  proc    near
        push    ebp
        mov         ebp,esp
        push    edx
        mov         edx,[ebp+8]
        xor         eax,eax
        in          al,dx
        pop         edx
        pop         ebp
        ret
inport  endp

;**************************************
;       VRAM Disp Start Addr Set
;**************************************
        public  dsp_adr_set
dsp_adr_set     proc    near
        push    ebp
        mov     ebp,esp
        push    edx

        mov     dx,0440h
        mov     al,21                   ; CRTC FA0
        out     dx,al
        mov     dx,0442h
        mov     eax,[ebp+8]
        out     dx,ax

        pop     edx
        pop     ebp
        ret
dsp_adr_set    endp

;**************************************
;   I/O Register access
;**************************************

;void   outregw( port, num,data )

        public  outregw
outregw proc    near
        mov     dx,[esp+4]
        mov     al,[esp+8]
        out     dx,al
        add     dx,2
        mov     ax,[esp+12]
        out     dx,ax
        ret
outregw endp

VRAM    ends
        end
