;------------------------------------------------------------------------;
;                        BIOS Display I/O macroses.                      ;
;------------------------------------------------------------------------;

;---------------------------- Color constants ---------------------------;
Black           EQU      0
Blue            EQU      1
Green           EQU      2
Cyan            EQU      3
Red             EQU      4
Magenta         EQU      5
Brown           EQU      6
Gray            EQU      7
DGray           EQU      8
LBlue           EQU      9
LGreen          EQU     10
LCyan           EQU     11
LRed            EQU     12
LMagenta        EQU     13
Yellow          EQU     14
White           EQU     15

;--------------------------- Graph constants ----------------------------;
SUL             EQU     "Ú"
SUR             EQU     "¿"
SDL             EQU     "À"
SDR             EQU     "Ù"
SLH             EQU     "Ä"
SLV             EQU     "³"


;------------------------------- CRT vars -------------------------------;
.DATA
Scr_H           DB      25
Scr_L           DB      80
TxtAttr         DB      7
WX              DB      0
WY              DB      0
WL              DB      80
WH              DB      25
C_X             DB      ?
C_Y             DB      ?

;------------------------------- Macroses -------------------------------;
@Border         MACRO Color
	Push	AX
	Push	BX
        Mov     AH, 0BH
        Xor     BH, BH
        Mov     BL, Color
        Int     10H
        Pop	BX
        Pop	AX
ENDM

@GetMode        MACRO           ;AL=Mode, AH=Screen width, BH=Page
        Mov     AH, 0FH
        Int     10H
        Mov     Scr_L, AH
ENDM

@SetMode        MACRO Mode
        Xor     AH, AH
        Mov     AL, Mode
        Int     10H
ENDM

@SetPage        MACRO Page
	Push	AX
        Mov     AH, 5
        Mov     AL, Page
        Int     10H
        Pop	AX
ENDM

@BlinkOff       MACRO
	Push	AX
	Push	BX
        Mov     AX, 1003H
        Xor     BL, BL
        Int     10H
        Pop	BX
        Pop	AX
ENDM

@BlinkOn        MACRO
	Push	AX
	Push	BX
        Mov     AX, 1003H
        Mov     BL, 1
        Int     10H
        Pop	BX
        Pop	AX
ENDM

@SetColor       MACRO CText, CBack
  IFNB <CText>
        And     TxtAttr, 0F0H
        Or      TxtAttr, CText
  ENDIF
  IFNB <CBack>
	Push	CX
        Mov     CH, CBack
        And     TxtAttr, 0FH
        Mov     CL, 4
        ShL     CH, CL
        Or      TxtAttr, CH
        Pop	CX
  ENDIF
ENDM

@Window         MACRO X, Y, L, H
        Mov     WX, X
        Mov     WY, Y
        Mov     WL, L
        Mov     WH, H
ENDM

@ClrScr         MACRO
	Push	AX
	Push	BX
	Push	CX
	Push	DX
        Mov     AX, 0700H
        Mov     BH, TxtAttr
        Mov     CL, WX
        Mov     CH, WY
        Mov     DL, CL
        Add     DL, WL
        Dec     DL
        Mov     DH, CH
        Add     DH, WH
        Dec     DH
        Int     10H
        Pop	DX
        Pop	CX
        Pop	BX
        Pop	AX
ENDM

@ScrollUp       MACRO Count
	Push	AX
	Push	BX
	Push	CX
	Push	DX
        Mov     AL, Count
        Mov     AH, 06
        Mov     BH, TxtAttr
        Mov     CL, WX
        Mov     CH, WY
        Mov     DL, CL
        Add     DL, WL
        Dec     DL
        Mov     DH, CH
        Add     DH, WH
        Dec     DH
        Int     10H
        Pop	DX
        Pop	CX
        Pop	BX
        Pop	AX
ENDM

@ScrollDown     MACRO Count
	Push	AX
	Push	BX
	Push	CX
	Push	DX
        Mov     AL, Count
        Mov     AH, 07
        Mov     BH, TxtAttr
        Mov     CL, WX
        Mov     CH, WY
        Mov     DL, CL
        Add     DL, WL
        Dec     DL
        Mov     DH, CH
        Add     DH, WH
        Dec     DH
        Int     10H
        Pop	DX
        Pop	CX
        Pop	BX
        Pop	AX
ENDM

@GetXY          MACRO Page      ;DL=X, DH=Y.
	Push	AX
	Push	BX
	Push	DX
  IFNB <Page>
        Mov     BH, Page
  ELSE
        Xor     BH, BH
  ENDIF
        Mov     AH, 3
        Int     10H
        Add     DL, WX
        Add     DH, WY
        Mov     C_X, DL
        Mov     C_Y, DH
        Pop	DX
        Pop	BX
        Pop	AX
ENDM

@GotoXY         MACRO X, Y, Page
	Push	AX
	Push	BX
	Push	DX
  IFNB <X>
        Mov     DL, X
  ELSE
        Mov     DL, C_X
  ENDIF
  IFNB <Y>
        Mov     DH, Y
  ELSE
        Mov     DH, C_Y
  ENDIF
  IFNB <Page>
        Mov     BH, Page
  ELSE
        Xor     BH, BH
  ENDIF
        Add     DL, WX
        Add     DH, WY
        Mov     AH, 2
        Int     10H
        Pop	DX
        Pop	BX
        Pop	AX
ENDM

@MoveXY         MACRO RX, RY
        Push    AX
        Push    BX
        Push    CX
        Push    DX
        @GetXY
        Pop     DX
        Pop     CX
        Pop     BX
        Pop     AX
        Add     C_X, RX
        Add     C_Y, RY
        @GotoXY C_X, C_Y
ENDM

@CursorOff      MACRO
	Push	AX
	Push	CX
        Mov     AH, 1
        Mov     CX, 2000H
        Int     10H
        Pop	CX
        Pop	AX
ENDM

@CursorOn       MACRO
	Push	AX
	Push	CX
        Mov     AH, 1
        Mov     CX, 0607H
        Int     10H
        Pop	CX
        Pop	AX
ENDM

@CursorBigOn    MACRO
	Push	AX
	Push	CX
        Mov     AH, 1
        Mov     CX, 0007H
        Int     10H
        Pop	CX
        Pop	AX
ENDM

@KeyPressed     MACRO           ;ZeroFlag=1 - False, ZeroFlag=0 - True
	Push	AX
        Mov     AH, 01
        Int     16H
        Pop	AX
ENDM

@ReadKey        MACRO           ;AH=ScanCode, AL=ASCII
        Xor     AH, AH
        Int     16H
ENDM

@WriteChar      MACRO Char, Loop, Page
	Push	AX
	Push	BX
	Push	CX
        Mov     AH, 09H
  IFNB <Page>
        Mov     BH, Page
  ELSE
        Xor     BH, BH
  ENDIF
        Mov     AL, Char
        Mov     BL, TxtAttr
  IFNB <Loop>
        Mov     CX, Loop
  ELSE
        Mov     CX, 1
  ENDIF
        Int     10H
        Pop	CX
        Pop	BX
        Pop	AX
ENDM

@GetScrDims     MACRO
	Push	AX
	Push	BX
        Push    DS
        Mov     AX, 40H
        Mov     DS, AX
        Mov     BX, 4AH
        Mov     AX, DS:[BX]
        Mov     BX, 84H
        Mov     AH, DS:[BX]
        Inc     AH
        Pop     DS
        Mov     Scr_L, AL
        Mov     Scr_H, AH
        Pop	BX
        Pop	AX
ENDM

@Delay		MACRO Hours, Mins, Secs, Hs
LOCAL	Scs, Mns, Hrs, Cycle, Quit
	Push	AX
	Push	BX
	Push	CX
	Push	DX
  IFNB  <Hours>
	Mov	CH, Hours
  ELSE
	Xor	CH, CH
  ENDIF
  IFNB  <Mins>
	Mov	CL, Mins
  ELSE
	Xor	CL, CL
  ENDIF
  IFNB  <Secs>
	Mov	DH, Secs
  ELSE
	Xor	DH, DH
  ENDIF
  IFNB  <Hs>
	Mov	DL, Hs
  ELSE
	Xor	DL, DL
  ENDIF
	Push	CX
	Push	DX
	Mov	AH, 2CH
	Int	21h
	Pop	BX
	Pop	AX
	Add	BL, DL
	Add	BH, DH
	Add	AL, CL
	Add	AH, CH
	Cmp	BL, 100
	JL	Scs
	Sub	BL, 100
	Inc	BH
Scs:
	Cmp	BH, 60
	JL	Mns
	Sub	BH, 60
	Inc	AL
Mns:
	Cmp	AL, 60
	JL	Hrs
	Sub	AL, 60
	Inc	AH
Hrs:
	Cmp	AH, 24
	JL	Cycle
	Sub	AH, 24
Cycle:
	Push	AX
	Push	BX
	Mov	AH, 2CH
	Int	21h
	Pop	BX
	Pop	AX
	Cmp	CX, AX
	JL	Cycle
	JG	Quit
	Cmp	DX, BX
	JL	Cycle
Quit:
	Pop	DX
	Pop	CX
	Pop	BX
	Pop	AX
ENDM

@Sound		MACRO Freq
	Push	AX
	Push	CX
	Push	DX
	Mov	AX, Freq
	Push	AX
	In	AL, 61H
	Or	AL, 3
	Out	61H, AL
	Mov	AL, 10110110B
	Out	43H, AL
	Pop	CX
	Mov	DX, 1190000 / 65536
	Mov	AX, 1190000 mod 65536
	Div	CX
	Out	42H, AL
	Mov	AL, AH
	Out	42H, AL
	Pop	DX
	Pop	CX
	Pop	AX
ENDM

@NoSound	MACRO
	Push	AX
	In	AL, 61H
	And	AL, 11111100B
	Out	61H, AL
	Pop	AX
ENDM

@WriteLn	MACRO
	@MoveXY 0, 1
	@GotoXY 0
ENDM
