aboutsummaryrefslogtreecommitdiff
path: root/CP15.cpp
blob: 4d1fee620d6cfc1196f22baa8e9c4f38765ea705 (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
/*
    Copyright 2016-2017 StapleButter

    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 "ARM.h"
#include "CP15.h"


// derp
namespace NDS
{
extern ARM* ARM9;
}

namespace CP15
{

u32 Control;

u32 DTCMSetting, ITCMSetting;

u8 ITCM[0x8000];
u32 ITCMSize;
u8 DTCM[0x4000];
u32 DTCMBase, DTCMSize;


void Reset()
{
    Control = 0x78; // dunno

    DTCMSetting = 0;
    ITCMSetting = 0;

    memset(ITCM, 0, 0x8000);
    memset(DTCM, 0, 0x4000);

    ITCMSize = 0;
    DTCMBase = 0xFFFFFFFF;
    DTCMSize = 0;
}


void UpdateDTCMSetting()
{
    if (Control & (1<<16))
    {
        DTCMBase = DTCMSetting & 0xFFFFF000;
        DTCMSize = 0x200 << ((DTCMSetting >> 1) & 0x1F);
        printf("DTCM [%08X] enabled at %08X, size %X\n", DTCMSetting, DTCMBase, DTCMSize);
    }
    else
    {
        DTCMBase = 0xFFFFFFFF;
        DTCMSize = 0;
        printf("DTCM disabled\n");
    }
}

void UpdateITCMSetting()
{
    if (Control & (1<<18))
    {
        ITCMSize = 0x200 << ((ITCMSetting >> 1) & 0x1F);
        printf("ITCM [%08X] enabled at %08X, size %X\n", ITCMSetting, 0, ITCMSize);
    }
    else
    {
        ITCMSize = 0;
        printf("ITCM disabled\n");
    }
}


void Write(u32 id, u32 val)
{
    //printf("CP15 write op %03X %08X %08X\n", id, val, NDS::ARM9->R[15]);

    switch (id)
    {
    case 0x100:
        val &= 0x000FF085;
        Control &= ~0x000FF085;
        Control |= val;
        UpdateDTCMSetting();
        UpdateITCMSetting();
        return;


    case 0x704:
    case 0x782:
        NDS::ARM9->Halt(1);
        return;


    case 0x761:
        //printf("inval data cache %08X\n", val);
        return;
    case 0x762:
        //printf("inval data cache SI\n");
        return;

    case 0x7A1:
        //printf("flush data cache %08X\n", val);
        return;
    case 0x7A2:
        //printf("flush data cache SI\n");
        return;


    case 0x910:
        DTCMSetting = val;
        UpdateDTCMSetting();
        return;
    case 0x911:
        ITCMSetting = val;
        UpdateITCMSetting();
        return;
    }

    if ((id&0xF00)!=0x700)
        printf("unknown CP15 write op %03X %08X\n", id, val);
}

u32 Read(u32 id)
{
    //printf("CP15 read op %03X %08X\n", id, NDS::ARM9->R[15]);

    switch (id)
    {
    case 0x000: // CPU ID
    case 0x003:
    case 0x004:
    case 0x005:
    case 0x006:
    case 0x007:
        return 0x41059461;

    case 0x001: // cache type
        return 0x0F0D2112;

    case 0x002: // TCM size
        return (6 << 6) | (5 << 18);


    case 0x100: // control reg
        return Control;


    case 0x910:
        return DTCMSetting;
    case 0x911:
        return ITCMSetting;
    }

    printf("unknown CP15 read op %03X\n", id);
    return 0;
}


// TCM are handled here.
// TODO: later on, handle PU, and maybe caches

bool HandleCodeRead16(u32 addr, u16* val)
{
    if (addr < ITCMSize)
    {
        *val = *(u16*)&ITCM[addr & 0x7FFF];
        return true;
    }

    return false;
}

bool HandleCodeRead32(u32 addr, u32* val)
{
    if (addr < ITCMSize)
    {
        *val = *(u32*)&ITCM[addr & 0x7FFF];
        return true;
    }

    return false;
}


bool HandleDataRead8(u32 addr, u8* val, u32 forceuser)
{
    if (addr < ITCMSize)
    {
        *val = *(u8*)&ITCM[addr & 0x7FFF];
        return true;
    }
    if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
    {
        *val = *(u8*)&DTCM[(addr - DTCMBase) & 0x3FFF];
        return true;
    }

    return false;
}

bool HandleDataRead16(u32 addr, u16* val, u32 forceuser)
{
    if (addr < ITCMSize)
    {
        *val = *(u16*)&ITCM[addr & 0x7FFF];
        return true;
    }
    if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
    {
        *val = *(u16*)&DTCM[(addr - DTCMBase) & 0x3FFF];
        return true;
    }

    return false;
}

bool HandleDataRead32(u32 addr, u32* val, u32 forceuser)
{
    if (addr < ITCMSize)
    {
        *val = *(u32*)&ITCM[addr & 0x7FFF];
        return true;
    }
    if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
    {
        *val = *(u32*)&DTCM[(addr - DTCMBase) & 0x3FFF];
        return true;
    }

    return false;
}

bool HandleDataWrite8(u32 addr, u8 val, u32 forceuser)
{
    if (addr < ITCMSize)
    {
        *(u8*)&ITCM[addr & 0x7FFF] = val;
        return true;
    }
    if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
    {
        *(u8*)&DTCM[(addr - DTCMBase) & 0x3FFF] = val;
        return true;
    }

    return false;
}

bool HandleDataWrite16(u32 addr, u16 val, u32 forceuser)
{
    if (addr < ITCMSize)
    {
        *(u16*)&ITCM[addr & 0x7FFF] = val;
        return true;
    }
    if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
    {
        *(u16*)&DTCM[(addr - DTCMBase) & 0x3FFF] = val;
        return true;
    }

    return false;
}

bool HandleDataWrite32(u32 addr, u32 val, u32 forceuser)
{
    if (addr < ITCMSize)
    {
        *(u32*)&ITCM[addr & 0x7FFF] = val;
        return true;
    }
    if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
    {
        *(u32*)&DTCM[(addr - DTCMBase) & 0x3FFF] = val;
        return true;
    }

    return false;
}

}