SamCoVT wrote:
Rather than replace Tali2's do-loop words, these words are meant to compliment them. They use the Y register similar to how one might in assembly to count down to zero, so only the upper limit is given, and it's limited to 8-bits (but you can get 256 loops by specifying 0 as the starting value). Because these words use the Y register, I just made word names with a "Y" prefix. After writing this, I realized that it doesn't really matter which register is used, and A could have been used just as well (and sometimes is used because Tali2 has a PUSH-A macro to get A on the top of the Forth data stack.
Quote:
' testingyyij cycle_test CYCLES: 5420625 ok
' testingdodoij cycle_test CYCLES: 11053940 ok
I implemented YLOOPS in Fleet Forth, an ITC Forth. Since it is for a Commodore 64, it uses an NMOS version of the 6502, the 6510. There are no PLY and PHY instructions.
In my first attempt to code (YLOOP) , I had to clear the Y index register to zero because the words BRANCH and ?BRANCH need it cleared. ( NEXT leaves zero in the Y index register).
Code:
CODE (YLOOP)
PLA TAY DEY
0= NOT IF
TYA PHA 0 # LDY
' BRANCH @ JMP
THEN
' ?BRANCH @ 8 + JMP END-CODE
I think this version of (YLOOP) for the 6510 is better.
Code:
CODE (YLOOP)
PLA SEC 1 # SBC
0= NOT IF
PHA
' BRANCH @ JMP
THEN
' ?BRANCH @ 8 + JMP END-CODE
Here is all of it.
Code:
CODE (YDO) ( B -- )
0 ,X LDA PHA POP JMP END-CODE
: YDO
COMPILE (YDO)
<MARK NEGATE ; IMMEDIATE
CODE (YLOOP)
PLA SEC 1 # SBC
0= NOT IF
PHA
' BRANCH @ JMP
THEN
' ?BRANCH @ 8 + JMP END-CODE
: YLOOP
COMPILE (YLOOP)
NEGATE <RESOLVE ; IMMEDIATE
CODE YI ( -- B )
PLA PHA
APUSH JMP END-CODE
CODE YJ ( -- B )
XSAVE STX TSX
$102 ,X LDA
XSAVE LDX
APUSH JMP END-CODE
CODE YK ( -- B )
XSAVE STX TSX
$103 ,X LDA
XSAVE LDX
APUSH JMP END-CODE
: TESTING
5 YDO
3 YDO
CR ." YI=" YI . ." YJ=" YJ .
YLOOP
YLOOP ;
: TESTING2
2 YDO
5 YDO
3 YDO
CR ." YI=" YI . ." YJ=" YJ . ." YK=" YK .
YLOOP
YLOOP
YLOOP ;
: TESTINGYYIJ
255 YDO 255 YDO YI YJ 2DROP YLOOP YLOOP ;
: TESTINGDODOIJ
255 0 DO 255 0 DO I J 2DROP LOOP LOOP ;
I included YK because Fleet Forth has K in the system loader. I don't think triply nested DO LOOPs are a good idea, but may be helpful during the prototyping phase to flesh out an idea.
I noticed that in your results testingyyij took slightly less than half the time of testingdodoij .
Since Fleet Forth is an ITC Forth, my results were that TESTINGYYIJ took about 83-84 percent the time of TESTINGDODOIJ .