Skip to content

Commit

Permalink
[ADAPTER] Add NES/SNES support
Browse files Browse the repository at this point in the history
  • Loading branch information
darthcloud committed Jul 25, 2020
1 parent d402c18 commit 42de0a0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
1 change: 1 addition & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ idf_component_register(SRCS "main.c"
"adapter/config.c"
"adapter/hid_parser.c"
"adapter/hid_generic.c"
"adapter/npiso.c"
"adapter/segaio.c"
"adapter/n64.c"
"adapter/dc.c"
Expand Down
13 changes: 7 additions & 6 deletions main/adapter/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../util.h"
#include "config.h"
#include "adapter.h"
#include "npiso.h"
#include "segaio.h"
#include "n64.h"
#include "dc.h"
Expand Down Expand Up @@ -52,11 +53,11 @@ static to_generic_t to_generic_func[BT_MAX] = {

static from_generic_t from_generic_func[WIRED_MAX] = {
NULL,
NULL,
npiso_from_generic,
NULL,
NULL,
segaio_from_generic,
NULL,
npiso_from_generic,
NULL,
segaio_from_generic,
n64_from_generic,
Expand Down Expand Up @@ -97,11 +98,11 @@ static fb_from_generic_t fb_from_generic_func[BT_MAX] = {

static meta_init_t meta_init_func[WIRED_MAX] = {
NULL,
NULL,
npiso_meta_init,
NULL,
NULL,
segaio_meta_init,
NULL,
npiso_meta_init,
NULL,
segaio_meta_init,
n64_meta_init,
Expand All @@ -113,11 +114,11 @@ static meta_init_t meta_init_func[WIRED_MAX] = {

static buffer_init_t buffer_init_func[WIRED_MAX] = {
NULL,
NULL,
npiso_init_buffer,
NULL,
NULL,
segaio_init_buffer,
NULL,
npiso_init_buffer,
NULL,
segaio_init_buffer,
n64_init_buffer,
Expand Down
76 changes: 76 additions & 0 deletions main/adapter/npiso.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <string.h>
#include "../zephyr/types.h"
#include "../util.h"
#include "npiso.h"

enum {
NPISO_LD_RIGHT = 0,
NPISO_LD_LEFT,
NPISO_LD_DOWN,
NPISO_LD_UP,
NPISO_START,
NPISO_SELECT,
NPISO_Y,
NPISO_B,
NPISO_R = 12,
NPISO_L,
NPISO_X,
NPISO_A,
};

const struct ctrl_meta npiso_btns_meta =
{
.polarity = 1,
};

struct npiso_map {
uint16_t buttons;
} __packed;

const uint32_t npiso_mask[4] = {0x113F0F00, 0x00000000, 0x00000000, 0x00000000};
const uint32_t npiso_desc[4] = {0x00000000, 0x00000000, 0x00000000, 0x00000000};

const uint32_t npiso_btns_mask[32] = {
0, 0, 0, 0,
0, 0, 0, 0,
BIT(NPISO_LD_LEFT), BIT(NPISO_LD_RIGHT), BIT(NPISO_LD_DOWN), BIT(NPISO_LD_UP),
0, 0, 0, 0,
BIT(NPISO_Y), BIT(NPISO_A), BIT(NPISO_B), BIT(NPISO_X),
BIT(NPISO_START), BIT(NPISO_SELECT), 0, 0,
BIT(NPISO_L), 0, 0, 0,
BIT(NPISO_R), 0, 0, 0,
};

void npiso_init_buffer(int32_t dev_mode, struct wired_data *wired_data) {
struct npiso_map *map = (struct npiso_map *)wired_data->output;

map->buttons = 0xFFFF;
}

void npiso_meta_init(int32_t dev_mode, struct generic_ctrl *ctrl_data) {
memset((void *)ctrl_data, 0, sizeof(*ctrl_data)*WIRED_MAX_DEV);

for (uint32_t i = 0; i < WIRED_MAX_DEV; i++) {
ctrl_data[i].mask = npiso_mask;
ctrl_data[i].desc = npiso_desc;
}
}

void npiso_from_generic(int32_t dev_mode, struct generic_ctrl *ctrl_data, struct wired_data *wired_data) {
struct npiso_map map_tmp;

memcpy((void *)&map_tmp, wired_data->output, sizeof(map_tmp));

for (uint32_t i = 0; i < ARRAY_SIZE(generic_btns_mask); i++) {
if (ctrl_data->map_mask[0] & BIT(i)) {
if (ctrl_data->btns[0].value & generic_btns_mask[i]) {
map_tmp.buttons &= ~npiso_btns_mask[i];
}
else {
map_tmp.buttons |= npiso_btns_mask[i];
}
}
}

memcpy(wired_data->output, (void *)&map_tmp, sizeof(map_tmp));
}
9 changes: 9 additions & 0 deletions main/adapter/npiso.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _NPISO_H_
#define _NPISO_H_
#include "adapter.h"

void npiso_meta_init(int32_t dev_mode, struct generic_ctrl *ctrl_data);
void npiso_init_buffer(int32_t dev_mode, struct wired_data *wired_data);
void npiso_from_generic(int32_t dev_mode, struct generic_ctrl *ctrl_data, struct wired_data *wired_data);

#endif /* _NPISO_H_ */

0 comments on commit 42de0a0

Please sign in to comment.