;       SCCSID = @(#)bvfmode2.asm       6.1 90/11/16
;SCCSID = @(#)bvfmode2.asm      6.2 90/01/23
; ****************************************************************************
; *                                                                          *
; *                       IBM/Microsoft Confidential                         *
; *                                                                          *
; *                 Copyright (c) IBM Corporation  1987, 1990                *
; *                 Copyright (c) Microsoft Corp.  1987, 1990                *
; *                           All Rights Reserved                            *
; *                                                                          *
; ****************************************************************************
        PAGE    58,132
        TITLE   BvsMode2.Asm -- Base Video Subsystem Level 2 Subroutines

;/*********************** START OF SPECIFICATIONS ***********************/
;/*                                                                     */
;/* SOURCE FILE NAME:  BvsMode2.Asm                                     */
;/*                                                                     */
;/* DESCRIPTIVE NAME:  Base Video Subsystem Level 2 Routines            */
;/*                                                                     */
;/* COPYRIGHT:  na                                                      */
;/*                                                                     */
;/* STATUS:     na                                                      */
;/*                                                                     */
;/* NOTES:   Executes on level 2                                        */
;/*                                                                     */
;/*    DEPENDENCIES:  none                                              */
;/*                                                                     */
;/*    RESTRICTIONS:  none                                              */
;/*                                                                     */
;/*    PATCH LABEL:   na                                                */
;/*                                                                     */
;/* ENTRY POINT:  OutWord,OutBytes,OutDxAl,                             */
;/*               PartialSave,PartialRest                               */
;/*                                                                     */
;/* EXTERNAL REFERENCES: None                                           */
;/*                                                                     */
;/************************ END OF SPECIFICATIONS ************************/

        .xlist
        INCLUDE bvfcb.inc               ;BVS Data Structures
        INCLUDE struc.inc               ;Structured Macros
        .list

BVSDSEG SEGMENT PARA PUBLIC 'DATA'
colorc  db      00h,00h,00h,00h,00h,2Ah,00h,2Ah,00h,00h,2Ah,2Ah
        db      2Ah,00h,00h,2Ah,00h,2Ah,2Ah,2Ah,00h,2Ah,2Ah,2Ah
        db      00h,00h,15h,00h,00h,3Fh,00h,2Ah,15h,00h,2Ah,3Fh
        db      2Ah,00h,15h,2Ah,00h,3Fh,2Ah,2Ah,15h,2Ah,2Ah,3Fh

        db      00h,15h,00h,00h,15h,2Ah,00h,3Fh,00h,00h,3Fh,2Ah
        db      2Ah,15h,00h,2Ah,15h,2Ah,2Ah,3Fh,00h,2Ah,3Fh,2Ah
        db      00h,15h,15h,00h,15h,3Fh,00h,3Fh,15h,00h,3Fh,3Fh
        db      2Ah,15h,15h,2Ah,15h,3Fh,2Ah,3Fh,15h,2Ah,3Fh,3Fh

        db      15h,00h,00h,15h,00h,2Ah,15h,2Ah,00h,15h,2Ah,2Ah
        db      3Fh,00h,00h,3Fh,00h,2Ah,3Fh,2Ah,00h,3Fh,2Ah,2Ah
        db      15h,00h,15h,15h,00h,3Fh,15h,2Ah,15h,15h,2Ah,3Fh
        db      3Fh,00h,15h,3Fh,00h,3Fh,3Fh,2Ah,15h,3Fh,2Ah,3Fh

        db      15h,15h,00h,15h,15h,2Ah,15h,3Fh,00h,15h,3Fh,2Ah
        db      3Fh,15h,00h,3Fh,15h,2Ah,3Fh,3Fh,00h,3Fh,3Fh,2Ah
        db      15h,15h,15h,15h,15h,3Fh,15h,3Fh,15h,15h,3Fh,3Fh
        db      3Fh,15h,15h,3Fh,15h,3Fh,3Fh,3Fh,15h,3Fh,3Fh,3Fh

monoc   db      00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h
        db      00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h
        db      2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah
        db      2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah

        db      00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h
        db      00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h
        db      3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh
        db      3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh

        db      00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h
        db      00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h
        db      2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah
        db      2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah,2Ah

        db      00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h
        db      00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h,00h
        db      3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh
        db      3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh,3Fh

sumregs db      00h,00h,00h,05h,05h,05h,19h,19h,19h,1Dh,1Dh,1Dh
        db      0Dh,0Dh,0Dh,11h,11h,11h,25h,25h,25h,2Ah,2Ah,2Ah
        db      02h,02h,02h,07h,07h,07h,1Bh,1Bh,1Bh,20h,20h,20h
        db      0Fh,0Fh,0Fh,14h,14h,14h,28h,28h,28h,2Ch,2Ch,2Ch

        db      0Ch,0Ch,0Ch,11h,11h,11h,25h,25h,25h,2Ah,2Ah,2Ah
        db      19h,19h,19h,1Eh,1Eh,1Eh,32h,32h,32h,36h,36h,36h
        db      0Fh,0Fh,0Fh,13h,13h,13h,27h,27h,27h,2Ch,2Ch,2Ch
        db      1Bh,1Bh,1Bh,20h,20h,20h,34h,34h,34h,39h,39h,39h

        db      06h,06h,06h,0Bh,0Bh,0Bh,1Fh,1Fh,1Fh,24h,24h,24h
        db      13h,13h,13h,18h,18h,18h,2Ch,2Ch,2Ch,30h,30h,30h
        db      09h,09h,09h,0Dh,0Dh,0Dh,21h,21h,21h,26h,26h,26h
        db      15h,15h,15h,1Ah,1Ah,1Ah,2Eh,2Eh,2Eh,33h,33h,33h

        db      13h,13h,13h,17h,17h,17h,2Bh,2Bh,2Bh,30h,30h,30h
        db      1Fh,1Fh,1Fh,24h,24h,24h,38h,38h,38h,3Dh,3Dh,3Dh
        db      15h,15h,15h,1Ah,1Ah,1Ah,2Eh,2Eh,2Eh,32h,32h,32h
        db      22h,22h,22h,26h,26h,26h,3Ah,3Ah,3Ah,3Fh,3Fh,3Fh
BVSDSEG ENDS

BVSNSEG SEGMENT PARA PUBLIC 'DATA'
BVSNSEG ENDS

BVS2SEG SEGMENT BYTE PUBLIC 'CODE'
        ASSUME  CS:BVS2SEG

;/*********************** START OF SPECIFICATIONS ***********************/
;/*                                                                     */
;/* SUBROUTINE NAME:  GetPals                                           */
;/*                                                                     */
;/* DESCRIPTIVE NAME:  Read the Palette Registers into the Shadow Buffer*/
;/*                                                                     */
;/* FUNCTION:   Read the palette registers into the Shadow Buffer       */
;/*                                                                     */
;/* NOTES:  Executes on Level 2 via Call Gate with no stack parameters. */
;/*         Routine transcribed from EGA BIOS.                          */
;/*                                                                     */
;/* ENTRY POINT:  GetPals                                               */
;/*   LINKAGE:    Far Call from BvsSetMode and BvsGetState              */
;/*                                                                     */
;/* INPUT:  None                                                        */
;/*                                                                     */
;/* EXIT-NORMAL:  Shadow buffer updated                                 */
;/*                                                                     */
;/* EXIT-ERROR:  None                                                   */
;/*                                                                     */
;/* EFFECTS:  None                                                      */
;/*                                                                     */
;/* INTERNAL REFERENCES:  None                                          */
;/*   ROUTINES:  ReadSwitch                                             */
;/*                                                                     */
;/* EXTERNAL REFERENCES:  None                                          */
;/*   ROUTINES:  None                                                   */
;/*                                                                     */
;/************************ END OF SPECIFICATIONS ************************/

GetPals PROC    FAR
        PUBLIC  GetPals

        pushall
        push    ds
        push    es

        mov     ax,bvsnseg
        mov     ds,ax                   ;PPBVSCB ptr
        mov     ds,[bx].ppbvs_bvssel
        push    ds
        pop     es

        test    [bx].bvs_devtype,vgaflags ;@@Z
        .if     nz
            mov     al,byte ptr [bx].bvs_physport
            add     al,6
            mov     [bx].bvs_palregsoff+1,al ;Update the input port

            mov     di,bvs_palregs      ;First reg in shadow buffer
            mov     cx,bvs_palregson-bvs_palregs ;Number regs to read
            mov     dx,[bx].bvs_physport
            add     dl,6                ;Get status port
            mov     ah,dl               ;Save the port

            cli
            .repeat
                in      al,dx           ;Synchronize Attr Addr Reg
                mov     dl,0C0h
                mov     al,bvs_palregson-bvs_palregs
                sub     al,cl
                out     dx,al           ;Point to current palette reg
                mov     dl,0C1h
                in      al,dx           ;Read the palette reg
                stosb                   ;Copy it to shadow buffer
                mov     dl,ah
            .loop

         push   ax                               ;wpw24789
         push   cx                               ;wpw24789
         push   dx                               ;wpw24789
         mov    dx, 03cch                        ;wpw24789 Test whether a
         in     al, dx                           ;wpw24789 monochrome adapter
         mov    cl, al                           ;wpw24789 is in the system.
         or     al, 01h                          ;wpw24789
         mov    dx, 03c2h                        ;wpw24789
         out    dx, al                           ;wpw24789
         mov    dx, 03b4h                        ;wpw24789
         mov    al, 0fh                          ;wpw24789
         out    dx, al                           ;wpw24789
         inc    dx                               ;wpw24789
         in     al, dx                           ;wpw24789
         xor    al, 0ffh                         ;wpw24789
         mov    ah, al                           ;wpw24789
         out    dx, al                           ;wpw24789
         in     al, dx                           ;wpw24789

          .if    <al eq ah> and                  ;wpw24789 If a monochrome
            test   [bx].bvs_type,mode_f_color    ;wpw24789 adapter is in the
           .if    z                              ;wpw24789 system and it
              mov    dx, 03c2h                   ;wpw24789 is in mono mode,
              mov    al, cl                      ;wpw24789
              out    dx, al                      ;wpw24789
              pop    dx                          ;wpw24789
              add    dl,20h                      ;wpw24789
              in     al,dx                       ;wpw24789
              mov    dl,0C0h                     ;wpw24789 Turn off the color
              mov    al,000h                     ;wpw24789 monitor.
              out    dx,al                       ;wpw24789

          .else
              mov    dx, 03c2h                   ;wpw24789
              mov    al, cl                      ;wpw24789
              out    dx, al                      ;wpw24789
              pop    dx                          ;wpw24789
              in     al,dx                       ;@@A
              mov    dl,0C0h                     ;@@A
              mov    al,020h                     ;@@A
              out    dx,al                       ;Re-enable the video ;@@A
          .endif
              pop    cx                          ;wpw24789
              pop    ax                          ;wpw24789
              sti
        .else
            mov     ax,40h
            mov     ds,ax
            lds     si,[bx].real_saveptr
            lds     si,dword ptr [si+4] ;Ptr to save table
            mov     ax,ds
            or      ax,si
            .if     nz                  ;Ensure valid ptr
                mov     di,bvs_palregs
                mov     cx,16
                rep     movsb           ;Move palregs to BVSCB
            .endif
        .endif

        pop     es
        pop     ds
        popall
        ret
GetPals ENDP

SetCLUT PROC    FAR
        PUBLIC  SetCLUT
        PUSH    ES
        push    ds
        push    si
        push    ax
        push    cx
        push    dx

        mov     ax,bvsdseg
        mov     ds,ax

        MOV     DX,BVSNSEG              ;get the per process selector
        MOV     ES,DX                   ;point to the per process data
        mov     si,offset colorc
        .IF     <ES:[BX].ppbvs_bvssel e BX> ;if there is not a BVSCB
            mov     dx,3cch             ;misc register
            in      al,dx               ;get misc value
            TEST    AL,1                ;check for color or monochrome
            .if     z                   ;Mono display
                mov     si,offset monoc
            .endif
        .ELSE                           ;there is a bvscb
            MOV     ES,ES:[BX].ppbvs_bvssel ;point to the BVSCB
            test    es:[bx].bvs_type,mode_f_color
            .if     z                   ;Mono mode
                mov     si,offset monoc
            .else
                test    es:[bx].bvs_type,mode_f_uncolor ;@@Z
                .if     nz or           ;Black and White Mode or ;@@Z
                test    es:[bx].bvs_devtype,vgmflag+vgpflag ;@@Z
                .if     nz              ;Mono display
                    mov     si,offset sumregs ;Use summed registers
                .endif
            .endif
        .ENDIF

        mov     cx,192
        mov     dx,3C8h
        sub     AX,AX

        .FOR    BX = 1 TO 4
            PUSH    CX                  ;save the count
            PUSH    SI                  ;save the source offset

            cli
            out     dx,al
            INC     DX

            .REPEAT                     ;do it
                LODSB                   ;get a byte
                OUT     DX,AL           ;send the color value
            .LOOP
            sti

            POP     SI                  ;restore the source offset
            POP     CX                  ;restore the count
            DEC     DX
            ADD     AH,64               ;point to the next set of regs
            MOV     AL,AH
        .NEXT   BX

        SUB     BX,BX
        pop     dx
        pop     cx
        pop     ax
        pop     si
        pop     ds
        POP     ES

        ret
SetCLUT ENDP

BVS2SEG ENDS
        END

;@@A = 05/04/88 STJ, Re-enable video after reading palette, P2732
;@@Z = 07/17/88 STJ, Add flags for other VGA types
;wpw24789= 1/11/93 WPW, Check whether the system has a monochrome adapter.
           If it has and it is in mono mode, turn off the color monitor.
           If it is in color mode, turn on the color monitor.
