Skip to navigation

Elite on the BBC Micro and NES

Text: DETOK2

[NES version, Bank 2]

Name: DETOK2 [Show more] Type: Subroutine Category: Text Summary: Print an extended text token (1-255) Deep dive: Extended text tokens
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * DETOK calls DETOK2 * PDESC calls DETOK2 * DTS_b2 calls via DTS

Arguments: A The token to be printed (1-255)
Returns: A A is preserved Y Y is preserved V(1 0) V(1 0) is preserved
Other entry points: DTS Print a single letter in the correct case
.DETOK2 CMP #32 ; If A < 32 then this is a jump token, so skip to DT3 to BCC DT3 ; process it BIT DTW3 ; If bit 7 of DTW3 is clear, then extended tokens are BPL DT8 ; enabled, so jump to DT8 to process them ; If we get there then this is not a jump token and ; extended tokens are not enabled, so we can call the ; standard text token routine at TT27 to print the token TAX ; Copy the token number from A into X TYA ; Store Y on the stack PHA LDA V ; Store V(1 0) on the stack PHA LDA V+1 PHA TXA ; Copy the token number from X back into A JSR TT27 ; Call TT27 to print the text token JMP DT7 ; Jump to DT7 to restore V(1 0) and Y from the stack and ; return from the subroutine .DT8 ; If we get here then this is not a jump token and ; extended tokens are enabled CMP characterEnd ; If A < characterEnd then this is a printable character BCC DTS ; in the chosen language, so jump down to DTS to print ; it CMP #129 ; If A < 129, so A is in the range 91-128, jump down to BCC DT6 ; DT6 to print a randomised token from the MTIN table CMP #215 ; If A < 215, so A is in the range 129-214, jump to BCS P%+5 ; DETOK as this is a recursive token, returning from the JMP DETOK ; subroutine using a tail call ; If we get here then A >= 215, so this is a two-letter ; token from the extended TKN2/QQ16 table SBC #215 ; Subtract 215 to get a token number in the range 0-12 ; (the C flag is set as we passed through the BCC above, ; so this subtraction is correct) ASL A ; Set A = A * 2, so it can be used as a pointer into the ; two-letter token tables at TKN2 and QQ16 PHA ; Store A on the stack, so we can restore it for the ; second letter below TAX ; Fetch the first letter of the two-letter token from LDA TKN2,X ; TKN2, which is at TKN2 + X JSR DTS ; Call DTS to print it PLA ; Restore A from the stack and transfer it into X TAX LDA TKN2+1,X ; Fetch the second letter of the two-letter token from ; TKN2, which is at TKN2 + X + 1, and fall through into ; DTS to print it CMP #'?' ; If the second letter of the token is a question mark BEQ DTM-1 ; then this is a one-letter token, so just return from ; the subroutine without printing (as DTM-1 contains an ; RTS) .DTS BIT DTW1 ; If bit 7 of DTW1 is clear then DTW1 must be %00000000, BPL DT5 ; so we do not change the character to lower case, so ; jump to DT5 to print the character in upper case BIT DTW6 ; If bit 7 of DTW6 is set, then lower case has been BMI DT10 ; enabled by jump token 13, {lower case}, so jump to ; DT10 to apply the lower case and single cap masks BIT DTW2 ; If bit 7 of DTW2 is set, then we are not currently BMI DT5 ; printing a word, so jump to DT5 so we skip the setting ; of lower case in Sentence Case (which we only want to ; do when we are already printing a word) .DT10 BIT DTW8 ; If bit 7 of DTW8 is clear then DTW8 must be %0000000 BPL DT5 ; (capitalise the next letter), so jump to DT5 to print ; the character in upper case ; If we get here then we know DTW8 is %11111111 (do not ; change case, so we now convert the character to lower ; case STX SC ; Store X in SC so we can retrieve it below TAX ; Convert the character in A into lower case by looking LDA lowerCase,X ; up the lower case ASCII value from the lowerCase table LDX SC ; Restore the value of X that we stored in SC AND DTW8 ; This instruction has no effect, because we know that ; DTW8 is %11111111 ; ; The code is left over from the BBC Micro version, in ; which DTW8 is used as a bitmask to convert a character ; to upper case .DT5 JMP DASC ; Jump to DASC to print the ASCII character in A, ; returning from the routine using a tail call .DT3 ; If we get here then the token number in A is in the ; range 1 to 32, so this is a jump token that should ; call the corresponding address in the jump table at ; JMTB TAX ; Copy the token number from A into X TYA ; Store Y on the stack PHA LDA V ; Store V(1 0) on the stack PHA LDA V+1 PHA TXA ; Copy the token number from X back into A ASL A ; Set A = A * 2, so it can be used as a pointer into the ; jump table at JMTB, though because the original range ; of values is 1-32, so the doubled range is 2-64, we ; need to take the offset into the jump table from ; JMTB-2 rather than JMTB TAX ; Copy the doubled token number from A into X LDA JMTB-2,X ; Set V(1 0) to the X-th address from the table at STA V ; JTM-2, so the JMP (V) instruction at label DTM below LDA JMTB-1,X ; calls the subroutine at the relevant address from the STA V+1 ; JMTB table TXA ; Copy the doubled token number from X back into A LSR A ; Halve A to get the original token number JSR DTM ; Call DTM to call the relevant JMTB subroutine in ; V(1 0) .DT7 PLA ; Restore V(1 0) from the stack, so it is preserved STA V+1 ; through calls to this routine PLA STA V PLA ; Restore Y from the stack, so it is preserved through TAY ; calls to this routine RTS ; Return from the subroutine .DTM JMP (V) ; Call the relevant JMTB subroutine, as V(1 0) points ; to the relevant address .DT6 ; If we get here then the token number in A is in the ; range 91-128, which means we print a randomly picked ; token from the token range given in the corresponding ; entry in the MTIN table STA SC ; Store the token number in SC SETUP_PPU_FOR_ICON_BAR ; If the PPU has started drawing the icon bar, configure ; the PPU to use nametable 0 and pattern table 0 TYA ; Store Y on the stack PHA LDA V ; Store V(1 0) on the stack PHA LDA V+1 PHA JSR DORND ; Set X to a random number TAX LDA #0 ; Set A to 0, so we can build a random number from 0 to ; 4 in A plus the C flag, with each number being equally ; likely CPX #51 ; Add 1 to A if X >= 51 ADC #0 CPX #102 ; Add 1 to A if X >= 102 ADC #0 CPX #153 ; Add 1 to A if X >= 153 ADC #0 CPX #204 ; Set the C flag if X >= 204 LDX SC ; Fetch the token number from SC into X, so X is now in ; the range 91-128 ADC MTIN-91,X ; Set A = MTIN-91 + token number (91-128) + random (0-4) ; = MTIN + token number (0-37) + random (0-4) JSR DETOK ; Call DETOK to print the extended recursive token in A JMP DT7 ; Jump to DT7 to restore V(1 0) and Y from the stack and ; return from the subroutine using a tail call