forked from skiselev/8088_bios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.inc
1466 lines (1306 loc) · 41.5 KB
/
video.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;=========================================================================
; video.inc - BIOS video services
; INT 10h, functions AH=00h to AH=0Fh
;-------------------------------------------------------------------------
;
; Compiles with NASM 2.13.02, might work with other versions
;
; This code is adopted from XT-clone BIOS by Anonymous
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
;
;=========================================================================
;-------------------------------------------------------------------------
; CRTC registers
crtc_cur_start equ 0Ah ; CRTC cursor start line register
crtc_cur_end equ 0Bh ; CRTC cursor end line register
crtc_offset_hi equ 0Ch ; CRTC start address high register
crtc_offset_lo equ 0Dh ; CRTC start address low register
crtc_cur_pos_hi equ 0Eh ; CRCT cursor location high register
crtc_cur_pos_lo equ 0Fh ; CRTC cursor location low register
crtc_pen_hi equ 10h ; CRTC light pen position high byte
crtc_pen_lo equ 11h ; CRTC light pen position low byte
;-------------------------------------------------------------------------
; control characters
bel equ 07h
bs equ 08h
lf equ 0Ah
cr equ 0Dh
setloc 0F045h ; int 10 functions table
int_10_dispatch:
dw int_10_fn00 ; Set video mode
dw int_10_fn01 ; Set text mode cursor shape
dw int_10_fn02 ; Set cursor position
dw int_10_fn03 ; Get cursor position and shape
dw int_10_fn04 ; Read light pen position
dw int_10_fn05 ; Set active display page
dw int_10_fn06 ; Scroll up window
dw int_10_fn07 ; Scroll down window
dw int_10_fn08 ; Read character and attribute
dw int_10_fn09 ; Write character and attribute
dw int_10_fn0A ; Write character only
dw int_10_fn0B ; Set background color or palette
dw int_10_fn0C ; Write graphics pixel
dw int_10_fn0D ; Read graphics pixel
dw int_10_fn0E ; Teletype output
dw int_10_fn0F ; Get current video mode
int_10_num_func equ ($-int_10_dispatch)/2
;-------------------------------------------------------------------------
; offsets for registers on stack
int_10_ax equ 0
int_10_al equ int_10_ax
int_10_ah equ int_10_ax+1
int_10_bx equ int_10_ax+2
int_10_bl equ int_10_bx
int_10_bh equ int_10_bx+1
int_10_cx equ int_10_bx+2
int_10_ch equ int_10_cx+1
int_10_dx equ int_10_cx+2
;=========================================================================
; int_10 - BIOS video services
; Input:
; AH - Function
; 00h - Set video mode
; 01h - Set text mode cursor shape
; 02h - Set cursor position
; 03h - Get cursor position and shape
; 04h - Read light pen position
; 05h - Select active display page
; 06h - Scroll up window
; 07h - Scroll down window
; 08h - Read character and attribute at cursor position
; 09h - Write character and attribute at cursor position
; 0Ah - Write character only at cursor position
; 0Bh -
; BH = 00h - Set background/border color
; BH = 01h - Set palette
; 0Ch - Write graphics pixel
; 0Dh - Read graphics pixel
; 0Eh - Teletype output
; 0Fh - Get current video mode
;-------------------------------------------------------------------------
setloc 0F065h ; int 10 Entry Point
int_10:
sti
cld ; ...strings auto-increment
push bp
push es
push ds
push si
push di
push dx
push cx
push bx
push ax
mov bx,biosdseg
mov ds,bx
mov bl,byte [video_mode]
cmp bl,07h ; check for monochrome mode
mov bx,0B800h ; assume CGA, BX = CGA video segment
jb .color ; it is CGA
mov bh,0B0h ; else MDA, BX = MDA video segment
.color:
mov es,bx ; load video segment to ES
mov bp,sp ; ...start of stack frame
cmp ah,int_10_num_func ; dispatch table size
jae .exit ; invalid function
mov bh,0
mov bl,ah
shl bx,1
cs call near [int_10_dispatch+bx]
.exit:
pop ax
pop bx
pop cx
pop dx
pop di
pop si
pop ds
pop es
pop bp
iret
;=========================================================================
; int_1D - Video parameters tables
; Contains values loaded to MC6845 CRTC registers 00h to 0Fh during
; video mode initialization
;-------------------------------------------------------------------------
setloc 0F0A4h ; int 1D (MDA and CGA Video Parm Table)
int_1D:
; CGA test 40x25 modes
db 38h, 28h, 2Dh, 0Ah, 1Fh, 06h, 19h, 1Ch
db 02h, 07h, 06h, 07h, 00h, 00h, 00h, 00h
; CGA text 80x25 modes
db 71h, 50h, 5Ah, 0Ah, 1Fh, 06h, 19h, 1Ch
db 02h, 07h, 06h, 07h, 00h, 00h, 00h, 00h
; CGA graphics modes
db 38h, 28h, 2Dh, 0Ah, 7Fh, 06h, 64h, 70h
db 02h, 01h, 06h, 07h, 00h, 00h, 00h, 00h
; MDA text 80x25 mode
db 61h, 50h, 52h, 0Fh, 19h, 06h, 19h, 19h
db 02h, 0Dh, 0Bh, 0Ch, 00h, 00h, 00h, 00h
page_size:
dw 0800h ; text 40x25 mode
dw 1000h ; text 80x25 mode
dw 4000h ; graphics modes
dw 4000h
columns:
db 40, 40 ; modes 0, 1 - 40x25
db 80, 80 ; modes 2, 3 - 80x25
db 40, 40 ; modes 4, 5 - 40x25
db 80, 80 ; modes 6, 7 - 80x25
MODES db 2Ch,28h,2Dh,29h,2Ah,2Eh,1Eh,29h ; Table of mode sets
TABMUL db 00h,00h,10h,10h,20h,20h,20h,30h ; Table lookup for multiply
;=========================================================================
; int_10_fn00 - Set video mode
; Input:
; AH = 00h
; AL = video mode
; 00h - CGA - text 40x25, 16 shades of gray
; 01h - CGA - text 40x25, 16 colors
; 02h - CGA - text 80x25, 16 shades of gray
; 03h - CGA - text 80x25, 16 colors
; 04h - CGA - graphics 320x200, 4 colors
; 05h - CGA - graphics 320x200, 4 shades of gray
; 06h - CGA - graphics 640x200, monochrome
; 07h - MDA - text 80x25, monochrome
;-------------------------------------------------------------------------
int_10_fn00:
mov bl,byte [bp+int_10_al] ; BL = video mode
; assume CGA mode
mov cx,0B800h ; CGA video memory segment
mov dx,3D4h ; port for MC6845 CRTC address register
mov al,byte [equipment_list] ; get equipment - low byte
and al,equip_video ; get video adapter type
cmp al,equip_mono ; monochrome?
mov al,0
jne .color ; jump if CGA/color mode
; set MDA mode
mov bl,07h ; MDA can only be 7
mov ch,0B0h ; MDA video memory segment
mov dl,0B4h ; port for MC6845 CRTC address register
inc ax
.color:
mov es,cx ; ES = video memory segment
mov word [video_port],dx ; Save current CRTC display port
add dl,4
out dx,al ; Reset the video
mov byte [video_mode],bl ; Save current CRTC mode
mov bh,0
push bx
push es
xor ax,ax
mov es,ax ; Load interrupt table segment to ES
es les si,[1Dh*4] ; Load video parameters table
; (INT 1Dh vector) to ES:SI
cs mov bl,byte [bx+TABMUL] ; Get BL for indexing into int_1D
add si,bx
es mov cx,word [si+crtc_cur_start] ; cursor shape from INT 1Dh table
xchg cl,ch ; convert to LSB format
mov word [video_cur_shape],cx ; store cursor shape
mov cx,10h ; Sixteen values to send
.setup_crt_loop:
es mov al,byte [si] ; Value to send in SI
call vid_crtc_writeb ; ...send it
inc ah ; ...bump count
inc si ; ...point to next
loop .setup_crt_loop ; ...loop until done
pop es
xor di,di
mov cx,2000h ; video memory size for CGA
xor ax,ax ; fill word for graphics mode
call vid_check_mode ; Set flags according to mode
jc .clear_screen ; jump if graphics mode
jnz .text_fill ; jump if CGA mode
mov cx,0800h ; video memory size for MDA
.text_fill:
mov ax,07h << 8 | ' ' ; fill word for test mode
.clear_screen:
repz stosw ; clear screen with fill word
mov dx,word [video_port] ; Get the port
add dl,4
pop bx
cs mov al,byte [bx+MODES] ; Load data to set for mode
out dx,al ; ...and send it
mov byte [video_mode_reg],al ; ...then save active data
inc dx
mov al,30h ; Assume not 640 x 200 b/w
cmp bl,6 ; ...correct?
jnz .set_palette
mov al,3Fh ; Palette for 640 x 200 b/w
.set_palette:
mov byte [video_palet_reg],al ; ...save palette
out dx,al ; ...send palette
mov ax,ds
mov es,ax
xor ax,ax
mov byte [video_page],al ; ...active page=page 0
mov cx,9 ; video_page_offt + video_cur_pos * 8
mov di,video_page_offt
rep stosw ; zero page offset and cursor position
cs mov al,byte [bx+columns] ; Get display width
mov word [video_columns],ax ; ...save it
and bl,0FEh ; Clear the LSB to get an index
; to 16-bit word page_size table
; FIXME: It returns graphics mode page
; size for mode 7. Probably not
; critical, as MDA has only one page
cs mov ax,word [bx+page_size] ; Get video page size
mov word [video_page_size],ax ; ...save it
ret
;=========================================================================
; int_10_fn01 - Set text-mode cursor shape
; Input:
; AH = 01h
; CH = cursor scan line start
; CL = cursor scan line end
; Output:
; none
;-------------------------------------------------------------------------
int_10_fn01:
mov word [video_cur_shape],cx ; save cursor shape to BIOS data area
mov ah,crtc_cur_start ; select CRTC cursor shape registers
call vid_crtc_writew ; write it to CRTC
ret
;=========================================================================
; int_10_fn02 - Set cursor position
; Input:
; AH = 02h
; BH = page number
; DH = cursor row (00h is top)
; DL = cursor column (00h is left)
; Output:
; none
;-------------------------------------------------------------------------
int_10_fn02:
mov bl,byte [bp+int_10_bh] ; BL = page number
cmp byte [video_page],bl ; is it on current page?
jne bios_set_cur_pos ; if not visible only update BIOS data
;=========================================================================
; set_cur_pos - set CRTC cursor position, update BIOS cursor location
; BL = page
; DH = cursor row (00h is top)
; DL = cursor column (00h is left)
;-------------------------------------------------------------------------
set_cur_pos:
mov ax,dx ; AX = cursor position
call vid_position_to_offset ; AX - offset
add ax,word [video_page_offt] ; + byte offset, regen reg.
shr ax,1
mov cx,ax
mov ah,crtc_cur_pos_hi ; CRCT cursor location high register
call vid_crtc_writew ; send cursor position to CRTC
bios_set_cur_pos:
mov bh,0
shl bl,1 ; index to words table
mov word [bx+video_cur_pos],dx ; save position to BIOS data area
ret
;=========================================================================
; int_10_fn03 - Get cursor position and shape
; Input:
; AH = 03h
; BH = page number
; Output:
; CH = cursor start scan line
; CL = cursor end scan line
; DH = cursor row (00h is top)
; DL = cursor column (00h is left)
;-------------------------------------------------------------------------
int_10_fn03:
mov bh,0
mov bl,byte [bp+int_10_bh] ; BL = page number
shl bl,1
mov ax,word [bx+video_cur_pos] ; get current cursor position
mov word [bp+int_10_dx],ax ; return position in DX
mov ax,word [video_cur_shape] ; get cursor shape
mov word [bp+int_10_cx],ax ; return cursor shape in CX
ret
;=========================================================================
; int_10_fn04 - Read light pen position
; Input:
; AH = 04h
; Output:
; AH - light pen trigger flag
; 00h not down/triggered
; 01h down/triggered
; If light pen is triggered:
; DH = character row
; DL = character column
; CH = pixel row
; BX = pixel column
;-------------------------------------------------------------------------
int_10_fn04:
mov byte [bp+int_10_ah],0 ; set AH = 0, light pen not triggered
mov dx,word [video_port]
add dl,6 ; CRTC status register
in al,dx ; read it
test al,4 ; test light pen switch bit
jz .reset_pen ; reset pen and return if switch is off
test al,2 ; test light pen tigger bit
jnz .read_pen ; continue if triggered
ret ; not triggered - return
.read_pen:
mov dx,word [video_port] ; CRTC index register
mov al,crtc_pen_hi ; CRTC pen position high byte register
out dx,al ; select it
inc dx ; CRTC data register
in al,dx ; read high byte of pen position
mov ah,al
dec dx ; CRTC index register
mov al,crtc_pen_lo ; CRTC pen position low byte register
out dx,al ; select it
inc dx ; CRTC data register
in al,dx ; read low byte of pen position
mov bh,0
mov bl,byte [video_mode] ; get current video mode
cs mov bl,byte [bx+.correction] ; light pen correction factor
sub ax,bx
jns .1
xor ax,ax ; set to zero if negative result
.1:
call vid_check_mode ; check video mode
jnc .text ; calculate character position if text
mov dl,40 ; divide by 40
div dl ; AL = row, AH = column (reminder)
mov bh,0
mov bl,ah
mov cl,3
shl bx,cl ; BX = AH * 8 - pixel column
mov ch,al
shl ch,1 ; CH = AL * 2 - pixel row
mov dl,ah ; DL = AH - character column
mov dh,al
shr dh,1
shr dh,1 ; DH = AL / 4 - character row
cmp byte [video_mode],6 ; check for 640x200 mode
jnz .exit
shl bx,1 ; adjust pixel column (double it)
shl dl,1 ; same or character column
jmp .exit
.text:
div byte [video_columns] ; divide by number of columns
xchg al,ah ; AL = column, AH = row
mov dx,ax ; save characer row,column to DH,AL
mov cl,3
shl ah,cl
mov ch,ah ; CH = AH * 8 - pixel row
mov bh,0
mov bl,al
shl bx,cl ; BX = AL * 8 - pixel column
.exit:
mov byte [bp+int_10_ah],1 ; set AH = 1, light pen triggered
mov word [bp+int_10_dx],dx ; ...row, column in user dx
mov word [bp+int_10_bx],bx ; ...pixel column in user bx
mov byte [bp+int_10_ch],ch ; ...raster line in user ch
.reset_pen:
mov dx,word [video_port] ; Get port of active CRTC card
add dl,7 ; clear light pen strobe reg
out dx,al ; reset it
ret
.correction:
db 3, 3, 5, 5, 3, 3, 3, 4 ; light pen correction
;=========================================================================
; int_10_fn05 - Select active display page
; Input:
; AH = 05h
; AL - new page number (00h is the first page)
; Output:
; none
;-------------------------------------------------------------------------
int_10_fn05:
mov byte [video_page],al ; update page number in BIOS data area
mov bl,al ; also copy it to BL
mov ah,0
mul word [video_page_size] ; calculate page offset
mov word [video_page_offt],ax ; save the offset
shr ax,1 ; calculate CRTC page start address
mov cx,ax ; save a copy to CX
mov ah,crtc_offset_hi ; CRTC start address high register
call vid_crtc_writew ; write new offset to CRTC
mov bh,0
shl bx,1
mov ax,word [bx+video_cur_pos] ; AX - cursor position for new page
call vid_position_to_offset ; AX - offset relative to start of page
shr ax,1
add cx,ax ; add to the page offset
mov ah,crtc_cur_pos_hi ; CRCT cursor location high register
call vid_crtc_writew ; send cursor position to CRTC
ret
;=========================================================================
; int_10_fn06 - scroll up window
; int_10_fn07 - scroll down window
; Input:
; AH = 06h (scroll up) or AH = 07 (scroll down)
; AL = number of rows by which to scroll up (00h = clear entire window)
; BH = attribute used to write blank rows at bottom of window
; CH,CL = row,column of window's upper left corner
; DH,DL = row,column of window's lower right corner
; Output:
; none
; TODO:
; optimize graphics fill
;-------------------------------------------------------------------------
int_10_fn06:
int_10_fn07:
call vid_check_mode
jc .graphics_scroll
xor si,si ; SI - "snow" workaround not required
cmp byte [video_mode],2
jb .no_snow
cmp byte [video_mode],3
ja .no_snow
mov si,0101010101010101b ; CGA "snow" workaround required
; mov si,0001000100010001b ; CGA "snow" workaround required
.no_snow:
mov ax,word [bp+int_10_dx] ; AX = window's lower right corner
push ax
cmp byte [bp+int_10_ah],07h ; check for scroll down function
jz .1 ; jump if scroll down
mov ax,word [bp+int_10_cx] ; AX = window's upper left corner
.1:
call vid_position_to_offset
add ax,word [video_page_offt]
mov di,ax ; DI = scroll copy destination address
; calculate scroll window size (DX)
pop dx ; DX = window's lower right corner
sub dx,word [bp+int_10_cx] ; substract windows's upper left corner
add dx,101h ; add 1x1
; calculate offset between the source and the destination (AX)
mov bx,word [video_columns] ; BX = columns (note BX <= 80)
shl bx,1 ; each character takes two bytes
mov al,byte [bp+int_10_al] ; AL = number of rows to scroll
push dx
mov ah,0
mul bx
pop dx
sub bl,dl ; BX = distance between end of one
sub bl,dl ; row and beggining of another
push ds
mov cx,es
mov ds,cx ; load video segment to DS
cmp byte [bp+int_10_ah],06h ; check for scroll up function
jz .2 ; jump if scroll up
neg ax ; negate offset
neg bx ; negate distance
std ; copy backwards
.2:
mov cl,byte [bp+int_10_al] ; CL = number of rows to scroll
or cl,cl
jz .text_fill_only ; jump if clear window only requested
xchg ax,si ; AX = snow workaround flag, SI = offset
add si,di ; SI = scroll copy source address
sub dh,cl ; DH = number of rows to copy
or bx,bx
jz .text_full_row_scroll
.text_scroll_loop:
mov ch,0
mov cl,dl ; CX = characters in row to copy
ror ax,1 ; rotate snow workaround flag
jnc .text_scroll_no_retrace
call .retrace_wait
.text_scroll_no_retrace:
repz movsw ; copy one row
;.text_scroll_next_row:
add si,bx ; SI = next row to copy source address
add di,bx ; DI = next row to copy destination
dec dh ; decrement row counter
jnz .text_scroll_loop ; jump if there is more rows to copy
.text_fill:
mov dh,byte [bp+int_10_al] ; DH = number of rows to fill
mov si,ax ; SI = snow workaround flag
.text_fill_only:
mov ch,0
mov ah,byte [bp+int_10_bh] ; AH = blank attribute
mov al,' ' ; AL = blank character
.text_fill_loop:
mov cl,dl ; CX = characters in row to fill
ror si,1 ; rotate snow workaround flag
jnc .text_fill_no_retrace ; jump if LSB was zero - no wait
call .retrace_wait ; wait for vertical retrace
.text_fill_no_retrace:
repz stosw ; fill one row
add di,bx ; DI = next row to fill destination
dec dh ; decrement row counter
jnz .text_fill_loop ; jump if there is more rows to fill
pop ds
ret
.text_full_row_scroll:
or ax,ax
jz .text_full_row_no_snow
push ax
mov al,dl
mul dh
.text_full_row_loop:
mov cx,240
cmp ax,cx
ja .copy_chunk
xchg ax,cx
xor ax,ax
jmp .do_copy
.copy_chunk:
sub ax,cx
.do_copy:
call .retrace_wait
rep movsw
or ax,ax
jnz .text_full_row_loop
pop ax
jmp .text_fill
.text_full_row_no_snow:
push ax
mov al,dl
mul dh
mov cx,ax
rep movsw
pop ax
jmp .text_fill
;-------------------------------------------------------------------------
; .retrace_wait - next till the next vertical retrace
.retrace_wait:
push ax
push dx
mov dx,03DAh ; DX = CGA status register
.retrace_wait_not_set:
in al,dx
test al,08h ; bit 3 set if vertical retrace
jnz .retrace_wait_not_set ; jump if retrace
.retrace_wait_set:
in al,dx
test al,08h ; bit 3 set if vertical retrace
jz .retrace_wait_set ; jump if no retrace
pop dx
pop ax
.retrace_exit:
ret
;-------------------------------------------------------------------------
; .graphics_scroll - scroll for graphics modes
.graphics_scroll:
mov ax,word [bp+int_10_dx] ; AX = window's lower right corner
push ax
cmp byte [bp+int_10_ah],07h ; check for scroll down function
jz .3 ; jump if scroll down
mov ax,word [bp+int_10_cx] ; AX = window's upper left corner
.3:
call vid_gfx_pos_to_offset
mov di,ax ; DI = scroll copy destination address
; calculate scroll windows size (DX)
pop dx ; DX = window's lower right corner
sub dx,word [bp+int_10_cx] ; substract window's upper left corner
add dx,101h ; add 1x1
shl dh,1 ; multiply by four: one character takes
shl dh,1 ; four bytes in each plane
mov al,byte [bp+int_10_ah] ; AL = function
cmp byte [video_mode],06h ; check for 640x200 mode
jz .4 ; jump if 640x200 mode
shl dl,1 ; double character width for 320x200
shl di,1 ; double character width for 320x200
cmp al,07h ; check for scroll down function
jnz .5 ; jump if scroll down
inc di ; scroll up - adjust source address
.4:
cmp al,07h ; check for scroll down function
jnz .5 ; jump if not scroll down
add di,0F0h ; adjust destination address
; for copying backwards
.5:
mov bl,byte [bp+int_10_al] ; BL = number of rows to scroll
shl bl,1 ; multiply by four: one character takes
shl bl,1 ; four bytes in each plane
push bx
sub dh,bl ; DH = number of rows to copy
mov al,50h
mul bl
mov bx,1FB0h
cmp byte [bp+int_10_ah],06h ; check for scroll up function
jz .6 ; jump if scroll up
neg ax ; negate offset for scroll down
mov bx,2050h
std ; copy backwards
.6:
mov si,di
add si,ax ; SI = scroll copy source address
pop ax
mov cx,es
mov ds,cx ; load video segment to DS
or al,al
jz .graphics_fill ; jump if clear window only requested
push ax
.graphics_scroll_loop:
mov ch,0
mov cl,dl ; CX = bytes in row to copy
push si
push di
repz movsb ; copy one row in the first plane
pop di
pop si
add si,2000h ; point SI and DI to the second plane
add di,2000h
mov cl,dl ; CX = bytes in row to copy
push si
push di
repz movsb ; copy one row in the second plane
pop di
pop si
sub si,bx ; SI = next row to copy source address
sub di,bx ; DI = next row to copy destination
dec dh ; decrement row counter
jnz .graphics_scroll_loop ; jump if there is more rows to copy
pop ax
mov dh,al ; DH = number of rows to fill
.graphics_fill:
mov al,byte [bp+int_10_bh] ; AL = fill color
mov ch,0
.graphics_fill_loop:
mov cl,dl ; CX = bytes in row to fill
push di
repz stosb ; fill one row in the first plane
pop di
add di,2000h ; point DI to the second plane
mov cl,dl ; CX = bytes in row to fill
push di
repz stosb ; fill one row in the second plane
pop di
sub di,bx
dec dh ; decrement row counter
jnz .graphics_fill_loop ; jumpif there is more rows to fill
ret
;=========================================================================
; int_10_fn08 - Read character and attribute
; Input:
; AH = 08h
; Output:
; AL - character read
; BH - video attribute (text modes only)
; int_10_fn09 - Write character and attribute
; Input:
; AH = 09h
; AL - character to write
; BH - page number
; BL - attribute (text modes) or color (graphics modes)
; CX - number of times to write character
; Output:
; none
; int_10_fn0A - Write character only
; Input:
; AH = 0Ah
; AL - character to write
; BH - page number
; CX - repeat count
; Output:
; none
;-------------------------------------------------------------------------
int_10_fn08:
int_10_fn09:
int_10_fn0A:
call vid_check_mode
jc .graphics ; jump if graphics mode
mov bl,byte [bp+int_10_bh] ; BL = page number
mov bh,0
push bx
call vid_current_offset
mov di,ax ; DI = character offset in the page
pop ax ; AX = page number
mul word [video_page_size] ; AX = page number * page size
add di,ax ; DI = character offset
mov si,di ; SI = character offset
mov dx,word [video_port] ; DX = CRTC port
add dx,6 ; DX = CGA status register
push ds
mov bx,es
mov ds,bx ; load video segment to DS
mov al,byte [bp+int_10_ah] ; AL = function
cmp al,08h ; check for read character function
jnz .text_write ; jump if not read char (write char)
.read_retrace_wait:
in al,dx
test al,01h ; bit 0 set if horizontal retrace
jnz .read_retrace_wait ; jump if retrace
cli
.read_no_retrace_wait:
in al,dx
test al,01h ; bit 0 set if horizontal retrace
jz .read_no_retrace_wait ; jump if no retrace
lodsw ; read character and attribute
sti
pop ds
mov word [bp+int_10_ax],ax ; return character and attribute in AX
ret
.text_write:
mov bl,byte [bp+int_10_al] ; BL = character to write
mov bh,byte [bp+int_10_bl] ; BH = attribute to write
mov cx,word [bp+int_10_cx] ; CX = number of times to write char
cmp al,0Ah ; check for write char only function
jz .text_write_char_only ; jump if write char only
.write_char_retrace:
in al,dx
test al,08h ; bit 3 set if vertical retrace
jnz .do_write_char_attr ; retrace is in progress - write char
.write_retrace_wait1:
in al,dx
test al,01h ; bit 0 set if horizontal retrace
jnz .write_retrace_wait1 ; jump if retrace
cli
.write_no_retrace_wait1:
in al,dx
test al,01h ; bit 0 set if horizontal retrace
jz .write_no_retrace_wait1 ; jump if no retrace
.do_write_char_attr:
mov ax,bx ; AX = character / attribute
stosw ; write it to video memory
sti
loop .write_char_retrace ; repeat CX times
pop ds
ret
.text_write_char_only:
in al,dx
test al,08h ; bit 3 set if vertical retrace
jnz .do_write_char_only ; retrace is in progress - write char
.write_retrace_wait2:
in al,dx
test al,01h ; bit 0 set if horizontal retrace
jnz .write_retrace_wait2 ; jump if retrace
cli
.write_no_retrace_wait2:
in al,dx
test al,01h ; bit 0 set if horizontal retrace
jz .write_no_retrace_wait2 ; jump if no retrace
.do_write_char_only:
mov al,bl ; AL = character to write
stosb ; write it to video memory
sti
inc di ; skip attribute
loop .text_write_char_only ; repeat CX times
pop ds
ret
.graphics:
cmp byte [bp+int_10_ah],08h ; check for read character function
jz .graphics_read
mov ax,word [video_cur_pos] ; Get cursor position
call vid_gfx_pos_to_offset ; ...convert (row,col) -> col
mov di,ax ; Save in displacement register
push ds
mov al,byte [bp+int_10_al] ; Get character to write
mov ah,0
or al,al ; Is it user character set?
js .CG9_02 ; ...skip if so
mov dx,cs ; Else use ROM character set
mov si,gfx_font ; load graphics font offset
jmp .CG9_03
.CG9_02:
and al,7Fh ; Origin to zero
xor bx,bx ; ...then go load
mov ds,bx ; ...user graphics
lds si,[7Ch] ; ...vector, offset in si
mov dx,ds ; ...segment into dx
.CG9_03:
pop ds ; Restore data segment
mov cl,3 ; ...char 8 pixels wide
shl ax,cl
add si,ax ; Add regen. buffer base addr.
mov cx,word [bp+int_10_cx] ; ...load char. count
cmp byte [video_mode],6 ; Is the mode 640 x 200 b/w?
push ds
mov ds,dx
jz .CG8_02 ; ...skip if so
shl di,1
mov al,byte [bp+int_10_bl] ; Get character attribute
and ax,3
mov bx,5555h
mul bx
mov dx,ax
mov bl,byte [bp+int_10_bl] ; Restore BL (character attribute)
.CG9_04:
mov bh,8 ; Char 8 pixels wide
push di
push si
.CG9_05:
lodsb ; Read the screen
push cx
push bx
xor bx,bx
mov cx,8
.CG9_06:
shr al,1 ; Shift bits thru byte
rcr bx,1
sar bx,1
loop .CG9_06
mov ax,bx ; Result into ax
pop bx
pop cx
and ax,dx
xchg ah,al
or bl,bl
jns .CG9_07
es xor ax,word [di]
.CG9_07:
es mov word [di],ax ; Write new word
xor di,2000h
test di,2000h ; Is this other plane?
jnz .CG9_08 ; ...nope
add di,50h ; Else advance character
.CG9_08:
dec bh ; Show another char written
jnz .CG9_05 ; ...more to go
pop si
pop di
inc di
inc di
loop .CG9_04
pop ds
ret
.CG8_02:
mov bl,byte [bp+int_10_bl] ; Get display page
mov dx,2000h ; ...size of graphics plane
.CG8_03:
mov bh,8 ; Pixel count to write
push di
push si
.CG8_04:
lodsb ; Read from one plane
or bl,bl ; ...done both planes?
jns .CG8_05 ; ...skip if not
es xor al,byte [di] ; Else load attribute
.CG8_05:
es mov byte [di],al ; Write out attribute
xor di,dx ; ...get other plane
test di,dx ; Done both planes?
jnz .CG8_06 ; ...skip if not
add di,50h ; Else position for now char
.CG8_06:
dec bh ; Show row of pixels read
jnz .CG8_04 ; ...not done all of them
pop si
pop di
inc di
loop .CG8_03
pop ds
ret