Debian Codegraph @

Source/Core/VideoCommon/CommandProcessor.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/CommandProcessor.h"
5
6 #include <atomic>
7 #include <fmt/format.h>
8
9 #include "Common/Assert.h"
10 #include "Common/ChunkFile.h"
11 #include "Common/CommonTypes.h"
12 #include "Common/Flag.h"
13 #include "Common/Logging/Log.h"
14 #include "Common/MsgHandler.h"
15 #include "Core/CoreTiming.h"
16 #include "Core/HW/GPFifo.h"
17 #include "Core/HW/MMIO.h"
18 #include "Core/HW/ProcessorInterface.h"
19 #include "Core/PowerPC/PowerPC.h"
20 #include "Core/System.h"
21 #include "VideoCommon/Fifo.h"
22
23 namespace CommandProcessor
24 {
25 static bool IsOnThread(const Core::System& system)
26 {
27 return system.IsDualCoreMode();
28 }
29
30 static void UpdateInterrupts_Wrapper(Core::System& system, u64 userdata, s64 cyclesLate)
31 {
32 system.GetCommandProcessor().UpdateInterrupts(userdata);
33 }
34
35 void SCPFifoStruct::Init()
36 {
37 CPBase = 0;
38 CPEnd = 0;
39 CPHiWatermark = 0;
40 CPLoWatermark = 0;
41 CPReadWriteDistance = 0;
42 CPWritePointer = 0;
43 CPReadPointer = 0;
44 CPBreakpoint = 0;
45 SafeCPReadPointer = 0;
46
47 bFF_GPLinkEnable = 0;
48 bFF_GPReadEnable = 0;
49 bFF_BPEnable = 0;
50 bFF_BPInt = 0;
51
52 bFF_Breakpoint.store(0, std::memory_order_relaxed);
53 bFF_HiWatermark.store(0, std::memory_order_relaxed);
54 bFF_HiWatermarkInt.store(0, std::memory_order_relaxed);
55 bFF_LoWatermark.store(0, std::memory_order_relaxed);
56 bFF_LoWatermarkInt.store(0, std::memory_order_relaxed);
57 }
58
59 void SCPFifoStruct::DoState(PointerWrap& p)
60 {
61 p.Do(CPBase);
62 p.Do(CPEnd);
63 p.Do(CPHiWatermark);
64 p.Do(CPLoWatermark);
65 p.Do(CPReadWriteDistance);
66 p.Do(CPWritePointer);
67 p.Do(CPReadPointer);
68 p.Do(CPBreakpoint);
69 p.Do(SafeCPReadPointer);
70
71 p.Do(bFF_GPLinkEnable);
72 p.Do(bFF_GPReadEnable);
73 p.Do(bFF_BPEnable);
74 p.Do(bFF_BPInt);
75 p.Do(bFF_Breakpoint);
76
77 p.Do(bFF_LoWatermarkInt);
78 p.Do(bFF_HiWatermarkInt);
79
80 p.Do(bFF_LoWatermark);
81 p.Do(bFF_HiWatermark);
82 }
83
84 void CommandProcessorManager::DoState(PointerWrap& p)
85 {
86 p.Do(m_cp_status_reg);
87 p.Do(m_cp_ctrl_reg);
88 p.Do(m_cp_clear_reg);
89 p.Do(m_perf_select);
90 p.Do(m_unk_0a_reg);
91 m_fifo.DoState(p);
92
93 p.Do(m_interrupt_set);
94 p.Do(m_interrupt_waiting);
95 }
96
97 static inline void WriteHigh(std::atomic<u32>& reg, u16 highbits)
98 {
99 reg.store((reg.load(std::memory_order_relaxed) & 0x0000FFFF) | (static_cast<u32>(highbits) << 16),
100 std::memory_order_relaxed);
101 }
102
103 CommandProcessorManager::CommandProcessorManager(Core::System& system) : m_system{system}
104 {
105 }
106
107 void CommandProcessorManager::Init()
108 {
109 m_cp_status_reg.Hex = 0;
110 m_cp_status_reg.CommandIdle = 1;
111 m_cp_status_reg.ReadIdle = 1;
112
113 m_cp_ctrl_reg.Hex = 0;
114
115 m_cp_clear_reg.Hex = 0;
116
117 m_fifo.Init();
118
119 m_is_fifo_error_seen = false;
120
121 m_interrupt_set.Clear();
122 m_interrupt_waiting.Clear();
123
124 m_event_type_update_interrupts =
125 m_system.GetCoreTiming().RegisterEvent("CPInterrupt", UpdateInterrupts_Wrapper);
126 }
127
128 void CommandProcessorManager::RegisterMMIO(MMIO::Mapping* mmio, u32 base)
129 {
130 constexpr u16 WMASK_LO_ALIGN_32BIT = 0xffe0;
131 const u16 WMASK_HI_RESTRICT = GetPhysicalAddressMask(m_system.IsWii()) >> 16;
132
133 struct
134 {
135 u32 addr;
136 u16* ptr;
137 // FIFO mmio regs in the range [cc000020-cc00003e] have certain bits that always read as 0
138 // For _LO registers in this range, only bits 0xffe0 can be set
139 // For _HI registers in this range, only bits 0x03ff can be set on GCN and 0x1fff on Wii
140 u16 wmask;
141 } directly_mapped_vars[] = {
142 {FIFO_BASE_LO, MMIO::Utils::LowPart(&m_fifo.CPBase), WMASK_LO_ALIGN_32BIT},
143 {FIFO_BASE_HI, MMIO::Utils::HighPart(&m_fifo.CPBase), WMASK_HI_RESTRICT},
144 {FIFO_END_LO, MMIO::Utils::LowPart(&m_fifo.CPEnd), WMASK_LO_ALIGN_32BIT},
145 {FIFO_END_HI, MMIO::Utils::HighPart(&m_fifo.CPEnd), WMASK_HI_RESTRICT},
146 {FIFO_HI_WATERMARK_LO, MMIO::Utils::LowPart(&m_fifo.CPHiWatermark), WMASK_LO_ALIGN_32BIT},
147 {FIFO_HI_WATERMARK_HI, MMIO::Utils::HighPart(&m_fifo.CPHiWatermark), WMASK_HI_RESTRICT},
148 {FIFO_LO_WATERMARK_LO, MMIO::Utils::LowPart(&m_fifo.CPLoWatermark), WMASK_LO_ALIGN_32BIT},
149 {FIFO_LO_WATERMARK_HI, MMIO::Utils::HighPart(&m_fifo.CPLoWatermark), WMASK_HI_RESTRICT},
150 // FIFO_RW_DISTANCE has some complex read code different for
151 // single/dual core.
152 {FIFO_WRITE_POINTER_LO, MMIO::Utils::LowPart(&m_fifo.CPWritePointer), WMASK_LO_ALIGN_32BIT},
153 {FIFO_WRITE_POINTER_HI, MMIO::Utils::HighPart(&m_fifo.CPWritePointer), WMASK_HI_RESTRICT},
154 // FIFO_READ_POINTER has different code for single/dual core.
155 {FIFO_BP_LO, MMIO::Utils::LowPart(&m_fifo.CPBreakpoint), WMASK_LO_ALIGN_32BIT},
156 {FIFO_BP_HI, MMIO::Utils::HighPart(&m_fifo.CPBreakpoint), WMASK_HI_RESTRICT},
157 };
158
159 for (auto& mapped_var : directly_mapped_vars)
160 {
161 mmio->Register(base | mapped_var.addr, MMIO::DirectRead<u16>(mapped_var.ptr),
162 MMIO::DirectWrite<u16>(mapped_var.ptr, mapped_var.wmask));
163 }
164
165 // Timing and metrics MMIOs are stubbed with fixed values.
166 struct
167 {
168 u32 addr;
169 u16 value;
170 } metrics_mmios[] = {
171 {XF_RASBUSY_L, 0},
172 {XF_RASBUSY_H, 0},
173 {XF_CLKS_L, 0},
174 {XF_CLKS_H, 0},
175 {XF_WAIT_IN_L, 0},
176 {XF_WAIT_IN_H, 0},
177 {XF_WAIT_OUT_L, 0},
178 {XF_WAIT_OUT_H, 0},
179 {VCACHE_METRIC_CHECK_L, 0},
180 {VCACHE_METRIC_CHECK_H, 0},
181 {VCACHE_METRIC_MISS_L, 0},
182 {VCACHE_METRIC_MISS_H, 0},
183 {VCACHE_METRIC_STALL_L, 0},
184 {VCACHE_METRIC_STALL_H, 0},
185 {CLKS_PER_VTX_OUT, 4},
186 };
187 for (auto& metrics_mmio : metrics_mmios)
188 {
189 mmio->Register(base | metrics_mmio.addr, MMIO::Constant<u16>(metrics_mmio.value),
190 MMIO::InvalidWrite<u16>());
191 }
192
193 mmio->Register(base | STATUS_REGISTER, MMIO::ComplexRead<u16>([](Core::System& system_, u32) {
194 auto& cp = system_.GetCommandProcessor();
195 system_.GetFifo().SyncGPUForRegisterAccess();
196 cp.SetCpStatusRegister();
197 return cp.m_cp_status_reg.Hex;
198 }),
199 MMIO::InvalidWrite<u16>());
200
201 mmio->Register(base | CTRL_REGISTER, MMIO::DirectRead<u16>(&m_cp_ctrl_reg.Hex),
202 MMIO::ComplexWrite<u16>([](Core::System& system_, u32, u16 val) {
203 auto& cp = system_.GetCommandProcessor();
204 UCPCtrlReg tmp(val & 0x3F);
205 cp.m_cp_ctrl_reg.Hex = tmp.Hex;
206 cp.SetCpControlRegister();
207 system_.GetFifo().RunGpu();
208 }));
209
210 mmio->Register(base | CLEAR_REGISTER, MMIO::Constant<u16>(0),
211 MMIO::ComplexWrite<u16>([](Core::System& system_, u32, u16 val) {
212 auto& cp = system_.GetCommandProcessor();
213 UCPClearReg tmp(val);
214 cp.m_cp_clear_reg.Hex = tmp.Hex;
215 cp.SetCpClearRegister();
216 system_.GetFifo().RunGpu();
217 }));
218
219 // TODO: Figure out how this works. Written by GXSetGPMetric. Nicktoons MLB has DWARF v2 with enum
220 // names.
221 mmio->Register(base | PERF_SELECT, MMIO::DirectRead<u16>(&m_perf_select),
222 MMIO::DirectWrite<u16>(&m_perf_select, 0x0007));
223
224 // TODO: Figure out what this is.
225 mmio->Register(base | UNK_0A_REGISTER, MMIO::DirectRead<u16>(&m_unk_0a_reg),
226 MMIO::DirectWrite<u16>(&m_unk_0a_reg, 0x00FF));
227
228 // Some MMIOs have different handlers for single core vs. dual core mode.
229 const bool is_on_thread = IsOnThread(m_system);
230 MMIO::ReadHandlingMethod<u16>* fifo_rw_distance_lo_r;
231 if (is_on_thread)
232 {
233 fifo_rw_distance_lo_r = MMIO::ComplexRead<u16>([](Core::System& system_, u32) {
234 const auto& fifo_ = system_.GetCommandProcessor().GetFifo();
235 if (fifo_.CPWritePointer.load(std::memory_order_relaxed) >=
236 fifo_.SafeCPReadPointer.load(std::memory_order_relaxed))
237 {
238 return static_cast<u16>(fifo_.CPWritePointer.load(std::memory_order_relaxed) -
239 fifo_.SafeCPReadPointer.load(std::memory_order_relaxed));
240 }
241 else
242 {
243 return static_cast<u16>(fifo_.CPEnd.load(std::memory_order_relaxed) -
244 fifo_.SafeCPReadPointer.load(std::memory_order_relaxed) +
245 fifo_.CPWritePointer.load(std::memory_order_relaxed) -
246 fifo_.CPBase.load(std::memory_order_relaxed) + 32);
247 }
248 });
249 }
250 else
251 {
252 fifo_rw_distance_lo_r =
253 MMIO::DirectRead<u16>(MMIO::Utils::LowPart(&m_fifo.CPReadWriteDistance));
254 }
255 mmio->Register(base | FIFO_RW_DISTANCE_LO, fifo_rw_distance_lo_r,
256 MMIO::DirectWrite<u16>(MMIO::Utils::LowPart(&m_fifo.CPReadWriteDistance),
257 WMASK_LO_ALIGN_32BIT));
258
259 MMIO::ReadHandlingMethod<u16>* fifo_rw_distance_hi_r;
260 if (is_on_thread)
261 {
262 fifo_rw_distance_hi_r = MMIO::ComplexRead<u16>([](Core::System& system_, u32) {
263 const auto& fifo_ = system_.GetCommandProcessor().GetFifo();
264 system_.GetFifo().SyncGPUForRegisterAccess();
265 if (fifo_.CPWritePointer.load(std::memory_order_relaxed) >=
266 fifo_.SafeCPReadPointer.load(std::memory_order_relaxed))
267 {
268 return (fifo_.CPWritePointer.load(std::memory_order_relaxed) -
269 fifo_.SafeCPReadPointer.load(std::memory_order_relaxed)) >>
270 16;
271 }
272 else
273 {
274 return (fifo_.CPEnd.load(std::memory_order_relaxed) -
275 fifo_.SafeCPReadPointer.load(std::memory_order_relaxed) +
276 fifo_.CPWritePointer.load(std::memory_order_relaxed) -
277 fifo_.CPBase.load(std::memory_order_relaxed) + 32) >>
278 16;
279 }
280 });
281 }
282 else
283 {
284 fifo_rw_distance_hi_r = MMIO::ComplexRead<u16>([](Core::System& system_, u32) {
285 const auto& fifo_ = system_.GetCommandProcessor().GetFifo();
286 system_.GetFifo().SyncGPUForRegisterAccess();
287 return fifo_.CPReadWriteDistance.load(std::memory_order_relaxed) >> 16;
288 });
289 }
290 mmio->Register(base | FIFO_RW_DISTANCE_HI, fifo_rw_distance_hi_r,
291 MMIO::ComplexWrite<u16>([WMASK_HI_RESTRICT](Core::System& system_, u32, u16 val) {
292 auto& fifo_ = system_.GetCommandProcessor().GetFifo();
293 system_.GetFifo().SyncGPUForRegisterAccess();
294 WriteHigh(fifo_.CPReadWriteDistance, val & WMASK_HI_RESTRICT);
295 system_.GetFifo().RunGpu();
296 }));
297
298 mmio->Register(
299 base | FIFO_READ_POINTER_LO,
300 is_on_thread ? MMIO::DirectRead<u16>(MMIO::Utils::LowPart(&m_fifo.SafeCPReadPointer)) :
301 MMIO::DirectRead<u16>(MMIO::Utils::LowPart(&m_fifo.CPReadPointer)),
302 MMIO::DirectWrite<u16>(MMIO::Utils::LowPart(&m_fifo.CPReadPointer), WMASK_LO_ALIGN_32BIT));
303
304 MMIO::ReadHandlingMethod<u16>* fifo_read_hi_r;
305 MMIO::WriteHandlingMethod<u16>* fifo_read_hi_w;
306 if (is_on_thread)
307 {
308 fifo_read_hi_r = MMIO::ComplexRead<u16>([](Core::System& system_, u32) {
309 auto& fifo_ = system_.GetCommandProcessor().GetFifo();
310 system_.GetFifo().SyncGPUForRegisterAccess();
311 return fifo_.SafeCPReadPointer.load(std::memory_order_relaxed) >> 16;
312 });
313 fifo_read_hi_w =
314 MMIO::ComplexWrite<u16>([WMASK_HI_RESTRICT](Core::System& system_, u32, u16 val) {
315 auto& fifo_ = system_.GetCommandProcessor().GetFifo();
316 system_.GetFifo().SyncGPUForRegisterAccess();
317 WriteHigh(fifo_.CPReadPointer, val & WMASK_HI_RESTRICT);
318 fifo_.SafeCPReadPointer.store(fifo_.CPReadPointer.load(std::memory_order_relaxed),
319 std::memory_order_relaxed);
320 });
321 }
322 else
323 {
324 fifo_read_hi_r = MMIO::ComplexRead<u16>([](Core::System& system_, u32) {
325 const auto& fifo_ = system_.GetCommandProcessor().GetFifo();
326 system_.GetFifo().SyncGPUForRegisterAccess();
327 return fifo_.CPReadPointer.load(std::memory_order_relaxed) >> 16;
328 });
329 fifo_read_hi_w =
330 MMIO::ComplexWrite<u16>([WMASK_HI_RESTRICT](Core::System& system_, u32, u16 val) {
331 auto& fifo_ = system_.GetCommandProcessor().GetFifo();
332 system_.GetFifo().SyncGPUForRegisterAccess();
333 WriteHigh(fifo_.CPReadPointer, val & WMASK_HI_RESTRICT);
334 });
335 }
336 mmio->Register(base | FIFO_READ_POINTER_HI, fifo_read_hi_r, fifo_read_hi_w);
337 }
338
339 void CommandProcessorManager::GatherPipeBursted()
340 {
341 SetCPStatusFromCPU();
342
343 auto& processor_interface = m_system.GetProcessorInterface();
344
345 // if we aren't linked, we don't care about gather pipe data
346 if (!m_cp_ctrl_reg.GPLinkEnable)
347 {
348 if (IsOnThread(m_system) && !m_system.GetFifo().UseDeterministicGPUThread())
349 {
350 // In multibuffer mode is not allowed write in the same FIFO attached to the GPU.
351 // Fix Pokemon XD in DC mode.
352 if ((processor_interface.m_fifo_cpu_end == m_fifo.CPEnd.load(std::memory_order_relaxed)) &&
353 (processor_interface.m_fifo_cpu_base == m_fifo.CPBase.load(std::memory_order_relaxed)) &&
354 m_fifo.CPReadWriteDistance.load(std::memory_order_relaxed) > 0)
355 {
356 m_system.GetFifo().FlushGpu();
357 }
358 }
359 m_system.GetFifo().RunGpu();
360 return;
361 }
362
363 // update the fifo pointer
364 if (m_fifo.CPWritePointer.load(std::memory_order_relaxed) ==
365 m_fifo.CPEnd.load(std::memory_order_relaxed))
366 {
367 m_fifo.CPWritePointer.store(m_fifo.CPBase, std::memory_order_relaxed);
368 }
369 else
370 {
371 m_fifo.CPWritePointer.fetch_add(GPFifo::GATHER_PIPE_SIZE, std::memory_order_relaxed);
372 }
373
374 if (m_cp_ctrl_reg.GPReadEnable && m_cp_ctrl_reg.GPLinkEnable)
375 {
376 processor_interface.m_fifo_cpu_write_pointer =
377 m_fifo.CPWritePointer.load(std::memory_order_relaxed);
378 processor_interface.m_fifo_cpu_base = m_fifo.CPBase.load(std::memory_order_relaxed);
379 processor_interface.m_fifo_cpu_end = m_fifo.CPEnd.load(std::memory_order_relaxed);
380 }
381
382 // If the game is running close to overflowing, make the exception checking more frequent.
383 if (m_fifo.bFF_HiWatermark.load(std::memory_order_relaxed) != 0)
384 m_system.GetCoreTiming().ForceExceptionCheck(0);
385
386 m_fifo.CPReadWriteDistance.fetch_add(GPFifo::GATHER_PIPE_SIZE, std::memory_order_seq_cst);
387
388 m_system.GetFifo().RunGpu();
389
390 ASSERT_MSG(COMMANDPROCESSOR,
391 m_fifo.CPReadWriteDistance.load(std::memory_order_relaxed) <=
392 m_fifo.CPEnd.load(std::memory_order_relaxed) -
393 m_fifo.CPBase.load(std::memory_order_relaxed),
394 "FIFO is overflowed by GatherPipe !\nCPU thread is too fast!");
395
396 // check if we are in sync
397 ASSERT_MSG(COMMANDPROCESSOR,
398 m_fifo.CPWritePointer.load(std::memory_order_relaxed) ==
399 processor_interface.m_fifo_cpu_write_pointer,
400 "FIFOs linked but out of sync");
401 ASSERT_MSG(COMMANDPROCESSOR,
402 m_fifo.CPBase.load(std::memory_order_relaxed) == processor_interface.m_fifo_cpu_base,
403 "FIFOs linked but out of sync");
404 ASSERT_MSG(COMMANDPROCESSOR,
405 m_fifo.CPEnd.load(std::memory_order_relaxed) == processor_interface.m_fifo_cpu_end,
406 "FIFOs linked but out of sync");
407 }
408
409 void CommandProcessorManager::UpdateInterrupts(u64 userdata)
410 {
411 if (userdata)
412 {
413 m_interrupt_set.Set();
414 DEBUG_LOG_FMT(COMMANDPROCESSOR, "Interrupt set");
415 m_system.GetProcessorInterface().SetInterrupt(INT_CAUSE_CP, true);
416 }
417 else
418 {
419 m_interrupt_set.Clear();
420 DEBUG_LOG_FMT(COMMANDPROCESSOR, "Interrupt cleared");
421 m_system.GetProcessorInterface().SetInterrupt(INT_CAUSE_CP, false);
422 }
423 m_system.GetCoreTiming().ForceExceptionCheck(0);
424 m_interrupt_waiting.Clear();
425 m_system.GetFifo().RunGpu();
426 }
427
428 void CommandProcessorManager::UpdateInterruptsFromVideoBackend(u64 userdata)
429 {
430 if (!m_system.GetFifo().UseDeterministicGPUThread())
431 {
432 m_system.GetCoreTiming().ScheduleEvent(0, m_event_type_update_interrupts, userdata,
433 CoreTiming::FromThread::NON_CPU);
434 }
435 }
436
437 bool CommandProcessorManager::IsInterruptWaiting() const
438 {
439 return m_interrupt_waiting.IsSet();
440 }
441
442 void CommandProcessorManager::SetCPStatusFromGPU()
443 {
444 // breakpoint
445 const bool breakpoint = m_fifo.bFF_Breakpoint.load(std::memory_order_relaxed);
446 if (m_fifo.bFF_BPEnable.load(std::memory_order_relaxed) != 0)
447 {
448 if (m_fifo.CPBreakpoint.load(std::memory_order_relaxed) ==
449 m_fifo.CPReadPointer.load(std::memory_order_relaxed))
450 {
451 if (!breakpoint)
452 {
453 DEBUG_LOG_FMT(COMMANDPROCESSOR, "Hit breakpoint at {}",
454 m_fifo.CPReadPointer.load(std::memory_order_relaxed));
455 m_fifo.bFF_Breakpoint.store(1, std::memory_order_relaxed);
456 }
457 }
458 else
459 {
460 if (breakpoint)
461 {
462 DEBUG_LOG_FMT(COMMANDPROCESSOR, "Cleared breakpoint at {}",
463 m_fifo.CPReadPointer.load(std::memory_order_relaxed));
464 m_fifo.bFF_Breakpoint.store(0, std::memory_order_relaxed);
465 }
466 }
467 }
468 else
469 {
470 if (breakpoint)
471 {
472 DEBUG_LOG_FMT(COMMANDPROCESSOR, "Cleared breakpoint at {}",
473 m_fifo.CPReadPointer.load(std::memory_order_relaxed));
474 m_fifo.bFF_Breakpoint = false;
475 }
476 }
477
478 // overflow & underflow check
479 m_fifo.bFF_HiWatermark.store(
480 (m_fifo.CPReadWriteDistance.load(std::memory_order_relaxed) > m_fifo.CPHiWatermark),
481 std::memory_order_relaxed);
482 m_fifo.bFF_LoWatermark.store(
483 (m_fifo.CPReadWriteDistance.load(std::memory_order_relaxed) < m_fifo.CPLoWatermark),
484 std::memory_order_relaxed);
485
486 bool bpInt = m_fifo.bFF_Breakpoint.load(std::memory_order_relaxed) &&
487 m_fifo.bFF_BPInt.load(std::memory_order_relaxed);
488 bool ovfInt = m_fifo.bFF_HiWatermark.load(std::memory_order_relaxed) &&
489 m_fifo.bFF_HiWatermarkInt.load(std::memory_order_relaxed);
490 bool undfInt = m_fifo.bFF_LoWatermark.load(std::memory_order_relaxed) &&
491 m_fifo.bFF_LoWatermarkInt.load(std::memory_order_relaxed);
492
493 bool interrupt = (bpInt || ovfInt || undfInt) && m_cp_ctrl_reg.GPReadEnable;
494
495 if (interrupt != m_interrupt_set.IsSet() && !m_interrupt_waiting.IsSet())
496 {
497 const u64 userdata = interrupt ? 1 : 0;
498 if (IsOnThread(m_system))
499 {
500 if (!interrupt || bpInt || undfInt || ovfInt)
501 {
502 // Schedule the interrupt asynchronously
503 m_interrupt_waiting.Set();
504 UpdateInterruptsFromVideoBackend(userdata);
505 }
506 }
507 else
508 {
509 UpdateInterrupts(userdata);
510 }
511 }
512 }
513
514 void CommandProcessorManager::SetCPStatusFromCPU()
515 {
516 // overflow & underflow check
517 m_fifo.bFF_HiWatermark.store(
518 (m_fifo.CPReadWriteDistance.load(std::memory_order_relaxed) > m_fifo.CPHiWatermark),
519 std::memory_order_relaxed);
520 m_fifo.bFF_LoWatermark.store(
521 (m_fifo.CPReadWriteDistance.load(std::memory_order_relaxed) < m_fifo.CPLoWatermark),
522 std::memory_order_relaxed);
523
524 bool bpInt = m_fifo.bFF_Breakpoint.load(std::memory_order_relaxed) &&
525 m_fifo.bFF_BPInt.load(std::memory_order_relaxed);
526 bool ovfInt = m_fifo.bFF_HiWatermark.load(std::memory_order_relaxed) &&
527 m_fifo.bFF_HiWatermarkInt.load(std::memory_order_relaxed);
528 bool undfInt = m_fifo.bFF_LoWatermark.load(std::memory_order_relaxed) &&
529 m_fifo.bFF_LoWatermarkInt.load(std::memory_order_relaxed);
530
531 bool interrupt = (bpInt || ovfInt || undfInt) && m_cp_ctrl_reg.GPReadEnable;
532
533 if (interrupt != m_interrupt_set.IsSet() && !m_interrupt_waiting.IsSet())
534 {
535 const u64 userdata = interrupt ? 1 : 0;
536 if (IsOnThread(m_system))
537 {
538 if (!interrupt || bpInt || undfInt || ovfInt)
539 {
540 m_interrupt_set.Set(interrupt);
541 DEBUG_LOG_FMT(COMMANDPROCESSOR, "Interrupt set");
542 m_system.GetProcessorInterface().SetInterrupt(INT_CAUSE_CP, interrupt);
543 }
544 }
545 else
546 {
547 UpdateInterrupts(userdata);
548 }
549 }
550 }
551
552 void CommandProcessorManager::SetCpStatusRegister()
553 {
554 // Here always there is one fifo attached to the GPU
555 m_cp_status_reg.Breakpoint = m_fifo.bFF_Breakpoint.load(std::memory_order_relaxed);
556 m_cp_status_reg.ReadIdle = !m_fifo.CPReadWriteDistance.load(std::memory_order_relaxed) ||
557 (m_fifo.CPReadPointer.load(std::memory_order_relaxed) ==
558 m_fifo.CPWritePointer.load(std::memory_order_relaxed));
559 m_cp_status_reg.CommandIdle = !m_fifo.CPReadWriteDistance.load(std::memory_order_relaxed) ||
560 Fifo::AtBreakpoint(m_system) ||
561 !m_fifo.bFF_GPReadEnable.load(std::memory_order_relaxed);
562 m_cp_status_reg.UnderflowLoWatermark = m_fifo.bFF_LoWatermark.load(std::memory_order_relaxed);
563 m_cp_status_reg.OverflowHiWatermark = m_fifo.bFF_HiWatermark.load(std::memory_order_relaxed);
564
565 DEBUG_LOG_FMT(COMMANDPROCESSOR, "\t Read from STATUS_REGISTER : {:04x}", m_cp_status_reg.Hex);
566 DEBUG_LOG_FMT(COMMANDPROCESSOR,
567 "(r) status: iBP {} | fReadIdle {} | fCmdIdle {} | iOvF {} | iUndF {}",
568 m_cp_status_reg.Breakpoint ? "ON" : "OFF", m_cp_status_reg.ReadIdle ? "ON" : "OFF",
569 m_cp_status_reg.CommandIdle ? "ON" : "OFF",
570 m_cp_status_reg.OverflowHiWatermark ? "ON" : "OFF",
571 m_cp_status_reg.UnderflowLoWatermark ? "ON" : "OFF");
572 }
573
574 void CommandProcessorManager::SetCpControlRegister()
575 {
576 // Just before disabling reads, give the GPU a chance to catch up.
577 if (m_fifo.bFF_GPReadEnable.load(std::memory_order_relaxed) && !m_cp_ctrl_reg.GPReadEnable)
578 {
579 m_system.GetFifo().SyncGPUForRegisterAccess();
580 }
581 m_fifo.bFF_GPReadEnable.store(m_cp_ctrl_reg.GPReadEnable, std::memory_order_relaxed);
582 m_fifo.bFF_BPInt.store(m_cp_ctrl_reg.BPInt, std::memory_order_relaxed);
583 m_fifo.bFF_BPEnable.store(m_cp_ctrl_reg.BPEnable, std::memory_order_relaxed);
584 m_fifo.bFF_HiWatermarkInt.store(m_cp_ctrl_reg.FifoOverflowIntEnable, std::memory_order_relaxed);
585 m_fifo.bFF_LoWatermarkInt.store(m_cp_ctrl_reg.FifoUnderflowIntEnable, std::memory_order_relaxed);
586 m_fifo.bFF_GPLinkEnable.store(m_cp_ctrl_reg.GPLinkEnable, std::memory_order_relaxed);
587
588 DEBUG_LOG_FMT(COMMANDPROCESSOR, "\t GPREAD {} | BP {} | Int {} | OvF {} | UndF {} | LINK {}",
589 m_fifo.bFF_GPReadEnable.load(std::memory_order_relaxed) ? "ON" : "OFF",
590 m_fifo.bFF_BPEnable.load(std::memory_order_relaxed) ? "ON" : "OFF",
591 m_fifo.bFF_BPInt.load(std::memory_order_relaxed) ? "ON" : "OFF",
592 m_cp_ctrl_reg.FifoOverflowIntEnable ? "ON" : "OFF",
593 m_cp_ctrl_reg.FifoUnderflowIntEnable ? "ON" : "OFF",
594 m_cp_ctrl_reg.GPLinkEnable ? "ON" : "OFF");
595 }
596
597 // NOTE: We intentionally don't emulate this function at the moment.
598 // We don't emulate proper GP timing anyway at the moment, so it would just slow down emulation.
599 void CommandProcessorManager::SetCpClearRegister()
600 {
601 }
602
603 void CommandProcessorManager::ResetFifo()
604 {
605 // Link fifos, disable interrupts, disable reads.
606 m_cp_ctrl_reg.Hex = 0x0010;
607 SetCpControlRegister();
608 m_perf_select = 0;
609 m_unk_0a_reg = 0;
610 m_fifo.CPLoWatermark = 0;
611 m_fifo.CPHiWatermark = GetPhysicalAddressMask(m_system.IsWii()) & ~31u;
612 SetCpStatusRegister();
613 }
614
615 void CommandProcessorManager::HandleUnknownOpcode(u8 cmd_byte, const u8* buffer, bool preprocess)
616 {
617 // Datel software uses 0x01 during startup, and Mario Party 5's Wiggler capsule accidentally uses
618 // 0x01-0x03 due to sending 4 more vertices than intended (see https://dolp.in/i8104).
619 // Prince of Persia: Rival Swords sends 0x3f if the home menu is opened during the intro cutscene
620 // due to a game bug resulting in an incorrect vertex desc that results in the float value 1.0,
621 // encoded as 0x3f800000, being parsed as an opcode (see https://dolp.in/i9203).
622 //
623 // Hardware testing indicates that these opcodes do nothing, so to avoid annoying the user with
624 // spurious popups, we don't create a panic alert in those cases. Other unknown opcodes
625 // (such as 0x18) seem to result in actual hangs on real hardware, so the alert still is important
626 // to keep around for unexpected cases.
627 const bool suppress_panic_alert = (cmd_byte <= 0x7) || (cmd_byte == 0x3f);
628
629 const auto log_level =
630 suppress_panic_alert ? Common::Log::LogLevel::LWARNING : Common::Log::LogLevel::LERROR;
631
632 // We always generate this log message, though we only generate the panic alerts once.
633 //
634 // PC and LR are generally inaccurate in dual-core and are still misleading in single-core
635 // due to the gather pipe queueing data. Changing GATHER_PIPE_SIZE to 1 and
636 // GATHER_PIPE_EXTRA_SIZE to 16 * 32 in GPFifo.h, and using the cached interpreter CPU emulation
637 // engine, can result in more accurate information (though it is still a bit delayed).
638 // PC and LR are meaningless when using the fifoplayer, and will generally not be helpful if the
639 // unknown opcode is inside of a display list. Also note that the changes in GPFifo.h are not
640 // accurate and may introduce timing issues.
641 const auto& ppc_state = m_system.GetPPCState();
642 GENERIC_LOG_FMT(
643 Common::Log::LogType::VIDEO, log_level,
644 "FIFO: Unknown Opcode {:#04x} @ {}, preprocessing = {}, CPBase: {:#010x}, CPEnd: "
645 "{:#010x}, CPHiWatermark: {:#010x}, CPLoWatermark: {:#010x}, CPReadWriteDistance: "
646 "{:#010x}, CPWritePointer: {:#010x}, CPReadPointer: {:#010x}, CPBreakpoint: "
647 "{:#010x}, bFF_GPReadEnable: {}, bFF_BPEnable: {}, bFF_BPInt: {}, bFF_Breakpoint: "
648 "{}, bFF_GPLinkEnable: {}, bFF_HiWatermarkInt: {}, bFF_LoWatermarkInt: {}, "
649 "approximate PC: {:08x}, approximate LR: {:08x}",
650 cmd_byte, fmt::ptr(buffer), preprocess ? "yes" : "no",
651 m_fifo.CPBase.load(std::memory_order_relaxed), m_fifo.CPEnd.load(std::memory_order_relaxed),
652 m_fifo.CPHiWatermark, m_fifo.CPLoWatermark,
653 m_fifo.CPReadWriteDistance.load(std::memory_order_relaxed),
654 m_fifo.CPWritePointer.load(std::memory_order_relaxed),
655 m_fifo.CPReadPointer.load(std::memory_order_relaxed),
656 m_fifo.CPBreakpoint.load(std::memory_order_relaxed),
657 m_fifo.bFF_GPReadEnable.load(std::memory_order_relaxed) ? "true" : "false",
658 m_fifo.bFF_BPEnable.load(std::memory_order_relaxed) ? "true" : "false",
659 m_fifo.bFF_BPInt.load(std::memory_order_relaxed) ? "true" : "false",
660 m_fifo.bFF_Breakpoint.load(std::memory_order_relaxed) ? "true" : "false",
661 m_fifo.bFF_GPLinkEnable.load(std::memory_order_relaxed) ? "true" : "false",
662 m_fifo.bFF_HiWatermarkInt.load(std::memory_order_relaxed) ? "true" : "false",
663 m_fifo.bFF_LoWatermarkInt.load(std::memory_order_relaxed) ? "true" : "false", ppc_state.pc,
664 LR(ppc_state));
665
666 if (!m_is_fifo_error_seen && !suppress_panic_alert)
667 {
668 m_is_fifo_error_seen = true;
669
670 // The panic alert contains an explanatory part that's worded differently depending on the
671 // user's settings, so as to offer the most relevant advice to the user.
672 const char* advice;
673 if (IsOnThread(m_system) && !m_system.GetFifo().UseDeterministicGPUThread())
674 {
675 if (!m_system.GetCoreTiming().UseSyncOnSkipIdle() && !m_system.GetFifo().UseSyncGPU())
676 {
677 // The SyncOnSkipIdle setting is only in the Android GUI, so we use the INI name on other platforms.
678 //
679 // TODO: Mark the Android string as translatable once we have translations on Android. It's
680 // currently untranslatable so translators won't try to look up how they translated "Synchronize
681 // GPU Thread" and "On Idle Skipping" and then not find those strings and become confused.
682 #ifdef ANDROID
683 advice = "Please change the \"Synchronize GPU Thread\" setting to \"On Idle Skipping\"! "
684 "It's currently set to \"Never\", which makes this problem very likely to happen.";
685 #else
686 // i18n: Please leave SyncOnSkipIdle and True untranslated.
687 // The user needs to enter these terms as-is in an INI file.
688 advice = _trans("Please change the \"SyncOnSkipIdle\" setting to \"True\"! "
689 "It's currently disabled, which makes this problem very likely to happen.");
690 #endif
691 }
692 else
693 {
694 advice = _trans(
695 "This error is usually caused by the emulated GPU desyncing with the emulated CPU. "
696 "Turn off the \"Dual Core\" setting to avoid this.");
697 }
698 }
699 else
700 {
701 advice = _trans(
702 "This error is usually caused by the emulated GPU desyncing with the emulated CPU, "
703 "but your current settings make this unlikely to happen. The error may also be caused by "
704 "a game bug.");
705 }
706
707 PanicAlertFmtT("GFX FIFO: Unknown Opcode ({0:#04x} @ {1}, preprocess={2}).\n"
708 "\n"
709 "{3}\n"
710 "\n"
711 "Further errors will be sent to the Video Backend log and "
712 "Dolphin will now likely crash or hang.",
713 cmd_byte, fmt::ptr(buffer), preprocess, Common::GetStringT(advice));
714 }
715 }
716
717 } // namespace CommandProcessor