; WordStar for DOS 7.0
; Video attributes handling

; Video attributes are used in various places on the WordStar display.
; When a video attribute changes, WordStar calls the VIDATT subroutine
; below with a video attribute code in the CX register. If the CH
; register is zero, WordStar is displaying regular text and the CL
; register bits mean the following video attributes are to be used...

;  Bit  Video Attribute

;  none  Normal text
;   0  Strike-out
;   1  Reserved
;   2  Marked text
;   3  Underlining
;   4  Subscripting
;   5  Superscripting
;   6  Highlighting
;   7  Italic

; If the CH register is non-zero, its bits have the following meaning...

;  Bit  Video Attribute

;   0  Extended characters
;   1  Status line
;   2  Menu text (can be normal or highlighted depending on CL)
;   3  Function key labels
;   4  Ruler line
;   5  Directory
;   6  Warnings and error messages

; WordStar expects the video attribute for the character to be returned
; in the AL register. Here is the default mapping for some of the
; WordStar video attributes into IBM attributes...

;  WordStar Video  IBM Video  Function

;    00000000   00000111  Normal
;    01110001   00001000  Bright
;    00001000   00000001  Underline
;    00000100   00010000  Inverse
;    00000010   10000000  Blink

; The VIDATT subroutine is used to change video attributes on the screen.
; On entry, WordStar will supply the attributes to turn on in the CX register.
; You must translate them into whatever your particular terminal requires.

; Return the IBM video attribute byte in AL.

; IBM Video Conversion

 = byte ptr ds:[4]    vidax  equ  byte ptr ds:[4]  ; Data segment location for VIDATT use
 = word ptr ds:[8]    viday  equ  word ptr ds:[8] ; Data segment location for SAV_VATT
 = byte ptr ds:[10]    vidaz  equ  byte ptr ds:[10] ; Data segment location for SAV_VATT+2

 032A        vidatt:
 032A  89 0E 0008      mov  viday,cx
 032E  BE 046B R      mov  si,offset vcolor+1
 0331  2E: 8A 04      mov  al,cs:[si]  ; Get normal color

 0334  E3 4E        jcxz  vida4    ; Normal?
 0336  8A E0        mov  ah,al

 0338  8A 36 0004      mov  dh,vidax  ; Know video type?
 033C  FE C6        inc  dh
 033E  75 0B        jnz  vida1

        ; Initialize

 0340  51        push  cx
 0341  B4 0F        mov  ah,15    ; Get video type
 0343  CD 10        int  10h
 0345  A2 0004        mov  vidax,al
 0348  59        pop  cx
 0349  EB DF        jmp  vidatt

        ; Non-text attributes

 034B  F6 C5 FE      vida1:  test  ch,11111110b  ; Not special text?
 034E  74 3F        jz  vida5

 0350  8A C5        mov  al,ch    ; Status line?
 0352  B3 11        mov  bl,17
 0354  D0 E8        shr  al,1
 0356  D0 E8        shr  al,1
 0358  72 25        jc  vida3

 035A  FE C3        inc  bl    ; Menu?
 035C  D0 E8        shr  al,1
 035E  72 14        jc  vida2

 0360  B3 14        mov  bl,17+3    ; Function key labels?
 0362  D0 E8        shr  al,1
 0364  72 0E        jc  vida2

 0366  B3 18        mov  bl,17+7    ; Ruler line?
 0368  D0 E8        shr  al,1
 036A  72 13        jc  vida3

 036C  B3 16        mov  bl,17+5    ; Directory?
 036E  D0 E8        shr  al,1
 0370  72 02        jc  vida2

 0372  B3 19        mov  bl,17+8    ; Warnings

 0374  0A C9      vida2:  or  cl,cl    ; Normal video?
 0376  74 07        jz  vida3

 0378  F6 C1 04        test  cl,100b    ; Inverse
 037B  75 0B        jnz  vida4a

 037D  FE C3        inc  bl    ; Highlighted

 037F  32 FF      vida3:  xor  bh,bh    ; Get video attribute from VCOLOR
 0381  2E: 8A 00      mov  al,cs:[si+bx]
 0384  A2 000A      vida4:  mov  byte ptr vidaz,al ; Save resulting value for next time
 0387  C3        ret

 0388  E8 037F R    vida4a:  call  vida3    ; Invert colors
 038B  B1 04        mov  cl,4
 038D  EB 1C        jmp  short vida7a

; Use VCOLOR table to convert WordStar video bits

 038F  8A E9      vida5:  mov  ch,cl    ; Original bits to CH

 0391  B2 08        mov  dl,8    ; Go through table
 0393  46        inc  si
 0394  D0 E9      vida6:  shr  cl,1    ; Bit not set?
 0396  73 07        jnc  vida7

 0398  2E: 22 04      and  al,cs:[si]  ; AND with mask, OR with new color
 039B  2E: 0A 44 01      or  al,cs:[si+1]

 039F  46      vida7:  inc  si    ; Next color in table
 03A0  46        inc  si
 03A1  FE CA        dec  dl
 03A3  75 EF        jnz  vida6

 03A5  B1 04        mov  cl,4    ; CL=4

 03A7  84 E9        test  ch,cl    ; Not marking a block?
 03A9  74 29        jz  vida10

; Don't use inverse?

 03AB  2E: F6 06 023A R 40    vida7a:  test  cs:ibmflg,01000000b ; Bit set indicating "no inverse"?
 03B1  75 23        jnz  vida11

 03B3  80 FE 08        cmp  dh,8    ; Color screen?
 03B6  75 08        jnz  vida9

; B&W

 03B8  A8 76        test  al,01110110b  ; Not underlining?
 03BA  75 02        jnz  vida8

 03BC  8A C4        mov  al,ah    ; Use normal color

 03BE  34 08      vida8:  xor  al,00001000b  ; Invert bright

; Color

 03C0  8A F0      vida9:  mov  dh,al    ; Swap foreground & background colors
 03C2  8A D0        mov  dl,al
 03C4  D2 E6        shl  dh,cl
 03C6  80 E6 70        and  dh,01110000b
 03C9  D2 EA        shr  dl,cl
 03CB  80 E2 07        and  dl,00000111b
 03CE  24 88        and  al,10001000b
 03D0  0A C6        or  al,dh
 03D2  0A C2        or  al,dl
 03D4  EB AE      vida10:  jmp  vida4

; Use bright instead of inverse

 03D6  0C 08      vida11:  or  al,00001000b  ; Set bright attribute
 03D8  EB AA        jmp  vida4

 = 00B0        vidatz  equ  $-vidatt  ; Size of VIDATT code

 03DA  0090[        db  (320-vidatz) dup(0) ; Extra bytes to fill out to 300th byte
     00
       ]

; Video attributes used in VIDATT for the IBM compatible video RAM. Note
; that the table is used both for monochrome and color and should be
; installed accordingly. The default is set up for monochrome.

; Each entry in the table contains both a mask that is AND'd with the video
; attribute, and a color code that is OR'd.

; The IBM PC uses a video attribute byte for each character. It is
; organized in the following way:

;  Bit  Function

;  0,1,2  Foreground color (characters)
;  3  Bright
;  4,5,6  Background color
;  7  Blink

; The color codes for the IBM PC are:

;  0 - black
;  1 - blue
;  2 - green
;  3 - cyan
;  4 - red
;  5 - magenta
;  6 - brown
;  7 - white (light grey)

 046A  00 07      vcolor  db  000h, 007h  ; Normal text
 046C  8F 08        db  08Fh, 008h  ; Strike-out
 046E  FF 00        db  0FFh, 000h  ; Reserved
 0470  FF 00        db  0FFh, 000h  ; Reserved (reverse video)
 0472  F8 01        db  0F8h, 001h  ; Underline (blue if color)
 0474  FF 08        db  0FFh, 008h  ; Subscripts
 0476  FF 08        db  0FFh, 008h  ; Superscripts
 0478  FF 08        db  0FFh, 008h  ; Bold, doublestrike & command tags
 047A  FF 08        db  0FFh, 008h  ; Italics

 047C  07        db  007h    ; Title bar
 047D  07        db  007h    ; Menus, dialogs, warnings
 047E  01        db  001h    ; Menu, dialog, warning high
 047F  01        db  001h    ; Style & dialog box buttons
 0480  70        db  070h    ; Button highlight
 0481  70        db  070h    ; Status line
 0482  0F        db  00Fh    ; Title highlight
 0483  70        db  070h    ; Ruler line
 0484  70        db  070h    ; Flag column
 0485  07        db  007h    ; Lists

 = 001C        vcoloz  equ  $-vcolor    ; Size of VCOLOR

 0486  0002[        db  (30-vcoloz) dup(0)  ; Extra bytes to fill out to 30th byte
     00
       ]

; Normally the status line and text are displayed in dim intensity
; so that bold and doublestruck text can be shown in high intensity.
; Setting BRITE to 0FFH reverses the usage of bright and dim for
; the status line, text, and directories; zero normally.

 0488  00      brite  db  0    ; Don't reverse
