1 // Copyright (C) 2003 Dolphin Project / 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 SVN repository and contact information can be found at
16 // http://code.google.com/p/dolphin-emu/
17 
18 #pragma once
19 
20 #include <cstring>
21 
22 #include "Common/CommonTypes.h"
23 #include "Core/Debugger/MemBlockInfo.h"
24 #include "Core/MemMap.h"
25 #include "Core/MIPS/MIPS.h"
26 
27 // To avoid pulling in the entire HLE.h.
28 extern MIPSState *currentMIPS;
29 
30 namespace Memory
31 {
32 
Memcpy(const u32 to_address,const void * from_data,const u32 len,const char * tag,size_t tagLen)33 inline void Memcpy(const u32 to_address, const void *from_data, const u32 len, const char *tag, size_t tagLen) {
34 	u8 *to = GetPointer(to_address);
35 	if (to) {
36 		memcpy(to, from_data, len);
37 		if (!tag) {
38 			tag = "Memcpy";
39 			tagLen = sizeof("Memcpy") - 1;
40 		}
41 		NotifyMemInfo(MemBlockFlags::WRITE, to_address, len, tag, tagLen);
42 	}
43 	// if not, GetPointer will log.
44 }
45 
Memcpy(void * to_data,const u32 from_address,const u32 len,const char * tag,size_t tagLen)46 inline void Memcpy(void *to_data, const u32 from_address, const u32 len, const char *tag, size_t tagLen) {
47 	const u8 *from = GetPointer(from_address);
48 	if (from) {
49 		memcpy(to_data, from, len);
50 		if (!tag) {
51 			tag = "Memcpy";
52 			tagLen = sizeof("Memcpy") - 1;
53 		}
54 		NotifyMemInfo(MemBlockFlags::READ, from_address, len, tag, tagLen);
55 	}
56 	// if not, GetPointer will log.
57 }
58 
Memcpy(const u32 to_address,const u32 from_address,const u32 len,const char * tag,size_t tagLen)59 inline void Memcpy(const u32 to_address, const u32 from_address, const u32 len, const char *tag, size_t tagLen) {
60 	u8 *to = GetPointer(to_address);
61 	if (to) {
62 		const u8 *from = GetPointer(from_address);
63 		if (from) {
64 			memcpy(to, from, len);
65 			char tagData[128];
66 			if (!tag) {
67 				const std::string srcTag = GetMemWriteTagAt(from_address, len);
68 				tag = tagData;
69 				tagLen = snprintf(tagData, sizeof(tagData), "Memcpy/%s", srcTag.c_str());
70 			}
71 			NotifyMemInfo(MemBlockFlags::READ, from_address, len, tag, tagLen);
72 			NotifyMemInfo(MemBlockFlags::WRITE, to_address, len, tag, tagLen);
73 		}
74 	}
75 	// if not, GetPointer will log.
76 }
77 
78 template<size_t tagLen>
Memcpy(const u32 to_address,const void * from_data,const u32 len,const char (& tag)[tagLen])79 inline void Memcpy(const u32 to_address, const void *from_data, const u32 len, const char(&tag)[tagLen]) {
80 	Memcpy(to_address, from_data, len, tag, tagLen);
81 }
82 
83 template<size_t tagLen>
Memcpy(void * to_data,const u32 from_address,const u32 len,const char (& tag)[tagLen])84 inline void Memcpy(void *to_data, const u32 from_address, const u32 len, const char(&tag)[tagLen]) {
85 	Memcpy(to_data, from_address, len, tag, tagLen);
86 }
87 
88 template<size_t tagLen>
Memcpy(const u32 to_address,const u32 from_address,const u32 len,const char (& tag)[tagLen])89 inline void Memcpy(const u32 to_address, const u32 from_address, const u32 len, const char(&tag)[tagLen]) {
90 	Memcpy(to_address, from_address, len, tag, tagLen);
91 }
92 
Memcpy(const u32 to_address,const void * from_data,const u32 len)93 inline void Memcpy(const u32 to_address, const void *from_data, const u32 len) {
94 	Memcpy(to_address, from_data, len, nullptr, 0);
95 }
96 
Memcpy(void * to_data,const u32 from_address,const u32 len)97 inline void Memcpy(void *to_data, const u32 from_address, const u32 len) {
98 	Memcpy(to_data, from_address, len, nullptr, 0);
99 }
100 
Memcpy(const u32 to_address,const u32 from_address,const u32 len)101 inline void Memcpy(const u32 to_address, const u32 from_address, const u32 len) {
102 	Memcpy(to_address, from_address, len, nullptr, 0);
103 }
104 
105 void Memset(const u32 _Address, const u8 _Data, const u32 _iLength, const char *tag = "Memset");
106 
107 template<class T>
ReadStruct(u32 address,T * ptr)108 void ReadStruct(u32 address, T *ptr)
109 {
110 	const u32 sz = (u32)sizeof(*ptr);
111 	Memcpy(ptr, address, sz);
112 }
113 
114 template<class T>
ReadStructUnchecked(u32 address,T * ptr)115 void ReadStructUnchecked(u32 address, T *ptr)
116 {
117 	const u32 sz = (u32)sizeof(*ptr);
118 	MemcpyUnchecked(ptr, address, sz);
119 }
120 
121 template<class T>
WriteStruct(u32 address,T * ptr)122 void WriteStruct(u32 address, T *ptr)
123 {
124 	const u32 sz = (u32)sizeof(*ptr);
125 	Memcpy(address, ptr, sz);
126 }
127 
128 template<class T>
WriteStructUnchecked(u32 address,T * ptr)129 void WriteStructUnchecked(u32 address, T *ptr)
130 {
131 	const u32 sz = (u32)sizeof(*ptr);
132 	MemcpyUnchecked(address, ptr, sz);
133 }
134 }
135