Skip to content

Commit b6b0afd

Browse files
committed
(s)nes pad refactoring by dee
1 parent 17f069e commit b6b0afd

File tree

3 files changed

+26
-61
lines changed

3 files changed

+26
-61
lines changed

drivers/nespad/nespad.cpp

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ static inline pio_sm_config nespad_program_get_default_config(uint offset) {
3030

3131
static PIO pio = pio1;
3232
static uint8_t sm = -1;
33-
uint8_t nespad_state = 0; // Joystick 1
34-
uint8_t nespad_state2 = 0; // Joystick 2
35-
uint8_t snespad_state = 0; // SNES Joystick
33+
uint32_t nespad_state = 0; // Joystick 1
34+
uint32_t nespad_state2 = 0; // Joystick 2
3635

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

8988
// Right-shift was used in sm config so bit order matches NES controller
9089
// bits used elsewhere in picones, but does require shifting down...
91-
uint32_t temp16=((pio->rxf[sm]))^ 0xFFFFFFFF;
92-
snespad_state = (temp16 >> 16) & 0xFF;
93-
90+
uint32_t temp=pio->rxf[sm]^ 0xFFFFFFFF;
9491
pio->txf[sm]=0;
95-
uint16_t temp1, temp2;
96-
// temp1 = temp16 & 0x5555; // 08070605.04030201
97-
// temp2 = temp16 & 0xAAAA; // 80706050.40302010
98-
// nespad_state=temp1|(temp1>>7); //84736251
99-
//nespad_state2=(temp2|(temp2<<7))>>8;//84736251
100-
// return;
101-
102-
103-
// 1 -------------------------------------------------------
104-
temp1 = temp16 & 0x5555; // 08070605.04030201
105-
temp2 = temp16 & 0xAAAA; // 80706050.40302010
106-
temp16 = temp16 & 0xAA55; // 80706050.04030201
107-
temp1 = temp1 >> 7; // 00000000.80706050
108-
temp2 = temp2 << 7; // 04030201.00000000
109-
temp16 = temp16 | temp1 | temp2; // 84736251.84736251
110-
// 2 -------------------------------------------------------
111-
temp1 = temp16 & 0x5050; // 04030000.04030000
112-
temp2 = temp16 & 0x0A0A; // 00006050.00006050
113-
temp16 = temp16 & 0xA5A5; // 80700201.80700201
114-
temp1 = temp1 >> 3; // 00004030.00004030
115-
temp2 = temp2 << 3; // 06050000.06050000
116-
temp16 = temp16 | temp1 | temp2; // 86754231.86754231
117-
// 3 -------------------------------------------------------
118-
temp1 = temp16 & 0x4444; // 06000200.06000200
119-
temp2 = temp16 & 0x2222; // 00700030.00700030
120-
temp16 = temp16 & 0x9999; // 80054001.80054001
121-
temp1 = temp1 >> 1; // 00600020.00600020
122-
temp2 = temp2 << 1; // 07000300.07000300
123-
temp16 = temp16 | temp1 | temp2; // 87654321.87654321
124-
//----------------------------------------------------------
125-
nespad_state = temp16; // 00000000.87654321 Joy1
126-
nespad_state2 = temp16 >> 8; // 00000000.87654321 Joy2
92+
nespad_state = temp & 0x555555; // Joy1
93+
nespad_state2 = temp >> 1 & 0x555555; // Joy2
12794
}
12895

12996

drivers/nespad/nespad.h

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
#pragma once
22

3-
#define DPAD_LEFT 0x40
4-
#define DPAD_RIGHT 0x80
5-
#define DPAD_DOWN 0x20
6-
#define DPAD_UP 0x10
7-
#define DPAD_START 0x08
8-
#define DPAD_SELECT 0x04
9-
#define DPAD_B 0x02
10-
#define DPAD_A 0x01
11-
12-
13-
#define DPAD_X (0x04 | 0x04 << 1)
14-
#define DPAD_LT (0x10 | 0x10 << 1)
15-
#define DPAD_RT (0x40 | 0x40 << 1)
16-
17-
18-
extern uint8_t nespad_state; // NES Joystick1
19-
extern uint8_t nespad_state2; // NES Joystick1
20-
extern uint8_t snespad_state; // SNES Joystick
3+
#define DPAD_LEFT 0x001000
4+
#define DPAD_RIGHT 0x004000
5+
#define DPAD_DOWN 0x000400
6+
#define DPAD_UP 0x000100
7+
#define DPAD_START 0x000040
8+
#define DPAD_SELECT 0x000010
9+
#define DPAD_B 0x000004 //Y on SNES
10+
#define DPAD_A 0x000001 //B on SNES
11+
12+
#define DPAD_Y 0x010000 //A on SNES
13+
#define DPAD_X 0x040000
14+
#define DPAD_LT 0x100000
15+
#define DPAD_RT 0x400000
16+
17+
extern uint32_t nespad_state; // (S)NES Joystick1
18+
extern uint32_t nespad_state2; // (S)NES Joystick2
2119

2220
extern bool nespad_begin(uint32_t cpu_khz, uint8_t clkPin, uint8_t dataPin,
2321
uint8_t latPin);

src/main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ int main() {
900900
//gb.direct.joypad = nespad_state;
901901
//------------------------------------------------------------------------------
902902
/* hotkeys (select + * combo)*/
903-
if (!(gb.direct.joypad & 0b00001100) || snespad_state & DPAD_X) {
903+
if (!(gb.direct.joypad & 0b00001100) || nespad_state & DPAD_X) {
904904

905905
static int keydown_counter = 0;
906906
char romname[24];
@@ -920,18 +920,18 @@ int main() {
920920
}
921921
}
922922
// TODO F2
923-
if ((snespad_state & DPAD_RT)) {
923+
if ((nespad_state & DPAD_RT)) {
924924
// wait for release to prevent cycle
925-
while (snespad_state & DPAD_RT) {
925+
while (nespad_state & DPAD_RT) {
926926
sleep_ms(500);
927927
}
928928
load();
929929
}
930930

931931
// TODO F3
932-
if ((snespad_state & DPAD_LT)) {
932+
if ((nespad_state & DPAD_LT)) {
933933
// wait for release to prevent cycle
934-
while (snespad_state & DPAD_LT) {
934+
while (nespad_state & DPAD_LT) {
935935
sleep_ms(500);
936936
}
937937
save();

0 commit comments

Comments
 (0)