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.

237 lines
5.7 KiB

  1. /* gc_n64_usb : Gamecube or N64 controller to USB adapter firmware
  2. Copyright (C) 2007-2016 Raphael Assenat <raph@raphnet.net>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/io.h>
  15. #include <avr/interrupt.h>
  16. #include <util/delay.h>
  17. #include <string.h>
  18. #include "gamepads.h"
  19. #include "gamecube.h"
  20. #include "gcn64_protocol.h"
  21. /*********** prototypes *************/
  22. static void gamecubeInit(unsigned char chn);
  23. static void gamecubeInitKB(unsigned char chn);
  24. static char gamecubeUpdate(unsigned char chn);
  25. static char gamecubeUpdateKB(unsigned char chn);
  26. static char gamecubeChanged(unsigned char chn);
  27. static char gc_rumbling[GAMEPAD_MAX_CHANNELS] = { };
  28. static char origins_set[GAMEPAD_MAX_CHANNELS] = { };
  29. static unsigned char orig_x[GAMEPAD_MAX_CHANNELS];
  30. static unsigned char orig_y[GAMEPAD_MAX_CHANNELS];
  31. static unsigned char orig_cx[GAMEPAD_MAX_CHANNELS];
  32. static unsigned char orig_cy[GAMEPAD_MAX_CHANNELS];
  33. static void gamecubeInit(unsigned char chn)
  34. {
  35. gamecubeUpdate(chn);
  36. }
  37. static void gamecubeInitKB(unsigned char chn)
  38. {
  39. gamecubeUpdateKB(chn);
  40. }
  41. void gc_decodeAnswer(unsigned char chn, unsigned char data[8])
  42. {
  43. unsigned char x,y,cx,cy;
  44. // Note: Checking seems a good idea, adds protection
  45. // against corruption (if the "constant" bits are invalid,
  46. // maybe others are : Drop the packet).
  47. //
  48. // However, I have seen bit 2 in a high state. To be as compatible
  49. // as possible, I decided NOT to look at these bits since instead
  50. // of being "constant" bits they might have a meaning I don't know.
  51. // Check the always 0 and always 1 bits
  52. #if 0
  53. if (gcn64_workbuf[0] || gcn64_workbuf[1] || gcn64_workbuf[2])
  54. return 1;
  55. if (!gcn64_workbuf[8])
  56. return 1;
  57. #endif
  58. /*
  59. (Source: Nintendo Gamecube Controller Protocol
  60. updated 8th March 2004, by James.)
  61. Bit Function
  62. 0-2 Always 0 << RAPH: Not true, I see 001!
  63. 3 Start
  64. 4 Y
  65. 5 X
  66. 6 B
  67. 7 A
  68. 8 Always 1
  69. 9 L
  70. 10 R
  71. 11 Z
  72. 12-15 Up,Down,Right,Left
  73. 16-23 Joy X
  74. 24-31 Joy Y
  75. 32-39 C Joystick X
  76. 40-47 C Joystick Y
  77. 48-55 Left Btn Val
  78. 56-63 Right Btn Val
  79. */
  80. last_built_report[chn].pad_type = PAD_TYPE_GAMECUBE;
  81. last_built_report[chn].gc.buttons = data[0] | data[1] << 8;
  82. x = data[2];
  83. y = data[3];
  84. cx = data[4];
  85. cy = data[5];
  86. last_built_report[chn].gc.lt = data[6];
  87. last_built_report[chn].gc.rt = data[7];
  88. #ifdef PAD_DATA_HAS_RAW
  89. memcpy(last_built_report[chn].gc.raw_data, data, 8);
  90. #endif
  91. if (origins_set[chn]) {
  92. last_built_report[chn].gc.x = ((int)x-(int)orig_x[chn]);
  93. last_built_report[chn].gc.y = ((int)y-(int)orig_y[chn]);
  94. last_built_report[chn].gc.cx = ((int)cx-(int)orig_cx[chn]);
  95. last_built_report[chn].gc.cy = ((int)cy-(int)orig_cy[chn]);
  96. } else {
  97. orig_x[chn] = x;
  98. orig_y[chn] = y;
  99. orig_cx[chn] = cx;
  100. orig_cy[chn] = cy;
  101. last_built_report[chn].gc.x = 0;
  102. last_built_report[chn].gc.y = 0;
  103. last_built_report[chn].gc.cx = 0;
  104. last_built_report[chn].gc.cy = 0;
  105. origins_set[chn] = 1;
  106. }
  107. }
  108. static char gamecubeUpdateKB(unsigned char chn)
  109. {
  110. unsigned char tmpdata[GC_GETSTATUS_REPLY_LENGTH];
  111. unsigned char count;
  112. unsigned char i, lrc;
  113. tmpdata[0] = GC_POLL_KB1;
  114. tmpdata[1] = GC_POLL_KB2;
  115. tmpdata[2] = GC_POLL_KB3;
  116. count = gcn64_transaction(chn, tmpdata, 3, tmpdata, GC_GETSTATUS_REPLY_LENGTH);
  117. if (count != GC_GETSTATUS_REPLY_LENGTH) {
  118. return 1;
  119. }
  120. // Compute LRC
  121. for (i=0, lrc=0; i<6; i++) {
  122. lrc ^= tmpdata[i];
  123. }
  124. if (tmpdata[7] != lrc) {
  125. return 1; // LRC error
  126. }
  127. // Ok, fill the report
  128. last_built_report[chn].pad_type = PAD_TYPE_GC_KB;
  129. for (i=0; i<3; i++) {
  130. last_built_report[chn].gckb.keys[i] = tmpdata[4+i];
  131. }
  132. return 0;
  133. }
  134. static char gamecubeUpdate(unsigned char chn)
  135. {
  136. unsigned char tmpdata[GC_GETSTATUS_REPLY_LENGTH];
  137. unsigned char count;
  138. tmpdata[0] = GC_GETSTATUS1;
  139. tmpdata[1] = GC_GETSTATUS2;
  140. tmpdata[2] = GC_GETSTATUS3(gc_rumbling[chn]);
  141. count = gcn64_transaction(chn, tmpdata, 3, tmpdata, GC_GETSTATUS_REPLY_LENGTH);
  142. if (count != GC_GETSTATUS_REPLY_LENGTH) {
  143. return 1;
  144. }
  145. gc_decodeAnswer(chn, tmpdata);
  146. return 0;
  147. }
  148. static void gamecubeHotplug(unsigned char chn)
  149. {
  150. // Make sure next read becomes the refence center values
  151. origins_set[chn] = 0;
  152. }
  153. static char gamecubeProbe(unsigned char chn)
  154. {
  155. origins_set[chn] = 0;
  156. if (gamecubeUpdate(chn)) {
  157. return 0;
  158. }
  159. return 1;
  160. }
  161. static char gamecubeChanged(unsigned char chn)
  162. {
  163. return memcmp(&last_built_report[chn], &last_sent_report[chn], sizeof(gamepad_data));
  164. }
  165. static void gamecubeGetReport(unsigned char chn, gamepad_data *dst)
  166. {
  167. if (dst)
  168. memcpy(dst, &last_built_report[chn], sizeof(gamepad_data));
  169. }
  170. static void gamecubeVibration(unsigned char chn, char enable)
  171. {
  172. gc_rumbling[chn] = enable;
  173. }
  174. Gamepad GamecubeGamepad = {
  175. .init = gamecubeInit,
  176. .update = gamecubeUpdate,
  177. .changed = gamecubeChanged,
  178. .getReport = gamecubeGetReport,
  179. .probe = gamecubeProbe,
  180. .setVibration = gamecubeVibration,
  181. .hotplug = gamecubeHotplug,
  182. };
  183. Gamepad *gamecubeGetGamepad(void)
  184. {
  185. return &GamecubeGamepad;
  186. }
  187. Gamepad GamecubeKeyboard = {
  188. .init = gamecubeInitKB,
  189. .update = gamecubeUpdateKB,
  190. .changed = gamecubeChanged,
  191. .getReport = gamecubeGetReport,
  192. .probe = gamecubeProbe,
  193. };
  194. Gamepad *gamecubeGetKeyboard(void)
  195. {
  196. return &GamecubeKeyboard;
  197. }