aboutsummaryrefslogtreecommitdiff
path: root/src/AREngine.cpp
blob: 4568473861b99709e8aa0b9f3865113862cff18a (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
/*
    Copyright 2016-2020 Arisotura

    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 "NDS.h"
#include "AREngine.h"


namespace AREngine
{

typedef struct
{
    u32 Code[2 * 64]; // TODO: more sensible size for this? allocate on demand?
    bool Enabled;

} CheatEntry;

// TODO: more sensible size for this? allocate on demand?
CheatEntry CheatCodes[64];
u32 NumCheatCodes;


bool Init()
{
    return true;
}

void DeInit()
{
    //
}

void Reset()
{
    memset(CheatCodes, 0, sizeof(CheatCodes));
    NumCheatCodes = 0;

    // TODO: acquire codes from a sensible source!
    CheatEntry* entry = &CheatCodes[0];
    u32* ptr = &entry->Code[0];
#define TEMP_PUTCODE(a, b) *ptr++ = a; *ptr++ = b;
    // NSMBDS EUR - giant fucking Mario
    TEMP_PUTCODE(0x1209DBD0, 0x0000027C);
    TEMP_PUTCODE(0x2209DBC0, 0x00000003);
    TEMP_PUTCODE(0x94000130, 0xFFFD0000);
    TEMP_PUTCODE(0x1209DBD0, 0x00000000);
    TEMP_PUTCODE(0x2209DBC0, 0x00000003);
    entry->Enabled = true;
    NumCheatCodes++;
}


#define case16(x) \
    case ((x)+0x00): case ((x)+0x01): case ((x)+0x02): case ((x)+0x03): \
    case ((x)+0x04): case ((x)+0x05): case ((x)+0x06): case ((x)+0x07): \
    case ((x)+0x08): case ((x)+0x09): case ((x)+0x0A): case ((x)+0x0B): \
    case ((x)+0x0C): case ((x)+0x0D): case ((x)+0x0E): case ((x)+0x0F)

void RunCheat(CheatEntry* entry)
{
    u32* code = &entry->Code[0];

    u32 cond = 1;
    u32 condstack = 0;

    for (;;)
    {
        u32 a = *code++;
        u32 b = *code++;
        if ((a|b) == 0) break;

        u8 op = a >> 24;

        if ((op < 0xD0 && op != 0xC5) || op > 0xD2)
        {
            if (!cond)
            {
                // TODO: skip parameters for opcode Ex
                continue;
            }
        }

        switch (op)
        {
        case16(0x00): // 32-bit write
            NDS::ARM7Write32(a & 0x0FFFFFFF, b);
            break;

        case16(0x10): // 16-bit write
            NDS::ARM7Write16(a & 0x0FFFFFFF, b & 0xFFFF);
            break;

        case16(0x20): // 8-bit write
            NDS::ARM7Write8(a & 0x0FFFFFFF, b & 0xFF);
            break;

        case16(0x90): // IF b.l = ((~b.h) & u16[a])
            {
                condstack <<= 1;
                condstack |= cond;

                u16 val = NDS::ARM7Read16(a & 0x0FFFFFFF);
                u16 chk = ~(b >> 16);
                chk &= val;

                cond = ((b & 0xFFFF) == chk) ? 1:0;
            }
            break;

        default:
            printf("!! bad AR opcode %08X %08X\n", a, b);
            return;
        }
    }
}

void RunCheats()
{
    // TODO: make it disableable in general

    for (u32 i = 0; i < NumCheatCodes; i++)
    {
        CheatEntry* entry = &CheatCodes[i];
        if (entry->Enabled)
            RunCheat(entry);
    }
}

}