-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.a
672 lines (587 loc) · 18.9 KB
/
string.a
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
GETIN = $FFE4
CHAROUT = $FFD2
PLOT = $FFF0
CHRIN = $FFCF
CHKOUT = $FFC9
CLRCHN = $FFCC
HEX_CHARS
!tx "0123456789ABCDEF"
LO_NIBBLE
!byte 0
; --------------------------------------------------
; About the string routines: The first byte of a string is a length
; byte followed by the characters of the string. The maximum length
; of a string including the length byte is 255 bytes. This also means
; that a string can contain at most 254 characters.
; --------------------------------------------------
; --------------------------------------------------
; This routine prints a fixed point number to the screen. The address
; of the number has to be given in TEMP_PTR/TEMP_PTR+1
;
; No other values are returned.
; --------------------------------------------------
printNumHex
ldy #0
lda (TEMP_PTR), y
bne .printMinus
lda #32
jsr CHAROUT
jmp .egal
.printMinus
lda #45
jsr CHAROUT
.egal
ldy #4
.loopPrint
lda (TEMP_PTR), y
jsr printByte
cpy #4
bne .continuePrint
lda #46 ; print decimal point after first byte
jsr CHAROUT
.continuePrint
dey
bne .loopPrint
jsr printCRLF
rts
!macro printFixedPoint .addr {
lda #<.addr
sta TEMP_PTR
lda #>.addr
sta TEMP_PTR+1
jsr printNumHex
}
; --------------------------------------------------
; This routine prints a 32 bit unsigned number to the screen. The
; address of the number has to be given in TEMP_PTR/TEMP_PTR+1
;
; No values are returned.
; --------------------------------------------------
print32BitUnsignedCall
ldy #4
.loopPrintUint
lda (TEMP_PTR), y
jsr printByte
dey
bne .loopPrintUint
jsr printCRLF
rts
!macro printUnsigned .addr {
+load16BitImmediate .addr, TEMP_PTR
jsr print32BitUnsignedCall
}
; --------------------------------------------------
; This routine prints a byte in hex and a CRLF to the screen. The
; value to be printed is taken from the accu.
;
; No values are returned.
; --------------------------------------------------
printByteCRLF
jsr printByte
jsr printCRLF
rts
; --------------------------------------------------
; This routine prints a byte in hex to the screen. The
; value to be printed is taken from the accu.
;
; No values are returned.
; --------------------------------------------------
printByte
jsr splitByte
stx LO_NIBBLE
tax
lda HEX_CHARS, X
jsr CHAROUT
ldx LO_NIBBLE
lda HEX_CHARS, X
jsr CHAROUT
rts
; --------------------------------------------------
; This routine prints CRLF to the screen.
;
; No values are returned.
; --------------------------------------------------
printCRLF
lda #13
jsr CHAROUT
lda #10
jsr CHAROUT
rts
.CONV_TEMP
!byte 0
; --------------------------------------------------
; This routine splits the value in accu its nibbles. The lower nibble
; is returned in x and its upper nibble in the accu
; --------------------------------------------------
splitByte
sta .CONV_TEMP
and #$0F
tax
lda .CONV_TEMP
and #$F0
lsr
lsr
lsr
lsr
rts
!macro printByteLN .addr {
lda .addr
jsr printByteCRLF
}
; --------------------------------------------------
; Wait for a key and return ASCII Code of key in Accumulator
;
; INPUT: None
; OUTPUT: ASCII code of read character in accumulator
; --------------------------------------------------
waitForKey
jsr GETIN ; get key from keyboard
cmp #0 ; if 0, no key pressed
beq waitForKey ; loop if no key pressed
rts ; ASCII Code of pressed key is now in accumulator
; --------------------------------------------------
; Check if key is pressed and return ASCII Code of key in Accumulator or
; 0 if no key was pressed.
;
; The zero flag is cleared if a key was pressed.
; --------------------------------------------------
checkKey
jsr GETIN ; get key from keyboard
cmp #0 ; if 0, no key pressed
rts
STR_LEN_TEMP
!byte 0
; --------------------------------------------------
; This routine copies a string referenced via TEMP_PTR to a string
; referenced by TEMP_PTR2.
; --------------------------------------------------
stringCopy
ldy #0
lda (TEMP_PTR),y
sta STR_LEN_TEMP
inc STR_LEN_TEMP ; take length byte into account, i.e. copy one more byte
.copyLoop
lda (TEMP_PTR),y
sta (TEMP_PTR2),y
iny
cpy STR_LEN_TEMP
bne .copyLoop
rts
!macro strCpy .addr1, .addr2 {
+load16BitImmediate .addr1, TEMP_PTR
+load16BitImmediate .addr2, TEMP_PTR2
jsr stringCopy
}
!macro locate .posx, .posy {
clc
ldx #.posy
ldy #.posx
jsr PLOT
}
!macro locateAddr .addrx, .addry {
clc
ldx .addry
ldy .addrx
jsr PLOT
}
PRINT_LEN
!byte 0
; --------------------------------------------------
; This routine prints a string referenced via TEMP_PTR to the screen
; --------------------------------------------------
printString
ldy #0
lda (TEMP_PTR),Y ; load length byte
sta PRINT_LEN
+inc16Bit TEMP_PTR ; move pointer to first byte of string
.loopPrintStr
cpy PRINT_LEN ; check current print index first to handle zero length strings correctly
beq .printDone
lda (TEMP_PTR),y
jsr CHAROUT
iny
jmp .loopPrintStr
.printDone
rts
!macro printStr .addr {
+load16BitImmediate .addr, TEMP_PTR
jsr printString
}
!macro printStrAt .posx, .posy, .addr {
+locate .posx, .posy
+printStr .addr
}
!macro writeStrToChannel .chan, .addr {
ldx #.chan
jsr CHKOUT ; use .chan as output channel
+printStr .addr
jsr CLRCHN
}
!macro printStrAtAddr .addrPosx, .addPosy, .addr {
+locateAddr .addrPosx, .addrPosy
+printStr .addr
}
.LEN_OUTPUT
!byte 0
.LEN_ALLOWED
!byte 0
.INDEX_OUTPUT
!byte 0
.INPUT_CHAR
!byte 0
; --------------------------------------------------
; This routine implements a robust string input allowing only characters
; from a given set. The address of the target buffer has to be specified in
; TEMP_PTR. TEMP_PTR2 has to point to the set of allowed characters.
; The x register has to contain the length of the target buffer and the y
; register the length of the set of allowed characters.
;
; This routine returns the length of the string entered in the accu
; --------------------------------------------------
getString
stx .LEN_OUTPUT
sty .LEN_ALLOWED
txa
jsr printSpaces ; clear input text
lda #0
sta .INDEX_OUTPUT ; set index in output to 0
.inputLoop
jsr showCursor ; draw cursor at current position
jsr waitForKey
cmp #13 ; CR
beq .inputDone ; => We are done
cmp #20 ; DELETE
beq .delete ; delete one character from result string
sta .INPUT_CHAR
jsr .checkIfInSet ; check if typed character is allowed
bne .inputLoop ; Not allowed => try again
lda .INPUT_CHAR
ldy .INDEX_OUTPUT
cpy .LEN_OUTPUT ; have we reached the maximum length?
beq .inputLoop ; yes => don't store
sta (TEMP_PTR), y ; store typed character
inc .INDEX_OUTPUT ; move to next position in target buffer
jsr CHAROUT ; print typed character
jmp .inputLoop ; let user type next character
.delete
lda .INDEX_OUTPUT
beq .inputLoop ; Output index is 0 => do nothing
dec .INDEX_OUTPUT ; decrement the output position
; emulate delete. This is necessary to not
; disturb the PETSCII UI
lda #32 ; space
jsr CHAROUT
lda #157 ; Cursor left
jsr CHAROUT
lda #157 ; Cursor left
jsr CHAROUT
lda #32 ; space
jsr CHAROUT
lda #157 ; Cursor left
jsr CHAROUT
jmp .inputLoop ; let user enter next character
.inputDone
jsr clearCursor ; delete cursor from screen
lda .INDEX_OUTPUT ; load length of target buffer in accu
rts
.checkIfInSet
ldy #0
.checkLoop
cmp (TEMP_PTR2),Y ; is typed character in allowed set
beq .found ; yes => zero flag is set when routine returns
iny
cpy .LEN_ALLOWED
bne .checkLoop ; try next character
ldy #1 ; typed character is nit allowed => zero flag is clear when routine returns
.found
rts
showCursor
lda #164
jsr CHAROUT ; print underscore
lda #157
jsr CHAROUT ; print cursor left
rts
clearCursor
lda #32 ; print blank
jsr CHAROUT
lda #157 ; print cursor left
jsr CHAROUT
rts
!macro inputStr .targetStr, .allowedStr {
+load16BitImmediate .targetStr+1, TEMP_PTR
; maximum length is current length of target string
ldx .targetStr
+load16BitImmediate .allowedStr+1, TEMP_PTR2
ldy .allowedStr
jsr getString
sta .targetStr
}
!macro clearLineAt .posx, .posy, .len {
+locate .posx, .posy
lda #.len
jsr printSpaces
}
.TEMP_X
!byte 0
.TEMP_Y
!byte 0
.TEMP_SIZE
!byte 0
; --------------------------------------------------
; printSpaces prints as many spaces as the value of the accu indicates
;
; This routine does not return a value
; --------------------------------------------------
printSpaces
sta .TEMP_SIZE
sec
jsr PLOT ; get cursor position
stx .TEMP_Y
sty .TEMP_X
ldy #0
lda #32 ; load code for space
.loopClear ; print desired number of blanks
cpy .TEMP_SIZE ; check first, this handles the case with length 0 correctly
beq .doneClear
jsr CHAROUT ; print blank
iny
jmp .loopClear
.doneClear
clc
ldx .TEMP_Y
ldy .TEMP_X
jsr PLOT ; set cursor to original value
rts
.CMP_LENGTH
!byte 0
; --------------------------------------------------
; This routine implements a string compare. TEMP_PTR has to point to the first
; and TMP_PTR2 to the second string
;
; The zero flag is set when the zwo strings are equal.
; --------------------------------------------------
strCmp
ldy #0
lda (TEMP_PTR),y
cmp (TEMP_PTR2),y
bne .doneCmpNotEqual ; length is different => strings are different
sta .CMP_LENGTH ; length is equal, store it in .CMP_LENGTH
cmp #0 ; check if length is zero
beq .doneCmpEqual ; both strings are empty => done
iny ; move index to first byte of string
.cmpNext
lda (TEMP_PTR),y
cmp (TEMP_PTR2),y
bne .doneCmpNotEqual ; current bytes are unequal => strings are different
cpy .CMP_LENGTH ; end reached?
beq .doneCmpEqual ; all bytes were equal => strings are equal
iny
jmp .cmpNext ; look at next byte
.doneCmpEqual
lda #0 ; set zero flag
rts
.doneCmpNotEqual
lda #1 ; clear zero flag
rts
!macro strCmpAddr .addr1, .addr2 {
+load16BitImmediate .addr1, TEMP_PTR
+load16BitImmediate .addr2, TEMP_PTR2
jsr strCmp
}
; --------------------------------------------------
; This routine appends the string to which TEMP_PTR points to the string to
; which TEMP_PTR2 points. The caller has to ensure that there is enough room
; in the target string.
;
; This routine has no return value
; --------------------------------------------------
.SOURCE_LEN
!byte 0
.READ_INDEX
!byte 0
.WRITE_INDEX
!byte 0
strCatCall
ldy #0
lda (TEMP_PTR),y
beq .catDone ; source string is empty => we are done
sta .SOURCE_LEN
lda (TEMP_PTR2),y ; load length byte of target string
sta .WRITE_INDEX
inc .WRITE_INDEX ; write position is current length + 1
ldy #1
sty .READ_INDEX ; read index is 1
.loopStrCat
; here y has to contain .READ_INDEX
lda (TEMP_PTR),Y ; load at read index
ldy .WRITE_INDEX
sta (TEMP_PTR2),Y ; store at write index
ldy .READ_INDEX
cpy .SOURCE_LEN
beq .adaptLength ; we have exhausted the source string
inc .WRITE_INDEX
inc .READ_INDEX
ldy .READ_INDEX
jmp .loopStrCat
.adaptLength
; adapt length of target
ldy #0
lda .WRITE_INDEX
sta (TEMP_PTR2),y
.catDone
rts
!macro strCat .addrStr1, .addrStr2 {
+load16BitImmediate .addrStr1, TEMP_PTR
+load16BitImmediate .addrStr2, TEMP_PTR2
jsr strCatCall
}
.CONV_TAB
!byte 0,10,20,30,40,50,60,70,80,90
.CONV_TEMP1
!byte 0,0
.CONV_TEMP2
!byte 0,0
; --------------------------------------------------
; This routine converts the 1-3 character string (containing only the characters 0-9)
; to which TEMP_PTR points to a 16bit number, the address of which has to be given in
; TEMP_PTR2.
;
; This routine sets the zero flag in case everything was OK
; --------------------------------------------------
atoiCall
ldy #0
sty .CONV_TEMP1 ; clear result
sty .CONV_TEMP1+1 ; clear result
lda (TEMP_PTR),Y ; load length into y register
beq .atoiNotOk ; length 0 => Not expected
cmp #4
bcs .atoiNotOk ; length >= 4 => Not expected
; process least significant digit
tay ; length into y register
lda (TEMP_PTR),Y ; load digit
sec
sbc #48 ; 48 = '0'
sta .CONV_TEMP1
; process next digit
dey
beq .atoiOk ; we are already done, there was only one digit
lda (TEMP_PTR),Y ; load next digit
sec
sbc #48 ; 48 = '0'
tax
clc
lda .CONV_TAB,X ; multiply by 10
adc .CONV_TEMP1 ; add multiplication result to temporary value
sta .CONV_TEMP1 ; here the value can be at most 99
; process most significant digit
dey
beq .atoiOk ; we are already done, there were only two digits
lda (TEMP_PTR),Y ; load next digit
sec
sbc #48 ; 48 = '0'
ldx #100
jsr mul16Bit ; multiply digit by 100
stx .CONV_TEMP2 ; multiplication result to .CONV_TEMP2
sta .CONV_TEMP2+1
+add16Bit .CONV_TEMP2, .CONV_TEMP1 ; add temporary results to get final result
.atoiOk
; copy temp val to result result address
ldy #0
lda .CONV_TEMP1
sta (TEMP_PTR2),Y
iny
lda .CONV_TEMP1+1
sta (TEMP_PTR2),Y
lda #0 ; set zero flag
rts
.atoiNotOk
lda #1 ; clear zero flag
rts
!macro atoi .addrStr, .addrResult {
+load16BitImmediate .addrStr, TEMP_PTR
+load16BitImmediate .addrResult, TEMP_PTR2
jsr atoiCall
}
MOD_10_TABLE
!byte 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5
!byte 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1
!byte 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7
!byte 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3
!byte 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
!byte 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5
!byte 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1
!byte 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7
!byte 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3
!byte 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
!byte 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5
!byte 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1
!byte 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7
!byte 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3
!byte 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
!byte 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5
DIV_10_TABLE
!byte 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1
!byte 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3
!byte 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
!byte 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6
!byte 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
!byte 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9
!byte 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11
!byte 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12
!byte 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14
!byte 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15
!byte 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17
!byte 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19
!byte 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20
!byte 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22
!byte 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23
!byte 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25
.ITOA_TEMP
!byte 0
.ITOA_INDEX
!byte 0
.ITOA_BUFFER
!tx " "
; --------------------------------------------------
; This routine turns the byte contained in the accu into string of decimal
; digits.
;
; The resulting string has to be referenced through TEMP_PTR
; --------------------------------------------------
itoaCall
ldx #0
stx .ITOA_INDEX
sta .ITOA_TEMP
; convert byte value to character digits in reverse order
.itoaLoop
tax
lda MOD_10_TABLE,x
tay
lda HEX_CHARS, y
ldy .ITOA_INDEX
sta .ITOA_BUFFER,y
inc .ITOA_INDEX
lda DIV_10_TABLE, x
bne .itoaLoop
; format result as string
lda .ITOA_INDEX
; copy result length
ldy #0
sta (TEMP_PTR), y
tax ; result length in X register
dex ; last index
iny
; copy string data to target buffer in such a way that the
; result is in correct order
.copyOutput
lda .ITOA_BUFFER, x
sta (TEMP_PTR), y
iny
dex
bpl .copyOutput
rts
!macro itoa .valAddr, .addrOutString {
+load16BitImmediate .addrOutString, TEMP_PTR
lda .valAddr
jsr itoaCall
}