
        opt     l-
        icl     'atarihw.ah'
        icl     'atarios.ah'
        icl     'vbxefx.ah'
        opt     l+
        
ptr1    equ     0x90
lines   equ     0xa0
ysav    equ     0xa1
phase   equ     0xa2
bank    equ     0xa3



;-----------------------------------------------------------
        org     $2400

Init    ldx     <weltxt
        ldy     >weltxt
        jmp     PUTLINE

weltxt  dta     c'VBXE BMPview (fx v1.09)',$9b

        ini     Init

;-----------------------------------------------------------
        org     $2400

Start   cld
        jsr     ClearScreen

        WinSetup #winbuf

err
ferr
loop
        WinDestroy
        FileClose
        FileBoxExecute #fmask #dirbuf #dirbuf_len
        jpl     try
        mva     #0 MB_CPU
        rts

try     FileOpen #fm.fname #CIO_Read
        jmi     err

        SetupTransfer #bitmap_header #CIO_getdata
        jmi     err

        lda     #0
        sta     bfType

        NextTransfer #54
        jmi     err

        lda     bfType
        cmp     #'B'
        jne     ferr
        lda     bfType+1
        cmp     #'M'
        jne     ferr

        lda     biSize
        cmp     #40
        jne     ferr

        lda     biWidth
        cmp     <320
        jne     ferr
        lda     biWidth+1
        cmp     >320
        jne     ferr
        lda     biWidth+2
        ora     biWidth+3
        jne     ferr

        lda     biHeight+1
        ora     biHeight+2
        ora     biHeight+3
        jne     ferr

        lda     biBitCount
        cmp     #8
        jne     ferr
        lda     biBitCount+1
        jne     ferr

        lda     biCompression
        ora     biCompression+1
        ora     biCompression+2
        ora     biCompression+3
        jne     ferr

        WinCreate #11 #8 #15 #5
        SetCursor #0 #2 #2
        PutText #tbot #0 #0 #1 #SRC_INTERNAL+DST_INTERNAL

        jmp     loa

tbot    .byte   "Loading ...",1

loa
        SetupTransfer #palette #CIO_getdata
        jmi     err
        NextTransfer #256*4
        jmi     err

        mwa     #palette ptr1
        ldx     #0
        stx     CSEL
        mva     #1 PSEL
pl1     ldy     #2
        lda     (ptr1),y
        sta     CR
        dey
        lda     (ptr1),y
        sta     CG
        dey
        lda     (ptr1),y
        sta     CB
        adw     ptr1 #4
        inx
        jne     pl1

        mva     biHeight lines
        mwa     #MEMAC_B_WINDOW ptr1
        ldy     #0
        mva     #0 bank
        ora     #0x80
        sta     MB_CPU
        jmp     lp4

lp1     lda     bank
        ora     #0x80
        sta     MB_CPU

lp0     lda     phase
        jne     ph2
        lda     linebuf,x
        inx
        jne     lp2
        inc     phase
        jmp     lp2
ph2     cpx     #64
        bcc     ph21

        dec     lines
        jeq     cxdl

lp4     sty     ysav
        mva     #0 MB_CPU
        SetupTransfer #linebuf #CIO_getdata
        jmi     err
        NextTransfer #320
        jmi     err
        ldx     #0
        stx     phase
        ldy     ysav
        jmp     lp1

ph21    lda     linebuf+0x100,x
        inx
lp2     sta     (ptr1),y
        iny
        bne     lp0
        inc     ptr1+1
        jpl     lp0
        mwa     #MEMAC_B_WINDOW ptr1
        inc     bank
        jmp     lp1

cxdl    lda     #0
        sta     adrez
        sta     adrez+1
        ;lda     #0x04
        sta     adrez+2
        mva     biHeight lines

cx1     dec     lines
        jeq     cx2
        lda     adrez
        add     <320
        sta     adrez
        lda     adrez+1
        adc     >320
        sta     adrez+1
        lda     adrez+2
        adc     #0
        sta     adrez+2
        jmp     cx1

cx2
        inc     bank
        lda     bank
        ora     #0x80
        sta     MB_CPU
        mwa     #MEMAC_B_WINDOW ptr1

        mva     biHeight lines
        cmp     #241
        bcc     cx21
        mva     #240 lines

cx21    lda     #240
        sub     lines
        lsr
        jeq     cx4
        sub     #1
        sta     lines

cx3     ldy     #0
        lda     <XDLC_RPTL
        sta     (ptr1),y
        iny
        lda     >XDLC_RPTL
        sta     (ptr1),y
        iny
        lda     lines
        sta     (ptr1),y
        adw     ptr1 #3

cx4     mva     biHeight lines

cx5     ldy     #0
        lda     lines
        cmp     #1
        jne     cx51
        lda     <XDLC_GMON+XDLC_OVADR+XDLC_END
        sta     (ptr1),y
        iny
        lda     >XDLC_GMON+XDLC_OVADR+XDLC_END
        jmp     cx52
cx51    lda     <XDLC_GMON+XDLC_OVADR
        sta     (ptr1),y
        iny
        lda     >XDLC_GMON+XDLC_OVADR
cx52    sta     (ptr1),y
        iny
        lda     adrez
        sta     (ptr1),y
        iny
        lda     adrez+1
        sta     (ptr1),y
        iny
        lda     adrez+2
        sta     (ptr1),y
        iny
        lda     #0
        sta     (ptr1),y
        iny
        sta     (ptr1),y
        adw     ptr1 #7

        lda     adrez
        sub     <320
        sta     adrez
        lda     adrez+1
        sbc     >320
        sta     adrez+1
        lda     adrez+2
        sbc     #0
        sta     adrez+2

        dec     lines
        jne     cx5

        lda     #0
        sta     XDL_ADR0
        lda     bank
        asl
        asl
        asl
        asl
        asl
        asl
        sta     XDL_ADR1
        lda     bank
        lsr
        lsr
        sta     XDL_ADR2

        mva     #0 DMACTLS
        mva     #VC_XDL_ENABLED+VC_NTR VIDEO_CONTROL

        GetKey

        mva     #0x22 DMACTLS
        mva     #0 VIDEO_CONTROL

        jmp     loop


adrez   .long   0

ClearScreen .local

        ;skasuj zawartość ekranu
        mwa     SAVMSC ptr1
        ldx     #24
l1      ldy     #39
        lda     #0
l2      sta     (ptr1),y
        dey
        bpl     l2
        adw     ptr1 #40
        dex
        bne     l1
        rts

        .endl

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

        ;opt     l-
        icl     'filebox.asm'
        ;opt     l+

;-----------------------------------------------------------
fmask   .byte   '*.BMP',$9b

bitmap_header
bfType          .word   0
bfSize          .dword  0
bfR1            .word   0
bfR2            .word   0
bfOffBits       .dword  0
biSize          .dword  0
biWidth         .dword  0
biHeight        .dword  0
biPlanes        .word   0
biBitCount      .word   0
biCompression   .dword  0
biSizeImage     .dword  0
biXPelsPerMeter .dword  0
biYPelsPerMeter .dword  0
biClrUsed       .dword  0
biClrImportant  .byte   0
biClrRotation   .byte   0
biReserved      .word   0

dirbuf_len      equ     0x1000

dirbuf  equ     *
palette equ     *
linebuf equ    *
winbuf  equ     0x8000

init2   jmp     FileBoxInit

        ini     init2
        run     Start