Reorganized wumpus, combining `wump_ui.pvvs` and `wump_game.pvvs`.
[vvhitespace] / examples / hunt-the-wumpus / wump_game.pvvs
... / ...
CommitLineData
1#ifndef WUMP_GAME
2#define WUMP_GAME
3
4@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5@ (c) 2019 Aaron Taylor <ataylor at subgeniuskitty dot com>
6@ See LICENSE.txt file for copyright and license details.
7@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
8
9@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
10@ Name:
11@ get_tunnel_destination
12@ Description:
13@ Returns the room number corresponding to the destination of a tunnel
14@ specified by room and slot.
15@ Call Stack:
16@ slot
17@ room_number <-- TOS
18@ Return Stack:
19@ dst_room_number <-- TOS
20@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
21NSSVTSSSTSSSN | MARK: 10001000 (get_tunnel_destination)
22@ The pointer we seek is:
23@ (room_number * room_struct_size) + 2 + slot + ROOM_DATA_BASE
24@ Where the '+2' accounts for the pit and bat booleans.
25NSTTSSSTSTTN | JSR > 10001011 (get_room_struct_size)
26TSSN | MULTIPLY
27SSSTSN | PUSH +2
28TSSS | ADD
29TSSS | ADD
30SSSTSSSSSSSSSSSSSN | PUSH 0x2000 (ROOM_DATA_BASE address)
31TSSS | ADD
32TTT | LOAD
33NTN | RTS
34
35@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
36@ Name:
37@ get_room_struct_size
38@ Description:
39@ Returns the size in words of the data structure for a single room.
40@ For example, with 3 links plus bat and pit booleans, the size is 5 words.
41@ Call Stack:
42@ <empty>
43@ Return Stack:
44@ size <-- TOS
45@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
46NSSVTSSSTSTTN | MARK: 10001011 (get_room_struct_size)
47SSSTSSSSSSSSSSTTN | PUSH 0x1003 (GAME_DATA_BASE+3 = links_per_room address)
48TTT | LOAD
49SSSTSN | PUSH +2
50TSSS | ADD
51NTN | RTS
52
53@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
54@ Name:
55@ room_has_bats
56@ Description:
57@ Check if room_number contains bats.
58@ Returns 1 or 0 representing true or false.
59@ Call Stack:
60@ room_number <-- TOS
61@ Return Stack:
62@ 1 or 0 <-- TOS
63@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
64NSSVTSSSTTSSN | MARK: 10001100 (room_has_bats)
65@ We seek the pointer:
66@ (room_number * room_struct_size) + 1 + ROOM_DATA_BASE
67@ where '+1' accounts for the offset of the bat boolean in the desired room.
68NSTTSSSTSTTN | JSR > 10001011 (get_room_struct_size)
69TSSN | MULTIPLY
70SSSTN | PUSH +1
71TSSS | ADD
72SSSTSSSSSSSSSSSSSN | PUSH 0x2000 (GAME_DATA_BASE address)
73TSSS | ADD
74TTT | LOAD
75NTN | RTS
76
77@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
78@ Name:
79@ room_has_pits
80@ Description:
81@ Check if room_number contains pits.
82@ Returns 1 or 0 representing true or false.
83@ Call Stack:
84@ room_number <-- TOS
85@ Return Stack:
86@ 1 or 0 <-- TOS
87@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
88NSSVTSSSTTSTN | MARK: 10001101 (room_has_pits)
89@ We seek the pointer:
90@ (room_number * room_struct_size) + 0 + ROOM_DATA_BASE
91@ where '+0' accounts for the offset of the pit boolean in the desired room.
92NSTTSSSTSTTN | JSR > 10001011 (get_room_struct_size)
93TSSN | MULTIPLY
94SSSTSSSSSSSSSSSSSN | PUSH 0x2000 (GAME_DATA_BASE address)
95TSSS | ADD
96TTT | LOAD
97NTN | RTS
98
99@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
100@ Name:
101@ room_has_wumpus
102@ Description:
103@ Check if room contains wumpus.
104@ Returns 1 or 0 representing true or false.
105@ Call Stack:
106@ room_number <-- TOS
107@ Return Stack:
108@ 1 or 0 <-- TOS
109@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
110NSSVTSTSSSSSN | MARK: 10100000 (room_has_wumpus)
111SSSTSSSSSSSSSTTTN | PUSH 0x1007 (wumpus_location address)
112TTT | LOAD
113TSST | SUBTRACT
114NTSTSTSSSSSSSSSSSSSN | BRZ > 10100000 00000000 (room_has_wumpus:true)
115SSSSN | PUSH 0 (false)
116NTN | RTS
117NSSVTSTSSSSSSSSSSSSSN | MARK: 10100000 00000000 (room_has_wumpus:true)
118SSSTN | PUSH 1 (true)
119NTN | RTS
120
121@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
122@ Name:
123@ are_bats_near
124@ Description:
125@ Given a room number, checks rooms within one hop for bats.
126@ Returns 1 if bats are present or 0 if no bats.
127@ Call Stack:
128@ room_number <-- TOS
129@ Return Stack:
130@ 1 or 0 <-- TOS
131@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
132#include <stack.pvvs>
133NSSVTSSTTTSSN | MARK: 10011100 (are_bats_near)
134
135@ Prepare the stack by loading the number of links per room and decrementing.
136@ We will loop until this reaches 0 or we find bats.
137SSSTSSSSSSSSSSTTN | PUSH 0x1003 (number_of_links_per_room address)
138TTT | LOAD
139SSSTN | PUSH 1
140TSST | SUBTRACT
141
142@ Check one nearby room on each pass through this loop.
143@ TOS> tunnel_index, room_number
144NSSVTSSTTTSSSSSSSSSSN | MARK: 10011100 00000000 (are_bats_near:loop)
145SNS | DUP
146SSSTTN | PUSH 3
147NSTTTSSN | JSR > 1100 (deepdup)
148NSTTSSSTSSSN | JSR > 10001000 (get_tunnel_destination)
149NSTTSSSTTSSN | JSR > 10001100 (room_has_bats)
150NTSTSSTTTSSSSSSSSSTN | BRZ > 10011100 00000001 (no_bats_in_this_room)
151@ Found bats. Clean up and return.
152SNN | DROP
153SNN | DROP
154SSSTN | PUSH 1
155NTN | RTS
156NSSVTSSTTTSSSSSSSSSTN | MARK: 10011100 00000001 (no_bats_in_this_room)
157@ Test for end of loop.
158SNS | DUP
159NTSTSSTTTSSSSSSSSTSN | BRZ > 10011100 00000010 (are_bats_near:loop_end)
160@ No bats found yet, but still need to check some rooms.
161@ Decrement tunnel index and loop again.
162SSSTN | PUSH 1
163TSST | SUBTRACT
164NSNTSSTTTSSSSSSSSSSN | JMP > 10011100 00000000 (are_bats_near:loop)
165@ No bats found in nearby rooms. Clean up and return.
166NSSVTSSTTTSSSSSSSSTSN | MARK: 10011100 00000010 (are_bats_near:loop_end)
167SNN | DROP
168SNN | DROP
169SSSSN | PUSH 0
170NTN | RTS
171
172@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
173@ Name:
174@ are_pits_near
175@ Description:
176@ Given a room number, checks rooms within one hop for pits.
177@ Returns 1 if pits are present or 0 if no pits.
178@ Call Stack:
179@ room_number <-- TOS
180@ Return Stack:
181@ 1 or 0 <-- TOS
182@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
183#include <stack.pvvs>
184NSSVTSSTTTSTN | MARK: 10011101 (are_pits_near)
185
186@ Prepare the stack by loading the number of links per room and decrementing.
187@ We will loop until this reaches 0 or we find pits.
188SSSTSSSSSSSSSSTTN | PUSH 0x1003 (number_of_links_per_room address)
189TTT | LOAD
190SSSTN | PUSH 1
191TSST | SUBTRACT
192
193@ Check one nearby room on each pass through this loop.
194@ TOS> tunnel_index, room_number
195NSSVTSSTTTSTSSSSSSSSN | MARK: 10011101 00000000 (are_pits_near:loop)
196SNS | DUP
197SSSTTN | PUSH 3
198NSTTTSSN | JSR > 1100 (deepdup)
199NSTTSSSTSSSN | JSR > 10001000 (get_tunnel_destination)
200NSTTSSSTTSTN | JSR > 10001101 (room_has_pits)
201NTSTSSTTTSTSSSSSSSTN | BRZ > 10011101 00000001 (no_pits_in_this_room)
202@ Found pits. Clean up and return.
203SNN | DROP
204SNN | DROP
205SSSTN | PUSH 1
206NTN | RTS
207NSSVTSSTTTSTSSSSSSSTN | MARK: 10011101 00000001 (no_pits_in_this_room)
208@ Test for end of loop.
209SNS | DUP
210NTSTSSTTTSTSSSSSSTSN | BRZ > 10011101 00000010 (are_pits_near:loop_end)
211@ No pits found yet, but still need to check some rooms.
212@ Decrement tunnel index and loop again.
213SSSTN | PUSH 1
214TSST | SUBTRACT
215NSNTSSTTTSTSSSSSSSSN | JMP > 10011101 00000000 (are_pits_near:loop)
216@ No pits found in nearby rooms. Clean up and return.
217NSSVTSSTTTSTSSSSSSTSN | MARK: 10011101 00000010 (are_pits_near:loop_end)
218SNN | DROP
219SNN | DROP
220SSSSN | PUSH 0
221NTN | RTS
222
223@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
224@ Name:
225@ is_wumpus_very_near
226@ Description:
227@ Given a room number, checks rooms within one hop for the wumpus.
228@ Returns 1 if wumpus is present, otherwise 0.
229@ Call Stack:
230@ room_number <-- TOS
231@ Return Stack:
232@ 1 or 0 <-- TOS
233@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
234#include <stack.pvvs>
235NSSVTSSTTTTTN | MARK: 10011111 (is_wumpus_very_near)
236
237@ Prepare the stack by loading the number of links per room and decrementing.
238@ We will loop until this reaches 0 or we find the wumpus.
239SSSTSSSSSSSSSSTTN | PUSH 0x1003 (number_of_links_per_room address)
240TTT | LOAD
241SSSTN | PUSH 1
242TSST | SUBTRACT
243
244@ Check one nearby room on each pass through this loop.
245@ TOS> tunnel_index, room_number
246NSSVTSSTTTTTSSSSSSSSN | MARK: 10011111 00000000 (is_wumpus_very_near:loop)
247SNS | DUP
248SSSTTN | PUSH 3
249NSTTTSSN | JSR > 1100 (deepdup)
250NSTTSSSTSSSN | JSR > 10001000 (get_tunnel_destination)
251NSTTSTSSSSSN | JSR > 10100000 (room_has_wumpus)
252NTSTSSTTTTTSSSSSSSTN | BRZ > 10011111 00000001 (no_wumpus_in_this_room)
253@ Found wumpus. Clean up and return.
254SNN | DROP
255SNN | DROP
256SSSTN | PUSH 1
257NTN | RTS
258NSSVTSSTTTTTSSSSSSSTN | MARK: 10011111 00000001 (no_wumpus_in_this_room)
259@ Test for end of loop.
260SNS | DUP
261NTSTSSTTTTTSSSSSSTSN | BRZ > 10011111 00000010 (is_wumpus_very_near:loop_end)
262@ No wumpus found yet, but still need to check some rooms.
263@ Decrement tunnel index and loop again.
264SSSTN | PUSH 1
265TSST | SUBTRACT
266NSNTSSTTTTTSSSSSSSSN | JMP > 10011111 00000000 (is_wumpus_very_near:loop)
267@ No wumpus found in nearby rooms. Clean up and return.
268NSSVTSSTTTTTSSSSSSTSN | MARK: 10011111 00000010 (is_wumpus_very_near:loop_end)
269SNN | DROP
270SNN | DROP
271SSSSN | PUSH 0
272NTN | RTS
273
274@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
275@ Name:
276@ is_wumpus_near
277@ Description:
278@ Given a room number, checks rooms within two hops for the wumpus.
279@ Returns 1 if wumpus is present, otherwise 0.
280@ Call Stack:
281@ room_number <-- TOS
282@ Return Stack:
283@ 1 or 0 <-- TOS
284@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
285#include <stack.pvvs>
286NSSVTSSTTTTSN | MARK: 10011110 (is_wumpus_near)
287
288@ Prepare the stack by loading the number of links per room and decrementing.
289@ We will loop until this reaches 0 or we find the wumpus.
290SSSTSSSSSSSSSSTTN | PUSH 0x1003 (number_of_links_per_room address)
291TTT | LOAD
292SSSTN | PUSH 1
293TSST | SUBTRACT
294
295@ Check one nearby room and its connecting rooms on each pass through this loop.
296@ TOS> tunnel_index, room_number
297NSSVTSSTTTTSSSSSSSSSN | MARK: 10011110 00000000 (is_wumpus_near:loop)
298SNS | DUP
299SSSTTN | PUSH 3
300NSTTTSSN | JSR > 1100 (deepdup)
301NSTTSSSTSSSN | JSR > 10001000 (get_tunnel_destination)
302@ TOS> tunnel_endpoint, tunnel_index, room_number
303SNS | DUP
304NSTTSTSSSSSN | JSR > 10100000 (room_has_wumpus)
305SSSTN | PUSH 1
306TSST | SUBTRACT
307NTSTSSTTTTSSSSSSSTTN | BRZ > 10011110 00000011 (found_wumpus_one_hop)
308NSTTSSTTTTTN | JSR > 10011111 (is_wumpus_very_near)
309SSSTN | PUSH 1
310TSST | SUBTRACT
311NTSTSSTTTTSSSSSSTSSN | BRZ > 10011110 00000100 (found_wumpus_two_hops)
312@ Test for end of loop.
313SNS | DUP
314NTSTSSTTTTSSSSSSSTSN | BRZ > 10011110 00000010 (is_wumpus_near:loop_end)
315@ No wumpus found yet, but still need to check some rooms.
316@ Decrement tunnel index and loop again.
317SSSTN | PUSH 1
318TSST | SUBTRACT
319NSNTSSTTTTSSSSSSSSSN | JMP > 10011110 00000000 (is_wumpus_near:loop)
320
321@ No wumpus found in nearby rooms. Clean up and return.
322NSSVTSSTTTTSSSSSSSTSN | MARK: 10011110 00000010 (is_wumpus_near:loop_end)
323SNN | DROP
324SNN | DROP
325SSSSN | PUSH 0
326NTN | RTS
327
328@ Found wumpus. Clean up and return.
329NSSVTSSTTTTSSSSSSSTTN | MARK: 10011110 00000011 (found_wumpus_one_hop)
330SNN | DROP
331NSSVTSSTTTTSSSSSSTSSN | MARK: 10011110 00000100 (found_wumpus_two_hops)
332SNN | DROP
333SNN | DROP
334SSSTN | PUSH 1
335NTN | RTS
336
337@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
338@ Name:
339@ seed_rng
340@ Description:
341@ Generate seed from keyboard input.
342@ Call Stack:
343@ <empty>
344@ Return Stack:
345@ <empty>
346@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
347#include <logic.pvvs>
348NSSVTSSSSSTTN | MARK: 10000011 (seed_rng)
349
350SSSTSSSSN | PUSH 16 (loop counter)
351SSSSN | PUSH 0 (rng seed)
352
353NSSVTSSSSSTTSSSSSSSSN | MARK: 10000011 00000000 (seed_rng:main loop)
354@ Get character from user and print ASCII '.' as feedback.
355SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (USER_INPUT_BUFFER address)
356TNTS | GETCHAR
357SSSTSTTTSN | PUSH ASCII '.'
358TNSS | PUTCHAR
359@ Left shift the seed by 4 bits.
360SSSTSSN | PUSH 4 (shift count)
361NSTTSTTSTN | JSR > 101101 (lshift)
362@ XOR seed with character from user.
363SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (USER_INPUT_BUFFER address)
364TTT | LOAD
365NSTTSTSTTN | JSR > 101011 (xor)
366@ Decrement counter
367SNT | SWAP
368SSSTN | PUSH +1
369TSST | SUBTRACT
370@ Test for loop completion
371SNS | DUP
372NTSTSSSSSTTSSSSSSSTN | BRZ > 10000011 00000001 (seed_rng:cleanup and return)
373SNT | SWAP
374NSNTSSSSSTTSSSSSSSSN | JMP > 10000011 00000000 (seed_rng:main loop)
375
376@ Store seed, clean up and return.
377NSSVTSSSSSTTSSSSSSSTN | MARK: 10000011 00000001 (seed_rng:cleanup and return)
378SNN | DROP
379SSSSN | PUSH 0 (seed address)
380SNT | SWAP
381TTS | STORE
382SSSTSTSN | PUSH ASCII '\n'
383SSSTSTSN | PUSH ASCII '\n'
384TNSS | PUTCHAR
385TNSS | PUTCHAR
386NTN | RTS
387
388@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
389@ Name:
390@ is_room_adjacent
391@ Description:
392@ Checks if 'room_number' is adjacent to the current player room.
393@ Call Stack:
394@ room_number <-- TOS
395@ Return Stack:
396@ (1 or 0) for true/false <-- TOS
397@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
398#include <stack.pvvs>
399NSSVTSTSSTSSN | MARK: 10100100 (is_room_adjacent)
400
401SSSTSSSSSSSSSSTTN | PUSH 0x1003 (ptr to number_of_tunnels_per_room)
402TTT | LOAD
403SSSTN | PUSH +1
404TSST | SUBTRACT
405
406@ TOS> tunnel_index, destination_room_num
407NSSVTSTSSTSSSSSSSSSSN | MARK: 10100100 00000000 (is_room_adjacent:main_loop)
408SSSTSN | PUSH +2
409NSTTTSSN | JSR > 1100 (deepdup)
410SSSTSN | PUSH +2
411NSTTTSSN | JSR > 1100 (deepdup)
412SSSTSSSSSSSSSTTSN | PUSH 0x1006 (ptr to player_location)
413TTT | LOAD
414NSTTSSSTSSSN | JSR > 10001000 (get_tunnel_destination)
415TSST | SUBTRACT
416NTSTSTSSTSSSSSSSSSTN | BRZ > 10100100 00000001 (is_room_adjacent:found_tunnel)
417SSSTN | PUSH +1
418TSST | SUBTRACT
419SNS | DUP
420NTTTSTSSTSSSSSSSSTSN | BMI > 10100100 00000010 (is_room_adjacent:no_match)
421NSNTSTSSTSSSSSSSSSSN | JMP > 10100100 00000000 (is_room_adjacent:main_loop)
422
423NSSVTSTSSTSSSSSSSSSTN | MARK: 10100100 00000001 (is_room_adjacent:found_tunnel)
424SNN | DROP
425SNN | DROP
426SSSTN | PUSH +1
427NTN | RTS
428
429NSSVTSTSSTSSSSSSSSTSN | MARK: 10100100 00000010 (is_room_adjacent:no_match)
430SNN | DROP
431SNN | DROP
432SSSSN | PUSH 0
433NTN | RTS
434
435@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
436@ Name:
437@ move_player
438@ Description:
439@ Prompts the player for a room number. Moves to that room, checking the new
440@ environment and executing consequences (fell in a pit, etc) as appropriate.
441@ Call Stack:
442@ <empty>
443@ Return Stack:
444@ <empty>
445@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
446#include <stdio.pvvs>
447#include <convert.pvvs>
448#include <math.pvvs>
449#include <string.pvvs>
450NSSVTSTSSSTTN | MARK: 10100011 (move_player)
451
452A"To which room do you wish to move?\n"
453SSSSN | PUSH 0 (number of string substitutions)
454NSTTSSSN | JSR > 1000 (printf)
455SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (buffer address)
456SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (buffer size)
457NSTTSSSTSN | JSR > 100010 (get_user_string)
458SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (USER_INPUT_BUFFER address)
459NSTTTSSSSN | JSR > 110000 (atoi)
460SNN | DROP
461
462@ The desired room number is now on the TOS. Verify that it is valid.
463SNS | DUP
464NSTTSTSSTSSN | JSR > 10100100 (is_room_adjacent)
465NTSTSTSSSTTSSSSSSSTN | BRZ > 10100011 00000001 (invalid room number)
466NSNTSTSSSTTSSSSSSSSN | JMP > 10100011 00000000 (valid room number)
467
468NSSVTSTSSSTTSSSSSSSTN | MARK: 10100011 00000001 (invalid room number)
469@ TOS> room_number
470SNN | DROP
471A"*Oof!* (you hit the wall)\n"
472SSSSN | PUSH 0 (number of string substitutions)
473NSTTSSSN | JSR > 1000 (printf)
474NSTTSSTTN | JSR > 10011 (fastrand)
475SSSTTSN | PUSH 6 (chance)
476TSTT | MODULO
477NTSTSTSSSTTSSSSSSTSN | BRZ > 10100011 00000010 (woke the wumpus)
478NTN | RTS
479NSSVTSTSSSTTSSSSSSTSN | MARK: 10100011 00000010 (woke the wumpus)
480A"Your colorful comments awaken the wumpus!\n"
481SSSSN | PUSH 0 (number of string substitutions)
482NSTTSSSN | JSR > 1000 (printf)
483NSTTSSTTN | JSR > 10011 (fastrand)
484SSSTSSSSSSSSSSTTN | PUSH 0x1003 (ptr to number of tunnels per room)
485TSTT | MODULO
486SSSTSSSSSSSSSTTTN | PUSH 0x1007 (ptr to wumpus location)
487TTT | LOAD
488NSTTSSSTSSSN | JSR > 10001000 (get_tunnel_destination)
489SSSTSSSSSSSSSTTTN | PUSH 0x1007 (ptr to wumpus location)
490SNT | SWAP
491TTS | STORE
492SSSTSSSSSSSSSTTSN | PUSH 0x1006 (ptr to player location)
493TTT | LOAD
494NSTTSTSSSSSN | JSR > 10100000 (room_has_wumpus)
495NTSTSTSSSTTSSSSSSTTN | BRZ > 10100011 00000011 (wumpus did not move to player)
496NSTTTTTTTTTSSSSSSSSN | JSR > 11111111 00000000 (wump_kill)
497SSSSN | PUSH 0 (number of string substitutions)
498NSTTSSSN | JSR > 1000 (printf)
499NNN | DIE
500NSSVTSTSSSTTSSSSSSTTN | MARK: 10100011 00000011 (wumpus did not move to player)
501NTN | RTS
502
503NSSVTSTSSSTTSSSSSSSSN | MARK: 10100011 00000000 (valid room number)
504@ TOS> room_number
505@ Move player to new room
506SSSTSSSSSSSSSTTSN | PUSH 0x1006 (ptr to player location)
507SNT | SWAP
508TTS | STORE
509@ Check for wumpus in new player location
510SSSTSSSSSSSSSTTSN | PUSH 0x1006 (ptr to player location)
511TTT | LOAD
512NSTTSTSSSSSN | JSR > 10100000 (room_has_wumpus)
513NTSTSTSSSTTSSSSSTSSN | BRZ > 10100011 00000100 (no wumpus in new room)
514NSTTTTTTTTTSSSSSSSSN | JSR > 11111111 00000000 (wump_kill)
515SSSSN | PUSH 0 (number of string substitutions)
516NSTTSSSN | JSR > 1000 (printf)
517NNN | DIE
518NSSVTSTSSSTTSSSSSTSSN | MARK: 10100011 00000100 (no wumpus in new room)
519@ Check for pits in new player location
520SSSTSSSSSSSSSTTSN | PUSH 0x1006 (ptr to player location)
521TTT | LOAD
522NSTTSSSTTSTN | JSR > 10001101 (room_has_pits)
523NTSTSTSSSTTSSSSSTSTN | BRZ > 10100011 00000101 (no pits in new room)
524NSTTSSTTN | JSR > 10011 (fastrand)
525SSSTTSN | PUSH 6 (chance)
526TSTT | MODULO
527NTSTSTSSSTTSSSSSTTSN | BRZ > 10100011 00000110 (survived the pits)
528NSTTTTTTTTTSSSSSTSTN | JSR > 11111111 00000101 (pit_kill)
529SSSSN | PUSH 0 (number of string substitutions)
530NSTTSSSN | JSR > 1000 (printf)
531NNN | DIE
532NSSVTSTSSSTTSSSSSTTSN | MARK: 10100011 00000110 (survived the pits)
533NSTTTTTTTTTSSSSSTTSN | JSR > 11111111 00000110 (pit_survive)
534SSSSN | PUSH 0 (number of string substitutions)
535NSTTSSSN | JSR > 1000 (printf)
536NSSVTSTSSSTTSSSSSTSTN | MARK: 10100011 00000101 (no pits in new room)
537@ Check for bats in new player location
538SSSTSSSSSSSSSTTSN | PUSH 0x1006 (ptr to player location)
539TTT | LOAD
540NSTTSSSTTSSN | JSR > 10001100 (room_has_bats)
541NTSTSTSSSTTSSSSSTTSN | BRZ > 10100011 00000110 (no bats in new room)
542A"*flap* *flap* *flap* (humongous bats pick you up and move you!)\n"
543SSSSN | PUSH 0 (number of string substitutions)
544NSTTSSSN | JSR > 1000 (printf)
545NSTTSSTTN | JSR > 10011 (fastrand)
546SSSTSSSSSSSSSSTTN | PUSH 0x1003 (ptr to number of tunnels per room)
547TSTT | MODULO
548SSSTSSSSSSSSSTTSN | PUSH 0x1006 (ptr to player location)
549TTT | LOAD
550NSTTSSSTSSSN | JSR > 10001000 (get_tunnel_destination)
551NSNTSTSSSTTSSSSSSSSN | JMP > 10100011 00000000 (valid room number)
552NSSVTSTSSSTTSSSSSTTSN | MARK: 10100011 00000110 (no bats in new room)
553NTN | RTS
554
555@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
556@ Name:
557@ move_or_shoot
558@ Description:
559@ Parse user input, branching to the appropriate subroutine to move or shoot.
560@ This function does not perform any boundary checks/limits.
561@ Call Stack:
562@ <empty>
563@ Return Stack:
564@ <empty>
565@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
566#include <stdio.pvvs>
567#include <string.pvvs>
568NSSVTSTSSSTSN | MARK: 10100010 (move_or_shoot)
569
570SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (buffer address)
571SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (buffer size)
572NSTTSSSTSN | JSR > 100010 (get_user_string)
573
574@ Examine the first character of the user input buffer for 'm' or 's'.
575@ If character is something else, prompt user to try again.
576SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (USER_INPUT_BUFFER address)
577TTT | LOAD
578SSSSTTSTTSTN | PUSH 109 (ASCII 'm')
579TSST | SUBTRACT
580NTSTSTSSSTSSSSSSSSSN | BRZ > 10100010 00000000 (move)
581SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (USER_INPUT_BUFFER address)
582TTT | LOAD
583SSSSTTTSSTTN | PUSH 115 (ASCII 's')
584TSST | SUBTRACT
585NTSTSTSSSTSSSSSSSSTN | BRZ > 10100010 00000001 (shoot)
586NSTTTTTTTTTSSSSTSSSN | JSR > 11111111 00001000 (problem_with_input)
587SSSSN | PUSH 0 (number of string substitutions)
588NSTTSSSN | JSR > 1000 (printf)
589NSNTSTSSSTSN | JMP > 10100010 (move_or_shoot)
590
591@ User typed 'm'
592NSSVTSTSSSTSSSSSSSSSN | MARK: 10100010 00000000 (move)
593NSTTSTSSSTTN | JSR > 10100011 (move_player)
594NTN | RTS
595
596@ User typed 's'
597NSSVTSTSSSTSSSSSSSSTN | MARK: 10100010 00000001 (shoot)
598@ TODO: JSR shoot
599NTN | RTS
600
601@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
602@ Name:
603@ get_answer
604@ Description:
605@ Parse user input, returning 0 if user string started with 'n' or 1 if 'y'.
606@ This function does not perform any boundary checks/limits.
607@ Call Stack:
608@ <empty>
609@ Return Stack:
610@ (1 or 0 for True/False) <--- TOS
611@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
612#include <stdio.pvvs>
613#include <string.pvvs>
614NSSVTSSTTSSSN | MARK: 10011000 (get_answer)
615
616@ TODO: Consider extending the GETCHAR instruction in VVS to indicate an empty
617@ buffer instead of blocking. This would allow a character by character
618@ check without printing a slew of retry messages if the buffer is
619@ non-empty.
620SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (buffer address)
621SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (buffer size)
622NSTTSSSTSN | JSR > 100010 (get_user_string)
623
624@ Examine the first character of the user input buffer for 'y' or 'n'.
625@ If character is something else, prompt user to try again.
626SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (USER_INPUT_BUFFER address)
627TTT | LOAD
628SSSSTTTTSSTN | PUSH 121 (ASCII 'y')
629TSST | SUBTRACT
630NTSTSSTTSSSSSSSSSSSN | BRZ > 10011000 00000000 (answer: yes)
631SSSTTSSSSSSSSSSSSN | PUSH 0x3000 (USER_INPUT_BUFFER address)
632TTT | LOAD
633SSSSTTSTTTSN | PUSH 110 (ASCII 'n')
634TSST | SUBTRACT
635NTSTSSTTSSSSSSSSSSTN | BRZ > 10011000 00000001 (answer: no)
636NSTTTTTTTTTSSSSTSSSN | JSR > 11111111 00001000 (problem_with_input)
637SSSSN | PUSH 0 (number of string substitutions)
638NSTTSSSN | JSR > 1000 (printf)
639NSNTSSTTSSSN | JMP > 10011000 (get_answer)
640
641@ User typed 'y'
642NSSVTSSTTSSSSSSSSSSSN | MARK: 10011000 00000000 (answer: yes)
643SSSTN | PUSH 1
644NTN | RTS
645
646@ User typed 'n'
647NSSVTSSTTSSSSSSSSSSTN | MARK: 10011000 00000001 (answer: no)
648SSSSN | PUSH 0
649NTN | RTS
650
651@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
652@ Name:
653@ print_cave_description
654@ Description:
655@ Prints information about the cave (number of rooms, etc).
656@ Call Stack:
657@ <empty>
658@ Return Stack:
659@ <empty>
660@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
661#include <stdio.pvvs>
662NSSVTSSTTSTTN | MARK: 10011011 (print_cave_description)
663NSTTTTTTTTTSSSSTSTSN | JSR > 11111111 00001010 (cave_description)
664SSSTSSSSSSSSSTSTN | PUSH 0x1005 (number_of_arrows address)
665TTT | LOAD
666SSSTSSSSSSSSSSSTN | PUSH 0x1001 (number_of_pits address)
667TTT | LOAD
668SSSTSSSSSSSSSSTSN | PUSH 0x1002 (number_of_bats address)
669TTT | LOAD
670SSSTSSSSSSSSSSTTN | PUSH 0x1003 (number_of_tunnels address)
671TTT | LOAD
672SSSTSSSSSSSSSSSSN | PUSH 0x1000 (number_of_rooms address)
673TTT | LOAD
674SSSTSTN | PUSH 5 (number of substitions)
675NSTTSSSN | JSR > 1000 (printf)
676NTN | RTS
677
678@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
679@ Name:
680@ print_room_stats
681@ Description:
682@ Prints information about current room and hints about nearby rooms.
683@ Call Stack:
684@ <empty>
685@ Return Stack:
686@ <empty>
687@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
688#include <wump_game.pvvs>
689#include <stdio.pvvs>
690#include <stack.pvvs>
691NSSVTSTSSSSTN | MARK: 10100001 (print_room_stats)
692
693@ Print location and arrow quantity remaining.
694A"You are in room %u of the cave and have %u arrows remaining.\n"
695SSSTSSSSSSSSSTSTN | PUSH 0x1005 (number_of_arrows address)
696TTT | LOAD
697SSSTSSSSSSSSSTTSN | PUSH 0x1006 (player_location address)
698TTT | LOAD
699SSSTSN | PUSH 2 (number of substitutions)
700NSTTSSSN | JSR > 1000 (printf)
701
702@ Print if bats/pits/wumpus nearby.
703SSSTSSSSSSSSSTTSN | PUSH 0x1006 (player_location address)
704TTT | LOAD
705SNS | DUP
706NSTTSSTTTSSN | JSR > 10011100 (are_bats_near)
707NTSTSTSSSSTSSSSSSSSN | BRZ > 10100001 00000000 (no_bats)
708A"*rustle* (Bats must be nearby.)\n"
709SSSSN | PUSH 0 (number of substitutions)
710NSTTSSSN | JSR > 1000 (printf)
711NSSVTSTSSSSTSSSSSSSSN | MARK: 10100001 00000000 (no_bats)
712SNS | DUP
713NSTTSSTTTSTN | JSR > 10011101 (are_pits_near)
714NTSTSTSSSSTSSSSSSSTN | BRZ > 10100001 00000001 (no_pits)
715A"*whoosh* (You feel a draft from nearby pits.)\n"
716SSSSN | PUSH 0 (number of substitutions)
717NSTTSSSN | JSR > 1000 (printf)
718NSSVTSTSSSSTSSSSSSSTN | MARK: 10100001 00000001 (no_pits)
719NSTTSSTTTTSN | JSR > 10011110 (is_wumpus_near)
720NTSTSTSSSSTSSSSSSTSN | BRZ > 10100001 00000010 (no_wumpus)
721A"*sniff* (You smell the evil Wumpus nearby!)\n"
722SSSSN | PUSH 0 (number of substitutions)
723NSTTSSSN | JSR > 1000 (printf)
724NSSVTSTSSSSTSSSSSSTSN | MARK: 10100001 00000010 (no_wumpus)
725
726@ Print a list of nearby rooms.
727A"This room contains tunnels to the following rooms:"
728SSSSN | PUSH 0 (number of substitutions)
729NSTTSSSN | JSR > 1000 (printf)
730SSSTSSSSSSSSSTTSN | PUSH 0x1006 (player_location address)
731TTT | LOAD
732SSSTSSSSSSSSSSTTN | PUSH 0x1003 (number_of_links_per_room address)
733TTT | LOAD
734SSSTN | PUSH 1
735TSST | SUBTRACT
736@ Print one room on each pass through this loop.
737@ TOS> tunnel_index, room_number
738NSSVTSTSSSSTSSSSSSTTN | MARK: 10100001 00000011 (print_room_list_loop)
739A" %u"
740SSSTSTN | PUSH 5
741NSTTTSSN | JSR > 1100 (deepdup)
742SSSTTTN | PUSH 7
743NSTTTSSN | JSR > 1100 (deepdup)
744NSTTSSSTSSSN | JSR > 10001000 (get_tunnel_destination)
745SSSTN | PUSH 1 (number of substitutions)
746NSTTSSSN | JSR > 1000 (printf)
747@ Test for end of loop
748SNS | DUP
749NTSTSTSSSSTSSSSSTSSN | BRZ > 10100001 00000100 (print_room_list_loop_end)
750SSSTN | PUSH 1
751TSST | SUBTRACT
752NSNTSTSSSSTSSSSSSTTN | JMP > 10100001 00000011 (print_room_list_loop)
753@ Clean up and return.
754NSSVTSTSSSSTSSSSSTSSN | MARK: 10100001 00000100 (print_room_list_loop_end)
755SSSTSTSN | PUSH 10 (ASCII '\n')
756TNSS | PUTCHAR
757SNN | DROP
758SNN | DROP
759NTN | RTS
760
761#endif