git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit 6c4009412867d546a4a4447fb1b75f8e7990884a
parent 3ae2a52757877352bf4608005313674a9003e270
Author: Toxa <56631470+untoxa@users.noreply.github.com>
Date:   Fri, 29 Sep 2023 09:56:12 +0300

Merge pull request #582 from michel-iwaniec/nes_snes_joypads_example

NES: Add example program for reading SNES joypads on NES
Diffstat:
Agbdk-lib/examples/nes/snes_joypads/Makefile30++++++++++++++++++++++++++++++
Agbdk-lib/examples/nes/snes_joypads/snes_joypads.c83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 113 insertions(+), 0 deletions(-)

diff --git a/gbdk-lib/examples/nes/snes_joypads/Makefile b/gbdk-lib/examples/nes/snes_joypads/Makefile @@ -0,0 +1,30 @@ +CC = ../../../bin/lcc -mmos6502:nes -Wm-yoA -Wl-j + +BINS = snes_joypads.nes + +all: $(BINS) + +ASRC = $(wildcard *.s) +CSRC = $(wildcard *.c) + +OBJS = $(CSRC:%.c=%.o) $(ASRC:%.s=%.o) + +compile.bat: Makefile + @echo "REM Automatically generated from Makefile" > compile.bat + @make -sn | sed y/\\//\\\\/ | sed s/mkdir\ -p\/mkdir\/ | grep -v make >> compile.bat + +# Compile and link single file in one pass + +%.o: %.c + $(CC) -c -o $@ $< + +%.o: %.s + $(CC) -c -o $@ $< + +$(BINS): $(OBJS) + $(CC) -Wm-yS -o $@ $^ + rm -f *.map *.noi *.ihx *.lst + +clean: + rm -f *.o *.lst *.map *.nes *~ *.rel *.cdb *.ihx *.lnk *.sym *.asm *.noi + diff --git a/gbdk-lib/examples/nes/snes_joypads/snes_joypads.c b/gbdk-lib/examples/nes/snes_joypads/snes_joypads.c @@ -0,0 +1,83 @@ +/* + snes_joypads.c + + An example of reading SNES joypad input on NES. + + Displays the status of keys pressed on the screen, + in the following format: (or whitespace when not pressed) + + JOYn: lrud SELECT START YBXALR + + SNES joypads use the same protocol as NES joypads, with + the number of valid bits on the serial interface being + greater for a SNES joypad. Even though the physical + connectors differ, simple adapters can be built from + extension coords. + + Because the "Four Score" 4-player adapter for the NES reports + 2 joypads in two separate bytes read serially, it can + easily be repurposed for 2 SNES joypads, by reinterpreting + the two bytes for two different NES joypads as the input + for one SNES joypad. + + The extra buttons on a SNES joypad can be quite useful for + adding debug functionality to your game while in development. + They can also be used in your final game - but consider making + this an optional feature, as not everyone will have a + SNES-to-NES joypad adapter, or be keen on the idea of using + SNES joypads on a NES. +*/ + +#include <stdio.h> +#include <gbdk/platform.h> +#include <gbdk/font.h> +#include <gbdk/console.h> + +#define JS_X J_B +#define JS_Y J_B +#define JS_B J_A +#define JS_A J_A +#define JS_L J_SELECT +#define JS_R J_START + +joypads_t joypads; + +void main(void) +{ + font_t ibm_font; + int i; + // Init font system and load font + font_init(); + ibm_font = font_load(font_ibm); + // 4 NES joypads = 2 SNES joypads + joypad_init(4, &joypads); + DISPLAY_ON; + while(TRUE) + { + // poll 4 NES joypads + joypad_ex(&joypads); + // Loop over 2 SNES joypads + for(i = 0; i < 2; i++) + { + int y = 4 + 2*i; + uint8_t joy = joypads.joypads[i]; // Common NES/SNES bits + uint8_t joy_s = joypads.joypads[i+2]; // SNES additional bits + gotoxy(1, y); + printf("JOY%d: ", i); + putchar((joy & J_LEFT) ? 'l' : ' '); + putchar((joy & J_RIGHT) ? 'r' : ' '); + putchar((joy & J_UP) ? 'u' : ' '); + putchar((joy & J_DOWN) ? 'd' : ' '); + printf( (joy & J_SELECT) ? "SELECT " : " "); + printf( (joy & J_START) ? "START " : " "); + putchar((joy & JS_Y) ? 'Y' : ' '); + putchar((joy & JS_B) ? 'B' : ' '); + putchar((joy_s & JS_X) ? 'X' : ' '); + putchar((joy_s & JS_A) ? 'A' : ' '); + putchar((joy_s & JS_L) ? 'L' : ' '); + putchar((joy_s & JS_R) ? 'R' : ' '); + + } + vsync(); + } +}

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.