-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdma.cpp
194 lines (162 loc) · 6.99 KB
/
dma.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
/*
Copyright 2019-2020 Hydr8gon
This file is part of NooDS.
NooDS 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.
NooDS 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 NooDS. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dma.h"
#include "core.h"
void Dma::transfer()
{
for (int i = 0; i < 4; i++)
{
// Only transfer on active channels
if (!(active & BIT(i)))
continue;
// Deactivate the channel now that it's being processed
active &= ~BIT(i);
int dstAddrCnt = (dmaCnt[i] & 0x00600000) >> 21;
int srcAddrCnt = (dmaCnt[i] & 0x01800000) >> 23;
int mode = (dmaCnt[i] & 0x38000000) >> 27;
int gxFifoCount = 0;
if (dmaCnt[i] & BIT(26)) // Whole word transfer
{
for (unsigned int j = 0; j < wordCounts[i]; j++)
{
// Transfer a word
core->memory.write<uint32_t>(cpu, dstAddrs[i], core->memory.read<uint32_t>(cpu, srcAddrs[i]));
// Adjust the source address
if (srcAddrCnt == 0) // Increment
srcAddrs[i] += 4;
else if (srcAddrCnt == 1) // Decrement
srcAddrs[i] -= 4;
// Adjust the destination address
if (dstAddrCnt == 0 || dstAddrCnt == 3) // Increment
dstAddrs[i] += 4;
else if (dstAddrCnt == 1) // Decrement
dstAddrs[i] -= 4;
// In GXFIFO mode, only send 112 words at a time
if (mode == 7 && ++gxFifoCount == 112)
break;
}
}
else // Half-word transfer
{
for (unsigned int j = 0; j < wordCounts[i]; j++)
{
// Transfer a half-word
core->memory.write<uint16_t>(cpu, dstAddrs[i], core->memory.read<uint16_t>(cpu, srcAddrs[i]));
// Adjust the source address
if (srcAddrCnt == 0) // Increment
srcAddrs[i] += 2;
else if (srcAddrCnt == 1) // Decrement
srcAddrs[i] -= 2;
// Adjust the destination address
if (dstAddrCnt == 0 || dstAddrCnt == 3) // Increment
dstAddrs[i] += 2;
else if (dstAddrCnt == 1) // Decrement
dstAddrs[i] -= 2;
// In GXFIFO mode, only send 112 words at a time
if (mode == 7 && ++gxFifoCount == 112)
break;
}
}
if (mode == 7)
{
// In GXFIFO mode, keep the channel active if the FIFO is still half empty
if (core->gpu3D.readGxStat() & BIT(25))
active |= BIT(i);
// Don't end a GXFIFO transfer if there are still words left
wordCounts[i] -= gxFifoCount;
if (wordCounts[i] > 0)
continue;
}
if ((dmaCnt[i] & BIT(25)) && mode != 0) // Repeat
{
// Reload the internal registers on repeat
wordCounts[i] = dmaCnt[i] & 0x001FFFFF;
if (dstAddrCnt == 3) // Increment and reload
dstAddrs[i] = dmaDad[i];
}
else
{
// End the transfer
dmaCnt[i] &= ~BIT(31);
active &= ~BIT(i);
}
// Trigger an end of transfer IRQ if enabled
if (dmaCnt[i] & BIT(30))
core->interpreter[cpu].sendInterrupt(8 + i);
}
}
void Dma::trigger(int mode, uint8_t channels)
{
// ARM7 DMAs don't use the lowest mode bit, so adjust accordingly
if (cpu == 1) mode <<= 1;
// Activate channels that are set to the triggered mode
for (int i = 0; i < 4; i++)
{
if ((channels & BIT(i)) && (dmaCnt[i] & BIT(31)) && ((dmaCnt[i] & 0x38000000) >> 27) == mode)
active |= BIT(i);
}
}
void Dma::disable(int mode, uint8_t channels)
{
// ARM7 DMAs don't use the lowest mode bit, so adjust accordingly
if (cpu == 1) mode <<= 1;
// Deactivate channels that are set to the disabled mode
// This usually isn't necessary, because most modes transfer once on a trigger event and then deactivate
// However, there are edge cases, such as having two GXFIFO DMAs and the first one fills the FIFO more than half
for (int i = 0; i < 4; i++)
{
if ((channels & BIT(i)) && (dmaCnt[i] & BIT(31)) && ((dmaCnt[i] & 0x38000000) >> 27) == mode)
active &= ~BIT(i);
}
}
void Dma::writeDmaSad(int channel, uint32_t mask, uint32_t value)
{
// Write to one of the DMASAD registers
mask &= ((cpu == 0 || channel != 0) ? 0x0FFFFFFF : 0x07FFFFFF);
dmaSad[channel] = (dmaSad[channel] & ~mask) | (value & mask);
}
void Dma::writeDmaDad(int channel, uint32_t mask, uint32_t value)
{
// Write to one of the DMADAD registers
mask &= ((cpu == 0 || channel == 3) ? 0x0FFFFFFF : 0x07FFFFFF);
dmaDad[channel] = (dmaDad[channel] & ~mask) | (value & mask);
}
void Dma::writeDmaCnt(int channel, uint32_t mask, uint32_t value)
{
uint32_t old = dmaCnt[channel];
// Write to one of the DMACNT registers
mask &= ((cpu == 0) ? 0xFFFFFFFF : (channel == 3 ? 0xF7E0FFFF : 0xF7E03FFF));
dmaCnt[channel] = (dmaCnt[channel] & ~mask) | (value & mask);
// Deactivate the channel if it's disabled or the mode changed
if (!(dmaCnt[channel] & BIT(31)) || (dmaCnt[channel] & 0x38000000) != (old & 0x38000000))
active &= ~BIT(channel);
// In GXFIFO mode, activate the channel immediately if the FIFO is already half empty
// All other modes are only triggered at the moment when the event happens
// For example, if a word from the DS cart is ready before starting a DMA, the DMA will not be triggered
if ((dmaCnt[channel] & BIT(31)) && ((dmaCnt[channel] & 0x38000000) >> 27) == 7 && (core->gpu3D.readGxStat() & BIT(25)))
active |= BIT(channel);
// Don't reload the internal registers unless the enable bit changed from 0 to 1
if ((old & BIT(31)) || !(dmaCnt[channel] & BIT(31)))
return;
// Reload the internal registers
dstAddrs[channel] = dmaDad[channel];
srcAddrs[channel] = dmaSad[channel];
wordCounts[channel] = dmaCnt[channel] & 0x001FFFFF;
// Activate the channel if it's set to immediate mode
// Reloading seems to be the only trigger for this, so an enabled channel changed to immediate will never transfer
// This also means that repeating doesn't work; in this case, the enabled bit is cleared after only one transfer
if (((dmaCnt[channel] & 0x38000000) >> 27) == 0)
active |= BIT(channel);
}