aboutsummaryrefslogtreecommitdiff
path: root/src/DSi_I2C.cpp
blob: 28f98dc8917e69469c3b6d5b48c433b935fdbd68 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
    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/.
*/

#include <stdio.h>
#include <string.h>
#include <math.h>
#include "DSi.h"
#include "DSi_I2C.h"
#include "DSi_Camera.h"
#include "ARM.h"
#include "SPI.h"
#include "Platform.h"

namespace melonDS
{
using Platform::Log;
using Platform::LogLevel;


// TODO: These are purely approximations
const double DSi_BPTWL::PowerButtonShutdownTime = 0.5;
const double DSi_BPTWL::PowerButtonForcedShutdownTime = 5.0;
const double DSi_BPTWL::VolumeSwitchRepeatStart = 0.5;
const double DSi_BPTWL::VolumeSwitchRepeatRate = 1.0 / 6;

// Could not find a pattern or a decent formula for these,
// regardless, they're only 64 bytes in size
const u8 DSi_BPTWL::VolumeDownTable[32] =
{
    0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x03,
    0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x09, 0x0A,
    0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
    0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
};

const u8 DSi_BPTWL::VolumeUpTable[32] =
{
    0x02, 0x03, 0x06, 0x07, 0x08, 0x0A, 0x0B, 0x0C,
    0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14,
    0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
    0x1D, 0x1E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F,
};


DSi_BPTWL::DSi_BPTWL(melonDS::DSi& dsi, DSi_I2CHost* host) : DSi_I2CDevice(dsi, host)
{
}

DSi_BPTWL::~DSi_BPTWL()
{
}

void DSi_BPTWL::Reset()
{
    CurPos = -1;
    memset(Registers, 0x5A, 0x100);

    Registers[0x00] = 0x33; // TODO: support others??
    Registers[0x01] = 0x00;
    Registers[0x02] = 0x50;
    Registers[0x10] = 0x00; // irq flag
    Registers[0x11] = 0x00; // reset
    Registers[0x12] = 0x00; // irq mode
    Registers[0x20] = 0x8F; // battery
    Registers[0x21] = 0x07;
    Registers[0x30] = 0x13;
    Registers[0x31] = 0x00; // camera power
    Registers[0x40] = 0x1F; // volume
    Registers[0x41] = 0x04; // backlight
    Registers[0x60] = 0x00;
    Registers[0x61] = 0x01;
    Registers[0x62] = 0x50;
    Registers[0x63] = 0x00;
    Registers[0x70] = 0x00; // boot flag
    Registers[0x71] = 0x00;
    Registers[0x72] = 0x00;
    Registers[0x73] = 0x00;
    Registers[0x74] = 0x00;
    Registers[0x75] = 0x00;
    Registers[0x76] = 0x00;
    Registers[0x77] = 0x00;
    Registers[0x80] = 0x10;
    Registers[0x81] = 0x64;

    // Ideally these should be replaced by a proper BPTWL core emulator
    PowerButtonTime = 0.0;
    PowerButtonDownFlag = false;
    PowerButtonShutdownFlag = false;
    VolumeSwitchTime = 0.0;
    VolumeSwitchRepeatTime = 0.0;
    VolumeSwitchKeysDown = 0;
    VolumeSwitchDownFlag = false;

}

void DSi_BPTWL::DoSavestate(Savestate* file)
{
    file->Section("I2BP");

    file->VarArray(Registers, 0x100);
    file->Var32(&CurPos);
}

// TODO: Needs more investigation on the other bits
inline bool DSi_BPTWL::GetIRQMode() const
{
    return Registers[0x12] & 0x01;
}

u8 DSi_BPTWL::GetBootFlag() const { return Registers[0x70]; }

bool DSi_BPTWL::GetBatteryCharging() const { return Registers[0x20] >> 7; }
void DSi_BPTWL::SetBatteryCharging(bool charging)
{
    Registers[0x20] = (((charging ? 0x8 : 0x0) << 4) | (Registers[0x20] & 0x0F));
}

u8 DSi_BPTWL::GetBatteryLevel() const { return Registers[0x20] & 0xF; }
void DSi_BPTWL::SetBatteryLevel(u8 batteryLevel)
{
    Registers[0x20] = ((Registers[0x20] & 0xF0) | (batteryLevel & 0x0F));
    //SPI_Powerman::SetBatteryLevelOkay(batteryLevel > batteryLevel_Low ? true : false);

    if (batteryLevel <= 1)
    {
        SetIRQ(batteryLevel ? IRQ_BatteryLow : IRQ_BatteryEmpty);
    }

}

u8 DSi_BPTWL::GetVolumeLevel() const { return Registers[0x40]; }
void DSi_BPTWL::SetVolumeLevel(u8 volume)
{
    Registers[0x40] = volume & 0x1F;
}

u8 DSi_BPTWL::GetBacklightLevel() const { return Registers[0x41]; }
void DSi_BPTWL::SetBacklightLevel(u8 backlight)
{
    Registers[0x41] = backlight > 4 ? 4 : backlight;
}


void DSi_BPTWL::ResetButtonState()
{
    PowerButtonTime = 0.0;
    PowerButtonDownFlag = false;
    PowerButtonShutdownFlag = false;

    VolumeSwitchKeysDown = 0;
    VolumeSwitchDownFlag = false;
    VolumeSwitchTime = 0.0;
    VolumeSwitchRepeatTime = 0.0;
}

void DSi_BPTWL::DoHardwareReset(bool direct)
{
    ResetButtonState();

    Log(LogLevel::Debug, "BPTWL: soft-reset\n");

    if (direct)
    {
        // TODO: This doesn't seem to stop the SPU
        DSi.SoftReset();
        return;
    }

    // TODO: soft-reset might need to be scheduled later!
    // TODO: this has been moved for the JIT to work, nothing is confirmed here
    DSi.ARM7.Halt(4);
}

void DSi_BPTWL::DoShutdown()
{
    ResetButtonState();
    DSi.Stop(Platform::StopReason::PowerOff);
}


void DSi_BPTWL::SetPowerButtonHeld(double time)
{
    if (!PowerButtonDownFlag)
    {
        PowerButtonDownFlag = true;
        PowerButtonTime = time;
        DoPowerButtonPress();
        return;
    }

    double elapsed = time - PowerButtonTime;
    if (elapsed < 0)
        return;

    if (elapsed >= PowerButtonForcedShutdownTime)
    {
        Log(LogLevel::Debug, "Force power off via DSi power button\n");
        DoPowerButtonForceShutdown();
        return;
    }

    if (elapsed >= PowerButtonShutdownTime)
    {
        DoPowerButtonShutdown();
    }
}

void DSi_BPTWL::SetPowerButtonReleased(double time)
{
    double elapsed = time - PowerButtonTime;
    if (elapsed >= 0 && elapsed < PowerButtonShutdownTime)
    {
        DoPowerButtonReset();
    }

    PowerButtonTime = 0.0;
    PowerButtonDownFlag = false;
    PowerButtonShutdownFlag = false;
}

void DSi_BPTWL::SetVolumeSwitchHeld(u32 key)
{
    VolumeSwitchKeysDown |= (1 << key);
}

void DSi_BPTWL::SetVolumeSwitchReleased(u32 key)
{
    VolumeSwitchKeysDown &= ~(1 << key);
    VolumeSwitchDownFlag = false;
    VolumeSwitchTime = 0.0;
    VolumeSwitchRepeatTime = 0.0;
}

inline bool DSi_BPTWL::CheckVolumeSwitchKeysValid() const
{
    bool up = VolumeSwitchKeysDown & (1 << volumeKey_Up);
    bool down = VolumeSwitchKeysDown & (1 << volumeKey_Down);

    return up != down;
}

s32 DSi_BPTWL::ProcessVolumeSwitchInput(double time)
{
    if (!CheckVolumeSwitchKeysValid())
        return -1;

    s32 key = VolumeSwitchKeysDown & (1 << volumeKey_Up) ? volumeKey_Up : volumeKey_Down;

    // Always fire an IRQ when first pressed
    if (!VolumeSwitchDownFlag)
    {
        VolumeSwitchDownFlag = true;
        VolumeSwitchTime = time;
        DoVolumeSwitchPress(key);
        return key;
    }

    // Handle key repetition mechanic
    if (VolumeSwitchRepeatTime == 0)
    {
        double elapsed = time - VolumeSwitchTime;
        if (elapsed < VolumeSwitchRepeatStart)
            return -1;

        VolumeSwitchRepeatTime = time;
        DoVolumeSwitchPress(key);
        return key;
    }

    double elapsed = time - VolumeSwitchRepeatTime;
    if (elapsed < VolumeSwitchRepeatRate)
        return -1;

    double rem = fmod(elapsed, VolumeSwitchRepeatRate);
    VolumeSwitchRepeatTime = time - rem;
    DoVolumeSwitchPress(key);
    return key;
}


void DSi_BPTWL::DoPowerButtonPress()
{
    // Set button pressed IRQ
    SetIRQ(IRQ_PowerButtonPressed);

    // There is no default hardware behavior for pressing the power button
}

void DSi_BPTWL::DoPowerButtonReset()
{
    // Reset via IRQ, handled by software
    SetIRQ(IRQ_PowerButtonReset);

    // Reset automatically via hardware
    if (!GetIRQMode())
    {
        // Assumes this isn't called during normal CPU execution
        DoHardwareReset(true);
    }
}

void DSi_BPTWL::DoPowerButtonShutdown()
{
    // Shutdown via IRQ, handled by software
    if (!PowerButtonShutdownFlag)
    {
        SetIRQ(IRQ_PowerButtonShutdown);
    }

    PowerButtonShutdownFlag = true;

    // Shutdown automatically via hardware
    if (!GetIRQMode())
    {
        DoShutdown();
    }

    // The IRQ is only fired once (hence the need for an if guard),
    // but the hardware shutdown is continuously triggered.
    // That way when switching the IRQ mode while holding
    // down the power button, the DSi will still shut down
}

void DSi_BPTWL::DoPowerButtonForceShutdown()
{
    DoShutdown();
}

void DSi_BPTWL::DoVolumeSwitchPress(u32 key)
{
    u8 volume = Registers[0x40];

    switch (key)
    {

    case volumeKey_Up:
        volume = VolumeUpTable[volume];
        break;

    case volumeKey_Down:
        volume = VolumeDownTable[volume];
        break;

    }

    Registers[0x40] = volume;

    SetIRQ(IRQ_VolumeSwitchPressed);
}

void DSi_BPTWL::SetIRQ(u8 irqFlag)
{
    Registers[0x10] |= irqFlag & IRQ_ValidMask;

    if (GetIRQMode())
    {
        DSi.SetIRQ2(IRQ2_DSi_BPTWL);
    }
}

void DSi_BPTWL::Acquire()
{
    //printf("BPTWL: start\n");
}

u8 DSi_BPTWL::Read(bool last)
{
    //printf("BPTWL: read %02X -> %02X @ %08X\n", CurPos, Registers[CurPos], NDS::GetPC(1));
    u8 ret = Registers[CurPos];

    // IRQ flags are automatically cleared upon read
    if (CurPos == 0x10)
    {
        Registers[0x10] = 0;
    }

    CurPos++;

    if (last)
    {
        CurPos = -1;
    }

    return ret;
}

void DSi_BPTWL::Write(u8 val, bool last)
{
    if (last)
    {
        CurPos = -1;
        return;
    }

    if (CurPos == 0xFFFFFFFF)
    {
        CurPos = val;
        //printf("BPTWL: reg=%02X\n", val);
        return;
    }

    if (CurPos == 0x11 && val == 0x01)
    {
        // Assumes this is called during normal CPU execution
        DoHardwareReset(false);
        val = 0; // checkme
        CurPos = -1;
        return;
    }

    // Mask volume level
    if (CurPos == 0x40)
    {
        val &= 0x1F;
    }

    // Clamp backlight level
    if (CurPos == 0x41)
    {
        val = val > 4 ? 4 : val;
    }

    if (CurPos == 0x11 || CurPos == 0x12 ||
        CurPos == 0x21 ||
        CurPos == 0x30 || CurPos == 0x31 ||
        CurPos == 0x40 || CurPos == 0x41 ||
        CurPos == 0x60 || CurPos == 0x63 ||
        (CurPos >= 0x70 && CurPos <= 0x77) ||
        CurPos == 0x80 || CurPos == 0x81)
    {
        Registers[CurPos] = val;
    }

    //printf("BPTWL: write %02X -> %02X\n", CurPos, val);
    CurPos++; // CHECKME
}


DSi_I2CHost::DSi_I2CHost(melonDS::DSi& dsi) : DSi(dsi)
{
    BPTWL = new DSi_BPTWL(dsi, this);
    Camera0 = new DSi_Camera(dsi, this, 0);
    Camera1 = new DSi_Camera(dsi, this, 1);
}

DSi_I2CHost::~DSi_I2CHost()
{
    delete BPTWL; BPTWL = nullptr;
    delete Camera0; Camera0 = nullptr;
    delete Camera1; Camera1 = nullptr;
}

void DSi_I2CHost::Reset()
{
    Cnt = 0;
    Data = 0;

    CurDeviceID = 0;
    CurDevice = nullptr;

    BPTWL->Reset();
    Camera0->Reset();
    Camera1->Reset();
}

void DSi_I2CHost::DoSavestate(Savestate* file)
{
    file->Section("I2Ci");

    file->Var8(&Cnt);
    file->Var8(&Data);
    file->Var8(&CurDeviceID);

    if (!file->Saving)
    {
        GetCurDevice();
    }

    BPTWL->DoSavestate(file);
    Camera0->DoSavestate(file);
    Camera1->DoSavestate(file);
}

void DSi_I2CHost::GetCurDevice()
{
    switch (CurDeviceID)
    {
    case 0x4A: CurDevice = BPTWL; break;
    case 0x78: CurDevice = Camera0; break;
    case 0x7A: CurDevice = Camera1; break;
    case 0xA0:
    case 0xE0: CurDevice = nullptr; break;
    default:
        Log(LogLevel::Warn, "I2C: unknown device %02X\n", CurDeviceID);
        CurDevice = nullptr;
        break;
    }
}

void DSi_I2CHost::WriteCnt(u8 val)
{
    //printf("I2C: write CNT %02X, %02X, %08X\n", val, Data, NDS::GetPC(1));

    // TODO: check ACK flag
    // TODO: transfer delay
    // TODO: IRQ
    // TODO: check read/write direction

    if (val & (1<<7))
    {
        bool islast = val & (1<<0);

        if (val & (1<<5))
        {
            // read
            val &= 0xF7;

            if (CurDevice)
            {
                Data = CurDevice->Read(islast);
            }
            else
            {
                Data = 0xFF;
            }

            //printf("I2C read, device=%02X, cnt=%02X, data=%02X, last=%d\n", Device, val, Data, islast);
        }
        else
        {
            // write
            val &= 0xE7;
            bool ack = true;

            if (val & (1<<1))
            {
                CurDeviceID = Data & 0xFE;
                //printf("I2C: %s start, device=%02X\n", (Data&0x01)?"read":"write", Device);

                GetCurDevice();
                if (CurDevice)
                {
                    CurDevice->Acquire();
                }
                else
                {
                    ack = false;
                }
            }
            else
            {
                //printf("I2C write, device=%02X, cnt=%02X, data=%02X, last=%d\n", Device, val, Data, islast);

                if (CurDevice)
                {
                    CurDevice->Write(Data, islast);
                }
                else
                {
                    ack = false;
                }
            }

            if (ack) val |= (1<<4);
        }

        val &= 0x7F;
    }

    Cnt = val;
}

u8 DSi_I2CHost::ReadData()
{
    return Data;
}

void DSi_I2CHost::WriteData(u8 val)
{
    Data = val;
}

}