Commit | Line | Data |
---|---|---|
23d17247 AT |
1 | #ifndef VVS_STDLIB_STACK |
2 | #define VVS_STDLIB_STACK | |
3 | ||
4 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
5 | @ Name: | |
6 | @ stackrotate | |
7 | @ Description: | |
6cb31a3e | 8 | @ Maximum rotation depth is 14. Stomps on heap[1]-heap[15]. |
23d17247 AT |
9 | @ Call Stack: |
10 | @ stack word n | |
11 | @ ... | |
12 | @ stack word 1 | |
13 | @ rotation depth (rd) <-- TOS | |
14 | @ Return Stack (n>rd=3): | |
15 | @ stack word n | |
16 | @ ... | |
17 | @ stack word 1 | |
18 | @ stack word 3 | |
19 | @ stack word 2 <-- TOS | |
20 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
21 | NSSVTSTSN | Mark: 1010 (stackrotate) | |
22 | ||
23 | @ For the convenience of other functions, modulo the | |
24 | @ rotation depth by the available registers. | |
6cb31a3e | 25 | SSSTTTSN | PUSH 14 |
23d17247 AT |
26 | TSTT | MODULO |
27 | ||
6cb31a3e AT |
28 | @ Use heap[15] for generating register addresses. |
29 | SSSTTTTN | PUSH 15 (ptr) | |
23d17247 AT |
30 | SSSSN | PUSH 0 |
31 | TTS | STORE | |
32 | ||
33 | @ Dump one word from stack to heap each pass through the loop. | |
34 | NSSVSSSSTSTSSSSSSSSSN | Mark: 00001010 00000000 | |
35 | SNT | SWAP | |
6cb31a3e AT |
36 | SSSTN | PUSH 1 (starting register) |
37 | SSSTTTTN | PUSH 15 (ptr) | |
23d17247 AT |
38 | TTT | LOAD |
39 | TSSS | ADD | |
40 | SNT | SWAP | |
41 | TTS | STORE | |
42 | ||
43 | @ See if the loop is complete. | |
44 | SNS | DUP | |
6cb31a3e | 45 | SSSTTTTN | PUSH 15 (ptr) |
23d17247 AT |
46 | TTT | LOAD |
47 | SSSTN | PUSH 1 | |
48 | TSSS | ADD | |
49 | TSST | SUBTRACT | |
50 | NTSSSSSTSTSSSSSSSSTN | BRZ > 00001010 00000001 | |
51 | ||
52 | @ Increment the loop counter | |
6cb31a3e | 53 | SSSTTTTN | PUSH 15 (ptr) |
23d17247 AT |
54 | TTT | LOAD |
55 | SSSTN | PUSH 1 | |
56 | TSSS | ADD | |
6cb31a3e | 57 | SSSTTTTN | PUSH 15 (ptr) |
23d17247 AT |
58 | SNT | SWAP |
59 | TTS | STORE | |
60 | NSNSSSSTSTSSSSSSSSSN | JMP > 00001010 00000000 | |
61 | ||
62 | @ The correct number of words have been stored to registers. | |
63 | @ Time to read them back in rotated order. | |
64 | ||
6cb31a3e | 65 | @ First, prepare the counter in heap[15] again. |
23d17247 AT |
66 | @ This consumes 'rd' from the stack. |
67 | NSSVSSSSTSTSSSSSSSSTN | Mark: 00001010 00000001 | |
68 | SSSTN | PUSH 1 | |
69 | TSST | SUBTRACT | |
6cb31a3e | 70 | SSSTTTTN | PUSH 15 (ptr) |
23d17247 AT |
71 | SNT | SWAP |
72 | TTS | STORE | |
73 | ||
74 | @ Second, read in the old TOS manually. | |
6cb31a3e | 75 | SSSTN | PUSH 1 (starting register) |
23d17247 AT |
76 | TTT | LOAD |
77 | ||
78 | @ Read one word per pass through this loop. | |
79 | NSSVSSSSTSTSSSSSSSTSN | Mark: 00001010 00000010 | |
6cb31a3e AT |
80 | SSSTN | PUSH 1 (starting register) |
81 | SSSTTTTN | PUSH 15 (ptr) | |
23d17247 AT |
82 | TTT | LOAD |
83 | TSSS | ADD | |
84 | TTT | LOAD | |
85 | ||
86 | @ See if the loop is complete. | |
6cb31a3e | 87 | SSSTTTTN | PUSH 15 (ptr) |
23d17247 AT |
88 | TTT | LOAD |
89 | SSSTN | PUSH 1 | |
90 | TSST | SUBTRACT | |
91 | NTSSSSSTSTSSSSSSSTTN | BRZ > 00001010 00000011 | |
92 | ||
93 | @ Decrement the loop counter and loop again. | |
6cb31a3e | 94 | SSSTTTTN | PUSH 15 (ptr) |
23d17247 AT |
95 | TTT | LOAD |
96 | SSSTN | PUSH 1 | |
97 | TSST | SUBTRACT | |
6cb31a3e | 98 | SSSTTTTN | PUSH 15 (ptr) |
23d17247 AT |
99 | SNT | SWAP |
100 | TTS | STORE | |
101 | NSNSSSSTSTSSSSSSSTSN | JMP > 00001010 00000010 | |
102 | ||
103 | @ Return | |
104 | NSSVSSSSTSTSSSSSSSTTN | Mark: 00001010 00000011 | |
105 | NTN | RTS | |
106 | ||
a67867ae AT |
107 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
108 | @ Name: | |
109 | @ stackrotatereverse | |
110 | @ Description: | |
6cb31a3e | 111 | @ Maximum rotation depth is 14. Stomps on heap[1]-heap[15]. |
a67867ae AT |
112 | @ Call Stack: |
113 | @ stack word n | |
114 | @ ... | |
115 | @ stack word 1 | |
116 | @ rotation depth (rd) <-- TOS | |
117 | @ Return Stack (n>rd=3): | |
118 | @ stack word n | |
119 | @ ... | |
120 | @ stack word 2 | |
121 | @ stack word 1 | |
122 | @ stack word 3 <-- TOS | |
123 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
124 | NSSVTSTTN | Mark: 1011 (stackrotatereverse) | |
125 | ||
126 | @ For the convenience of other functions, modulo the | |
127 | @ rotation depth by the available registers. | |
6cb31a3e | 128 | SSSTTTSN | PUSH 14 |
a67867ae AT |
129 | TSTT | MODULO |
130 | ||
6cb31a3e AT |
131 | @ Use heap[15] for generating register addresses. |
132 | SSSTTTTN | PUSH 15 (ptr) | |
a67867ae AT |
133 | SSSSN | PUSH 0 |
134 | TTS | STORE | |
135 | ||
136 | @ Dump one word from stack to heap each pass through the loop. | |
137 | NSSVSSSSTSTTSSSSSSSSN | Mark: 00001011 00000000 | |
138 | SNT | SWAP | |
6cb31a3e AT |
139 | SSSTN | PUSH 1 (starting register) |
140 | SSSTTTTN | PUSH 15 (ptr) | |
a67867ae AT |
141 | TTT | LOAD |
142 | TSSS | ADD | |
143 | SNT | SWAP | |
144 | TTS | STORE | |
145 | ||
146 | @ See if the loop is complete. | |
147 | SNS | DUP | |
6cb31a3e | 148 | SSSTTTTN | PUSH 15 (ptr) |
a67867ae AT |
149 | TTT | LOAD |
150 | SSSTN | PUSH 1 | |
151 | TSSS | ADD | |
152 | TSST | SUBTRACT | |
153 | NTSSSSSTSTTSSSSSSSTN | BRZ > 00001011 00000001 | |
154 | ||
155 | @ Increment the loop counter | |
6cb31a3e | 156 | SSSTTTTN | PUSH 15 (ptr) |
a67867ae AT |
157 | TTT | LOAD |
158 | SSSTN | PUSH 1 | |
159 | TSSS | ADD | |
6cb31a3e | 160 | SSSTTTTN | PUSH 15 (ptr) |
a67867ae AT |
161 | SNT | SWAP |
162 | TTS | STORE | |
163 | NSNSSSSTSTTSSSSSSSSN | JMP > 00001011 00000000 | |
164 | ||
165 | @ The correct number of words have been stored to registers. | |
166 | @ Time to read them back in rotated order. | |
167 | ||
6cb31a3e | 168 | @ First, prepare the counter in heap[15] again. |
a67867ae AT |
169 | NSSVSSSSTSTTSSSSSSSTN | Mark: 00001011 00000001 |
170 | SSSTN | PUSH 1 | |
171 | TSST | SUBTRACT | |
172 | SNS | DUP | |
173 | SSSTN | PUSH 1 | |
174 | TSST | SUBTRACT | |
6cb31a3e | 175 | SSSTTTTN | PUSH 15 (ptr) |
a67867ae AT |
176 | SNT | SWAP |
177 | TTS | STORE | |
178 | ||
179 | @ Read one word per pass through this loop. | |
180 | @ Store it behind 'rd' on the stack. | |
181 | NSSVSSSSTSTTSSSSSSTSN | Mark: 00001011 00000010 | |
6cb31a3e AT |
182 | SSSTN | PUSH 1 (starting register) |
183 | SSSTTTTN | PUSH 15 (ptr) | |
a67867ae AT |
184 | TTT | LOAD |
185 | TSSS | ADD | |
186 | TTT | LOAD | |
187 | SNT | SWAP | |
188 | ||
189 | @ See if the loop is complete. | |
6cb31a3e | 190 | SSSTTTTN | PUSH 15 (ptr) |
a67867ae AT |
191 | TTT | LOAD |
192 | NTSSSSSTSTTSSSSSSTTN | BRZ > 00001011 00000011 | |
193 | ||
194 | @ Decrement the loop counter and loop again. | |
6cb31a3e | 195 | SSSTTTTN | PUSH 15 (ptr) |
a67867ae AT |
196 | TTT | LOAD |
197 | SSSTN | PUSH 1 | |
198 | TSST | SUBTRACT | |
6cb31a3e | 199 | SSSTTTTN | PUSH 15 (ptr) |
a67867ae AT |
200 | SNT | SWAP |
201 | TTS | STORE | |
202 | NSNSSSSTSTTSSSSSSTSN | JMP > 00001011 00000010 | |
203 | ||
204 | @ Read in the final value manually and return. | |
205 | NSSVSSSSTSTTSSSSSSTTN | Mark: 00001011 00000011 | |
6cb31a3e | 206 | SSSTN | PUSH 1 (starting register) |
a67867ae AT |
207 | TSSS | ADD |
208 | TTT | LOAD | |
209 | NTN | RTS | |
210 | ||
f0499c77 AT |
211 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
212 | @ Name: | |
213 | @ deepdup | |
214 | @ Description: | |
215 | @ Duplicates an item deep on the stack, placing the duplicate on TOS. | |
216 | @ By default, maximum depth is 11. | |
217 | @ True maximum depth is (max depth of stackrotate & stackrotatereverse)-3. | |
218 | @ Call Stack: | |
219 | @ stack word n | |
220 | @ ... | |
221 | @ stack word 1 | |
222 | @ dupdepth <-- TOS | |
223 | @ Return Stack: | |
224 | @ stack word n | |
225 | @ ... | |
226 | @ stack word 1 | |
227 | @ copy of stack word 3 <-- TOS | |
228 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
229 | NSSVTTSSN | Mark: 1100 (deepdup) | |
230 | ||
231 | @ Copy 'dupdepth' and use it to bring forth the desired stack word. | |
232 | SNS | DUP | |
233 | SSSTN | PUSH 1 | |
234 | TSSS | ADD | |
235 | NSTTSTTN | JSR > 1011 (stackrotatereverse) | |
236 | ||
237 | @ Copy the desired stack word and return it back into the stack. | |
238 | SNS | DUP | |
239 | SSSTTN | PUSH 3 | |
240 | NSTTSTTN | JSR > 1011 (stackrotatereverse) | |
241 | SSSTN | PUSH 1 | |
242 | TSSS | ADD | |
243 | NSTTSTSN | JSR > 1010 (stackrotate) | |
244 | NTN | RTS | |
245 | ||
23d17247 | 246 | #endif |
a67867ae | 247 |