You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.4 KiB

  1. /* DaemonBite (S)NES Controllers to USB Adapter
  2. * Author: Mikael Norrgård <mick@daemonbite.com>
  3. *
  4. * Copyright (c) 2020 Mikael Norrgård <http://daemonbite.com>
  5. *
  6. * GNU GENERAL PUBLIC LICENSE
  7. * Version 3, 29 June 2007
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include "Gamepad.h"
  24. // ATT: 20 chars max (including NULL at the end) according to Arduino source code.
  25. // Additionally serial number is used to differentiate arduino projects to have different button maps!
  26. const char *gp_serial = "NES/SNES to USB";
  27. #define DEBUG
  28. #define GAMEPAD_COUNT 2 // NOTE: To have more than 2 two gamepads you need to disable the CDC of the Arduino, there is a specific project for that.
  29. #define GAMEPAD_COUNT_MAX 2
  30. #define BUTTON_READ_DELAY 20 // Delay between button reads in µs
  31. #define CYCLES_LATCH 128 // 12µs according to specs (8 seems to work fine) (1 cycle @ 16MHz takes 62.5ns so 62.5ns * 128 = 8000ns = 8µs)
  32. #define CYCLES_CLOCK 64 // 6µs according to specs (4 seems to work fine)
  33. #define CYCLES_PAUSE 64 // 6µs according to specs (4 seems to work fine)
  34. #define UP 0x01
  35. #define DOWN 0x02
  36. #define LEFT 0x04
  37. #define RIGHT 0x08
  38. #define DELAY_CYCLES(n) __builtin_avr_delay_cycles(n)
  39. // Wire it all up according to the following table:
  40. //
  41. // NES SNES Arduino Pro Micro
  42. // --------------------------------------
  43. // VCC VCC (All gamepads)
  44. // GND GND (All gamepads)
  45. // OUT0 (LATCH) 2 (PD1, All gamepads)
  46. // CUP (CLOCK) 3 (PD0, All gamepads)
  47. // D1 (GP1: DATA) A0 (PF7, Gamepad 1)
  48. // D1 (GP2: DATA) A1 (PF6, Gamepad 2)
  49. // D1 (GP3: DATA) A2 (PF5, Gamepad 3, not currently used)
  50. // D1 (GP4: DATA) A3 (PF4, Gamepad 4, not currently used)
  51. enum ControllerType {
  52. NONE,
  53. NES,
  54. SNES
  55. };
  56. // Set up USB HID gamepads
  57. Gamepad_ Gamepad[GAMEPAD_COUNT];
  58. // Controllers
  59. uint8_t buttons[GAMEPAD_COUNT_MAX][2] = {{0,0},{0,0}};
  60. uint8_t buttonsPrev[GAMEPAD_COUNT_MAX][2] = {{0,0},{0,0}};
  61. uint8_t gpBit[GAMEPAD_COUNT_MAX] = {B10000000,B01000000};
  62. ControllerType controllerType[GAMEPAD_COUNT_MAX] = {NONE,NONE};
  63. uint8_t btnByte[12] = {0,0,0,0,1,1,1,1,0,0,0,0};
  64. uint8_t btnBits[12] = {0x01,0x04,0x40,0x80,UP,DOWN,LEFT,RIGHT,0x02,0x08,0x10,0x20};
  65. uint8_t gp = 0;
  66. uint8_t buttonCount = 12;
  67. // Timing
  68. uint32_t microsButtons = 0;
  69. #ifdef DEBUG
  70. uint32_t microsStart = 0;
  71. uint32_t microsEnd = 0;
  72. uint8_t counter = 0;
  73. #endif
  74. void setup()
  75. {
  76. // Setup latch and clock pins (2,3 or PD1, PD0)
  77. DDRD |= B00000011; // output
  78. PORTD &= ~B00000011; // low
  79. // Setup data pins A0-A3 (PF7-PF4)
  80. DDRF &= ~B11110000; // inputs
  81. PORTF |= B11110000; // enable internal pull-ups
  82. DDRC &= ~B01000000; // input
  83. PORTC |= B01000000; // enable internal pull-up
  84. #ifdef DEBUG
  85. Serial.begin(115200);
  86. delay(2000);
  87. #endif
  88. delay(3000);
  89. detectControllerTypes();
  90. }
  91. void loop() { while(1)
  92. {
  93. // See if enough time has passed since last button read
  94. if((micros() - microsButtons) > BUTTON_READ_DELAY)
  95. {
  96. #ifdef DEBUG
  97. microsStart = micros();
  98. #endif
  99. // Pulse latch
  100. sendLatch();
  101. for(uint8_t btn=0; btn<buttonCount; btn++)
  102. {
  103. for(gp=0; gp<GAMEPAD_COUNT; gp++)
  104. (PINF & gpBit[gp]) ? buttons[gp][btnByte[btn]] &= ~btnBits[btn] : buttons[gp][btnByte[btn]] |= btnBits[btn];
  105. sendClock();
  106. }
  107. // Check gamepad type
  108. for(gp=0; gp<GAMEPAD_COUNT; gp++)
  109. {
  110. if(controllerType[gp] == NES) { // NES
  111. bitWrite(buttons[gp][0], 1, bitRead(buttons[gp][0], 0));
  112. bitWrite(buttons[gp][0], 0, bitRead(buttons[gp][0], 2));
  113. buttons[gp][0] &= 0xC3;
  114. }
  115. }
  116. for(gp=0; gp<GAMEPAD_COUNT; gp++)
  117. {
  118. // Has any buttons changed state?
  119. if (buttons[gp] != buttonsPrev[gp])
  120. {
  121. Gamepad[gp]._GamepadReport.buttons = buttons[gp][0];
  122. Gamepad[gp]._GamepadReport.Y = ((buttons[gp][1] & DOWN) >> 1) - (buttons[gp][1] & UP);
  123. Gamepad[gp]._GamepadReport.X = ((buttons[gp][1] & RIGHT) >> 3) - ((buttons[gp][1] & LEFT) >> 2);
  124. buttonsPrev[gp][0] = buttons[gp][0];
  125. buttonsPrev[gp][1] = buttons[gp][1];
  126. Gamepad[gp].send();
  127. }
  128. }
  129. microsButtons = micros();
  130. #ifdef DEBUG
  131. microsEnd = micros();
  132. if(counter < 20) {
  133. Serial.println(microsEnd-microsStart);
  134. counter++;
  135. }
  136. #endif
  137. }
  138. }}
  139. void detectControllerTypes()
  140. {
  141. uint8_t buttonCountNew = 0;
  142. // Read the controllers a few times to detect controller type
  143. for(uint8_t i=0; i<4; i++)
  144. {
  145. // Pulse latch
  146. sendLatch();
  147. // Read all buttons
  148. for(uint8_t btn=0; btn<buttonCount; btn++)
  149. {
  150. for(gp=0; gp<GAMEPAD_COUNT; gp++)
  151. (PINF & gpBit[gp]) ? buttons[gp][btnByte[btn]] &= ~btnBits[btn] : buttons[gp][btnByte[btn]] |= btnBits[btn];
  152. sendClock();
  153. }
  154. // Check controller types and set buttonCount to max needed
  155. for(gp=0; gp<GAMEPAD_COUNT; gp++)
  156. {
  157. if((buttons[gp][0] & 0xF3A) == 0xF3A) { // NES
  158. if(controllerType[gp] != SNES)
  159. controllerType[gp] = NES;
  160. if(buttonCountNew < 8)
  161. buttonCountNew = 8;
  162. }
  163. else { // SNES Gamepad
  164. controllerType[gp] = SNES;
  165. if(buttonCountNew < 12)
  166. buttonCountNew = 12;
  167. }
  168. }
  169. }
  170. // Set updated button count to avoid unneccesary button reads (for simpler controller types)
  171. buttonCount = buttonCountNew;
  172. }
  173. void sendLatch()
  174. {
  175. // Send a latch pulse to (S)NES controller(s)
  176. PORTD |= B00000010; // Set HIGH
  177. DELAY_CYCLES(CYCLES_LATCH);
  178. PORTD &= ~B00000010; // Set LOW
  179. DELAY_CYCLES(CYCLES_PAUSE);
  180. }
  181. void sendClock()
  182. {
  183. // Send a clock pulse to (S)NES controller(s)
  184. PORTD |= B10000001; // Set HIGH
  185. DELAY_CYCLES(CYCLES_CLOCK);
  186. PORTD &= ~B10000001; // Set LOW
  187. DELAY_CYCLES(CYCLES_PAUSE);
  188. }