Browse code

Add source code of the applications

Pawel Jablonski authored on 18/03/2023 16:35:34
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,399 @@
1
+; Author: Pawel Jablonski
2
+; E-mail: pj@xirx.net
3
+; WWW: xirx.net
4
+; GIT: git.xirx.net
5
+;
6
+; License: You can use this code however you like
7
+; but leave information about the original author.
8
+; Code is free for non-commercial and commercial use.
9
+
10
+map ../../../bin/rom/rom.map
11
+
12
+; Date change message
13
+const changedMessage "Date has been changed"
14
+
15
+; Error messages
16
+const errorMessageLine0 "Date has wrong format"
17
+const errorMessageLine1 "Please use: YYMMDDhhmmss"
18
+
19
+; Prefix for year
20
+const yearPrefix "20"
21
+
22
+; First label is the entry point
23
+.entry:
24
+	call .main
25
+	ret
26
+
27
+; Arguments
28
+;	X - Arguments address low
29
+;	Y - Arguments address high
30
+; No return
31
+.main:
32
+	push A
33
+	push X
34
+	push Y
35
+	; Check length of arguments
36
+	call .xStringLength
37
+	; Print current date and time if argument list is empty
38
+	cmp 0, A
39
+	je .main_print
40
+	; Parse arguments
41
+	call .parseDate
42
+	; Write new date nad time if arguments are correct
43
+	cmp 0, A
44
+	jne .main_write
45
+	; Load address to first line of error message
46
+	mov low(*errorMessageLine0), X
47
+	mov high(*errorMessageLine0), Y
48
+	; Print first line of error message
49
+	call .xStringPrint
50
+	call .xStringNewLine
51
+	call .xStringNewLine
52
+	; Load address to second line of error message
53
+	mov low(*errorMessageLine1), X
54
+	mov high(*errorMessageLine1), Y
55
+	; Print second line of error message
56
+	call .xStringPrint
57
+	pop Y
58
+	pop X
59
+	pop A
60
+	ret
61
+	; Write date and time to RTC
62
+.main_write:
63
+	; Write date and time to RTC buffers
64
+	call .writeDate
65
+	; Write date and time from RTC buffers to RTC
66
+	call .xRTCWrite
67
+	; Load address to message
68
+	mov low(*changedMessage), X
69
+	mov high(*changedMessage), Y
70
+	; Print message
71
+	call .xStringPrint
72
+	call .xStringNewLine
73
+	call .xStringNewLine
74
+	; Print date and time from RTC
75
+.main_print:
76
+	; Read date and time from RTC to RTC buffers
77
+	call .xRTCRead
78
+	; Print current date and time
79
+	call .printDate
80
+	pop Y
81
+	pop X
82
+	pop A
83
+	ret
84
+
85
+; Buffer for parse and write
86
+var year
87
+var month
88
+var day
89
+var hour
90
+var minute
91
+var second
92
+
93
+; No arguments
94
+; Return:
95
+; 	A - success (0 - error, 1 - ok)
96
+.parseDate:
97
+	push B
98
+	push X
99
+	push Y
100
+	; Check length of arguments
101
+	call .xStringLength
102
+	; Good input should has 12 characters
103
+	cmp 12, A
104
+	jne .parseDate_error
105
+	; Good input should has only digits
106
+	call .parseDigits
107
+	cmp 0, A
108
+	je .parseDate_error
109
+	; Converting two digits to year
110
+	call .convertTwoDigits
111
+	str A, *year
112
+	; Move to next field
113
+	add 2, X
114
+	incc Y
115
+	; Converting two digits to month
116
+	call .convertTwoDigits
117
+	; Months are 1 to 12
118
+	cmp 1, A
119
+	; 1 > A
120
+	jg .parseDate_error
121
+	cmp 12, A
122
+	; 12 < A
123
+	jl .parseDate_error
124
+	str A, *month
125
+	; Move to next field
126
+	add 2, X
127
+	incc Y
128
+	; Converting two digits to day
129
+	call .convertTwoDigits
130
+	; Days start from 1
131
+	cmp 1, A
132
+	; 1 > A
133
+	jg .parseDate_error
134
+	mov A, B
135
+	push B
136
+	; Calculating quantity of days for selected year and month
137
+	ldr *year, A
138
+	ldr *month, B
139
+	call .getDaysInMonth
140
+	pop B
141
+	xchg A, B
142
+	; Day number could not be higher than quantity of days in month
143
+	cmp B, A
144
+	; B < A
145
+	jl .parseDate_error
146
+	str A, *day
147
+	; Move to next field
148
+	add 2, X
149
+	incc Y
150
+	; Hours are 0 to 23
151
+	call .convertTwoDigits
152
+	cmp 23, A
153
+	; 23 < A
154
+	jl .parseDate_error
155
+	str A, *hour
156
+	; Move to next field
157
+	add 2, X
158
+	incc Y
159
+	; Minutes are 0 to 59
160
+	call .convertTwoDigits
161
+	cmp 59, A
162
+	; 59 < A
163
+	jl .parseDate_error
164
+	str A, *minute
165
+	; Move to next field
166
+	add 2, X
167
+	incc Y
168
+	; Seconds are 0 to 59
169
+	call .convertTwoDigits
170
+	cmp 59, A
171
+	; 59 < A
172
+	jl .parseDate_error
173
+	str A, *second
174
+	mov 1, A
175
+	pop Y
176
+	pop X
177
+	pop B
178
+	ret
179
+	; Parse ends with error
180
+.parseDate_error:
181
+	mov 0, A
182
+	pop Y
183
+	pop X
184
+	pop B
185
+	ret
186
+
187
+; Days in month table
188
+const daysNumber 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
189
+
190
+; Arguments:
191
+;	A - year
192
+;	B - month
193
+; Return:
194
+;	A - days in month
195
+.getDaysInMonth:
196
+	push B
197
+	push X
198
+	push Y
199
+	; Check an extra day for February
200
+	cmp 2, B
201
+	jne .getDaysInMonth_notLeapYear
202
+	; Years divisible by 4 are leap years 
203
+	and 0b11, A
204
+	cmp 0, A
205
+	jne .getDaysInMonth_notLeapYear
206
+	; Set 29 days if it is Februray in leap year
207
+	mov 29, A
208
+	jmp .getDaysInMonth_end
209
+	; Select days quanity of month
210
+.getDaysInMonth_notLeapYear:
211
+	mov low(*daysNumber), X
212
+	mov high(*daysNumber), Y
213
+	dec B
214
+	add B, X
215
+	incc Y
216
+	ldf A
217
+.getDaysInMonth_end:
218
+	pop Y
219
+	pop X
220
+	pop B
221
+	ret
222
+
223
+; Arguments:
224
+; 	X - low address
225
+;	Y - high address
226
+; Return:
227
+; 	A - success (0 - error, 1 - ok)
228
+.parseDigits:
229
+	push B
230
+	push X
231
+	push Y
232
+	; Clear loop counter
233
+	clr B
234
+	; Check every char
235
+.parseDigits_Loop:
236
+	; Load char from buffer
237
+	ldr A
238
+	; It must be '0' or higher
239
+	cmp '0', A
240
+	; '0' > A
241
+	jg .parseDigits_Error
242
+	; It must be '9' or lower
243
+	cmp '9', A
244
+	; '9' < A
245
+	jl .parseDigits_Error
246
+	; Move to next char
247
+	inc X
248
+	incc Y
249
+	; Repeat 11+1 times
250
+	loope 11, B, .parseDigits_Loop
251
+	; All chars are digits
252
+	mov 1, A
253
+	pop Y
254
+	pop X
255
+	pop B
256
+	ret
257
+	; One or more chars are not digit
258
+.parseDigits_Error:
259
+	mov 0, A
260
+	pop Y
261
+	pop X
262
+	pop B
263
+	ret
264
+
265
+; Converting table number from string to hex
266
+const convertTable 00, 10, 20, 30, 40, 50, 60, 70, 80, 90
267
+
268
+; Arguments:
269
+; 	X - low address
270
+;	Y - high address
271
+; Return:
272
+; 	A - number
273
+.convertTwoDigits:
274
+	push B
275
+	push X
276
+	push Y
277
+	; Load first digit
278
+	ldr B
279
+	; Convert from char to number
280
+	sub '0', B
281
+	; Move to next digit
282
+	inc X
283
+	incc Y
284
+	; Load second digit
285
+	ldr A
286
+	; Convert from char to number
287
+	sub '0', A
288
+	; Set address to converting table
289
+	mov low(*convertTable), X
290
+	mov high(*convertTable), Y
291
+	; Calc number to take
292
+	add B, X
293
+	incc Y
294
+	; Load number from table
295
+	ldf B
296
+	; Add number from table to the second digit
297
+	add B, A
298
+	pop Y
299
+	pop X
300
+	pop B
301
+	ret
302
+
303
+; No arguments
304
+; No return
305
+.writeDate:
306
+	push A
307
+	; Load year from buffer
308
+	ldr *year, A
309
+	; Write year to RTC buffer
310
+	call .xRTCSetYear
311
+	; Load month from buffer
312
+	ldr *month, A
313
+	; Write month to RTC buffer
314
+	call .xRTCSetMonth
315
+	; Load day from buffer
316
+	ldr *day, A
317
+	; Write day to RTC buffer
318
+	call .xRTCSetDay
319
+	; Load hour from buffer
320
+	ldr *hour, A
321
+	; Write hour to RTC buffer
322
+	call .xRTCSetHour
323
+	; Load minute from buffer
324
+	ldr *minute, A
325
+	; Write minute to RTC buffer
326
+	call .xRTCSetMinute
327
+	; Load second from buffer
328
+	ldr *second, A
329
+	; Write second to RTC buffer
330
+	call .xRTCSetSecond
331
+	pop A
332
+	ret
333
+
334
+; No arguments
335
+; No return
336
+.printDate:
337
+	push A
338
+	push X
339
+	push Y
340
+	; Load address to prefix
341
+	mov low(*yearPrefix), X
342
+	mov high(*yearPrefix), Y
343
+	; Print prefix
344
+	call .xStringPrint
345
+	; Read year
346
+	call .xRTCGetYear
347
+	; Print year
348
+	call .printTwoDigits
349
+	mov '.', A
350
+	call .xStringPrintChar
351
+	; Read month
352
+	call .xRTCGetMonth
353
+	; Print month
354
+	call .printTwoDigits
355
+	mov '.', A
356
+	call .xStringPrintChar
357
+	; Read day
358
+	call .xRTCGetDay
359
+	; Print day
360
+	call .printTwoDigits
361
+	mov ' ', A
362
+	call .xStringPrintChar
363
+	; Read hour
364
+	call .xRTCGetHour
365
+	; Print hour
366
+	call .printTwoDigits
367
+	mov ':', A
368
+	call .xStringPrintChar
369
+	; Read minute
370
+	call .xRTCGetMinute
371
+	; Print minute
372
+	call .printTwoDigits
373
+	mov ':', A
374
+	call .xStringPrintChar
375
+	; Read second
376
+	call .xRTCGetSecond
377
+	; Print second
378
+	call .printTwoDigits
379
+	pop Y
380
+	pop X
381
+	pop A
382
+	ret
383
+
384
+; Arguments:
385
+; 	A - number
386
+; No return
387
+.printTwoDigits:
388
+	; 10 <= A
389
+	cmp 10, A
390
+	jle .printTwoDigits_noZero
391
+	push A
392
+	; Print zero if lower than 10
393
+	mov '0', A
394
+	call .xStringPrintChar
395
+	pop A
396
+.printTwoDigits_noZero:
397
+	; Print number
398
+	call .xStringPrintU8
399
+	ret