v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
group.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * Sifteo SDK
4  *
5  * Copyright <c> 2012 Sifteo, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #pragma once
27 #ifdef NOT_USERSPACE
28 # error This is a userspace-only header, not allowed by the current build.
29 #endif
30 
31 #include <sifteo/abi.h>
32 #include <sifteo/limits.h>
33 #include <sifteo/macros.h>
34 #include <sifteo/math.h>
35 
36 namespace Sifteo {
37 
54 struct AssetGroup {
55  _SYSAssetGroup sys;
56  _SYSAssetGroupCube cubes[CUBE_ALLOCATION];
57 
59  operator const _SYSAssetGroup& () const { return sys; }
60  operator _SYSAssetGroup& () { return sys; }
61  operator const _SYSAssetGroup* () const { return &sys; }
62  operator _SYSAssetGroup* () { return &sys; }
63 
72  ALWAYS_INLINE const _SYSAssetGroupHeader *sysHeader(bool requireConst=false) const
73  {
74  // AssetGroups are typically in RAM, but we want the static
75  // initializer data so that our return value is known at compile time.
76  _SYSAssetGroup *G = (_SYSAssetGroup*)
77  _SYS_lti_initializer(reinterpret_cast<const void*>(&sys), requireConst);
78  return reinterpret_cast<const _SYSAssetGroupHeader*>(G->pHdr);
79  }
80 
84  unsigned numTiles() const {
85  return sysHeader()->numTiles;
86  }
87 
93  unsigned tileAllocation() const {
94  return roundup<unsigned>(numTiles(), _SYS_ASSET_GROUP_SIZE_UNIT);
95  }
96 
100  unsigned compressedSize() const {
101  return sysHeader()->dataSize;
102  }
103 
114  uint16_t baseAddress(_SYSCubeID cube) const {
115  ASSERT(cube < CUBE_ALLOCATION);
116  return cubes[cube].baseAddr;
117  }
118 
134  bool isInstalled(_SYSCubeIDVector vec) {
135  return _SYS_asset_findInCache(*this, vec) == vec;
136  }
137 
146  bool isInstalled(_SYSCubeID cube) {
147  return isInstalled(_SYSCubeIDVector(0x80000000 >> cube));
148  }
149 };
150 
151 
181 class AssetSlot {
182 public:
183  _SYSAssetSlot sys;
184 
186  explicit AssetSlot(_SYSAssetSlot sys) : sys(sys) {}
187 
189  AssetSlot(const AssetSlot &other) : sys(other.sys) {}
190 
192  operator const _SYSAssetSlot& () const { return sys; }
193  operator const _SYSAssetSlot* () const { return &sys; }
194 
201  return AssetSlot(_SYS_lti_counter("Sifteo.AssetGroupSlot", 0));
202  }
203 
211  unsigned tilesFree(_SYSCubeIDVector cubes = -1) const {
212  return _SYS_asset_slotTilesFree(*this, cubes);
213  }
214 
224  bool hasRoomFor(const AssetGroup &group, _SYSCubeIDVector cubes = -1) const {
225  return tilesFree(cubes) >= group.tileAllocation();
226  }
227 
241  void erase(_SYSCubeIDVector cubes = -1) const {
242  _SYS_asset_slotErase(*this, cubes);
243  }
244 
259  // _SYSMetadataBootAsset
260  _SYS_lti_metadata(_SYS_METADATA_BOOT_ASSET, "IBBBB",
261  group.sysHeader(true), sys, 0, 0, 0);
262 
263  // Update base address from the cache. Make sure it was successful.
264  _SYSCubeIDVector vec = _SYS_asset_findInCache(group, -1);
265  ASSERT(vec != 0);
266 
267  return *this;
268  }
269 };
270 
275 }; // namespace Sifteo
AssetSlots are numbered containers, in a cube's flash memory, which can hold AssetGroups.
Definition: group.h:181
#define ALWAYS_INLINE
Always enable inlining for one function.
Definition: macros.h:76
A bundle of compressed tile data, for use by AssetImages.
Definition: group.h:54
ALWAYS_INLINE const _SYSAssetGroupHeader * sysHeader(bool requireConst=false) const
Get a pointer to the read-only system data for this asset group.
Definition: group.h:72
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
bool hasRoomFor(const AssetGroup &group, _SYSCubeIDVector cubes=-1) const
Is there room in this slot to load a particular AssetGroup without erasing the slot?
Definition: group.h:224
static ALWAYS_INLINE AssetSlot allocate()
Statically allocate a new AssetSlot.
Definition: group.h:200
uint16_t baseAddress(_SYSCubeID cube) const
Return the base address of this asset group, as loaded onto the specified cube.
Definition: group.h:114
#define CUBE_ALLOCATION
Compile-time cube limit for the current application.
Definition: limits.h:54
AssetSlot(const AssetSlot &other)
Copy constructor.
Definition: group.h:189
unsigned tilesFree(_SYSCubeIDVector cubes=-1) const
How much space is remaining in this slot, measured in tiles?
Definition: group.h:211
unsigned compressedSize() const
Get the compressed size of this asset group, in bytes.
Definition: group.h:100
unsigned tileAllocation() const
How many tiles will this group use up in its AssetSlot?
Definition: group.h:93
unsigned numTiles() const
Get the size of this asset group, in tiles.
Definition: group.h:84
Definition: array.h:34
bool isInstalled(_SYSCubeIDVector vec)
Is this AssetGroup installed on all cubes in the given vector?
Definition: group.h:134
ALWAYS_INLINE AssetSlot bootstrap(AssetGroup &group) const
Mark a particular AssetGroup as a "bootstrap" asset for this slot.
Definition: group.h:258
AssetSlot(_SYSAssetSlot sys)
Explicit conversion from a system object.
Definition: group.h:186
void erase(_SYSCubeIDVector cubes=-1) const
Erase this slot.
Definition: group.h:241
bool isInstalled(_SYSCubeID cube)
Is this AssetGroup installed on a particular cube?
Definition: group.h:146
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658