aboutsummaryrefslogtreecommitdiff
path: root/src/DSi_I2C.h
blob: 5dfeebd0c235f3572b7726b1591b933b40a6ce82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
    Copyright 2016-2023 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/.
*/

#ifndef DSI_I2C_H
#define DSI_I2C_H

#include "types.h"
#include "Savestate.h"

namespace melonDS
{
class DSi_I2CHost;
class DSi_Camera;
class DSi;
class DSi_I2CDevice
{
public:
    DSi_I2CDevice(melonDS::DSi& dsi, DSi_I2CHost* host) : DSi(dsi), Host(host) {}
    virtual ~DSi_I2CDevice() {}
    virtual void Reset() = 0;
    virtual void DoSavestate(Savestate* file) = 0;

    virtual void Acquire() = 0;
    virtual u8 Read(bool last) = 0;
    virtual void Write(u8 val, bool last) = 0;

protected:
    melonDS::DSi& DSi;
    DSi_I2CHost* Host;
};

class DSi_BPTWL : public DSi_I2CDevice
{
public:

    enum
    {
        batteryLevel_Critical = 0x0,
        batteryLevel_AlmostEmpty = 0x1,
        batteryLevel_Low = 0x3,
        batteryLevel_Half = 0x7,
        batteryLevel_ThreeQuarters = 0xB,
        batteryLevel_Full = 0xF
    };

    enum
    {
        volumeKey_Up,
        volumeKey_Down,
    };

    enum
    {
        IRQ_PowerButtonReset    = 0x01, // Triggered after releasing the power button quickly
        IRQ_PowerButtonShutdown = 0x02, // Triggered after holding the power button for less than a second
        IRQ_PowerButtonPressed  = 0x08, // Triggered after pressing the power button
        IRQ_BatteryEmpty        = 0x10, //
        IRQ_BatteryLow          = 0x20, // Triggered when the battery level reaches 1
        IRQ_VolumeSwitchPressed = 0x40, // Triggered once when the volume sliders are first pressed and repeatedly when held down
        /*
            Bit 2 (0x04) could be set when holding the power button for more than 5 seconds? (forced power off)
            It is unknown whether it is set as the console powers off immediately.
            Bit 7 (0x80) is unused?
            Both bits are never used by the official ARM7 libraries, but could have some undocumented hardware functionality (?).
        */
        IRQ_ValidMask           = 0x7B,
    };

    DSi_BPTWL(melonDS::DSi& dsi, DSi_I2CHost* host);
    ~DSi_BPTWL() override;
    void Reset() override;
    void DoSavestate(Savestate* file) override;

    u8 GetBootFlag() const;

    bool GetBatteryCharging() const;
    void SetBatteryCharging(bool charging);

    u8 GetBatteryLevel() const;
    void SetBatteryLevel(u8 batteryLevel);

    // 0-31
    u8 GetVolumeLevel() const;
    void SetVolumeLevel(u8 volume);

    // 0-4
    u8 GetBacklightLevel() const;
    void SetBacklightLevel(u8 backlight);

    void DoHardwareReset(bool direct);
    void DoShutdown();

    // Used by hotkeys
    void SetPowerButtonHeld(double time);
    void SetPowerButtonReleased(double time);
    void SetVolumeSwitchHeld(u32 key);
    void SetVolumeSwitchReleased(u32 key);
    s32 ProcessVolumeSwitchInput(double time);

    void DoPowerButtonPress();
    void DoPowerButtonReset();
    void DoPowerButtonShutdown();
    void DoPowerButtonForceShutdown();
    void DoVolumeSwitchPress(u32 key);

    void SetIRQ(u8 irqFlag);

    void Acquire() override;
    u8 Read(bool last) override;
    void Write(u8 val, bool last) override;

private:
    static const double PowerButtonShutdownTime;
    static const double PowerButtonForcedShutdownTime;
    static const double VolumeSwitchRepeatStart;
    static const double VolumeSwitchRepeatRate;

    static const u8 VolumeDownTable[32];
    static const u8 VolumeUpTable[32];

    double PowerButtonTime;
    bool PowerButtonDownFlag;
    bool PowerButtonShutdownFlag;
    double VolumeSwitchTime;
    double VolumeSwitchRepeatTime;
    bool VolumeSwitchDownFlag ;
    u32 VolumeSwitchKeysDown;

    u8 Registers[0x100];
    u32 CurPos;

    bool GetIRQMode() const;

    void ResetButtonState();
    bool CheckVolumeSwitchKeysValid() const;
};


class DSi_I2CHost
{
public:
    DSi_I2CHost(melonDS::DSi& dsi);
    ~DSi_I2CHost();
    void Reset();
    void DoSavestate(Savestate* file);

    DSi_BPTWL* GetBPTWL() { return BPTWL; }
    DSi_Camera* GetOuterCamera() { return Camera0; }
    DSi_Camera* GetInnerCamera() { return Camera1; }

    u8 ReadCnt() { return Cnt; }
    void WriteCnt(u8 val);

    u8 ReadData();
    void WriteData(u8 val);

private:
    melonDS::DSi& DSi;
    u8 Cnt;
    u8 Data;

    DSi_BPTWL* BPTWL;       // 4A / BPTWL IC
    DSi_Camera* Camera0;    // 78 / facing outside
    DSi_Camera* Camera1;    // 7A / selfie cam

    u8 CurDeviceID;
    DSi_I2CDevice* CurDevice;

    void GetCurDevice();
};

}
#endif // DSI_I2C_H