├── .arduino-ci.yml ├── .github ├── FUNDING.yml └── workflows │ ├── arduino_ci.yml │ └── main.yml ├── LICENSE ├── README.md ├── examples ├── GCKonamiCode │ └── GCKonamiCode.ino ├── GCPadDump │ └── GCPadDump.ino ├── GCPadToUSB │ ├── .arduino-ci.yml │ └── GCPadToUSB.ino ├── MegaDriveMultiplexer │ └── MegaDriveMultiplexer.ino ├── N64PadDump │ └── N64PadDump.ino ├── N64PadToMegaDrive │ └── N64PadToMegaDrive.ino ├── N64PadToUSB │ ├── .arduino-ci.yml │ └── N64PadToUSB.ino └── N64PadToUsbDigispark │ ├── .arduino-ci.yml │ └── N64PadToUsbDigispark.ino ├── extras ├── GameCubeControllerPinout.jpg ├── N64ControllerPinout.jpg ├── README.md └── social.png ├── install_dependencies.sh ├── library.properties └── src ├── GCPad.cpp ├── GCPad.h ├── N64Pad.cpp ├── N64Pad.h └── protocol ├── N64PadProtocol.cpp ├── N64PadProtocol.h ├── int0.S ├── pcint.S ├── pinconfig.h └── usbpause.h /.arduino-ci.yml: -------------------------------------------------------------------------------- 1 | compile: 2 | platforms: 3 | - uno 4 | - leonardo 5 | - mega2560 6 | 7 | examples: 8 | exclude_dirs: 9 | - N64PadToUsbDigispark 10 | - N64PadToUSB 11 | - GCPadToUSB 12 | 13 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: openretroworks 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.github/workflows/arduino_ci.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Arduino_CI 3 | 4 | on: [push, pull_request] 5 | 6 | jobs: 7 | arduino_ci: 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v2 12 | - uses: Arduino-CI/action@stable-1.x 13 | env: 14 | CUSTOM_INIT_SCRIPT: install_dependencies.sh 15 | 16 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: arduino/arduino-lint-action 2 | 3 | # Controls when the action will run. 4 | on: 5 | # Triggers the workflow on push or pull request events but only for the master branch 6 | push: 7 | branches: [ master ] 8 | pull_request: 9 | branches: [ master ] 10 | 11 | # Allows you to run this workflow manually from the Actions tab 12 | workflow_dispatch: 13 | 14 | # A workflow run is made up of one or more jobs that can run sequentially or in parallel 15 | jobs: 16 | # This workflow contains a single job called "build" 17 | lint: 18 | # The type of runner that the job will run on 19 | runs-on: ubuntu-latest 20 | 21 | # Steps represent a sequence of tasks that will be executed as part of the job 22 | steps: 23 | # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it 24 | - uses: actions/checkout@v2 25 | - uses: arduino/arduino-lint-action@v1 26 | with: 27 | library-manager: update 28 | compliance: strict 29 | 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # N64PadForArduino - Nintendo 64/GameCube controller interface library for Arduino 2 | 3 | ![GitHub release (latest by date)](https://img.shields.io/github/v/release/SukkoPera/N64PadForArduino) 4 | ![GitHub Release Date](https://img.shields.io/github/release-date/SukkoPera/N64PadForArduino?color=blue&label=last%20release) 5 | ![GitHub commits since latest release (by date)](https://img.shields.io/github/commits-since/SukkoPera/N64PadForArduino/latest?color=orange) 6 | ![Hits](https://hitcounter.pythonanywhere.com/count/tag.svg?url=https%3A%2F%2Fgithub.com%2FSukkoPera%2FN64PadForArduino) 7 | 8 | [![arduino/arduino-lint-action](https://github.com/SukkoPera/N64PadForArduino/actions/workflows/main.yml/badge.svg)](https://github.com/SukkoPera/N64PadForArduino/actions/workflows/main.yml) 9 | [![Arduino_CI](https://github.com/SukkoPera/N64PadForArduino/actions/workflows/arduino_ci.yml/badge.svg)](https://github.com/SukkoPera/N64PadForArduino/actions/workflows/arduino_ci.yml) 10 | 11 | N64PadForArduino is an Arduino library that allows interfacing controllers designed for the Nintendo 64 and GameCube with Arduino boards. 12 | 13 | The N64/GC controller protocol is pretty fast, as every bit is 4 microseconds long. Consequently, "slow" CPUs have a hard time decoding it. There are a few code samples out there but they are unreliable, so I set to write the one library to rule them all. 14 | 15 | ## Features 16 | Currently, N64PadForArduino provides access to all buttons and axes available on N64 and GC controllers. 17 | 18 | It does NOT allow interacting with the MemoryPak on N64 controllers nor driving the vibration motors available on GC controllers. I'm not interested in these features, but if you are, please open an Issue saying so. If many people ask, I will look into them. 19 | 20 | ## Using the Library 21 | The N64/GC protocol only uses a single data pin, which is driven in an open-collector fashion. 22 | 23 | The N64 protocol is so fast that the only reliable way to decode it on a 16 MHz Arduino is using interrupts and an ISR written in assembly language. The library supports both *external* interrupts (i.e.: INT0, INT1, etc.) and *pin-change* interrupts (PCINT0, PCINT1, etc.), so you can use almost any pin. The biggest drawback is that you must choose your pin at compile time. This can be done in the [pinconfig.h file](https://github.com/SukkoPera/N64PadForArduino/blob/master/src/protocol/pinconfig.h). By default, it will use pin 3 on all the supported platforms (Uno/Nano/Leonardo/Mega). (On a side note, I have tried to get rid of this restriction, I succeeded for the C part but I never managed to make the assembly part fast enough, with PCINTs; feel free to try and submit a Pull Request though :)). 24 | 25 | Another restriction is that using more than one controller is next to impossible, unfortunately. 26 | 27 | On the Leonardo, the library will also use Timer1, since it needs to disable the Timer0 interrupt (the one used by `millis()`) while it's talking with the controller for reliability reasons. 28 | 29 | Once you have chosen your pin, you can just refer to the [example sketches](https://github.com/SukkoPera/N64PadForArduino/tree/master/examples/) to learn how to use this library, as the interface should be quite straightforward. 30 | 31 | The API has a few rough edges and is not guaranteed to be stable, but any changes will be to make it easier to use. 32 | 33 | Among the examples, there is one which will turn any N64/GC controller into a USB one simply by using an Arduino Leonardo or Micro. It is an excellent way to make a cheap adapter and to test the controller and library. 34 | 35 | ## Wiring the Controller 36 | N64/GC controllers all work at 3.3V. They don't seem to require much current (if someone has exact figures, please provide them) so they will be happy being powered from the Uno onboard 3.3V regulator, which is known to only be able to provide about 50mA. 37 | 38 | You will NOT need any level translator for the data pin. This is because the pin is driven in an open-collector fashion, which never puts voltage on the line but lets a pull-up resistor do the job. Controllers seem to have this pull-up resistor internally, but you might want/need to add an external one, say 1-10k (I'd start with 2.2k since the line must rise quickly). Wire it to 3.3V of course. 39 | 40 | It is common practice to build a cheap extension cable and cut it to get a proper plug. Note that currently you can also get cheap PCB-mount connectors for both the N64 and GC from Chinese shopping sites or from [raphnet](https://www.raphnet-tech.com/products/gc_controller_connector/index.php) in Canada. 41 | 42 | ### Nintendo 64 43 | ![N64 Pinout](extras/N64ControllerPinout.jpg) 44 | 45 | ### GameCube 46 | ![GameCube Pinout](extras/GameCubeControllerPinout.jpg) 47 | 48 | NOTE: The 5V pin seems to only be used to power the rumble motors. Since this library doesn't currently support them, it can be left unconnected. 49 | 50 | ## Compatibility List 51 | N64PadForArduino was primarily tested with official Nintendo controllers, but it aims to be compatible with all devices. If you find one that doesn't work, please open an issue and I'll do my best to add support for it. 52 | 53 | Regarding Arduino boards, it was tested on the Uno, Leonardo and Mega. Other AVR-based boards should work, but the library might need some tailoring regarding interrupt setup. As far as clock speeds are concerned, all tests were done at 16 MHz but the library is written in a way that only depends *slightly* on this exact speed. Actually, the faster, the better, so it should be easy to get it working fine at 20 MHz, for instance. It *might* also work at 8 MHz, too, but this again was not tested (and I actually doubt that, to be honest). Probably the only required change is adding/removing [some of these `nop`s](https://github.com/SukkoPera/N64PadForArduino/blob/master/src/protocol/N64PadProtocol.cpp#L66) in order to make sure the delays actually last as long as they are supposed to and 54 | [some of these](https://github.com/SukkoPera/N64PadForArduino/blob/master/src/protocol/int0.S#L49) to make sure that the subsequent `sbic` instruction falls right in the meaningful part of the bit (that is between 1 and 3 us since the interrupt occurred). 55 | 56 | While it is not a supported board, the library was also tested successfully on the Digispark, and an ATtinyX5 configuration is included. 57 | 58 | It will NOT work on the ESP8266, ESP32, nor STM32 as the ISR is written in AVR assembly. 59 | 60 | ## Releases 61 | If you want to use this library, you are recommended to get [the latest release](https://github.com/SukkoPera/N64PadForArduino/releases) rather than the current git version, as the latter might be under development and is not guaranteed to be working. 62 | 63 | Every release is accompanied by any relevant notes about it, which you are recommended to read carefully. 64 | 65 | ## License 66 | N64PadForArduino is released under the GNU General Public License (GPL) v3. If you make any modifications to the library, **you must** contribute them back. 67 | 68 | N64PadForArduino is provided to you ‘as is’ and without any express or implied warranties whatsoever with respect to its functionality, operability or use, including, without limitation, any implied warranties of merchantability, fitness for a particular purpose or infringement. We expressly disclaim any liability whatsoever for any direct, indirect, consequential, incidental or special damages, including, without limitation, lost revenues, lost profits, losses resulting from business interruption or loss of data, regardless of the form of action or legal theory under which the liability may be asserted, even if advised of the possibility or likelihood of such damages. 69 | 70 | ## Thanks 71 | - [Andrew](https://www.mixdown.ca/n64dev/) 72 | - [James Ward](http://www.int03.co.uk/crema/hardware/gamecube/gc-control.htm) 73 | - All the other guys who helped understand how the N64/GC controller protocol works. 74 | -------------------------------------------------------------------------------- /examples/GCKonamiCode/GCKonamiCode.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * This sketch shows how a precise sequence of button presses can be detected. 21 | * 22 | * For details on the Konami code, see https://en.wikipedia.org/wiki/Konami_Code 23 | */ 24 | 25 | #include 26 | 27 | GCPad pad; 28 | 29 | void setup () { 30 | Serial.begin (115200); 31 | 32 | Serial.println ("Probing for pad..."); 33 | if (pad.begin ()) { 34 | Serial.println ("Pad detected"); 35 | } 36 | delay (500); 37 | 38 | pinMode (LED_BUILTIN, OUTPUT); 39 | 40 | Serial.println ("Enter the Konami code on your pad (Up, up, down, down, left, right, left, right, B, A)"); 41 | } 42 | 43 | #define KONAMI_SEQ_LEN 10 44 | 45 | word konami_seq[KONAMI_SEQ_LEN] = { 46 | GCPad::BTN_D_UP, 47 | GCPad::BTN_D_UP, 48 | GCPad::BTN_D_DOWN, 49 | GCPad::BTN_D_DOWN, 50 | GCPad::BTN_D_LEFT, 51 | GCPad::BTN_D_RIGHT, 52 | GCPad::BTN_D_LEFT, 53 | GCPad::BTN_D_RIGHT, 54 | GCPad::BTN_B, 55 | GCPad::BTN_A 56 | }; 57 | 58 | void loop () { 59 | static byte konami_cnt = 0; 60 | 61 | pad.read (); 62 | 63 | digitalWrite (LED_BUILTIN, pad.buttons != 0); 64 | 65 | if (konami_cnt % 2 == 0) { 66 | // Wait for next button to be PRESSED alone 67 | if (pad.buttons == konami_seq[konami_cnt / 2]) 68 | konami_cnt++; 69 | else if (pad.buttons != 0) 70 | konami_cnt = 0; 71 | } else { 72 | // Wait for last button pressed to be RELEASED 73 | if (pad.buttons == 0) 74 | konami_cnt++; 75 | else if (pad.buttons != konami_seq[konami_cnt / 2]) 76 | konami_cnt = 0; 77 | } 78 | 79 | // Check if sequence is complete 80 | if (konami_cnt == KONAMI_SEQ_LEN * 2) { 81 | Serial.println ("Konami got you!"); 82 | konami_cnt = 0; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /examples/GCPadDump/GCPadDump.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * Sketch that shows basic usage of the library, reading the controller status 21 | * and continuously reporting its state. 22 | * 23 | * When looking straight at the connector on the controller cable, you will see 24 | * two rows of pins. Let's number the top row 3, 2, 1 from left to right and the 25 | * bottom row 6, 5, 4. Then: 26 | * 27 | * 1. +5V (for rumble motor only) 28 | * 2. Data 29 | * 3. Ground for +5V 30 | * 4. Ground for 3.3V 31 | * 5. ? 32 | * 6. +3.3V 33 | * 7. Ground (for shielding) 34 | * 35 | * There is a picture in the extras directory that will help with identifying 36 | * which pin is which. 37 | * 38 | * Connect: 39 | * - Ground for +3.3V 40 | * - +3.3V 41 | * - Data with pin 2 - WARNING: Only this pin is supported for input at the 42 | * moment! 43 | * - Optionally +5V, if you want to use rumble functions 44 | * 45 | * Finally add a pullup resistor between +3.3V and Data. I used 10 kOhm, but 46 | * probably anything in the range 1-10k will be fine. This might even be 47 | * unnecessary, as some controllers have an internal one, so just try! But again, 48 | * note that it's +3.3V, NOT +5V! 49 | * 50 | * Note that I have distinghuished the grounds, as to get my official controller 51 | * to work, I had to connect pin 4, it didn't work with pin 3 only! 52 | */ 53 | 54 | #include 55 | 56 | GCPad pad; 57 | 58 | void setup () { 59 | Serial.begin (115200); 60 | 61 | Serial.println ("Probing for pad..."); 62 | if (pad.begin ()) { 63 | Serial.println ("Pad detected"); 64 | } 65 | delay (500); 66 | 67 | pinMode (LED_BUILTIN, OUTPUT); 68 | } 69 | 70 | 71 | void loop () { 72 | pad.read (); 73 | 74 | digitalWrite (LED_BUILTIN, pad.buttons != 0); 75 | 76 | Serial.print ("Pressed: "); 77 | if (pad.buttons & GCPad::BTN_A) 78 | Serial.print ("A "); 79 | if (pad.buttons & GCPad::BTN_B) 80 | Serial.print ("B "); 81 | if (pad.buttons & GCPad::BTN_X) 82 | Serial.print ("X "); 83 | if (pad.buttons & GCPad::BTN_Y) 84 | Serial.print ("Y "); 85 | if (pad.buttons & GCPad::BTN_Z) 86 | Serial.print ("Z "); 87 | if (pad.buttons & GCPad::BTN_START) 88 | Serial.print ("Start "); 89 | if (pad.buttons & GCPad::BTN_D_UP) 90 | Serial.print ("Up "); 91 | if (pad.buttons & GCPad::BTN_D_DOWN) 92 | Serial.print ("Down "); 93 | if (pad.buttons & GCPad::BTN_D_LEFT) 94 | Serial.print ("Left "); 95 | if (pad.buttons & GCPad::BTN_D_RIGHT) 96 | Serial.print ("Right "); 97 | if (pad.buttons & GCPad::BTN_L) 98 | Serial.print ("L "); 99 | if (pad.buttons & GCPad::BTN_R) 100 | Serial.print ("R "); 101 | Serial.println (""); 102 | 103 | Serial.print ("X = "); 104 | Serial.println (pad.x); 105 | Serial.print ("Y = "); 106 | Serial.println (pad.y); 107 | 108 | Serial.print ("C-Stick X = "); 109 | Serial.println (pad.c_x); 110 | Serial.print ("C-Stick Y = "); 111 | Serial.println (pad.c_y); 112 | 113 | Serial.print ("Left Trigger = "); 114 | Serial.println (pad.left_trigger); 115 | Serial.print ("Right Trigger = "); 116 | Serial.println (pad.right_trigger); 117 | 118 | Serial.println (""); 119 | 120 | delay (1000); 121 | } 122 | -------------------------------------------------------------------------------- /examples/GCPadToUSB/.arduino-ci.yml: -------------------------------------------------------------------------------- 1 | compile: 2 | platforms: 3 | - leonardo 4 | -------------------------------------------------------------------------------- /examples/GCPadToUSB/GCPadToUSB.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * This sketch shows how the library can be used, together with the Arduino 21 | * Joystick Library, to turn a GameCube controller into an USB one that can be 22 | * used on PCs or on a Raspberry Pi running a GC emulator (one day...) :). 23 | * 24 | * For details on the Arduino Joystick Library, see 25 | * https://github.com/MHeironimus/ArduinoJoystickLibrary. 26 | */ 27 | 28 | #include 29 | #include 30 | 31 | 32 | /** \brief Dead zone for analog sticks 33 | * 34 | * If the analog stick moves less than this value from the center position, it 35 | * is considered still. 36 | * 37 | * \sa ANALOG_IDLE_VALUE 38 | */ 39 | const byte ANALOG_DEAD_ZONE = 20U; 40 | 41 | /** \brief Analog sticks minimum value 42 | * 43 | * Minimum value reported by analog sticks. This usually means that the stick is 44 | * fully either at the top or left position. Note that some sticks might not get 45 | * fully down to this value. 46 | * 47 | * Note that this should actually b -128 to 127, however, the true Nintendo 64 48 | * controller range is about 63% of it (mechanically limited), so the actual 49 | * range is about -81 to 81. Worn controllers might report even less, so we are 50 | * leaving this here for easy customization. 51 | * 52 | * \sa ANALOG_MAX_VALUE 53 | * \sa ANALOG_IDLE_VALUE 54 | */ 55 | const uint8_t ANALOG_MIN_VALUE = 20; 56 | 57 | /** \brief Analog sticks maximum value 58 | * 59 | * Maximum value reported by analog sticks. This usually means that the stick is 60 | * fully either at the bottom or right position. Note that some sticks might not 61 | * get fully up to this value. 62 | * 63 | * \sa ANALOGI_MIN_VALUE 64 | * \sa ANALOG_IDLE_VALUE 65 | */ 66 | const uint8_t ANALOG_MAX_VALUE = 225; 67 | 68 | /** \brief Analog sticks idle value 69 | * 70 | * Value reported when an analog stick is in the (ideal) center position. Note 71 | * that old and worn-out sticks might not self-center perfectly when released, 72 | * so you should never rely on this precise value to be reported. 73 | * 74 | * \sa ANALOG_MIN_VALUE 75 | * \sa ANALOG_MAX_VALUE 76 | */ 77 | const uint8_t ANALOG_IDLE_VALUE = 127; 78 | 79 | /** \brief Analog triggers minimum value 80 | * 81 | * Minimum value reported by analog triggers. 82 | * 83 | * Note that this should actually b -128 to 127, however, the true Nintendo 64 84 | * controller range is about 63% of it (mechanically limited), so the actual 85 | * range is about -81 to 81. Worn controllers might report even less, so we are 86 | * leaving this here for easy customization. 87 | * 88 | * \sa TRIGGER_MAX_VALUE 89 | */ 90 | const uint8_t TRIGGER_MIN_VALUE = 10; 91 | 92 | /** \brief Analog triggers maximum value 93 | * 94 | * Maximum value reported by analog sticks. This means that the stick is fully 95 | * pressed. Note that some sticks might not get fully up to this value. 96 | * 97 | * \sa TRIGGER_MIN_VALUE 98 | */ 99 | const uint8_t TRIGGER_MAX_VALUE = 250; 100 | 101 | /** \brief Analog triggers threshold value 102 | * 103 | * Trigger buttons appear both as analog accelerator/brake pedals and as digital 104 | * buttons. The latter will be reported as pressed when the analog value gets 105 | * past this threshold. 106 | * 107 | * \sa TRIGGER_MIN_VALUE 108 | * \sa TRIGGER_MAX_VALUE 109 | */ 110 | const uint8_t L_R_THRESHOLD = 100; 111 | 112 | /******************************************************************************/ 113 | 114 | 115 | GCPad pad; 116 | 117 | Joystick_ usbStick ( 118 | JOYSTICK_DEFAULT_REPORT_ID, 119 | JOYSTICK_TYPE_MULTI_AXIS, 120 | 8, // buttonCount 121 | 0, // hatSwitchCount (0-2) 122 | true, // includeXAxis 123 | true, // includeYAxis 124 | false, // includeZAxis 125 | true, // includeRxAxis 126 | true, // includeRyAxis 127 | false, // includeRzAxis 128 | false, // includeRudder 129 | false, // includeThrottle 130 | true, // includeAccelerator 131 | true, // includeBrake 132 | false // includeSteering 133 | ); 134 | 135 | bool mapLeftStickToDPad = false; 136 | 137 | #define toDegrees(rad) (rad * 180.0 / PI) 138 | 139 | #define deadify(var, thres) (abs (var) > thres ? (var) : 0) 140 | 141 | 142 | void setup () { 143 | pinMode (LED_BUILTIN, OUTPUT); 144 | 145 | if (!pad.begin ()) { 146 | // Report error, somehow 147 | while (1) { 148 | digitalWrite (LED_BUILTIN, HIGH); 149 | delay (300); 150 | digitalWrite (LED_BUILTIN, LOW); 151 | delay (700); 152 | } 153 | } 154 | 155 | // Check for button A 156 | pad.read (); 157 | if ((pad.buttons & GCPad::BTN_A) != 0) { 158 | mapLeftStickToDPad = true; 159 | 160 | // Signal we got it! 161 | digitalWrite (LED_BUILTIN, HIGH); 162 | delay (200); 163 | digitalWrite (LED_BUILTIN, LOW); 164 | delay (200); 165 | digitalWrite (LED_BUILTIN, HIGH); 166 | delay (200); 167 | digitalWrite (LED_BUILTIN, LOW); 168 | delay (200); 169 | digitalWrite (LED_BUILTIN, HIGH); 170 | delay (1000); 171 | } 172 | 173 | // Init Joystick library 174 | usbStick.begin (false); // We'll call sendState() manually to minimize lag 175 | usbStick.setXAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE); 176 | usbStick.setYAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE); 177 | usbStick.setRxAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE); 178 | usbStick.setRyAxisRange (ANALOG_MAX_VALUE, ANALOG_MIN_VALUE); // Analog is positive UP on controller, DOWN in joystick library 179 | usbStick.setAcceleratorRange (0, 260); 180 | usbStick.setBrakeRange (0, 260); 181 | } 182 | 183 | // Value axes report when centered 184 | #define CENTER_POS 127 185 | 186 | void loop () { 187 | pad.read (); 188 | 189 | digitalWrite (LED_BUILTIN, pad.buttons != 0); 190 | 191 | // Buttons first! 192 | usbStick.setButton (0, (pad.buttons & GCPad::BTN_A) != 0); 193 | usbStick.setButton (1, (pad.buttons & GCPad::BTN_B) != 0); 194 | usbStick.setButton (2, (pad.buttons & GCPad::BTN_X) != 0); 195 | usbStick.setButton (3, (pad.buttons & GCPad::BTN_Y) != 0); 196 | usbStick.setButton (4, (pad.buttons & GCPad::BTN_Z) != 0); 197 | usbStick.setButton (5, (pad.buttons & GCPad::BTN_START) != 0); 198 | 199 | // Use analog value to trigger L & R 200 | usbStick.setButton (6, pad.left_trigger > L_R_THRESHOLD); 201 | usbStick.setButton (7, pad.right_trigger > L_R_THRESHOLD); 202 | 203 | // If you prefer to trigger them on full stop use this 204 | //~ usbStick.setButton (6, (pad.buttons & GCPad::BTN_L) != 0); 205 | //~ usbStick.setButton (7, (pad.buttons & GCPad::BTN_R) != 0); 206 | 207 | // L & R are also mapped to accelerator and brake 208 | usbStick.setBrake (pad.left_trigger); 209 | usbStick.setAccelerator (pad.right_trigger); 210 | 211 | // D-Pad makes up the X/Y axes 212 | if ((pad.buttons & GCPad::BTN_D_UP) != 0) { 213 | usbStick.setYAxis (ANALOG_MIN_VALUE); 214 | } else if ((pad.buttons & GCPad::BTN_D_DOWN) != 0) { 215 | usbStick.setYAxis (ANALOG_MAX_VALUE); 216 | } else { 217 | usbStick.setYAxis (ANALOG_IDLE_VALUE); 218 | } 219 | 220 | if ((pad.buttons & GCPad::BTN_D_LEFT) != 0) { 221 | usbStick.setXAxis (ANALOG_MIN_VALUE); 222 | } else if ((pad.buttons & GCPad::BTN_D_RIGHT) != 0) { 223 | usbStick.setXAxis (ANALOG_MAX_VALUE); 224 | } else { 225 | usbStick.setXAxis (ANALOG_IDLE_VALUE); 226 | } 227 | 228 | // Set the analog sticks 229 | if (!mapLeftStickToDPad) { 230 | // The analog stick gets mapped to the X/Y rotation axes 231 | usbStick.setRxAxis (pad.x); 232 | usbStick.setRyAxis (pad.y); 233 | } else { 234 | // TBD 235 | //~ controllerData.dpadUpOn |= pad.y > (CENTER_POS + STICK_DPAD_EMU_THRESHOLD); 236 | //~ controllerData.dpadDownOn |= pad.y < (CENTER_POS - STICK_DPAD_EMU_THRESHOLD); 237 | //~ controllerData.dpadLeftOn |= pad.x < (CENTER_POS - STICK_DPAD_EMU_THRESHOLD); 238 | //~ controllerData.dpadRightOn |= pad.x > (CENTER_POS + STICK_DPAD_EMU_THRESHOLD); 239 | } 240 | 241 | // "C" analog is the hat switch 242 | // We flip coordinates to avoid having to invert them in atan2() 243 | int8_t rx = ANALOG_IDLE_VALUE - pad.c_x - 1; // [0 ... 255] -> [127 ... -128] 244 | rx = deadify (rx, ANALOG_DEAD_ZONE); 245 | 246 | int8_t ry = ANALOG_IDLE_VALUE - pad.c_y - 1; 247 | ry = deadify (ry, ANALOG_DEAD_ZONE); 248 | 249 | if (rx == 0 && ry == 0) { 250 | usbStick.setHatSwitch (0, JOYSTICK_HATSWITCH_RELEASE); 251 | } else { 252 | /* atan2() will yield something between -PI and +PI, so we 253 | * add 2*PI first to make it always positive, and then we 254 | * subtract PI / 2 because setHatSwitch() has 0 degrees at 255 | * north. 256 | * 257 | * Also we would need to invert the arguments to atan2() 258 | * since setHatSwitch() grows clockwise while radians go the 259 | * other way, but we have already done that when we 260 | * calculated rx and ry. Smart, huh? 261 | */ 262 | float angle = atan2 (ry, rx) + 2 * PI - PI / 2; 263 | uint16_t intAngle = ((uint16_t) (toDegrees (angle) + 0.5)) % 360; 264 | usbStick.setHatSwitch (0, intAngle); 265 | } 266 | 267 | // All done, send data for real! 268 | usbStick.sendState (); 269 | } 270 | -------------------------------------------------------------------------------- /examples/MegaDriveMultiplexer/MegaDriveMultiplexer.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * This sketch is the multiplexer that should be run on a second Arduino to get 21 | * the MegaDriveInterface sketch working. Please see that sketch for 22 | * instructions. 23 | * 24 | * Note that this sketch MUST BE RUN ON AN ARDUINO wired exactly as described. 25 | * The timing was very tricky to figure out. I have tried to use an ATtiny861 26 | * instead but couldn't get it working, for the sake of it! 27 | */ 28 | 29 | #define INPORT1 PINB 30 | #define DIR1 DDRB 31 | #define MASK1 0x0F 32 | 33 | #define INPORT2 PIND 34 | #define DIR2 DDRD 35 | #define MASK2 0x0F 36 | 37 | #define OUTPORT PORTC 38 | #define OUTDIR DDRC 39 | #define OUTMASK 0x0F 40 | 41 | #define SELECT_PIN_PINPORT PIND 42 | #define SELECT_PIN_DIR DDRD 43 | #define SELECT_PIN_BIT PD7 44 | 45 | void setup () { 46 | DIR1 &= ~MASK1; 47 | DIR2 &= ~MASK2; 48 | 49 | OUTDIR |= OUTMASK; 50 | 51 | SELECT_PIN_DIR &= ~(1 << SELECT_PIN_BIT); 52 | 53 | noInterrupts (); 54 | } 55 | 56 | #define NOP __asm__ __volatile__ ("nop\n\t") 57 | 58 | void loop () { 59 | while (1) { 60 | if (SELECT_PIN_PINPORT & (1 << SELECT_PIN_BIT)) { 61 | OUTPORT = INPORT2; 62 | } else { 63 | OUTPORT = INPORT1; 64 | NOP; // Hold for a bit 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /examples/N64PadDump/N64PadDump.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * Sketch that shows basic usage of the library, reading the controller status 21 | * and continuously reporting its state. 22 | * 23 | * When looking straight at the connector on the controller cable, pinout is as 24 | * follows: 25 | * _ 26 | * /^ ^\ 27 | * / \ 28 | * |_._._._| 29 | * 1 2 3 30 | * 31 | * Where: 32 | * - 1. Ground 33 | * - 2. Data 34 | * - 3. Vcc 35 | * 36 | * Connect: 37 | * - Ground with... well, ground 38 | * - Data with pin 2 - WARNING: Only this pin is supported for input at the 39 | * moment! 40 | * - Vcc with 3v3 (Warning! The controller is NOT a 5v part!) 41 | * 42 | * Finally add a pullup resistor between 3v3 and Data. I used 10 kOhm, but 43 | * probably anything in the range 1-10k will be fine. 44 | * 45 | * (Pardon my sub-par ASCII-art skillz!) 46 | */ 47 | 48 | #include 49 | 50 | N64Pad pad; 51 | 52 | void setup () { 53 | Serial.begin (115200); 54 | while (!Serial) 55 | ; 56 | 57 | Serial.println ("Ready!"); 58 | } 59 | 60 | 61 | void loop () { 62 | static boolean haveController = false; 63 | static uint16_t oldButtons = 0; 64 | static int8_t oldX = 0, oldY = 0; 65 | 66 | if (!haveController) { 67 | if (pad.begin ()) { 68 | // Controller detected! 69 | digitalWrite (LED_BUILTIN, HIGH); 70 | Serial.println (F("Controller found!")); 71 | haveController = true; 72 | } else { 73 | delay (333); 74 | } 75 | } else { 76 | if (!pad.read ()) { 77 | // Controller lost :( 78 | digitalWrite (LED_BUILTIN, LOW); 79 | Serial.println (F("Controller lost :(")); 80 | haveController = false; 81 | } else { 82 | if (pad.buttons != oldButtons || pad.x != oldX || pad.y != oldY) { 83 | Serial.print ("Pressed: "); 84 | if (pad.buttons & N64Pad::BTN_A) 85 | Serial.print ("A "); 86 | if (pad.buttons & N64Pad::BTN_B) 87 | Serial.print ("B "); 88 | if (pad.buttons & N64Pad::BTN_Z) 89 | Serial.print ("Z "); 90 | if (pad.buttons & N64Pad::BTN_START) 91 | Serial.print ("Start "); 92 | if (pad.buttons & N64Pad::BTN_UP) 93 | Serial.print ("Up "); 94 | if (pad.buttons & N64Pad::BTN_DOWN) 95 | Serial.print ("Down "); 96 | if (pad.buttons & N64Pad::BTN_LEFT) 97 | Serial.print ("Left "); 98 | if (pad.buttons & N64Pad::BTN_RIGHT) 99 | Serial.print ("Right "); 100 | if (pad.buttons & N64Pad::BTN_L) 101 | Serial.print ("L "); 102 | if (pad.buttons & N64Pad::BTN_R) 103 | Serial.print ("R "); 104 | if (pad.buttons & N64Pad::BTN_C_UP) 105 | Serial.print ("C_Up "); 106 | if (pad.buttons & N64Pad::BTN_C_DOWN) 107 | Serial.print ("C_Down "); 108 | if (pad.buttons & N64Pad::BTN_C_LEFT) 109 | Serial.print ("C_Left "); 110 | if (pad.buttons & N64Pad::BTN_C_RIGHT) 111 | Serial.print ("C_Right "); 112 | Serial.println (""); 113 | 114 | Serial.print ("X = "); 115 | Serial.println (pad.x); 116 | Serial.print ("Y = "); 117 | Serial.println (pad.y); 118 | 119 | Serial.println (""); 120 | 121 | oldButtons = pad.buttons; 122 | oldX = pad.x; 123 | oldY = pad.y; 124 | } 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /examples/N64PadToMegaDrive/N64PadToMegaDrive.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * First of all be aware that for this to work you will need to use either: 21 | * - A 74HC157 Quad Multiplexer (The same chip that is used in original 22 | * MegaDrive controllers!) 23 | * - A second Arduino running the MegaDriveMultiplexer example. 24 | * 25 | * This is due to the speed required to give the Megadrive correct readings. 26 | * Please refer to the following documents to understand the protocol: 27 | * - https://code.google.com/p/micro-64-controller/wiki/Protocol 28 | * - http://afermiano.com/index.php/n64-controller-protocol 29 | * - http://eecs.umich.edu/courses/eecs373/Lec/StudentW14/N64%20Controller.pdf 30 | * 31 | * Experience shows that when the SELECT signal changes state, the outputs must 32 | * be switched *very* quickly (i.e.: a couple us). I had to get rid of all the 33 | * bitwise logic and even of interrupts to get a 16 MHz Arduino-based 34 | * multiplexer deliver correct readings. Of course this doesn't play very well 35 | * with the fact that N64 controller readings take ~200 us, so the chances of 36 | * doing everything with a single Arduino are very low. 37 | * 38 | * The sketch works by polling the N64 Controller and delivering outputs as 39 | * follows: 40 | * - UP and DOWN are connected straight to the MegaDrive 41 | * - LEFT, RIGHT, B and C are delivered as a set to the external multiplexer. 42 | * - A and START are delivered as another set to the external multiplexer. 43 | * 44 | * Button mappings: 45 | * - A and L are mapped to A 46 | * - B and R are mapped to B 47 | * - Z and the four C buttons are mapped to C 48 | * - Start is mapped to... well, Start 49 | * - The analog stick is mapped to the directional buttons 50 | * 51 | * Connections if using the 74HC157: 52 | * - Arduino Pin 2 -> 74HC157 Pin 2 53 | * - Arduino Pin 3 -> 74HC157 Pin 5 54 | * - Arduino Pin 4 -> 74HC157 Pin 11 55 | * - Arduino Pin 5 -> 74HC157 Pin 14 56 | * - Arduino Pin 8 -> MegaDrive Pad Port Pin 1 57 | * - Arduino Pin 9 -> MegaDrive Pad Port Pin 2 58 | * - Arduino Pin 10 -> 74HC157 Pin 3 59 | * - Arduino Pin 11 -> 74HC157 Pin 6 60 | * - Arduino Pin 12 -> 74HC157 Pin 10 61 | * - Arduino Pin 13 -> 74HC157 Pin 13 62 | * - Arduino +5V -> 74HC157 Pin 16 63 | * - Arduino GND -> 74HC157 Pin 8 64 | * - 74HC157 Pin 15 -> 74HC157 Pin 8 (or Arduino GND) 65 | * - MegaDrive Pad Port Pin 7 -> 74HC157 Pin 1 66 | * - 74HC157 Pin 4 -> MegaDrive Pad Port Pin 3 67 | * - 74HC157 Pin 7 -> MegaDrive Pad Port Pin 4 68 | * - 74HC157 Pin 9 -> MegaDrive Pad Port Pin 6 69 | * - 74HC157 Pin 12 -> MegaDrive Pad Port Pin 9 70 | * - MegaDrive Pad Port Pin 8 -> Arduino GND (or 74HC157 Pin 8) 71 | * 72 | * Connections if using a second Arduino with MegaDriveMultiplexer: 73 | * - Arduino Pin 2 -> MegaDriveMultiplexer Pin 8 74 | * - Arduino Pin 3 -> MegaDriveMultiplexer Pin 9 75 | * - Arduino Pin 4 -> MegaDriveMultiplexer Pin 10 76 | * - Arduino Pin 5 -> MegaDriveMultiplexer Pin 11 77 | * - Arduino Pin 8 -> MegaDrive Pad Port Pin 1 78 | * - Arduino Pin 9 -> MegaDrive Pad Port Pin 2 79 | * - Arduino Pin 10 -> MegaDriveMultiplexer Pin 0 80 | * - Arduino Pin 11 -> MegaDriveMultiplexer Pin 1 81 | * - Arduino Pin 12 -> MegaDriveMultiplexer Pin 2 82 | * - Arduino Pin 13 -> MegaDriveMultiplexer Pin 3 83 | * - Arduino +5V -> MegaDriveMultiplexer Vin (If not otherwise powered) 84 | * - Arduino GND -> MegaDriveMultiplexer GND 85 | * - MegaDrive Pad Port Pin 7 -> MegaDriveMultiplexer Pin 7 86 | * - MegaDriveMultiplexer Pin A0 -> MegaDrive Pad Port Pin 3 87 | * - MegaDriveMultiplexer Pin A1 -> MegaDrive Pad Port Pin 4 88 | * - MegaDriveMultiplexer Pin A2 -> MegaDrive Pad Port Pin 6 89 | * - MegaDriveMultiplexer Pin A3 -> MegaDrive Pad Port Pin 9 90 | * - MegaDrive Pad Port Pin 8 -> Arduino GND (or MegaDriveMultiplexer GND) 91 | * 92 | * In both cases: 93 | * - (Optional) Pin A5 goes to a LED + series resistor that will blink when 94 | * buttons are pressed. Useful to show that something is working. 95 | * - If you want to power the Arduinos from the MegaDrive, you can take +5V from 96 | * Pad Port Pin 5. I can't guarantee the MegaDrive can stand the extra load, 97 | * but its voltage regulators are mounted on a big heatsink, so I guess it 98 | * will :). 99 | * 100 | * Note that in this sketch we use direct port manipulation to change all the 101 | * bits at once. This is not strictly necessary, so it may change in the future. 102 | */ 103 | 104 | #include 105 | 106 | /* These are the offsets that the analog stick must move before we trigger the 107 | * corresponding directional button 108 | * 109 | * 20 means about a quarter, feels fine to me 110 | */ 111 | #define MIN_X_OFFSET 20 112 | #define MIN_Y_OFFSET MIN_X_OFFSET 113 | 114 | // We use pin 13 for other stuff 115 | #define LED_PIN A5 116 | 117 | N64Pad pad; 118 | 119 | void setup () { 120 | pinMode (LED_PIN, OUTPUT); 121 | 122 | if (!pad.begin ()) { 123 | // Report error, somehow 124 | while (1) { 125 | digitalWrite (LED_PIN, HIGH); 126 | delay (300); 127 | digitalWrite (LED_PIN, LOW); 128 | delay (700); 129 | } 130 | } 131 | 132 | // Setup pins for MD output 133 | DDRD |= ((1 << DDD2) | (1 << DDD3) | (1 << DDD4) | (1 << DDD5)); 134 | DDRB |= ((1 << DDB2) | (1 << DDB3) | (1 << DDB4) | (1 << DDB5)); 135 | DDRB |= ((1 << DDB1) | (1 << DDB0)); 136 | } 137 | 138 | 139 | // Update as fast as we can 140 | void loop () { 141 | pad.read (); 142 | 143 | /* To understand this, keep in mind that the MegaDrive uses the LOW state to 144 | * indicate that a button is pressed, and review De Morgan's laws 145 | */ 146 | PORTB = (PORTB & 0xFC) 147 | | (pad.y < MIN_Y_OFFSET && ((pad.buttons & N64Pad::BTN_UP) == 0)) << PB0 148 | | (pad.y > -MIN_Y_OFFSET && ((pad.buttons & N64Pad::BTN_DOWN) == 0)) << PB1 149 | ; 150 | 151 | // This is the set of outputs to send the MegaDrive when SELECT is LOW 152 | PORTD = (PORTD & 0xC3) 153 | | ((pad.buttons & (N64Pad::BTN_A | N64Pad::BTN_L)) == 0) << PD4 154 | | ((pad.buttons & N64Pad::BTN_START) == 0) << PD5 155 | ; 156 | 157 | // This is the set of outputs to send the MegaDrive when SELECT is HIGH 158 | PORTB = (PORTB & 0xC3) 159 | | (pad.x > -MIN_X_OFFSET && ((pad.buttons & N64Pad::BTN_LEFT) == 0)) << PB2 160 | | (pad.x < MIN_X_OFFSET && ((pad.buttons & N64Pad::BTN_RIGHT) == 0)) << PB3 161 | | ((pad.buttons & (N64Pad::BTN_B | N64Pad::BTN_R)) == 0) << PB4 162 | | ((pad.buttons & (N64Pad::BTN_C_UP | N64Pad::BTN_C_DOWN | N64Pad::BTN_C_LEFT | N64Pad::BTN_C_RIGHT | N64Pad::BTN_Z)) == 0) << PB5 163 | ; 164 | 165 | // Blink led with buttons 166 | digitalWrite (LED_PIN, pad.buttons != 0); 167 | } 168 | -------------------------------------------------------------------------------- /examples/N64PadToUSB/.arduino-ci.yml: -------------------------------------------------------------------------------- 1 | compile: 2 | platforms: 3 | - leonardo 4 | -------------------------------------------------------------------------------- /examples/N64PadToUSB/N64PadToUSB.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * This sketch shows how the library can be used, together with the Arduino 21 | * Joystick Library, to turn a N64 controller into an USB one that can be used 22 | * on PCs or on a Raspberry Pi running a N64 emulator :). 23 | * 24 | * For details on the Arduino Joystick Library, see 25 | * https://github.com/MHeironimus/ArduinoJoystickLibrary. 26 | */ 27 | 28 | #include 29 | #include 30 | 31 | /** \brief Dead zone for analog sticks 32 | * 33 | * If the analog stick moves less than this value from the center position, it 34 | * is considered still when it emulates the D-Pad. 35 | * 36 | * \sa ANALOG_IDLE_VALUE 37 | */ 38 | const byte ANALOG_DEAD_ZONE = 20U; 39 | 40 | /** \brief Analog sticks minimum value 41 | * 42 | * Minimum value reported by analog sticks. This usually means that the stick is 43 | * fully either at the top or left position. Note that some sticks might not get 44 | * fully down to this value. 45 | * 46 | * Note that this should actually b -128 to 127, however, the true Nintendo 64 47 | * controller range is about 63% of it (mechanically limited), so the actual 48 | * range is about -81 to 81. Worn controllers might report even less, so we are 49 | * leaving this here for easy customization. 50 | * 51 | * \sa ANALOG_MAX_VALUE 52 | * \sa ANALOG_IDLE_VALUE 53 | */ 54 | const int8_t ANALOG_MIN_VALUE = -80; 55 | 56 | /** \brief Analog sticks maximum value 57 | * 58 | * Maximum value reported by analog sticks. This usually means that the stick is 59 | * fully either at the bottom or right position. Note that some sticks might not 60 | * get fully up to this value. 61 | * 62 | * \sa ANALOGI_MIN_VALUE 63 | * \sa ANALOG_IDLE_VALUE 64 | */ 65 | const int8_t ANALOG_MAX_VALUE = 80; 66 | 67 | /** \brief Analog sticks idle value 68 | * 69 | * Value reported when an analog stick is in the (ideal) center position. Note 70 | * that old and worn-out sticks might not self-center perfectly when released, 71 | * so you should never rely on this precise value to be reported. 72 | * 73 | * \sa ANALOG_MIN_VALUE 74 | * \sa ANALOG_MAX_VALUE 75 | */ 76 | const int8_t ANALOG_IDLE_VALUE = 0; 77 | 78 | /******************************************************************************/ 79 | 80 | N64Pad pad; 81 | 82 | Joystick_ usbStick ( 83 | JOYSTICK_DEFAULT_REPORT_ID, 84 | JOYSTICK_TYPE_MULTI_AXIS, 85 | 10, // buttonCount 86 | 0, // hatSwitchCount (0-2) 87 | true, // includeXAxis 88 | true, // includeYAxis 89 | false, // includeZAxis 90 | true, // includeRxAxis 91 | true, // includeRyAxis 92 | false, // includeRzAxis 93 | false, // includeRudder 94 | false, // includeThrottle 95 | false, // includeAccelerator 96 | false, // includeBrake 97 | false // includeSteering 98 | ); 99 | 100 | bool mapAnalogToDPad = false; 101 | 102 | #define deadify(var, thres) (abs (var) > thres ? (var) : 0) 103 | 104 | void flashLed (byte n) { 105 | for (byte i = 0; i < n; ++i) { 106 | digitalWrite (LED_BUILTIN, LOW); 107 | delay (40); 108 | digitalWrite (LED_BUILTIN, HIGH); 109 | delay (80); 110 | } 111 | } 112 | 113 | void setup () { 114 | pinMode (LED_BUILTIN, OUTPUT); 115 | 116 | // Init Joystick library 117 | usbStick.begin (false); // We'll call sendState() manually to minimize lag 118 | usbStick.setXAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE); 119 | usbStick.setYAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE); 120 | usbStick.setRxAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE); 121 | usbStick.setRyAxisRange (ANALOG_MAX_VALUE, ANALOG_MIN_VALUE); // Analog is positive UP on controller, DOWN in joystick library 122 | } 123 | 124 | 125 | void loop () { 126 | static boolean haveController = false; 127 | 128 | if (!haveController) { 129 | if (pad.begin ()) { 130 | // Controller detected! 131 | digitalWrite (LED_BUILTIN, HIGH); 132 | haveController = true; 133 | } else { 134 | delay (333); 135 | } 136 | } else { 137 | if (!pad.read ()) { 138 | // Controller lost :( 139 | digitalWrite (LED_BUILTIN, LOW); 140 | haveController = false; 141 | } else { 142 | // Controller was read fine 143 | if ((pad.buttons & N64Pad::BTN_LRSTART) != 0) { 144 | // This combo toggles mapAnalogToDPad 145 | mapAnalogToDPad = !mapAnalogToDPad; 146 | flashLed (2 + (byte) mapAnalogToDPad); 147 | } else { 148 | // Map buttons! 149 | usbStick.setButton (0, (pad.buttons & N64Pad::BTN_B) != 0); 150 | usbStick.setButton (1, (pad.buttons & N64Pad::BTN_A) != 0); 151 | usbStick.setButton (2, (pad.buttons & N64Pad::BTN_C_LEFT) != 0); 152 | usbStick.setButton (3, (pad.buttons & N64Pad::BTN_C_DOWN) != 0); 153 | usbStick.setButton (4, (pad.buttons & N64Pad::BTN_C_UP) != 0); 154 | usbStick.setButton (5, (pad.buttons & N64Pad::BTN_C_RIGHT) != 0); 155 | usbStick.setButton (6, (pad.buttons & N64Pad::BTN_L) != 0); 156 | usbStick.setButton (7, (pad.buttons & N64Pad::BTN_R) != 0); 157 | usbStick.setButton (8, (pad.buttons & N64Pad::BTN_Z) != 0); 158 | usbStick.setButton (9, (pad.buttons & N64Pad::BTN_START) != 0); 159 | 160 | if (!mapAnalogToDPad) { 161 | // D-Pad makes up the X/Y axes 162 | if ((pad.buttons & N64Pad::BTN_UP) != 0) { 163 | usbStick.setYAxis (ANALOG_MIN_VALUE); 164 | } else if ((pad.buttons & N64Pad::BTN_DOWN) != 0) { 165 | usbStick.setYAxis (ANALOG_MAX_VALUE); 166 | } else { 167 | usbStick.setYAxis (ANALOG_IDLE_VALUE); 168 | } 169 | 170 | if ((pad.buttons & N64Pad::BTN_LEFT) != 0) { 171 | usbStick.setXAxis (ANALOG_MIN_VALUE); 172 | } else if ((pad.buttons & N64Pad::BTN_RIGHT) != 0) { 173 | usbStick.setXAxis (ANALOG_MAX_VALUE); 174 | } else { 175 | usbStick.setXAxis (ANALOG_IDLE_VALUE); 176 | } 177 | 178 | // The analog stick gets mapped to the X/Y rotation axes 179 | usbStick.setRxAxis (pad.x); 180 | usbStick.setRyAxis (pad.y); 181 | } else { 182 | // Both the D-Pad and analog stick control the X/Y axes 183 | if ((pad.buttons & N64Pad::BTN_UP || pad.y > ANALOG_DEAD_ZONE) != 0) { 184 | usbStick.setYAxis (ANALOG_MIN_VALUE); 185 | } else if ((pad.buttons & N64Pad::BTN_DOWN || pad.y < -ANALOG_DEAD_ZONE) != 0) { 186 | usbStick.setYAxis (ANALOG_MAX_VALUE); 187 | } else { 188 | usbStick.setYAxis (ANALOG_IDLE_VALUE); 189 | } 190 | 191 | if ((pad.buttons & N64Pad::BTN_LEFT || pad.x < -ANALOG_DEAD_ZONE) != 0) { 192 | usbStick.setXAxis (ANALOG_MIN_VALUE); 193 | } else if ((pad.buttons & N64Pad::BTN_RIGHT || pad.x > ANALOG_DEAD_ZONE) != 0) { 194 | usbStick.setXAxis (ANALOG_MAX_VALUE); 195 | } else { 196 | usbStick.setXAxis (ANALOG_IDLE_VALUE); 197 | } 198 | } 199 | 200 | // All done, send data for real! 201 | usbStick.sendState (); 202 | } 203 | } 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /examples/N64PadToUsbDigispark/.arduino-ci.yml: -------------------------------------------------------------------------------- 1 | compile: 2 | platforms: 3 | # Just skip, as Digispark is not available in Arduino_CI 4 | -------------------------------------------------------------------------------- /examples/N64PadToUsbDigispark/N64PadToUsbDigispark.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * This sketch shows how the library can be used to turn a N64 controller into 21 | * an USB one that can be used on PCs or on a Raspberry Pi running a N64 22 | * emulator with a simple and cheap Digispark board :). 23 | * 24 | * For details on the Digispark, see http://digistump.com/products/1. 25 | * 26 | * Please use this core these days: https://github.com/ArminJo/DigistumpArduino. 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | N64Pad pad; 33 | 34 | void setup () { 35 | // We'll never touch these, let's leave them halfway through all along 36 | DigiJoystick.setSLIDER ((byte) 128); 37 | DigiJoystick.setZROT ((byte) 128); 38 | } 39 | 40 | void loop () { 41 | static boolean haveController = false; 42 | 43 | if (!haveController) { 44 | if (pad.begin ()) { 45 | // Controller detected! 46 | haveController = true; 47 | } else { 48 | DigiJoystick.delay (333); 49 | } 50 | } else { 51 | if (!pad.read ()) { 52 | // Controller lost :( 53 | haveController = false; 54 | } else { 55 | // Map buttons! 56 | byte buttonsLow = 0, buttonsHigh = 0; 57 | if ((pad.buttons & N64Pad::BTN_B) != 0) 58 | buttonsLow |= 1 << 0; 59 | if ((pad.buttons & N64Pad::BTN_A) != 0) 60 | buttonsLow |= 1 << 1; 61 | if ((pad.buttons & N64Pad::BTN_C_LEFT) != 0) 62 | buttonsLow |= 1 << 2; 63 | if ((pad.buttons & N64Pad::BTN_C_DOWN) != 0) 64 | buttonsLow |= 1 << 3; 65 | if ((pad.buttons & N64Pad::BTN_C_UP) != 0) 66 | buttonsLow |= 1 << 4; 67 | if ((pad.buttons & N64Pad::BTN_C_RIGHT) != 0) 68 | buttonsLow |= 1 << 5; 69 | if ((pad.buttons & N64Pad::BTN_L) != 0) 70 | buttonsLow |= 1 << 6; 71 | if ((pad.buttons & N64Pad::BTN_R) != 0) 72 | buttonsLow |= 1 << 7; 73 | if ((pad.buttons & N64Pad::BTN_Z) != 0) 74 | buttonsHigh |= 1 << 0; 75 | if ((pad.buttons & N64Pad::BTN_START) != 0) 76 | buttonsHigh |= 1 << 1; 77 | DigiJoystick.setButtons (buttonsLow, buttonsHigh); 78 | 79 | // D-Pad makes up the X/Y axes 80 | if ((pad.buttons & N64Pad::BTN_UP) != 0) { 81 | DigiJoystick.setY ((byte) 0); 82 | } else if ((pad.buttons & N64Pad::BTN_DOWN) != 0) { 83 | DigiJoystick.setY ((byte) 255); 84 | } else { 85 | DigiJoystick.setY ((byte) 128); 86 | } 87 | 88 | if ((pad.buttons & N64Pad::BTN_LEFT) != 0) { 89 | DigiJoystick.setX ((byte) 0); 90 | } else if ((pad.buttons & N64Pad::BTN_RIGHT) != 0) { 91 | DigiJoystick.setX ((byte) 255); 92 | } else { 93 | DigiJoystick.setX ((byte) 128); 94 | } 95 | 96 | // The analog stick gets mapped to the X/Y rotation axes 97 | DigiJoystick.setXROT ((byte) (pad.x + (byte) 128)); 98 | DigiJoystick.setYROT ((byte) (((byte) 128) - pad.y)); // Y grows the opposite way! 99 | } 100 | } 101 | 102 | // Send data for real - Call this at least every 50ms in any case 103 | DigiJoystick.update (); 104 | } 105 | -------------------------------------------------------------------------------- /extras/GameCubeControllerPinout.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukkoPera/N64PadForArduino/04396586d94eb793562dcbe96a5de003745b4b25/extras/GameCubeControllerPinout.jpg -------------------------------------------------------------------------------- /extras/N64ControllerPinout.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukkoPera/N64PadForArduino/04396586d94eb793562dcbe96a5de003745b4b25/extras/N64ControllerPinout.jpg -------------------------------------------------------------------------------- /extras/README.md: -------------------------------------------------------------------------------- 1 | ## Credits 2 | 3 | - N64 Pinout image by [Pieter-Jan](http://www.pieter-jan.com/node/10) 4 | - GameCube Pinout image by [Hunter K](https://filthypants.blogspot.com/2018/12/retro-console-rj45-pinouts-ps360-mc.html) 5 | - Social image by [BobJones6732](https://www.instructables.com/id/Turn-an-N64-Controller-into-a-USB-Gamepad-using-an/) 6 | -------------------------------------------------------------------------------- /extras/social.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukkoPera/N64PadForArduino/04396586d94eb793562dcbe96a5de003745b4b25/extras/social.png -------------------------------------------------------------------------------- /install_dependencies.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # This file is only used by the Arduino_CI GitHub action to install 4 | # the libraries some examples depend upon before compiling 5 | 6 | #wget https://github.com/MHeironimus/ArduinoJoystickLibrary/archive/refs/tags/v2.0.7.tar.gz -o ArduinoJoystickLibrary-2.0.7.tar.gz 7 | curl -Lo ArduinoJoystickLibrary-2.0.7.tar.gz https://github.com/MHeironimus/ArduinoJoystickLibrary/archive/refs/tags/v2.0.7.tar.gz 8 | tar zxvf ArduinoJoystickLibrary-2.0.7.tar.gz 9 | rm -f ArduinoJoystickLibrary-2.0.7.tar.gz 10 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=N64Pad 2 | version=0.2.0 3 | author=SukkoPera 4 | maintainer=SukkoPera 5 | sentence=Interface with Nintendo 64/GameCube controllers 6 | paragraph=Only tested with official Nintendo controllers 7 | category=Device Control 8 | url=https://github.com/SukkoPera/N64PadForArduino 9 | architectures=avr 10 | -------------------------------------------------------------------------------- /src/GCPad.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************/ 19 | 20 | #include "GCPad.h" 21 | 22 | /* These must follow the order from ProtoCommand, first byte is expected length 23 | * of reply 24 | */ 25 | const byte GCPad::protoCommands[CMD_NUMBER][COMMAND_SIZE + 1] = { 26 | // CMD_POLL - Buffer size required: 8 bytes 27 | {8, 0x40, 0x03, 0x02}, 28 | 29 | // CMD_RUMBLE_ON - Do we even have a reply? 30 | {1, 0x40, 0x00, 0x01}, 31 | 32 | // CMD_RUMBLE_OFF - Ditto 33 | {1, 0x40, 0x00, 0x00} 34 | }; 35 | 36 | boolean GCPad::begin () { 37 | buttons = 0; 38 | x = 0; 39 | y = 0; 40 | c_x = 0; 41 | c_y = 0; 42 | left_trigger = 0; 43 | right_trigger = 0; 44 | 45 | last_poll = 0; 46 | 47 | // It seems we need nothing special 48 | return true; 49 | } 50 | 51 | boolean GCPad::read () { 52 | boolean ret = true; 53 | 54 | if (last_poll == 0 || millis () - last_poll >= 10) { 55 | if ((ret = (runCommand (CMD_POLL) != NULL))) { 56 | // The mask makes sure unused bits are 0, some seem to be always 1 57 | buttons = ((((uint16_t) buf[0]) << 8) | buf[1]) & ~(0xE080); 58 | x = buf[2]; 59 | y = buf[3]; 60 | c_x = buf[4]; 61 | c_y = buf[5]; 62 | left_trigger = buf[6]; 63 | right_trigger = buf[7]; 64 | 65 | last_poll = millis (); 66 | } 67 | } 68 | 69 | return ret; 70 | } 71 | 72 | byte *GCPad::runCommand (const ProtoCommand cmd) { 73 | byte *ret = NULL; 74 | if (proto.runCommand (protoCommands[cmd] + 1, COMMAND_SIZE, buf, protoCommands[cmd][0])) { 75 | ret = buf; 76 | } 77 | 78 | return ret; 79 | } 80 | -------------------------------------------------------------------------------- /src/GCPad.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * GameCube reference: 21 | * http://www.int03.co.uk/crema/hardware/gamecube/gc-control.html 22 | */ 23 | 24 | #include "protocol/N64PadProtocol.h" 25 | 26 | class GCPad { 27 | public: 28 | enum PadButton { 29 | /* Always 0 = 1 << 15, */ 30 | /* Always 0 = 1 << 14, */ 31 | /* Unknown = 1 << 13, */ 32 | BTN_START = 1 << 12, 33 | BTN_Y = 1 << 11, 34 | BTN_X = 1 << 10, 35 | BTN_B = 1 << 9, 36 | BTN_A = 1 << 8, 37 | /* Always 1 = 1 << 7, */ 38 | BTN_L = 1 << 6, 39 | BTN_R = 1 << 5, 40 | BTN_Z = 1 << 4, 41 | BTN_D_UP = 1 << 3, 42 | BTN_D_DOWN = 1 << 2, 43 | BTN_D_RIGHT = 1 << 1, 44 | BTN_D_LEFT = 1 << 0 45 | }; 46 | 47 | // Button status register. Use PadButton values to test this. 1 means pressed. 48 | uint16_t buttons; 49 | 50 | /* X-Axis coordinate (Growing RIGHT) 51 | * 52 | * Range for analog position is -128 to 127, however, true GameCube 53 | * controller is mechanically limited, so the actual range is about 54 | * 20 to 225. 55 | */ 56 | uint8_t x; 57 | 58 | /* Y-Axis Coordinate (Growing UP) 59 | * 60 | * See the comment about x above 61 | */ 62 | uint8_t y; 63 | 64 | /* C-Stick X-Axis coordinate (Growing RIGHT) 65 | * 66 | * Range for analog position is -128 to 127, however, true GameCube 67 | * controller is mechanically limited, so the actual range is about 68 | * 20 to 225. 69 | */ 70 | uint8_t c_x; 71 | 72 | /* C-Stick Y-Axis Coordinate (Growing UP) 73 | * 74 | * See the comment about c_x above 75 | */ 76 | uint8_t c_y; 77 | 78 | /* Left trigger 79 | * 80 | * Range is 0-255, but full range seems to be hard to reach. The L 81 | * button seems to trigger at ~200. Note that this might not be 0 when 82 | * fully unpressed! 83 | */ 84 | uint8_t left_trigger; 85 | 86 | /* Right trigger 87 | * 88 | * See the comment about left_trigger above 89 | */ 90 | uint8_t right_trigger; 91 | 92 | // This can also be called anytime to reset the controller 93 | boolean begin (); 94 | 95 | /* Reads the current state of the joystick. 96 | * 97 | * Note that this functions disables interrupts and runs for 350+ us! 98 | */ 99 | boolean read (); 100 | 101 | private: 102 | N64PadProtocol proto; 103 | 104 | // Size of a single command in bytes, seems fixed 105 | static const int COMMAND_SIZE = 3; 106 | 107 | enum ProtoCommand { 108 | CMD_POLL = 0, 109 | CMD_RUMBLE_ON = 1, 110 | CMD_RUMBLE_OFF = 2, 111 | CMD_NUMBER // Leave at end 112 | }; 113 | 114 | // First byte is expected reply length 115 | static const byte protoCommands[CMD_NUMBER][COMMAND_SIZE + 1]; 116 | 117 | // 8 should enough for all our uses 118 | byte buf[8]; 119 | 120 | // millis() last time controller was polled 121 | unsigned long last_poll; 122 | 123 | byte *runCommand (const ProtoCommand cmd); 124 | }; 125 | -------------------------------------------------------------------------------- /src/N64Pad.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************/ 19 | 20 | #include "N64Pad.h" 21 | 22 | /* These must follow the order from ProtoCommand, first byte is expected length 23 | * of reply 24 | */ 25 | const byte N64Pad::protoCommands[CMD_NUMBER][1 + 1] = { 26 | // CMD_IDENTIFY - Buffer size required: 3 bytes 27 | {3, 0x00}, 28 | 29 | // CMD_POLL - 4 30 | {4, 0x01}, 31 | 32 | // CMD_READ - ? 33 | {1, 0x02}, 34 | 35 | // CMD_WRITE - ? 36 | {1, 0x03}, 37 | 38 | // CMD_RESET - 3 39 | {3, 0xFF} 40 | }; 41 | 42 | boolean N64Pad::begin () { 43 | proto.begin (); 44 | 45 | buttons = 0; 46 | x = 0; 47 | y = 0; 48 | last_poll = 0; 49 | 50 | // I'm not sure non-Nintendo controllers return 5 51 | if (runCommand (CMD_RESET)) { 52 | last_poll = millis (); 53 | return buf[0] == 5; 54 | } else { 55 | return false; 56 | } 57 | } 58 | 59 | boolean N64Pad::read () { 60 | boolean ret = true; 61 | 62 | if (millis () - last_poll >= MIN_POLL_INTERVAL_MS) { 63 | if ((ret = (runCommand (CMD_POLL) != NULL))) { 64 | buttons = ((((uint16_t) buf[0]) << 8) | buf[1]); 65 | x = (int8_t) buf[2]; 66 | y = (int8_t) buf[3]; 67 | 68 | last_poll = millis (); 69 | } 70 | } 71 | 72 | return ret; 73 | } 74 | 75 | byte *N64Pad::runCommand (const ProtoCommand cmd) { 76 | byte *ret = NULL; 77 | if (proto.runCommand (&(protoCommands[(byte) cmd][1]), 1, buf, protoCommands[(byte) cmd][0])) { 78 | ret = buf; 79 | } 80 | 81 | return ret; 82 | } 83 | -------------------------------------------------------------------------------- /src/N64Pad.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************/ 19 | 20 | #include "protocol/N64PadProtocol.h" 21 | 22 | class N64Pad { 23 | public: 24 | const byte MIN_POLL_INTERVAL_MS = 1000U / 60U; 25 | 26 | enum PadButton { 27 | BTN_A = 1 << 15, 28 | BTN_B = 1 << 14, 29 | BTN_Z = 1 << 13, 30 | BTN_START = 1 << 12, 31 | BTN_UP = 1 << 11, 32 | BTN_DOWN = 1 << 10, 33 | BTN_LEFT = 1 << 9, 34 | BTN_RIGHT = 1 << 8, 35 | BTN_LRSTART = 1 << 7, // This is set when L+R+Start are pressed (and BTN_START is not) 36 | /* Unused = 1 << 6, */ 37 | BTN_L = 1 << 5, 38 | BTN_R = 1 << 4, 39 | BTN_C_UP = 1 << 3, 40 | BTN_C_DOWN = 1 << 2, 41 | BTN_C_LEFT = 1 << 1, 42 | BTN_C_RIGHT = 1 << 0 43 | }; 44 | 45 | // Button status register. Use PadButton values to test this. 1 means pressed. 46 | uint16_t buttons; 47 | 48 | /* X-Axis coordinate (Positive RIGHT) 49 | * 50 | * Range for analog position is -128 to 127, however, true Nintendo 64 51 | * controller range is about 63% of it (mechanically limited), so the actual 52 | * range is about -81 to 81 (less for worn-out controllers). 53 | */ 54 | int8_t x; 55 | 56 | /* Y-Axis Coordinate (Positive UP) 57 | * 58 | * See the comment about x above 59 | */ 60 | int8_t y; 61 | 62 | // This can also be called anytime to reset the controller 63 | boolean begin (); 64 | 65 | /* Reads the current state of the joystick. 66 | * 67 | * Note that this functions disables interrupts and runs for 160+ us! 68 | */ 69 | boolean read (); 70 | 71 | private: 72 | N64PadProtocol proto; 73 | 74 | enum ProtoCommand { 75 | CMD_IDENTIFY = 0, 76 | CMD_POLL, 77 | CMD_READ, 78 | CMD_WRITE, 79 | CMD_RESET, 80 | 81 | CMD_NUMBER // Leave at end 82 | }; 83 | 84 | // First byte is expected reply length, second byte is actual command byte 85 | static const byte protoCommands[CMD_NUMBER][1 + 1]; 86 | 87 | // 4 is enough for all our uses 88 | byte buf[4]; 89 | 90 | // millis() last time controller was polled 91 | unsigned long last_poll; 92 | 93 | byte *runCommand (const ProtoCommand cmd); 94 | }; 95 | -------------------------------------------------------------------------------- /src/protocol/N64PadProtocol.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************* 19 | * 20 | * See the following links for some references on the N64/GC controller 21 | * protocol: 22 | * - http://www.int03.co.uk/crema/hardware/gamecube/gc-control.htm 23 | * - https://www.mixdown.ca/n64dev/ 24 | */ 25 | 26 | /* Here are a few #defines that will disable some "things happening in the 27 | * background" while we are polling the controller. The controller protocol is 28 | * very fast and we need all the speed we can use to catch up with it. 29 | * 30 | * All defaults have been tested with Arduino 1.8.12. 31 | */ 32 | #if defined(__AVR_ATmega328P__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega168__) || defined (__AVR_ATtiny88__) || defined (__AVR_ATtiny48__) 33 | // This can be enabled, but does not seem necessary 34 | //~ #define DISABLE_USART 35 | //~ #define DISABLE_MILLIS 36 | #elif defined ( __AVR_ATmega32U4__) 37 | // These are absolutely necessary for reliable readings 38 | #define DISABLE_USB_INTERRUPTS 39 | #define DISABLE_MILLIS 40 | 41 | // Don't touch this 42 | #include "usbpause.h" 43 | UsbPause usbMagic; 44 | #elif defined (ARDUINO_AVR_DIGISPARK) 45 | // This is necessary on the Digispark, which uses a software USB implementation 46 | #define DISABLE_USB_INTERRUPTS 47 | #endif 48 | 49 | 50 | #include "N64PadProtocol.h" 51 | #include "pinconfig.h" 52 | 53 | /* A read will be considered failed if it hasn't completed within this amount of 54 | * microseconds. The N64/GC protocol takes 4us per bit. The longest command 55 | * reply we support is GC's poll command which returns 8 bytes, so this must be 56 | * at least 8 * 8 * 4 = 256 us plus some margin. Note that this is only used 57 | * when DISABLE_MILLIS is NOT defined, when it is a hw timer is used, which is 58 | * initialized in begin(), so if you change this make sure to tune the value 59 | * there accordingly, too. 60 | */ 61 | #define COMMAND_TIMEOUT 300 62 | 63 | // Delay 62.5ns on a 16MHz AtMega 64 | #define NOP __asm__ __volatile__ ("nop\n\t") 65 | 66 | #define delay025us() do { \ 67 | NOP; NOP; NOP; NOP; \ 68 | } while (0) 69 | 70 | #define delay05us() do { \ 71 | delay025us(); \ 72 | delay025us(); \ 73 | } while (0) 74 | 75 | #define delay1us() do { \ 76 | delay05us(); \ 77 | delay05us(); \ 78 | } while (0) 79 | 80 | #define delay2us() do { \ 81 | delay1us(); \ 82 | delay1us(); \ 83 | } while (0) 84 | 85 | #define delay3us() do { \ 86 | delay1us(); \ 87 | delay2us(); \ 88 | } while (0) 89 | 90 | byte repbuf2[8]; 91 | static volatile byte *curByte = &GPIOR2; 92 | static volatile byte *curBit = &GPIOR1; 93 | 94 | #ifdef DISABLE_MILLIS 95 | static volatile boolean timeout = false; 96 | 97 | ISR (TIMER1_COMPA_vect) { 98 | N64PadProtocol::stopTimer (); 99 | } 100 | #endif 101 | 102 | void N64PadProtocol::begin () { 103 | // Prepare interrupts: INT0 is triggered by pin 2 FALLING 104 | noInterrupts (); 105 | prepareInterrupt (); 106 | interrupts (); 107 | // Do not enable interrupt here! 108 | 109 | #ifdef DISABLE_MILLIS 110 | /* Since we disable the timer interrupt we need some other way to trigger a 111 | * read timeout, let's use timer 1 112 | */ 113 | TCCR1A = 0; 114 | TCCR1B = 0; 115 | TCCR1B |= (1 << WGM12); // Clear Timer on Compare (CTC) 116 | TCCR1B |= (1 << CS10); // Prescaler = 1 117 | OCR1A = 4799; // 16000000/((4799+1)*1) => 3333Hz/300us 118 | #endif 119 | 120 | // Signalling output 121 | //~ DDRC |= (1 << DDC7); 122 | } 123 | 124 | inline void N64PadProtocol::startTimer () { 125 | #ifdef DISABLE_MILLIS 126 | timeout = false; 127 | TCNT1 = 0; // counter = 0 128 | TIFR1 |= (1 << OCF1A); // Clear pending interrupt, if any 129 | TIMSK1 |= (1 << OCIE1A); // Trigger ISR on output Compare Match A 130 | #endif 131 | } 132 | 133 | inline void N64PadProtocol::stopTimer () { 134 | #ifdef DISABLE_MILLIS 135 | timeout = true; 136 | TIMSK1 &= ~(1 << OCIE1A); // Do not retrigger 137 | #endif 138 | } 139 | 140 | inline static void sendLow () { 141 | // Switch pin to output mode, it will be low by default 142 | PAD_DIR |= (1 << PAD_BIT); 143 | } 144 | 145 | inline static void sendHigh () { 146 | // Switch pin to input mode (Hi-Z), pullups will be disabled by default 147 | PAD_DIR &= ~(1 << PAD_BIT); 148 | } 149 | 150 | // To send a 0 bit the data line is pulled low for 3us and let high for 1us 151 | inline static void sendZero () { 152 | sendLow (); 153 | delay3us (); 154 | sendHigh (); 155 | delay1us (); 156 | } 157 | 158 | // To send a 1 the data line is pulled low for 1us and let high for 3us 159 | inline static void sendOne () { 160 | sendLow (); 161 | delay1us (); 162 | sendHigh (); 163 | delay3us (); 164 | } 165 | 166 | // "Console stop bit" is line low for 1us, and high for 2us (3us total). 167 | inline static void sendStop () { 168 | sendLow (); 169 | delay1us (); 170 | sendHigh (); 171 | 172 | /* Now, we would be supposed to delay 2 us here, but we're cutting it a bit 173 | * short since we need to enable interrupts and be sure not to miss the first 174 | * falling edge driven by the controller. 175 | */ 176 | delay1us (); 177 | //~ delay05us (); 178 | } 179 | 180 | // This must be implemented like this, as it cannot be too slow, or the controller won't recognize the signal 181 | inline static void sendCmd (const byte *cmdbuf, const byte cmdsz) { 182 | for (byte j = 0; j < cmdsz; j++) { 183 | byte cmdbyte = cmdbuf[j]; 184 | for (byte i = 0; i < 8; i++) { 185 | // MSB first 186 | if (cmdbyte & 0x80) 187 | sendOne (); 188 | else 189 | sendZero (); 190 | cmdbyte <<= 1; 191 | } 192 | } 193 | sendStop (); 194 | } 195 | 196 | boolean N64PadProtocol::runCommand (const byte *cmdbuf, const byte cmdsz, byte *repbuf, byte repsz) { 197 | for (byte i = 0; i < repsz; i++) 198 | repbuf2[i] = 0; 199 | 200 | // Prepare things for the INT0 ISR 201 | *curBit = 8; 202 | *curByte = 0; 203 | 204 | // Disable "things happening in the background" as needed 205 | #ifdef DISABLE_MILLIS 206 | noInterrupts (); 207 | byte oldTIMSK0 = TIMSK0; 208 | TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A) | (1 << TOIE0)); 209 | TIFR0 |= (1 << OCF0B) | (1 << OCF0A) | (1 << TOV0); 210 | interrupts (); 211 | #else 212 | unsigned long start = micros (); 213 | #endif 214 | 215 | #ifdef DISABLE_USART 216 | byte oldUCSR0B = UCSR0B; 217 | UCSR0B &= ~((1 << UCSZ02) | (1 << RXB80) | (1 << TXB80)); 218 | #endif 219 | 220 | #ifdef DISABLE_USB_INTERRUPTS 221 | #ifdef ARDUINO_AVR_DIGISPARK 222 | /* The Digispark USB implementation is software-based and uses a Pin-Change 223 | * Interrupt 224 | */ 225 | register byte oldGIMSK = GIMSK; 226 | GIMSK &= ~(1 << PCIE); 227 | #else 228 | usbMagic.pause (); 229 | #endif 230 | #endif 231 | 232 | #ifdef DISABLE_MILLIS 233 | // Start timeout timer 234 | startTimer (); 235 | #endif 236 | 237 | // We can send the command now 238 | sendCmd (cmdbuf, cmdsz); 239 | 240 | // Enable interrupt handling - QUICK!!! 241 | enableInterrupt (); 242 | 243 | // OK, just wait for the reply buffer to fill at last 244 | while (*curByte < repsz 245 | #ifndef DISABLE_MILLIS 246 | && micros () - start <= COMMAND_TIMEOUT 247 | #else 248 | && !timeout 249 | #endif 250 | ) 251 | ; 252 | 253 | // Done, ISRs are no longer needed 254 | #ifdef DISABLE_MILLIS 255 | stopTimer (); // Even if it already happened, it won't hurt 256 | TIMSK0 = oldTIMSK0; 257 | #endif 258 | disableInterrupt (); 259 | 260 | // Reenable things happening in background 261 | #ifdef DISABLE_USB_INTERRUPTS 262 | #ifdef ARDUINO_AVR_DIGISPARK 263 | GIMSK = oldGIMSK; 264 | #else 265 | usbMagic.resume (); 266 | #endif 267 | #endif 268 | 269 | #ifdef DISABLE_USART 270 | UCSR0B = oldUCSR0B; 271 | #endif 272 | 273 | 274 | 275 | // FIXME 276 | memcpy (repbuf, repbuf2, *curByte); 277 | 278 | return *curByte == repsz; 279 | } 280 | -------------------------------------------------------------------------------- /src/protocol/N64PadProtocol.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************/ 19 | 20 | #ifndef N64PADPROTOCOL_INCLUDED 21 | #define N64PADPROTOCOL_INCLUDED 22 | 23 | #include 24 | 25 | class N64PadProtocol { 26 | public: 27 | void begin (); 28 | 29 | /* NOTE: This disables interrupts and runs for ~30 us per byte to 30 | * exchange! 31 | */ 32 | boolean runCommand (const byte *cmdbuf, const byte cmdsz, byte *repbuf, byte repsz); 33 | 34 | // Needs to be public as called from ISR 35 | static void stopTimer (); 36 | 37 | private: 38 | static void startTimer (); 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /src/protocol/int0.S: -------------------------------------------------------------------------------- 1 | ; This file is part of N64Pad for Arduino. 2 | ; 3 | ; Copyright (C) 2015-2021 by SukkoPera 4 | ; 5 | ; N64Pad is free software: you can redistribute it and/or modify 6 | ; it under the terms of the GNU General Public License as published by 7 | ; the Free Software Foundation, either version 3 of the License, or 8 | ; (at your option) any later version. 9 | ; 10 | ; N64Pad is distributed in the hope that it will be useful, 11 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ; GNU General Public License for more details. 14 | ; 15 | ; You should have received a copy of the GNU General Public License 16 | ; along with N64Pad. If not, see . 17 | 18 | #include 19 | #include "pinconfig.h" 20 | 21 | #ifdef N64PAD_USE_INTX 22 | 23 | .section .text 24 | 25 | .extern repbuf2 26 | 27 | .global N64PAD_INT_VECTOR 28 | 29 | #define REG_DATA _SFR_IO_ADDR (GPIOR0) 30 | #define REG_CURBIT _SFR_IO_ADDR (GPIOR1) 31 | #define REG_CURBYTE _SFR_IO_ADDR (GPIOR2) 32 | 33 | ; Uno 34 | ;~ #define SIGNAL sbi _SFR_IO_ADDR (PINB), PB5 35 | 36 | ; Leonardo 37 | ;~ #define SIGNAL sbi _SFR_IO_ADDR (PINC), PC7 38 | 39 | ; Disable 40 | #define SIGNAL 41 | 42 | N64PAD_INT_VECTOR: 43 | push r24 44 | in r24, _SFR_IO_ADDR (SREG) 45 | push r24 46 | push ZL 47 | push ZH 48 | 49 | ; OK, so... We got here so fast that the line is not ready yet, so we'd 50 | ; better sit down for a while, LOL :). The number of NOPs might need to be 51 | ; tailored, but 2 to 4 seems the sweet spot for the Uno (I didn't try more 52 | ; though). 2 also seems to be good on the Leonardo, so let's go with that by 53 | ; default 54 | nop 55 | nop 56 | ;~ nop 57 | ;~ nop 58 | 59 | ; Got a one, store it 60 | in r24, REG_DATA 61 | lsl r24 62 | SIGNAL 63 | sbic _SFR_IO_ADDR (PAD_INPORT), PAD_BIT 64 | sbr r24, 1 65 | SIGNAL 66 | out REG_DATA, r24 67 | 68 | ; Next bit 69 | in r24, REG_CURBIT 70 | dec r24 71 | brne done 72 | 73 | ; Current byte is done 74 | ldi ZL, lo8 (repbuf2) 75 | ldi ZH, hi8 (repbuf2) 76 | in r24, REG_CURBYTE 77 | add ZL, r24 78 | clr r24 79 | adc ZH, r24 80 | in r24, REG_DATA 81 | st Z, r24 82 | 83 | ; Prepare for next byte 84 | in r24, REG_CURBYTE ; Byte count += 1 85 | inc r24 86 | out REG_CURBYTE, r24 87 | clr r24 ; Zero buffer 88 | out REG_DATA, r24 89 | ldi r24, 8 ; Bit count = 8 90 | 91 | done: 92 | out REG_CURBIT, r24 93 | 94 | pop ZH 95 | pop ZL 96 | pop r24 97 | out _SFR_IO_ADDR (SREG), r24 98 | pop r24 99 | 100 | reti 101 | 102 | #endif 103 | -------------------------------------------------------------------------------- /src/protocol/pcint.S: -------------------------------------------------------------------------------- 1 | ; This file is part of N64Pad for Arduino. 2 | ; 3 | ; Copyright (C) 2015-2021 by SukkoPera 4 | ; 5 | ; N64Pad is free software: you can redistribute it and/or modify 6 | ; it under the terms of the GNU General Public License as published by 7 | ; the Free Software Foundation, either version 3 of the License, or 8 | ; (at your option) any later version. 9 | ; 10 | ; N64Pad is distributed in the hope that it will be useful, 11 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ; GNU General Public License for more details. 14 | ; 15 | ; You should have received a copy of the GNU General Public License 16 | ; along with N64Pad. If not, see . 17 | 18 | #include 19 | #include "pinconfig.h" 20 | 21 | #ifdef N64PAD_USE_PCINT 22 | 23 | .section .text 24 | 25 | .extern repbuf2 26 | 27 | .global N64PAD_INT_VECTOR 28 | 29 | #define REG_DATA _SFR_IO_ADDR (GPIOR0) 30 | #define REG_CURBIT _SFR_IO_ADDR (GPIOR1) 31 | #define REG_CURBYTE _SFR_IO_ADDR (GPIOR2) 32 | 33 | ; Uno 34 | ;~ #define SIGNAL sbi _SFR_IO_ADDR (PINB), PB5 35 | 36 | ; Leonardo 37 | ;~ #define SIGNAL sbi _SFR_IO_ADDR (PINC), PC7 38 | 39 | ; Disable 40 | #define SIGNAL 41 | 42 | N64PAD_INT_VECTOR: 43 | ; PCINTs are called for both edges, so make sure we're on the right one 44 | sbic _SFR_IO_ADDR (PAD_INPORT), PAD_BIT 45 | reti 46 | 47 | push r24 48 | in r24, _SFR_IO_ADDR (SREG) 49 | push r24 50 | ;~ push ZL ; Don't push, save it GPIOR later to save clocks 51 | push ZH 52 | 53 | in r24, REG_DATA 54 | lsl r24 55 | SIGNAL 56 | sbic _SFR_IO_ADDR (PAD_INPORT), PAD_BIT 57 | sbr r24, 1 ; Got a one, store it 58 | SIGNAL 59 | out REG_DATA, r24 60 | 61 | ; Next bit 62 | in r24, REG_CURBIT 63 | dec r24 64 | brne done 65 | 66 | ; Current byte is done 67 | out REG_CURBIT, ZL 68 | ldi ZL, lo8 (repbuf2) 69 | ldi ZH, hi8 (repbuf2) 70 | in r24, REG_CURBYTE 71 | add ZL, r24 72 | inc r24 ; Byte count += 1 73 | out REG_CURBYTE, r24 74 | clr r24 75 | adc ZH, r24 ; Maybe if we are sure the address doesn't carry, we can skip this? 76 | in r24, REG_DATA 77 | st Z, r24 78 | in ZL, REG_CURBIT 79 | 80 | ; Prepare for next byte 81 | clr r24 ; Zero buffer 82 | out REG_DATA, r24 83 | ldi r24, 8 ; Bit count = 8 84 | 85 | done: 86 | out REG_CURBIT, r24 87 | 88 | pop ZH 89 | ;~ pop ZL 90 | pop r24 91 | out _SFR_IO_ADDR (SREG), r24 92 | pop r24 93 | 94 | reti 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /src/protocol/pinconfig.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of N64Pad for Arduino. * 3 | * * 4 | * Copyright (C) 2015-2021 by SukkoPera * 5 | * * 6 | * N64Pad is free software: you can redistribute it and/or modify * 7 | * it under the terms of the GNU General Public License as published by * 8 | * the Free Software Foundation, either version 3 of the License, or * 9 | * (at your option) any later version. * 10 | * * 11 | * N64Pad is distributed in the hope that it will be useful, * 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 14 | * GNU General Public License for more details. * 15 | * * 16 | * You should have received a copy of the GNU General Public License * 17 | * along with N64Pad. If not, see . * 18 | ******************************************************************************/ 19 | 20 | // NOTE: This file is included both from C and assembly code! 21 | 22 | #if defined (__AVR_ATtiny25__) || defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__) 23 | // Pin 2, PB2, INT0 - Tested OK 24 | #define PAD_DIR DDRB 25 | #define PAD_OUTPORT PORTB 26 | #define PAD_INPORT PINB 27 | #define PAD_BIT PB2 28 | #define N64PAD_USE_INTX 29 | #define N64PAD_INT_VECTOR INT0_vect 30 | #define prepareInterrupt() {MCUCR |= (1 << ISC01); MCUCR &= ~(1 << ISC00);} 31 | #define enableInterrupt() {GIFR |= (1 << INTF0); GIMSK |= (1 << INT0);} 32 | #define disableInterrupt() {GIMSK &= ~(1 << INT0);} 33 | #elif defined(__AVR_ATmega328P__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega168__) || defined (__AVR_ATtiny88__) || defined (__AVR_ATtiny48__) 34 | // Arduino Uno, Nano, Pro Mini 35 | 36 | // Pin 2, INT0 - Tested OK 37 | //~ #define PAD_DIR DDRD 38 | //~ #define PAD_OUTPORT PORTD 39 | //~ #define PAD_INPORT PIND 40 | //~ #define PAD_BIT PD2 41 | //~ #define N64PAD_USE_INTX 42 | //~ #define N64PAD_INT_VECTOR INT0_vect 43 | //~ #define prepareInterrupt() {EICRA |= (1 << ISC01); EICRA &= ~(1 << ISC00);} 44 | //~ #define enableInterrupt() {EIFR |= (1 << INTF0); EIMSK |= (1 << INT0);} 45 | //~ #define disableInterrupt() {EIMSK &= ~(1 << INT0);} 46 | 47 | // Pin 3, INT1 - Tested OK 48 | #define PAD_DIR DDRD 49 | #define PAD_OUTPORT PORTD 50 | #define PAD_INPORT PIND 51 | #define PAD_BIT PD3 52 | #define N64PAD_USE_INTX 53 | #define N64PAD_INT_VECTOR INT1_vect 54 | #define prepareInterrupt() {EICRA |= (1 << ISC11); EICRA &= ~(1 << ISC10);} 55 | #define enableInterrupt() {EIFR |= (1 << INTF1); EIMSK |= (1 << INT1);} 56 | #define disableInterrupt() {EIMSK &= ~(1 << INT1);} 57 | #elif defined (__AVR_ATmega32U4__) 58 | // Arduino Leonardo, Micro 59 | 60 | // Pin 3, PD0, INT0 - Tested OK 61 | #define PAD_DIR DDRD 62 | #define PAD_OUTPORT PORTD 63 | #define PAD_INPORT PIND 64 | #define PAD_BIT PD0 65 | #define N64PAD_USE_INTX 66 | #define N64PAD_INT_VECTOR INT0_vect 67 | #define prepareInterrupt() {EICRA |= (1 << ISC01); EICRA &= ~(1 << ISC00);} 68 | #define enableInterrupt() {EIFR |= (1 << INTF0); EIMSK |= (1 << INT0);} 69 | #define disableInterrupt() {EIMSK &= ~(1 << INT0);} 70 | 71 | // Pin 8, PB4, PCINT4 - Tested OK 72 | //~ #define PAD_DIR DDRB 73 | //~ #define PAD_OUTPORT PORTB 74 | //~ #define PAD_INPORT PINB 75 | //~ #define PAD_BIT PB4 76 | //~ #define N64PAD_USE_PCINT 77 | //~ #define N64PAD_INT_VECTOR PCINT0_vect 78 | //~ #define prepareInterrupt() {PCMSK0 |= (1 << PCINT4);} 79 | //~ #define enableInterrupt() {PCIFR |= (1 << PCIF0); PCICR |= (1 << PCIE0);} 80 | //~ #define disableInterrupt() {PCICR &= ~(1 << PCIE0);} 81 | 82 | #elif defined (__AVR_ATmega2560__) 83 | // Arduino Mega 84 | 85 | // Pin 3, PE5, INT5 - Tested OK 86 | #define PAD_DIR DDRE 87 | #define PAD_OUTPORT PORTE 88 | #define PAD_INPORT PINE 89 | #define PAD_BIT PE5 90 | #define N64PAD_USE_INTX 91 | #define N64PAD_INT_VECTOR INT5_vect 92 | #define prepareInterrupt() {EICRB |= (1 << ISC51); EICRB &= ~(1 << ISC50);} 93 | #define enableInterrupt() {EIFR |= (1 << INTF5); EIMSK |= (1 << INT5);} 94 | #define disableInterrupt() {EIMSK &= ~(1 << INT5);} 95 | #else 96 | // At least for the moment... 97 | #error "This library is not currently supported on this platform" 98 | #endif 99 | -------------------------------------------------------------------------------- /src/protocol/usbpause.h: -------------------------------------------------------------------------------- 1 | /* The code in this file has been derived from the USBPause library 2.0.0 2 | * https://github.com/pololu/usb-pause-arduino 3 | * 4 | * Hence, this file retains its original license, which is as follows: 5 | * 6 | * Copyright (c) 2014 Pololu Corporation. For more information, see 7 | * 8 | * http://www.pololu.com/ 9 | * http://forum.pololu.com/ 10 | * 11 | * Permission is hereby granted, free of charge, to any person 12 | * obtaining a copy of this software and associated documentation 13 | * files (the "Software"), to deal in the Software without 14 | * restriction, including without limitation the rights to use, 15 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | * copies of the Software, and to permit persons to whom the 17 | * Software is furnished to do so, subject to the following 18 | * conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be 21 | * included in all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 25 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 27 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 28 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 30 | * OTHER DEALINGS IN THE SOFTWARE. 31 | */ 32 | 33 | #include 34 | 35 | #ifdef __AVR_ATmega32U4__ 36 | class UsbPause 37 | { 38 | /// The saved value of the UDIEN register. 39 | uint8_t savedUDIEN; 40 | 41 | /// The saved value of the UENUM register. 42 | uint8_t savedUENUM; 43 | 44 | /// The saved value of the UEIENX register for endpoint 0. 45 | uint8_t savedUEIENX0; 46 | 47 | public: 48 | 49 | void pause() 50 | { 51 | // Disable the general USB interrupt. This must be done 52 | // first, because the general USB interrupt might change the 53 | // state of the EP0 interrupt, but not the other way around. 54 | savedUDIEN = UDIEN; 55 | UDIEN = 0; 56 | 57 | // Select endpoint 0. 58 | savedUENUM = UENUM; 59 | UENUM = 0; 60 | 61 | // Disable endpoint 0 interrupts. 62 | savedUEIENX0 = UEIENX; 63 | UEIENX = 0; 64 | } 65 | 66 | void resume() 67 | { 68 | // Restore endpoint 0 interrupts. 69 | UENUM = 0; 70 | UEIENX = savedUEIENX0; 71 | 72 | // Restore endpoint selection. 73 | UENUM = savedUENUM; 74 | 75 | // Restore general device interrupt. 76 | UDIEN = savedUDIEN; 77 | } 78 | }; 79 | #endif 80 | --------------------------------------------------------------------------------