134 lines
6.8 KiB
C
134 lines
6.8 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;
|
|
}
|
|
/* Existing keyboard-only fixtures leave joystick mapping disabled. */
|
|
static Q16 read_keys(Q8 mode){return keyboard_read(&raw_buttons,mode,2);}
|
|
static QActions port_poll(QInput *in,Q8 port,Q16 ms,Q8 playing)
|
|
{
|
|
Q16 held=keyboard_read(&raw_buttons,playing?KEYBOARD_PLAY:0,port);
|
|
Q8 primary=port<2&&!(raw_buttons&(port==1?2:1));
|
|
return q_input_step(in,held,primary,31,31,31,31,port<2,ms,playing,0);
|
|
}
|
|
static Q8 occupied(const QGame *g)
|
|
{
|
|
Q8 x,y,n=0;for(y=0;y<20;++y)for(x=0;x<10;++x)if(g->board.cells[y][x])++n;
|
|
return n;
|
|
}
|
|
static QActions poll(QInput *in,Q8 button,Q16 ms)
|
|
{
|
|
return q_input_step(in,read_keys(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(read_keys(KEYBOARD_PLAY)==0);
|
|
for(pressed=0;pressed<4;++pressed){
|
|
raw_buttons=(Q8)(15&~(1<<pressed));
|
|
assert(read_keys(KEYBOARD_PLAY)==0);
|
|
contact_probe=KEY_PROBE_BREAK;contact_bit=KEY_BIT_BREAK;
|
|
assert(read_keys(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,read_keys(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,read_keys(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,read_keys(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,read_keys(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(read_keys(KEYBOARD_PLAY)==Q_CW);
|
|
contact_probe=KEY_PROBE_A;contact_bit=KEY_BIT_A;assert(read_keys(KEYBOARD_PLAY)==Q_LEFT);
|
|
contact_probe=KEY_PROBE_D;contact_bit=KEY_BIT_D;assert(read_keys(KEYBOARD_PLAY)==Q_RIGHT);
|
|
assert(read_keys(KEYBOARD_INITIALS)==0);
|
|
contact_probe=KEY_PROBE_UP;contact_bit=KEY_BIT_UP;
|
|
assert(read_keys(KEYBOARD_PLAY)==Q_CCW);
|
|
assert(read_keys(KEYBOARD_PLAY|KEYBOARD_CLOCKWISE)==Q_CW);
|
|
contact_probe=KEY_PROBE_SPACE;contact_bit=KEY_BIT_SPACE;
|
|
assert(read_keys(KEYBOARD_PLAY)==Q_HARD);
|
|
assert(read_keys(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(read_keys(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(read_keys(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);
|
|
}
|
|
/* Both physical secondary lines use the real adapter and edge handler.
|
|
* Held fire drops one piece, without alias rotations or repeated drops. */
|
|
for(pressed=0;pressed<2;++pressed){
|
|
Q8 held_sample,secondary=(Q8)(pressed?8:4);
|
|
raw_buttons=15;q_input_reset(&in);port_poll(&in,pressed,0,1);
|
|
q_start(&game,&options);
|
|
raw_buttons=(Q8)(15&~secondary);a=port_poll(&in,pressed,1,1);
|
|
assert(a.pressed==Q_HARD);assert(q_drop(&game)&&occupied(&game)==4);
|
|
for(held_sample=0;held_sample<8;++held_sample){
|
|
a=port_poll(&in,pressed,500,1);assert(!a.pressed);
|
|
if(a.pressed&Q_HARD)q_drop(&game);
|
|
}
|
|
assert(occupied(&game)==4);
|
|
raw_buttons=15;a=port_poll(&in,pressed,1,1);assert(!a.pressed);
|
|
raw_buttons=(Q8)(15&~secondary);a=port_poll(&in,pressed,1,1);
|
|
assert(a.pressed==Q_HARD);assert(q_drop(&game)&&occupied(&game)==8);
|
|
/* Wrong port and Off cannot turn this secondary into a hard drop. */
|
|
assert(!(keyboard_read(&raw_buttons,KEYBOARD_PLAY,(Q8)(1-pressed))&Q_HARD));
|
|
assert(!(keyboard_read(&raw_buttons,KEYBOARD_PLAY,2)&Q_HARD));
|
|
assert(!keyboard_read(&raw_buttons,0,pressed));
|
|
/* A held secondary across a menu/resume requires release first. */
|
|
q_input_release(&in);a=port_poll(&in,pressed,1,1);assert(!a.pressed);
|
|
a=port_poll(&in,pressed,1000,1);assert(!a.pressed);
|
|
raw_buttons=15;port_poll(&in,pressed,1,1);
|
|
raw_buttons=(Q8)(15&~secondary);a=port_poll(&in,pressed,1,1);
|
|
assert(a.pressed==Q_HARD);
|
|
}
|
|
puts("PASS: shared fire/keyboard rows, one lasting fire-release rotation, unaffected keyboard rows, both secondary hard-drop buttons, held/transition guards and port selection");
|
|
return 0;
|
|
}
|