Not strictly 6502-related, but this place is the best collection of brains I have to pick on the subject
I have a running project to develop a stack-oriented homebrew CPU (details
here and
here, if you want 'em,) and now that I have a mostly-functional simulator for the architecture, naturally enough I'm trying to develop a Forth implementation for it. Figure I may as well create a standing thread for this; I only have the one real question at the moment, but I'm sure I'll have others down the line...
So I've browsed over Brad Rodriguez's "Moving Forth" a few times in the past as I try to wrap my head around how Forth works on an internal level, and I'm back to working through it as I try to roll up some sample code for this project to evaluate my own design decisions. I'm currently looking at the part where he explains DOES> - he makes a fair production out of it, and it seems to me like it's maybe not as complicated as
all that, but on the other hand I'm not 100% sure I've got it, either. Seems to me, if I'm following it all correctly, that all the DODOES routine really is is a way for a common "handler" routine invoked by a class of words to get the "return address" - in other words, the address of the parameter field for any specific word invoking that handler - pushed onto the parameter stack, no?
Because if that's all it is, this becomes fairly trivial. I'm using an STC model, since the CPU architecture is designed around that, and based on Rodriguez's example, the DODOES subroutine could be implemented like so:
Code:
CTD ; Pop top-of-return-stack to the parameter stack
; ( -- dodoes-return )
CTD ; Pop next item from return stack to parameter stack
; ( -- dodoes-return handler-return )
SWN ; Swap parameter-stack top and next items
; ( -- handler-return dodoes-return )
JMP ; Jump indirect to @TOS (return from DODOES)
; ( -- handler-return )
Better yet, if I'm understanding it correctly, it could be inlined as a single CTD instruction (popping the parameter-field address to the parameter stack and leaving only the parent-thread return address on the return stack.) But, as I said, I'm not 100% clear on whether I am understanding this all correctly, plus I've seen some comments 'round here about the whole DOES> thing being both simpler and yet a little trickier than it looks in STC models since there's no distinction between the code and parameter fields...? Curious to know if I've grasped this properly or not; I've looked at the standard documentation, but it gives a whole lot of
what and not a lot of
why.