Compare commits

...

5 Commits

Author SHA1 Message Date
Juhani Krekelä a993cab542 Implement line reading in calculator.asm 2022-08-17 04:07:11 +03:00
Juhani Krekelä 7a4c6295f3 Add stack manipulation subroutines 2022-08-17 03:02:04 +03:00
Juhani Krekelä 95c29fc2e0 Add tmpWord2 in calculator.asm 2022-08-17 03:01:34 +03:00
Juhani Krekelä 109cdf5a3a Don't clobber tmpWord in peekWord in calculator.asm 2022-08-17 03:00:51 +03:00
Juhani Krekelä cbfbbbcf54 Have stPrinthex pop TOS in calculator.asm 2022-08-17 02:54:36 +03:00
1 changed files with 236 additions and 26 deletions

View File

@ -1,21 +1,143 @@
org 0
start:
load r0, lit01
load r1, litff
cleq r0, r0, pushWord
cleq r0, r0, stPrinthex
xor r0, r0
load r1, lit41
cleq r0, r0, pushWord
cleq r0, r0, stPrinthex
cleq r0, r0, stSub
cleq r0, r0, stPrinthex
cleq r0, r0, stReadline
cleq r0, r0, dumpstack
halt
dumpstack:
load r0, SPStart+0
load r1, SPStart+1
store dumpstackPtr+0, r0
store dumpstackPtr+1, r1
dumpstackLoop:
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
load r2, SP+0
load r3, SP+1
brneq r0, r2, dumpstackDumpword
breq r1, r3, dumpstackEnd
dumpstackDumpword:
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
store loadByteHigh, r0
store loadByteLow, r1
cleq r0, r0, loadByte
cleq r0, r0, writehexByte
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
cleq r0, r0, incWord
store dumpstackPtr+0, r0
store dumpstackPtr+1, r1
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
store loadByteHigh, r0
store loadByteLow, r1
cleq r0, r0, loadByte
cleq r0, r0, writehexByte
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
cleq r0, r0, incWord
store dumpstackPtr+0, r0
store dumpstackPtr+1, r1
load r0, litcharSpace
store ffff, r0
breq r0, r0, dumpstackLoop
dumpstackEnd:
breq r0, r0, newline
dumpstackPtr: addr dumpstackPtr
stReadline:
; Current index starts at 0
xor r0, r0
xor r1, r1
cleq r0, r0, pushWord
readlineLoop:
; Read a byte of input
cleq r0, r0, stKey
; Is it enter?
cleq r0, r0, peekWord
load r0, litcharCR
breq r0, r1, readlineEnter
; Is it backspace?
load r0, litcharBS
breq r0, r1, readlineBackspace
; Is the buffer full?
cleq r0, r0, stOver
load r0, linebufSize+0
load r1, linebufSize+1
cleq r0, r0, pushWord
cleq r0, r0, stGtEq
brneq r0, r1, readlineBufFull
; Calculate address
cleq r0, r0, stOver
load r0, linebufStart+0
load r1, linebufStart+1
cleq r0, r0, pushWord
cleq r0, r0, stAdd
; Store the read byte
cleq r0, r0, stStoreByte
; Increase index
cleq r0, r0, stInc
breq r0, r0, readlineLoop
readlineEnter:
; Remove the CR byte off the stack, as we only want to return the length
cleq r0, r0, popWord
breq r0, r0, newline
readlineBackspace:
; Remove the BS byte off the stack
cleq r0, r0, popWord
; Are we at the beginning of the line?
cleq r0, r0, stDup
xor r0, r0
xor r1, r1
cleq r0, r0, pushWord
cleq r0, r0, stSwap
cleq r0, r0, stGtEq
brneq r0, r1, readlineLoop
; Decrease the index and erase the echoed character
cleq r0, r0, stDec
load r3, litcharSpace
store ffff, r3
load r3, litcharBS
store ffff, r3
breq r0, r0, readlineLoop
readlineBufFull:
; Drop the input byte and erase the echoed character
cleq r0, r0, popWord
load r3, litcharBS
store ffff, r3
load r3, litcharSpace
store ffff, r3
load r3, litcharBS
store ffff, r3
breq r0, r0, readlineLoop
; ==================================================================
; Stack-based functions
; ==================================================================
@ -42,6 +164,18 @@ stSub:
cleq r0, r0, subWord
breq r0, r0, pushWord
; a -- a+1
stInc:
cleq r0, r0, popWord
cleq r0, r0, incWord
breq r0, r0, pushWord
; a -- a-1
stDec:
cleq r0, r0, popWord
cleq r0, r0, decWord
breq r0, r0, pushWord
; a b --
; if a >= b then r0:r1 = 0001
; else r0:r1 = 0000
@ -62,7 +196,7 @@ stGtEq:
; n -- n
stPrinthex:
cleq r0, r0, peekWord
cleq r0, r0, popWord
cleq r0, r0, writehexWord
breq r0, r0, newline
@ -78,6 +212,51 @@ stEmit:
store ffff, r1
ret
; ------------------------------------------------------------------
; Stack manipulation
; ------------------------------------------------------------------
; a - a a
stDup:
cleq r0, r0, peekWord
breq r0, r0, pushWord
; a b -- a b a
stOver:
cleq r0, r0, popWord
cleq r0, r0, tmpStoreWord01
cleq r0, r0, peekWord
cleq r0, r0, tmp2StoreWord01
cleq r0, r0, tmpLoadWord01
cleq r0, r0, pushWord
cleq r0, r0, tmp2LoadWord01
breq r0, r0, pushWord
; a b -- b a
stSwap:
cleq r0, r0, popWord
cleq r0, r0, tmpStoreWord01
cleq r0, r0, popWord
cleq r0, r0, tmp2StoreWord01
cleq r0, r0, tmpLoadWord01
cleq r0, r0, pushWord
cleq r0, r0, tmp2LoadWord01
breq r0, r0, pushWord
; ------------------------------------------------------------------
; Memory
; ------------------------------------------------------------------
; a p --
stStoreByte:
cleq r0, r0, popWord
store storeByteHigh, r0
store storeByteLow, r1
cleq r0, r0, popWord
xor r0, r0
or r0, r1
breq r0, r0, storeByte
; ==================================================================
; Low-level functions
; ==================================================================
@ -88,16 +267,8 @@ stEmit:
tmpWordHigh: data 0
tmpWordLow: data 0
; out:
; r0:r1 = word
; clobbers:
; r2, r3
peekWord:
cleq r0, r0, popWord
cleq r0, r0, tmpStoreWord01
cleq r0, r0, pushWord
breq r0, r0, tmpLoadWord01
tmpWord2High: data 0
tmpWord2Low: data 0
; in:
; r0:r1 = word
@ -120,6 +291,36 @@ tmpLoadWord23:
load r3, tmpWordLow
ret
; in:
; r0:r1 = word
tmp2StoreWord01:
store tmpWord2High, r0
store tmpWord2Low, r1
ret
; out:
; r0:r1 = word
tmp2LoadWord01:
load r0, tmpWord2High
load r1, tmpWord2Low
ret
; out:
; r0:r1 = word
; clobbers:
; r2, r2
peekWord:
cleq r0, r0, popWord
store peekWordHigh, r0
store peekWordLow, r1
cleq r0, r0, pushWord
load r0, peekWordHigh
load r1, peekWordLow
ret
peekWordHigh: data 0
peekWordLow: data 0
; in:
; r0:r1 = word
; clobbers:
@ -685,6 +886,9 @@ lit0d: data 0d
lit0e: data 0e
lit0f: data 0f
litcharSpace:
lit20: data 20
litchar0:
lit30: data 30
litchar1:
@ -741,8 +945,14 @@ lit72: data 72
litff: data ff
; ==================================================================
; Stack
; Data
; ==================================================================
SP: addr stackBase
stackBase:
linebufStart: addr end
linebufSize:
data 00
data 80
SP: addr end + 80
SPStart: addr end + 80
end: