This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdmac.cpp
455 lines (388 loc) · 11.5 KB
/
dmac.cpp
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
#include <dmac.hpp>
#include <Bus.hpp>
#include <EE.hpp>
#include <sif.hpp>
#include <gs/gif.hpp>
#include <cassert>
inline uint32_t get_channel(uint32_t value)
{
switch (value)
{
case 0x80: return 0;
case 0x90: return 1;
case 0xa0: return 2;
case 0xb0: return 3;
case 0xb4: return 4;
case 0xc0: return 5;
case 0xc4: return 6;
case 0xc8: return 7;
case 0xd0: return 8;
case 0xd4: return 9;
default:
printf("[DMAC] Invalid channel id provided 0x%X, aborting...\n", value);
exit(1);
}
}
constexpr const char* REGS[] =
{
"Dn_CHCR",
"Dn_MADR",
"Dn_QWC",
"Dn_TADR",
"Dn_ASR0",
"Dn_ASR1",
"", "",
"Dn_SADR"
};
constexpr const char* GLOBALS[] =
{
"D_CTRL",
"D_STAT",
"D_PCR",
"D_SQWC",
"D_RBSR",
"D_RBOR",
"D_STADT",
};
DMAController::DMAController(Bus* parent, EmotionEngine* cpu) :
bus(parent), cpu(cpu)
{}
uint32_t DMAController::read_channel(uint32_t addr)
{
assert((addr & 0xff) <= 0x80);
uint32_t id = (addr >> 8) & 0xff;
uint32_t channel = get_channel(id);
uint32_t offset = (addr >> 4) & 0xf;
auto ptr = (uint32_t*)&channels[channel] + offset;
printf("[DMAC] Reading 0x%X from %s of channel %d\n", *ptr, REGS[offset], channel);
return *ptr;
}
void DMAController::write_channel(uint32_t addr, uint32_t data)
{
assert((addr & 0xff) <= 0x80);
uint32_t id = (addr >> 8) & 0xff;
uint32_t channel = get_channel(id);
uint32_t offset = (addr >> 4) & 0xf;
auto ptr = (uint32_t*)&channels[channel] + offset;
printf("[DMAC] Writing 0x%X to %s of channel %d\n", data, REGS[offset], channel);
/* The lower bits of MADR must be zero: */
/* NOTE: This is actually required since the BIOS writes
unaligned addresses to the GIF channel for some reason
and expects it to be read correctly... */
if (offset == 1)
data &= 0x01fffff0;
*ptr = data;
if (channels[channel].control.running)
{
printf("\n[DMAC] Transfer for channel %d started (%d)!\n\n", channel, channels[channel].qword_count);
}
}
uint32_t DMAController::read_global(uint32_t addr)
{
assert(addr <= 0x1000E060);
uint32_t offset = (addr >> 4) & 0xf;
auto ptr = (uint32_t*)&globals + offset;
printf("[DMAC] Reading 0x%X from %s\n", *ptr, GLOBALS[offset]);
return *ptr;
}
uint32_t DMAController::read_enabler(uint32_t addr)
{
assert(addr == 0x1000F520);
printf("[DMAC] Reading D_ENABLER = 0x%X\n", globals.d_enable);
return globals.d_enable;
}
void DMAController::write_global(uint32_t addr, uint32_t data)
{
assert(addr <= 0x1000E060);
uint32_t offset = (addr >> 4) & 0xf;
auto ptr = (uint32_t*)&globals + offset;
printf("[DMAC] Writing 0x%X to %s\n", data, GLOBALS[offset]);
if (offset == 1) /* D_STAT */
{
auto& cop0 = cpu->cop0;
/* The lower bits are cleared while the upper ones are reversed */
globals.d_stat.clear &= ~(data & 0xffff);
globals.d_stat.reverse ^= (data >> 16);
/* IMPORTANT: Update COP0 INT1 status when D_STAT is written */
cop0.cause.ip1_pending = globals.d_stat.channel_irq & globals.d_stat.channel_irq_mask;
}
else
{
*ptr = data;
}
}
void DMAController::write_enabler(uint32_t addr, uint32_t data)
{
assert(addr == 0x1000F590);
printf("[DMAC] Writing D_ENABLEW = 0x%X\n", data);
globals.d_enable = data;
}
void DMAController::tick(uint32_t cycles)
{
if (globals.d_enable & 0x10000)
return;
for (int cycle = cycles; cycle > 0; cycle--)
{
/* Check each channel */
for (uint32_t id = 0; id < 10; id++)
{
auto& channel = channels[id];
if (channel.control.running)
{
/* Transfer any pending qwords */
if (channel.qword_count > 0)
{
/* This is channel specific */
switch (id)
{
case DMAChannels::VIF0:
case DMAChannels::VIF1:
{
auto& vif = bus->vif[id];
uint128_t qword = *(uint128_t*)&bus->eeRam[channel.address];
if (vif->write_fifo(0, qword))
{
uint64_t upper = qword >> 64, lower = qword;
printf("[DMAC] Writing 0x%lX%016lX to VIF%d\n", upper, lower, id);
channel.address += 16;
channel.qword_count--;
if (!channel.qword_count && !channel.control.mode)
channel.end_transfer = true;
}
break;
}
case DMAChannels::GIFC:
{
auto& gif = bus->gif;
uint128_t qword = *(uint128_t*)&bus->eeRam[channel.address];
/* Send data to the PATH3 port of the GIF */
if (gif->write_path3(0, qword))
{
uint64_t upper = qword >> 64, lower = qword;
printf("[DMAC][GIF] Writing 0x%lX%016lX to PATH3\n", upper, lower);
channel.address += 16;
channel.qword_count--;
if (!channel.qword_count && !channel.control.mode)
channel.end_transfer = true;
}
break;
}
case DMAChannels::SIF0:
{
/* SIF0 receives data from the SIF0 fifo */
auto& sif = bus->sif;
if (sif->sif0_fifo.size() >= 4)
{
uint32_t data[4];
for (int i = 0; i < 4; i++)
{
data[i] = sif->sif0_fifo.front();
sif->sif0_fifo.pop();
}
uint128_t qword = *(uint128_t*)data;
uint64_t upper = qword >> 64, lower = qword;
printf("[DMAC][SIF0] Receiving packet from SIF0 FIFO: 0x%lX%016lX\n", upper, lower);
/* Write the packet to the specified address */
bus->Write128(channel.address, *(Register*)&qword);
/* MADR/TADR update while a transfer is ongoing */
channel.qword_count--;
channel.address += 16;
}
break;
}
case DMAChannels::SIF1:
{
/* SIF1 pushes data to the SIF1 fifo */
auto& sif = bus->sif;
uint128_t qword = *(uint128_t*)&bus->eeRam[channel.address];
uint32_t* data = (uint32_t*)&qword;
for (int i = 0; i < 4; i++)
{
sif->sif1_fifo.push(data[i]);
}
uint64_t upper = qword >> 64, lower = qword;
printf("[DMAC][SIF1] Transfering to SIF1 FIFO: 0x%lX%016lX\n", upper, lower);
/* MADR/TADR update while a transfer is ongoing */
channel.qword_count--;
channel.address += 16;
break;
}
default:
printf("[DMAC] Unknown channel transfer with id %d\n", id);
}
} /* If the transfer ended, disable channel */
else if (channel.end_transfer)
{
printf("[DMAC] End transfer of channel %d\n", id);
/* End the transfer */
channel.end_transfer = false;
channel.control.running = 0;
/* Set the channel bit in the interrupt field of D_STAT */
globals.d_stat.channel_irq |= (1 << id);
/* Check for interrupts */
if (globals.d_stat.channel_irq & globals.d_stat.channel_irq_mask)
{
printf("\n[DMAC] INT1!\n\n");
cpu->cop0.cause.ip1_pending = 1;
}
}
else /* Read the next DMAtag */
{
fetch_tag(id);
}
}
}
}
}
void DMAController::fetch_tag(uint32_t id)
{
DMATag tag;
auto& channel = channels[id];
switch (id)
{
case DMAChannels::GIFC:
{
assert(!channel.tag_address.mem_select);
auto& gif = bus->gif;
auto address = channel.tag_address.address;
tag.value = *(uint128_t*)&bus->eeRam[address];
printf("[DMAC] Read GIF DMA tag 0x%lX\n", (uint64_t)tag.value);
/* Update channel from tag */
channel.qword_count = tag.qwords;
channel.control.tag = (tag.value >> 16) & 0xffff;
uint16_t tag_id = tag.id;
switch (tag_id)
{
case DMASourceID::REFE:
channel.address = tag.address;
channel.tag_address.value += 16;
channel.end_transfer = true;
break;
case DMASourceID::CNT:
channel.address = channel.tag_address.address + 16;
channel.tag_address.value = channel.address + channel.qword_count * 16;
break;
case DMASourceID::END:
channel.address = channel.tag_address.address + 16;
channel.end_transfer = true;
break;
default:
printf("\n[DMAC] Unrecognized GIF DMAtag id %d\n", tag_id);
}
/* Just end transfer, since an interrupt will be raised there anyways */
if (channel.control.enable_irq_bit && tag.irq)
channel.end_transfer = true;
break;
}
case DMAChannels::VIF0:
case DMAChannels::VIF1:
{
assert(!channel.tag_address.mem_select);
auto& vif = bus->vif[id];
auto address = channel.tag_address.address;
tag.value = *(uint128_t*)&bus->eeRam[address];
printf("[DMAC] Read VIF%d DMA tag 0x%lX\n", id, (uint64_t)tag.value);
/* Transfer the tag before any data */
if (channel.control.transfer_tag)
{
if (!vif->write_fifo<uint64_t>(0, tag.data))
return;
}
/* Update channel from tag */
channel.qword_count = tag.qwords;
channel.control.tag = (tag.value >> 16) & 0xffff;
uint16_t tag_id = tag.id;
switch (tag_id)
{
case DMASourceID::REFE:
channel.address = tag.address;
channel.tag_address.value += 16;
channel.end_transfer = true;
break;
case DMASourceID::CNT:
channel.address = channel.tag_address.address + 16;
channel.tag_address.value = channel.address + channel.qword_count * 16;
break;
case DMASourceID::NEXT:
channel.address = channel.tag_address.address + 16;
channel.tag_address.address = tag.address;
break;
default:
printf("\n[DMAC] Unrecognized VIF%d DMAtag id %d\n", id, tag_id);
}
/* Just end transfer, since an interrupt will be raised there anyways */
if (channel.control.enable_irq_bit && tag.irq)
channel.end_transfer = true;
break;
}
case DMAChannels::SIF0:
{
auto& sif = bus->sif;
if (sif->sif0_fifo.size() >= 2)
{
uint32_t data[2] = {};
for (int i = 0; i < 2; i++)
{
data[i] = sif->sif0_fifo.front();
sif->sif0_fifo.pop();
}
tag.value = *(uint64_t*)data;
printf("[DMAC] Read SIF0 DMA tag 0x%lX\n", (uint64_t)tag.value);
/* Update channel from tag */
channel.qword_count = tag.qwords;
channel.control.tag = (tag.value >> 16) & 0xffff;
channel.address = tag.address;
channel.tag_address.address += 16;
printf("[DMAC] QWC: %d\nADDR: 0x%X\n", channel.qword_count, channel.address);
/* Just end transfer, since an interrupt will be raised there anyways */
if (channel.control.enable_irq_bit && tag.irq)
channel.end_transfer = true;
}
break;
}
case DMAChannels::SIF1:
{
assert(channel.control.mode == 1);
assert(!channel.tag_address.mem_select);
auto address = channel.tag_address.address;
/* Get tag from memory */
/* TODO: Access to other memory types */
tag.value = *(uint128_t*)&bus->eeRam[address];
printf("[DMAC] Read SIF1 DMA tag 0x%lX\n", (uint64_t)tag.value);
/* Update channel from tag */
channel.qword_count = tag.qwords;
channel.control.tag = (tag.value >> 16) & 0xffff;
uint16_t tag_id = tag.id;
switch (tag_id)
{
case DMASourceID::REFE:
{
channel.address = tag.address;
channel.tag_address.value += 16;
channel.end_transfer = true;
break;
}
case DMASourceID::NEXT:
{
channel.address = channel.tag_address.address + 16;
channel.tag_address.value = tag.address;
break;
}
case DMASourceID::REF:
{
channel.address = tag.address;
channel.tag_address.value += 16;
break;
}
default:
printf("\n[DMAC] Unrecognized SIF1 DMAtag id %d\n", tag_id);
}
/* Just end transfer, since an interrupt will be raised there anyways */
if (channel.control.enable_irq_bit && tag.irq)
channel.end_transfer = true;
break;
}
default:
printf("[DMAC] Unknown channel %d\n", id);
}
}