Aseba  1.5.5
vm.h
Go to the documentation of this file.
1 /*
2  Aseba - an event-based framework for distributed robot control
3  Copyright (C) 2007--2016:
4  Stephane Magnenat <stephane at magnenat dot net>
5  (http://stephane.magnenat.net)
6  and other contributors, see authors.txt for details
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation, version 3 of the License.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __ASEBA_VM_H
22 #define __ASEBA_VM_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include "../common/types.h"
29 
53 
54 enum
55 {
57 };
58 
69 typedef struct
70 {
71  // node id
72  uint16 nodeId;
73 
74  // bytecode
78  // variables
82  // execution stack
86  // execution
87  uint16 flags;
88  uint16 pc;
89  sint16 sp;
90 
91  // breakpoint
92  uint16 breakpoints[ASEBA_MAX_BREAKPOINTS];
93  uint16 breakpointsCount;
94 } AsebaVMState;
95 
96 // Macros to work with masks
97 
99 #define AsebaMaskSet(v, m) ((v) |= (m))
100 #define AsebaMaskClear(v, m) ((v) &= (~(m)))
102 #define AsebaMaskIsSet(v, m) (((v) & (m)) != 0)
104 #define AsebaMaskIsClear(v, m) (((v) & (m)) == 0)
106 
107 
108 // Functions provided by aseba-core
109 
110 
116 void AsebaVMInit(AsebaVMState *vm);
117 
120 
125 
130 uint16 AsebaVMRun(AsebaVMState *vm, uint16 stepsLimit);
131 
134 void AsebaVMDebugMessage(AsebaVMState *vm, uint16 id, uint16 *data, uint16 dataLength);
135 
137 void AsebaVMEmitNodeSpecificError(AsebaVMState *vm, const char* message);
138 
140 uint16 AsebaVMShouldDropPacket(AsebaVMState *vm, uint16 source, const uint8* data);
141 
142 // Functions implemented outside by the glue/transport layer
143 
146 void AsebaSendMessage(AsebaVMState *vm, uint16 id, const void *data, uint16 size);
147 
148 #ifdef __BIG_ENDIAN__
149 
151 void AsebaSendMessageWords(AsebaVMState *vm, uint16 type, const uint16* data, uint16 count);
152 #else
153  #define AsebaSendMessageWords(vm,type,data,size) AsebaSendMessage(vm,type,data,(size)*2)
154 #endif
155 
157 void AsebaSendVariables(AsebaVMState *vm, uint16 start, uint16 length);
158 
161 
164 
167 
170 
173 
175 #ifdef DISABLE_WEAK_CALLBACKS
176 static void AsebaVMRunCB(AsebaVMState *vm) {}
177 #else // DISABLE_WEAK_CALLBACKS
178 void __attribute__((weak)) AsebaVMRunCB(AsebaVMState *vm);
179 #endif // DISABLE_WEAK_CALLBACKS
180 
182 #ifdef DISABLE_WEAK_CALLBACKS
183 static void AsebaVMResetCB(AsebaVMState *vm) {}
184 #else // DISABLE_WEAK_CALLBACKS
185 void __attribute__((weak)) AsebaVMResetCB(AsebaVMState *vm);
186 #endif // DISABLE_WEAK_CALLBACKS
187 
190 #ifdef DISABLE_WEAK_CALLBACKS
191 static void AsebaVMErrorCB(AsebaVMState *vm, const char* message) {}
192 #else // DISABLE_WEAK_CALLBACKS
193 void __attribute__((weak)) AsebaVMErrorCB(AsebaVMState *vm, const char* message);
194 #endif // DISABLE_WEAK_CALLBACKS
195 
196 
197 // Function optionally implemented
198 
199 #ifdef ASEBA_ASSERT
200 
202 typedef enum
203 {
204  ASEBA_ASSERT_UNKNOWN = 0,
205  ASEBA_ASSERT_UNKNOWN_UNARY_OPERATOR,
206  ASEBA_ASSERT_UNKNOWN_BINARY_OPERATOR,
207  ASEBA_ASSERT_UNKNOWN_BYTECODE,
208  ASEBA_ASSERT_STACK_OVERFLOW,
209  ASEBA_ASSERT_STACK_UNDERFLOW,
210  ASEBA_ASSERT_OUT_OF_VARIABLES_BOUNDS,
211  ASEBA_ASSERT_OUT_OF_BYTECODE_BOUNDS,
212  ASEBA_ASSERT_STEP_OUT_OF_RUN,
213  ASEBA_ASSERT_BREAKPOINT_OUT_OF_BYTECODE_BOUNDS,
214  ASEBA_ASSERT_EMIT_BUFFER_TOO_LONG,
216 
218 void AsebaAssert(AsebaVMState *vm, AsebaAssertReason reason);
219 
220 #endif /* ASEBA_ASSERT */
221 
224 #ifdef __cplusplus
225 } /* closing brace for extern "C" */
226 #endif
227 
228 #endif
sint16 * variables
variables of size variableCount
Definition: vm.h:80
uint16 * bytecode
bytecode space of size bytecodeSize
Definition: vm.h:76
This structure contains the state of the Aseba VM.
Definition: vm.h:69
void AsebaVMDebugMessage(AsebaVMState *vm, uint16 id, uint16 *data, uint16 dataLength)
Execute a debug action from a debug message.
Definition: vm.c:734
void AsebaAssert(AsebaVMState *vm, AsebaAssertReason reason)
If ASEBA_ASSERT is defined, this function is called when an error arise.
uint16 AsebaVMGetEventAddress(AsebaVMState *vm, uint16 event)
Return the starting address of an event, or 0 if the event is not handled.
Definition: vm.c:55
void AsebaVMEmitNodeSpecificError(AsebaVMState *vm, const char *message)
Can be called by glue code (including native functions), to stop vm and emit a node specific error...
Definition: vm.c:534
void AsebaResetIntoBootloader(AsebaVMState *vm)
Called by AsebaVMDebugMessage when VM must restart the node and enter to the bootloader, write an empty function to leave feature unsupported.
uint16 bytecodeSize
total amount of bytecode space
Definition: vm.h:75
AsebaAssertReason
Possible causes of AsebaAssert.
Definition: vm.h:202
void AsebaSendVariables(AsebaVMState *vm, uint16 start, uint16 length)
Called by AsebaVMDebugMessage when some variables must be sent efficiently.
void AsebaVMInit(AsebaVMState *vm)
Setup the execution status of the VM.
Definition: vm.c:44
uint16 AsebaVMSetupEvent(AsebaVMState *vm, uint16 event)
Setup VM to execute an event.
Definition: vm.c:68
unsigned char uint8
8 bits unsigned integer
Definition: types.h:38
uint16 variablesSize
total amount of variables space
Definition: vm.h:79
maximum number of simultaneous breakpoints the target supports
Definition: vm.h:56
void AsebaNativeFunction(AsebaVMState *vm, uint16 id)
Called by AsebaStep to perform a native function call.
unsigned short uint16
16 bits unsigned integer
Definition: types.h:36
signed short sint16
16 bits signed integer
Definition: types.h:35
void AsebaSendDescription(AsebaVMState *vm)
Called by AsebaVMDebugMessage when VM must send its description on the network.
uint16 AsebaVMRun(AsebaVMState *vm, uint16 stepsLimit)
Run the VM depending on the current execution mode.
Definition: vm.c:651
sint16 * stack
execution stack of size stackSize
Definition: vm.h:84
void AsebaWriteBytecode(AsebaVMState *vm)
Called by AsebaVMDebugMessage when VM must write its bytecode to flash, write an empty function to le...
void __attribute__((weak)) AsebaVMRunCB(AsebaVMState *vm)
Called by AsebaVMDebugMessage when VM is going to be run.
void AsebaPutVmToSleep(AsebaVMState *vm)
Called by AsebaVMDebugMessage when VM must put to node in deep sleep.
uint16 AsebaVMShouldDropPacket(AsebaVMState *vm, uint16 source, const uint8 *data)
Return non-zero if VM will ignore the packet, 0 otherwise.
Definition: vm.c:883
void AsebaSendMessage(AsebaVMState *vm, uint16 id, const void *data, uint16 size)
Called by AsebaStep if there is a message (not an user event) to send.
uint16 stackSize
depth of execution stack
Definition: vm.h:83