-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.ASM
1269 lines (1111 loc) · 19.6 KB
/
matrix.ASM
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
;all functions return result in register ax
masm
.model small
.stack 500h
.n equ 0dh
_32 equ db 66h
_00 equ dw 0000h
wait_retrace macro
local a
push dx
mov dx, 3dah
a:
in al, dx
and al, 08h
jz a
pop dx
endm
screen_off macro
cli
push dx
mov dx, 3c4h
mov al, 1
out dx, al
inc dx
in al, dx
or al, 00100000b
out dx, al
pop dx
sti
endm
screen_on macro
cli
push dx
mov dx, 3c4h
mov al, 1
out dx, al
inc dx
in al, dx
and al, 11011111b
out dx, al
pop dx
sti
endm
RGB struc
r db ?
g db ?
b db ?
; _reserved db ? ;for align
RGB ends
VgaInfoBlock struc
VESASignature db "VBE2"
VESAVersion dw ?
OEMStringPtr dd ?
Capabilities db 4 dup (?)
VideModePtr dd ?
TotalMemory dw ?
oemsoftwarerev dw ?
oemvendornameptr dd ?
oemproductnameptr dd ?
oemproductrevptr dd ?
Reserved db 222 dup (?)
oemdata db 256 dup (?)
VgaInfoBlock ends
ModeInfoBlock struc
;Mandatory information for all VBE revisions
ModeAttributes dw ?
WinAAttributes db ?
WinBAttributes db ?
WinGranularity dw ?
WinSize dw ?
WinASegment dw ?
WinBSegment dw ?
WinFunctPtr dd ?
BytesPerScanLine dw ?
;Mandatory information for VBE 1.2 and above
XResolution dw ?
YResolution dw ?
XCharSize db ?
YCharSize db ?
NumberOfPlanes db ?
BitsPerPixel db ?
NumberOfBanks db ?
MemoryModel db ?
BankSize db ?
NumberOfImagePages db ?
Reserved0 db 1
;Direct Color fields (required for direct/6 and YUV/7 memory models)
RedMaskSize db ?
RedFieldPosition db ?
GreenMaskSize db ?
GreenFieldPosition db ?
BlueMaskSize db ?
BlueFieldPosition db ?
RsvdMaskSize db ?
RsvdFieldPosition db ?
DirectColorModeInfo db ?
;Mandatory information for VBE 2.0 and above
PhysBasePtr dd ?
Reserved1 dd 0
Reserved2 dw 0
;Mandatory information for VBE 3.0 and above
LinButesPerScanLine dw ?
BnkNumberOfImagePages db ?
LinNumberOfImagePages db ?
LinRedMaskSize db ?
LinRedFieldPosition db ?
LinGreenMaskSize db ?
LinGreenFieldPosition db ?
LinBlueMaskSize db ?
LinBlueFieldPosition db ?
LinRsvdMaskSize db ?
LinRsvdFieldPosition db ?
MaxPixelClock dd ?
Reserved3 db 189 dup (?)
ModeInfoBlock ends
.data
;----------------------
;this section containe the images of chars
;здесь будет шрифт
;char 8x14
db 528 dup (0)
db 1504 dup (?)
;----------------------
;back buffer for matrix
;временный буфер
matrix db 64*2*30 dup (0)
;-----------------------------
;|1|0|0|1|0|1|1|1|0|1|0|0|0|1| - 1 есть символ
;|0|1|0|1|0|1|1|1|0|1|1|0|0|1| - 0 нет символа
;|1|0|0|1|0|1|1|1|0|1|0|0|1|1| - положение символа на экране:
;|1|0|0|1|0|1|0|1|0|1|1|0|1|1| x = col * 10
;|1|0|1|0|0|1|1|0|0|1|0|0|0|1| y = row * 16 + смещение (цикл полного сдвига вниз)
;-----------------------------
;+ (0-16) - смещение таблицы на экране
;----------------------
vgainfo VgaInfoBlock <>
modeinfo ModeInfoBlock <>
color RGB <>
db 10 dup (?)
mas db "Hello!",0dh,"How are you?",0dh,"My name is Artsem",0
press db 0dh,"Press any key to continue.",0
x dw 0
y dw 0
xres dw 160
yres dw 25
attrib db 0ah
char db 0
val dw 0
seed1 dw 13h
seed2 dw 4h
xg_res dw 640
yg_res dw 480
sss dd 65536
temp dd ?
tempw dw ?
last_k dw -1
_signature db "VESA Signature: ",0
_version db "VESA Version: ",0
_memory db "Total memory: ",0
_nBanks db "Number of banks: ",0
_BankSize db "Bank size: ",0
_res db "About 640x480x32:",0dh,0
_error db "Error! Press any key to exit",0
_exit db "Program terminated. Press any key to continue...",0
_copyright db "Author: Korolyov Artyom PO521. Press any key to continue...",0
_image db "back.raw",0
_font db "font.fnt",0
_a db "Note: this is a demo!",0
.code
;-------------------------------------------------
load_font proc
push dx
push cx
push bx
mov ax, 3d00h
mov dx, offset _font
int 21h
mov bx, ax
mov ah, 3fh
mov dx, 528
mov cx, 1504
int 21h
mov ah, 3eh
int 21h
pop bx
pop cx
pop dx
ret
endp
;---------------------------------------------------------------------------------------
calc_graphics_offset proc _x:WORD, _y:WORD ;uses ax - y, bx - x; return result in ax
push bp
mov bp, sp
_32
sub dx, dx
_32
sub ax, ax
mov ax, _y
_32
mov bx, ax
_32
; shl ax, 9
db 0c1h
db 0e0h
db 09h
_32
; shl bx, 7
db 0c1h
db 0e3h
db 07h
_32
add ax, bx
mov dx, _x
_32
add ax, dx
_32
mov bx, ax
_32
; shr ax, 16
db 0c1h
db 0e8h
db 10h
mov dx, ax
_32
; shl ax, 16
db 0c1h
db 0e0h
db 10h
_32
sub bx, ax
mov bp, bx
xor bx, bx
cmp last_k, dx
je _next
mov last_k, dx
call dword ptr offset modeinfo.WinFunctPtr
_next:
mov ax, bp ;d
pop bp
ret 4
endp
;---------------------------------------------------------------------------------------
clear_text_screen proc
push es
push cx
push dx
push bx
mov ax, 0b800h
mov es, ax
mov cx, 2000
xor dx,dx
xor bx,bx
@@cl_up:
mov es:[bx], dx
inc bx
inc bx
loop @@cl_up
pop bx
pop dx
pop cx
pop es
mov x, 0
mov y, 0
ret
endp
;---------------------------------------------------------------------------------------
;calculate offset in video buffer
calc_offset proc
push bx
mov ax, y
mov bx, ax
shl ax, 7
shl bx, 5
add ax, bx
add ax, x
pop bx
ret
endp
;---------------------------------------------------------------------------------------
;this function return the length of the string, including zero symbol at the end of the string
StrLength proc ptStr:WORD
push bp
mov bp,sp
push si
push cx
mov si, [bp + 4]
xor cx,cx
@@a10:
lodsb
inc cx
cmp al, 0
jne @@a10
mov ax, cx
pop cx
pop si
pop bp
ret 2
endp
;---------------------------------------------------------------------------------------
;convert 16bit integer value to ASCII string
inttostr proc ptStr:WORD, Value:WORD
push bp
mov bp, sp
push es
push di
push si
push cx
push dx
mov ax, @data
mov es, ax
mov di, ptStr
mov si, di
mov ax, Value
mov cx, 10
@@first:
cmp ax, 10
jb @@last
xor dx, dx
div cx
or dl, 30h
mov [di], dl
inc di
jmp @@first
@@last:
or al, 30h
mov [di], al
mov byte ptr [di+1],0
;reverse
@@rev:
cmp di, si
jbe @@end
mov dl, [di]
mov al, [si]
mov [di], al
mov [si], dl
dec di
inc si
jmp @@rev
@@end:
pop dx
pop cx
pop si
pop di
pop es
pop bp
ret 4
endp
;-------------------------------------------------------------------------------------------
;write null-terminated string to screen that in text mode
write0 PROC ptStr:WORD
push bp
mov bp, sp
push es
push si
push di
push dx
mov ax, 0b800h
mov es, ax
mov si, [bp + 4]
call calc_offset
mov di, ax
mov dh, attrib
@@a1:
mov dl, ds:[si]
cmp dl, 0
je @@a15
cmp dl, 0dh
jne @@a14
mov x, 0
inc y
call calc_offset
mov di, ax
inc si
jmp @@a1
@@a14:mov es:[di], dx
inc si
add di, 2
add x, 2
jmp @@a1
@@a15:
pop dx
pop di
pop si
pop es
pop bp
ret 2
ENDP
;-------------------------------------------------------------------------------------------
;write fixed number of charachers in string to screen in text mode
write PROC ptStr:WORD, N:WORD
push bp
mov bp, sp
push es
push si
push di
push cx
mov ax, 0b800h
push ax
pop es
mov si, [bp + 4]
call calc_offset
mov di, ax
mov ah, attrib
mov cx, [bp + 6]
add x, cx
@@a2:
mov al, ds:[si]
mov es:[di], ax
inc si
add di, 2
loop @@a2
a14:
pop cx
pop di
pop si
pop es
pop bp
ret 4
endp
;-----------------------------------------------------------------------------
;get information about vesa
do_vesa_info proc
mov ax, @data ;load address of data segment
mov es, ax ;and copy it to es
mov ax, 4f00h ;vesa function
mov di, offset vgainfo
int 10h ;get vesainfo
;output text - VESA Signature
mov bx, offset _signature
push bx
call write0
;output vesa signature
mov attrib, 0ch ;attribut of text
mov ax, 4 ;number of char to out
push ax
mov bx, offset vgainfo.VESASignature
push bx
call write
;output text - VESA Version
mov attrib, 0ah
mov x, 0 ;go to new line
inc y
mov bx, offset _version
push bx
call write0
;conver int version to string
mov attrib, 0ch
mov ax, vgainfo.VESAVersion
xchg ah, al
push ax
mov bx, offset mas
push bx
call inttostr
;out version
push bx
call write0
;out text - Total memory
mov attrib, 0ah
mov x, 0
inc y
mov bx, offset _memory
push bx
call write0
mov ax, vgainfo.TotalMemory
;shl ax, 6
push ax
mov bx, offset mas
push bx
call inttostr
mov attrib, 0ch
push bx
call write0
mov bx, offset vgainfo.oemvendornameptr
mov dx, [bx]
mov ax, [bx+2]
push ds
mov ds, ax
push dx
mov x,0
inc y
call write0
mov dx, [bx+4]
mov ax, [bx+6]
mov ds, ax
push dx
call write0
mov dx, [bx+8]
mov ax, [bx+10]
mov ds, ax
push dx
call write0
xor ax,ax
int 16h
call clear_text_screen
mov attrib, 00011001b
mov bx, offset _res
push bx
call write0
call do_mode_info
pop ds
ret
endp
;------------------------------------------------------------------------------
;get information about video mode
do_mode_info proc ;mode:word
push bp
push cx
push es
push di
push bx
mov attrib, 0ch
mov ax, @data
mov es, ax
mov ax, 4f01h
mov cx, 112h
mov di, offset modeinfo
int 10h
mov bx, offset mas
push modeinfo.xresolution
push bx
call inttostr
push bx
call write0
mov al, 'x'
mov ah, 0h
mov word ptr [bx], ax
push bx
call write0
push modeinfo.yresolution
push bx
call inttostr
push bx
call write0
mov al, 'x'
mov ah, 0h
mov word ptr [bx], ax
push bx
call write0
mov al, modeinfo.bitsperpixel
cbw
push ax
push bx
call inttostr
push bx
call write0
pop bx
pop di
pop es
pop cx
pop bp
ret
endp
;-----------------------------------------------------------------
;put pixel
put_pixel proc _x:word, _y:word, lpColor:word
push bp
mov bp, sp
push bx
push dx
push es
push si
mov si, lpColor
mov ax, _y ;_y
mov bx, _x ;_x
push ax
push bx
call calc_graphics_offset
mov bx, ax
mov ax, 0a000h
mov es, ax
mov dx, 3c4h
mov ax, 0102h ;b
out dx, ax
mov al, byte ptr ds:[si+2]
mov es:[bx], al
mov ax, 0202h ;g
out dx, ax
mov al, byte ptr ds:[si+1]
mov es:[bx], al
mov ax, 0402h ;r
out dx, ax
mov al, byte ptr ds:[si]
mov es:[bx], al
pop si
pop es
pop dx
pop bx
pop bp
ret 6
endp
;----------------------------------------------------------------
;put image on screen at x,y
;first two words in RowData must be _width and _height
put_image proc _x:word, _y:word, lpStr:word
push bp
mov bp, sp
push bx
push cx
push dx
push di
mov ax, 3d00h
mov dx, lpStr
int 21h
mov si, offset mas
mov bx, ax
mov ah, 3fh
mov dx, si
mov cx, 2
int 21h
add si, 2
mov ah, 3fh
mov dx, si
int 21h
mov di, _x
mov cx, ds:[si]
.img1: ;x - di
push cx
mov si, offset mas
mov cx, ds:[si]
.img2:
push cx
mov ah, 3fh
mov dx, offset color
mov cx, 3
int 21h
pop cx
push dx
mov ax, _y
push ax
push di
call put_pixel
inc di
loop .img2
pop cx
inc _y
mov di, _x
loop .img1
pop di
pop dx
pop cx
pop bx
pop bp
ret 6
endp
;----------------------------------------------------------------
;draw a char
put_char proc _x:word, _y:word, lpChar:word, lpColor:word
push bp
mov bp, sp
push si
xor ax, ax
mov al, byte ptr [lpChar]
shl ax, 4
mov si, ax
mov cx, 16
mov bx, _x
mov dx, _y
.putchar1:
mov al, byte ptr ds:[si]
push cx
mov cx, 8
.putchar2:
shr al, 1
jnc .putcharend
push ax
push lpColor
push dx
push bx
call put_pixel
pop ax
.putcharend:
inc bx
loop .putchar2
pop cx
inc dx
mov bx, _x
inc si
loop .putchar1
pop si
pop bp
ret 8
endp
;----------------------------------------------------------------
;draw string
g_write0 proc _x:word, _y:word, lpStr:word, lpColor:word
push bp
mov bp, sp
push si
push dx
push bx
xor ax, ax
mov si, lpStr
mov dx, _x
mov bx, _y
.g_write0_start:
mov al, byte ptr ds:[si]
cmp al, 0
je .g_write0_end
cmp al, 0dh
jne .g_write0_next
add bx, 15
mov dx, _x
inc si
mov al, byte ptr ds:[si]
.g_write0_next:
push bx
push dx
push lpColor
push ax
push bx
push dx
call put_char
pop dx
pop bx
add dx, 8
inc si
jmp .g_write0_start
.g_write0_end:
pop bx
pop dx
pop si
pop bp
ret 8
endp
;----------------------------------------------------------------
;set video mode 112 (640x480x16.8M)
set_mode proc
;установка видео режима 112h
;при ошибке вывод соответствующего сообщени¤
;иначе сбор сведений в modeinfo
;должна вызыватьс¤ до начала процесса рисовани¤
;и после определени¤ хар-ик всех доступных видеорежимов
;выключаем экран
screen_off
push bx
mov ax, 4f02h
mov bx, 112h
int 10h
cli
;устанавливаем режим байтов контроллера ЁЋ“
push dx
mov dx, 3d4h
mov al, 17h
out dx, al
inc dx
in al, dx
or al, 01000000b
out dx, al
;сбрасываем режим "двойное слово" контроллера ЁЋ“
dec dx
mov al, 14h
out dx, al
inc dx
in al, dx
and al, 10111111b
out dx, al
;сбрасываем режим "цепочка 4"
mov dx, 3c4h
mov al, 4
out dx, al
inc dx
in al, dx
and al, 11110111b
out dx, al
;разрешаем все плоскости дл¤ записи
dec dx
mov al, 2
out dx, al
inc dx
or al, 00001111b
out dx, al
sti
pop dx
;включаем экран
screen_on
push es
push cx
push di
mov ax, @data
mov es, ax
mov ax, 4f01h
mov cx, 112h
mov di, offset modeinfo
int 10h
mov color.r, 0
mov color.g, 0
mov color.b, 255
mov dx, offset color
mov ax, offset _nBanks
sub cx,cx
push dx
push ax
push cx
push cx
call g_write0
sub ax, ax
mov al, modeinfo.numberofbanks
cbw
mov bx, offset mas
push ax
push bx
call inttostr
push dx
push bx
mov ax, 200
push cx
push ax
call g_write0
mov ax, offset _a
mov bx, 0
add cx, 20
push dx
push ax
push cx
push bx
call g_write0
pop di
pop cx
pop es
xor ax,ax
jmp set_mode_exit
set_mode_error_exit:
mov ax, 3
int 10h
mov bx, offset _error
mov y, 0
mov x, 0
push bx
call write0
xor ax,ax
int 16h
mov ax, 0ffffh
set_mode_exit:
pop bx
ret
endp
;------------------------------------------
;show splash
splash proc
mov bx, offset _image
push bx
sub bx, bx
push bx
push bx
call put_image
mov color.b, 255
mov color.r, 0
mov color.g, 0
mov bx, offset color
push bx
mov bx, offset _copyright
push bx
mov bx, 465
push bx
sub bx, bx
push bx
call g_write0
sub ax,ax
int 16h
ret
endp
; generate random value
rand proc
mov cx, seed1
mov bx, seed2
mov dx, 015ah
mov ax, 4e35h
push si
xchg si, ax
xchg dx, ax
test ax,ax
je _111
mul bx
_111:
jcxz _222
xchg cx, ax
mul si
add ax, cx
_222:
xchg si, ax
mul bx
add dx, si
pop si
add ax, 0001h