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