Skip to content

Commit da59823

Browse files
committed
add mapping for every key & macro to use them
1 parent c3b4e9c commit da59823

File tree

2 files changed

+154
-46
lines changed

2 files changed

+154
-46
lines changed

src/nmi/keyboardmap.asm

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
; Assisted with the following python:
2+
; kbmap = """
3+
; CloseBracket OpenBracket RETURN F8 STOP Yen SHIFTRight KANA
4+
; Semicolon Colon atSign F7 Caret Dash Slash Underscore
5+
; K L O F6 0 P Comma Period
6+
; J U I F5 8 9 N M
7+
; H G Y F4 6 7 V B
8+
; D R T F3 4 5 C F
9+
; A S W F2 3 E Z X
10+
; CTR Q ESC F1 2 1 GRPH SHIFTLeft
11+
; Left Right UP CLR_HOME INS DEL Space Down
12+
; """
13+
; for row, keys in enumerate(kbmap.strip().splitlines()):
14+
; for mask, key in enumerate(keys.split()):
15+
; mask = 0x80 >> mask
16+
; print(f"key{key:<12} = ${row:02x}{mask:02x}")
17+
18+
19+
; 2 bytes to represent each key
20+
; hi byte is an index into keyboardInput and represents row
21+
; lo byte is a bitmask to single out key's bit
22+
; result is not zero if key pressed
23+
24+
.macro readKeyDirect keyMap
25+
lda keyboardInput+>keyMap
26+
and #<keyMap
27+
.endmacro
28+
29+
keyF1 = $0710
30+
keyF2 = $0610
31+
keyF3 = $0510
32+
keyF4 = $0410
33+
keyF5 = $0310
34+
keyF6 = $0210
35+
keyF7 = $0110
36+
keyF8 = $0010
37+
38+
keyStop = $0008
39+
keyReturn = $0020
40+
41+
keyShiftRight = $0002
42+
keyShiftLeft = $0701
43+
44+
keyESC = $0720
45+
keyCTR = $0780
46+
keyGRPH = $0702
47+
48+
keyCLR_HOME = $0810
49+
keyINS = $0808
50+
keyDEL = $0804
51+
52+
keyUp = $0820
53+
keyLeft = $0880
54+
keyRight = $0840
55+
keyDown = $0801
56+
57+
keyCloseBracket = $0080 ; ]
58+
keyOpenBracket = $0040 ; [
59+
keyYen = $0004 ; ¥
60+
keySemicolon = $0180 ; ;
61+
keyColon = $0140 ; :
62+
keyatSign = $0120 ; @
63+
keyCaret = $0108 ; ^
64+
keySlash = $0102 ; /
65+
keyUnderscore = $0101 ; _
66+
keyComma = $0202 ; ,
67+
keyPeriod = $0201 ; .
68+
keyDash = $0104 ; -
69+
keyKana = $0001
70+
71+
keySpace = $0802
72+
73+
key0 = $0208
74+
key1 = $0704
75+
key2 = $0708
76+
key3 = $0608
77+
key4 = $0508
78+
key5 = $0504
79+
key6 = $0408
80+
key7 = $0404
81+
key8 = $0308
82+
key9 = $0304
83+
84+
keyA = $0680
85+
keyB = $0401
86+
keyC = $0502
87+
keyD = $0580
88+
keyE = $0604
89+
keyF = $0501
90+
keyG = $0440
91+
keyH = $0480
92+
keyI = $0320
93+
keyJ = $0380
94+
keyK = $0280
95+
keyL = $0240
96+
keyM = $0301
97+
keyN = $0302
98+
keyO = $0220
99+
keyP = $0204
100+
keyQ = $0740
101+
keyR = $0540
102+
keyS = $0640
103+
keyT = $0520
104+
keyU = $0340
105+
keyV = $0402
106+
keyW = $0620
107+
keyX = $0601
108+
keyY = $0420
109+
keyZ = $0602

src/nmi/pollkeyboard.asm

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
.include "keyboardmap.asm"
2+
3+
; for remapping, see above map file for full list
4+
kbMappedUp = keyK
5+
kbMappedDown = keyJ
6+
kbMappedLeft = keyH
7+
kbMappedRight = keyL
8+
kbMappedB = keyD
9+
kbMappedA = keyF
10+
kbMappedSelect = keyShiftLeft
11+
kbMappedStart = keyReturn
12+
13+
114
; https://www.nesdev.org/wiki/Family_BASIC_Keyboard
215

316
; Input ($4016 write)
@@ -82,92 +95,63 @@ pollKeyboard:
8295
@ret: rts
8396

8497

85-
; Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7
86-
; 0 ] [ RETURN F8 STOP ¥ RSHIFT KANA
87-
; 1 ; : @ F7 ^ - / _
88-
; 2 K L O F6 0 P , .
89-
; 3 J U I F5 8 9 N M
90-
; 4 H G Y F4 6 7 V B
91-
; 5 D R T F3 4 5 C F
92-
; 6 A S W F2 3 E Z X
93-
; 7 CTR Q ESC F1 2 1 GRPH LSHIFT
94-
; 8 LEFT RIGHT UP CLR HOME INS DEL SPACE DOWN
95-
96-
kbUp = $0280 ; k
97-
kbDown = $0380 ; j
98-
kbLeft = $0480 ; h
99-
kbRight = $0240 ; l
100-
kbB = $0580 ; d
101-
kbA = $0501 ; f
102-
kbSelect = $0701 ; lshift
103-
kbStart = $0020 ; return
104-
105-
10698
mapKeysToButtons:
10799
lda entryActive
108-
bne @startOnly
109-
lda keyboardInput+>kbUp
110-
and #<kbUp
100+
; ignore all keys except for start/return during high score entry
101+
bne @readStart
102+
103+
readKeyDirect kbMappedUp
111104
beq @upNotPressed
112105
lda newlyPressedKeys
113106
ora #BUTTON_UP
114107
sta newlyPressedKeys
115-
bne @skipDownRead
108+
bne @downNotPressed ; skip down read if up is pressed
116109
@upNotPressed:
117110

118-
lda keyboardInput+>kbDown
119-
and #<kbDown
111+
readKeyDirect kbMappedDown
120112
beq @downNotPressed
121113
lda newlyPressedKeys
122114
ora #BUTTON_DOWN
123115
sta newlyPressedKeys
124-
@skipDownRead:
125116
@downNotPressed:
126-
lda keyboardInput+>kbLeft
127-
and #<kbLeft
117+
118+
readKeyDirect kbMappedLeft
128119
beq @leftNotPressed
129120
lda newlyPressedKeys
130121
ora #BUTTON_LEFT
131122
sta newlyPressedKeys
132-
bne @skipRightRead
123+
bne @rightNotPressed ; skip right read if left is pressed
133124
@leftNotPressed:
134125

135-
lda keyboardInput+>kbRight
136-
and #<kbRight
126+
readKeyDirect kbMappedRight
137127
beq @rightNotPressed
138128
lda newlyPressedKeys
139129
ora #BUTTON_RIGHT
140130
sta newlyPressedKeys
141-
@skipRightRead:
142131
@rightNotPressed:
143132

144-
lda keyboardInput+>kbB
145-
and #<kbB
133+
readKeyDirect kbMappedB
146134
beq @bNotPressed
147135
lda newlyPressedKeys
148136
ora #BUTTON_B
149137
sta newlyPressedKeys
150138
@bNotPressed:
151139

152-
lda keyboardInput+>kbA
153-
and #<kbA
140+
readKeyDirect kbMappedA
154141
beq @aNotPressed
155142
lda newlyPressedKeys
156143
ora #BUTTON_A
157144
sta newlyPressedKeys
158145
@aNotPressed:
159146

160-
lda keyboardInput+>kbSelect
161-
and #<kbSelect
162-
beq @selectNotPressed
147+
readKeyDirect kbMappedSelect
148+
beq @readStart
163149
lda newlyPressedKeys
164150
ora #BUTTON_SELECT
165151
sta newlyPressedKeys
166-
@selectNotPressed:
167-
@startOnly:
152+
@readStart:
168153

169-
lda keyboardInput+>kbStart
170-
and #<kbStart
154+
readKeyDirect kbMappedStart
171155
beq @startNotPressed
172156
lda newlyPressedKeys
173157
ora #BUTTON_START
@@ -193,7 +177,21 @@ mapKeysToButtons:
193177
sta heldButtons_player1
194178
rts
195179

196-
shiftFlag := $08
180+
; Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7
181+
; 0 ] [ RETURN F8 STOP ¥ RSHIFT KANA
182+
; 1 ; : @ F7 ^ - / _
183+
; 2 K L O F6 0 P , .
184+
; 3 J U I F5 8 9 N M
185+
; 4 H G Y F4 6 7 V B
186+
; 5 D R T F3 4 5 C F
187+
; 6 A S W F2 3 E Z X
188+
; 7 CTR Q ESC F1 2 1 GRPH LSHIFT
189+
; 8 LEFT RIGHT UP CLR_HOME INS DEL SPACE DOWN
190+
191+
; each byte represents row, column and if shift should be read
192+
; only keys supported by the score entry routine are included
193+
194+
shiftFlag = $08
197195
charToKbMap:
198196
.byte $86 ; Space
199197
.byte $60 ; A
@@ -242,6 +240,7 @@ charToKbMap:
242240
.byte $16 | shiftFlag ; ?
243241
.byte $15 ; -
244242
.byte $85 ; del ; treated differently
243+
; future feature: read left and right to shift cursor position
245244

246245
charToKbMapEnd:
247246

0 commit comments

Comments
 (0)