v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
abi/types.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 #ifndef _SIFTEO_ABI_TYPES_H
27 #define _SIFTEO_ABI_TYPES_H
28 
29 #ifdef NOT_USERSPACE
30 # include <stdint.h>
31 #endif
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /*
38  * Standard integer types. This is a subset of what's in stdint.h,
39  * but we define them ourselves since system headers are not available.
40  *
41  * If we're doing a non-userspace build, however, these are pulled in
42  * from stdint.h above.
43  */
44 
45 #ifndef NOT_USERSPACE
46  typedef signed char int8_t;
47  typedef unsigned char uint8_t;
48  typedef signed short int16_t;
49  typedef unsigned short uint16_t;
50  typedef signed int int32_t;
51  typedef unsigned int uint32_t;
52  typedef signed long long int64_t;
53  typedef unsigned long long uint64_t;
54  typedef signed long intptr_t;
55  typedef unsigned long uintptr_t;
56 #endif
57 
58 // We can use 'bool' even in pure C code.
59 #ifndef __cplusplus
60  typedef uint8_t bool;
61 #endif
62 
63 /*
64  * Basic data types which are valid across the user/system boundary.
65  */
66 
67 #define _SYS_NUM_CUBE_SLOTS 24 // Total supported cube slots
68 #define _SYS_CUBE_ID_INVALID 0xFF // Reserved _SYSCubeID value
69 #define _SYS_BATTERY_MAX 0x10000 // Battery levels are 16.16 fixed point
70 
71 typedef uint8_t _SYSCubeID; // Cube slot index
72 typedef uint8_t _SYSNeighborID; // Neighbored object ID (superset of _SYSCubeID)
73 typedef int8_t _SYSSideID; // Cube side index
74 typedef uint32_t _SYSCubeIDVector; // One bit for each cube slot, MSB-first
75 typedef uint8_t _SYSAssetSlot; // Ordinal for one of the game's asset slots
76 
77 /*
78  * Small vector types
79  */
80 
81 struct _SYSInt2 {
82  int32_t x, y;
83 };
84 
85 struct _SYSInt3 {
86  int32_t x, y, z;
87 };
88 
89 union _SYSByte4 {
90  struct {
91  int8_t x, y, z, w;
92  };
93  uint32_t value;
94 };
95 
96 /*
97  * Neighbors
98  */
99 
100 #define _SYS_NEIGHBOR_TYPE_MASK 0xE0 // Mask for neighbor type bits
101 #define _SYS_NEIGHBOR_ID_MASK 0x1F // Mask for neighbor ID bits
102 
103 #define _SYS_NEIGHBOR_CUBE 0x00 // Neighbored to normal cube
104 #define _SYS_NEIGHBOR_BASE 0x20 // Neighbored to base
105 
106 #define _SYS_NEIGHBOR_NONE 0xFF // No neighbor
107 
108 union _SYSNeighborState {
109  uint32_t value;
110  _SYSNeighborID sides[4];
111 };
112 
113 /*
114  * Motion reporting
115  */
116 
117 #define _SYS_MOTION_MAX_ENTRIES 256 // Max size of motion buffer
118 #define _SYS_MOTION_TIMESTAMP_NS 250000 // Nanoseconds per timestamp unit (0.25 ms)
119 #define _SYS_MOTION_TIMESTAMP_HZ 4000 // Reciprocal of _SYS_MOTION_TIMESTAMP_NS
120 
121 struct _SYSMotionBufferHeader {
122  uint8_t tail;
123  uint8_t last;
124  uint8_t rate;
125  uint8_t reserved;
126  /*
127  * Followed by variable-size array of _SYSByte4.
128  *
129  * The 'w' field is used for a timestamp, encoded as a (delta - 1) since
130  * the last mesurement, in units of _SYS_MOTION_TIMESTAMP_NS. This means
131  * the maximum representable delta is exactly 64 milliseconds. Deltas of
132  * longer than this will be represented by duplicating samples in the buffer.
133  */
134 };
135 
136 struct _SYSMotionBuffer {
137  struct _SYSMotionBufferHeader header;
138  union _SYSByte4 samples[_SYS_MOTION_MAX_ENTRIES];
139 };
140 
141 struct _SYSMotionMedianAxis {
142  int8_t median;
143  int8_t minimum;
144  int8_t maximum;
145 };
146 
147 struct _SYSMotionMedian {
148  struct _SYSMotionMedianAxis axes[3];
149 };
150 
151 /*
152  * Type bits, for use in the 'tag' for the low-level _SYS_log() handler.
153  * Normally these don't need to be used in usermode code, they're inserted
154  * automatically by slinky when expanding _SYS_lti_log().
155  */
156 
157 #define _SYS_LOGTYPE_FMT 0 // param = strtab offest
158 #define _SYS_LOGTYPE_STRING 1 // param = 0, v1 = ptr
159 #define _SYS_LOGTYPE_HEXDUMP 2 // param = length, v1 = ptr
160 #define _SYS_LOGTYPE_SCRIPT 3 // param = script type
161 
162 #define _SYS_SCRIPT_NONE 0 // Normal logging
163 #define _SYS_SCRIPT_LUA 1 // Built-in Lua interpreter
164 
165 /*
166  * Internal state of the Pseudorandom Number Generator, maintained in user RAM.
167  */
168 
169 struct _SYSPseudoRandomState {
170  uint32_t a, b, c, d;
171 };
172 
173 /*
174  * Versioning.
175  *
176  * SYS_FEATUREs is a mask of features supported by the current version of the OS.
177  *
178  * NB: be sure to add any new features to _SYS_FEATURE_ALL.
179  */
180 
181 #define _SYS_OS_VERSION_MASK 0xffffff
182 #define _SYS_HW_VERSION_SHIFT 24
183 
184 // defaults for earlier OS versions that don't support _SYS_version()
185 #define _SYS_OS_VERSION_NONE 0x00
186 
187 #define _SYS_HW_VERSION_NONE 0x00
188 #define _SYS_HW_VERSION_GEN_2 0x02
189 #define _SYS_HW_VERSION_GEN_2_5 0x04
190 
191 #define _SYS_FEATURE_SYS_VERSION (1 << 0)
192 #define _SYS_FEATURE_BLUETOOTH (1 << 1)
193 #define _SYS_FEATURE_ALL (_SYS_FEATURE_SYS_VERSION | _SYS_FEATURE_BLUETOOTH)
194 
195 /*
196  * Hardware IDs are 64-bit numbers that uniquely identify a
197  * particular cube. A valid HWIDs never contains 0xFF bytes.
198  */
199 
200 #define _SYS_HWID_BYTES 8
201 #define _SYS_HWID_BITS 64
202 #define _SYS_INVALID_HWID ((uint64_t)-1)
203 
204 /*
205  * Filesystem
206  */
207 
208 #define _SYS_FS_VOL_GAME 0x4d47
209 #define _SYS_FS_VOL_LAUNCHER 0x4e4c
210 
211 #define _SYS_FS_MAX_OBJECT_KEYS 256
212 #define _SYS_FS_MAX_OBJECT_SIZE 4080
213 
214 // Opaque nonzero ID for a filesystem volume
215 typedef uint32_t _SYSVolumeHandle;
216 
217 // Application-defined ID for a key in our key/value object store
218 typedef uint8_t _SYSObjectKey;
219 
220 struct _SYSFilesystemInfo {
221  uint32_t unitSize; // Size of allocation unit, in bytes
222  uint32_t totalUnits; // Total number of allocation units on device
223  uint32_t freeUnits; // Number of free or immediately-recyclable units
224  uint32_t systemUnits; // Used by system data (cube pairing data, asset cache)
225  uint32_t launcherElfUnits; // Used by launcher ELF volumes
226  uint32_t launcherObjUnits; // Used by StoredObjects owned by launcher
227  uint32_t gameElfUnits; // Used by game ELF volumes
228  uint32_t gameObjUnits; // Used by StoredObjects owned by games
229  uint32_t selfElfUnits; // Used by the currently executing volume (may overlap with above)
230  uint32_t selfObjUnits; // StoredObjects owned by the currently executing volume (may overlap with above)
231 };
232 
233 
234 /*
235  * I/O (common)
236  */
237 
238 struct _SYSIoQueueHeader {
239  uint8_t head;
240  uint8_t tail;
241  uint8_t last;
242  uint8_t reserved;
243  // Followed by variable-size array of _SYS***Packet
244 };
245 
246 
247 /*
248  * Bluetooth
249  */
250 
251 #define _SYS_BT_PACKET_BYTES 19
252 #define _SYS_BT_MAX_QUEUED_PACKETS 256
253 
254 struct _SYSBluetoothPacket {
255  uint8_t length;
256  uint8_t type;
257  uint8_t bytes[_SYS_BT_PACKET_BYTES];
258 };
259 
260 struct _SYSBluetoothQueueHeader {
261  uint8_t head;
262  uint8_t tail;
263  uint8_t last;
264  uint8_t reserved;
265  // Followed by variable-size array of _SYSBluetoothPacket
266 };
267 
268 struct _SYSBluetoothQueue {
269  struct _SYSBluetoothQueueHeader header;
270  struct _SYSBluetoothPacket packets[_SYS_BT_MAX_QUEUED_PACKETS];
271 };
272 
273 struct _SYSBluetoothCounters {
274  uint32_t rxPackets;
275  uint32_t txPackets;
276  uint32_t rxBytes;
277  uint32_t txBytes;
278  uint32_t rxUserDropped;
279 };
280 
281 /*
282  * USB
283  */
284 
285 #define _SYS_USB_PACKET_BYTES 60
286 #define _SYS_USB_MAX_QUEUED_PACKETS 80
287 
288 struct _SYSUsbPacket {
289  uint8_t length;
290  uint8_t reserved0;
291  uint16_t reserved1;
292  uint32_t type;
293  uint8_t bytes[_SYS_USB_PACKET_BYTES];
294 };
295 
296 struct _SYSUsbQueue {
297  struct _SYSIoQueueHeader header;
298  struct _SYSUsbPacket packets[_SYS_USB_MAX_QUEUED_PACKETS];
299 };
300 
301 struct _SYSUsbCounters {
302  uint32_t rxPackets;
303  uint32_t txPackets;
304  uint32_t rxBytes;
305  uint32_t txBytes;
306  uint32_t rxUserDropped;
307 };
308 
309 /*
310  * RFC4122 compatible UUIDs.
311  *
312  * These are used in game metadata, to uniquely identify a particular
313  * binary. They are stored in network byte order, with field names compatible
314  * with RFC4122.
315  */
316 
317 struct _SYSUUID {
318  union {
319  struct {
320  uint32_t time_low;
321  uint16_t time_mid;
322  uint16_t time_hi_and_version;
323  uint8_t clk_seq_hi_res;
324  uint8_t clk_seq_low;
325  uint8_t node[6];
326  };
327  uint8_t bytes[16];
328  uint16_t hwords[8];
329  uint32_t words[4];
330  };
331 };
332 
333 /*
334  * System error codes, returned by some syscalls:
335  * - _SYS_fs_objectRead
336  * - _SYS_fs_objectWrite
337  *
338  * Where possible, these numbers line up with standard POSIX errno values.
339  */
340 
341 #define _SYS_ENOENT -2 // No such object found
342 #define _SYS_EFAULT -14 // Bad address
343 #define _SYS_EINVAL -22 // Invalid argument
344 #define _SYS_ENOSPC -28 // No space left on device
345 
346 /*
347  * Flags for _SYS_shutdown()
348  */
349 
350 #define _SYS_SHUTDOWN_WITH_UI (1 << 0) // Present shutdown user interface, allow cancellation
351 
352 
353 #ifdef __cplusplus
354 } // extern "C"
355 #endif
356 
357 #endif