Repository navigation
Expand file tree
/
Copy pathclkdev.asm
More file actions
877 lines (825 loc) · 19.9 KB
/
Copy pathclkdev.asm
File metadata and controls
877 lines (825 loc) · 19.9 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
;
; BASIC-DOS Physical Clock Device Driver
;
; @author Jeff Parsons <Jeff@pcjs.org>
; @copyright (c) 2020-2026 Jeff Parsons
; @license MIT <https://basicdos.com/LICENSE.txt>
;
; This file is part of PCjs, a computer emulation software project at pcjs.org
;
BIOSAPI equ 1
CLKTIMER equ 1
DOSFUNC equ 1
include macros.inc
include bios.inc
include dev.inc
include devapi.inc
include dosapi.inc
DEV group CODE,DATA
CODE segment para public 'CODE'
public CLOCK
CLOCK DDH <offset DEV:ddclk_end+16,,DDATTR_CLOCK+DDATTR_CHAR+DDATTR_IOCTL,offset ddclk_init,-1,2020244B434F4C43h>
dw offset DEV:ddclk_timer ; CLKX (see dev.inc)
DEFLBL CMDTBL,word
dw ddclk_none, ddclk_none, ddclk_none, ddclk_ctlin ; 0-3
dw ddclk_read, ddclk_none, ddclk_none, ddclk_none ; 4-7
dw ddclk_write, ddclk_none, ddclk_none, ddclk_none ; 8-11
dw ddclk_ctlout, ddclk_none, ddclk_none ; 12-14
DEFABS CMDTBL_SIZE,<($ - CMDTBL) SHR 1>
DEFPTR tmr_int,0 ; timer hardware interrupt handler
DEFPTR wait_ptr,0FFFFh,0 ; chain of waiting packets (none)
DEFPTR tmr_list,0FFFFh,0 ; chain of running timers (none)
DEFBYTE dateDay,10 ; 1-31 (default for PC and XT)
DEFBYTE dateMonth,12 ; 1-12
DEFWORD dateYear,2020 ; 1980-
DEFLONG ticksToday,0 ; ticks since midnight (from BIOS)
DEFWORD sndTicks,0 ; ticks until the speaker is turned off
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Driver request
;
; Inputs:
; ES:BX -> DDP
;
; Outputs:
;
ASSUME CS:CODE, DS:NOTHING, ES:NOTHING, SS:NOTHING
DEFPROC ddclk_req,far
mov di,bx ; ES:DI -> DDP
mov bl,es:[di].DDP_CMD
cmp bl,CMDTBL_SIZE
jb ddq1
mov bl,0
ddq1: mov ds,cs:[0].DDH_DATA
ASSUME DS:CODE
mov bh,0
add bx,bx
call CMDTBL[bx]
ret
ENDPROC ddclk_req
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ddclk_ctlin
;
; Inputs:
; ES:DI -> DDPRW
;
; Outputs:
; Varies
;
; Modifies:
; AX, CX, DX
;
ASSUME CS:CODE, DS:CODE, ES:NOTHING, SS:NOTHING
DEFPROC ddclk_ctlin
mov al,es:[di].DDP_CODE ; AL = IOCTL code
mov cx,es:[di].DDPRW_LENGTH ; CX = IOCTL input value
mov dx,es:[di].DDPRW_LBA ; DX = IOCTL input value
mov es:[di].DDP_STATUS,DDSTAT_ERROR + DDERR_UNKCMD
cmp al,IOCTL_SOUND
jne dci1
;
; For SOUND requests, DX = PIT divisor and CX = # ticks; we turn off any
; current sound and then, if DX and CX are non-zero, start the new sound and
; let ddclk_interrupt turn it off after CX ticks.
;
cli
mov [sndTicks],0
in al,61h
and al,0FCh
out 61h,al ; turn the speaker off
test dx,dx
jz dci0
jcxz dci0
mov al,0B6h ; PIT channel 2, mode 3, LSB then MSB
out 43h,al
mov al,dl
out 42h,al
mov al,dh
out 42h,al
in al,61h
or al,03h
out 61h,al ; turn the speaker on
mov [sndTicks],cx
dci0: sti
jmp dci8
dci1: cmp al,IOCTL_WAIT
jne dci2
;
; For WAIT requests, convert # ms in CX:DX (1000/second to # ticks (18.2/sec).
;
; 1 tick is equivalent to approximately 55ms, so that's the granularity of
; WAIT requests as well as all clock-driven context switches; context switches
; can also be triggered by other hardware interrupts, like keyboard interrupts,
; so this is not the full measure of context-switching granularity.
;
add dx,27 ; add 1/2 tick (as # ms) for rounding
adc cx,0
mov bx,55 ; BX = divisor
xchg ax,cx ; AX = high dividend
mov cx,dx ; CX = low dividend
sub dx,dx
div bx ; AX = high quotient
xchg ax,cx ; AX = low dividend, CX = high quotient
div bx ; AX = low quotient
mov es:[di].DDPRW_LBA,ax ; CX:AX = # ticks
mov es:[di].DDPRW_LENGTH,cx
;
; Now add this packet to an internal chain of "waiting" packets, and tell
; DOS that we're waiting; DOS will suspend the current SCB until we notify
; it that this packet's conditions are satisfied.
;
cli
mov ax,di
xchg [wait_ptr].OFF,ax
mov es:[di].DDP_PTR.OFF,ax
mov ax,es
xchg [wait_ptr].SEG,ax
mov es:[di].DDP_PTR.SEG,ax
sti
;
; The WAIT condition is satisfied when the packet's LENGTH:OFFSET pair (which
; should contain a standard CX:DX tick count) has been decremented to zero.
;
mov dx,es ; DX:DI -> packet (aka "wait ID")
mov ah,DOS_UTL_WAIT
call far ptr DDINT_UTIL ; wait on DX:DI (see int_util)
jnc dci1x
;
; The wait was interrupted (eg, by ABORT or CTRLC), so the packet must be
; removed from the chain; if it's no longer there, then the wait ended
; normally after all, so treat that as success.
;
push ax
call remove_packet ; carry clear if packet removed
pop ax
jc dci1x
mov es:[di].DDP_STATUS,DDSTAT_ERROR + DDERR_GENFAIL
ret
dci1x: jmp dci8
dci2: cmp al,IOCTL_SETDATE
jne dci3
;
; For SETDATE requests, CL = year (0-127), DH = month (1-12), DL = day (1-31).
;
cli
mov ch,0
add cx,1980
mov [dateYear],cx
mov word ptr [dateDay],dx
sti
jmp dci8
dci3: cmp al,IOCTL_SETTIME
je dci3a
jmp dci4
;
; For SETTIME requests, we convert the hours (CH), minutes (CL), seconds
; (DH), and hundredths (DL) to a number of ticks. We start by converting
; time to the total number of seconds (hundredths are dealt with later).
;
dci3a: push dx ; save DX
mov ax,3600 ; AX = # seconds in 1 hour
mov dl,ch
mov dh,0
mul dx ; DX:AX = # seconds in CH hours
xchg bx,ax ; DX:BX
mov al,60
mul cl ; AX = # seconds in CL minutes
add bx,ax
adc dx,0 ; DX:BX = # seconds in hours and mins
pop ax ; AH = seconds (from DX)
push ax
mov al,ah ; AL = seconds
cbw ; AX = seconds
add bx,ax
adc dx,0 ; DX:BX += seconds
;
; Now we'll convert total # seconds to total # ticks, by noting that
; there are 1193180/65536 ticks/sec, so 1193180/65536 * seconds = ticks.
; Seconds has an upper limit of 86400 for the day, so ticks has an upper
; limit of 1573040. Using the ratio 1573040/86400 = ticks/seconds, solve
; for ticks: 1573040 * seconds / 86400, or 19663 * seconds / 1080.
;
shr dx,1 ; DX:BX / 2 yields a max 43200
rcr bx,1 ; BX = seconds / 2
pushf ; save remainder, if any, for later
mov ax,19663
mul bx ; DX:AX = BX * 19663
mov bx,540 ; divisor is 1080 / 2
;
; Divide DX:AX by BX in stages, so that we don't risk division overflow.
;
xchg cx,ax ; save low dividend
xchg ax,dx ; divide high dividend
sub dx,dx ; DX:AX is new dividend
div bx ; AX is high quotient
xchg ax,cx ; move to CX, restore low dividend
div bx ; AX is low quotient
IFDEF MAXDEBUG
DPRINTF <"initial number of ticks: %lu\r\n">,ax,cx
DPRINTF <"with remainder of %d after division by %u\r\n">,dx,bx
ENDIF
;
; CX:AX = quotient (# ticks). DX/540 is a fractional tick, which we convert
; to hundredths and add to the hundredths adjustment below.
;
push ax
xchg ax,dx
mov dl,100
div dl
xchg bx,ax ; BL = additional hundredths
mov dx,cx
pop cx ; DX:CX = tick count
IFDEF MAXDEBUG
DPRINTF <"hundredths for fractional tick: %bu\r\n">,bx
ENDIF
popf ; did seconds / 2 produce a remainder?
jnc dci3b ; no
add bl,100 ; yes, add another 100 hundredths
IFDEF MAXDEBUG
DPRINTF <"hundredths for additional second: %u\r\n">,bx
ENDIF
dci3b: pop ax ; AL = hundredths (from original DX)
add al,bl ; add any hundredths from above
IFDEF MAXDEBUG
DPRINTF <"total hundredths: %bu\r\n">,ax
ENDIF
mov ah,18
mul ah ; AX = hundredths * 18
mov bl,100
div bl ; AL = (hundredths * 18) / 100
cbw ; AX = # ticks for hundredths
add cx,ax
adc dx,0 ; DX:CX += ticks for hundredths
IFDEF MAXDEBUG
DPRINTF <"new tick count: %lu\r\n">,cx,dx
ENDIF
cli
mov [ticksToday].LOW,cx
mov [ticksToday].HIW,dx
sti
jmp dci8
dci4: cmp al,IOCTL_GETDATE
jne dci5
;
; For GETDATE requests, return the date in "packed" format:
;
; Y Y Y Y Y Y Y M M M M D D D D D
; 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
;
; where Y = year-1980 (0-127), M = month (1-12), and D = day (1-31).
;
cli
mov dx,[dateYear]
sub dx,1980
mov cl,4 ; make room for month (4 bits)
shl dx,cl
or dl,[dateMonth]
inc cx
shl dx,cl ; make room for day (5 bits)
or dl,[dateDay]
sti
jmp dci8
dci4x: jmp dci9
dci5: cmp al,IOCTL_GETTIME
jne dci4x
;
; For GETTIME requests, return the time in "packed" format:
;
; H H H H H M M M M M M S S S S S
; 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
;
; where H = hours (1-12), M = minutes (0-59), and S = seconds / 2 (0-29)
;
; However, we must first convert # ticks to hours/minutes/seconds. There
; are approximately 32771 ticks per half-hour, 1092 ticks per minute, and 18
; ticks per sec.
;
cli
mov ax,[ticksToday].LOW
mov dx,[ticksToday].HIW
sti
IFDEF MAXDEBUG
DPRINTF <"current tick count: %lu\r\n">,ax,dx
ENDIF
;
; TODO: the true divisor is 32771.66748046875, so the remainder may be too
; large; this appears to be good enough for now, but deal with it eventually.
;
mov bx,32772
div bx ; AX = # half-hours
shr ax,1 ; AX = # hours
xchg cx,ax ; CL = # hours
xchg ax,dx ; AX = # half-hour ticks remaining
mov dx,0 ; DX:AX
jnc dci5a
add ax,32772 ; DX:AX = # ticks remaining
adc dx,0
dci5a: mov bx,1092
div bx ; AX = # minutes
;
; The true divisor is 1092.388916015625, so the remainder may be too large;
; multiply AX (# of minutes) by 3889 and divide by 10000 to yield the number
; of ticks to reduce the remainder (DX) by.
;
push ax
push dx
mov bx,3889
mul bx
mov bx,10000
div bx
pop dx
sub dx,ax
jae dci5b
sub dx,dx
dci5b: pop ax
mov ch,al ; CH = # minutes
xchg ax,dx ; AX = # ticks remaining
mov bx,10
mul bx ; DX:AX = # ticks * 10
mov bx,182
div bx ; AX = # seconds
push dx ; DX = # ticks * 10 remaining (< 182)
;
; Sanitize the time values now, ensuring we never return values out of bounds.
;
cmp al,60 ; AL = # seconds
jb dci6
mov al,0
inc ch
dci6: cmp ch,60 ; CH = # minutes
jb dci6b
mov ch,0
inc cx
dci6b: cmp cl,24 ; CL = # hours
jb dci7
mov cl,0
;
; Create the "packed" time format now.
;
dci7: mov dl,cl ; start with hours (5 bits)
mov cl,6 ; make room for minutes (6 bits)
shl dx,cl
or dl,ch ; add the minutes
dec cx ; make room for seconds / 2 (5 bits)
shl dx,cl
pop cx ; CX = # ticks remaining (from above)
shr al,1 ; AL = seconds / 2
jnc dci7a
add cx,182
dci7a: or dl,al
xchg ax,cx ; AX = # ticks remaining (< 364)
;
; At this point, the packed result is ready, but we'd also like to convert
; the remaining ticks in AX to hundredths (< 200). AX/364 = N/200. We return
; up to 200 hundredths to compensate for the "packed" time containing only
; the nearest EVEN second.
;
push dx
mov cx,200
mul cx ; DX:AX = AX * 200
mov cx,364
div cx ; AX = (AX * 200) / 364
pop dx
;
; dev_request returns AX = the # bytes "transferred" (the original length less
; the remaining length), so that's how we return the hundredths.
;
sub es:[di].DDPRW_LENGTH,ax
dci8: mov es:[di].DDP_CONTEXT,dx ; return value goes in context field
mov es:[di].DDP_STATUS,DDSTAT_DONE
dci9: ret
ENDPROC ddclk_ctlin
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ddclk_ctlout
;
; Inputs:
; ES:DI -> DDPRW
;
; Outputs:
;
ASSUME CS:CODE, DS:CODE, ES:NOTHING, SS:NOTHING
DEFPROC ddclk_ctlout
ret
ENDPROC ddclk_ctlout
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; remove_packet
;
; Removes a packet from the chain of waiting packets, if it's still there.
;
; Inputs:
; ES:DI -> DDP
;
; Outputs:
; Carry clear if the packet was removed, set if it wasn't found
;
; Modifies:
; AX
;
ASSUME CS:CODE, DS:NOTHING, ES:NOTHING, SS:NOTHING
DEFPROC remove_packet
push bx
push cx
push ds
mov ds,cs:[0].DDH_DATA
mov bx,offset wait_ptr ; DS:BX -> first link
mov cx,es ; CX:DI -> packet to remove
pushf
cli
rp1: mov ax,[bx].OFF
cmp ax,-1 ; end of chain?
je rp8 ; yes, packet not found
cmp ax,di
jne rp2
cmp [bx].SEG,cx
je rp3
rp2: lds bx,dword ptr [bx] ; DS:BX -> next packet
lea bx,[bx].DDP_PTR ; DS:BX -> its link
jmp rp1
rp3: mov ax,es:[di].DDP_PTR.OFF ; unlink the packet
mov [bx].OFF,ax
mov ax,es:[di].DDP_PTR.SEG
mov [bx].SEG,ax
popf
clc
jmp short rp9
rp8: popf
stc
rp9: pop ds
pop cx
pop bx
ret
ENDPROC remove_packet
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ddclk_read
;
; Inputs:
; ES:DI -> DDPRW
;
; Outputs:
;
ASSUME CS:CODE, DS:CODE, ES:NOTHING, SS:NOTHING
DEFPROC ddclk_read
ret
ENDPROC ddclk_read
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ddclk_write
;
; Inputs:
; ES:DI -> DDPRW
;
; Outputs:
;
ASSUME CS:CODE, DS:CODE, ES:NOTHING, SS:NOTHING
DEFPROC ddclk_write
ret
ENDPROC ddclk_write
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ddclk_none (handler for unimplemented functions)
;
; Inputs:
; ES:DI -> DDP
;
; Outputs:
;
ASSUME CS:CODE, DS:CODE, ES:NOTHING, SS:NOTHING
DEFPROC ddclk_none
mov es:[di].DDP_STATUS,DDSTAT_ERROR + DDERR_UNKCMD
stc
ret
ENDPROC ddclk_none
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ddclk_timer (CLKX_TIMER)
;
; Starts (or cancels) a timer for another driver: after CX milliseconds
; (rounded up to whole clock ticks, where the first tick may be partial), the
; timer's TMR_FUNC is called from our timer interrupt handler, with ES:DI
; pointing to the timer and interrupts disabled; it may modify any registers
; except SS:SP and BP. Starting a timer that's already running restarts it.
; This is a far entry (see CLKX) that may be called from interrupt handlers.
;
; Inputs:
; ES:DI -> TMR (with TMR_FUNC set)
; CX = milliseconds (0 to cancel)
;
; Outputs:
; None
;
; Modifies:
; None
;
ASSUME CS:CODE, DS:NOTHING, ES:NOTHING, SS:NOTHING
DEFPROC ddclk_timer,far
push ax
push dx
push ds
mov ds,cs:[0].DDH_DATA
ASSUME DS:CODE
pushf
cli
call tmr_remove
jcxz dt9
mov ax,cx
add ax,54
jnc dt1
mov ax,-1
dt1: sub dx,dx
push cx
mov cx,55
div cx ; AX = ticks
pop cx
mov es:[di].TMR_TICKS,ax
mov ax,di
xchg [tmr_list].OFF,ax
mov es:[di].TMR_NEXT.OFF,ax
mov ax,es
xchg [tmr_list].SEG,ax
mov es:[di].TMR_NEXT.SEG,ax
dt9: popf
pop ds
ASSUME DS:NOTHING
pop dx
pop ax
ret
ENDPROC ddclk_timer
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; tmr_remove
;
; Removes a timer from the chain of running timers, if it's there.
;
; Inputs:
; ES:DI -> TMR
;
; Modifies:
; AX
;
ASSUME CS:CODE, DS:NOTHING, ES:NOTHING, SS:NOTHING
DEFPROC tmr_remove
push bx
push ds
mov ds,cs:[0].DDH_DATA
mov bx,offset tmr_list ; DS:BX -> first link
tr1: mov ax,[bx].OFF
cmp ax,-1 ; end of chain?
je tr9 ; yes
cmp ax,di
jne tr2
mov ax,es
cmp [bx].SEG,ax
je tr3
tr2: lds bx,dword ptr [bx] ; DS:BX -> next timer (and its link)
jmp tr1
tr3: mov ax,es:[di].TMR_NEXT.OFF ; unlink the timer
mov [bx].OFF,ax
mov ax,es:[di].TMR_NEXT.SEG
mov [bx].SEG,ax
tr9: pop ds
pop bx
ret
ENDPROC tmr_remove
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; run_timers
;
; Counts down every running timer, and calls (and removes) any that expire.
;
; Inputs:
; DS = CODE
;
; Modifies:
; AX, BX, DX, DI, ES
;
ASSUME CS:CODE, DS:CODE, ES:NOTHING, SS:NOTHING
DEFPROC run_timers
mov bx,offset tmr_list ; DS:BX -> link
rt1: les di,[bx] ; ES:DI -> timer, if any
cmp di,-1 ; end of chain?
je rt9 ; yes
dec es:[di].TMR_TICKS ; has it expired?
jnz rt2 ; no
mov ax,es:[di].TMR_NEXT.OFF ; yes, unlink it
mov [bx].OFF,ax
mov ax,es:[di].TMR_NEXT.SEG
mov [bx].SEG,ax
push bx
push cx
push si
push ds
call dword ptr es:[di].TMR_FUNC
pop ds
pop si
pop cx
pop bx
jmp rt1 ; the same link is next
rt2: mov bx,di ; DS:BX -> this timer's link
push es
pop ds
jmp rt1
rt9: mov ds,cs:[0].DDH_DATA
ret
ENDPROC run_timers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ddclk_interrupt (timer hardware interrupt handler)
;
; Inputs:
; None
;
; Outputs:
; None
;
; Modifies:
; None
;
ASSUME CS:CODE, DS:NOTHING, ES:NOTHING, SS:NOTHING
DEFPROC ddclk_interrupt,far
call far ptr DDINT_ENTER
pushf
call [tmr_int]
push ax
push bx
push dx
push di
push ds
push es
mov ds,cs:[0].DDH_DATA
ASSUME DS:CODE
add [ticksToday].LOW,1
adc [ticksToday].HIW,0
;
; The maximum ticks per day (1573040) is 0018:00B0.
;
cmp [ticksToday].HIW,18h
jb ddi0a ; no overflow yet
ASSERT Z
ja ddi0 ; large overflow (how'd that happen?)
cmp [ticksToday].LOW,00B0h
jb ddi0a
ddi0: mov [ticksToday].LOW,0
mov [ticksToday].HIW,0
;
; We use a DOS utility function to increment the date, since DOS already
; contains the required calendar data to properly increment a date (ie, it
; is not a device-specific operation).
;
mov cx,[dateYear]
mov dx,word ptr [dateDay]
DOSUTIL INCDATE
mov [dateYear],cx
mov word ptr [dateDay],dx
ddi0a: cmp [sndTicks],0 ; is a sound playing?
je ddi0b ; no
dec [sndTicks] ; yes, has it ended?
jnz ddi0b ; no
in al,61h
and al,0FCh
out 61h,al ; yes, turn the speaker off
ddi0b: call run_timers
mov bx,offset wait_ptr ; DS:BX -> ptr
les di,[bx] ; ES:DI -> packet, if any
ASSUME ES:NOTHING
sti
ddi1: cmp di,-1 ; end of chain?
je ddi9 ; yes
ASSERT STRUCT,es:[di],DDP
;
; We wait for the double-word decrement to underflow (ie, to go from 0 to -1)
; since that's the simplest to detect. And while you might think that means we
; always wait 1 tick longer than requested -- well, sort of. We have no idea
; how much time elapsed between the request's arrival and the first tick; that
; time could be almost zero, so think of the tick count as "full" ticks.
;
sub es:[di].DDPRW_LBA,1
sbb es:[di].DDPRW_LENGTH,0
jae ddi6 ; keep waiting
;
; Notify DOS that the SCB associated with this packet is done waiting.
;
ddi2: mov dx,es ; DX:DI -> packet (aka "wait ID")
mov ah,DOS_UTL_ENDWAIT
call far ptr DDINT_UTIL ; end the wait on DX:DI (see int_util)
jnc ddi3
;
; If ENDWAIT returns an error, we presume that we simply got ahead of the
; WAIT call, so rewind the count to zero and leave the packet on the list.
;
mov es:[di].DDPRW_LBA,0
mov es:[di].DDPRW_LENGTH,0
jmp short ddi6
;
; WAIT condition has been satisfied, remove packet from wait_ptr list.
;
ddi3: cli
mov ax,es:[di].DDP_PTR.OFF
mov [bx].OFF,ax
mov ax,es:[di].DDP_PTR.SEG
mov [bx].SEG,ax
sti
jmp short ddi7
ddi6: lea bx,[di].DDP_PTR ; update prev addr ptr in DS:BX
push es
pop ds
ddi7: les di,es:[di].DDP_PTR
jmp ddi1
ddi9: pop es
pop ds
pop di
pop dx
pop bx
pop ax
stc ; set carry to indicate yield
jmp far ptr DDINT_LEAVE
ENDPROC ddclk_interrupt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Driver initialization
;
; Inputs:
; ES:BX -> DDPI
;
; Outputs:
; DDPI's DDPI_END updated
;
ASSUME CS:CODE, DS:NOTHING, ES:NOTHING, SS:NOTHING
DEFPROC ddclk_init,far
ASSERT STRUCT,es:[bx],DDP
mov es:[bx].DDPI_END.OFF,offset ddclk_init
mov ds,cs:[0].DDH_DATA
ASSUME DS:CODE
mov ds:[0].DDH_REQUEST,offset DEV:ddclk_req
;
; Start with the BIOS time of day (which an AT-class BIOS sets from its
; real-time clock) and the real-time clock's date, if any. A PC or XT BIOS
; doesn't support TIME_GETDATE, so CX remains zero and our default date stands.
;
mov ah,TIME_GETTICKS
int INT_TIME ; CX:DX is current tick count
mov [ticksToday].LOW,dx
mov [ticksToday].HIW,cx
mov ah,TIME_GETDATE
sub cx,cx
int INT_TIME ; CX = century/year, DX = month/day
jcxz ci1 ; no real-time clock date
mov al,cl
call bcd_bin ; AX = year within century
xchg ax,cx ; CX = that year, AH = century (BCD)
mov al,ah
call bcd_bin ; AX = century
mov ah,100
mul ah
add cx,ax
mov [dateYear],cx
mov al,dh
call bcd_bin
mov [dateMonth],al
mov al,dl
call bcd_bin
mov [dateDay],al
ci1:
;
; Install an INT 08h hardware interrupt handler, which we will use to service
; WAIT requests, and which will also drive the DOS scheduler as soon as DOS has
; initialized and revectored DDINT_ENTER/DDINT_LEAVE.
;
mov al,INT_HW_TMR
mov cx,offset ddclk_interrupt
mov dx,cs
cli
call far ptr DDINT_SETVEC ; DX:CX = previous vector
mov [tmr_int].OFF,cx
mov [tmr_int].SEG,dx
sti
ret
ENDPROC ddclk_init
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; bcd_bin
;
; Inputs:
; AL = BCD value (0-99)
;
; Outputs:
; AX = binary value
;
DEFPROC bcd_bin
push cx
mov cl,4
mov ah,al
shr ah,cl ; AH = tens digit
and al,0Fh ; AL = ones digit
aad ; AX = AH * 10 + AL
pop cx
ret
ENDPROC bcd_bin
CODE ends
DATA segment para public 'DATA'
ddclk_end db 16 dup(0)
DATA ends
end