-
Notifications
You must be signed in to change notification settings - Fork 17
/
settime.z80
212 lines (195 loc) · 4.06 KB
/
settime.z80
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
;; settime.z80
;;
;; Read date and time from xtrs emulator trap and set
;; TRS-80 system date and time.
;;
;; Copyright (c) 1998 Ulrich Mueller
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use, copy,
;; modify, merge, publish, distribute, sublicense, and/or sell copies
;; of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;; Model I/III addresses
@exit equ 402dh
@abort equ 4030h
;; Model 1 and 3 store the last 2 digits of the year in the year byte
time1$ equ 4041h ; seconds/minutes/hours
date1$ equ 4044h ; year/day/month
time3$ equ 4217h
date3$ equ 421ah
;; Model 4 SVCs
@svc equ 40 ; rst address for SVCs
;@svc equ 5 ; older zmac requires 8080-style "rst 5"
@exit6 equ 22
@abort6 equ 21
;; Model 4 stores the offset from 1900 in the year byte
time4$ equ 002dh
date4$ equ 0033h
;; Emulator trap instructions
emt_time equ 36edh
org 5200h
settime:
call initj ; init OS-dependent tables
ld bc, 0ffffh
ld a, 1 ; get local time from Unix
defw emt_time ; BCDE: seconds since 1970
ld a, b
and c
inc a
jp z, abort
di
ld hl, (time)
ld a, 60
call divide
ld (hl), a ; seconds
inc hl
ld a, 60
call divide
ld (hl), a ; minutes
inc hl
ld a, 24
call divide
ld (hl), a ; hours
ex de, hl ; HL: days since 1970
ld bc, 19*256+70-1
ld de, 365
year1: inc c ; count years in C
ld a, c
sub 100
jr c, year2
ld c, a
inc b ; centuries
ld a, b
year2: and 3
sub 1
sbc hl, de
jr nc, year1
rlca
adc hl, de ; HL: days since 1 january
push hl
ld hl, (date)
call putyr ; year to (hl)
inc hl
ex (sp), hl
and 1 ; A=1 for leap year, 0 else
add a, 28
ld c, a
ld b, 0
ld d, b
month1: inc b ; count months in B
ld a, b
ld e, c
cp 2
jr z, month2 ; february
ld e, 31
and 9
jp po, month2 ; 31 days
dec e ; 30 days
month2: sbc hl, de ; subtract length of month
jr nc, month1
adc hl, de
ld c, l
pop hl
ld (hl), c ; day
inc hl
ld (hl), b ; month
ei
ld hl,0 ; needed on Model 4 --mann
jr exit
;; divide BCDE / A
;; returns quotient in BCDE, remainder in A
divide: push hl
neg
ld h, a
sub a
ld l, 33
div1: rla
add a, h
jr c, div2
sub h
div2: rl e
rl d
rl c
rl b
dec l
jr nz, div1
pop hl
ret
;; Jump tables for OS independence
;; Model 1
startj:
exit: jp @exit
abort: jp @abort
time: defw time1$
date: defw date1$
putyr: jp putyr1
endj:
;; Model 3
startj3:
jp @exit
jp @abort
defw time3$
defw date3$
jp putyr3
;; Model 4
startj4:
ld a, @exit6
rst @svc
ld a, @abort6
rst @svc
defw time4$
defw date4$
jp putyr4
;; Initialize tables
;; Changed to work even on a Model III (or Model 4 in III mode)
;; using MODELA/III as its ROM. Previous version didn't. --mann
;;
initj: ld hl, startj4 ; model 4?
ld a, (000ah)
cp 40h
jr nz, movej ; go if so
ld hl, startj3 ; model 3?
ld a, (0125h)
cp 'I'
ret nz ; return if not
movej: ld de, startj
ld bc, endj - startj
ldir
ret
;; Create year byte.
;; On entry, c has 2-digit year, b has century.
;; On exit, (hl) has year byte.
;; Model I/III, put 2-digit year
putyr1:
putyr3: ld (hl), c
ret
;; Model 4, put offset from 1900 (laboriously recomputed, sigh)
putyr4: push af
push bc
ld a, b
sub 19
ld b, a
ld a, c
jr z, py41
py40: add a, 100
djnz py40
py41: ld (hl), a
pop bc
pop af
ret
end settime