1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #include "Common/Serialize/Serializer.h"
19 #include "Common/Serialize/SerializeFuncs.h"
20 #include "Core/CoreTiming.h"
21 #include "Core/MemMapHelpers.h"
22 #include "Core/Reporting.h"
23 #include "Core/HLE/HLE.h"
24 #include "Core/HLE/sceDmac.h"
25 #include "Core/HLE/sceKernel.h"
26 #include "Core/HLE/FunctionWrappers.h"
27 #include "Core/Debugger/Breakpoints.h"
28 #include "GPU/GPUInterface.h"
29 #include "GPU/GPUState.h"
30 
31 u64 dmacMemcpyDeadline;
32 
__DmacInit()33 void __DmacInit() {
34 	dmacMemcpyDeadline = 0;
35 }
36 
__DmacDoState(PointerWrap & p)37 void __DmacDoState(PointerWrap &p) {
38 	auto s = p.Section("sceDmac", 0, 1);
39 	if (s == 0) {
40 		dmacMemcpyDeadline = 0;
41 		return;
42 	}
43 
44 	Do(p, dmacMemcpyDeadline);
45 }
46 
__DmacMemcpy(u32 dst,u32 src,u32 size)47 static int __DmacMemcpy(u32 dst, u32 src, u32 size) {
48 	bool skip = false;
49 	if (Memory::IsVRAMAddress(src) || Memory::IsVRAMAddress(dst)) {
50 		skip = gpu->PerformMemoryCopy(dst, src, size);
51 	}
52 	if (!skip) {
53 		currentMIPS->InvalidateICache(src, size);
54 		const std::string tag = "DmacMemcpy/" + GetMemWriteTagAt(src, size);
55 		Memory::Memcpy(dst, src, size, tag.c_str(), tag.size());
56 		currentMIPS->InvalidateICache(dst, size);
57 	}
58 
59 	// This number seems strangely reproducible.
60 	if (size >= 272) {
61 		// Approx. 225 MiB/s or 235929600 B/s, so let's go with 236 B/us.
62 		int delayUs = size / 236;
63 		dmacMemcpyDeadline = CoreTiming::GetTicks() + usToCycles(delayUs);
64 		return hleDelayResult(0, "dmac copy", delayUs);
65 	}
66 	return 0;
67 }
68 
sceDmacMemcpy(u32 dst,u32 src,u32 size)69 static u32 sceDmacMemcpy(u32 dst, u32 src, u32 size) {
70 	if (size == 0) {
71 		// Some games seem to do this frequently.
72 		DEBUG_LOG(HLE, "sceDmacMemcpy(dest=%08x, src=%08x, size=%i): invalid size", dst, src, size);
73 		return SCE_KERNEL_ERROR_INVALID_SIZE;
74 	}
75 	if (!Memory::IsValidAddress(dst) || !Memory::IsValidAddress(src)) {
76 		ERROR_LOG(HLE, "sceDmacMemcpy(dest=%08x, src=%08x, size=%i): invalid address", dst, src, size);
77 		return SCE_KERNEL_ERROR_INVALID_POINTER;
78 	}
79 	if (dst + size >= 0x80000000 || src + size >= 0x80000000 || size >= 0x80000000) {
80 		ERROR_LOG(HLE, "sceDmacMemcpy(dest=%08x, src=%08x, size=%i): illegal size", dst, src, size);
81 		return SCE_KERNEL_ERROR_PRIV_REQUIRED;
82 	}
83 
84 	if (dmacMemcpyDeadline > CoreTiming::GetTicks()) {
85 		WARN_LOG_REPORT_ONCE(overlapDmacMemcpy, HLE, "sceDmacMemcpy(dest=%08x, src=%08x, size=%d): overlapping read", dst, src, size);
86 		// TODO: Should block, seems like copy doesn't start until previous finishes.
87 		// Might matter for overlapping copies.
88 	} else {
89 		DEBUG_LOG(HLE, "sceDmacMemcpy(dest=%08x, src=%08x, size=%i)", dst, src, size);
90 	}
91 
92 	return __DmacMemcpy(dst, src, size);
93 }
94 
sceDmacTryMemcpy(u32 dst,u32 src,u32 size)95 static u32 sceDmacTryMemcpy(u32 dst, u32 src, u32 size) {
96 	if (size == 0) {
97 		ERROR_LOG(HLE, "sceDmacTryMemcpy(dest=%08x, src=%08x, size=%i): invalid size", dst, src, size);
98 		return SCE_KERNEL_ERROR_INVALID_SIZE;
99 	}
100 	if (!Memory::IsValidAddress(dst) || !Memory::IsValidAddress(src)) {
101 		ERROR_LOG(HLE, "sceDmacTryMemcpy(dest=%08x, src=%08x, size=%i): invalid address", dst, src, size);
102 		return SCE_KERNEL_ERROR_INVALID_POINTER;
103 	}
104 	if (dst + size >= 0x80000000 || src + size >= 0x80000000 || size >= 0x80000000) {
105 		ERROR_LOG(HLE, "sceDmacTryMemcpy(dest=%08x, src=%08x, size=%i): illegal size", dst, src, size);
106 		return SCE_KERNEL_ERROR_PRIV_REQUIRED;
107 	}
108 
109 	if (dmacMemcpyDeadline > CoreTiming::GetTicks()) {
110 		DEBUG_LOG(HLE, "sceDmacTryMemcpy(dest=%08x, src=%08x, size=%i): busy", dst, src, size);
111 		return SCE_KERNEL_ERROR_BUSY;
112 	} else {
113 		DEBUG_LOG(HLE, "sceDmacTryMemcpy(dest=%08x, src=%08x, size=%i)", dst, src, size);
114 	}
115 
116 	return __DmacMemcpy(dst, src, size);
117 }
118 
119 const HLEFunction sceDmac[] = {
120 	{0X617F3FE6, &WrapU_UUU<sceDmacMemcpy>,          "sceDmacMemcpy",    'x', "xxx"},
121 	{0XD97F94D8, &WrapU_UUU<sceDmacTryMemcpy>,       "sceDmacTryMemcpy", 'x', "xxx"},
122 };
123 
Register_sceDmac()124 void Register_sceDmac() {
125 	RegisterModule("sceDmac", ARRAY_SIZE(sceDmac), sceDmac);
126 }
127