-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput.asm
721 lines (620 loc) · 25 KB
/
input.asm
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
;===
; Input
;
; Reads and interprets joypad inputs
;====
.define input.ENABLED 1
;====
; Settings
;====
;===
; input.ENABLE_PORT_2
; Ensure this value is defined to enable the reading of port 2
;===
;====
; Dependencies
;====
.ifndef utils.assert
.include "./utils/assert.asm"
.endif
.ifndef utils.port
.include "./utils/port.asm"
.endif
.ifndef utils.ram
.include "utils/ram.asm"
utils.ram.assertRamSlot
.endif
;====
; Constants
;====
.define input.UP %00000001
.define input.DOWN %00000010
.define input.LEFT %00000100
.define input.RIGHT %00001000
.define input.BUTTON_1 %00010000
.define input.BUTTON_2 %00100000
.define input.UP_BIT 0
.define input.DOWN_BIT 1
.define input.LEFT_BIT 2
.define input.RIGHT_BIT 3
.define input.BUTTON_1_BIT 4
.define input.BUTTON_2_BIT 5
.define input.PORT_1 $dc
.define input.PORT_2 $dd
;====
; RAM section storing the last port that was read with either input.readPort1
; or input.readPort2
;====
.ramsection "input.ram.activePort" slot utils.ram.SLOT
input.ram.activePort.current: db
input.ram.activePort.previous: db
.ends
;====
; RAM section to store the previous input values for each port
;====
.ramsection "input.ram.previous" slot utils.ram.SLOT
input.ram.previous.port1: db
input.ram.previous.port2: db
.ends
;====
; Initialises the input handler in RAM
;====
.macro "input.init"
; Initialise all buttons to released
utils.clobbers "af"
xor a
ld (input.ram.activePort.current), a
ld (input.ram.activePort.previous), a
.ifdef input.ENABLE_PORT_2
ld (input.ram.previous.port1), a
ld (input.ram.previous.port2), a
.endif
utils.clobbers.end
.endm
;====
; Reads the input from controller port 1 into the ram buffer
;
; The reset bits represent the buttons currently pressed
;
; xx000000
; |||||||*- Up
; ||||||*-- Down
; |||||*--- Left
; ||||*---- Right
; |||*----- Button 1
; ||*------ Button 2
; ** junk
;====
.macro "input.readPort1"
.ifdef input.ENABLE_PORT_2
utils.clobbers "af"
; Copy previous value of port 1 to activePort.previous
ld a, (input.ram.previous.port1) ; load previous.port1
ld (input.ram.activePort.previous), a ; store in activePort.previous
; Load current port 1 input and store in activePort.current
utils.port.read input.PORT_1 ; load input
xor $ff ; invert so 1 = pressed and 0 = released
ld (input.ram.activePort.current), a ; store in activePort.current
ld (input.ram.previous.port1), a ; store in previous.port1 for next time
utils.clobbers.end
.else
utils.clobbers "af", "hl"
ld a, (input.ram.activePort.current) ; load previous input
ld h, a ; store in H as previous value
utils.port.read input.PORT_1 ; load input
xor $ff ; invert so 1 = pressed and 0 = released
ld l, a ; set to L
; Set activePort current to L and previous to H
ld (input.ram.activePort.current), hl
utils.clobbers.end
.endif
.endm
;====
; Reads the input from controller port 2 into the RAM buffer
; See input.readPort1 documentation for details
;====
.section "input._readPort2" free
input._readPort2:
; Copy previous value of port 2 to activePort.previous
ld a, (input.ram.previous.port2) ; load previous.port1
ld (input.ram.activePort.previous), a ; store in activePort.previous
; Retrieve up and down buttons, which are stored within the PORT_1 byte
utils.port.read input.PORT_1
and %11000000 ; clear port 1 buttons
ld b, a ; store in B (DU------)
; Read remaining buttons from PORT_2
utils.port.read input.PORT_2
and %00001111 ; reset misc. bits (----21RL)
; Combine into 1 byte (DU--21RL)
or b
; Rotate left twice to match port 1 format
rlca ; rotate DU--21RL to U--21RLD
rlca ; rotate U--21RLD to --21RLDU
; Invert so 1 = pressed and 0 = released
xor $ff
; Store in ram buffer
ld (input.ram.activePort.current), a
ld (input.ram.previous.port2), a ; store in previous.port2 for next time
ret
.ends
;====
; Alias for input._readPort2
;====
.macro "input.readPort2"
.ifndef input.ENABLE_PORT_2
.print "input.asm \.: input.ENABLE_PORT_2 setting is not defined\n"
.fail
.endif
utils.clobbers "af" "bc"
call input._readPort2
utils.clobbers.end
.endm
;====
; Load A with buttons that are pressed down this frame and were pressed down
; in the last frame
;
; @out a held buttons (--21RLDU)
;====
.macro "input.loadAHeld"
utils.clobbers "hl"
; Load current input into L and previous into H
ld hl, (input.ram.activePort.current)
ld a, l ; load current into A
and h ; AND with previous
utils.clobbers.end
.endm
;====
; Sets A with buttons that were released last frame but are now pressed
;
; @out a the just-pressed buttons (--21RLDU)
;====
.macro "input.loadAPressed"
utils.clobbers "hl"
; Load L with current input value and H with previous
ld hl, (input.ram.activePort.current)
ld a, l ; load current into A
xor h ; XOR with previous. The set bits are now buttons that have changed
and l ; AND with current; Set bits have changed AND are currently pressed
utils.clobbers.end
.endm
;====
; Load A with buttons that were pressed in the previous frame but are now released
;
; @out a released buttons (--21RLDU)
;====
.macro "input.loadAReleased"
utils.clobbers "hl"
; Load L with current input value and H with previous
ld hl, (input.ram.activePort.current)
ld a, l ; load current into A
xor h ; XOR with previous. The set bits are now buttons that have changed
and h ; AND with previous; Set bits have changed AND are not currently pressed
utils.clobbers.end
.endm
;====
; Check if one or more buttons are currently pressed
;
; @in ...buttons the button(s) to check (input.UP, input.BUTTON_1 etc)
; @in else the address to jump to if the button(s) are not pressed
;====
.macro "input.if"
.if NARGS == 2
utils.assert.range \1, input.UP, input.BUTTON_2, "input.asm \.: Invalid button argument"
utils.assert.label \2, "input.asm \.: Invalid else argument"
utils.clobbers.withBranching "af"
ld a, (input.ram.activePort.current)
and \1 ; check button bit
utils.clobbers.end.jpz \2 ; jp to else if the bit was not set
utils.clobbers.end
.else
;===
; Check if multiple buttons are pressed
;===
; OR button masks together to create a single mask
.define mask\.\@ 0
.repeat NARGS - 1
utils.assert.range \1, input.UP, input.BUTTON_2, "input.asm \.: Invalid button argument"
.redefine mask\.\@ mask\.\@ | \1
.shift ; shift arguments so \2 becomes \1
.endr
; Assert remaining \1 argument is the else label
utils.assert.label \1, "input.asm \.: Expected last argument to be a label"
utils.clobbers.withBranching "af"
ld a, (input.ram.activePort.current)
and mask\.\@ ; clear other buttons
cp mask\.\@ ; compare result with mask
utils.clobbers.end.jpnz \1 ; jp to else if not all buttons are pressed
utils.clobbers.end
.endif
.endm
;====
; Check if one or more buttons have been pressed in both this frame and the
; previous frame
;
; @in ...buttons the button(s) to check (input.UP, input.BUTTON_1 etc)
; @in else the address to jump to if the button has not been held
;====
.macro "input.ifHeld"
.if NARGS == 2
utils.assert.range \1, input.UP, input.BUTTON_2, "input.asm \.: Invalid button argument"
utils.assert.label \2, "input.asm \.: Invalid else argument"
utils.clobbers.withBranching "af"
input.loadAHeld ; load A with held buttons
and \1 ; check button bit
utils.clobbers.end.jpz \2 ; jp to else if the bit was not set
utils.clobbers.end
.else
;===
; Check if multiple buttons are pressed
;===
; OR button masks together to create a single mask
.define mask\.\@ 0
.repeat NARGS - 1
utils.assert.range \1, input.UP, input.BUTTON_2, "input.asm \.: Invalid button argument"
.redefine mask\.\@ mask\.\@ | \1
.shift ; shift arguments so \2 becomes \1
.endr
; Assert remaining \1 argument is the else label
utils.assert.label \1, "input.asm \.: Expected last argument to be a label"
utils.clobbers.withBranching "af"
input.loadAHeld ; load A with held buttons
and mask\.\@ ; clear other buttons
cp mask\.\@ ; compare result with mask
utils.clobbers.end.jpnz \1 ; jp to else if not all buttons are held
utils.clobbers.end
.endif
.endm
;====
; Check if the given button(s) have just been pressed this frame
;
; @in ...buttons one or more button(s) to check (input.UP, input.BUTTON_1 etc)
; @in else the address to jump to if the button(s) are either not
; pressed, or were already pressed last frame
;====
.macro "input.ifPressed"
.if NARGS == 2
utils.assert.range \1, input.UP, input.BUTTON_2, "input.asm \.: Invalid button argument"
utils.assert.label \2, "input.asm \.: Invalid label argument"
; Load input that was released last frame but is now pressed
utils.clobbers.withBranching "af"
input.loadAPressed
and \1 ; check button bit
utils.clobbers.end.jpz \2 ; jp to else if the bit was not set
utils.clobbers.end
.else
;===
; Check if all buttons are pressed, and that not all of them were pressed
; last frame. This is a little more complex so the buttons don't all
; have to have been pressed down in a single frame
;===
; OR button masks together to create a single mask
.define mask\.\@ 0
.repeat NARGS - 1
utils.assert.range \1, input.UP, input.BUTTON_2, "input.asm \.: Invalid button argument"
.redefine mask\.\@ mask\.\@ | \1
.shift ; shift arguments so \2 becomes \1
.endr
; Assert remaining \1 argument is the else label
utils.assert.label \1, "input.asm \.: Expected last argument to be a label"
utils.clobbers.withBranching "af" "hl"
; Load H with previous and L with current
ld hl, (input.ram.activePort.current)
; If all given buttons are currently pressed
ld a, l ; load current into A
ld l, mask\.\@ ; load buttons mask into L
and l ; filter out other buttons
cp l ; check if all given buttons are currently pressed
utils.clobbers.end.jpnz \1 ; jp if all given buttons aren't pressed
; All given buttons are pressed; Check if they were pressed last frame
ld a, h ; load previous input
and l ; filter out other buttons
cp l ; check if all given buttons are currently pressed
; Jump to else if all were already pressed last frame
utils.clobbers.end.jpz \1
utils.clobbers.end
.endif
.endm
;====
; Check if the given button(s) have just been released this frame
;
; @in ...buttons one or more button(s) to check (input.UP, input.BUTTON_1 etc)
; @in else the address to jump to if the button(s) are either not
; pressed, or were already pressed last frame
;====
.macro "input.ifReleased"
.if NARGS == 2
utils.assert.range \1, input.UP, input.BUTTON_2, "input.asm \.: Invalid button argument"
utils.assert.label \2, "input.asm \.: Invalid label argument"
; Load input that was released last frame but is now pressed
utils.clobbers.withBranching "af"
input.loadAReleased
and \1 ; check button bit
utils.clobbers.end.jpz \2 ; jp to else if the bit was not set
utils.clobbers.end
.else
; OR button masks together to create a single mask
.define mask\.\@ 0
.repeat NARGS - 1
utils.assert.range \1, input.UP, input.BUTTON_2, "input.asm \.: Invalid button argument"
.redefine mask\.\@ mask\.\@ | \1
.shift ; shift arguments so \2 becomes \1
.endr
; Assert remaining \1 argument is the else label
utils.assert.label \1, "input.asm \.: Expected last argument to be a label"
;===
; Check if all buttons had been pressed last frame, but not all are now
;===
utils.clobbers.withBranching "af" "hl"
; Load H with previous and L with current
ld hl, (input.ram.activePort.current)
; If all given buttons were pressed
ld a, h ; load previous into A
ld h, mask\.\@ ; load buttons mask into H
and h ; filter out other buttons
cp h ; check if all given buttons were pressed
; Jump if all given buttons weren't pressed last frame
utils.clobbers.end.jpnz \1
; All given buttons were pressed; Check if any are now released
ld a, l ; load current input
and h ; filter out other buttons
cp h ; check if all given buttons are currently pressed
; Jump to else if all are still pressed
utils.clobbers.end.jpz \1
utils.clobbers.end
.endif
.endm
;====
; Checks if either direction on an axis is pressed and jumps to the relevant
; label. If the negative direction (left or up) on the axis is pressed, it
; will continue the code flow without jumping.
;
; Should be called within a utils.clobbers.withBranching scope protecting AF
;
; @in a input value to check (--21RLDU)
;
; @in negativeDir negative direction on the axis (input.LEFT or input.UP)
; @in positiveDir positive direction on the axis (input.RIGHT or input.DOWN)
; @in negativeLabel will continue to this label if the negative direction is
; pressed
; @in positiveLabel label to jump to if the positive direction is pressed
; @in else label to jump to if neither direction is pressed
;====
.macro "input._jpIfDirection" args negativeDir positiveDir negativeLabel positiveLabel elseLabel
utils.assert.equals NARGS 5 "input.asm \.: Invalid number of arguments given"
utils.assert.oneOf negativeDir, input.LEFT, input.UP, "input.asm \.: Invalid 'negativeDir' argument"
utils.assert.oneOf positiveDir, input.RIGHT, input.DOWN, "input.asm \.: Invalid 'positiveDir' argument"
utils.assert.label negativeLabel "input.asm \.: Invalid 'negativeLabel' argument"
utils.assert.label positiveLabel "input.asm \.: Invalid 'positiveLabel' argument"
utils.assert.label elseLabel "input.asm \.: Invalid 'elseLabel' argument"
and negativeDir | positiveDir ; check if either direction is pressed
utils.clobbers.end.jpz elseLabel ; jp to else label if neither are pressed
and positiveDir ; check positive direction
utils.clobbers.end.jpnz positiveLabel ; jp if positive direction pressed
; ...continue to the negativeLabel handler
.endm
;====
; Jumps to the relevant label if either left or right are currently pressed
;
; @in left the label to continue to if LEFT is currently pressed
; @in right the label to jp to if RIGHT is currently pressed
; @in else the label to jp to if neither LEFT nor RIGHT are currently pressed
;====
.macro "input.ifXDir" args left right else
utils.assert.equals NARGS 3 "input.asm \.: Invalid number of arguments given"
utils.assert.label left "input.asm \.: Invalid 'left' argument"
utils.assert.label right "input.asm \.: Invalid 'right' argument"
utils.assert.label else "input.asm \.: Invalid 'else' argument"
; Load currently pressed input and jump to relevant label
utils.clobbers.withBranching "af"
ld a, (input.ram.activePort.current)
input._jpIfDirection input.LEFT input.RIGHT left right else
utils.clobbers.end
.endm
;====
; Jumps to the relevant label if either left or right have been pressed for
; both this frame and the previous frame
;
; @in left the label to continue to if LEFT is held
; @in right the label to jp to if RIGHT is held
; @in else the label to jp to if neither LEFT nor RIGHT are held
;====
.macro "input.ifXDirHeld" args left right else
utils.assert.equals NARGS 3 "input.asm \.: Invalid number of arguments given"
utils.assert.label left "input.asm \.: Invalid 'left' argument"
utils.assert.label right "input.asm \.: Invalid 'right' argument"
utils.assert.label else "input.asm \.: Invalid 'else' argument"
utils.clobbers.withBranching "af"
; Load held input and jump to relevant label
input.loadAHeld
input._jpIfDirection input.LEFT input.RIGHT left right else
utils.clobbers.end
.endm
;====
; Jumps to the relevant label if either left or right have just been pressed
; this frame
;
; @in left the label to continue to if LEFT has just been pressed
; @in right the label to jp to if RIGHT had just been pressed
; @in else the label to jp to if neither LEFT nor RIGHT have just been pressed
;====
.macro "input.ifXDirPressed" args left right else
utils.assert.equals NARGS 3 "input.asm \.: Invalid number of arguments given"
utils.assert.label left "input.asm \.: Invalid 'left' argument"
utils.assert.label right "input.asm \.: Invalid 'right' argument"
utils.assert.label else "input.asm \.: Invalid 'else' argument"
utils.clobbers.withBranching "af"
; Load input that was released last frame but is now pressed
input.loadAPressed
; Jump to the relevant label
input._jpIfDirection input.LEFT input.RIGHT left right else
utils.clobbers.end
.endm
;====
; Jumps to the relevant label if either left or right were pressed but have
; just been released
;
; @in left the label to continue to if LEFT has just been released
; @in right the label to jp to if RIGHT had just been released
; @in else the label to jp to if neither LEFT nor RIGHT have just been released
;====
.macro "input.ifXDirReleased" args left right else
utils.assert.equals NARGS 3 "input.asm \.: Invalid number of arguments given"
utils.assert.label left "input.asm \.: Invalid 'left' argument"
utils.assert.label right "input.asm \.: Invalid 'right' argument"
utils.assert.label else "input.asm \.: Invalid 'else' argument"
utils.clobbers.withBranching "af"
; Load input that was pressed last frame but has just been released
input.loadAReleased
; Jump to the relevant label
input._jpIfDirection input.LEFT input.RIGHT left right else
utils.clobbers.end
.endm
;====
; Jumps to the relevant label if either up or down are currently pressed
;
; @in up the label to continue to if UP is currently pressed
; @in down the label to jp to if DOWN is currently pressed
; @in else the label to jp to if neither UP nor DOWN are currently pressed
;====
.macro "input.ifYDir" args up down else
utils.assert.equals NARGS 3 "input.asm \.: Invalid number of arguments given"
utils.assert.label up "input.asm \.: Invalid 'up' argument"
utils.assert.label down "input.asm \.: Invalid 'down' argument"
utils.assert.label else "input.asm \.: Invalid 'else' argument"
; Load currently pressed direction and jump to the relevant label
utils.clobbers.withBranching "af"
ld a, (input.ram.activePort.current)
input._jpIfDirection input.UP input.DOWN up down else
utils.clobbers.end
.endm
;====
; Jumps to the relevant label if either up or down have been pressed for this
; frame and the previous frame
;
; @in up the label to continue to if UP is held
; @in down the label to jp to if DOWN is held
; @in else the label to jp to if neither UP nor DOWN are held
;====
.macro "input.ifYDirHeld" args up down else
utils.assert.equals NARGS 3 "input.asm \.: Invalid number of arguments given"
utils.assert.label up "input.asm \.: Invalid 'up' argument"
utils.assert.label down "input.asm \.: Invalid 'down' argument"
utils.assert.label else "input.asm \.: Invalid 'else' argument"
; Load held buttons and jump to the relevant label
utils.clobbers.withBranching "af"
input.loadAHeld
input._jpIfDirection input.UP input.DOWN up down else
utils.clobbers.end
.endm
;====
; Jumps to the relevant label if either up or down have just been pressed this
; frame.
;
; @in up the label to continue to if UP has just been pressed
; @in down the label to jp to if DOWN had just been pressed
; @in else the label to jp to if neither UP or DOWN have just been pressed
;====
.macro "input.ifYDirPressed" args up down else
utils.assert.equals NARGS 3 "input.asm \.: Invalid number of arguments given"
utils.assert.label up "input.asm \.: Invalid 'up' argument"
utils.assert.label down "input.asm \.: Invalid 'down' argument"
utils.assert.label else "input.asm \.: Invalid 'else' argument"
utils.clobbers.withBranching "af"
; Load input that was released last frame but is now pressed
input.loadAPressed
; Jump to the relevant label
input._jpIfDirection input.UP input.DOWN up down else
utils.clobbers.end
.endm
;====
; Jumps to the relevant label if either up or down were pressed last frame but
; have now been released
;
; @in up the label to continue to if UP has just been released
; @in down the label to jp to if DOWN had just been released
; @in else the label to jp to if neither UP or DOWN have just been released
;====
.macro "input.ifYDirReleased" args up down else
utils.assert.equals NARGS 3 "input.asm \.: Invalid number of arguments given"
utils.assert.label up "input.asm \.: Invalid 'up' argument"
utils.assert.label down "input.asm \.: Invalid 'down' argument"
utils.assert.label else "input.asm \.: Invalid 'else' argument"
utils.clobbers.withBranching "af"
; Load input that was pressed but has just been released
input.loadAReleased
; Jump to the relevant label
input._jpIfDirection input.UP input.DOWN up down else
utils.clobbers.end
.endm
;====
; Load the X direction (left/right) into register A. By default, -1 = left,
; 1 = right, 0 = none. The result is multiplied by the optional multiplier
; at assemble time
;
; Ensure you have called input.readPort1 or input.readPort2
;
; @in [multiplier] optional multiplier for the result (default 1)
; @out a -1 = left, 1 = right, 0 = none. This value will be
; multiplied by the multiplier at assemble time
;====
.macro "input.loadADirX" isolated args multiplier
.ifndef multiplier
.redefine multiplier 1
.endif
utils.assert.number multiplier, "input.asm \.: Expected multiplier to be a number"
; Read current input data
ld a, (input.ram.activePort.current)
; Check if left is being pressed
bit input.LEFT_BIT, a
jp z, +
; Left is pressed
ld a, -1 * multiplier
jp \.\@end
+:
; Check if right is being pressed
bit input.RIGHT_BIT, a
jp z, +
; Right is pressed
ld a, 1 * multiplier
jp \.\@end
+:
; Nothing pressed
xor a ; a = 0
\.\@end:
.endm
;====
; Load the Y direction (up/down) into register A. By default, -1 = up,
; 1 = down, 0 = none. The result is multiplied by the optional multiplier
; at assemble time
;
; Ensure you have called input.readPort1 or input.readPort2
;
; @in [multiplier] optional multiplier for the result (default 1)
; @out a -1 = up, 1 = down, 0 = none. This will be multiplied
; by the multiplier at assemble time
;====
.macro "input.loadADirY" isolated args multiplier
.ifndef multiplier
.redefine multiplier 1
.endif
utils.assert.number multiplier, "input.asm \.: Expected multiplier to be a number"
; Read current input data
ld a, (input.ram.activePort.current)
; Check if up is being pressed
bit input.UP_BIT, a
jp z, +
; Up is pressed
ld a, -1 * multiplier
jp \.\@end
+:
; Check if down is being pressed
bit input.DOWN_BIT, a
jp z, +
; Down is pressed
ld a, 1 * multiplier
jp \.\@end
+:
; Nothing pressed
xor a ; a = 0
\.\@end:
.endm