Checking for Data Stack under- and overflow on the 65816
Posted: Thu Dec 08, 2016 2:34 pm
I'm trying to figure out a good way to check for over- and underflow with Liara Forth for the 265SXB (65186). Tali Forth pretty much just ignored the whole problem, but we're trying to up our game here somewhat, you know, like a real Forth or something. This while putting speed before size (within reason).
Part of the problem is that I'm keeping TOS in A, which makes a lot of words a lot faster. NOS is then on a Direct Page with X as the Data Stack Pointer (so lda.dx 00 / LDA $00,X is the second stack entry, lda.dx 02 / LDA $02,X third stack entry, etc).
So far, the only thing I can think of is creating a counter in the Direct Page (Data Stack Counter, DSC) and DEC/INC it parallel with stack operations. The count itself would start with zero, not one, so that $FFFF would signal an empty stack, which is easy to test for with help of the n flag. Overflow is a bit more tricky, but I'm worried less about that, having yet to ever have an actual overflow in real life Forth usage.
This way, DROP becomes We check for underflow after we return to the main interpretation loop with something like What I don't like is that I'm getting into a bookkeeping nightmare where one wrong dec.d/inc.d would mess stuff up royally. Also, this seems somewhat inelegant, though still better than an outright comparison. This can't be a new problem - anybody have suggestions?
Part of the problem is that I'm keeping TOS in A, which makes a lot of words a lot faster. NOS is then on a Direct Page with X as the Data Stack Pointer (so lda.dx 00 / LDA $00,X is the second stack entry, lda.dx 02 / LDA $02,X third stack entry, etc).
So far, the only thing I can think of is creating a counter in the Direct Page (Data Stack Counter, DSC) and DEC/INC it parallel with stack operations. The count itself would start with zero, not one, so that $FFFF would signal an empty stack, which is easy to test for with help of the n flag. Overflow is a bit more tricky, but I'm worried less about that, having yet to ever have an actual overflow in real life Forth usage.
This way, DROP becomes
Code: Select all
lda.dx 00 ; (LDA $00,X) move NOS to TOS
inx
inx
dec.d dsc ; (DEC DSC) decrease Data Stack Counter
rtsCode: Select all
ldy.d dsc ; (LDY DSC) Y is used as scratchpad register
bmi underflow_error