Repository navigation
Expand file tree
/
Copy pathstdlib.asm
More file actions
1967 lines (1823 loc) · 53.8 KB
/
Copy pathstdlib.asm
File metadata and controls
1967 lines (1823 loc) · 53.8 KB
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
keep obj/stdlib
mcopy stdlib.macros
case on
****************************************************************
*
* StdLib - Standard Library Utility Functions
*
* This code implements the tables and subroutines needed to
* support the standard C library STDLIB.
*
* December 1988
* Mike Westerfield
*
* Copyright 1988
* Byte Works, Inc.
*
* Note: Portions of this library appear in SysFloat
*
****************************************************************
*
StdLib start dummy segment
copy equates.asm
end
****************************************************************
*
* void abort()
*
* Stop the program.
*
****************************************************************
*
abort start
ph2 #SIGABRT
jsl raise
lda #-1
brl ~C_QUIT
end
****************************************************************
*
* int abs(int i)
*
* Return the absolute value of i.
*
* Inputs:
* i - argument
*
* Outputs:
* Returns abs(i).
*
****************************************************************
*
abs start
i equ 4 position of argument on stack
lda i,S A := i
bpl lb1 if A < 0 then
eor #$FFFF A := -A
inc A
lb1 tay return A
lda 2,S
sta 4,S
pla
sta 1,S
tya
rtl
end
****************************************************************
*
* void *aligned_alloc(size_t alignment, size_t size)
*
* Allocate memory with specified alignment.
*
* Inputs:
* alignment - alignment to use (only value allowed is 1)
* size - bytes of memory to allocate
*
* Outputs:
* Returns pointer to allocated memory, or NULL on error.
*
****************************************************************
*
aligned_alloc start
csubroutine (4:alignment,4:size),0
lda alignment check that alignment==1
dec a
ora alignment+2
beq good
stz size return NULL on error
stz size+2
lda #EINVAL
sta >errno
bra ret
good ph4 <size call malloc
jsl malloc
sta size
stx size+2
ret creturn 4:size
end
****************************************************************
*
* int atexit(func)
* void (*func)();
*
* This function is used to build a list of functions that will
* be called as part of the exit processing.
*
* Inputs:
* func - address of the function to call on exit
*
* Outputs:
* Returns 0 if successful, -1 if not.
*
****************************************************************
*
atexit start
ptr equ 1 work pointer
rval equ 5 return value
csubroutine (4:func),6
lda #-1 assume we will fail
sta rval assume we will fail
dec4 func we need the addr-1, not the addr
ph4 #8 get space for the record
jsl malloc
stx ptr+2
sta ptr
ora ptr+2 quit now if we failed
beq lb1
ldy #2 place the record in the exit list
lda >~EXITLIST
sta [ptr]
lda >~EXITLIST+2
sta [ptr],Y
lda ptr
sta >~EXITLIST
lda ptr+2
sta >~EXITLIST+2
iny place the function address in the record
iny
lda func
sta [ptr],Y
iny
iny
lda func+2
sta [ptr],Y
inc rval success...
lb1 creturn 2:rval
end
****************************************************************
*
* int at_quick_exit(func)
* void (*func)();
*
* This function is used to build a list of functions that will
* be called as part of the quick exit processing.
*
* Inputs:
* func - address of the function to call on quick exit
*
* Outputs:
* Returns 0 if successful, -1 if not.
*
****************************************************************
*
at_quick_exit start
ptr equ 1 work pointer
rval equ 5 return value
csubroutine (4:func),6
lda #-1 assume we will fail
sta rval assume we will fail
dec4 func we need the addr-1, not the addr
ph4 #8 get space for the record
jsl malloc
stx ptr+2
sta ptr
ora ptr+2 quit now if we failed
beq lb1
ldy #2 place the record in the exit list
lda >~QUICKEXITLIST
sta [ptr]
lda >~QUICKEXITLIST+2
sta [ptr],Y
lda ptr
sta >~QUICKEXITLIST
lda ptr+2
sta >~QUICKEXITLIST+2
iny place the function address in the record
iny
lda func
sta [ptr],Y
iny
iny
lda func+2
sta [ptr],Y
inc rval success...
lb1 creturn 2:rval
end
****************************************************************
*
* atof - convert a string to a float
*
* Inputs:
* str - pointer to the string
*
* Outputs:
* X-A - pointer to converted number
*
****************************************************************
*
atof start
ph4 #0 no pointer returned
lda 10,S pass the string addr on
pha
lda 10,S
pha
jsl strtod convert the string
tay fix the stack
lda 2,S
sta 6,S
pla
sta 3,S
pla
tya
rtl
end
****************************************************************
*
* atoi - convert a string to an int
* atol - convert a string to a long
*
* Inputs:
* str - pointer to the string
*
* Outputs:
* X-A - converted number
*
****************************************************************
*
atoi start
atol entry
ph2 #10 base 10
ph4 #0 no pointer returned
lda 12,S pass the string addr on
pha
lda 12,S
pha
jsl strtol convert the string
tay fix the stack
lda 2,S
sta 6,S
pla
sta 3,S
pla
tya
rtl
end
****************************************************************
*
* atoll - convert a string to a long long
*
* Inputs:
* str - pointer to the string
*
* Outputs:
* converted number
*
****************************************************************
*
atoll start
ph2 #10 base 10
ph4 #0 no pointer returned
lda 12,S pass the string addr on
pha
lda 12,S
pha note: x reg is unchanged
jsl strtoll convert the string
lda 2,S fix the stack
sta 6,S
pla
sta 3,S
pla
rtl
end
****************************************************************
*
* char *bsearch(key, base, count, size, compar)
* void *key, *base;
* size_t count, size;
* int (*compar)(const void *, const void *)
*
* Inputs:
* key - pointer to element to search for
* base - start address of the array to search
* count - # elements in the array
* size - size of each array element
* compar - function that compares array elements
*
* Outputs:
* Returns a pointer to the array element found; NULL if
* no match was found.
*
****************************************************************
*
bsearch start
left equ 1 left index
right equ 5 right index
test equ 9 test index
addr equ 13 address of array element of index test
csubroutine (4:key,4:base,4:count,4:size,4:compar),16
lda count if count is 0 then
ora count+2
jeq lb5 just return a null pointer
lda compar patch the call address
sta >jsl+1
lda compar+1
sta >jsl+2
stz left left = 0
stz left+2
sub4 count,#1,right right = count-1
lb1 clc test = (left+right)/2
lda left
adc right
sta test
lda left+2
adc right+2
lsr A
sta test+2
ror test
mul4 test,size,addr addr = test*size + base
add4 addr,base
ph4 <addr compare the array elements
ph4 <key
jsl jsl jsl
tax quit if *addr = *key
beq lb6
bmi lb2 if *key > *addr then
add4 test,#1,left left = test+1
bra lb3 else
lb2 sub4 test,#1,right right = test-1
lb3 lda right+2 loop if right >= left
bmi lb5
cmp left+2
bne lb4
lda right
cmp left
lb4 bge lb1
lb5 stz addr no match - return null
stz addr+2
lb6 creturn 4:addr
end
****************************************************************
*
* void call_once(short *flag, void (*func)(void));
*
* Call func once, if flag indicates it has not already been
* called. Do not return until the call to func has completed,
* either from this call_once invocation or another one.
*
* Inputs:
* flag - indicating if func was called already
* func - function to call once
*
* Note: This has synchronization suitable for multithreading,
* even though ORCA/C does not currently support it.
*
****************************************************************
*
call_once start
csubroutine (4:flag,4:func)
rtl_addr equ after-1
php test and set flag
sei
lda [flag]
bne nocall if not already set
inc a flag that call is in progress
sta [flag]
plp
ldy func call func
lda func+2
and #$00ff
ora #rtl_addr|8
pea rtl_addr|-8
pha
dey
phy
rtl
after lda #-1 flag that the call is done
sta [flag]
bra ret else
nocall plp
wait lda [flag] wait for any concurrent call to finish
bpl wait
ret creturn
end
****************************************************************
*
* div_t div(n,d)
* int n,d;
*
* Inputs:
* n - numerator
* d - denominator
*
* Outputs:
* div_t - contains result & remainder
*
****************************************************************
*
div start
addr equ 1
csubroutine (2:n,2:d),4
phb use local data
phk
plb
lda n do the divide
ldx d
jsl ~DIV2
sta div_t save the results
stx div_t+2
lda n if the numerator is negative then
bpl lb1
sub2 #0,div_t+2,div_t+2 make the remainder negative
lb1 lla addr,div_t return the address
plb
creturn 4:addr
div_t ds 4
end
****************************************************************
*
* void exit(int status);
*
* void _exit(int status);
*
* void _Exit(int status);
*
* void quick_exit(int status);
*
* Stop the program. Exit cleans up, _exit does not. Status
* is the status returned to the shell.
*
* Inputs:
* status - exit code
*
****************************************************************
*
exit start
jsr ~EXIT
_exit entry
_Exit entry
lda 4,S
brl ~C_QUIT
end
quick_exit start
jsr ~QUICKEXIT
lda 4,S
brl ~C_QUIT
end
****************************************************************
*
* void free_aligned_sized(void *ptr, size_t alignment, size_t size);
*
* Free an allocation from aligned_alloc, given the pointer
* and the requested alignment and allocation size.
*
****************************************************************
*
free_aligned_sized start
pla remove alignment and size from stack
sta 7,s
pla
sta 7,s
plx
pla
xba
sta 6,s
txa
sta 5,s
jml free call free
end
****************************************************************
*
* void free_sized(void *ptr, size_t size);
*
* Free an allocation, given the pointer and allocation size.
*
****************************************************************
*
free_sized start
lda 6,s remove size from stack
sta 10,s
plx
ply
pla
sta 3,s
pla
phy
phx
jml free call free
end
****************************************************************
*
* char *getenv(const char *name)
*
* Returns a pointer to a shell variable. If the shell variable
* has no value, a null is returned.
*
* Inputs:
* namePtr - pointer to the name of the shell variable
*
* Outputs:
* Returns a pointer to the shell variable
*
****************************************************************
*
getenv start
ptr equ 1 pointer to the shell variable
csubroutine (4:namePtr),4
phb use local addressing
phk
plb
lla ptr,0 initialize the pointer to null
short I,M copy the variable name to the buffer
ldy #0
lb1 lda [namePtr],Y
beq lb2
iny
sta name,Y
bne lb1
dey
lb2 sty name
long I,M
Read_Variable rdRec read the shell variable
bcs lb3 if there was no error then
lda var if the variable was set then
and #$00FF
beq lb3
short I,M set the null terminator
ldx var
stz var+1,X
long I,M
lla ptr,var+1 set the pointer to return
lb3 plb restore B
creturn 4:ptr
rdRec dc a4'name,var' read variable record
name ds 256 shell variable name
var ds 257 shell variable value
end
****************************************************************
*
* long labs(long i)
*
* Return the absolute value of i.
*
* Inputs:
* i - argument
*
* Outputs:
* Returns abs(i).
*
****************************************************************
*
labs start
csubroutine (4:i),0
lda i+2
bpl lb1
sub4 #0,i,i
lb1 creturn 4:i
end
****************************************************************
*
* long long llabs(long long i)
*
* Return the absolute value of i.
*
* Inputs:
* i - argument
*
* Outputs:
* Returns abs(i).
*
****************************************************************
*
llabs start
imaxabs entry
retptr equ 1
csubroutine (8:i),4
stx retptr
stz retptr+2
ph8 <i
jsl ~ABS8
pla
sta [retptr]
ldy #2
pla
sta [retptr],y
iny
iny
pla
sta [retptr],y
iny
iny
pla
sta [retptr],y
creturn
end
****************************************************************
*
* ldiv_t ldiv(n,d)
* long n,d;
*
* Inputs:
* n - numerator
* d - denominator
*
* Outputs:
* ldiv_t - contains result & remainder
*
****************************************************************
*
ldiv start
addr equ 1
csubroutine (4:n,4:d),4
phb use local addressing
phk
plb
ph4 <n do the divide
ph4 <d
jsl ~DIV4
pl4 div_t
pl4 div_t+4
lda n+2 if the numerator is negative then
bpl lb1
sub4 #0,div_t+4,div_t+4 make the remainder negative
lb1 lla addr,div_t return the result
plb
creturn 4:addr
div_t ds 8
end
****************************************************************
*
* lldiv_t lldiv(long long n, long long d)
*
* Inputs:
* n - numerator
* d - denominator
*
* Outputs:
* lldiv_t - contains result & remainder
*
****************************************************************
*
lldiv start
imaxdiv entry
addr equ 1
csubroutine (8:n,8:d),4
phb use local addressing
phk
plb
ph8 <n do the divide
ph8 <d
jsl ~CDIV8
pl8 lldiv_t
pl8 lldiv_t+8
lla addr,lldiv_t return the result
plb
creturn 4:addr
lldiv_t ds 16
end
****************************************************************
*
* int mblen(const char *s, size_t n)
*
* Inputs:
* s - NULL or pointer to character
* n - maximum number of bytes to inspect
*
* Outputs:
* If s is NULL, returns 0, indicating encodings are not
* state-dependent. Otherwise, returns 0 if s points to a
* null character, -1 if the next n or fewer bytes do not
* form a valid character, or the number of bytes forming
* a valid character.
*
* Note: This implementation assumes we do not support actual
* multi-byte or state-dependent character encodings.
*
****************************************************************
*
mblen start
csubroutine (4:s,4:n)
ldx #0
lda s if s == NULL
ora s+2
beq ret return 0
lda n if n == 0
ora n+2
bne readchar
dex return -1
bra ret
readchar lda [s] if *s == '\0'
and #$00FF
beq ret return 0
inx else return 1
ret stx n
creturn 2:n
end
****************************************************************
*
* size_t memalignment(const void *p)
*
* Inputs:
* p - a pointer
*
* Outputs:
* The maximum alignment satisfied by p.
*
****************************************************************
*
memalignment start
csubroutine (4:p)
lda p compute alignment = p & -p
beq lb1
eor #$ffff
inc a
and p
sta p
stz p+2
bra ret
lb1 lda p+2
eor #$ffff
inc a
and p+2
sta p+2
ret creturn 4:p
end
****************************************************************
*
* void qsort(base, count, size, compar)
* void *base;
* size_t count, size;
* int (*compar)(const void *, const void *)
*
* Inputs:
* base - start address of the array to sort
* count - # elements in the array
* size - size of each array element
* compar - function that compares array elements
*
* Outputs:
* The array is sorted on exit.
*
****************************************************************
*
qsort start
csubroutine (4:base,4:count,4:size,4:compar),0
lda count nothing to do if count is 0
ora count+2
beq done
phb
phk
plb
dec4 count set count to the addr of the last entry
mul4 count,size
add4 count,base
move4 size,lsize save size in a global var
lda compar set the jsl addresses
sta jsl1+1
sta jsl2+1
lda compar+1
sta jsl1+2
sta jsl2+2
plb
ph4 <count do the sort
ph4 <base
jsl rsort
done creturn
end
****************************************************************
*
* rand - get a random number
*
* Outputs:
* A - random number
*
****************************************************************
*
rand start
lda >~srand if no initialization then
bne lb1
ph2 #1 initialize with a value of 1
jsl srand
lb1 jsl ~RANX find the random number
lda >~SEED
and #$7FFF
rtl
~srand entry
dc i'0'
end
****************************************************************
*
* rsort - recursive sort for qsort
*
* Inputs:
* first - first array element to sort
* last - last array element to sort
*
****************************************************************
*
rsort private
left equ 1 left address
right equ 5 right address
csubroutine (4:first,4:last),8
sr0 phb
phk
plb
lda last+2 if last <= first then quit
bmi sr1a
cmp first+2
bne sr1
lda last
cmp first
sr1 bgt sr1b
sr1a plb
creturn
sr1b move4 last,right right = last
move4 first,left left = first
bra sr3
sr2 add4 left,lsize inc left until *left >= *last
sr3 plb
ph4 <last
ph4 <left
jsl1 entry
jsl jsl1
phb
phk
plb
tax
bmi sr2
sr4 lda right quit if right = first
cmp first
bne sr4a
lda right+2
cmp first+2
beq sr4b
sr4a sub4 right,lsize dec right until *right <= *last
plb
ph4 <last
ph4 <right
jsl2 entry
jsl jsl2
phb
phk
plb
dec A
bpl sr4
sr4b ph4 <left swap left/right entries
ph4 <right
jsr swap
lda left+2 loop if left < right
cmp right+2
bne sr5
lda left
cmp right
sr5 blt sr2
ph4 <right swap left/right entries
ph4 <left
jsr swap
ph4 <left swap left/last entries
ph4 <last
jsr swap
sub4 left,lsize,right calculate bounds of subarrays
add4 left,lsize (first..right and left..last)
add4 first,last,mid calculate midpoint of range being sorted
lsr mid+2
ror mid
cmpl right,mid if right < mid then
bge sr6
plb
ph4 <right sort left subarray recursively
ph4 <first
jsl rsort
move4 left,first sort right subarray via tail call
brl sr0
sr6 plb else
ph4 <last sort right subarray recursively
ph4 <left
jsl rsort
move4 right,last sort left subarray via tail call
brl sr0
;
; swap - swap two entries
;
l equ 3 left entry
r equ 7 right entry
swap tsc set up addressing
phd
tcd
lda lsize+2 move 64K chunks
beq sw2
sta banks
ldy #0
sw1 lda [l],Y
tax
lda [r],Y
sta [l],Y
txa
sta [r],Y
dey