Skip to navigation

Elite on the BBC Micro and NES

Text: DETOK

[NES version, Bank 2]

Name: DETOK [Show more] Type: Subroutine Category: Text Summary: Print an extended recursive token from the TKN1 token table Deep dive: Extended text tokens
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * BRIS calls DETOK * DETOK2 calls DETOK * DETOK_b2 calls DETOK * PDESC calls DETOK * DETOK3 calls via DTEN

Arguments: A The recursive token to be printed, in the range 1-255
Returns: A A is preserved Y Y is preserved V(1 0) V(1 0) is preserved
Other entry points: DTEN Print recursive token number X from the token table pointed to by (A V), used to print tokens from the RUTOK table via calls to DETOK3
.DETOK TAX ; Copy the token number from A into X 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 TXA ; Copy the token number from X into A PHA ; Store A on the stack, so we can retrieve it later TYA ; Store Y on the stack PHA LDA V ; Store V(1 0) on the stack PHA LDA V+1 PHA LDA TKN1Lo ; Set V(1 0) to the address of the TKN1 table for the STA V ; chosen language LDA TKN1Hi STA V+1 .DTEN LDY #0 ; First, we need to work our way through the table until ; we get to the token that we want to print. Tokens are ; delimited by #VE, and VE EOR VE = 0, so we work our ; way through the table in, counting #VE delimiters ; until we have passed X of them, at which point we jump ; down to DTL2 to do the actual printing. So first, we ; set a counter Y to point to the character offset as we ; scan through the table .DTL1 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 LDA (V),Y ; Load the character at offset Y in the token table, ; which is the next character from the token table EOR #VE ; Tokens are stored in memory having been EOR'd with ; #VE, so we repeat the EOR to get the actual character ; in this token BNE DT1 ; If the result is non-zero, then this is a character ; in a token rather than the delimiter (which is #VE), ; so jump to DT1 DEX ; We have just scanned the end of a token, so decrement ; X, which contains the token number we are looking for BEQ DTL2 ; If X has now reached zero then we have found the token ; we are looking for, so jump down to DTL2 to print it .DT1 INY ; Otherwise this isn't the token we are looking for, so ; increment the character pointer BNE DTL1 ; If Y hasn't just wrapped around to 0, loop back to ; DTL1 to process the next character INC V+1 ; We have just crossed into a new page, so increment ; V+1 so that V points to the start of the new page BNE DTL1 ; Jump back to DTL1 to process the next character (this ; BNE is effectively a JMP as V+1 won't reach zero ; before we reach the end of the token table) .DTL2 INY ; We just detected the delimiter byte before the token ; that we want to print, so increment the character ; pointer to point to the first character of the token, ; rather than the delimiter BNE P%+4 ; If Y hasn't just wrapped around to 0, skip the next ; instruction INC V+1 ; We have just crossed into a new page, so increment ; V+1 so that V points to the start of the new page LDA (V),Y ; Load the character at offset Y in the token table, ; which is the next character from the token we want to ; print EOR #VE ; Tokens are stored in memory having been EOR'd with ; #VE, so we repeat the EOR to get the actual character ; in this token BEQ DTEX ; If the result is zero, then this is the delimiter at ; the end of the token to print (which is #VE), so jump ; to DTEX to return from the subroutine, as we are done ; printing JSR DETOK2 ; Otherwise call DETOK2 to print this part of the token JMP DTL2 ; Jump back to DTL2 to process the next character .DTEX 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 PLA ; Restore A from the stack, so it is preserved through ; calls to this routine RTS ; Return from the subroutine