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