File: display.asm

package info (click to toggle)
dosemu-freedos 1%3A0.0.b9r5a%2Betch.1-0etch1
  • links: PTS
  • area: contrib
  • in suites: etch
  • size: 19,744 kB
  • ctags: 23,279
  • sloc: ansic: 143,864; asm: 20,397; makefile: 3,868; perl: 1,106; yacc: 690; sh: 553; pascal: 297; xml: 150; cpp: 67
file content (1217 lines) | stat: -rw-r--r-- 39,054 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217

;   FreeDOS DISPLAY.SYS           v0.10
;   FreeDOS PRINTER.SYS
;
;   ===================================================================
;
;   FreeDOS driver to add support of codepage management for the
;   default CON driver (for screen) or default PRN driver (for printer)
;
;   Copyright (C) 2002-03 Aitor Santamara_Merino
;   email:  aitor.sm@wanadoo.es
;
;   (contributed code from DISPLAY 0.5b package, and patches by
;    Ilya V. Vasilyev aka AtH//UgF@hMoscow (hscool@netclub.ru)
;    in  VIDEOINT.ASM, SELECTD.ASM and DISPHW.ASM)
;
;   WWW:    http://www.freedos.org/
;
;   ===================================================================
;
;   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 2 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, write to the Free Software
;   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
;   ===================================================================
;
;   Limitations before version 1.0:
;   - commandline tool (no device driver, no IOCTL yet)
;   - to be used with supplied MODECON.EXE only
;   - parses RAW files only (no CPI files)

CPU     8086

;
;   Driver you want to compile (uncomment ONLY one!)
;

%define DISPLAY
;%define PRINTER

;       .       .       .       .       .       .       line that rules
;
;===================================================================
; VERSION CONSTANT
;===================================================================
;
; NOTE: To change it, please also modify the output string at the
;       bottom of the file

Version                 EQU     000AH   ; Version of the driver


;===================================================================
; DRIVER ERROR CONSTANTS
;===================================================================
;
; Explained the functions where errors are admissible

ERR_CPNotPrepared       EQU     26      ; Select (SW)
ERR_CPNotSelected       EQU     26      ; Query
ERR_KEYBFailed          EQU     27      ; Select (HW/SW)
ERR_QueryDeviceError    EQU     27      ; Query
ERR_DevCPNotFound       EQU     27      ; Prepare
ERR_SelPrepDeviceError  EQU     29      ; Select (HW/SW), Prepare
ERR_FileDamaged         EQU     31      ; Prepare

;===================================================================
; OTHER CONSTANTS
;===================================================================
;

CommandLine_Offset      EQU     080h    ; where in PSP is commandline

%ifdef          DISPLAY
DriverSignature         EQU     1
%endif
%ifdef          PRINTER
DriverSignature         EQU     2
%endif

;===================================================================
; PROGRAM HEADER
;===================================================================
;
;               .       .       .       .       .       line that rules

                ORG     100H
                jmp     NEAR Install     ; simply go to Install

DevName         DB      "        "      ; Device name

;===================================================================
; INTERRUPT SERVICES  (DISPLAY only)
;===================================================================
;
;       .       .       .       .       .       .       line that rules

%ifdef          DISPLAY

%include        "videoint.asm"
%include        "muxint.asm"

%endif

;===================================================================
; SELECT RESIDENT SUBROUTINES
;===================================================================
;
;       .       .       .       .       .       .       line that rules


; Fn:   FindCodepage
; Does: Finds a codepage in the prepared array
; In:   BX=new codepage
; Out:  CL=position in table (starts on 0=first hardware codepage, ...)
;          (0ffh if not found)
;       CH=00h


FindCodepage:   xor     cx,cx           ; CL: total table size
                mov     cl,[cs:wNumSoftCPs]
                add     cl,[cs:wNumHardCPs]
                push    bx
                mov     ax,bx
                mov     bl,cl           ; copy of table size on BL
                inc     cl              ; to control the loop better

                mov     di,wHardCPs     ; DS:SI: begining of table
                push    es
                push    cs
                pop     es
                cld

        repne   scasw

                test    cl,0ffh
                jnz     CodepageFound
                mov     cl,0ffh
                jmp     FindCodepageEnd


CodepageFound:
                xchg    bl,cl
                sub     cl,bl           ; tablesize-loop = position

FindCodepageEnd:
                pop     es
                pop     bx
                ret


; Fn:   SelectCodepage
; Does: Selects a codepage from the pool
; In:   BX=new codepage
; Out:  AX=0 if not found, and in this case
;         DX= error code (otherwise DX untouched)
;


SelectCodepage: ;************ PUSH globally DI, SI, DX

                push    di
                push    si
                push    dx

                ;************ check if KEYB/PRINT are ready for the change

                push    bx              ; save cp number, just in case
;jmp KeybOK ; Fast and dirty way to bypass Keyb support
%ifdef          DISPLAY
                mov     ax,0ad80h       ; first see if it is installed
                int     02fh
                cmp     al,0ffh
                jne     KeybOK          ; if not found, continue
                mov     ax,0ad81h
                pop     bx
                clc
                push    bx
                int     02fh
                jnc     KeybOK
                jmp     KeybFailed
%endif

%ifdef          PRINTER
                push    ds
                mov     ax,0104h        ; Freeze status to read config
                int     02fh
                jc      PrintFailed
                mov     dl,[ds:si]      ; get first byte of the first
                pop     ds              ; file in the print queue
                mov     ax,0105h        ; Restore status (hope to succeed!)
                int     02fh
                test    dl,0ffh         ; now test if there are files to
                jz      KeybOK          ; print, if there is any -> fail
                jmp     KeybFailed      ; (DS already pop-ed)

PrintFailed:    pop     ds
%endif

KeybFailed:     pop     bx              ; KEYB or PRINT are not ready
                mov     dx, ERR_KEYBFailed
                jmp     SelectCodepageError

                ;************ If =current, then REFRESH

KeybOK:
                pop     bx              ; recover codepage number

                cmp     bx, [cs:wCPselected]
                jne     CheckBuffer

                push    cx
                test    BYTE [cs:bActive], 0ffh
                jz      RefreshHW

PreRefreshSW:
                push    es
                push    ds
                
                jmp     RefreshSW

                ;************ see if it is the one in the buffer

CheckBuffer:
                cmp     bx,[cs:wCPbuffer]
                jne     NotRefresh

                push    cx
                jmp     PreRefreshSW

                ;************ see if it is in the table
                
NotRefresh:
                push    cx
                call    FindCodepage    ; see if we find it
                cmp     cl, 0ffh
                jne     CPFound
                mov     dx, ERR_CPNotPrepared
                pop     cx
                jmp     SelectCodepageError

CPFound:
                ;************ READY TO SELECT

                cmp     cx,[cs:wNumHardCPs]
                jae     SetSWCodepage
                mov     [cs:whtCPselected], cx

                ;************ set the hardware codepage

RefreshHW:      mov     cx,[cs:whtCPselected]
                call    [cs:pRefreshHWcp]
                pop     cx

                jc      SelectCodepageError

                mov     BYTE [cs:bActive],0

                jmp     SelectCodepageSuccess

                ;************ set the software codepage

SetSWCodepage:

                push    es
                push    ds

                push    cs              ; source at ds:si
                pop     ds

                mov     si,SCP1
                sub     cx,[cs:wNumHardCPs]
                mov     al,cl
                mov     dx,[cs:wTableSize]
                shl     dx,1            ; table size is in Words
                mul     dx              ; offset = SCP1 + TableSize * pos
                add     si,ax

                push    cs              ; target at es:di
                pop     es

                mov     di,bFont8x8

                mov     cx,[cs:wTableSize]
           rep  movsw

RefreshSW:
                mov     ax,0b000h       ; first see if GRAFTABL is active
                int     02fh
                cmp     al,0ffh
                jne     NoGraftabl

                push    bx              ; preserve BX!
                mov     ax,0b001h       ; GRAFTABL present, import the
                push    cs              ; table to DS:BX
                pop     ds
                mov     bx,dGraftablBuf
                int     02fh
                pop     bx
                
NoGraftabl:
                mov     al,-1           ; unknown vide mode!
                call    [cs:pRefreshSWcp]

                pop     ds
                pop     es
                pop     cx

                mov     [cs:wCPbuffer],bx
                                        ; the buffer we copied

                mov     dx, ERR_SelPrepDeviceError
                                        ; in case of success, DX will be
                                        ; POP-ed

                jc      SelectCodepageError
                
                mov     BYTE [cs:bActive],1

                ;************ exit routines: we have BX=CP

SelectCodepageSuccess:

                mov     [cs:wCPselected],bx
                mov     ax, 0001h
                clc

                jmp     SelectCodepageEnd2

SelectCodepageError:
                mov     si,sp
                mov     word [ss:si],dx
                xor     ax,ax
                stc
                jmp     SelectCodepageEnd2

SelectCodepageEnd:
                pop     cx
SelectCodepageEnd2:
                pop     dx
                pop     si
                pop     di
                ret
                

;===================================================================
; PREPARE RESIDENT SUBROUTINES
;===================================================================
;
;       .       .       .       .       .       .       line that rules


; Fn:   ReadSubfont
; Does: Reads a subfont from the appropriate section of the CPI file
; In:   DS:SI->  position in the CPI file where the subfont starts
;       ES:DI->  position in the DISPLAY buffers where to copy (begining)
; Out:  CF set on error, clear otherwise
;       If success: DI,SI point to the byte immediately after the whole block
;       SI,AX,CX: trashed
;
; FORMAT OF THE SUBFONT  (offsets respect to DI)
; ---------------------
; 0  DB  Size:     values 16, 14 or 8
; 1  DB  witdth:   MUST be 8
; 2  DW  (ignored)
; 4  DW  n.chars:  MUST be 255
; 6  <data>
;

ReadSubfont:

                ;**************  check DB 8 annd DW 255
                mov     al,[ds:si+1]
                cmp     al,8
                jne     ReadSubfontError
                mov     ax,[si+4]
                cmp     ax,256
                jne     ReadSubfontError

                ;************** check the number of subfonts
                mov     al,[si]         ; get the subfont size to ax
                mov     cx,[cs:wNumSubFonts]

                cmp     al,8
                je      ComputeSizeToMove
                cmp     cx,2
                jb      ReadSubfontError

                cmp     al,14
                je      ComputeSizeToMove
                cmp     cx,3
                jb      ReadSubfontError

                cmp     al,16
                jne     ReadSubfontError
                
                ;************** add the appropriate offset
                ;               get CX=number of WORDS to move
ComputeSizeToMove:
                add     si,6            ; now points to the data
                mov     cx,2048         ; initialize to 2048 bytes (8x256)
                
                cmp     al,8
                je      ReadSubfontMoveData
                add     di,cx           ; destination now skips 8x8
                mov     cx,3584         ; set to 3584 bytes (14x256)
                cmp     al,14
                je      ReadSubfontMoveData
                cmp     al,16
                jne     ReadSubfontError
                add     di,cx           ; destination now skips 8x14
                mov     cx,4096         ; set to 4096 bytes (16x256)

                ;************** Move the data
ReadSubfontMoveData:
                shr     cx,1            ; WORD granularity!
                cld
           rep  movsw

                clc
                ret

ReadSubfontError:
                stc
                ret

; Fn:   ReadCodepage
; Does: Reads a codepage from the appropriate section of the CPI file
; In:   DS:SI->  position in the CPI file where the codepage starts
;       DX:      number of buffer where to read font
;       BX:      codepage number to be set
; Out:  CF=1:    either success or error in file (DX updated).
;                Prepare must termiante
;       CF=0:    this portion of the CPI file does not contain the needed
;                information. Prepare may loop to the next entry
;
; Uses: AX, CX, SI, ES, DI
;
; FORMAT OF THE CODEPAGE (FONT) (offsets respect to DI)
; ----------------------
;  0  DW      FontHeaderSize (usually 28)
;  2  DD      Far Pointer to Next Codepage (FONT)
;  6  DW   *  Driver type signature (1=DISPLAY, 2=PRINTER)
;  8  8DB  *  Hardware type string (e.g. "EGA     ")
; 16  DW   +  CP-ID (e.g. 850)
; 18  6DB     Reserved (empty)
; 24  DD      Pointer to next FontHeader
; --- the  limit of the font header (28)
; 28  DW   *  Signature (1) (FONT)
; 30  DW   *  Number of subfonts (usually 3)
; 32  DW   *  Size of the whole subfonts block (usually 9746 bytes)
;
; (*)  to be tested

ReadCodepage:
                ;**************  check all signatures and everything
                mov     ax,[si+6]      ; it is for us
                cmp     ax,DriverSignature
                jne     ReadCodepageNext

                mov     ax,[si+28]      ; it is a FONT
                cmp     ax,1
                jne     ReadCodepageError

                push    si
                add     si,8            ; DS:SI->String in CPI file

                push    cs
                pop     es
                mov     di,sHWFontName  ; ES:DI->Our font name

                mov     cx,8
          repe  cmpsb
                pop     si
                jne     ReadCodepageNext

                mov     ax,[si+30]      ; we force it to have 3 subfonts
                cmp     ax,3
                jne     ReadCodepageError

                mov     ax,[si+32]      ; we force it to be 9746
                cmp     ax,9746
                jne     ReadCodepageError

                ;**************  read the data
                add     si,34           ; 34-16=18

                push    dx
                mov     ax, [cs:wTableSize]
                shl     ax, 1           ; table size is in Words
                mul     dx              ; offset = SCP1 + TableSize * pos
                add     ax, SCP1
                mov     di, ax
                pop     dx

                push    di
                call    ReadSubfont
                pop     di
                jc      ReadCodepageError

                push    di
                call    ReadSubfont
                pop     di
                jc      ReadCodepageError

                call    ReadSubfont
                jc      ReadCodepageError

                mov     di, [cs:wNumHardCPs]
                add     di, dx
                shl     di, 1
                add     di, wHardCPs    ; everything correct: store cp
                mov     [cs:di], bx

                ;**************  success!!
                xor     dx,dx
                jmp     ReadCodepageGotoend

                ;**************  exit points

ReadCodepageError:
                mov     dx,ERR_FileDamaged
ReadCodepageGotoend:
                stc
                jmp     ReadCodepageEnd
                
ReadCodepageNext:
                clc

ReadCodepageEnd:
                ret


; Fn:   PrepareCodepage
; Does: Prepares the codepage in the oldest pool
; In:   BX:      codepage number
;       DS:SI->  RAW table to be copied from
;       DX:      position in the table where TO copy (starts on 0)
; Out:  DX:      Error code, 0 if not error
;
; FORMAT OF THE CODEPAGE (FONT)
; ----------------------
;  0  DB      0ffh, "FONT   "
;  8  DB 15   (ignore)
; 23  DW      Number of CPs in file
; 25  --- (Font 1 starts here)

PrepareCodepage:
                push    ax
                push    cx
                push    es
                push    si
                push    di

                ;*** test 1: number buffer < MaxBuffers

                cmp     dl,[cs:wNumSoftCPs]
                ja      passtable       ; compare position with array size

                ;*** test 2: header of the CPI file

                push    cs              ; ES segment = CS
                pop     es


                mov     di, sCPIsignature
                mov     cx, 8
           repe cmpsb
                jne     ErrorInSubfonts

                mov     cx, [ds:si+15]  ; 15=23-8; number of CPI files
                add     si,17           ; si-> first font

loopFindCP:     cmp     bx,[ds:si+16]
                jne     nextFindCP
                call    ReadCodepage
                stc
                jc      prepareEnd
nextFindCP:
                add     si,9780         ; 9780=distance between fonts
                loop    loopFindCP

                mov     dx, ERR_DevCPNotFound
                jmp     prepareEnd

ErrorInSubfonts:
                mov      dx,ERR_FileDamaged
                call     prepareEnd

passtable:      mov     dx, ERR_SelPrepDeviceError

prepareEnd:
                pop     di
                pop     si
                pop     es
                pop     cx
                pop     ax

                ret


;===================================================================
; HARDWARE DEPENDANT SELECT ROUTINES
;===================================================================
;
;       .       .       .       .       .       .       line that rules

%ifdef          DISPLAY
%include        "selectd.asm"
%endif

%ifdef          PRINTER
%include        "selectp.asm"
%endif




;===================================================================
; DISPLAY DATA SEGMENT
;===================================================================
;
;               .       .       .       .       .       line that rules

bActive         DB      0               ; SW codepage selected?
dGraftablBuf    DD      0               ; Graftabl table Buffer
wCPselected     DW      -1              ; Selected codepage
wCPbuffer       DW      0               ; CP in buffer bFont8x8
whtCPselected   DW      0               ; position in the hardware table of
                                        ; current hardware codepage
wTableSize	DW	0		; Total RAW table size in WORDS

pRefreshHWcp    DW      0               ; Hardware codepage select procedure
pRefreshSWcp    DW      0               ; Software codepage select procedure

sCPIsignature   DB      0ffh,"FONT   "  ; signature of a CPI file
sHWFontName     DB      "        "      ; space for the hardware name to be
                                        ; searched in the CPI file

bFont8x8        times   2048 DB 0       ; space for the 8x8 font
bFont8x14       times   3584 DB 0       ; space for the 8x14 font
bFont8x16       times   4096 DB 0       ; space for the 8x16 font

wNumSoftCPs     DW      0               ; number of software codepages
wNumSubFonts    DW      0               ; number of subfonts (m)
wNumHardCPs     DW      1               ; number of hardware codepages
wHardCPs        DW      0               ; hardware CP list
wSoftCPs        DW      0,0,0,0,0       ; number of software codepages


;===================================================================
; DISPLAY DATA POOLS
;===================================================================
;               .       .       .       .       .       line that rules
;
; Prepared Codepage Tables (array of these structures)
; (maximum= 5 of the (8,14,16) size
;

SCP1            times 9728 db 0
                times 9728 db 0
                times 9728 db 0
                times 9728 db 0
                times 9728 db 0

bLastByte:


;===================================================================
; NON-RESIDENT VARIABLES
;===================================================================
;

pMemTop:        DW      bLastByte       ; top of memory
wMinFontSize:   DW      0               ; minimum font size
                                        ; 1=8x8  2=8x8,14  3=8x8,14,16
bMaxHWcpNumber: DB      0               ; maximum number of hardware CPs
                                        ; admissible, 0 if unknown


;===================================================================
; NON-RESIDENT HARDWARE DEPENDANT ROUTINES AND TABLE
;===================================================================
;

%ifdef          DISPLAY
%include        "HWInitD.asm"
%endif

%ifdef          PRINTER
%include        "HWInitP.asm"
%endif



;===================================================================
; MAIN FOR THE .COM VERSION
;===================================================================
;
;               .       .       .       .       .       line that rules


Install:
                push    cs
                call    DoInstall       ; call DoInstall
                jc      Done            ; Carry? then we should exit
                mov     dx,[pMemTop]
                int     27H             ; TSR  for .COM files
Done:           int     20H             ; Exit for .COM files



;===================================================================
; INSTALLATION RUNTIME FUNCTIONS
;===================================================================
;
;               .       .       .       .       .       line that rules


; Fn:   OutStrDX
; In:   DX: near pointer to string to be displayed
; Out:  -

OutStrDX:       mov     ah,9
                int     21H
		stc			; Error
                ret			; From DoInstall



; Fn:   DoInstall
; In:   -
; Out:  CF on error (do not stay resident)


DoInstall:
                ;****** show copyright and version

                mov     dx,copyVer
                call    OutStrDX

                ;****** check if already loaded
%ifdef DISPLAY
                mov     ax,0ad00H
                int     2fH
                cmp     al,-1
                jne     TestDRK

                mov     dx,errAlready
                jmp     OutStrDX

                ;****** Some versions of DR-KEYB are a no-no
TestDRK:
                mov     ax,0ad80h
                xor     bx,bx
                mov     cx,bx
                int     02fh
                cmp     ah,0ffh         ; KEYB installed?
                jne     ScanCommandline
                test    bx,0ffffh       ; MS-KEYB?
                jnz     ScanCommandline
                test    cx,0ffffh       ; FD-KEYB?
                jz      ScanCommandline
                cmp     ch,7
                jae     ScanCommandline ; check DR-KEYB version
                mov     dx,errNoDRDOS
                jmp     OutStrDX
%endif

                ;****** SCAN COMMANDLINE, in STAGES
ScanCommandline:
                cld 
                cli

                push    cs
                pop     es
                push    cs
                pop     ds


                ; 1.- Find the string and capitalize it

                mov     cx, [cs:CommandLine_Offset]
                mov     si, CommandLine_Offset+1
loop0:          mov     al, [cs:si]
                cmp     al, 13
                je      endUpper
                cmp     al, 'a'
                jb      cont0
                cmp     al, 'z'
                ja      cont0
                sub     al, 'a'-'A'
                mov     [cs:si], al
cont0:          inc     si
                loop    loop0
endUpper:

                ; 2.- Remove starting spaces
    
                mov     cx, [cs:CommandLine_Offset]
                mov     si, CommandLine_Offset+1
loop1:          lodsb
                cmp     al, 13
                jne     skp1
                mov     dx, SES_ParamRequired
                jmp     SyntaxError
skp1:           cmp     al, ' '
                loopne  loop1


                ; 3.- Save device name
                ;     Also block illegal chars (ASCII compatibility
                ;     assumed)

                mov     di, DevName
                mov     cx, 8
loop2:          mov     al, [es:si]
                cmp     al, '='
                je      EndDevName
                cmp     al, ':'
                je      EndDevName
                cmp     al, 13
                jne     skp2
                mov     dx, SES_UnexpectedEOL
                jmp     SyntaxError
skp2:           cmp     al, '/'
                je      illegalChar
                cmp     al, '\'
                je      illegalChar
                cmp     al, 34          ; illegal char: "
                je      illegalChar
                cmp     al, '*'
                je      illegalChar
                cmp     al, '|'
                je      illegalChar
                cmp     al, '<'         ; illegal chars: '<'..'?'
                jb      cont1
                cmp     al, '?'
                ja      cont1
                cmp     al, '='         ; and =
                je      cont1              
                jmp     illegalChar
cont1:          movsb
                loop    loop2
                mov     dx, SES_NameTooLong
                jmp     SyntaxError

illegalChar:    mov     dx, SES_IllegalChar
                jmp     SyntaxError

EndDevName:
                cmp     al, ':'
                jne     cont2
                inc     si

cont2:          inc     si              ; it was pointing to =
                mov     al, '('
                mov     di, si
                scasb
                je      skp3
                mov     dx, SES_OpenBrExpected
                jmp     SyntaxError
       

                ; 4.- Get hardware name and initialize it

skp3:           inc     si
                mov     di, HardwareTables

loop3b:
                push    si              ; si->begining of the name
                mov     cx, 8           ; length
           repe cmpsb
                je      HwTypeFound8c   ; if =, CX=0 => found (check ,)
                mov     al, [ds:si-1]
                mov     dl, [es:di-1]

                add     di,cx           ; di->CP-HardwareName
                
                cmp     al, ','
                jne     skp3b           ; if commandline<>, error

                test    dl, 0ffh
                jz      HwTypeFound     ; if sourcename<>0 error

skp3b:
                add     di,12           ; skip the table
                mov     al, [es:di]
                pop     si              ; recover si->beginig of the name
                test    al, 0ffh        ; 0=end of the table
                jnz     loop3b

                mov     dx, SES_WrongHwName
                jmp     SyntaxError     ; hardware NOT supported

HwTypeFound8c:  mov     al, [ds:si]     ; if name is 8 char long, we still
                cmp     al, ','         ; need to check the comma
                jne     needC
                inc     si
                
HwTypeFound:
                push    si              ; save SI (pos in string)
                mov     si,di           ; DI points to the begining of
                                        ; the hardware name

                mov     di,sHWFontName
                mov     cx,8
           rep  movsb
                                        ; initialize it!
                mov     ax,[cs:si+2]
                call    [cs:si]
                jc      OutStrDX        ; print error string and exit

                pop     si              ; recover si


                pop     di              ; discard 1 element from stack
                                        ; (was: begining of HwName in params)
                jmp     GetHWCodepages  ; goto next stage

needC:          ; error if comma not found
                mov     dx, SES_CommaExpected
                jmp     SyntaxError

                ; 5.- HWCodepage

GetHWCodepages:
                cmp     byte [cs:si],'('
                jne     HW1codepage
                inc     si
                mov     di,wHardCPs
                mov     ch,5
                call    ReadSW
                xor     ch,ch
                mov     [cs:wNumHardCPs],cx
                jmp     CommaBeforeFonts
HW1codepage:
                call    ReadNumber
                mov     [cs:wHardCPs], ax

CommaBeforeFonts:
                mov     di, si
                mov     al,','
                scasb
                jne     needC           ; , expected
                inc     si

                ; check that it does not exceed the maximum number of hw fonts
                test     BYTE [cs:bMaxHWcpNumber],0ffh
                jz       ArraySize
                mov      cx,[cs:wNumHardCPs]
                cmp      cx,[cs:bMaxHWcpNumber]
                jna      ArraySize
                mov      dx, SES_TooManyHWPools
                jmp      SyntaxError

                ; 6.- ArraySize

ArraySize:
%ifdef          DISPLAY
                cmp     byte [cs:si],'('
                jne     NoSubfonts      ; (fonts,subfonts) specified
                inc     si
                mov     di,wNumSoftCPs
                mov     ch,2
                call    ReadSW
                mov     ax,[cs:wNumSoftCPs]
                jmp     numberOk
NoSubfonts:
%endif
                call    ReadNumber      ; number of fonts only
                cmp     ax, 5           ; maximum: 5
                jna     numberOk
                mov     dx, SES_TooManyPools
                jmp     SyntaxError

numberOk:
                test    al,0ffh         ; minimum: 1
                jnz     jmp4b
                inc     al
jmp4b:          mov     [cs:wNumSoftCPs], ax
                                        ; number of subfonts ok?
                mov     ax,[cs:wMinFontSize]
                cmp     ax,[cs:wNumSubFonts]
                jbe     ClosingBracket
                mov     [cs:wNumSubFonts],ax

                ; 7.- No more arguments

ClosingBracket:
                mov     di, si
                mov     al,')'
                scasb
                je      jmp5
                mov     dx, SES_CloseBrExpected
                jmp     SyntaxError
jmp5:           inc     si

loop4b:         lodsb
                cmp     al,' '
                je      loop4b
                cmp     al,13
                je      jmp6
                mov     dx, SES_WrongNumberPars
                jmp     SyntaxError     ; wrong # of params specified

jmp6:           sti                     ; success


                ;****** END SCAN COMMANDLINE (everything OK)

                ;****** Compute the size of the table (from subfonts)

                mov     ax,[cs:wNumSubFonts]
                cmp     ax,1
                ja      Subfonts2
                mov     WORD [wTableSize],128*8
                jmp     SetVectors
Subfonts2:      cmp     ax,2
                ja      Subfonts3
                mov     WORD [wTableSize],128*(8+14)
                jmp     SetVectors
Subfonts3:      mov     WORD [wTableSize],128*(8+14+16)

                ;****** fill the GraftablBuffer

                mov     WORD [cs:dGraftablBuf],bFont8x8
                push    cs
                pop     ax
                mov     [cs:dGraftablBuf+2],ax

                ;****** set our interrupt vectors (DISPLAY ONLY)
SetVectors:

%ifdef          DISPLAY

                mov     ax,352fH        ; Get vector 2f
                int     21H
                mov     [dOld2f],bx
                mov     [dOld2f+2],es

                mov     dx,New2f
                mov     ax,252fH        ; Set vector 2f
                int     21H

                mov     ax,3510H        ; Get vector 10
                int     21H
                mov     [dOld10],bx
                mov     [dOld10+2],es

                mov     dx,New10
                mov     ax,2510H        ; Set vector 10
                int     21H
%endif

                ;****** Compute the resident size

                mov     dx,[cs:wNumSoftCPs]
                mov     ax,[cs:wTableSize]
                shl     ax,1
                mul     dx              ; wTableSize x NumSoftCPs
                add     ax,SCP1         ; + BeginCodepage1
                mov     [cs:pMemTop],ax

                ;****** Free environtment space

EndInstall:
                mov     ax, [cs:02ch]
                mov     es, ax
                mov     ah, 049h
                int     21h

                clc
                retf

; Fn:   WriteNumber
; In:   AX: number of at most 3 cyphers to be written
; Out:  -
; Note: SyntaxErrorStr space is reused (as it is no longer used when
;       this function is called)
;       I know this is not the most optimal code in the world...

WriteNumber:     mov    di,SyntaxErrorStr     ; reuse the space
                 mov    bx,100
                 call   DWriteCypher
div10:           mov    al,ah
                 xor    ah,ah
                 mov    bx,10
                 call   DWriteCypher
                 mov    al,ah
                 call   WriteCypher
                 mov    BYTE [es:di],'$'
                 mov    dx, SyntaxErrorStr
                 jmp    OutStrDX              ; write the str and exit

DWriteCypher:    div    bl

; with these two lines, heading 0 were not printer, but numbers 100-109
; were not correctly printed; I don't think it's worth to make this nicer
;
; -- Aitor
;                 test   al,0FFh
;                 jz     retWC
WriteCypher:     add    al,'0'
                 mov    [es:di],al
                 inc    di
retWC:           ret


; Fn:   SyntaxError
; In:   DX: error string to be displayed
; Out:  -

SyntaxError:     push   dx                 ; save address of string to output
                 mov    dx, SyntaxErrorStr
                 call   OutStrDX

                 mov    ax, si
                 sub    ax, CommandLine_Offset
                 call   WriteNumber

                 pop    dx
                 call   OutStrDX

                 mov    dx, ReturnString
                 call   OutStrDX

                 sti
                 stc
                 jmp    Done

; Fn:   ReadNumber
; In:   ES:SI -> a string number of at most 5 cyphers
; Out:  AX the number read
; Note: it stops whenever it finds the first non-number character or
;       whenever 5 characters are read
;       I know this is not the most optimal code in the world...


ReadNumber:      xor    ax,ax
                 xor    bx,bx
                 mov    cx,5
loop5:           cmp    BYTE [es:si],'0'
                 jb     EndReadNumber
                 cmp    BYTE [es:si],'9'
                 ja     EndReadNumber
                 mov    dx, 10             ; DX annihilated by MUL!
                 mul    dx
                 mov    bl, [es:si]
                 add    ax, bx
                 sub    ax, '0'
                 inc    si
                 loop   loop5
EndReadNumber:   ret



; Fn:   ReadSW
; In:   ES:SI -> list of the form [number,] ... )
;       ES:DI -> array of WORD to store the list
;       CH maximum admissible length
; Out:  CL lenght of the list
; Note: it stops whenever it finds the )
;       on error condition (illegal char, list too long) it aborts
;       automatically


ReadSW:          xor    cl, cl
NextItemListSW:  push   cx
                 call   ReadNumber
                 pop    cx
                 inc    cl
                 stosw
                 lodsb
                 cmp    al, ','
                 jne    NotCommaSW
                 cmp    cl, ch
                 jb     NextItemListSW
                 mov    dx, SES_ListTooLong
                 jmp    SyntaxError
NotCommaSW:      cmp    al, ')'
                 je     ReadSWEnd
                 mov    dx, SES_IllegalChar
                 jmp    SyntaxError
ReadSWEnd:       ret


;===================================================================
; STRINGS, ARRAYS and STRUCTURES
;
;               .       .       .       .       .       line that rules


; Copyright message
%ifdef           DISPLAY
copyVer          DB     "FreeDOS DISPLAY ver. 0.10", 0dH, 0aH, "$"
%endif
%ifdef           PRINTER
copyVer          DB     "FreeDOS PRINTER ver. 0.10", 0dH, 0aH, "$"
%endif
ReturnString     DB      0dH, 0aH, "$"

;               LOCALISED Strings

%include        "strings.en"

                 END