// SPDX-License-Identifier: GPL-3.0-or-later #include #include #define MUX_LOW (*(unsigned char *)0xFF01) #define KEY_COLUMNS (*(unsigned char *)0xFF02) #define MUX_HIGH (*(unsigned char *)0xFF03) #define DAC (*(unsigned char *)0xFF20) #define SOUND_GATE (*(unsigned char *)0xFF23) // BControl selects the axis. Try each bit of the six-bit DAC once, preserving // the two unrelated PIA pins on every write (including serial output). static byte readAxis(byte axis, byte lowPins) { byte trialBit, value; Joystick_selectAnalogMuxInputs(axis); asm { clr :value lda #32 sta :trialBit @trial lda :value ora :trialBit tfr a,b lslb lslb orb :lowPins stb >$ff20 tst >$ff00 bpl @next // Comparator low: this trial is too high. sta :value @next lsr :trialBit bne @trial } return value; } static signed char direction(byte axis) { if (axis < 26) return -1; if (axis > 37) return 1; return 0; } void input_sample(VccInput *input) { byte flags, low, high, gate, dac, columns; byte axis, x, y, rows, down, previous; if (!input || input->port > INPUT_LEFT) return; axis = input->port << 1; // Keep an interrupt from selecting a different axis halfway through a // conversion. BASIC can resume scanning immediately after restoration. asm { tfr cc,a orcc #$50 sta :flags } low = MUX_LOW; high = MUX_HIGH; gate = SOUND_GATE; columns = KEY_COLUMNS; dac = DAC; Joystick_disableSound(); x = readAxis(axis, dac & 3); y = readAxis(axis + 1, dac & 3); // BControl deselects every keyboard column before reading button rows. // Right primary/secondary are rows 0/2, left are rows 1/3, active low. rows = Joystick_getButtonStates2(); KEY_COLUMNS = columns; DAC = dac; MUX_LOW = low; MUX_HIGH = high; SOUND_GATE = gate; asm { lda :flags tfr a,cc } down = (byte)~rows >> input->port; down = (down & 1) | ((down >> 1) & 2); previous = input->held; input->x = x; input->y = y; input->dx = direction(x); input->dy = direction(y); input->held = down; input->pressed = down & ~previous; input->released = previous & ~down; } void input_init(VccInput *input, byte port) { if (!input) return; input->port = port; input->x = input->y = 31; input->dx = input->dy = 0; input->held = 0; input_sample(input); input->pressed = input->released = 0; }