-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoypad_bounce_test.asm
More file actions
643 lines (481 loc) · 16 KB
/
joypad_bounce_test.asm
File metadata and controls
643 lines (481 loc) · 16 KB
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
// A test ROM design to display the contact bound on key-press and key-release.
//
// It reads 12 bits of controller port 1 every scanline, writes the data to a buffer and waits for
// no button presses for 262 scanlines (1 frame). Then it builds four HDMA tables to display ~200
// scanlines after button-press and ~200 scanlines before button-release.
//
// For each pair of vertical lines represents one button.
// The left line is scanlines after button press and the right line is scanlines before release.
//
// CAUTION: This test is designed for one button at a time.
// CAUTION: This test cannot read controller data and build the HDMA tables at the same time.
//
// SPDX-FileCopyrightText: © 2024 Marcus Rowe <undisbeliever@gmail.com>
// SPDX-License-Identifier: Zlib
//
// Copyright © 2024 Marcus Rowe <undisbeliever@gmail.com>
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the
// use of this software.
//
// Permission is granted to anyone to use this software for any purpose, including
// commercial applications, and to alter it and redistribute it freely, subject to
// the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software in
// a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
define MEMORY_MAP = LOROM
define ROM_SIZE = 1
define ROM_SPEED = fast
define REGION = Japan
define ROM_NAME = "JOYPAD BOUNCE TEST"
define VERSION = 1
architecture wdc65816-strict
include "../common.inc"
createCodeBlock(code, 0x808000, 0x80ffaf)
createRamBlock(zeropage, 0x00, 0xff)
createRamBlock(lowram, 0x7e0100, 0x7e1eff)
createRamBlock(stack, 0x7e1f00, 0x7e1fff)
include "../reset_handler.inc"
include "../break_handler.inc"
include "../dma_forceblank.inc"
// This test does not use interrupts
constant NmiHandler = BreakHandler.ISR
// All four backgrounds use the same tilemap and tiles
constant VRAM_BG_TILES_WADDR = 0x1000
constant VRAM_BG_MAP_WADDR = 0x0000
constant VRAM_OBJ_TILES_WADDR = 0x6000
constant N_BITS = 12;
constant SPACING_BETWEEN_BITS = 12
constant SPACING_BETWEEN_LINES = 4
constant TOTAL_WIDTH = (N_BITS - 1) * SPACING_BETWEEN_BITS + SPACING_BETWEEN_LINES + 2
constant BG1_X_OFFSET = -(256 - TOTAL_WIDTH) / 2
constant BG2_X_OFFSET = BG1_X_OFFSET - 4 * SPACING_BETWEEN_BITS
constant BG3_X_OFFSET = BG1_X_OFFSET - SPACING_BETWEEN_LINES
constant BG4_X_OFFSET = BG2_X_OFFSET - SPACING_BETWEEN_LINES
constant DISPLAY_HEIGHT = 224
constant Y_OFFSET = 10
constant N_SCANLINES_TO_DISPLAY = DISPLAY_HEIGHT - Y_OFFSET
// Number of scanlines of no-button presses to wait if the MainLoop starts with a button pressed
constant WAIT_FOR_RELEASE_THREASHOLD = 262
// Number of scanlines with 0 depressed buttons before displaying data.
constant RELEASE_THREASHOLD = 262 // 1 frame
constant PRESS_BUFFER_SIZE = N_SCANLINES_TO_DISPLAY
constant RELEASE_BUFFER_SIZE = 0x600
assert(RELEASE_BUFFER_SIZE > DISPLAY_HEIGHT + RELEASE_THREASHOLD + 32)
allocate(zpTmpByte, zeropage, 1)
// Used to determine if the release buffer is empty
// HDMA double buffer control variable
// (word index into `HdmaTable*_TableAddr`)
allocate(currentHdmaTable, zeropage, 2)
// Determines if the `buttonReleaseBuffer` is full or not.
// (byte flag)
allocate(releaseBufferNotFull, zeropage, 1)
// Index of the first line to of the release buffer to draw.
// (word index into `buttonReleaseBuffer_*`).
allocate(releaseBufferIndex, zeropage, 2)
// Button press buffer
allocate(buttonPressBuffer_l, lowram, PRESS_BUFFER_SIZE)
allocate(buttonPressBuffer_h, lowram, PRESS_BUFFER_SIZE)
// Button release circular buffer
allocate(buttonReleaseBuffer_l, lowram, RELEASE_BUFFER_SIZE)
allocate(buttonReleaseBuffer_h, lowram, RELEASE_BUFFER_SIZE)
assert(DISPLAY_HEIGHT * 2 + 32 < 0x200)
allocate(hdmaTable1_a, lowram, 0x200)
allocate(hdmaTable1_b, lowram, 0x200)
allocate(hdmaTable2_a, lowram, 0x200)
allocate(hdmaTable2_b, lowram, 0x200)
allocate(hdmaTable3_a, lowram, 0x200)
allocate(hdmaTable3_b, lowram, 0x200)
allocate(hdmaTable4_a, lowram, 0x200)
allocate(hdmaTable4_b, lowram, 0x200)
// The number of times the HDMA tables have been displayed
// (4x 0-9 digits)
allocate(readCounterDigits, lowram, 4)
constant readCounterDigits_size = 4
// DB = 0x80
macro WaitForStartOfVBlank() {
assert8a()
InVBlankLoop{#}:
assert(HVBJOY.vBlank == 0x80)
bit.w HVBJOY
bmi InVBlankLoop{#}
WaitForVBlankLoop{#}:
assert(HVBJOY.vBlank == 0x80)
bit.w HVBJOY
bpl WaitForVBlankLoop{#}
}
// DB = 0x80
macro WaitForHBlank() {
assert8a()
// Wait for H-Blank
Loop{#}:
assert(HVBJOY.hBlank == 0x40)
bit.w HVBJOY
bvc Loop{#}
}
// OUT: A = joypad low byte
// KEEP: X, Y
// DB = 0x80
macro ReadJoypadBits_l() {
assert8a()
// Latch the joypad
lda.b #JOYSER0.latch
sta.w JOYSER0
stz.w JOYSER0
variable _i = 0
while _i < 7 {
lda.w JOYSER0
lsr
rol.b zpTmpByte
_i = _i + 1
}
lda.w JOYSER0
lsr
lda.b zpTmpByte
rol
}
// OUT: A = joypad hight byte (CAUTION: not rotated)
// KEEP: X, Y
// DB = 0x80
macro ReadJoypadBits_h() {
variable _i = 0
while _i < 3 {
lda.w JOYSER0
lsr
rol.b zpTmpByte
_i = _i + 1
}
lda.w JOYSER0
lsr
lda.b zpTmpByte
and.b #0b111
rol
}
// DB = 0x80
au()
iu()
code()
function Main {
rep #$30
sep #$20
a8()
i16()
stz.w NMITIMEN
// Set PPU registers
lda.b #INIDISP.force | 0xf
sta.w INIDISP
lda.b #(VRAM_BG_MAP_WADDR / BGXSC.base.walign) << BGXSC.base.shift | BGXSC.map.s32x32
sta.w BG1SC
sta.w BG2SC
sta.w BG3SC
sta.w BG4SC
lda.b #((VRAM_BG_TILES_WADDR / BG12NBA.walign) << BG12NBA.bg1.shift) | ((VRAM_BG_TILES_WADDR / BG12NBA.walign) << BG12NBA.bg2.shift)
sta.w BG12NBA
sta.w BG34NBA
lda.b #OBSEL.size.s8_32 | (VRAM_OBJ_TILES_WADDR / OBSEL.base.walign) << OBSEL.base.shift
sta.w OBSEL
lda.b #TM.bg1 | TM.bg2 | TM.bg3 | TM.bg4 | TM.obj
sta.w TM
lda.b #BG1_X_OFFSET
sta.w BG1HOFS
lda.b #BG1_X_OFFSET >> 16
sta.w BG1HOFS
lda.b #BG2_X_OFFSET
sta.w BG2HOFS
lda.b #BG2_X_OFFSET >> 16
sta.w BG2HOFS
lda.b #BG3_X_OFFSET
sta.w BG3HOFS
lda.b #BG3_X_OFFSET >> 16
sta.w BG3HOFS
lda.b #BG4_X_OFFSET
sta.w BG4HOFS
lda.b #BG4_X_OFFSET >> 16
sta.w BG4HOFS
ldx.w #VRAM_BG_MAP_WADDR
stx.w VMADD
Dma.ForceBlank.ToVram(Resources.Bg_Tilemap)
ldx.w #VRAM_BG_TILES_WADDR
stx.w VMADD
Dma.ForceBlank.ToVram(Resources.Bg_Tiles)
ldx.w #VRAM_OBJ_TILES_WADDR
stx.w VMADD
Dma.ForceBlank.ToVram(Resources.Obj_Tiles)
stz.w CGADD
Dma.ForceBlank.ToCgram(Resources.Palette)
Dma.ForceBlank.ToCgram(Resources.Palette)
Dma.ForceBlank.ToCgram(Resources.ObjPalette)
stz.w OAMADDL
stz.w OAMADDL
Dma.ForceBlank.ToOam(Resources.Oam)
// Clear Hi table
ldx.w #128 * 2
stx.w OAMADD
stz.w OAMDATA
// Reset readCounterDigits
ldx.w #readCounterDigits_size - 1
lda.b #9
-
sta.w readCounterDigits,x
dex
bpl -
ldx.w #0
jsr BuildAndDisplayHdmaTables
lda.b #15
sta.w INIDISP
MainLoop:
lda.b #1
sta.b releaseBufferNotFull
// If a button has been pressed, Wait for WAIT_FOR_RELEASE_THREASHOLD scanlines of no button presses.
// (To ensure buttonPressBuffer contains the initial key-press and contact bounce data)
ldx.w #1
WaitForKeyRelease:
WaitForHBlank()
ReadJoypadBits_l()
sta.b zpTmpByte
ReadJoypadBits_h()
ora.b zpTmpByte
beq +
ldx.w #WAIT_FOR_RELEASE_THREASHOLD
+
dex
bne WaitForKeyRelease
// Wait until a button has been pressed
WaitForKeyPressLoop:
WaitForHBlank()
ReadJoypadBits_l()
sta.w buttonPressBuffer_l + PRESS_BUFFER_SIZE - 1
ReadJoypadBits_h()
sta.w buttonPressBuffer_h + PRESS_BUFFER_SIZE - 1
ora.w buttonPressBuffer_l + PRESS_BUFFER_SIZE - 1
beq WaitForKeyPressLoop
// Read `N_KEYDOWN_SCANLINES`
ldx.w #PRESS_BUFFER_SIZE - 2
KeyPressLoop:
WaitForHBlank()
ReadJoypadBits_l()
sta.w buttonPressBuffer_l,x
ReadJoypadBits_h()
sta.w buttonPressBuffer_h,x
dex
bpl KeyPressLoop
ldy.w #RELEASE_THREASHOLD
ldx.w #RELEASE_BUFFER_SIZE
KeyReleaseLoop:
dex
bpl +
ldx.w #RELEASE_BUFFER_SIZE - 1
stz.b releaseBufferNotFull
+
WaitForHBlank()
ReadJoypadBits_l()
sta.w buttonReleaseBuffer_l,x
ReadJoypadBits_h()
sta.w buttonReleaseBuffer_h,x
ora.w buttonReleaseBuffer_l,x
beq +
ldy.w #RELEASE_THREASHOLD
jmp KeyReleaseLoop
+
dey
bne KeyReleaseLoop
rep #$10
i16()
jsr BuildAndDisplayHdmaTables
jmp MainLoop
}
// IN: X = buttonReleaseBuffer index
// DB = 0x7e
a8()
i16()
function BuildAndDisplayHdmaTables {
lda.b releaseBufferNotFull
rep #$30
a16()
beq +
cpx.w #RELEASE_BUFFER_SIZE - RELEASE_THREASHOLD - N_SCANLINES_TO_DISPLAY
bcc +
lda.w #RELEASE_BUFFER_SIZE - 1
bra ++
+
// The release buffer is full or contains enough data to display
txa
clc
adc.w #RELEASE_THREASHOLD + N_SCANLINES_TO_DISPLAY - 1
cmp.w #RELEASE_BUFFER_SIZE
bcc +
// carry set
sbc.w #RELEASE_BUFFER_SIZE
+
sta.b releaseBufferIndex
sep #$20
a8()
ldy.b currentHdmaTable
beq +
ldy.w #0
bra ++
+
ldy.w #2
+
sty.w currentHdmaTable
// IN: X = table offset
macro _BuildTable(evaluate table, evaluate data, evaluate bufferSize, evaluate yOffset) {
assert8a()
assert16i()
ldy.b currentHdmaTable
lda.w HdmaTable{table}_TableAddr,y
sta.w WMADDL
lda.w HdmaTable{table}_TableAddr + 1,y
sta.w WMADDM
stz.w WMADDH
ldy.w #-1
if {yOffset} > 0 {
// HDMA table entry - repeat mode, yOffset lines
lda.b #0x80 | {yOffset}
sta.w WMDATA
-
tya
sta.w WMDATA
stz.w WMDATA
dey
cpy.w #-1 - {yOffset}
bne -
}
// Use a different `nScanlines1` value for each table to stagger HDMA entries
// and the reduce maximum HDMA time in H-Blank.
evaluate nScanlines1 = 127 - {table}
evaluate nScanlines2 = DISPLAY_HEIGHT - {nScanlines1} - {yOffset}
assert({nScanlines2} < 127)
// HDMA table entry - repeat mode, nScanlines1 lines
lda.b #0x80 | {nScanlines1}
sta.w WMDATA
-
tya
clc
adc.w {data},x
sta.w WMDATA
stz.w WMDATA
dex
bpl +
ldx.w #{bufferSize} - 1
+
dey
cpy.w #-1 - {yOffset} - {nScanlines1}
bne +
// HDMA table entry - repeat mode, nScanlines2 lines
lda.b #0x80 | {nScanlines2}
sta.w WMDATA
+
cpy.w #-1 - DISPLAY_HEIGHT
bne -
// HDMA end
stz.w WMDATA
}
ldx.w #PRESS_BUFFER_SIZE - 1
_BuildTable(1, buttonPressBuffer_l, PRESS_BUFFER_SIZE, Y_OFFSET)
ldx.w #PRESS_BUFFER_SIZE - 1
_BuildTable(2, buttonPressBuffer_h, PRESS_BUFFER_SIZE, Y_OFFSET)
ldx.b releaseBufferIndex
_BuildTable(3, buttonReleaseBuffer_l, RELEASE_BUFFER_SIZE, 0)
ldx.b releaseBufferIndex
_BuildTable(4, buttonReleaseBuffer_h, RELEASE_BUFFER_SIZE, 0)
// Clear release buffer
ldx.w #RELEASE_BUFFER_SIZE - 1
-
stz.w buttonReleaseBuffer_l,x
stz.w buttonReleaseBuffer_h,x
dex
bpl -
// Increment read counter digits
ldx.w #readCounterDigits_size - 1
-
inc.w readCounterDigits,x
lda.w readCounterDigits,x
cmp.b #10
bcc +
stz.w readCounterDigits,x
dex
bpl -
+
WaitForStartOfVBlank()
ldy.b currentHdmaTable
macro _SetupHdmaChannel(evaluate c, evaluate bBusAddr, addrTable) {
assert8a()
assert16i()
lda.b #DMAP.direction.toPpu | DMAP.transfer.writeTwice
sta.w DMAP{c}
lda.b #{bBusAddr}
sta.w BBAD{c}
ldx.w {addrTable},y
stx.w A1T{c}
stz.w A1B{c}
}
_SetupHdmaChannel(7, BG1VOFS, HdmaTable1_TableAddr)
_SetupHdmaChannel(6, BG2VOFS, HdmaTable2_TableAddr)
_SetupHdmaChannel(5, BG3VOFS, HdmaTable3_TableAddr)
_SetupHdmaChannel(4, BG4VOFS, HdmaTable4_TableAddr)
lda.b #HDMAEN.dma7 | HDMAEN.dma6 | HDMAEN.dma5 | HDMAEN.dma4
sta.w HDMAEN
// Transfer readCounterDigits to OAM
variable _i = 0
while _i < readCounterDigits_size {
ldx.w #_i * 2 + 1
stx.w OAMADD
lda.w readCounterDigits + _i
sta.w OAMDATA
stz.w OAMDATA // OAMDATA is a write-twice register when writing to the low-table
_i = _i + 1
}
rts
}
HdmaTable1_TableAddr:
dw hdmaTable1_a, hdmaTable1_b
HdmaTable2_TableAddr:
dw hdmaTable2_a, hdmaTable2_b
HdmaTable3_TableAddr:
dw hdmaTable3_a, hdmaTable3_b
HdmaTable4_TableAddr:
dw hdmaTable4_a, hdmaTable4_b
namespace Resources {
insert Bg_Tiles, "../../gen/test-patterns/scanline-bit-pattern.2bpp"
insert Bg_Tilemap, "../../gen/test-patterns/scanline-bit-pattern.tilemap"
insert Obj_Tiles, "../../gen/test-patterns/obj-digits-4bpp-tiles.tiles"
Palette:
dw 0, 0, ToPalette( 6, 6, 0), ToPalette(31, 31, 0) // B (yellow)
dw 0, 0, ToPalette( 0, 8, 0), ToPalette( 0, 24, 0) // Y (green)
dw 0, 0, ToPalette( 6, 0, 6), ToPalette(31, 0, 31) // select
dw 0, 0, ToPalette( 6, 0, 6), ToPalette(31, 0, 31) // start
dw 0, 0, ToPalette( 6, 6, 6), ToPalette(31, 31, 31) // up
dw 0, 0, ToPalette( 6, 6, 6), ToPalette(31, 31, 31) // down
dw 0, 0, ToPalette( 6, 6, 6), ToPalette(31, 31, 31) // left
dw 0, 0, ToPalette( 6, 6, 6), ToPalette(31, 31, 31) // right
// Shifting BG2 4 lines saves me 4 rol instructions
dw 0, 0, 0, 0
dw 0, 0, 0, 0
dw 0, 0, 0, 0
dw 0, 0, 0, 0
dw 0, 0, ToPalette( 8, 0, 0), ToPalette(31, 0, 0) // A (red)
dw 0, 0, ToPalette( 0, 0, 8), ToPalette( 6, 6, 31) // X (blue)
dw 0, 0, ToPalette( 6, 4, 6), ToPalette(25, 20, 25) // L
dw 0, 0, ToPalette( 6, 4, 6), ToPalette(25, 20, 25) // R
constant Palette.size = pc() - Palette
assert(Palette.size == 16 * 4 * 2)
insert ObjPalette, "../../gen/test-patterns/obj-digits-4bpp-tiles.pal"
Oam:
variable _i = 0
while _i < readCounterDigits_size {
db 218 + _i * 6, 16, 0, 0
_i = _i + 1
}
constant Oam.size = pc() - Oam
}
finalizeMemory()