1
2 /* ScummVM - Graphic Adventure Engine
3 *
4 * ScummVM is the legal property of its developers, whose names
5 * are too numerous to list here. Please refer to the COPYRIGHT
6 * file distributed with this source distribution.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 *
22 */
23
24 #ifndef PSP_MEMORY_H
25 #define PSP_MEMORY_H
26
27 #define MIN_AMOUNT_FOR_COMPLEX_COPY 8
28 #define MIN_AMOUNT_FOR_MISALIGNED_COPY 8
29
30 //#define __PSP_DEBUG_PRINT__
31
32 //#include "backends/platform/psp/trace.h"
33
34 // These instructions don't generate automatically but are faster then copying byte by byte
lwl_copy(byte * dst,const byte * src)35 inline void lwl_copy(byte *dst, const byte *src) {
36 uint32 data;
37 asm volatile ("lwr %0,0(%1)\n\t"
38 "lwl %0,3(%1)\n\t"
39 : "=&r" (data) : "r" (src), "m" (*src));
40
41 asm volatile ("swr %1,0(%2)\n\t"
42 "swl %1,3(%2)\n\t"
43 : "=m" (*dst) : "r" (data), "r" (dst));
44 }
45
46 /**
47 * Class that does memory copying and swapping if needed
48 */
49 class PspMemory {
50 private:
51 static void testCopy(const byte *debugDst, const byte *debugSrc, uint32 debugBytes);
52 static void copy(byte *dst, const byte *src, uint32 bytes);
53 static void copy32Aligned(uint32 *dst32, const uint32 *src32, uint32 bytes);
54 static void copy32Misaligned(uint32 *dst32, const byte *src, uint32 bytes, uint32 alignSrc);
55
copy8(byte * dst,const byte * src,int32 bytes)56 static inline void copy8(byte *dst, const byte *src, int32 bytes) {
57 //PSP_DEBUG_PRINT("copy8 called with dst[%p], src[%p], bytes[%d]\n", dst, src, bytes);
58 uint32 words = bytes >> 2;
59 for (; words; words--) {
60 lwl_copy(dst, src);
61 dst += 4;
62 src += 4;
63 }
64
65 uint32 bytesLeft = bytes & 0x3;
66 for (; bytesLeft; bytesLeft--) {
67 *dst++ = *src++;
68 }
69 }
70
71 public:
72 // This is the interface to the outside world
fastCopy(void * dstv,const void * srcv,int32 bytes)73 static void *fastCopy(void *dstv, const void *srcv, int32 bytes) {
74 byte *dst = (byte *)dstv;
75 const byte *src = (const byte *)srcv;
76
77 if (bytes < MIN_AMOUNT_FOR_COMPLEX_COPY) {
78 copy8(dst, src, bytes);
79 } else { // go to more powerful copy
80 copy(dst, src, bytes);
81 }
82
83 return dstv;
84 }
85 };
86
psp_memcpy(void * dst,const void * src,int32 bytes)87 inline void *psp_memcpy(void *dst, const void *src, int32 bytes) {
88 return PspMemory::fastCopy(dst, src, bytes);
89 }
90
91 #endif /* PSP_MEMORY_H */
92
93 #if defined(PSP_INCLUDE_SWAP) && !defined(PSP_MEMORY_SWAP_H)
94 #define PSP_MEMORY_SWAP_H
95
96 //#include "backends/platform/psp/psppixelformat.h"
97
98 class PspMemorySwap {
99 private:
100 static void testSwap(const uint16 *debugDst, const uint16 *debugSrc, uint32 debugBytes, PSPPixelFormat &format);
101 static void swap(uint16 *dst16, const uint16 *src16, uint32 bytes, PSPPixelFormat &format);
102 static void swap32Aligned(uint32 *dst32, const uint32 *src32, uint32 bytes, PSPPixelFormat &format);
103 static void swap32Misaligned(uint32 *dst32, const uint16 *src16, uint32 bytes, PSPPixelFormat &format);
104 // For swapping, we know that we have multiples of 16 bits
swap16(uint16 * dst16,const uint16 * src16,uint32 bytes,PSPPixelFormat & format)105 static void swap16(uint16 *dst16, const uint16 *src16, uint32 bytes, PSPPixelFormat &format) {
106 PSP_DEBUG_PRINT("swap16 called with dst16[%p], src16[%p], bytes[%d]\n", dst16, src16, bytes);
107 uint32 shorts = bytes >> 1;
108
109 while (shorts--) {
110 *dst16++ = format.swapRedBlue16(*src16++);
111 }
112 }
113
114 public:
fastSwap(byte * dst,const byte * src,uint32 bytes,PSPPixelFormat & format)115 static void fastSwap(byte *dst, const byte *src, uint32 bytes, PSPPixelFormat &format) {
116 if (bytes < MIN_AMOUNT_FOR_COMPLEX_COPY * 2) {
117 swap16((uint16 *)dst, (const uint16 *)src, bytes, format);
118 } else { // go to more powerful copy
119 swap((uint16 *)dst, (const uint16 *)src, bytes, format);
120 }
121 }
122 };
123
124 #endif /* PSP_INCLUDE_SWAP */
125