;       Static Name Aliases
;
        TITLE   vbufwin
;       NAME    vbufwin.C

;EQUATES
;
        buffer = 4
        row = 8
        col = 10
        width = 12
        height = 14
        direction = 16
        retrace = 18

        .287
_TEXT   SEGMENT  BYTE PUBLIC 'CODE'
_TEXT   ENDS
_DATA   SEGMENT  WORD PUBLIC 'DATA'
_DATA   ENDS
CONST   SEGMENT  WORD PUBLIC 'CONST'
CONST   ENDS
_BSS    SEGMENT  WORD PUBLIC 'BSS'
_BSS    ENDS
DGROUP  GROUP   CONST,  _BSS,   _DATA
        ASSUME  CS: _TEXT, DS: DGROUP, SS: DGROUP, ES: DGROUP
_TEXT      SEGMENT

        PUBLIC  _vbufwin
_vbufwin        PROC NEAR
        push    bp
        mov     bp,sp
        push    si
        push    di
        push    ds

;       buffer = 4
;       row = 8
;       col = 10
;       width = 12
;       height = 14
;       direction = 16
;       retrace = 18

        mov     ah,15
        int     10h
        cmp     al,7
        jne     $color

        mov     ax,0b000h
        xor     dx,dx                      ;retrace
        jmp     SHORT $voffset
$color:
        mov     ax,0b800h
        mov     dx,[bp+retrace]
$voffset:
        push    ax
        mov     ax,160
        mul     BYTE PTR [bp+row] ;row
        mov     bx,WORD PTR [bp+col]       ;col
        shl     bx,1
        add     ax,bx
        mov     bx,[bp+direction]               ;direction
        cmp     bx,0
        jne     $put
        pop     ds
        mov     si,ax
        les     di,[bp+buffer]                ;buffer
        jmp     short $outer
$put:
        pop     es
        mov     di,ax
        lds     si,[bp+buffer]                ;buffer

$outer:
        mov     cx,[bp+height]               ;height
        jcxz    $cleanup
$loop1:
        cmp     bx,0
        jne     push_di
        push    si
        jmp     short $inner
push_di:
        push    di
$inner:
        push    cx
        mov     cx,[bp+width]               ;width
        jcxz    $loop2end
$loop2:
        cmp     dx,0
        je      stashit
        push    dx

        mov     dx,3dah
$WC30:
        in      al,dx
        test    al,1
        jnz     $WC30
        cli
$WC32:
        in      al,dx
        test    al,1
        jz      $WC32
        pop     dx
stashit:
        movsw
        sti
        loop    $loop2
$loop2end:
        pop     cx
        cmp     bx,0
        jne     pop_di
        pop     si
        add     si,160
        loop    $loop1
        jmp     short $cleanup
pop_di:
        pop     di
        add     di,160
        loop    $loop1

$cleanup:
        pop     ds
        pop     di
        pop     si
        mov     sp,bp
        pop     bp
        ret

_vbufwin        ENDP
_TEXT   ENDS
END
