-
Notifications
You must be signed in to change notification settings - Fork 0
/
cop1.cpp
235 lines (197 loc) · 5.55 KB
/
cop1.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
// ============================================================================
// Portable MIPS III emulator
// COP1 - FPU
// Marcos Medeiros, 2015
// ============================================================================
#include <iostream>
#include <cstdint>
#include <cmath>
#include "mips3.h"
#include "mipsdef.h"
#include "memory.h"
#include <QDebug>
#define FPR_s(n) (*(float *) ((void *)&m_state.cpr[1][n]))
#define FPR_d(n) (*(double *) ((void *)&m_state.cpr[1][n]))
#define FD_d FPR_d(FDNUM)
#define FS_d FPR_d(FSNUM)
#define FT_d FPR_d(FTNUM)
#define FD_s FPR_s(FDNUM)
#define FS_s FPR_s(FSNUM)
#define FT_s FPR_s(FTNUM)
#define FD_w (*(uint32_t *) &m_state.cpr[1][FDNUM])
#define FS_w (*(uint32_t *) &m_state.cpr[1][FSNUM])
#define FT_w (*(uint32_t *) &m_state.cpr[1][FTNUM])
#define FD_l m_state.cpr[1][FDNUM]
#define FS_l m_state.cpr[1][FSNUM]
#define FT_l m_state.cpr[1][FTNUM]
#define FCR31 m_state.fcr[31]
#define FLOAT (RSNUM == 16 || RSNUM == 17)
// Double/Single precision
#define DP (RSNUM == 17 || RSNUM == 21)
#define SP (RSNUM == 16 || RSNUM == 20)
#define INTEGER (RSNUM == 20 || RSNUM == 21)
// Word/Long precision
#define WT (RSNUM == 20)
#define LT (RSNUM == 21)
namespace mips
{
void mips3::cop1_reset()
{
for (int i = 0; i < 32; i++) {
FPR_d(i) = -1.0;
m_state.fcr[i] = 0;
}
}
void mips3::cop1_execute_16(uint32_t opcode)
{
qDebug() << QString::number(m_state.pc, 16) << "Op:" << RSNUM << (opcode & 0x3F) << "[FPU16]";
}
void mips3::cop1_execute_32(uint32_t opcode)
{
//qDebug() << QString(dasm(opcode, m_prev_pc).c_str());
switch (RSNUM) {
// MFC1 rt, rd
case 0x00:
if (RTNUM)
RT = (int32_t) m_state.cpr[1][RDNUM];
break;
// DMFC1 rt, rd
case 0x01:
if (RTNUM)
RT = (uint64_t) m_state.cpr[1][RDNUM];
break;
// CFC1 rt, fs
case 0x02:
if (RTNUM)
RT = (int32_t) m_state.fcr[FSNUM];
break;
// MTC1 rt, fs
case 0x04:
m_state.cpr[1][FSNUM] = (uint32_t) RT;
break;
// DMTC1 rt, fs
case 0x05:
m_state.cpr[1][FSNUM] = RT;
break;
// CTC1 rt, fs
case 0x06:
m_state.fcr[FSNUM] = (int32_t) RT;
break;
// BC
case 0x08:
{
switch ((opcode >> 16) & 3) {
// BC1F offset
case 0x00:
if (~FCR31 & 0x800000) {
m_next_pc = m_state.pc + ((int32_t)(SIMM) << 2);
m_delay_slot = true;
}
break;
// BC1FL offset
case 0x02:
if (~FCR31 & 0x800000) {
m_next_pc = m_state.pc + ((int32_t)(SIMM) << 2);
m_delay_slot = true;
} else
m_state.pc += 4;
break;
// BC1T offset
case 0x01:
if (FCR31 & 0x800000) {
m_next_pc = m_state.pc + ((int32_t)(SIMM) << 2);
m_delay_slot = true;
}
break;
// BC1TL offset
case 0x03:
if (FCR31 & 0x800000) {
m_next_pc = m_state.pc + ((int32_t)(SIMM) << 2);
m_delay_slot = true;
} else
m_state.pc += 4;
break;
default:
qDebug() << QString::number(m_state.pc, 16) << "OpBC:" << ((opcode >> 16) & 0x1F) << "[FPU32][COP]";
break;
}
break;
}
default:
{
switch (opcode & 0x3F) {
// ADD.fmt
case 0x00:
if (SP) FD_s = FS_s + FT_s;
else FD_d = FS_d + FT_d;
break;
// SUB.fmt
case 0x01:
if (SP) FD_s = FS_s - FT_s;
else FD_d = FS_d - FT_d;
break;
// MUL.fmt
case 0x02:
if (SP) FD_s = FS_s * FT_s;
else FD_d = FS_d * FT_d;
break;
// DIV.fmt
case 0x03:
if (SP) FD_s = FS_s / FT_s;
else FD_d = FS_d / FT_d;
break;
// SQRT.fmt
case 0x04:
if (SP) FD_s = sqrt(FS_s);
else FD_d = sqrt(FS_d);
break;
// ABS.fmt
case 0x05:
if (SP) FD_s = abs(FS_s);
else FD_d = abs(FS_d);
break;
// MOV.fmt
case 0x06:
if (SP) FD_s = FS_s;
else FD_d = FS_d;
break;
// NEG.fmt
case 0x07:
if (SP) FD_s = -FS_s;
else FD_d = -FS_d;
break;
// CVT.S.x
case 0x20:
if (INTEGER) {
if (SP) FD_s = (int32_t) FS_w;
else FD_s = (int64_t) FS_l;
} else
FD_s = FS_d;
break;
// CVT.W.x
case 0x24:
if (SP) FD_w = (int32_t) FS_s;
else FD_w = (int32_t) FS_d;
break;
// C.UN.x fs, ft
case 0x32:
if (SP) FCR31 = (isnan(FS_s) || isnan(FT_s)) ? FCR31 | 0x800000 : FCR31 & ~0x800000;
else FCR31 &= ~0x800000;
break;
// C.OLT.x fs, ft
case 0x34:
if (SP) FCR31 = (FS_s < FT_s) ? FCR31 | 0x800000 : FCR31 & ~0x800000;
else FCR31 = (FS_d < FT_d) ? FCR31 | 0x800000 : FCR31 & ~0x800000;
break;
// C.F fs, ft
case 0x3C:
FCR31 &= ~800000;
break;
default:
qDebug() << QString::number(m_state.pc, 16) << "Op:" << RSNUM << (opcode & 0x3F) << "[FPU32]";
break;
}
}
}
}
}