-
-
Notifications
You must be signed in to change notification settings - Fork 562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slot-2 Motion Pak, Guitar Grip emulation #2183
Open
asiekierka
wants to merge
7
commits into
melonDS-emu:master
Choose a base branch
from
asiekierka:feature/motion-pak-emulation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+467
−5
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
664f77b
Add DS Motion Pak emulation
asiekierka f8af7b9
Add retail Motion Pak emulation, Guitar Grip emulation
asiekierka 10c308a
Simplify Motion Pak acceleration conversion formula
asiekierka b6ff439
Fix Motion Pak emulation axes
asiekierka 9eea7a1
Motion Pak: Emulate console laying on a flat table when motion input …
asiekierka 882d11a
Motion Pak: Add comment
asiekierka 9222b40
GBACartMotionPak: Update comment
asiekierka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
/* | ||
Copyright 2016-2024 melonDS team | ||
|
||
This file is part of melonDS. | ||
|
||
melonDS is free software: you can redistribute it and/or modify it under | ||
the terms of the GNU General Public License as published by the Free | ||
Software Foundation, either version 3 of the License, or (at your option) | ||
any later version. | ||
|
||
melonDS is distributed in the hope that it will be useful, but WITHOUT ANY | ||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License along | ||
with melonDS. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
|
||
#include <assert.h> | ||
#include "NDS.h" | ||
#include "GBACart.h" | ||
#include "Platform.h" | ||
#include <algorithm> | ||
#include "math.h" | ||
|
||
namespace melonDS | ||
{ | ||
using Platform::Log; | ||
using Platform::LogLevel; | ||
|
||
namespace GBACart | ||
{ | ||
|
||
CartMotionPakHomebrew::CartMotionPakHomebrew(void* userdata) : | ||
CartCommon(MotionPakHomebrew), | ||
UserData(userdata) | ||
{ | ||
} | ||
|
||
CartMotionPakHomebrew::~CartMotionPakHomebrew() = default; | ||
|
||
void CartMotionPakHomebrew::Reset() | ||
{ | ||
ShiftVal = 0; | ||
} | ||
|
||
void CartMotionPakHomebrew::DoSavestate(Savestate* file) | ||
{ | ||
CartCommon::DoSavestate(file); | ||
file->Var16(&ShiftVal); | ||
} | ||
|
||
u16 CartMotionPakHomebrew::ROMRead(u32 addr) const | ||
{ | ||
// CHECKME: Does this apply to the homebrew cart as well? | ||
return 0xFCFF; | ||
} | ||
|
||
static int AccelerationToMotionPak(float accel) | ||
{ | ||
const float GRAVITY_M_S2 = 9.80665f; | ||
|
||
return std::clamp( | ||
(int) ((accel / (5 * GRAVITY_M_S2) + 0.5) * 4096), | ||
0, 4095 | ||
); | ||
} | ||
|
||
static int AccelerationToMotionPakRetail(float accel) | ||
{ | ||
const float GRAVITY_M_S2 = 9.80665f; | ||
|
||
return std::clamp( | ||
(int) ((accel / (5 * GRAVITY_M_S2) + 0.5) * 256), | ||
0, 254 | ||
); | ||
} | ||
|
||
static int RotationToMotionPak(float rot) | ||
{ | ||
const float DEGREES_PER_RAD = 180 / M_PI; | ||
const float COUNTS_PER_DEG_PER_SEC = 0.825; | ||
const int CENTER = 1680; | ||
|
||
return std::clamp( | ||
(int) ((rot * DEGREES_PER_RAD * COUNTS_PER_DEG_PER_SEC) + CENTER + 0.5), | ||
0, 4095 | ||
); | ||
} | ||
|
||
u8 CartMotionPakHomebrew::SRAMRead(u32 addr) | ||
{ | ||
// CHECKME: SRAM address mask | ||
addr &= 0xFFFF; | ||
|
||
switch (addr) | ||
{ | ||
case 0: | ||
// Read next byte | ||
break; | ||
case 2: | ||
// Read X acceleration | ||
ShiftVal = AccelerationToMotionPak(Platform::Addon_MotionQuery(Platform::MotionAccelerationX, UserData)) << 4; | ||
// CHECKME: First byte returned when reading acceleration/rotation | ||
return 0; | ||
case 4: | ||
// Read Y acceleration | ||
ShiftVal = AccelerationToMotionPak(Platform::Addon_MotionQuery(Platform::MotionAccelerationY, UserData)) << 4; | ||
return 0; | ||
case 6: | ||
// Read Z acceleration | ||
ShiftVal = AccelerationToMotionPak(Platform::Addon_MotionQuery(Platform::MotionAccelerationZ, UserData)) << 4; | ||
return 0; | ||
case 8: | ||
// Read Z rotation | ||
// CHECKME: This is a guess, compare with real hardware | ||
ShiftVal = RotationToMotionPak(-Platform::Addon_MotionQuery(Platform::MotionRotationZ, UserData)) << 4; | ||
return 0; | ||
case 10: | ||
// Identify cart | ||
ShiftVal = 0xF00F; | ||
return 0; | ||
case 12: | ||
case 14: | ||
case 16: | ||
case 18: | ||
// Read/enable analog inputs | ||
// | ||
// These are not connected by defualt and require do-it-yourself cart | ||
// modification, so there is no reason to emulate them. | ||
ShiftVal = 0; | ||
break; | ||
} | ||
|
||
// Read high byte from the emulated shift register | ||
u8 val = ShiftVal >> 8; | ||
ShiftVal <<= 8; | ||
return val; | ||
} | ||
|
||
CartMotionPakRetail::CartMotionPakRetail(void* userdata) : | ||
CartCommon(MotionPakRetail), | ||
UserData(userdata) | ||
{ | ||
} | ||
|
||
CartMotionPakRetail::~CartMotionPakRetail() = default; | ||
|
||
void CartMotionPakRetail::Reset() | ||
{ | ||
Value = 0; | ||
Step = 16; | ||
} | ||
|
||
void CartMotionPakRetail::DoSavestate(Savestate* file) | ||
{ | ||
CartCommon::DoSavestate(file); | ||
file->Var8(&Value); | ||
file->Var8(&Step); | ||
} | ||
|
||
u16 CartMotionPakRetail::ROMRead(u32 addr) const | ||
{ | ||
// A9-A8 is pulled low on a real Motion Pack. | ||
return 0xFCFF; | ||
} | ||
|
||
u8 CartMotionPakRetail::SRAMRead(u32 addr) | ||
{ | ||
switch (Step) | ||
{ | ||
case 0: // Synchronization - read 0xFF | ||
Value = 0xFF; | ||
break; | ||
case 4: // X acceleration | ||
Value = AccelerationToMotionPakRetail(Platform::Addon_MotionQuery(Platform::MotionAccelerationX, UserData)); | ||
break; | ||
case 8: // Y acceleration | ||
Value = AccelerationToMotionPakRetail(Platform::Addon_MotionQuery(Platform::MotionAccelerationY, UserData)); | ||
break; | ||
case 12: // Z acceleration | ||
Value = AccelerationToMotionPakRetail(Platform::Addon_MotionQuery(Platform::MotionAccelerationZ, UserData)); | ||
break; | ||
case 16: // Synchronization - read 0b00 | ||
Step = 0; | ||
return 0; | ||
} | ||
|
||
int shift = 6 - ((Step & 3) * 2); | ||
Step++; | ||
return (Value >> shift) & 0x03; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may have a hard time here as I'm not entirely convinced the homebrew Slot2 motion pack was ever actually sold/made available. The only product I can find with a motion pack is supposedly the Neoflash "R6Gold".
All of my knowledge of the homebrew cart's spec comes from libnds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely was. https://web.archive.org/web/20080509173200/http://www.ndsmotion.com/ has a list of stores and photos of the packaging.
The problem is that these types of products (the DSerial Edge has a similar problem) were predominantly popular with homebrew developers themselves, so not only did they probably sell in highly limited qualities, they're unlikely to pop up on the second-hand market due to personal nostalgia.
There were six products in total which featured Kionix's technology, five of which were for the DS family of consoles; I maintain a list here: https://wiki.asie.pl/doku.php?id=notes:flashcart:ds_motion_card - of those, the GBAccelerometer is the easiest to find (it's still available in stock in 2024), but that's not a DS product; next, there's the Activision pak, and after that probably the R6 Gold?
I tested with an MK6-Motion myself, which happens to feature both the accelerometer and gyroscope chip; since the chips themselves and thus values output should be identical between the Card and a Pak, I deemed the emulation to be accurate enough for running homebrew (which is the important thing for me from a preservation perspective), but I can't verify fine details like the Pak's open bus behaviour or any additional SPI/ROM commands the Card might have.
There's some additional bits of information in a README bundled with the original DS Motion Card/Pak driver source code on said website.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Either way, I don't expect this to be resolved as part of this PR, and the detection mechanism used by all homebrew for the Pak doesn't rely on open bus behaviour.)