-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdates.asm
286 lines (248 loc) · 5.99 KB
/
dates.asm
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
; Normalizes the variables containing the selected date, and also updates
; selected_month_length, start_weekday and is_leap_year. That is, this
; subroutine for example changes "0 January 1997" to "31 December 1996".
; This should be called after every cursor movement.
normalizeSelectedDate:
kld(a, (selected_day))
ld d, a
kld(a, (selected_month))
ld e, a
kld(hl, (selected_year))
kld(a, (selected_month_length))
ld b, a
; correct underflowed day
ld a, d
cp 128
jr c, +_
kcall(toPreviousMonth)
jr .done
_:
; correct overflowed day
ld a, d
cp b
jr c, +_
kcall(toNextMonth)
_:
.done:
ld a, d
kld((selected_day), a)
ld a, e
kld((selected_month), a)
kld((selected_year), hl)
ret
;; toPreviousMonth
;; Decreases selected_month and updates selected_month_length, start_weekday
;; and is_leap_year.
;; Inputs:
;; D: the day (0-30)
;; E: the month (0-11)
;; HL: the year
;; Outputs:
;; D: updated day
;; E: updated month
;; HL: updated year
;; Destroys:
;; A
toPreviousMonth:
; decrease the month
dec e
; if the month is < 0, go to the previous year
ld a, e
cp 128
jr c, +_
add a, 12
ld e, a
dec hl
_:
; update selected_month_length, start_weekday, is_leap_year
kcall(updateMonthData)
; update the selected day
kld(a, (selected_month_length))
add a, d
ld d, a
ret
;; toNextMonth
;; Increases selected_month and updates selected_month_length, start_weekday
;; and is_leap_year.
;; Inputs:
;; D: the day (0-30)
;; E: the month (0-11)
;; HL: the year
;; Outputs:
;; D: updated day
;; E: updated month
;; HL: updated year
;; Destroys:
;; A
toNextMonth:
; increase the month
inc e
; update the selected day
kld(a, (selected_month_length))
sub a, d
neg
ld d, a
; if the month is >= 12, go to the next year
ld a, e
cp 12
jr c, +_
sub a, 12
ld e, a
inc hl
_:
; update selected_month_length, start_weekday, is_leap_year
kcall(updateMonthData)
ret
;; updateMonthData
;; Updates selected_month_length, start_weekday and is_leap_year.
;; Inputs:
;; D: the day (0-30)
;; E: the month (0-11)
;; HL: the year
;; Outputs:
;; D: updated day
;; E: updated month
;; HL: updated year
;; Destroys:
;; A
updateMonthData:
push bc
kcall(weekdayMonthStart)
ld a, b
kld((start_weekday), a)
ld a, c
kld((is_leap_year), a)
push hl
pcall(monthLength)
pop hl
kld((selected_month_length), a)
pop bc
ret
;; weekdayYearStart
;; Computes the weekday of 1 January of a given year in the Gregorian
;; calendar. Also checks whether the year is a leap year or not.
;; Inputs:
;; HL: the year
;; Outputs:
;; B: the weekday (0-6, 0 = Sunday, 6 = Saturday) of 1 January of the year
;; C: indicates whether the year is a leap year or not (1 if leap; 0 if
;; non-leap)
;; Destroys:
;; A
;; Notes:
;; This uses a formula proposed by Gauss. See
;; http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Gauss.27_algorithm
;; Interleaved with this calculation, it is determined whether we are dealing
;; with a leap year or not.
weekdayYearStart:
push de \ push hl
; c will be zero if it is a leap year; non-zero otherwise
ld c, 0
dec hl
; (1 + 5 (hl % 4) + 4 (hl % 100) + 6 (hl % 400)) % 7
; 6 (hl % 400)
ld de, -400
_: add hl, de
jr c, -_
ld de, 400
add hl, de
; if hl = 399, the year was divisible by 400, so increase c
ld a, h
cp 1
jr nz, +_
ld a, l
cp 399-256
jr nz, +_
inc c
_:
push hl
ld d, h
ld e, l
add hl, hl ; 2x
add hl, de ; 3x
add hl, hl ; 6x
push bc
ld c, 7
pcall(divHLByC)
pop bc
ld b, a ; store intermediate result in b
pop hl
; 4 (hl % 100)
ld de, -100
_: add hl, de
jr c, -_
ld de, 100
add hl, de
; if hl = 99, the year was divisible by 100, so decrease c
ld a, h
cp 0
jr nz, +_
ld a, l
cp 99
jr nz, +_
dec c
_:
push hl
add hl, hl ; 2x
add hl, hl ; 4x
push bc
ld c, 7
pcall(divHLByC)
pop bc
add a, b
ld b, a
pop hl
; 5 (hl % 4)
ld a, l
and 0b00000011
; if a = 3, the year was divisible by 4, so increase c
cp 3
jr nz, +_
inc c
_:
ld l, a
add a, a ; 2x
add a, a ; 4x
add a, l ; 5x
add a, b
; the +1, and divide by 7
inc a
_: sub a, 7
jr nc, -_
add a, 7
ld b, a
pop hl \ pop de
ret
;; weekdayMonthStart
;; Computes the weekday of 1 January of a given year in the Gregorian
;; calendar. Also checks whether the year is a leap year or not.
;; Inputs:
;; HL: the year
;; E: the month
;; Outputs:
;; B: the weekday (0-6, 0 = Sunday, 6 = Saturday) of 1 January of the year
;; C: indicates whether the year is a leap year or not (1 if leap; 0 if
;; non-leap)
;; Destroys:
;; A
;; Notes:
;; This simply uses [weekdayYearStart], plus some offsets for the months.
weekdayMonthStart:
kcall(weekdayYearStart)
push hl \ push de
ld a, c
cp 0
jr z, +_
kld(hl, month_start_weekday_leap)
jr ++_
_: kld(hl, month_start_weekday_non_leap)
_: ld d, 0
add hl, de
ld a, (hl)
add a, b
cp 7
jr c, +_
sub a, 7
_: ld b, a
pop de \ pop hl
ret