A name typed from a grid, the way Zelda's is
A screen for typing a name, with a grid of letters we drew ourselves. Play it: the arrows move the cursor, A types the letter under it, and holding an arrow keeps it moving. The program is 268 instructions in 611 bytes, built and checked the same way.
Play it here The cartridge, to play anywhere that plays NES cartridges: menu.nes
On the screen
The cursor sits behind the letters
On the screen where you register your name, The Legend of Zelda’s cursor is a sprite drawn behind the background, so the letter under it shows through, and it blinks. It moves the frame after you press, and if you hold the button it waits a moment and then keeps going, faster. At the edge of the grid it does not stop: it carries on in reading order, so going left from the first letter takes you to the last cell. Every step makes a click, and A writes one tile, the letter, into your name the frame after. This cartridge does the same with a grid, a wait, a beat and a click of its own.
| this cartridge | The Legend of Zelda | |
|---|---|---|
| frames from a press to the cursor moving | 0 | 1 |
| frames held before it moves again | 20 | 17 |
| then frames between moves | 6 | 9 |
| letters across the grid | 10 | 11 |
| left from the first cell goes to cell | 39 | 43 |
| tiles written for each letter typed, and frames after A | 1, 1 after | 1, 1 after |
| the cursor is drawn behind the letters | yes | yes |
| frames the cursor is shown, then hidden | 16, then 16 | 8, then 8 |
| moves that made a click | 13 of 13 | 11 of 11 |
As written
The part of the program this lesson is about. The whole program is a plain text file: menu.s
;; Up, Down, Left and Right (bits 3 to 0 of the pad). A new press moves
;; at once and sets the wait at 20 frames; held, the cursor moves again
;; when the wait runs out, and then every 6.
steer:
LDA $01
AND #$0F
BEQ still
LDA $02
AND #$0F
BNE held
LDA #$14
STA $06
JMP step
held:
DEC $06
BNE still
LDA #$06
STA $06
step:
LDA $01
LSR A
BCS right
LSR A
BCS left
LSR A
BCS down
LDA $40
SEC
SBC #$0A
BCS moved
ADC #$28
JMP moved
down:
LDA $40
CLC
ADC #$0A
CMP #$28
BCC moved
SBC #$28
JMP moved
left:
LDA $40
SEC
SBC #$01
BCS moved
LDA #$27
JMP moved
right:
LDA $40
CLC
ADC #$01
CMP #$28
BCC moved
LDA #$00
moved:
STA $40
;; The click: a short high note on the first square channel.
LDA #$9F
STA $4000
LDA #$00
STA $4001
LDA #$40
STA $4002
LDA #$00
STA $4003
still:
RTS
;; A new press of A: the letter under the cursor waits to be written at
;; the end of the name, while the name has room. The empty cell types a
;; space: nothing is written and the name moves on.
type:
LDA $02
EOR #$FF
AND $01
AND #$80
BEQ full
LDA $41
CMP #$08
BCS full
CLC
ADC #$CC
STA $43
INC $41
LDA $40
JSR tileof
STA $42