forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.gamepad.nes.spin
359 lines (309 loc) · 11.7 KB
/
input.gamepad.nes.spin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
{
--------------------------------------------
Filename: input.gamepad.nes.spin
Author: Jesse Burt
Description: Driver for NES gamepads
Copyright (c) 2023
Started Apr 16, 2023
Updated Apr 16, 2023
See end of file for terms of use.
--------------------------------------------
NOTE: This is based on excerpts of NES_Game_emulator_010.spin,
originally by Darryl Biggar.
Usage:
For single-shot reads (doesn't require an extra core/cog),
1) call startx() with the appropriate I/O pins
2) call one of read_ctrl1(), read_ctrl2(), or read_both() to read the button state(s)
of the connected gamepad(s). The button states are returned as 8-bit masks.
3) call ctrl#_a(), ctrl#_b(), ctrl#_select(), ctrl#_start(), ctrl#_up(), ctrl#_down(),
ctrl#_left(), ctrl#_right() as needed, where # is 1 or 2
The button states can also be read directly by the parent object by reading the
_ctrl1_state and/or _ctrl2_state variables. Example:
' Parent object
OBJ nes: "input.gamepad.nes"
PUB do_something() | p1, p2
p1 := nes._ctrl1_state
p2 := nes._ctrl2_state
For continuous background reads (requires an extra core/cog),
1) call startx_cog() with the appropriate I/O pins
2) call ctrl#_a(), ctrl#_b(), ctrl#_select(), ctrl#_start(), ctrl#_up(), ctrl#_down(),
ctrl#_left(), ctrl#_right() as needed, where # is 1 or 2
The button states can also be read directly by the parent object by reading the
_ctrl1_state and/or _ctrl2_state variables. Example:
' Parent object
OBJ nes: "input.gamepad.nes"
PUB do_something() | p1, p2
p1 := nes._ctrl1_state
p2 := nes._ctrl2_state
NOTE: Gamepad #2 is optional. If it isn't used, data returned by ctrl2_*() methods
is undefined
}
CON
{ gamepad button masks }
CTRL_A = 1 << 7
CTRL_B = 1 << 6
CTRL_SEL = 1 << 5
CTRL_ST = 1 << 4
CTRL_UP = 1 << 3
CTRL_DN = 1 << 2
CTRL_LT = 1 << 1
CTRL_RT = 1 << 0
VAR
{ I/O and low-level }
long _LATCH, _CLK, _DATA1, _DATA2
long _ctrl_stack[11], _cog
{ controller button states (directly readable by parent) }
byte _ctrl1_state, _ctrl2_state
PUB startx(LATCH_PIN, CLK_PIN, DATA1_PIN, DATA2_PIN): status
' Start one-shot NES controller driver
' LATCH_PIN: gamepad LATCH pin
' CLK_PIN: gamepad CLOCK pin
' DATA1_PIN: gamepad 1 DATA pin
' DATA2_PIN: gamepad 2 DATA pin (optional; specify outside of range 0..31 to ignore)
' Returns:
' cog ID + 1 of current (parent of this object) cog ID
longmove(@_LATCH, @LATCH_PIN, 4)
dira[LATCH_PIN] := 1
dira[CLK_PIN] := 1
dira[DATA1_PIN] := 0
if ( lookdown(DATA2_PIN: 0..31) ) ' don't touch gamepad #2 pin unless it's valid
dira[DATA2_PIN] := 0
outa[CLK_PIN] := 0
outa[LATCH_PIN] := 0
outa[LATCH_PIN] := 1 ' JOY_SH/LDn = 1
outa[LATCH_PIN] := 0 ' JOY_SH/LDn = 0
return cogid+1
PUB startx_cog(LATCH_PIN, CLK_PIN, DATA1_PIN, DATA2_PIN): status
' Start a continuous (asynchronous/background) NES controller driver
' LATCH_PIN: gamepad LATCH pin
' CLK_PIN: gamepad CLOCK pin
' DATA1_PIN: gamepad 1 DATA pin
' DATA2_PIN: gamepad 2 DATA pin
' Returns:
' cog ID + 1 of secondary cog ID
' 0 if no more cogs available
stop()
longmove(@_LATCH, @LATCH_PIN, 4)
if ( status := (cognew(cog_read_controllers(), @_ctrl_stack) + 1) )
_cog := status
PUB stop{}
' Stop the driver
if ( _cog )
cogstop(_cog-1)
longfill(@_LATCH, 0, 16)
bytefill(@_ctrl1_state, 0, 2)
PUB ctrl1_connected{}: c
' Flag indicating controller 1 is connected
' Returns:
' 0: not connected
' -1: connected
return (_ctrl1_state <> $ff)
PUB ctrl2_connected{}: c
' Flag indicating controller 2 is connected
' Returns:
' 0: not connected
' -1: connected
return (_ctrl2_state <> $ff)
PUB ctrl1_a{}: s
' State of ctrl 1 A button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl1_state & CTRL_A) <> 0 )
PUB ctrl1_b{}: s
' State of ctrl 1 B button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl1_state & CTRL_B) <> 0 )
PUB ctrl1_select{}: s
' State of ctrl 1 select button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl1_state & CTRL_SEL) <> 0 )
PUB ctrl1_start{}: s
' State of ctrl 1 start button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl1_state & CTRL_ST) <> 0 )
PUB ctrl1_up{}: s
' State of ctrl 1 D-pad up button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl1_state & CTRL_UP) <> 0 )
PUB ctrl1_down{}: s
' State of ctrl 1 D-pad down button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl1_state & CTRL_DN) <> 0 )
PUB ctrl1_left{}: s
' State of ctrl 1 D-pad left button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl1_state & CTRL_LT) <> 0 )
PUB ctrl1_right{}: s
' State of ctrl 1 D-pad right button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl1_state & CTRL_RT) <> 0 )
PUB ctrl2_a{}: s
' State of ctrl 2 A button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl2_state & CTRL_A) <> 0 )
PUB ctrl2_b{}: s
' State of ctrl 2 B button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl2_state & CTRL_B) <> 0 )
PUB ctrl2_select{}: s
' State of ctrl 2 select button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl2_state & CTRL_SEL) <> 0 )
PUB ctrl2_start{}: s
' State of ctrl 2 start button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl2_state & CTRL_ST) <> 0 )
PUB ctrl2_up{}: s
' State of ctrl 2 D-pad up button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl2_state & CTRL_UP) <> 0 )
PUB ctrl2_down{}: s
' State of ctrl 2 D-pad down button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl2_state & CTRL_DN) <> 0 )
PUB ctrl2_left{}: s
' State of ctrl 2 D-pad left button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl2_state & CTRL_LT) <> 0 )
PUB ctrl2_right{}: s
' State of ctrl 2 D-pad right button
' Returns:
' 0: not pressed
' -1: pressed
' NOTE: if the driver was started in cogless mode, read_both() or read_ctrl1() must be called
' first to update button states
return ( (_ctrl2_state & CTRL_RT) <> 0 )
PUB read_both{}: nes_bits | i
' Read the state of both controllers 1 and 2
' Returns:
' MSB: 8-bit controller 2 state
' LSB: 8-bit controller 1 state
' NOTE: State is also cached in RAM
outa[_LATCH] := 1 ' latch state
outa[_LATCH] := 0
nes_bits := ina[_DATA1] | (ina[_DATA2] << 8)
repeat i from 0 to 6
outa[_CLK] := 1 ' pulse clock
outa[_CLK] := 0
nes_bits := (nes_bits << 1) | ina[_DATA1] | (ina[_DATA2] << 8)
_ctrl1_state := !nes_bits.byte[0] ' store states in RAM
_ctrl2_state := !nes_bits.byte[1]
return (!nes_bits & $FFFF)
PUB read_ctrl1{}: nes_bits | i
' Read the state of controller 1
' Returns:
' 8-bit controller 1 state
' NOTE: State is also cached in RAM
outa[_LATCH] := 1 ' latch state
outa[_LATCH] := 0
nes_bits := ina[_DATA1]
repeat i from 0 to 6
outa[_CLK] := 1 ' pulse clock
outa[_CLK] := 0
nes_bits := (nes_bits << 1) | ina[_DATA1]
_ctrl1_state := !nes_bits ' store state in RAM
return (!nes_bits & $FF)
PUB read_ctrl2{}: nes_bits | i
' Read the state of controller 2
' Returns:
' 8-bit controller 2 state
' NOTE: State is also cached in RAM
outa[_LATCH] := 1 ' latch state
outa[_LATCH] := 0
nes_bits := ina[_DATA2]
repeat i from 0 to 6
outa[_CLK] := 1 ' pulse clock
outa[_CLK] := 0
nes_bits := (nes_bits << 1) | ina[_DATA2]
_ctrl2_state := !nes_bits ' store state in RAM
return (!nes_bits & $FF)
PRI cog_read_controllers()
' Read gamepads in async/background cog and store states in RAM
dira[_LATCH] := 1
dira[_CLK] := 1
dira[_DATA1] := 0
outa[_CLK] := 0
outa[_LATCH] := 0
outa[_LATCH] := 1
if ( lookdown(_DATA2: 0..31) )
dira[_DATA2] := 0
repeat
read_both{}
else
repeat
read_ctrl1{}
DAT
{
Copyright 2023 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}