ScummVM API documentation
memory.h
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 modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (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, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef PSP_MEMORY_H
24 #define PSP_MEMORY_H
25 
26 #define MIN_AMOUNT_FOR_COMPLEX_COPY 8
27 #define MIN_AMOUNT_FOR_MISALIGNED_COPY 8
28 
29 //#define __PSP_DEBUG_PRINT__
30 
31 //#include "backends/platform/psp/trace.h"
32 
33 // These instructions don't generate automatically but are faster then copying byte by byte
34 inline void lwl_copy(byte *dst, const byte *src) {
35  uint32 data;
36  asm volatile ("lwr %0,0(%1)\n\t"
37  "lwl %0,3(%1)\n\t"
38  : "=&r" (data) : "r" (src), "m" (*src));
39 
40  asm volatile ("swr %1,0(%2)\n\t"
41  "swl %1,3(%2)\n\t"
42  : "=m" (*dst) : "r" (data), "r" (dst));
43 }
44 
48 class PspMemory {
49 private:
50  static void testCopy(const byte *debugDst, const byte *debugSrc, uint32 debugBytes);
51  static void copy(byte *dst, const byte *src, uint32 bytes);
52  static void copy32Aligned(uint32 *dst32, const uint32 *src32, uint32 bytes);
53  static void copy32Misaligned(uint32 *dst32, const byte *src, uint32 bytes, uint32 alignSrc);
54 
55  static inline void copy8(byte *dst, const byte *src, int32 bytes) {
56  //PSP_DEBUG_PRINT("copy8 called with dst[%p], src[%p], bytes[%d]\n", dst, src, bytes);
57  uint32 words = bytes >> 2;
58  for (; words; words--) {
59  lwl_copy(dst, src);
60  dst += 4;
61  src += 4;
62  }
63 
64  uint32 bytesLeft = bytes & 0x3;
65  for (; bytesLeft; bytesLeft--) {
66  *dst++ = *src++;
67  }
68  }
69 
70 public:
71  // This is the interface to the outside world
72  static void *fastCopy(void *dstv, const void *srcv, int32 bytes) {
73  byte *dst = (byte *)dstv;
74  const byte *src = (const byte *)srcv;
75 
76  if (bytes < MIN_AMOUNT_FOR_COMPLEX_COPY) {
77  copy8(dst, src, bytes);
78  } else { // go to more powerful copy
79  copy(dst, src, bytes);
80  }
81 
82  return dstv;
83  }
84 };
85 
86 inline void *psp_memcpy(void *dst, const void *src, int32 bytes) {
87  return PspMemory::fastCopy(dst, src, bytes);
88 }
89 
90 #endif /* PSP_MEMORY_H */
91 
92 #if defined(PSP_INCLUDE_SWAP) && !defined(PSP_MEMORY_SWAP_H)
93 #define PSP_MEMORY_SWAP_H
94 
95 //#include "backends/platform/psp/psppixelformat.h"
96 
97 class PspMemorySwap {
98 private:
99  static void testSwap(const uint16 *debugDst, const uint16 *debugSrc, uint32 debugBytes, PSPPixelFormat &format);
100  static void swap(uint16 *dst16, const uint16 *src16, uint32 bytes, PSPPixelFormat &format);
101  static void swap32Aligned(uint32 *dst32, const uint32 *src32, uint32 bytes, PSPPixelFormat &format);
102  static void swap32Misaligned(uint32 *dst32, const uint16 *src16, uint32 bytes, PSPPixelFormat &format);
103  // For swapping, we know that we have multiples of 16 bits
104  static void swap16(uint16 *dst16, const uint16 *src16, uint32 bytes, PSPPixelFormat &format) {
105  PSP_DEBUG_PRINT("swap16 called with dst16[%p], src16[%p], bytes[%d]\n", dst16, src16, bytes);
106  uint32 shorts = bytes >> 1;
107 
108  while (shorts--) {
109  *dst16++ = format.swapRedBlue16(*src16++);
110  }
111  }
112 
113 public:
114  static void fastSwap(byte *dst, const byte *src, uint32 bytes, PSPPixelFormat &format) {
115  if (bytes < MIN_AMOUNT_FOR_COMPLEX_COPY * 2) {
116  swap16((uint16 *)dst, (const uint16 *)src, bytes, format);
117  } else { // go to more powerful copy
118  swap((uint16 *)dst, (const uint16 *)src, bytes, format);
119  }
120  }
121 };
122 
123 #endif /* PSP_INCLUDE_SWAP */
Definition: memory.h:48
Definition: psppixelformat.h:34