Files
Quadrapassel/tests/keyboard_test.c
T
Brian Holman a07c41555a Fix target joystick calibration arguments and scene transitions
Avoid a demonstrated CMOC -O2 removed-index-reload error with independent calibration and position pointers. Keep established joystick readiness across menus, stabilize shared fire/keyboard scans, and protect input sampling from IRQ interference.

Ship pristine QUADPSL04.DSK. Native right-port emulated analog/button checks pass for both directions, four lasting T-piece turns, and steering after keyboard resume. Host input/store/rules/video regressions and unchanged memory guards pass. Preserve personal score disks and record physical/128 KB/audio limits.
2026-11-11 14:26:06 -07:00

94 lines
4.9 KiB
C

/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <coco.h>
#include "keyboard.h"
static Q8 raw_buttons=15,contact_probe,contact_bit,edge_at,probes,edge_buttons;
byte readJoystickButtons(void){return raw_buttons;}
/* Model the shared PIA input lines: a fire button grounds its row for EVERY
* keyboard column probe. A real key grounds it only for its selected column. */
byte isKeyPressed(byte probe,byte bit)
{
++probes;
if(edge_at&&probes==edge_at){raw_buttons=edge_buttons;edge_at=0;}
if((bit&15)&~raw_buttons)return 1;
return probe==contact_probe&&(bit&contact_bit)!=0;
}
static QActions poll(QInput *in,Q8 button,Q16 ms)
{
return q_input_step(in,keyboard_read(&raw_buttons,KEYBOARD_PLAY),button,31,31,31,31,1,ms,1,0);
}
int main(void)
{
QInput in;QActions a;QGame game;QOptions options={0,Q_MODERN,0,1};Q8 pressed;
/* CoCo right fire grounds A/D/E. It must not masquerade as left/right/CW. */
raw_buttons=14;assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==0);
for(pressed=0;pressed<4;++pressed){
raw_buttons=(Q8)(15&~(1<<pressed));
assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==0);
contact_probe=KEY_PROBE_BREAK;contact_bit=KEY_BIT_BREAK;
assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==Q_BACK);
contact_bit=0;
}
raw_buttons=15;q_input_reset(&in);poll(&in,0,0);
q_start(&game,&options);game.active=q_spawn(0,0);game.active.y=6;
raw_buttons=14;a=poll(&in,1,100);
assert(!(a.pressed&(Q_CW|Q_CCW|Q_HARD|Q_HOLD))&&!a.repeats);
a=poll(&in,1,100);assert(!(a.pressed&(Q_CW|Q_CCW|Q_PAUSE)));
raw_buttons=15;a=poll(&in,0,1);assert(a.pressed&Q_CCW);
assert(q_rotate(&game,0)&&game.active.rotation==1);
a=poll(&in,0,1);assert(!(a.pressed&(Q_CW|Q_CCW))&&game.active.rotation==1);
/* Fire held while steering must not add opposing A/D or E actions. */
q_input_reset(&in);poll(&in,0,0);raw_buttons=14;
a=q_input_step(&in,keyboard_read(&raw_buttons,KEYBOARD_PLAY),1,0,31,31,31,1,1,1,0);
assert(a.move==-1&&a.repeats==1&&!(a.pressed&Q_CW));
a=q_input_step(&in,keyboard_read(&raw_buttons,KEYBOARD_PLAY),1,63,31,31,31,1,1,1,0);
assert(a.move==1&&a.repeats==1&&!(a.pressed&Q_CW));
/* Clockwise preference also yields one edge on release, not on press. */
raw_buttons=15;q_input_reset(&in);poll(&in,0,0);raw_buttons=14;
a=q_input_step(&in,keyboard_read(&raw_buttons,KEYBOARD_PLAY|KEYBOARD_CLOCKWISE),1,31,31,31,31,1,100,1,1);
assert(!(a.pressed&(Q_CW|Q_CCW)));raw_buttons=15;
a=q_input_step(&in,keyboard_read(&raw_buttons,KEYBOARD_PLAY|KEYBOARD_CLOCKWISE),0,31,31,31,31,1,1,1,1);
assert((a.pressed&Q_CW)&&!(a.pressed&Q_CCW));
/* Real keyboard aliases still work when their shared row is available. */
contact_probe=KEY_PROBE_E;contact_bit=KEY_BIT_E;assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==Q_CW);
contact_probe=KEY_PROBE_A;contact_bit=KEY_BIT_A;assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==Q_LEFT);
contact_probe=KEY_PROBE_D;contact_bit=KEY_BIT_D;assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==Q_RIGHT);
assert(keyboard_read(&raw_buttons,KEYBOARD_INITIALS)==0);
contact_probe=KEY_PROBE_UP;contact_bit=KEY_BIT_UP;
assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==Q_CCW);
assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY|KEYBOARD_CLOCKWISE)==Q_CW);
contact_probe=KEY_PROBE_SPACE;contact_bit=KEY_BIT_SPACE;
assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==Q_HARD);
assert(keyboard_read(&raw_buttons,0)==Q_CONFIRM);
/* A fire press arriving after the opening sample must cause a rescan.
* Every reachable scan boundary is checked, including held/released edges. */
contact_bit=0;
{
static const Q8 ambiguous[4]={3,1,4,5};Q8 line;
for(line=0;line<4;++line){
for(pressed=1;pressed<=15;++pressed){
raw_buttons=15;probes=0;edge_at=pressed;edge_buttons=(Q8)(15&~(1<<line));
assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==0&&!edge_at);
}
for(pressed=1;pressed<=15-ambiguous[line];++pressed){
raw_buttons=(Q8)(15&~(1<<line));probes=0;edge_at=pressed;edge_buttons=15;
assert(keyboard_read(&raw_buttons,KEYBOARD_PLAY)==0&&!edge_at);
}
}
}
/* Four releases advance an asymmetric piece through every orientation. */
raw_buttons=15;q_input_reset(&in);poll(&in,0,0);
game.active=q_spawn(0,0);game.active.y=6;
for(pressed=0;pressed<4;++pressed){
raw_buttons=14;a=poll(&in,1,100);assert(!(a.pressed&(Q_CW|Q_CCW)));
raw_buttons=15;a=poll(&in,0,1);assert(a.pressed&Q_CCW);
assert(q_rotate(&game,0)&&game.active.rotation==(pressed+1)%4);
a=poll(&in,0,1);assert(!(a.pressed&(Q_CW|Q_CCW)));
assert(game.active.rotation==(pressed+1)%4);
}
puts("PASS: shared fire/keyboard rows, one lasting fire-release rotation, unaffected keyboard rows and gameplay/menu aliases");
return 0;
}