Skip to content
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

Implement BizHawk's touch input interpolation changes in the core #2168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/DSi_SPI_TSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ void DSi_TSC::SetTouchCoords(u16 x, u16 y)
TouchX = x;
TouchY = y;

DeltaX = DeltaY = 0;

u8 oldpress = Bank3Regs[0x0E] & 0x01;

if (y == 0xFFF)
Expand Down Expand Up @@ -121,6 +123,22 @@ void DSi_TSC::SetTouchCoords(u16 x, u16 y)
}
}

void DSi_TSC::MoveTouchCoords(u16 x, u16 y)
{
if (TSCMode == 0x00) return TSC::MoveTouchCoords(x, y);

if (Bank3Regs[0x0E] & 0x01)
{
return SetTouchCoords(x, y);
}

DeltaX = (x << 4) - (TouchX & ~0x8000);
DeltaY = (y << 4) - (TouchY & ~0x8000);

Bank3Regs[0x09] = 0x80;
Bank3Regs[0x0E] &= ~0x01;
}

void DSi_TSC::MicInputFrame(const s16* data, int samples)
{
if (TSCMode == 0x00) return TSC::MicInputFrame(data, samples);
Expand Down Expand Up @@ -162,17 +180,19 @@ void DSi_TSC::Write(u8 val)
{
// X coordinates

if (id & 0x01) Data = TouchX >> 8;
else Data = TouchX & 0xFF;
u16 touchX = TouchX + CreateTouchOffset(DeltaX);
if (id & 0x01) Data = touchX >> 8;
else Data = touchX & 0xFF;

TouchX &= 0x7FFF;
}
else if (id < 0x15)
{
// Y coordinates

if (id & 0x01) Data = TouchY >> 8;
else Data = TouchY & 0xFF;
u16 touchY = TouchY + CreateTouchOffset(DeltaY);
if (id & 0x01) Data = touchY >> 8;
else Data = touchY & 0xFF;

TouchY &= 0x7FFF; // checkme
}
Expand Down
1 change: 1 addition & 0 deletions src/DSi_SPI_TSC.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DSi_TSC : public TSC
void SetMode(u8 mode);

void SetTouchCoords(u16 x, u16 y) override;
void MoveTouchCoords(u16 x, u16 y) override;
void MicInputFrame(const s16* data, int samples) override;

void Write(u8 val) override;
Expand Down
5 changes: 5 additions & 0 deletions src/NDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,11 @@ void NDS::TouchScreen(u16 x, u16 y)
SPI.GetTSC()->SetTouchCoords(x, y);
}

void NDS::MoveTouch(u16 x, u16 y)
{
SPI.GetTSC()->MoveTouchCoords(x, y);
}

void NDS::ReleaseScreen()
{
SPI.GetTSC()->SetTouchCoords(0x000, 0xFFF);
Expand Down
1 change: 1 addition & 0 deletions src/NDS.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ class NDS
bool IsRunning() const noexcept { return Running; }

void TouchScreen(u16 x, u16 y);
void MoveTouch(u16 x, u16 y);
void ReleaseScreen();

void SetKeyMask(u32 mask);
Expand Down
23 changes: 21 additions & 2 deletions src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ void PowerMan::Write(u8 val)
}


s16 TSC::CreateTouchOffset(s16 delta) const
{
// 560190 cycles per frame
s64 cyclepos = (s64)NDS.GetSysClockCycles(2);
return (cyclepos * delta) / 560190;
}

TSC::TSC(melonDS::NDS& nds) : SPIDevice(nds)
{
Expand Down Expand Up @@ -392,6 +398,8 @@ void TSC::SetTouchCoords(u16 x, u16 y)
TouchX = x;
TouchY = y;

DeltaX = DeltaY = 0;

if (y == 0xFFF)
{
// released
Expand All @@ -404,6 +412,17 @@ void TSC::SetTouchCoords(u16 x, u16 y)
NDS.KeyInput &= ~(1 << (16+6));
}

void TSC::MoveTouchCoords(u16 x, u16 y)
{
if (NDS.KeyInput & (1 << (16+6)))
{
return SetTouchCoords(x, y);
}

DeltaX = (x << 4) - TouchX;
DeltaY = (y << 4) - TouchY;
}

void TSC::MicInputFrame(const s16* data, int samples)
{
if (!data)
Expand Down Expand Up @@ -433,8 +452,8 @@ void TSC::Write(u8 val)

switch (ControlByte & 0x70)
{
case 0x10: ConvResult = TouchY; break;
case 0x50: ConvResult = TouchX; break;
case 0x10: ConvResult = TouchY + CreateTouchOffset(DeltaY); break;
case 0x50: ConvResult = TouchX + CreateTouchOffset(DeltaX); break;

case 0x60:
{
Expand Down
4 changes: 4 additions & 0 deletions src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class TSC : public SPIDevice
virtual void DoSavestate(Savestate* file) override;

virtual void SetTouchCoords(u16 x, u16 y);
virtual void MoveTouchCoords(u16 x, u16 y);
virtual void MicInputFrame(const s16* data, int samples);

virtual void Write(u8 val) override;
Expand All @@ -131,9 +132,12 @@ class TSC : public SPIDevice
u16 ConvResult;

u16 TouchX, TouchY;
s16 DeltaX, DeltaY;

s16 MicBuffer[1024];
int MicBufferLen;

s16 CreateTouchOffset(s16 delta) const;
};


Expand Down
Loading