Last task is to see if my adventure understand the two characters in the verb and the name. But I want more. I also want to know what to do if my adventure understand the verb. Again, I will have a vector with the verbs and a vector with the names my adventure understand. As the previous vector, each position uses two bytes. In this case, positions in the vector are not memory positions, but the letters of the verbs and names.
The code will traverse the vector and, if a match is found, I store the numeric position for the following routines.
I use a helper subroutine. This subroutine traverse a vector searching for a match, every time it found a match calls another subroutine called the callback.
This code need the vector to traverse as a parameter and the token to search as additional parameter.
I use indirect addressing so I have to use two memory positions in the page 0. Indirect addressing is a concept vastly explained in any 6052 programing web or book. If you need more help understanding it, let me know.
First the code of the main subroutine which set the parameter and callas the helper subroutine.
;---------------
TokenizeVerb
; Set the vector of verbs as parameter
; of the helper
lda #verb_tokens
sta P0_TMP_B
lda verb
sta token
lda verb+1
sta token+1
jsr f_Tokens
; Token en verb_index
sta verb_index
;pla
;sty verb_index
rts
I use the subroutine F_Token for searching for a valid verb and for a valid name too.
;------
; Find a token in a vector
; The token must be in $Token
; Returns the index of the token in a
; starts with 1, a = 0 means it has not been found
;
F_tokens
ldy #$0
F_Loop
lda (P0_TMP_A),y
beq F_Exit ; N
cmp token
bne Next1
iny
lda (P0_TMP_A),y
cmp token+1
bne Next2
iny
tya
lsr
F_Exit ; a = 0 not found, other found
rts
Next1 iny
Next2 iny
jmp F_Loop
If my code traverse all VERBs (or Names) and no match found, the adventure show an error message and repeat the input.
I do the same for name. Here you have the subroutine called from F_Token for the name vector.
;---------------
TokenizeName
lda #name_tokens
sta P0_TMP_B
lda name
sta token
lda name+1
sta token+1
jsr f_Tokens
; Token en verb_index
sta name_index
rts
One I have the indexes, is time to implement each verb of the game.
This routine is, provably, one of the most complex one in the code. Do not worry if you do not understand it. You may try to rewrite it I a simple way. If it works, it is ok.