git.y1.nz

gbdk-2020

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

gbdk-lib/examples/nes/snes_joypads/snes_joypads.c

      1 /*
      2     snes_joypads.c
      3 
      4     An example of reading SNES joypad input on NES.
      5     
      6     Displays the status of keys pressed on the screen,
      7     in the following format: (or whitespace when not pressed)
      8     
      9     JOYn: lrud SELECT START YBXALR
     10 
     11     SNES joypads use the same protocol as NES joypads, with
     12     the number of valid bits on the serial interface being 
     13     greater for a SNES joypad. Even though the physical
     14     connectors differ, simple adapters can be built from
     15     extension coords.
     16     
     17     Because the "Four Score" 4-player adapter for the NES reports
     18     2 joypads in two separate bytes read serially, it can 
     19     easily be repurposed for 2 SNES joypads, by reinterpreting
     20     the two bytes for two different NES joypads as the input
     21     for one SNES joypad.
     22     
     23     The extra buttons on a SNES joypad can be quite useful for 
     24     adding debug functionality to your game while in development.
     25     They can also be used in your final game - but consider making
     26     this an optional feature, as not everyone will have a 
     27     SNES-to-NES joypad adapter, or be keen on the idea of using
     28     SNES joypads on a NES.
     29 */
     30 
     31 #include <stdio.h>
     32 #include <gbdk/platform.h>
     33 #include <gbdk/font.h>
     34 #include <gbdk/console.h>
     35 
     36 #define JS_X J_B
     37 #define JS_Y J_B
     38 #define JS_B J_A 
     39 #define JS_A J_A
     40 #define JS_L J_SELECT 
     41 #define JS_R J_START
     42 
     43 joypads_t joypads;
     44 
     45 void main(void)
     46 {
     47     font_t ibm_font;
     48     int i;
     49     // Init font system and load font
     50     font_init();
     51     ibm_font = font_load(font_ibm);
     52     // 4 NES joypads = 2 SNES joypads
     53     joypad_init(4, &joypads);
     54     DISPLAY_ON;
     55     while(TRUE)
     56     {
     57         // poll 4 NES joypads
     58         joypad_ex(&joypads);
     59         // Loop over 2 SNES joypads
     60         for(i = 0; i < 2; i++)
     61         {
     62             int y = 4 + 2*i;
     63             uint8_t joy = joypads.joypads[i];     // Common NES/SNES bits
     64             uint8_t joy_s = joypads.joypads[i+2]; // SNES additional bits
     65             gotoxy(1, y);
     66             printf("JOY%d: ", i);
     67             putchar((joy & J_LEFT)   ? 'l' : ' ');
     68             putchar((joy & J_RIGHT)  ? 'r' : ' ');
     69             putchar((joy & J_UP)     ? 'u' : ' ');
     70             putchar((joy & J_DOWN)   ? 'd' : ' ');
     71             printf( (joy & J_SELECT) ? "SELECT " : "       ");
     72             printf( (joy & J_START)  ? "START "  : "      ");
     73             putchar((joy & JS_Y)     ? 'Y' : ' ');
     74             putchar((joy & JS_B)     ? 'B' : ' ');
     75             putchar((joy_s & JS_X)   ? 'X' : ' ');
     76             putchar((joy_s & JS_A)   ? 'A' : ' ');
     77             putchar((joy_s & JS_L)   ? 'L' : ' ');
     78             putchar((joy_s & JS_R)   ? 'R' : ' ');
     79 
     80         }
     81         vsync();
     82     }
     83 }

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