| 1 | #ifndef VVS_STDLIB_STACK |
| 2 | #define VVS_STDLIB_STACK |
| 3 | |
| 4 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 5 | @ Name: |
| 6 | @ stackrotate |
| 7 | @ Description: |
| 8 | @ Maximum rotation depth is five. Stomps on heap[9]-heap[14]. |
| 9 | @ Assumes rotation depth greater than two, otherwise use SWAP. |
| 10 | @ Call Stack: |
| 11 | @ stack word n |
| 12 | @ ... |
| 13 | @ stack word 1 |
| 14 | @ rotation depth (rd) <-- TOS |
| 15 | @ Return Stack (n>rd=3): |
| 16 | @ stack word n |
| 17 | @ ... |
| 18 | @ stack word 1 |
| 19 | @ stack word 3 |
| 20 | @ stack word 2 <-- TOS |
| 21 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 22 | NSSVTSTSN | Mark: 1010 (stackrotate) |
| 23 | |
| 24 | @ For the convenience of other functions, modulo the |
| 25 | @ rotation depth by the available registers. |
| 26 | SSSTSTN | PUSH 5 |
| 27 | TSTT | MODULO |
| 28 | |
| 29 | @ Use heap[14] for generating register addresses. |
| 30 | SSSTTTSN | PUSH 14 (ptr) |
| 31 | SSSSN | PUSH 0 |
| 32 | TTS | STORE |
| 33 | |
| 34 | @ Dump one word from stack to heap each pass through the loop. |
| 35 | NSSVSSSSTSTSSSSSSSSSN | Mark: 00001010 00000000 |
| 36 | SNT | SWAP |
| 37 | SSSTSSTN | PUSH 9 (starting register) |
| 38 | SSSTTTSN | PUSH 14 (ptr) |
| 39 | TTT | LOAD |
| 40 | TSSS | ADD |
| 41 | SNT | SWAP |
| 42 | TTS | STORE |
| 43 | |
| 44 | @ See if the loop is complete. |
| 45 | SNS | DUP |
| 46 | SSSTTTSN | PUSH 14 (ptr) |
| 47 | TTT | LOAD |
| 48 | SSSTN | PUSH 1 |
| 49 | TSSS | ADD |
| 50 | TSST | SUBTRACT |
| 51 | NTSSSSSTSTSSSSSSSSTN | BRZ > 00001010 00000001 |
| 52 | |
| 53 | @ Increment the loop counter |
| 54 | SSSTTTSN | PUSH 14 (ptr) |
| 55 | TTT | LOAD |
| 56 | SSSTN | PUSH 1 |
| 57 | TSSS | ADD |
| 58 | SSSTTTSN | PUSH 14 (ptr) |
| 59 | SNT | SWAP |
| 60 | TTS | STORE |
| 61 | NSNSSSSTSTSSSSSSSSSN | JMP > 00001010 00000000 |
| 62 | |
| 63 | @ The correct number of words have been stored to registers. |
| 64 | @ Time to read them back in rotated order. |
| 65 | |
| 66 | @ First, prepare the counter in heap[14] again. |
| 67 | @ This consumes 'rd' from the stack. |
| 68 | NSSVSSSSTSTSSSSSSSSTN | Mark: 00001010 00000001 |
| 69 | SSSTN | PUSH 1 |
| 70 | TSST | SUBTRACT |
| 71 | SSSTTTSN | PUSH 14 (ptr) |
| 72 | SNT | SWAP |
| 73 | TTS | STORE |
| 74 | |
| 75 | @ Second, read in the old TOS manually. |
| 76 | SSSTSSTN | PUSH 9 (starting register) |
| 77 | TTT | LOAD |
| 78 | |
| 79 | @ Read one word per pass through this loop. |
| 80 | NSSVSSSSTSTSSSSSSSTSN | Mark: 00001010 00000010 |
| 81 | SSSTSSTN | PUSH 9 (starting register) |
| 82 | SSSTTTSN | PUSH 14 (ptr) |
| 83 | TTT | LOAD |
| 84 | TSSS | ADD |
| 85 | TTT | LOAD |
| 86 | |
| 87 | @ See if the loop is complete. |
| 88 | SSSTTTSN | PUSH 14 (ptr) |
| 89 | TTT | LOAD |
| 90 | SSSTN | PUSH 1 |
| 91 | TSST | SUBTRACT |
| 92 | NTSSSSSTSTSSSSSSSTTN | BRZ > 00001010 00000011 |
| 93 | |
| 94 | @ Decrement the loop counter and loop again. |
| 95 | SSSTTTSN | PUSH 14 (ptr) |
| 96 | TTT | LOAD |
| 97 | SSSTN | PUSH 1 |
| 98 | TSST | SUBTRACT |
| 99 | SSSTTTSN | PUSH 14 (ptr) |
| 100 | SNT | SWAP |
| 101 | TTS | STORE |
| 102 | NSNSSSSTSTSSSSSSSTSN | JMP > 00001010 00000010 |
| 103 | |
| 104 | @ Return |
| 105 | NSSVSSSSTSTSSSSSSSTTN | Mark: 00001010 00000011 |
| 106 | NTN | RTS |
| 107 | |
| 108 | #endif |