-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipc.cpp
145 lines (122 loc) · 4.72 KB
/
ipc.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
/*
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 "ipc.h"
#include "core.h"
void Ipc::writeIpcSync(bool cpu, uint16_t mask, uint16_t value)
{
// Write to one of the IPCSYNC registers
mask &= 0x4F00;
ipcSync[cpu] = (ipcSync[cpu] & ~mask) | (value & mask);
// Copy the input bits from this CPU to the output bits for the other CPU
ipcSync[!cpu] = (ipcSync[!cpu] & ~((mask >> 8) & 0x000F)) | (((value & mask) >> 8) & 0x000F);
// Trigger a remote IRQ if enabled on both sides
if ((value & BIT(13)) && (ipcSync[!cpu] & BIT(14)))
core->interpreter[!cpu].sendInterrupt(16);
}
void Ipc::writeIpcFifoCnt(bool cpu, uint16_t mask, uint16_t value)
{
// Clear the FIFO if the clear bit is set
if ((value & BIT(3)) && !fifos[cpu].empty())
{
// Empty the FIFO
while (!fifos[cpu].empty())
fifos[cpu].pop();
ipcFifoRecv[!cpu] = 0;
// Set the FIFO empty bits and clear the FIFO full bits
ipcFifoCnt[cpu] |= BIT(0);
ipcFifoCnt[cpu] &= ~BIT(1);
ipcFifoCnt[!cpu] |= BIT(8);
ipcFifoCnt[!cpu] &= ~BIT(9);
// Trigger a send FIFO empty IRQ if enabled
if (ipcFifoCnt[cpu] & BIT(2))
core->interpreter[cpu].sendInterrupt(17);
}
// Trigger a send FIFO empty IRQ if the enable bit is set and the FIFO is empty
if ((ipcFifoCnt[cpu] & BIT(0)) && !(ipcFifoCnt[cpu] & BIT(2)) && (value & BIT(2)))
core->interpreter[cpu].sendInterrupt(17);
// Trigger a receive FIFO not empty IRQ if the enable bit is set and the FIFO isn't empty
if (!(ipcFifoCnt[cpu] & BIT(8)) && !(ipcFifoCnt[cpu] & BIT(10)) && (value & BIT(10)))
core->interpreter[cpu].sendInterrupt(18);
// If the error bit is set, acknowledge the error by clearing it
if (value & BIT(14)) ipcFifoCnt[cpu] &= ~BIT(14);
// Write to one of the IPCFIFOCNT registers
mask &= 0x8404;
ipcFifoCnt[cpu] = (ipcFifoCnt[cpu] & ~mask) | (value & mask);
}
void Ipc::writeIpcFifoSend(bool cpu, uint32_t mask, uint32_t value)
{
if (ipcFifoCnt[cpu] & BIT(15)) // FIFO enabled
{
if (fifos[cpu].size() < 16) // FIFO not full
{
// Push a word to the FIFO
fifos[cpu].push(value & mask);
if (fifos[cpu].size() == 1)
{
// If the FIFO is no longer empty, clear the empty bits
ipcFifoCnt[cpu] &= ~BIT(0);
ipcFifoCnt[!cpu] &= ~BIT(8);
// Trigger a receive FIFO not empty IRQ if enabled
if (ipcFifoCnt[!cpu] & BIT(10))
core->interpreter[!cpu].sendInterrupt(18);
}
else if (fifos[cpu].size() == 16)
{
// If the FIFO is now full, set the full bits
ipcFifoCnt[cpu] |= BIT(1);
ipcFifoCnt[!cpu] |= BIT(9);
}
}
else
{
// The FIFO can only hold 16 words, so indicate a send full error
ipcFifoCnt[cpu] |= BIT(14);
}
}
}
uint32_t Ipc::readIpcFifoRecv(bool cpu)
{
if (!fifos[!cpu].empty())
{
// Receive a word from the FIFO
ipcFifoRecv[cpu] = fifos[!cpu].front();
if (ipcFifoCnt[cpu] & BIT(15)) // FIFO enabled
{
// Remove the received word from the FIFO
fifos[!cpu].pop();
if (fifos[!cpu].empty())
{
// If the FIFO is now empty, set the empty bits
ipcFifoCnt[cpu] |= BIT(8);
ipcFifoCnt[!cpu] |= BIT(0);
// Trigger a receive FIFO empty IRQ if enabled
if (ipcFifoCnt[!cpu] & BIT(2))
core->interpreter[!cpu].sendInterrupt(17);
}
else if (fifos[!cpu].size() == 15)
{
// If the FIFO is no longer full, clear the full bits
ipcFifoCnt[cpu] &= ~BIT(9);
ipcFifoCnt[!cpu] &= ~BIT(1);
}
}
}
else
{
// If the FIFO is empty, indicate a receive empty error
ipcFifoCnt[cpu] |= BIT(14);
}
return ipcFifoRecv[cpu];
}