Skip to content

Commit

Permalink
(s)nes pad refactoring by dee
Browse files Browse the repository at this point in the history
  • Loading branch information
xrip committed Feb 23, 2024
1 parent 17f069e commit b6b0afd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 61 deletions.
43 changes: 5 additions & 38 deletions drivers/nespad/nespad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ static inline pio_sm_config nespad_program_get_default_config(uint offset) {

static PIO pio = pio1;
static uint8_t sm = -1;
uint8_t nespad_state = 0; // Joystick 1
uint8_t nespad_state2 = 0; // Joystick 2
uint8_t snespad_state = 0; // SNES Joystick
uint32_t nespad_state = 0; // Joystick 1
uint32_t nespad_state2 = 0; // Joystick 2

bool nespad_begin(uint32_t cpu_khz, uint8_t clkPin, uint8_t dataPin,uint8_t latPin) {
if (pio_can_add_program(pio, &nespad_program) &&
Expand Down Expand Up @@ -88,42 +87,10 @@ void nespad_read()

// Right-shift was used in sm config so bit order matches NES controller
// bits used elsewhere in picones, but does require shifting down...
uint32_t temp16=((pio->rxf[sm]))^ 0xFFFFFFFF;
snespad_state = (temp16 >> 16) & 0xFF;

uint32_t temp=pio->rxf[sm]^ 0xFFFFFFFF;
pio->txf[sm]=0;
uint16_t temp1, temp2;
// temp1 = temp16 & 0x5555; // 08070605.04030201
// temp2 = temp16 & 0xAAAA; // 80706050.40302010
// nespad_state=temp1|(temp1>>7); //84736251
//nespad_state2=(temp2|(temp2<<7))>>8;//84736251
// return;


// 1 -------------------------------------------------------
temp1 = temp16 & 0x5555; // 08070605.04030201
temp2 = temp16 & 0xAAAA; // 80706050.40302010
temp16 = temp16 & 0xAA55; // 80706050.04030201
temp1 = temp1 >> 7; // 00000000.80706050
temp2 = temp2 << 7; // 04030201.00000000
temp16 = temp16 | temp1 | temp2; // 84736251.84736251
// 2 -------------------------------------------------------
temp1 = temp16 & 0x5050; // 04030000.04030000
temp2 = temp16 & 0x0A0A; // 00006050.00006050
temp16 = temp16 & 0xA5A5; // 80700201.80700201
temp1 = temp1 >> 3; // 00004030.00004030
temp2 = temp2 << 3; // 06050000.06050000
temp16 = temp16 | temp1 | temp2; // 86754231.86754231
// 3 -------------------------------------------------------
temp1 = temp16 & 0x4444; // 06000200.06000200
temp2 = temp16 & 0x2222; // 00700030.00700030
temp16 = temp16 & 0x9999; // 80054001.80054001
temp1 = temp1 >> 1; // 00600020.00600020
temp2 = temp2 << 1; // 07000300.07000300
temp16 = temp16 | temp1 | temp2; // 87654321.87654321
//----------------------------------------------------------
nespad_state = temp16; // 00000000.87654321 Joy1
nespad_state2 = temp16 >> 8; // 00000000.87654321 Joy2
nespad_state = temp & 0x555555; // Joy1
nespad_state2 = temp >> 1 & 0x555555; // Joy2
}


34 changes: 16 additions & 18 deletions drivers/nespad/nespad.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#pragma once

#define DPAD_LEFT 0x40
#define DPAD_RIGHT 0x80
#define DPAD_DOWN 0x20
#define DPAD_UP 0x10
#define DPAD_START 0x08
#define DPAD_SELECT 0x04
#define DPAD_B 0x02
#define DPAD_A 0x01


#define DPAD_X (0x04 | 0x04 << 1)
#define DPAD_LT (0x10 | 0x10 << 1)
#define DPAD_RT (0x40 | 0x40 << 1)


extern uint8_t nespad_state; // NES Joystick1
extern uint8_t nespad_state2; // NES Joystick1
extern uint8_t snespad_state; // SNES Joystick
#define DPAD_LEFT 0x001000
#define DPAD_RIGHT 0x004000
#define DPAD_DOWN 0x000400
#define DPAD_UP 0x000100
#define DPAD_START 0x000040
#define DPAD_SELECT 0x000010
#define DPAD_B 0x000004 //Y on SNES
#define DPAD_A 0x000001 //B on SNES

#define DPAD_Y 0x010000 //A on SNES
#define DPAD_X 0x040000
#define DPAD_LT 0x100000
#define DPAD_RT 0x400000

extern uint32_t nespad_state; // (S)NES Joystick1
extern uint32_t nespad_state2; // (S)NES Joystick2

extern bool nespad_begin(uint32_t cpu_khz, uint8_t clkPin, uint8_t dataPin,
uint8_t latPin);
Expand Down
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ int main() {
//gb.direct.joypad = nespad_state;
//------------------------------------------------------------------------------
/* hotkeys (select + * combo)*/
if (!(gb.direct.joypad & 0b00001100) || snespad_state & DPAD_X) {
if (!(gb.direct.joypad & 0b00001100) || nespad_state & DPAD_X) {

static int keydown_counter = 0;
char romname[24];
Expand All @@ -920,18 +920,18 @@ int main() {
}
}
// TODO F2
if ((snespad_state & DPAD_RT)) {
if ((nespad_state & DPAD_RT)) {
// wait for release to prevent cycle
while (snespad_state & DPAD_RT) {
while (nespad_state & DPAD_RT) {
sleep_ms(500);
}
load();
}

// TODO F3
if ((snespad_state & DPAD_LT)) {
if ((nespad_state & DPAD_LT)) {
// wait for release to prevent cycle
while (snespad_state & DPAD_LT) {
while (nespad_state & DPAD_LT) {
sleep_ms(500);
}
save();
Expand Down

0 comments on commit b6b0afd

Please sign in to comment.