2019-04-13 16:01:20 -04:00
|
|
|
; shell
|
|
|
|
;
|
2019-04-14 15:18:41 -04:00
|
|
|
; Runs a shell over a block device interface.
|
2019-04-13 16:01:20 -04:00
|
|
|
|
2019-05-31 14:50:43 -04:00
|
|
|
; The shell spits a welcome prompt, wait for input and compare the first 4 chars
|
|
|
|
; of the input with a command table and call the appropriate routine if it's
|
|
|
|
; found, an error if it's not.
|
2019-04-13 22:39:28 -04:00
|
|
|
;
|
2019-05-31 14:50:43 -04:00
|
|
|
; To determine the correct routine to call we first go through cmds in
|
|
|
|
; shellCmdTbl. This means that we first go through internal cmds, then cmds
|
|
|
|
; "grafted" by glue code.
|
|
|
|
;
|
|
|
|
; If the command isn't found, SHELL_CMDHOOK is called, which should set A to
|
|
|
|
; zero if it executes something. Otherwise, SHELL_ERR_UNKNOWN_CMD will be
|
|
|
|
; returned.
|
2019-04-13 22:39:28 -04:00
|
|
|
;
|
|
|
|
; See constants below for error codes.
|
2019-04-14 11:22:45 -04:00
|
|
|
;
|
|
|
|
; All numerical values in the Collapse OS shell are represented and parsed in
|
|
|
|
; hexadecimal form, without prefix or suffix.
|
2019-04-13 16:01:20 -04:00
|
|
|
|
2019-04-22 22:41:37 -04:00
|
|
|
; *** REQUIREMENTS ***
|
2019-06-02 14:05:20 -04:00
|
|
|
; err
|
|
|
|
; core
|
2019-05-17 09:33:20 -04:00
|
|
|
; parse
|
2019-04-22 22:41:37 -04:00
|
|
|
; stdio
|
|
|
|
|
2019-04-14 14:24:29 -04:00
|
|
|
; *** DEFINES ***
|
2019-04-15 16:53:11 -04:00
|
|
|
; SHELL_EXTRA_CMD_COUNT: Number of extra cmds to be expected after the regular
|
|
|
|
; ones. See comment in COMMANDS section for details.
|
2019-04-14 14:24:29 -04:00
|
|
|
; SHELL_RAMSTART
|
|
|
|
|
2019-04-13 16:01:20 -04:00
|
|
|
; *** CONSTS ***
|
|
|
|
|
2019-04-13 22:39:28 -04:00
|
|
|
; number of entries in shellCmdTbl
|
2019-06-02 10:50:18 -04:00
|
|
|
.equ SHELL_CMD_COUNT 6+SHELL_EXTRA_CMD_COUNT
|
2019-04-13 22:39:28 -04:00
|
|
|
|
2019-04-14 11:11:13 -04:00
|
|
|
; *** VARIABLES ***
|
2019-04-14 23:14:07 -04:00
|
|
|
; Memory address that the shell is currently "pointing at" for peek, load, call
|
2019-04-15 20:42:12 -04:00
|
|
|
; operations. Set with mptr.
|
2019-05-17 19:36:32 -04:00
|
|
|
.equ SHELL_MEM_PTR SHELL_RAMSTART
|
2019-04-14 13:56:04 -04:00
|
|
|
|
2019-04-14 23:14:07 -04:00
|
|
|
; Places where we store arguments specifiers and where resulting values are
|
|
|
|
; written to after parsing.
|
2019-05-17 19:36:32 -04:00
|
|
|
.equ SHELL_CMD_ARGS SHELL_MEM_PTR+2
|
2019-04-14 23:14:07 -04:00
|
|
|
|
2019-05-31 14:50:43 -04:00
|
|
|
; Pointer to a hook to call when a cmd name isn't found
|
2019-07-13 09:19:01 -04:00
|
|
|
.equ SHELL_CMDHOOK SHELL_CMD_ARGS+PARSE_ARG_MAXCOUNT
|
2019-05-31 14:50:43 -04:00
|
|
|
|
2019-07-02 13:11:37 -04:00
|
|
|
; Pointer to a routine to call at each shell loop interation
|
|
|
|
.equ SHELL_LOOPHOOK SHELL_CMDHOOK+2
|
|
|
|
.equ SHELL_RAMEND SHELL_LOOPHOOK+2
|
2019-04-14 11:11:13 -04:00
|
|
|
|
2019-04-13 22:39:28 -04:00
|
|
|
; *** CODE ***
|
2019-04-13 16:01:20 -04:00
|
|
|
shellInit:
|
2019-04-14 11:11:13 -04:00
|
|
|
xor a
|
|
|
|
ld (SHELL_MEM_PTR), a
|
2019-05-20 07:38:15 -04:00
|
|
|
ld (SHELL_MEM_PTR+1), a
|
2019-05-31 14:50:43 -04:00
|
|
|
ld hl, noop
|
|
|
|
ld (SHELL_CMDHOOK), hl
|
2019-07-02 13:11:37 -04:00
|
|
|
ld (SHELL_LOOPHOOK), hl
|
2019-04-14 11:11:13 -04:00
|
|
|
|
2019-04-14 14:53:16 -04:00
|
|
|
; print welcome
|
|
|
|
ld hl, .welcome
|
2019-07-13 09:19:01 -04:00
|
|
|
jp printstr
|
2019-04-13 16:01:20 -04:00
|
|
|
|
2019-04-14 14:53:16 -04:00
|
|
|
.welcome:
|
|
|
|
.db "Collapse OS", ASCII_CR, ASCII_LF, "> ", 0
|
2019-04-13 22:39:28 -04:00
|
|
|
|
2019-04-16 11:25:54 -04:00
|
|
|
; Inifite loop that processes input. Because it's infinite, you should jump
|
|
|
|
; to it rather than call it. Saves two precious bytes in the stack.
|
2019-04-13 16:01:20 -04:00
|
|
|
shellLoop:
|
2019-07-02 13:11:37 -04:00
|
|
|
; First, call the loop hook
|
|
|
|
ld ix, (SHELL_LOOPHOOK)
|
|
|
|
call callIX
|
|
|
|
; Then, let's wait until something is typed.
|
2019-07-13 09:19:01 -04:00
|
|
|
call stdioReadC
|
|
|
|
jr nz, shellLoop ; not done? loop
|
|
|
|
; We're done. Process line.
|
2019-04-14 14:53:16 -04:00
|
|
|
call printcrlf
|
2019-07-13 09:19:01 -04:00
|
|
|
call stdioGetLine
|
2019-04-14 13:56:04 -04:00
|
|
|
call shellParse
|
2019-04-14 14:53:16 -04:00
|
|
|
ld hl, .prompt
|
|
|
|
call printstr
|
2019-04-14 11:22:45 -04:00
|
|
|
jr shellLoop
|
|
|
|
|
2019-04-14 14:53:16 -04:00
|
|
|
.prompt:
|
|
|
|
.db "> ", 0
|
|
|
|
|
2019-04-13 22:39:28 -04:00
|
|
|
; Parse command (null terminated) at HL and calls it
|
|
|
|
shellParse:
|
|
|
|
push af
|
|
|
|
push bc
|
|
|
|
push de
|
2019-04-14 11:11:13 -04:00
|
|
|
push hl
|
2019-04-14 17:01:28 -04:00
|
|
|
push ix
|
2019-04-13 22:39:28 -04:00
|
|
|
|
|
|
|
ld de, shellCmdTbl
|
|
|
|
ld a, SHELL_CMD_COUNT
|
|
|
|
ld b, a
|
|
|
|
|
|
|
|
.loop:
|
2019-04-15 15:08:00 -04:00
|
|
|
push de ; we need to keep that table entry around...
|
|
|
|
call intoDE ; Jump from the table entry to the cmd addr.
|
2019-04-13 22:39:28 -04:00
|
|
|
ld a, 4 ; 4 chars to compare
|
|
|
|
call strncmp
|
2019-04-15 15:08:00 -04:00
|
|
|
pop de
|
2019-04-13 22:39:28 -04:00
|
|
|
jr z, .found
|
2019-04-15 15:08:00 -04:00
|
|
|
inc de
|
2019-04-15 16:53:11 -04:00
|
|
|
inc de
|
2019-04-13 22:39:28 -04:00
|
|
|
djnz .loop
|
|
|
|
|
|
|
|
; exhausted loop? not found
|
|
|
|
ld a, SHELL_ERR_UNKNOWN_CMD
|
2019-05-31 14:50:43 -04:00
|
|
|
; Before erroring out, let's try SHELL_HOOK.
|
|
|
|
ld ix, (SHELL_CMDHOOK)
|
|
|
|
call callIX
|
|
|
|
jr z, .end ; oh, not an error!
|
|
|
|
; still an error. Might be different than SHELL_ERR_UNKNOWN_CMD though.
|
|
|
|
; maybe a routine was called, but errored out.
|
2019-04-15 20:38:25 -04:00
|
|
|
jr .error
|
2019-04-13 22:39:28 -04:00
|
|
|
|
|
|
|
.found:
|
2019-04-15 15:08:00 -04:00
|
|
|
; we found our command. DE points to its table entry. Now, let's parse
|
|
|
|
; our args.
|
|
|
|
call intoDE ; Jump from the table entry to the cmd addr.
|
|
|
|
|
|
|
|
; advance the HL pointer to the beginning of the args.
|
2019-04-23 09:37:22 -04:00
|
|
|
ld a, ' '
|
|
|
|
call findchar
|
2019-06-02 14:46:07 -04:00
|
|
|
or a ; end of string? don't increase HL
|
|
|
|
jr z, .noargs
|
|
|
|
inc hl ; char after space
|
2019-04-15 15:08:00 -04:00
|
|
|
|
2019-06-02 14:46:07 -04:00
|
|
|
.noargs:
|
2019-04-14 23:14:07 -04:00
|
|
|
; Now, let's have DE point to the argspecs
|
2019-04-14 11:11:13 -04:00
|
|
|
ld a, 4
|
|
|
|
call addDE
|
2019-04-15 15:08:00 -04:00
|
|
|
|
2019-04-14 23:14:07 -04:00
|
|
|
; We're ready to parse args
|
2019-06-02 14:05:20 -04:00
|
|
|
ld ix, SHELL_CMD_ARGS
|
|
|
|
call parseArgs
|
2019-05-31 14:50:43 -04:00
|
|
|
or a ; cp 0
|
2019-04-14 23:14:07 -04:00
|
|
|
jr nz, .parseerror
|
|
|
|
|
|
|
|
; Args parsed, now we can load the routine address and call it.
|
|
|
|
; let's have DE point to the jump line
|
2019-06-02 14:05:20 -04:00
|
|
|
ld hl, SHELL_CMD_ARGS
|
|
|
|
ld a, PARSE_ARG_MAXCOUNT
|
2019-04-14 23:14:07 -04:00
|
|
|
call addDE
|
2019-05-20 07:38:15 -04:00
|
|
|
push de \ pop ix
|
2019-04-14 23:14:07 -04:00
|
|
|
; Ready to roll!
|
2019-04-14 17:01:28 -04:00
|
|
|
call callIX
|
2019-05-31 14:50:43 -04:00
|
|
|
or a ; cp 0
|
2019-04-15 20:38:25 -04:00
|
|
|
jr nz, .error ; if A is non-zero, we have an error
|
2019-04-14 23:14:07 -04:00
|
|
|
jr .end
|
2019-04-13 22:39:28 -04:00
|
|
|
|
2019-04-14 23:14:07 -04:00
|
|
|
.parseerror:
|
|
|
|
ld a, SHELL_ERR_BAD_ARGS
|
2019-04-15 20:38:25 -04:00
|
|
|
.error:
|
2019-04-14 23:14:07 -04:00
|
|
|
call shellPrintErr
|
2019-04-13 22:39:28 -04:00
|
|
|
.end:
|
2019-04-14 17:01:28 -04:00
|
|
|
pop ix
|
2019-04-14 11:11:13 -04:00
|
|
|
pop hl
|
2019-04-13 22:39:28 -04:00
|
|
|
pop de
|
|
|
|
pop bc
|
|
|
|
pop af
|
|
|
|
ret
|
|
|
|
|
2019-04-14 11:11:13 -04:00
|
|
|
; Print the error code set in A (in hex)
|
2019-04-13 22:39:28 -04:00
|
|
|
shellPrintErr:
|
|
|
|
push af
|
|
|
|
push hl
|
|
|
|
|
|
|
|
ld hl, .str
|
2019-04-13 16:01:20 -04:00
|
|
|
call printstr
|
2019-04-16 08:35:44 -04:00
|
|
|
call printHex
|
2019-04-13 16:01:20 -04:00
|
|
|
call printcrlf
|
2019-04-13 22:39:28 -04:00
|
|
|
|
|
|
|
pop hl
|
|
|
|
pop af
|
|
|
|
ret
|
|
|
|
|
|
|
|
.str:
|
|
|
|
.db "ERR ", 0
|
|
|
|
|
|
|
|
; *** COMMANDS ***
|
2019-06-02 14:05:20 -04:00
|
|
|
; A command is a 4 char names, followed by a PARSE_ARG_MAXCOUNT bytes of
|
2019-04-15 15:08:00 -04:00
|
|
|
; argument specs, followed by the routine. Then, a simple table of addresses
|
|
|
|
; is compiled in a block and this is what is iterated upon when we want all
|
|
|
|
; available commands.
|
|
|
|
;
|
2019-06-02 14:05:20 -04:00
|
|
|
; Format: 4 bytes name followed by PARSE_ARG_MAXCOUNT bytes specifiers,
|
2019-04-15 15:08:00 -04:00
|
|
|
; followed by 3 bytes jump. fill names with zeroes
|
|
|
|
;
|
2019-04-14 23:14:07 -04:00
|
|
|
; When these commands are called, HL points to the first byte of the
|
|
|
|
; parsed command args.
|
2019-04-15 16:53:11 -04:00
|
|
|
;
|
2019-04-15 20:38:25 -04:00
|
|
|
; If the command is a success, it should set A to zero. If the command results
|
|
|
|
; in an error, it should set an error code in A.
|
|
|
|
;
|
2019-04-15 16:53:11 -04:00
|
|
|
; Extra commands: Other parts might define new commands. You can add these
|
|
|
|
; commands to your shell. First, set SHELL_EXTRA_CMD_COUNT to
|
|
|
|
; the number of extra commands to add, then add a ".dw"
|
|
|
|
; directive *just* after your '#include "shell.asm"'. Voila!
|
|
|
|
;
|
2019-04-15 15:08:00 -04:00
|
|
|
|
2019-04-14 11:54:18 -04:00
|
|
|
; Set memory pointer to the specified address (word).
|
2019-04-15 20:42:12 -04:00
|
|
|
; Example: mptr 01fe
|
|
|
|
shellMptrCmd:
|
|
|
|
.db "mptr", 0b011, 0b001, 0
|
|
|
|
shellMptr:
|
2019-04-14 11:11:13 -04:00
|
|
|
push hl
|
|
|
|
|
2019-04-16 08:35:44 -04:00
|
|
|
; reminder: z80 is little-endian
|
2019-04-14 23:14:07 -04:00
|
|
|
ld a, (hl)
|
|
|
|
ld (SHELL_MEM_PTR+1), a
|
|
|
|
inc hl
|
|
|
|
ld a, (hl)
|
|
|
|
ld (SHELL_MEM_PTR), a
|
2019-04-14 11:11:13 -04:00
|
|
|
|
2019-04-16 08:35:44 -04:00
|
|
|
ld hl, (SHELL_MEM_PTR)
|
|
|
|
ld a, h
|
|
|
|
call printHex
|
|
|
|
ld a, l
|
|
|
|
call printHex
|
2019-04-14 11:11:13 -04:00
|
|
|
call printcrlf
|
|
|
|
|
|
|
|
pop hl
|
2019-04-15 20:38:25 -04:00
|
|
|
xor a
|
2019-04-13 16:01:20 -04:00
|
|
|
ret
|
2019-04-13 22:39:28 -04:00
|
|
|
|
2019-04-14 11:11:13 -04:00
|
|
|
|
2019-06-17 08:18:28 -04:00
|
|
|
; peek the number of bytes specified by argument where memory pointer points to
|
|
|
|
; and display their value. If 0 is specified, 0x100 bytes are peeked.
|
2019-04-14 11:54:18 -04:00
|
|
|
;
|
|
|
|
; Example: peek 2 (will print 2 bytes)
|
2019-04-15 15:08:00 -04:00
|
|
|
shellPeekCmd:
|
2019-06-17 08:18:28 -04:00
|
|
|
.db "peek", 0b001, 0, 0
|
2019-04-13 22:39:28 -04:00
|
|
|
shellPeek:
|
2019-04-14 11:54:18 -04:00
|
|
|
push bc
|
2019-04-14 11:11:13 -04:00
|
|
|
push hl
|
|
|
|
|
2019-04-14 23:14:07 -04:00
|
|
|
ld a, (hl)
|
|
|
|
ld b, a
|
2019-04-14 11:11:13 -04:00
|
|
|
ld hl, (SHELL_MEM_PTR)
|
2019-04-14 11:54:18 -04:00
|
|
|
.loop: ld a, (hl)
|
2019-04-16 08:35:44 -04:00
|
|
|
call printHex
|
2019-04-14 11:54:18 -04:00
|
|
|
inc hl
|
|
|
|
djnz .loop
|
2019-04-14 11:11:13 -04:00
|
|
|
call printcrlf
|
|
|
|
|
2019-04-14 11:54:18 -04:00
|
|
|
.end:
|
2019-04-14 11:11:13 -04:00
|
|
|
pop hl
|
2019-04-14 11:54:18 -04:00
|
|
|
pop bc
|
2019-04-15 20:38:25 -04:00
|
|
|
xor a
|
2019-04-13 22:39:28 -04:00
|
|
|
ret
|
|
|
|
|
2019-06-17 08:18:28 -04:00
|
|
|
; poke specified number of bytes where memory pointer points and set them to
|
|
|
|
; bytes typed through stdioGetC. Blocks until all bytes have been fetched.
|
2019-06-02 10:50:18 -04:00
|
|
|
shellPokeCmd:
|
2019-06-17 08:18:28 -04:00
|
|
|
.db "poke", 0b001, 0, 0
|
2019-06-02 10:50:18 -04:00
|
|
|
shellPoke:
|
2019-04-14 14:53:16 -04:00
|
|
|
push bc
|
|
|
|
push hl
|
|
|
|
|
2019-04-14 23:14:07 -04:00
|
|
|
ld a, (hl)
|
2019-04-14 14:53:16 -04:00
|
|
|
ld b, a
|
|
|
|
ld hl, (SHELL_MEM_PTR)
|
2019-06-02 10:50:18 -04:00
|
|
|
.loop: call stdioGetC
|
|
|
|
jr nz, .loop ; nothing typed? loop
|
2019-04-14 14:53:16 -04:00
|
|
|
ld (hl), a
|
|
|
|
inc hl
|
|
|
|
djnz .loop
|
2019-04-22 16:01:31 -04:00
|
|
|
|
|
|
|
pop hl
|
|
|
|
pop bc
|
|
|
|
xor a
|
|
|
|
ret
|
|
|
|
|
2019-04-14 17:01:28 -04:00
|
|
|
; Calls the routine where the memory pointer currently points. This can take two
|
|
|
|
; parameters, A and HL. The first one is a byte, the second, a word. These are
|
|
|
|
; the values that A and HL are going to be set to just before calling.
|
|
|
|
; Example: run 42 cafe
|
2019-04-15 15:08:00 -04:00
|
|
|
shellCallCmd:
|
|
|
|
.db "call", 0b101, 0b111, 0b001
|
2019-04-14 17:01:28 -04:00
|
|
|
shellCall:
|
|
|
|
push hl
|
2019-04-14 23:14:07 -04:00
|
|
|
push ix
|
2019-04-14 17:01:28 -04:00
|
|
|
|
|
|
|
; Let's recap here. At this point, we have:
|
|
|
|
; 1. The address we want to execute in (SHELL_MEM_PTR)
|
2019-04-14 23:14:07 -04:00
|
|
|
; 2. our A arg as the first byte of (HL)
|
|
|
|
; 2. our HL arg as (HL+1) and (HL+2)
|
2019-04-14 17:01:28 -04:00
|
|
|
; Ready, set, go!
|
2019-05-17 19:36:32 -04:00
|
|
|
ld ix, (SHELL_MEM_PTR)
|
2019-04-14 23:14:07 -04:00
|
|
|
ld a, (hl)
|
|
|
|
ex af, af'
|
|
|
|
inc hl
|
|
|
|
ld a, (hl)
|
|
|
|
exx
|
|
|
|
ld h, a
|
|
|
|
exx
|
|
|
|
inc hl
|
|
|
|
ld a, (hl)
|
|
|
|
exx
|
|
|
|
ld l, a
|
|
|
|
ex af, af'
|
2019-04-14 17:01:28 -04:00
|
|
|
call callIX
|
|
|
|
|
|
|
|
.end:
|
2019-04-14 23:14:07 -04:00
|
|
|
pop ix
|
2019-04-14 17:01:28 -04:00
|
|
|
pop hl
|
2019-04-15 20:38:25 -04:00
|
|
|
xor a
|
2019-04-14 17:01:28 -04:00
|
|
|
ret
|
|
|
|
|
2019-05-02 15:18:47 -04:00
|
|
|
shellIORDCmd:
|
|
|
|
.db "iord", 0b001, 0, 0
|
|
|
|
push bc
|
|
|
|
ld a, (hl)
|
|
|
|
ld c, a
|
|
|
|
in a, (c)
|
|
|
|
call printHex
|
|
|
|
xor a
|
|
|
|
pop bc
|
|
|
|
ret
|
|
|
|
|
|
|
|
shellIOWRCmd:
|
|
|
|
.db "iowr", 0b001, 0b001, 0
|
|
|
|
push bc
|
|
|
|
ld a, (hl)
|
|
|
|
ld c, a
|
|
|
|
inc hl
|
|
|
|
ld a, (hl)
|
|
|
|
out (c), a
|
|
|
|
xor a
|
|
|
|
pop bc
|
|
|
|
ret
|
|
|
|
|
2019-04-15 16:53:11 -04:00
|
|
|
; This table is at the very end of the file on purpose. The idea is to be able
|
|
|
|
; to graft extra commands easily after an include in the glue file.
|
2019-04-13 22:39:28 -04:00
|
|
|
shellCmdTbl:
|
2019-06-02 10:50:18 -04:00
|
|
|
.dw shellMptrCmd, shellPeekCmd, shellPokeCmd, shellCallCmd
|
2019-05-02 15:18:47 -04:00
|
|
|
.dw shellIORDCmd, shellIOWRCmd
|
2019-04-13 16:01:20 -04:00
|
|
|
|