aboutsummaryrefslogtreecommitdiffstats
path: root/c/libc.s
blob: a26042e97bc29b571067feeb849003a728bd93b3 (plain) (blame)
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
	.module libc
	.globl _writes, _putc, _puts, _nl, _strlen, _readc, _sexit, _getpid, _getargs
	.globl _open, _close, _fgetc, _fputc, _flist, _fremove
	;; SDCC sm83 sdcccall(1): 1st arg -> A (byte) or DE (pointer); ret A / BC.
	;; rst $30 (the trap) clobbers BC/DE/HL; SDCC treats them caller-saved, so
	;; wrappers need not preserve them - but we compute cleanly regardless.
	.area _CODE

	;; void writes(const char *buf /*DE*/, unsigned char len /*A*/)
_writes:
	ld	b, a
	ld	c, #3			; SYS_WRITE
	rst	#0x30
	ret

	;; char *getargs(void) -> BC : the arg string (command line past word 1).
	;; The shell leaves "cmd\0args\0" at 0xA000, inherited by the child via fork.
_getargs:
	ld	hl, #0xa000
1$:	ld	a, (hl)
	inc	hl
	or	a
	jr	nz, 1$
	ld	b, h
	ld	c, l
	ret

	;; void putc(char c /*A*/)
_putc:
	ld	(0xff01), a
	ld	a, #0x81
	ld	(0xff02), a
	ret

	;; void puts(const char *s /*DE*/) - write until NUL (no newline)
_puts:
1$:	ld	a, (de)
	or	a
	ret	z
	ld	(0xff01), a
	ld	a, #0x81
	ld	(0xff02), a
	inc	de
	jr	1$

	;; void nl(void) - CRLF
_nl:
	ld	a, #0x0d
	ld	(0xff01), a
	ld	a, #0x81
	ld	(0xff02), a
	ld	a, #0x0a
	ld	(0xff01), a
	ld	a, #0x81
	ld	(0xff02), a
	ret

	;; unsigned char strlen(const char *s /*DE*/) -> A
_strlen:
	ld	c, #0
1$:	ld	a, (de)
	or	a
	jr	z, 2$
	inc	c
	inc	de
	jr	1$
2$:	ld	a, c
	ret

	;; char readc(void) -> A   (4 = EOF)
_readc:
	ld	c, #2			; SYS_READ
	rst	#0x30
	ret

	;; void sexit(unsigned char code /*A*/) - never returns
_sexit:
	ld	b, a
	ld	c, #0			; SYS_EXIT
	rst	#0x30
	ret

	;; unsigned char getpid(void) -> A
_getpid:
	ld	c, #8			; SYS_GETPID
	rst	#0x30
	ret

	;; unsigned char open(const char *name /*DE*/, unsigned char mode /*A*/) -> A(fd)
_open:
	ld	b, a			; B = mode
	ld	c, #4			; SYS_OPEN
	rst	#0x30
	ret

	;; void close(unsigned char fd /*A*/)
_close:
	ld	b, a
	ld	c, #5			; SYS_CLOSE
	rst	#0x30
	ret

	;; int fgetc(unsigned char fd /*A*/) -> BC (byte, or -1 at EOF)
_fgetc:
	ld	b, a
	ld	c, #12			; SYS_GETB
	rst	#0x30
	jr	c, 1$
	ld	c, a
	ld	b, #0
	ret
1$:	ld	bc, #0xffff
	ret

	;; void fputc(unsigned char fd /*A*/, char ch /*E*/)
	;; byte goes to the kernel in E (the trap clobbers A during dispatch)
_fputc:
	ld	b, a			; B = fd  (ch stays in E)
	ld	c, #13			; SYS_PUTB
	rst	#0x30
	ret

	;; unsigned char flist(unsigned char slot /*A*/, char *namebuf /*DE*/) -> A
_flist:
	ld	b, a
	ld	c, #14			; SYS_LIST
	rst	#0x30
	ret

	;; void fremove(const char *name /*DE*/)
_fremove:
	ld	c, #15			; SYS_REMOVE
	rst	#0x30
	ret