-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock.asm
More file actions
1344 lines (1187 loc) · 25.5 KB
/
clock.asm
File metadata and controls
1344 lines (1187 loc) · 25.5 KB
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
PAGE 59,132
;
; clock.asm - a DOS clock driver for Intel Above Board/PS multi-function card
;
; Copyright (c) 1985 Intel Corporation
;
; Disassembled using Ghidra
;
; Build with MASM 4.0 or 5.10:
; masm clock,,,;
; link clock,,,;
; exe2bin clock.exe clock.sys
;
IFDEF TWEAK2
; moving most of this into a proc saves a lot of bytes
write_char MACRO arg1
push ax
IFNB <arg1>
mov al,arg1
ENDIF
call print_char ; print char in al
pop ax
ENDM
ELSE
write_char MACRO arg1
push ax
push dx
;----------+-------------+-------------
; | TWEAK | TWEAK
; | disabled | enabled
;----------+-------------+-------------
; no arg1 | mov dl,al | mov dl,al
; | |
;----------+-------------+-------------
; arg1 | mov dl,al |
; provided | mov dl,arg1 | mov dl,arg1
;----------+-------------+-------------
IFB <arg1>
mov dl,al
ELSE
IFNDEF TWEAK
mov dl,al
ENDIF
mov dl,arg1
ENDIF
mov ah,2
int 21h ; int 21h, ah = 2 - write character from dl to standard output
pop dx
pop ax
ENDM
ENDIF ; ENDIFDEF TWEAK2
;
; Write string to console
;
write_str MACRO pstr, no_save_si
IFB <no_save_si>
push si
ENDIF
IFDEF TWEAK2
mov si,offset pstr
ELSE
lea si,pstr
ENDIF
call print_str
IFB <no_save_si>
pop si
ENDIF
ENDM
nopc MACRO
IFNDEF TWEAK
nop
ENDIF
ENDM
code segment
assume cs:code,ds:code,es:code,ss:code
org 0
; DOS driver header
; next driver offset and segment, for a single driver sys file it should be 0FFFFh
ndrv_ofs dw -1
ndrv_seg dw -1
; DOS driver attributes:
CURRENT_STDOUT equ 0000000000000001b ; current standard output device
CURRENT_STDIN equ 0000000000000010b ; current standard input device
CURRENT_CLOCK equ 0000000000000100b ; current clock device
CURRENT_NUL equ 0000000000001000b ; current NUL device
; bits 4-10 are reserved, should be 0
MEDIA_CHANGE equ 0000100000000000b ; media change recognized (DOS 3.0+?)
; bit 12 is reserved, should be 0
OUTPUT_UNTIL equ 0010000000000000b ; "output until instructed" for character device driver
NON_IBM equ 0010000000000000b ; "non-IBM format" for block device driver
IOCTL equ 0100000000000000b ; IOCTL support
CHAR_DRV equ 1000000000000000b ; 1 - characted device driver, 0 - block device driver
drv_attr dw CURRENT_NUL + CHAR_DRV
; offset to strategy routine
dw offset strategy
; offset to interrupt routine
dw offset interrupt
; driver name for character device driver or number of devices supported for block device driver
driver_name db 8 dup (0)
; end of DOS driver header
; misc constants
RTC_OK equ 0FFh
RTC_BAD equ 0
NIBBLE equ 0Fh ; 4 lower bits in a byte
RTC_SEC1 equ 2C0h
RTC_SEC10 equ 2C1h
RTC_MIN1 equ 2C2h
RTC_MIN10 equ 2C3h
RTC_HOUR1 equ 2C4h
RTC_HOUR10 equ 2C5h
RTC_DAY1 equ 2C6h
RTC_DAY10 equ 2C7h
RTC_MONTH1 equ 2C8h
RTC_MONTH10 equ 2C9h
RTC_YEAR1 equ 2CAh
RTC_YEAR10 equ 2CBh
RTC_WEEKDAY equ 2CCh
RTC_CONTROL equ 2CDh
; Strategy routine - DOS calls it to initialize the driver and then repeatedly before
; each subsequent I/O request from the driver interrupt routine.
; DOS passes the address to a data structure in ES:BX. It describes the requested operation
; for driver to perform. The address is stores and control is returned to DOS immediately.
; DOS then calls the interrupt routine of the driver to execute the operation.
strategy proc far
mov word ptr cs:[request],bx ; store offset and
mov word ptr cs:[request + 2],es ; segment of the pointer to request header
ret
request dd 0 ; pointer to request header provided by DOS
; The request data structure passed by DOS to strategy routine is at least 13 bytes
; and tells the driver what it should do. Some operations can use additional data past
; 13 bytes. Here is the data structure format:
; +00h - data block length in bytes (word)
; +01h - device number in commnunication (word)
; +02h - command code (word)
; +05h - reserved (qword)
; +0Dh - media descriptor (byte)
; +0Eh - buffer offset address (word)
; +10h - buffer segment address (word)
; +12h - number (word)
; +14h - starting sector (qword)
strategy endp
; Interrupt routine - DOS calls it immediately after calling the strategy routine.
; It first pushes to stack all the registers that may get modified by the driver routines.
; Next it gets the command code from the data block + 2 and then calls required
; command routine. After the command routine returns, interrupt routine sets
; status field in request header and pops register values from the stack.
; It then returns control to DOS.
interrupt proc far
; clear interrupt flag (no interrupts until sti)
cli
mov word ptr cs:[store_ss],ss
mov word ptr cs:[store_sp],sp
mov ax,cs
; set ss to driver code segment
mov ss,ax
IFDEF TWEAK2
mov sp,offset stack_bottom
ELSE
lea sp,[stack_bottom]
ENDIF
sti
; push flags
pushf
; this is wonky, as we just messed with ax above to copy cs to ss
push ax
push bx
push cx
push dx
push si
push di
push bp
push ds
push es
IFDEF TWEAK2
push cs ; (-2 bytes to use stack)
; set ds to driver code segment
pop ds
ELSE
mov ax,cs
; set ds to driver code segment
mov ds,ax
ENDIF
les bx,dword ptr [request]
; read command code from DOS request header
mov al,byte ptr es:[bx + 02h]
cmp al,15
jbe no_bump_al ; jump if al is lower or equal to 15 (0Fh)
mov al,10h ; if al > 15 (0Fh), set it to 16 (10h)
IFDEF TWEAK2
no_bump_al: cbw ; ah = 0 (-1 byte)
ELSE
no_bump_al: xor ah,ah ; ah = 0
ENDIF
shl ax,1 ; convert routine number to offset: ax = ax * 2
IFDEF TWEAK2
xchg ax,si ; si = ax (-1 byte)
ELSE
mov si,ax
ENDIF
call word ptr [si + jump_table]
pop es
pop ds
pop bp
pop di
pop si
pop dx
pop cx
pop bx
pop ax
popf
cli
mov ss,word ptr cs:[store_ss]
mov sp,word ptr cs:[store_sp]
sti
ret
interrupt endp
; jump table for DOS driver functions
jump_table dw init
dw dummy
dw dummy
dw dummy
dw read
dw dummy
dw dummy
dw dummy
dw write
dw write
dw dummy
dw dummy
dw dummy
dw dummy
dw dummy
dw dummy
dw dummy
; jump table end
; this should be at 94h offset
read proc near
les bx,dword ptr [request]
call rtc_get_time
les bx,dword ptr es:[bx + 0Eh] ; request header +0Eh - buffer offset address
mov al,[second]
mov byte ptr es:[bx + 05h],al ; request header +05h - reserved?
mov al,[minute]
mov byte ptr es:[bx + 02h],al ; request header +02h - command code
mov al,[hour]
mov byte ptr es:[bx + 03h],al ; request header +03h - status
mov al,0
mov byte ptr es:[bx + 04h],al ; request header +04h - reserved?
call rtc_get_date
les bx,dword ptr [request]
cmp word ptr es:[bx + 03h],100h
jnz l0cc
call set_days_total
l0cc: les bx,dword ptr [request]
les bx,dword ptr es:[bx + 0Eh] ; request header +0Eh - buffer offset address
mov dx,word ptr [days_total]
dec dx
mov word ptr es:[bx],dx
ret
read endp
; this should be at 0DDh offset
; clobbers bx
rtc_detect proc near
IFDEF TWEAK2
mov bx,1 ; save bytes by using a countdown loop
ELSE
xor bl,bl
ENDIF
l0df:
IFDEF TWEAK
call rtc_init
ELSE
mov dx,RTC_CONTROL ;\
in al,dx ; \ same as call rtc_init
and al,0Eh ; /
out dx,al ;/
ENDIF
; I/O delay
nop
nop
nop
in al,dx
or al,1 ; set HOLD flag
out dx,al
IFNDEF TWEAK2
inc bl
ENDIF
mov cx,100
l0f2: in al,dx
test al,2 ; BUSY flag?
jz rtc_found
loop l0f2
IFDEF TWEAK2
dec bx
ELSE
cmp bl,2
ENDIF
jnz l0df
IFNDEF TWEAK
jmp near ptr rtc_not_found ; unnecessary, can be commented out
rtc_not_found:
ENDIF
mov byte ptr [rtc_status],RTC_BAD
nopc
ret
rtc_found: mov byte ptr [rtc_status],RTC_OK
nopc
ret
rtc_detect endp
; this should be at 10Fh offset
rtc_init proc near
mov dx,RTC_CONTROL
in al,dx
and al,0Eh
out dx,al
ret
rtc_init endp
; this should be at 117h offset
rtc_get_time proc near
call rtc_detect
mov dx,RTC_SEC1 ; 2C0h
in al,dx
and al,NIBBLE
cmp al,9
IFDEF TWEAK2
ja bad_time ; bad time if CF=0 and ZF=0
ELSE
jbe sec1
jmp near ptr bad_time
ENDIF
; set lower digit of second, for example for 47 seconds it will be 7
sec1: mov [second],al
IFDEF TWEAK2
; these port numbers are sequential, so -2 bytes to just inc instead
inc dx ; 2C1h RTC_SEC10
ELSE
mov dx,RTC_SEC10 ; 2C1h
ENDIF
in al,dx
and al,NIBBLE
cmp al,5
IFDEF TWEAK2
ja bad_time
ELSE
jbe sec10
jmp near ptr bad_time
ENDIF
; set higher digit of second, for example for 47 seconds, al is 4
sec10: mul byte ptr [ten]
add byte ptr [second],al
; now al is seconds from 0 to 59
IFDEF TWEAK2
inc dx ; 2C2h RTC_MIN1
ELSE
mov dx,RTC_MIN1 ; 2C2h
ENDIF
in al,dx
and al,NIBBLE
cmp al,9
IFDEF TWEAK2
ja bad_time
ELSE
jbe min1
jmp near ptr bad_time
ENDIF
min1: mov [minute],al
IFDEF TWEAK2
inc dx ; 2C3h RTC_MIN10
ELSE
mov dx,RTC_MIN10 ; 2C3h
ENDIF
in al,dx
and al,NIBBLE
cmp al,5
IFDEF TWEAK2
ja bad_time
ELSE
jbe min10
jmp near ptr bad_time
ENDIF
min10: mul byte ptr [ten]
add byte ptr [minute],al
; now al is minutes from 0 to 59
IFDEF TWEAK2
inc dx ; 2C4h RTC_HOUR1
ELSE
mov dx,RTC_HOUR1 ; 2C4h
ENDIF
in al,dx
and al,NIBBLE
cmp al,9
IFDEF TWEAK2
ja bad_time
ELSE
jbe hour1
jmp near ptr bad_time
ENDIF
hour1: mov [hour],al
IFDEF TWEAK2
inc dx ; 2C5h RTC_HOUR10
ELSE
mov dx,RTC_HOUR10 ; 2C5h
ENDIF
in al,dx
and al,NIBBLE
cmp al,2
IFDEF TWEAK2
ja bad_time
ELSE
jbe hour10
jmp near ptr bad_time
ENDIF
hour10: mul byte ptr [ten]
add byte ptr [hour],al
mov ah,byte ptr [hour]
IFDEF TWEAK
; bugfix, hour should be between 0 and 23
; credit to Krille @ vcfed forum
cmp ah,23
ELSE
; original Intel driver allows 24 as a valid hour
cmp ah,24
ENDIF
ja bad_time
; now al is hour from 0 to 23
call rtc_init
les bx,dword ptr [request]
mov word ptr es:[bx + 03h],0100h ; request header +03h - status
ret
bad_time: call rtc_init
les bx,dword ptr [request]
mov word ptr es:[bx + 03h],810Ch ; request header +03h - status
xor ax,ax
IFDEF TWEAK2
mov word ptr [second],ax ; -1 byte
ELSE
mov [second],al
mov [minute],al
ENDIF
mov [hour],al
ret
rtc_get_time endp
; this should be at 1B9h offset
rtc_get_date proc near
call rtc_detect
mov dx,RTC_DAY1
in al,dx
and al,NIBBLE
cmp al,9
IFDEF TWEAK2
ja bad_date
ELSE
jbe day1
jmp near ptr bad_date
ENDIF
day1: mov [day],al
mov dx,RTC_DAY10
in al,dx
and al,NIBBLE
cmp al,3
IFDEF TWEAK2
ja bad_date
ELSE
jbe day10
jmp near ptr bad_date
ENDIF
day10: mul byte ptr [ten]
add byte ptr [day],al
; now al is day from 1 to 31
mov dx,RTC_MONTH1
in al,dx
and al,NIBBLE
cmp al,9
IFDEF TWEAK2
ja bad_date
ELSE
jbe month1
jmp near ptr bad_date
ENDIF
month1: mov [month],al
mov dx,RTC_MONTH10
in al,dx
and al,NIBBLE
cmp al,1
IFDEF TWEAK2
ja bad_date
ELSE
jbe month10
jmp near ptr bad_date
ENDIF
month10: mul byte ptr [ten]
add byte ptr [month],al
mov al,[month]
cmp al,12
ja bad_date
; now al is month from 1 to 12
call month_to_days
mov ax,[days_in_month]
; check if current day is less or equal to the number of days in the current month
; i.e. detect things like February 30 or April 31 as bad
cmp al,byte ptr [day]
jc bad_date
mov dx,RTC_YEAR1
in al,dx
and al,NIBBLE
cmp al,9
ja bad_date
mov [year],al
mov dx,RTC_YEAR10
in al,dx
and al,NIBBLE
cmp al,9
ja bad_date
mul byte ptr [ten]
add byte ptr [year],al
; now al is year from 0 to 99
call rtc_init
les bx,dword ptr [request]
mov word ptr es:[bx + 03h],100h ; request header +03h - status
ret
bad_date: call rtc_init
les bx,dword ptr [request]
mov word ptr es:[bx + 03h],810Ch ; request header +03h - status
xor ax,ax
inc ax
mov [days_total],ax ; if the RTC date is invalid, set days_total to 1
ret
rtc_get_date endp
; this should be at 25Ah offset
set_days_total proc near
call year_to_days
; take days from previous years
mov [days_total],ax
call pm_to_days
; add days from previous months this year
add word ptr [days_total],ax
xor ax,ax
; add day of the current month
mov al,[day]
add word ptr [days_total],ax
ret
set_days_total endp
; this should be at 271h offset
; convert past months this year to days
pm_to_days proc near
test byte ptr [year],3
jnz not_leap_year
mov bx,offset table_leap
jmp short lookup
nopc
not_leap_year: mov bx,offset table_non_leap
lookup:
IFDEF TWEAK2
mov al,month
cbw
ELSE
xor ax,ax
mov al,[month]
ENDIF
shl ax,1 ; ax = ax * 2
IFDEF TWEAK2
xchg ax,si
ELSE
mov si,ax ; an offset to word?
ENDIF
mov ax,word ptr [bx + si]
ret
pm_to_days endp
; this should be at 28Dh offset
month_to_days proc near
push ax
IFDEF TWEAK2
mov al,month
cbw
ELSE
xor ax,ax
mov al,[month]
ENDIF
mov bx,offset table_days_in_month ; it should be 4B4h
; al = days in current month, no leap year correction for february
xlat
; if year & 3 <> 0, consider it leap - this is probably limited to 1980s
; TODO: check relevant leap years: 1984, 1988, 1992
;
; Leap year occurs in each year that is an integer multiple of 4,
; except for years evenly divisible by 100, but not by 400
; the year is a leap year and has 366 days
; otherwise it is not a leap year and has 365 days
test byte ptr [year],3
jnz not_leap_year2
mov cl,2
; check if month = 2 (february)
cmp byte ptr [month],cl
jnz not_leap_year2
; add one day if it is february and it's a leap year: 28 + 1 = 29
IFDEF TWEAK2
inc ax
ELSE
inc al
ENDIF
not_leap_year2: mov [days_in_month],ax
pop ax
ret
month_to_days endp
; this should be at 2ADh offset
year_to_days proc near
IFDEF TWEAK2
mov al,[year]
cbw
ELSE
xor ax,ax
mov al,[year]
ENDIF
mul word ptr [days_per_year]
xor cx,cx
IFDEF TWEAK2
add cl,[year]
ELSE
mov cl,[year]
cmp byte ptr [year],0
ENDIF
jz year0
dec cl
shr cx,1
shr cx,1 ; cx = cx / 4
inc cx
add ax,cx ; add a day every four years
year0: ret
year_to_days endp
; this should be at 2CDh offset
write proc near
les bx,request
les bx,dword ptr es:[bx + 0Eh] ; request header +0Eh - buffer offset address
mov ax,word ptr es:[bx]
inc ax
cmp ax,08EADh
jc l2ef
cmp ax,0AB35h
jbe l2e6
jmp near ptr l3ab
l2e6: mov ax,08EADh
mov [days_total],ax
IFDEF TWEAK2
jmp short l30e
ELSE
jmp near ptr l30e
ENDIF
l2ef: mov [days_total],ax
cmp word ptr [days_total],0
jnz l30e
mov byte ptr [year],0
nopc
mov byte ptr [month],1
nopc
mov byte ptr [day],1
nopc
IFDEF TWEAK2
jmp short l311
ELSE
jmp near ptr l311
ENDIF
l30e: call calc_day
l311: call rtc_detect
IFDEF TWEAK2
mov al,[day]
cbw
ELSE
xor ax,ax
mov al,[day]
ENDIF
div byte ptr [ten]
mov dx,RTC_DAY10
out dx,al
mov dx,RTC_DAY1
xchg ah,al
out dx,al
xor ax,ax
mov al,[month]
div byte ptr [ten]
mov dx,RTC_MONTH10
out dx,al
mov dx,RTC_MONTH1
xchg ah,al
out dx,al
IFDEF TWEAK2
mov al,[year]
cbw
ELSE
xor ax,ax
mov al,[year]
ENDIF
div byte ptr [ten]
mov dx,RTC_YEAR10
out dx,al
mov dx,RTC_YEAR1
xchg ah,al
out dx,al
les bx,dword ptr [request]
les bx,dword ptr es:[bx + 0Eh] ; request header +0Eh - buffer offset address
xor ax,ax
mov al,byte ptr es:[bx + 03h] ; request header +03h - status
cmp al,24
jnc l3ab
div byte ptr [ten]
mov dx,RTC_HOUR10
out dx,al
mov dx,RTC_HOUR1
xchg ah,al
out dx,al
xor ax,ax
mov al,byte ptr es:[bx + 02h] ; request header +02h - command code
cmp al,3Ch
jnc l3ab
div byte ptr [ten]
mov dx,RTC_MIN10
out dx,al
mov dx,RTC_MIN1
xchg ah,al
out dx,al
xor ax,ax
mov al,byte ptr es:[bx + 05h] ; request header +05h - reserved?
cmp al,3Ch
jnc l3ab
div byte ptr [ten]
mov dx,RTC_SEC10
out dx,al
mov dx,RTC_SEC1
xchg ah,al
out dx,al
call rtc_init
les bx,dword ptr [request]
mov word ptr es:[bx + 03h],100h
ret
l3ab: call rtc_init
les bx,dword ptr [request]
mov word ptr es:[bx + 03h],810ch
ret
write endp
; this should be at 3B9h offset
calc_day proc near
xor dx,dx
mov ax,[days_total]
div word ptr [days_per_year]
mov [year],al
call year_to_days
cmp ax,word ptr [days_total]
IFDEF TWEAK2
jc l3d8
ELSE
jnc l3d1
jmp near ptr l3d8
ENDIF
l3d1: dec byte ptr [year]
call year_to_days
l3d8: mov cx,word ptr [days_total]
sub cx,ax
mov word ptr [days_total],cx
call calc_month
mov ax,[days_total]
mov [day],al
ret
calc_day endp
; this should be at 3ECh offset
calc_month proc near
mov ax,[days_total]
xor si,si
test byte ptr [year],3
jnz not_leap_year3
mov bx,offset table_leap
jmp short leap_year3
nopc
not_leap_year3: mov bx,offset table_non_leap
leap_year3: cmp ax,word ptr [bx + si + 2] ; check if it is before leap day?
jle before_leap_d
add si,2
jmp short leap_year3
before_leap_d: mov ax,word ptr [bx + si]
sub word ptr [days_total],ax
shr si,1 ; si = si / 2
mov ax,si
mov [month],al
ret
calc_month endp
; this should be at 419h offset
dummy proc near
; setup status in request header before returing to DOS
mov word ptr es:[bx + 3],8103h
ret
dummy endp
; this should be at 420h offset
second db 0 ; 0 to 59
minute db 0 ; 0 to 59
hour db 0 ; 0 to 23
day db 0 ; 1 to 31
month db 0 ; 1 to 12
year db 0
; this should be at 426h offset
rtc_status db RTC_BAD
; this should be at 427h offset
days_total dw 0
days_in_month dw 0
dw 0
; this should be at 42Dh offset
ten db 10
days_per_year dw 365
; this should be at 430h offset
store_ss dw 0
store_sp dw 0
; reserve some space for stack
db 128 dup (5Eh)
stack_bottom: ; this should be at 4B4h offset
table_days_in_month:
db 0 ; 0 - no such month, unused
db 31 ; 1 - january has 31 days
db 28 ; 2 - february has 28 days (TODO: what about 29 on leap year?)
db 31 ; 3 - march has 31 days
db 30 ; 4 - april has 30 days
db 31 ; 5 - may has 31 days
db 30 ; 6 - june has 30 days
db 31 ; 7 - july has 31 days
db 31 ; 8 - august has 31 days
db 30 ; 9 - september has 30 days
db 31 ; 10 - october has 31 days
db 30 ; 11 - november has 30 days
db 31 ; 12 - december has 31 days
; this should be at 4C1h offset
table_non_leap:
dw 0 ; 0 - no such month, unused
dw 0 ; 1 - days in year before january
dw 31 ; 2 - days in year before february
dw 59 ; 3 - days in year before match
dw 90 ; 4 - days in year before april
dw 120 ; 5 - days in year before may
dw 151 ; 6 - days in year before june
dw 181 ; 7 - days in year before july
dw 212 ; 8 - days in year before august
dw 243 ; 9 - days in year before september
dw 273 ; 10 - days in year before october
dw 304 ; 11 - days in year before november
dw 334 ; 12 - days in year before december
dw 365 ; 13 - total days in year, probably this is never unused
; this should be at 4DDh offset
table_leap:
dw 0 ; 0 - no such month, unused
dw 0 ; 1 - days in leap year before january
dw 31 ; 2 - days in leap year before february
dw 60 ; 3 - days in leap year before match
dw 91 ; 4 - days in leap year before april
dw 121 ; 5 - days in leap year before may
dw 152 ; 6 - days in leap year before june
dw 182 ; 7 - days in leap year before july
dw 213 ; 8 - days in leap year before august
dw 244 ; 9 - days in leap year before september
dw 274 ; 10 - days in leap year before october
dw 305 ; 11 - days in leap year before november
dw 335 ; 12 - days in leap year before december
dw 366 ; 13 - total days in leap year, probably this is never unused
db 7 dup (0)
; this should be at 500h
init proc near ; initialization routine
cli
mov ax,cs
mov ds,ax
mov es,ax
mov dx,2CFh
mov al,1
out dx,al
mov al,5
out dx,al
mov al,4
out dx,al
mov dx,RTC_CONTROL
mov al,0
out dx,al
mov dx,2CEh
mov al,3
out dx,al
write_str banner
call rtc_detect
call rtc_init
cmp byte ptr [rtc_status],RTC_OK
jz l538
jmp l6d1
l538: mov dx,RTC_SEC1
in al,dx
and al,NIBBLE
add al,1
daa ; adjusts the sum of two packed BCD values to create a packed BCD result
and al,NIBBLE
mov [second],al
mov cx,0FFFFh
l549: push cx
mov cx,12
l54d: loop l54d ; delay loop? skip something?