-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.a86
432 lines (421 loc) · 13.7 KB
/
clock.a86
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
;
; Licensed under the MIT license. See LICENSE file in the project root for details.
;
time_read:
IF ATCLOCK
mov ah, 02h
clc
int 01Ah
cmp cx, 2359h
jl time_dec
cmp dh, 59h
ja time_read
time_dec:
call decode_at_time
jc int_err
mov ah, 04h
clc
int 01Ah
jc int_err
call decode_at_date
ELSE
call read_rtc_clock
ENDIF
call print_time
mov cx,0Ch
int 0E0h
sub al,29H
ja cpm31_cpm
call cpm_set_time
call quit
cpm31_cpm:
call cpm31_set_time
call quit
IF ATCLOCK
int_86err:
lea si, msg_86err
call print_str
call quit
int_err:
lea si, msg_interr
call print_str
call quit
ENDIF
print_time:
push bx
push si
mov bx, 10
mov ax, word ptr tm_buffer ; year
call print_dec4
mov al, '-'
call print_char
mov al, byte ptr tm_buffer+3 ; month
inc ax
call print_dec2
mov al, '-'
call print_char
mov al, byte ptr tm_buffer+2 ; day
inc ax
call print_dec2
mov al, ' '
call print_char
mov al, byte ptr tm_buffer+5 ; hour
call print_dec2
mov al, ':'
call print_char
mov al, byte ptr tm_buffer+4 ; minute
call print_dec2
mov al, ':'
call print_char
mov al, byte ptr tm_buffer+7 ; second
call print_dec2
mov al, '.'
call print_char
mov al, byte ptr tm_buffer+6 ; centisecond
call print_dec2
lea si, msg_nl
call print_str
pop si
pop bx
ret
IF ATCLOCK
decode_at_time:
push ax
push bx
; hour
mov al, ch
call bcd_conv_byte
mov byte ptr tm_buffer+5, al
; minute
mov al, cl
call bcd_conv_byte
mov byte ptr tm_buffer+4, al
; second
mov al, dh
call bcd_conv_byte
mov byte ptr tm_buffer+7, al
; centisecond
mov byte ptr tm_buffer+6, 0
pop ax
pop bx
ret
decode_at_date:
push ax
push bx
; century
mov al, ch
call bcd_conv_byte
mov byte ptr tm_buffer, al
; year
mov al, cl
call bcd_conv_byte
mov byte ptr tm_buffer+1, al
mov al, byte ptr tm_buffer
xor ah, ah
mov bl, 100
mul bl
mov bl, byte ptr tm_buffer+1
xor bh, bh
add ax, bx
mov word ptr tm_buffer, ax
; month
mov al, dh
call bcd_conv_byte
dec al
mov byte ptr tm_buffer+3, al
; day
mov al, dl
call bcd_conv_byte
dec al
mov byte ptr tm_buffer+2, al
pop bx
pop ax
ret
ELSE
read_rtc_clock:
push ax
push bx
push dx
mov dx, CPORT
mov al, 0Ah
out dx, al
mov dx, DPORT
mov al, 20h
out dx, al
mov dx, CPORT
mov al, 0Bh
out dx, al
mov dx, DPORT
mov al, 27h
out dx, al
rtc_wait:
mov dx, CPORT
mov al, 0 ; second
out dx, al
mov dx, DPORT
in al, dx
cmp al, 58
jz rtc_wait
cmp al, 59
jz rtc_wait
mov byte ptr tm_buffer+7, al
mov byte ptr tm_buffer+6, 0
mov dx, CPORT
mov al, 2 ; minutes
out dx, al
mov dx, DPORT
in al, dx
mov byte ptr tm_buffer+4, al
mov dx, CPORT
mov al, 4 ; hour
out dx, al
mov dx, DPORT
in al, dx
mov byte ptr tm_buffer+5, al
IF DS12C887
mov dx, CPORT
mov al, 32h ; century
out dx, al
ELSE
mov al, 20
ENDIF
xor ah, ah
mov bl, 100
mul bl
mov bx, ax
mov dx, DPORT
in al, dx
mov dx, CPORT
mov al, 9 ; year
out dx, al
mov dx, DPORT
in al, dx
xor ah, ah
add ax, bx
mov word ptr tm_buffer, ax
mov dx, CPORT
mov al, 8 ; month
out dx, al
mov dx, DPORT
in al, dx
dec al
mov byte ptr tm_buffer+3, al
mov dx, CPORT
mov al, 7 ; day
out dx, al
mov dx, DPORT
in al, dx
dec al
mov byte ptr tm_buffer+2, al
pop dx
pop bx
pop ax
ret
ENDIF
bcd_conv_byte:
push bx
push cx
push ax
mov cl, 04h
ror al, cl
and al, 0Fh
mov bl, 10
mul bl
pop bx
and bl, 0Fh
add al, bl
pop cx
pop bx
ret
leap_days:
push si
push cx
push dx
mov cl, 2
shr ax, cl
mov dx, ax
and ax, 3
jnz leap_days_end
lea cx, month_day
add cx, bx
add cx, bx
mov si, cx
mov cx, [si]
cmp cx, 59
jae leap_days_end
dec dx
leap_days_end:
mov ax,dx
pop dx
pop cx
pop si
ret
cpm31_set_time:
push es
push si
push ax
push bx
push cx
push dx
; (year-1978)*365
mov ax, word ptr tm_buffer
push ax
sub ax, 1978
mov cx, 365
mul cx
mov dx, ax
; +day
mov cl, byte ptr tm_buffer+2
xor ch, ch
add cl, 1
add dx, cx
; +leap_days(1978,0)
mov ax, 1979
mov bx, 0
call leap_days
sub dx, ax
; +month_days
mov bl, byte ptr tm_buffer+3
xor bh, bh
lea cx, month_day
add cx, bx
add cx, bx
mov si, cx
mov cx, [si]
add dx, cx
; +leap_days(year, month)
pop ax
mov bl, byte ptr tm_buffer+3
xor bh, bh
call leap_days
add dx, ax
mov word ptr stamp_year, dx
mov al, byte ptr tm_buffer+5
xor ah, ah
mov bl, 10
div bl
mov cl, 4
rol al, cl
add al, ah
mov byte ptr stamp_hours, al
mov al, byte ptr tm_buffer+4
xor ah, ah
mov bl, 10
div bl
mov cl, 4
rol al, cl
add al, ah
mov byte ptr stamp_mins, al
mov al, byte ptr tm_buffer+7
xor ah, ah
mov bl, 10
div bl
mov cl, 4
rol al, cl
add al, ah
mov byte ptr stamp_secs, al
mov cx,ds
mov es,cx
lea dx, stamp_year
mov cl, 68h
int 0E0h
pop dx
pop cx
pop bx
pop ax
pop si
pop es
ret
cpm_set_time:
push es
push ax
push bx
push cx
push dx
cli
mov cl, 31h ;Call Get Sysdata Addrs interrupt.
int 0E0h ;CP/M interrupt. ES:BX -> Sysdata.
add bx, 20h ;ES:BX now -> 17 bytes of time/date data.
xor ah, ah
mov al, byte ptr tm_buffer+3 ;Month
inc al
mov dl, 10
div dl
add al, '0'
mov es:[bx], al
inc bx
mov al, ah
add al, '0'
mov es:[bx], al
inc bx
inc bx
xor ah, ah
mov al, byte ptr tm_buffer+2 ;Day
inc al
mov dl, 10
div dl
add al, '0'
mov es:[bx], al
inc bx
mov al, ah
add al, '0'
mov es:[bx], al
inc bx
inc bx
xor dx, dx
mov ax, word ptr tm_buffer ;Year
mov cx, 100
div cx
mov ax, dx
xor ah, ah
mov dl, 10
div dl
add al, '0'
mov es:[bx], al
inc bx
mov al, ah
add al, '0'
mov es:[bx], al
inc bx
inc bx
xor ah, ah
mov al, byte ptr tm_buffer+5 ;Hour
mov dl, 10
div dl
add al, '0'
mov es:[bx], al
inc bx
mov al, ah
add al, '0'
mov es:[bx], al
inc bx
inc bx
xor ah, ah
mov al, byte ptr tm_buffer+4 ;Min
mov dl, 10
div dl
add al, '0'
mov es:[bx], al
inc bx
mov al, ah
add al, '0'
mov es:[bx], al
inc bx
inc bx
xor ah, ah
mov al, byte ptr tm_buffer+7 ;Sec
mov dl, 10
div dl
add al, '0'
mov es:[bx], al
inc bx
mov al, ah
add al, '0'
mov es:[bx], al
sti
pop dx
pop cx
pop bx
pop ax
pop es
ret