diff options
Diffstat (limited to 'c')
| -rw-r--r-- | c/gbos.h | 2 | ||||
| -rw-r--r-- | c/libc.s | 15 | ||||
| -rw-r--r-- | c/necho.c | 44 |
3 files changed, 60 insertions, 1 deletions
@@ -13,6 +13,8 @@ char readc(void); /* one input byte, EOF at end */ void sexit(unsigned char code); unsigned char getpid(void); unsigned char pipe(void); /* -> read fd; write end is read+1 */ +void ssend(unsigned char b); /* link-port: transmit one byte */ +unsigned char srecv(void); /* link-port: receive one byte (blocks) */ /* helpers (c/libc.c) */ void putu(unsigned int n); /* print decimal */ @@ -1,6 +1,6 @@ .module libc .globl _writes, _putc, _puts, _nl, _strlen, _readc, _sexit, _getpid, _getargs - .globl _open, _close, _fgetc, _fputc, _flist, _fremove, _pipe + .globl _open, _close, _fgetc, _fputc, _flist, _fremove, _pipe, _ssend, _srecv ;; 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. @@ -92,6 +92,19 @@ _pipe: rst #0x30 ret + ;; void ssend(unsigned char b /*A*/) -- link-port transmit +_ssend: + ld e, a + ld c, #29 ; SYS_SSEND + rst #0x30 + ret + + ;; unsigned char srecv(void) -> A -- link-port receive (blocks) +_srecv: + ld c, #30 ; SYS_SRECV + rst #0x30 + ret + ;; unsigned char open(const char *name /*DE*/, unsigned char mode /*A*/) -> A(fd) _open: ld b, a ; B = mode diff --git a/c/necho.c b/c/necho.c new file mode 100644 index 0000000..b9dc9d5 --- /dev/null +++ b/c/necho.c @@ -0,0 +1,44 @@ +#include "gbos.h" +/* necho: send a SLIP-framed packet over the link port and print the reply. + Talks to a host gateway (tools/gateway.py) that echoes frames back. */ + +#define SLIP_END 0xC0 +#define SLIP_ESC 0xDB +#define SLIP_ESC_END 0xDC +#define SLIP_ESC_ESC 0xDD + +static void slip_send(const char *buf, unsigned char len) { + unsigned char i, c; + ssend(SLIP_END); + for (i = 0; i < len; i++) { + c = (unsigned char)buf[i]; + if (c == SLIP_END) { ssend(SLIP_ESC); ssend(SLIP_ESC_END); } + else if (c == SLIP_ESC) { ssend(SLIP_ESC); ssend(SLIP_ESC_ESC); } + else ssend(c); + } + ssend(SLIP_END); +} + +static unsigned char slip_recv(char *buf, unsigned char max) { + unsigned char len = 0, c; + for (;;) { + c = srecv(); + if (c == SLIP_END) { if (len) return len; continue; } + if (c == SLIP_ESC) { + c = srecv(); + if (c == SLIP_ESC_END) c = SLIP_END; + else if (c == SLIP_ESC_ESC) c = SLIP_ESC; + } + if (len < max) buf[len++] = (char)c; + } +} + +void main(void) { + char buf[64]; + unsigned char len; + puts("net: send 'hello from gbos'"); nl(); + slip_send("hello from gbos", 15); + len = slip_recv(buf, 63); + buf[len] = 0; + puts("net: recv '"); puts(buf); puts("'"); nl(); +} |
