v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
group.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * This file is part of the public interface to the Sifteo SDK.
4  * Copyright <c> 2012 Sifteo, Inc. All rights reserved.
5  */
6 
7 #pragma once
8 #ifdef NOT_USERSPACE
9 # error This is a userspace-only header, not allowed by the current build.
10 #endif
11 
12 #include <sifteo/abi.h>
13 #include <sifteo/limits.h>
14 #include <sifteo/macros.h>
15 #include <sifteo/math.h>
16 
17 namespace Sifteo {
18 
35 struct AssetGroup {
36  _SYSAssetGroup sys;
37  _SYSAssetGroupCube cubes[CUBE_ALLOCATION];
38 
40  operator const _SYSAssetGroup& () const { return sys; }
41  operator _SYSAssetGroup& () { return sys; }
42  operator const _SYSAssetGroup* () const { return &sys; }
43  operator _SYSAssetGroup* () { return &sys; }
44 
53  ALWAYS_INLINE const _SYSAssetGroupHeader *sysHeader(bool requireConst=false) const
54  {
55  // AssetGroups are typically in RAM, but we want the static
56  // initializer data so that our return value is known at compile time.
57  _SYSAssetGroup *G = (_SYSAssetGroup*)
58  _SYS_lti_initializer(reinterpret_cast<const void*>(&sys), requireConst);
59  return reinterpret_cast<const _SYSAssetGroupHeader*>(G->pHdr);
60  }
61 
65  unsigned numTiles() const {
66  return sysHeader()->numTiles;
67  }
68 
74  unsigned tileAllocation() const {
75  return roundup<unsigned>(numTiles(), _SYS_ASSET_GROUP_SIZE_UNIT);
76  }
77 
81  unsigned compressedSize() const {
82  return sysHeader()->dataSize;
83  }
84 
95  uint16_t baseAddress(_SYSCubeID cube) const {
96  ASSERT(cube < CUBE_ALLOCATION);
97  return cubes[cube].baseAddr;
98  }
99 
115  bool isInstalled(_SYSCubeIDVector vec) {
116  return _SYS_asset_findInCache(*this, vec) == vec;
117  }
118 
127  bool isInstalled(_SYSCubeID cube) {
128  return isInstalled(_SYSCubeIDVector(0x80000000 >> cube));
129  }
130 };
131 
132 
162 class AssetSlot {
163 public:
164  _SYSAssetSlot sys;
165 
167  explicit AssetSlot(_SYSAssetSlot sys) : sys(sys) {}
168 
170  AssetSlot(const AssetSlot &other) : sys(other.sys) {}
171 
173  operator const _SYSAssetSlot& () const { return sys; }
174  operator const _SYSAssetSlot* () const { return &sys; }
175 
182  return AssetSlot(_SYS_lti_counter("Sifteo.AssetGroupSlot", 0));
183  }
184 
192  unsigned tilesFree(_SYSCubeIDVector cubes = -1) const {
193  return _SYS_asset_slotTilesFree(*this, cubes);
194  }
195 
205  bool hasRoomFor(const AssetGroup &group, _SYSCubeIDVector cubes = -1) const {
206  return tilesFree(cubes) >= group.tileAllocation();
207  }
208 
222  void erase(_SYSCubeIDVector cubes = -1) const {
223  _SYS_asset_slotErase(*this, cubes);
224  }
225 
240  // _SYSMetadataBootAsset
241  _SYS_lti_metadata(_SYS_METADATA_BOOT_ASSET, "IBBBB",
242  group.sysHeader(true), sys, 0, 0, 0);
243 
244  // Update base address from the cache. Make sure it was successful.
245  _SYSCubeIDVector vec = _SYS_asset_findInCache(group, -1);
246  ASSERT(vec != 0);
247 
248  return *this;
249  }
250 };
251 
256 }; // namespace Sifteo