forth-riscv

My forth
git clone git://git.electrosoup.com/forth-riscv
Log | Files | Refs

commit 6b5863463084e563b626da50f4ea7a9aff0edb95
parent 7c564c2bdfec3ad170e5fab00f810266b7324eb1
Author: Christian Ermann <christianermann@gmail.com>
Date:   Wed,  4 Dec 2024 10:55:28 -0800

Refactor 'accept' and 'refill'

Diffstat:
Msrc/forth.s | 140+++++++++++++++++++++++++++++++------------------------------------------------
1 file changed, 55 insertions(+), 85 deletions(-)

diff --git a/src/forth.s b/src/forth.s @@ -298,75 +298,63 @@ defcode "key", key, 0x6815C86C jal uart_get_char next -defcode "accept", accept, 0x08247E29 - pop x # address - push_ret ra - jal accept_impl - pop_ret ra - next - -accept_impl: - push_ret ra - mv s4, w - mv s5, w - beqz s5, 2f - li s6, 0x0A # '\n' - li s7, 0x0D # '\r' -1: - push x +defword "accept", accept, 0x08247E29 + # ( dst-addr max-len -- len ) + lw x, 0*cell(psp) + sw w, 0*cell(psp) + mv y, w + blez y, _accept_done + li s3, 0x0A # '\n' + li s4, 0x0D # '\r' +_accept_loop: jal uart_get_char - pop x - beq w, s6, 2f - beq w, s7, 2f + beq w, s3, _accept_done + beq w, s4, _accept_done sb w, 0(x) addi x, x, 1 - addi s5, s5, -1 - bnez s5, 1b -2: - sub w, s4, s5 - pop_ret ra - ret + addi y, y, -1 + bgtz y, _accept_loop +_accept_done: + lw w, 0*cell(psp) + addi psp, psp, 1*cell + sub w, w, y + exit defword "refill", refill, 0x238BAA91 - jal source_id - jal fetch + addi psp, psp, -2*cell + sw w, -1*cell(psp) + # check input source + la w, _source_id + lw w, 0(w) beqz w, _refill_tib _refill_fib: - jal to_ret - jal fib - jal from_ret - jal read_line - jal q_branch - .int _refill_failed - jal fib - jal drop - jal swap - jal branch - .int _refill_success + la y, _fib + 1*cell + lw x, -1*cell(y) + sw y, 0*cell(psp) + push x + jal read_line # ( dst max-len file-id -- len status ) + pop x + beqz w, _refill_fail + mv w, x + la x, _fib + 1*cell + j _refill_okay _refill_tib: - pop w - jal tib - jal accept - jal dup - jal q_branch - .int _refill_failed - jal tib - jal drop - jal swap -_refill_success: - jal tick_source - jal two_store - jal lit - .int 0 - jal source_offset - jal store - jal lit - .int -1 + la x, _tib + 1*cell + lw w, -1*cell(x) + sw x, 0*cell(psp) + jal accept # ( dst max-len -- len ) + beqz w, _refill_fail + la x, _tib + 1*cell +_refill_okay: + la y, _tick_source + sw w, 0*cell(y) + sw x, 1*cell(y) + la x, _source_offset + sw zero, 0*cell(x) + li w, -1 exit -_refill_failed: - jal drop - jal lit - .int 0 +_refill_fail: + mv w, zero exit defword "source", source, 0x1BCF29D8 @@ -374,24 +362,6 @@ defword "source", source, 0x1BCF29D8 jal two_fetch exit -defcode "tib", tib, 0xC90B0194 - # ( -- addr len ) - push w - la w, _tib - push w - la w, _tib_len - load_cell w, 0(w) - next - -defcode "fib", fib, 0xBCE49236 - # ( -- addr len ) - push w - la w, _fib - push w - la w, _fib_len - load_cell w, 0(w) - next - defcode "skip-while", skip_while, 0xBBFD4B86 # ( addr1 len1 -- addr2 len2 ) lw x, 0*cell(psp) @@ -1662,14 +1632,14 @@ _okay: .ascii "ok\n" _okay_len = (. - _okay) -word_buffer: .space 255 +.section ".data" .balign cell - -_tib: .space 255 -_tib_len: .int 255 - -_fib: .space 255 -_fib_len: .int 255 +_tib: + .int 255 + .space 255 +_fib: + .int 255 + .space 255 .section ".text.boot" .globl start