;*DDK*************************************************************************/
;
; COPYRIGHT    Copyright (C) 1995 IBM Corporation
;
;    The following IBM OS/2 WARP source code is provided to you solely for
;    the purpose of assisting you in your development of OS/2 WARP device
;    drivers. You may use this code in accordance with the IBM License
;    Agreement provided in the IBM Device Driver Source Kit for OS/2. This
;    Copyright statement may not be removed.;
;*****************************************************************************/
        page    ,132
;/*****************************************************************************
;*
;* SOURCE FILE NAME = CRITSEC.ASM
;*
;* DESCRIPTIVE NAME = Critical Sections
;*
;*
;* VERSION      V2.0
;*
;* DATE         01/21/89
;*
;* DESCRIPTION  This file contains critical sections that must be aligned so  
;*              as to not cross page boundaries, as well as any data they     
;*              access.  We use the fact that segments are aligned on page    
;*              boundaries, so that our critical sections will be protected   
;*              from page faults as long as they don't fill a whole page.     
;*              This can be checked at initialization in a firewall.  In      
;*              addition, there are .erre directives at the end of the code   
;*              segment in this file to halt compilation if it's size exceeds 
;*              a page size.  Unfortunately, the system page size can change  
;*              in future releases of OS/2.  If this file compiles, but the   
;*              firewall causes a rip, then the page size has probably        
;*              changed.  In that case, adjust the PAGESIZE constant defined  
;*              on the "make" command line.  Since there's no way to guarantee
;*              that the desired data segment will be in RAM when it is       
;*              accessed, any data accessed from within a critical section    
;*              must be stored in a fixed memory segment, in this case        
;*              PtrData.  The Data segment, if any, in this file may contain  
;*              relevant data for the critical code, but data in it cannot be 
;*              accessed from within the cli/sti bracket.                     
;*                                                                            
;*
;* FUNCTIONS    reprogram_hardware
;*              InitTonysBarNGrill
;*                                
;* NOTES        NONE
;*
;* STRUCTURES   NONE
;*
;* EXTERNAL REFERENCES
;*
;*              NONE
;*
;* EXTERNAL FUNCTIONS
;*
;*              NONE
;*
;* CHANGE ACTIVITY =
;*   DATE      FLAG        APAR   CHANGE DESCRIPTION
;*   --------  ----------  -----  --------------------------------------
;*   mm/dd/yy  @Vr.mpppxx  xxxxx  xxxxxxx
;*   01/05/89                     Moved to beginning of _TEXT segment to     
;*                                avoid page faults within cli/sti bracket.
;*   01/17/89                     Subroutinized InitTonysBarNGrill and    
;*                                moved it to the beginning of the _TEXT
;*                                segment.
;*   01/21/89                     Written by Bob Grudem
;*****************************************************************************/


.286p
        .xlist
        include cmacros.inc
        include pmgre.inc
        include driver.inc
        include egafam.inc
        include egamemf.inc
        .list


sBegin  PtrData
        externB screen_busy
sEnd    PtrData

        page
sBegin  Data

;/*
;** Table of data for programming the EGA/VGA, used by reprogram_hardware.
;**
;** The entries with "+1" in the port address are marked for special
;** handling, which means the output must be performed with asynchronous
;** reset.  Since extended time without synchronous reset can cause
;** data loss in video RAM, these entries are output within a critical
;** section to prevent interrupts, and the whole function has been moved
;** to the beginning of its segment to be sure of not crossing a page
;** boundary.  This table is accessed outside of the critical section,
;** so it can be placed in the main Data segment.
;**
;** Some of the entries in this table set important mode values, some others
;** simply set registers to known values.
;*/

        public  inittbl
inittbl label   word

        

        dw      EGA_BASE+SEQ_ADDR+1,  SEQ_MODE+(SM_EXTENDED+SM_ODD_PLANE)*256 ;memory mode
        dw      EGA_BASE+SEQ_ADDR,    SEQ_MAP_MASK+MM_ALL*256 ;enable all planes
        dw      EGA_BASE+GRAF_ADDR,   GRAF_SET_RESET   ;set to black
        dw      EGA_BASE+GRAF_ADDR,   GRAF_ENAB_SR     ;disable all planes
        dw      EGA_BASE+GRAF_ADDR,   GRAF_COL_COMP    ;set to black
        dw      EGA_BASE+GRAF_ADDR,   GRAF_DATA_ROT    ;no rotation, rop=S
        dw      EGA_BASE+GRAF_ADDR,   GRAF_READ_MAP    ;no color compare
        dw      EGA_BASE+GRAF_ADDR,   GRAF_MODE        ;write/read mode 0
        dw      EGA_BASE+GRAF_ADDR+1, GRAF_MISC+(MS_NON_ALPHA+MS_A0000_64K)*256 ;grafx misc
        dw      EGA_BASE+GRAF_ADDR,   GRAF_CDC+0Fh*256 ;set to white
        dw      EGA_BASE+GRAF_ADDR,   GRAF_BIT_MASK+0FFh*256  ;enable all bits
        dw      0                                      ;end-of-table marker
        externW wSVGAtype
sEnd    Data


        page
sBegin  Code
        assumes cs,Code
;/*
;**-----------------------------------------------------------------------;
;*/

    public  begin_crit_code
begin_crit_code label   byte

;/*
;**-----------------------------------------------------------------------;
;*/


;/***************************************************************************
;*
;* FUNCTION NAME = reprogram_hardware  
;*
;* DESCRIPTION   = The EGA/VGA is reprogrammed to fit our needs.  The details
;*                 are contained in the table inittbl above.  Some output    
;*                 sequences in the table could result in loss of data in    
;*                 video memory if interrupted.  The possible interruptions  
;*                 are page faults and hardware interrupts.  To protect      
;*                 against hardware interrupts, cli and sti instructions     
;*                 surround the critical section of code.  To protect against
;*                 page faults, the function is near the beginning of its    
;*                 segment.  Since segments are loaded on page boundaries, we
;*                 can without much difficulty arrange that the critical     
;*                 section does not cross a page boundary (some compile-time 
;*                 checking, and a run-time firewall to be precise).         
;*                                                                           
;*                 Registers Preserved:                                                                
;*                       BX,BP,DS,ES                                                                   
;*                 Registers Destroyed:                                                                
;*                       AX,CX,DX,SI,DI,FLAGS                                                          
;*                                                                           
;* INPUT         = NONE
;* OUTPUT        = NONE
;*
;* RETURN-NORMAL = NONE
;* RETURN-ERROR  = NONE
;*
;**************************************************************************/

        assumes ds,Data
        assumes es,EGAMem                         ;ES:0 -> top of VRAM

cProc   reprogram_hardware,<NEAR,PUBLIC>
cBegin

        mov     dx,EGA_BASE+IN_STAT_1
        in      al,dx                             ;reset the ATC flip-flop
        mov     dl,IN_STAT_1-20h
        in      al,dx                             ;use mono port too, for good measure
        mov     dl,ATTR_WRITE
        sub     al,al
        out     dx,al                             ;clear palette source bit to turn video off

        mov     si,DataOFFSET inittbl
restore_init:                                     ;initialize all regs to default
        lodsw
        or      ax,ax
        jz      restore_initdone
        mov     dx,ax
        lodsw
        test    dx,1                              ;special-handling bit set?
        jz      restore_initcont                  ;no

;/*
;** This next block uses the carry flag to indicate that special handling
;** is in effect.  Notice that after the "stc", no instruction alters the
;** carry flag before the "jnc" can read it.
;*/

        and     dx,not 1                          ;clear special-handling bit
        stc
        mov     di,dx
        mov     cx,ax

        cli                                       ;can't use out16 because of this CLI
        mov     dl,SEQ_ADDR
        mov     ax,0100h                          ;clear synchronous reset bit
        out     dx,ax

IFDEF VGA                                         ;FIX for timing problem on IBM VGA
                                                  ;bypass trashing "mystery reg" 7
                                                  ;for these chipsets
        pushf
        mov     ax,wSVGAtype
        cmp     ax,VIDEO7_ADAPTER
        je      @F
        cmp     ax,TSENG_ADAPTER
        je      @F
        mov     ax,0007h                          ;select "mystery reg" 7 of sequencer
        out     dx,ax                             ;and write a 0 to it
        mov     ax,013Fh                          ;select "mystery reg" 3Fh of the CRTC
        mov     dl,CRTC_ADDR                      ; 
        out     dx,ax                             ;write a 1 to the color location
        mov     dl,CRTC_ADDR-20h                  ; 
        out     dx,ax                             ;and to the mono location as well
@@:
        popf
ENDIF                                             ;END FIX for timing problem on IBM VGA
        mov     ax,cx
        mov     dx,di
restore_initcont:
        out     dx,ax                             ;do the OUT specified in the tbl
        jnc     restore_init

        mov     dl,SEQ_ADDR                       ;if special handling was required
        mov     ax,0300h                          ;turn synchronous reset bit back on
        out     dx,ax
        sti
        jmp     restore_init                      ;loop until table exhausted
restore_initdone:
        mov     dl,SEQ_ADDR
        mov     al,SEQ_MAP_MASK                   ;leave sequencer addr positioned here
        out     dx,al
cEnd

        page
;/***************************************************************************
;*
;* FUNCTION NAME = InitTonysBarNGrill   
;*
;* DESCRIPTION   = This function sets the EGA to color-write mode.  This     
;*                 means that when a byte is written to video RAM, the four  
;*                 lower bits are treated as a solid color to write.  For    
;*                 example, if the value written out is 00001010b, then the  
;*                 EGA will fill plane 0 through 3 with values 00h, FFh, 00h,
;*                 FFh, respectively.  In processor-write mode, planes 0     
;*                 through 3 would get 0Ah, 0Ah, 0Ah, 0Ah, respectively.     
;*                 Once a color has been written to video RAM, it can be read
;*                 back to fill the EGA's latches.  The EGA must be set to   
;*                 data-read mode, so that the latches get each plane's data 
;*                 without any interpretation.  Since the EGA is assumed to  
;*                 be processor-write mode throughout the display driver,    
;*                 reset it before exiting.  Furthermore, since the          
;*                 interrupt-time pointer drawing code must be able to change
;*                 and restore the EGA's state, and our state detection code 
;*                 assumes the write mode is processor-write, these          
;*                 manipulations must be done inside a cli/sti critical      
;*                 section to prevent mouse interrupts.                      
;*                                                                           
;*                 Registers Destroyed:                                                                 
;*                       AX,BX,FLAGS                                                                    
;*                 Registers Preserved:                                                                 
;*                       CX,DX,SI,DI,BP,DS,ES                                                           
;*                                                                           
;* INPUT         = BH = background color                   
;*                 DX = EGA_BASE + GRAF_ADDR                                                                         
;*                 all EGA color planes enabled for output                                                           
;*                                                                           
;* OUTPUT        = NONE
;*
;* RETURN-NORMAL = NONE
;* RETURN-ERROR  = NONE
;*
;**************************************************************************/

        assumes ds,nothing
        assumes es,EGAMem

cProc   InitTonysBarNGrill,<NEAR,PUBLIC>
cBegin

        mov     ax,(M_COLOR_WRITE + M_DATA_READ) shl 8 + GRAF_MODE
        cli
        out16   dx,ax
        mov     tonys_bar_n_grill,bh             ;store color in video RAM
        mov     bh,tonys_bar_n_grill             ;read back to set the latches
        mov     ax,(M_PROC_WRITE + M_DATA_READ) shl 8 + GRAF_MODE
        out16   dx,ax
        sti
cEnd

;/*
;** This label is compared to DOSPAGESIZE in DeviceSpecificInit
;*/

        public  end_crit_code
end_crit_code   label   byte


sEnd    Code

end
