NAME      HERCULES
;
;Hercules Card Initialization
;
_TEXT   SEGMENT  BYTE PUBLIC 'CODE'
_TEXT   ENDS
CONST   SEGMENT  WORD PUBLIC 'CONST'
CONST   ENDS
_BSS    SEGMENT  WORD PUBLIC 'BSS'
_BSS    ENDS
_DATA   SEGMENT  WORD PUBLIC 'DATA'
_DATA   ENDS

DGROUP  GROUP   CONST, _BSS, _DATA
        ASSUME  CS:_TEXT, DS: DGROUP, SS: DGROUP, ES: DGROUP

PUBLIC _GHERCMODE, _THERCMODE

_DATA     SEGMENT

INDEX     equ       03B4h               ;Port Addresses
CNTRL     equ       03B8h
CONFIG    equ       03BFh

GTABLE    db        35h, 2Dh, 2Eh, 07h
          db        5Bh, 02h, 57h, 57h
          db        02h, 03h, 00h, 00h

TTABLE    db        61h, 50h, 52h, 0Fh
          db        19h, 06h, 19h, 19h
          db        02h, 0Dh, 0Bh, 0Ch

_DATA     ENDS

;
; GRAPHICS MODE - Programs the 6845 CRT controller for the 720x348
; graphics mode.  The active page for both writing and display is set
; to the defaults value of page 0.
;
_TEXT SEGMENT

_GHERCMODE          proc      near

          push      bp                  ;FROM HERE...
          mov       bp,sp               ; ...
          push      di                  ; ...
          push      si                  ; TO HERE IS STANDARD C ENTRY
          push      es

          mov       dx,CONFIG
          mov       al,1
          out       dx,al               ; Configuration Switch HALF

          mov       al,2                ; Set al for GRAPHICS mode
          lea       si,GTABLE
          mov       bx,0
          mov       cx,4000h
          call      SETMD

          pop       es
          pop       si                  ; STANDARD C EXIT FROM HERE
          pop       di                  ; THRU "RET"
          mov       sp,bp
          pop       bp
          ret

_GHERCMODE          endp
;
; TEXT MODE - program the 6845 and CRT control register to
; produce text mode.
;
_THERCMODE          proc      near

          push      bp                  ;FROM HERE...
          mov       bp,sp               ; ...
          push      di                  ; ...
          push      si                  ; TO HERE IS STANDARD C ENTRY
          push      es

          mov       dx,CONFIG
          mov       al,0
          out       dx,al               ; Configuration Switch DIAG

          mov       al,20h              ; Set al for TEXT mode
          lea       si,TTABLE
          mov       bx,720h
          mov       cx,2000
          call      SETMD

          pop       es
          pop       si                  ; STANDARD C EXIT FROM HERE
          pop       di                  ; THRU "RET"
          mov       sp,bp
          pop       bp
          ret

_THERCMODE          endp
;
; Sets mode to graphics or text (called by gmode or tmode)
; depending on register al.
;         si = parameter table
;         cx = number of words to be cleared
;         bx = blank value
;
SETMD     proc      near
          push      ax                            ; Placed Here to be
          push      bx                            ; pulled for use by
          push      cx                            ; the program.
;
; change mode but without the screen being on.
;
          mov       dx,CNTRL
          out       dx,al
;
; initialize the 6845
;
          mov       ax,ds
          mov       es,ax               ; also point es:si
                                        ; to the parameter table.
          mov       dx,INDEX
          mov       cx,12               ; 12 parameters to be output

          xor       ah,ah               ; starting from reg. 0

PARMS:    mov       al,ah
          out       dx,al               ; output register number

          inc       dx
          lodsb
          out       dx,al               ; output data

          inc       ah                  ; next value
          dec       dx
          loop      PARMS

          pop       cx                  ; clear the buffer
          mov       ax,0B000h
          cld

          mov       es,ax
          xor       di,di
          pop       ax
          rep       stosw
;
; turn on the page 0 screen
;
          mov       dx,CNTRL
          pop       ax
          add       al,8
          out       dx,al

          ret
SETMD     endp

_TEXT     ENDS
          END