nabaztag_hack_kit 0.1.0.beta3 → 0.1.0.beta8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -2
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +2 -2
  5. data/CHANGELOG.md +17 -2
  6. data/LICENSE +21 -0
  7. data/README.md +115 -44
  8. data/Rakefile +2 -6
  9. data/bytecode/{Commands.md → _docs/commands.md} +0 -0
  10. data/bytecode/{Docs.md → _docs/grammar.md} +0 -0
  11. data/bytecode/_docs/grammar.pdf +0 -0
  12. data/bytecode/{src → _original}/README.md +0 -0
  13. data/bytecode/{src → _original}/lib/arp.mtl +0 -0
  14. data/bytecode/{src → _original}/lib/cfg.mtl +0 -0
  15. data/bytecode/{src → _original}/lib/choreos.mtl +0 -0
  16. data/bytecode/{src → _original}/lib/dhcp.mtl +0 -0
  17. data/bytecode/{src → _original}/lib/dns.mtl +0 -0
  18. data/bytecode/{src → _original}/lib/http.mtl +0 -0
  19. data/bytecode/{src → _original}/lib/tcp.mtl +0 -0
  20. data/bytecode/{src → _original}/lib/udp.mtl +0 -0
  21. data/bytecode/{src → _original}/lib/util.mtl +0 -0
  22. data/bytecode/{src → _original}/lib/var.mtl +0 -0
  23. data/bytecode/{src → _original}/lib/wav.mtl +0 -0
  24. data/bytecode/{src → _original}/lib/wifi.mtl +0 -0
  25. data/bytecode/{src → _original}/nominal-ping.mtl +0 -0
  26. data/bytecode/main.mtl +11 -11
  27. data/bytecode/test/test.mtl +8 -8
  28. data/examples/basic/Gemfile +1 -3
  29. data/examples/basic/config.ru +14 -4
  30. data/examples/record/Gemfile +0 -5
  31. data/examples/record/{config.ru.example → config.ru} +2 -2
  32. data/examples/record/server.rb +0 -2
  33. data/ext/Makefile +10 -0
  34. data/ext/bin/mtl_comp +29 -0
  35. data/{bytecode → ext}/bin/mtl_merge +0 -0
  36. data/ext/bin/mtl_simu +29 -0
  37. data/{bytecode/src/mtl → ext}/extconf.rb +0 -0
  38. data/ext/mtl_linux/.gitignore +7 -0
  39. data/ext/mtl_linux/LICENSE +21 -0
  40. data/ext/mtl_linux/Makefile +116 -0
  41. data/ext/mtl_linux/MetalC.project +196 -0
  42. data/ext/mtl_linux/conf.bin +0 -0
  43. data/{bytecode/src/mtl → ext/mtl_linux}/conf.bin.sans_password +0 -0
  44. data/{bytecode/src/mtl → ext/mtl_linux}/config.txt +0 -0
  45. data/ext/mtl_linux/inc/log.h +21 -0
  46. data/{bytecode/src/mtl → ext/mtl_linux/inc}/properties.h +0 -0
  47. data/{bytecode/src/mtl/linux_simuaudio.h → ext/mtl_linux/inc/simu/linux/simuaudio.h} +0 -0
  48. data/{bytecode/src/mtl/linux_simunet.h → ext/mtl_linux/inc/simu/linux/simunet.h} +0 -0
  49. data/{bytecode/src/mtl → ext/mtl_linux/inc/simu/win}/simuaudio.h +18 -18
  50. data/{bytecode/src/mtl → ext/mtl_linux/inc/simu/win}/simunet.h +0 -0
  51. data/ext/mtl_linux/inc/vcomp/compiler.h +200 -0
  52. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/file.h +11 -11
  53. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/filesystem.h +0 -0
  54. data/ext/mtl_linux/inc/vcomp/interpreter.h +121 -0
  55. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/memory.h +120 -92
  56. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/param.h +0 -0
  57. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/parser.h +27 -27
  58. data/ext/mtl_linux/inc/vcomp/prodbuffer.h +42 -0
  59. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/resource.h +1 -1
  60. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/terminal.h +8 -8
  61. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/util.h +7 -7
  62. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vaudio.h +0 -0
  63. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vbc.h +1 -1
  64. data/ext/mtl_linux/inc/vm/vbc_str.h +167 -0
  65. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vinterp.h +0 -0
  66. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vloader.h +0 -0
  67. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vlog.h +0 -0
  68. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vmem.h +0 -0
  69. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vnet.h +0 -0
  70. data/ext/mtl_linux/nominal.mtl +3959 -0
  71. data/ext/mtl_linux/src/comp/main.cpp +123 -0
  72. data/ext/mtl_linux/src/simu/dumpbc.c +2566 -0
  73. data/ext/mtl_linux/src/simu/linux/main.cpp +288 -0
  74. data/ext/mtl_linux/src/simu/linux/simu.c +309 -0
  75. data/ext/mtl_linux/src/simu/linux/simuaudio.c +44 -0
  76. data/ext/mtl_linux/src/simu/linux/simunet.c +707 -0
  77. data/ext/mtl_linux/src/simu/log.c +340 -0
  78. data/ext/mtl_linux/src/simu/properties.c +368 -0
  79. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/GTKANAL.H +0 -0
  80. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/LAYER3.C +0 -0
  81. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/TABINIT.C +0 -0
  82. data/ext/mtl_linux/src/simu/win/mp3/common.c +302 -0
  83. data/ext/mtl_linux/src/simu/win/mp3/dct64_i386.c +316 -0
  84. data/ext/mtl_linux/src/simu/win/mp3/decode_i386.c +160 -0
  85. data/ext/mtl_linux/src/simu/win/mp3/huffman.h +332 -0
  86. data/ext/mtl_linux/src/simu/win/mp3/interface.c +276 -0
  87. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/mpg123.h +34 -30
  88. data/ext/mtl_linux/src/simu/win/mp3/mpglib.h +47 -0
  89. data/ext/mtl_linux/src/simu/win/simu.c +819 -0
  90. data/ext/mtl_linux/src/simu/win/simuaudio.c +787 -0
  91. data/ext/mtl_linux/src/simu/win/simunet.c +489 -0
  92. data/ext/mtl_linux/src/vcomp/bootstrap.cpp +95 -0
  93. data/ext/mtl_linux/src/vcomp/compiler.cpp +587 -0
  94. data/ext/mtl_linux/src/vcomp/compiler_file.cpp +1253 -0
  95. data/ext/mtl_linux/src/vcomp/compiler_prog.cpp +437 -0
  96. data/ext/mtl_linux/src/vcomp/compiler_term.cpp +1500 -0
  97. data/ext/mtl_linux/src/vcomp/compiler_type.cpp +1238 -0
  98. data/ext/mtl_linux/src/vcomp/compiler_var.cpp +502 -0
  99. data/ext/mtl_linux/src/vcomp/file.cpp +100 -0
  100. data/ext/mtl_linux/src/vcomp/interpreter.cpp +93 -0
  101. data/ext/mtl_linux/src/vcomp/memory.cpp +313 -0
  102. data/ext/mtl_linux/src/vcomp/parser.cpp +584 -0
  103. data/ext/mtl_linux/src/vcomp/parser_xml.cpp +137 -0
  104. data/{bytecode/src/mtl → ext/mtl_linux/src}/vcomp/prodbuffer.cpp +57 -36
  105. data/ext/mtl_linux/src/vcomp/stdlib_core.cpp +130 -0
  106. data/ext/mtl_linux/src/vcomp/terminal.cpp +80 -0
  107. data/{bytecode/src/mtl → ext/mtl_linux/src}/vcomp/util.cpp +18 -11
  108. data/ext/mtl_linux/src/vm/vaudio.c +830 -0
  109. data/ext/mtl_linux/src/vm/vinterp.c +1780 -0
  110. data/ext/mtl_linux/src/vm/vloader.c +139 -0
  111. data/ext/mtl_linux/src/vm/vlog.c +782 -0
  112. data/ext/mtl_linux/src/vm/vmem.c +461 -0
  113. data/ext/mtl_linux/src/vm/vnet.c +298 -0
  114. data/{bytecode/src/mtl → ext/mtl_linux}/utils/correct_const.sh +0 -0
  115. data/lib/nabaztag_hack_kit/server.rb +4 -7
  116. data/lib/nabaztag_hack_kit/version.rb +1 -1
  117. data/lib/nabaztag_hack_kit.rb +2 -2
  118. data/nabaztag_hack_kit.gemspec +10 -11
  119. data/spec/integration/server_spec.rb +3 -3
  120. data/spec/spec_helper.rb +2 -0
  121. data/spec/unit/message_spec.rb +12 -18
  122. data/spec/unit/server_spec.rb +13 -10
  123. metadata +145 -137
  124. data/bytecode/bin/mtl_comp +0 -50
  125. data/bytecode/bin/mtl_simu +0 -49
  126. data/bytecode/src/mtl/Makefile +0 -42
  127. data/bytecode/src/mtl/README.md +0 -13
  128. data/bytecode/src/mtl/bc.cpp +0 -1891
  129. data/bytecode/src/mtl/dumpbc.c +0 -2566
  130. data/bytecode/src/mtl/linux_simu.c +0 -271
  131. data/bytecode/src/mtl/linux_simuaudio.c +0 -16
  132. data/bytecode/src/mtl/linux_simunet.c +0 -620
  133. data/bytecode/src/mtl/log.c +0 -297
  134. data/bytecode/src/mtl/log.h +0 -20
  135. data/bytecode/src/mtl/main_compiler.cpp +0 -104
  136. data/bytecode/src/mtl/main_simu.cpp +0 -221
  137. data/bytecode/src/mtl/mp3/common.c +0 -265
  138. data/bytecode/src/mtl/mp3/dct64_i386.c +0 -316
  139. data/bytecode/src/mtl/mp3/decode_i386.c +0 -155
  140. data/bytecode/src/mtl/mp3/huffman.h +0 -332
  141. data/bytecode/src/mtl/mp3/interface.c +0 -258
  142. data/bytecode/src/mtl/mp3/mpglib.h +0 -44
  143. data/bytecode/src/mtl/properties.c +0 -293
  144. data/bytecode/src/mtl/simu.c +0 -750
  145. data/bytecode/src/mtl/simuaudio.c +0 -662
  146. data/bytecode/src/mtl/simunet.c +0 -400
  147. data/bytecode/src/mtl/vaudio.c +0 -677
  148. data/bytecode/src/mtl/vbc_str.h +0 -166
  149. data/bytecode/src/mtl/vcomp/Makefile +0 -29
  150. data/bytecode/src/mtl/vcomp/bootstrap.cpp +0 -89
  151. data/bytecode/src/mtl/vcomp/compiler.cpp +0 -470
  152. data/bytecode/src/mtl/vcomp/compiler.h +0 -200
  153. data/bytecode/src/mtl/vcomp/compiler_file.cpp +0 -929
  154. data/bytecode/src/mtl/vcomp/compiler_prog.cpp +0 -250
  155. data/bytecode/src/mtl/vcomp/compiler_term.cpp +0 -1053
  156. data/bytecode/src/mtl/vcomp/compiler_type.cpp +0 -872
  157. data/bytecode/src/mtl/vcomp/compiler_var.cpp +0 -289
  158. data/bytecode/src/mtl/vcomp/file.cpp +0 -79
  159. data/bytecode/src/mtl/vcomp/interpreter.cpp +0 -85
  160. data/bytecode/src/mtl/vcomp/interpreter.h +0 -121
  161. data/bytecode/src/mtl/vcomp/memory.cpp +0 -241
  162. data/bytecode/src/mtl/vcomp/parser.cpp +0 -427
  163. data/bytecode/src/mtl/vcomp/parser_xml.cpp +0 -124
  164. data/bytecode/src/mtl/vcomp/prodbuffer.h +0 -42
  165. data/bytecode/src/mtl/vcomp/stdlib_core.cpp +0 -122
  166. data/bytecode/src/mtl/vcomp/terminal.cpp +0 -73
  167. data/bytecode/src/mtl/vinterp.c +0 -1349
  168. data/bytecode/src/mtl/vloader.c +0 -127
  169. data/bytecode/src/mtl/vlog.c +0 -589
  170. data/bytecode/src/mtl/vmem.c +0 -424
  171. data/bytecode/src/mtl/vnet.c +0 -255
  172. data/examples/basic/Procfile +0 -2
  173. data/examples/basic/server.rb +0 -14
@@ -0,0 +1,782 @@
1
+ // VLISP Virtual Machine - 2006 - by Sylvain Huet
2
+ // Lowcost IS Powerfull
3
+
4
+ #include <string.h>
5
+
6
+ #include"vmem.h"
7
+ #include"vloader.h"
8
+ #ifdef VSIMU
9
+ #include <stdlib.h>
10
+ #include<stdio.h>
11
+ #include<time.h>
12
+ #include <sys/time.h>
13
+ #endif
14
+ #ifdef VREAL
15
+ #include "ML674061.h"
16
+ #include "common.h"
17
+ #include "irq.h"
18
+ #include "spi.h"
19
+ #include "led.h"
20
+ #include "mem.h"
21
+ #include "uart.h"
22
+ #include "debug.h"
23
+ #include "usbh.h"
24
+
25
+ #include "delay.h"
26
+ #include "audio.h"
27
+ #include "motor.h"
28
+ #include "usbctrl.h"
29
+ #include "ml60842.h"
30
+ #include "hcdmem.h"
31
+ #include "hcd.h"
32
+ #include "inarm.h"
33
+ #include "i2c.h"
34
+ #include "rt2501usb.h"
35
+ #include "mem.h"
36
+ #include<stdio.h>
37
+ #include<string.h>
38
+
39
+ #include"led.h"
40
+ #include"motor.h"
41
+ #include"delay.h"
42
+ #include"rfid.h"
43
+ #endif
44
+ #include"vlog.h"
45
+
46
+ void logSecho(int p,int nl)
47
+ {
48
+ if (p==NIL)
49
+ {
50
+ consolestr("NIL");
51
+ }
52
+ else
53
+ {
54
+ consolebin((unsigned char*)VSTARTBIN(VALTOPNT(p)),VSIZEBIN(VALTOPNT(p)));
55
+ }
56
+ if (nl)
57
+ {
58
+ consolestr(ENDLINE);
59
+ }
60
+ }
61
+
62
+ void logIecho(int i,int nl)
63
+ {
64
+ if (i==NIL)
65
+ {
66
+ consolestr("NIL");
67
+ }
68
+ else
69
+ {
70
+ consoleint(VALTOINT(i));
71
+ }
72
+ if (nl)
73
+ {
74
+ consolestr(ENDLINE);
75
+ }
76
+ }
77
+
78
+ extern int currentop;
79
+ void logGC()
80
+ {
81
+ consolestr("#GC : sp=");
82
+ consoleint(-vmem_stack);
83
+ consolestr(" hp=");
84
+ consoleint(vmem_heapindex);
85
+ consolestr(" used=");
86
+ consoleint((vmem_heapindex-vmem_stack)*100/VMEM_LENGTH);
87
+ consolestr("%"ENDLINE);
88
+ consolestr(" b:");
89
+ consolehx(*((int*)vmem_heap));
90
+ consolestr(" bc:");
91
+ consolehx(*((int*)bytecode));
92
+ consolestr(" st:");
93
+ consolehx(vmem_start);
94
+ consolestr(" op:");
95
+ consolehx(currentop);
96
+ consolestr(ENDLINE);
97
+
98
+ }
99
+
100
+
101
+ // pour le firmware, le "fichier" ouvert est toujours l'eeprom
102
+
103
+ int sysLoad(char *dst,int i,int ldst,char *filename,int j,int len)
104
+ {
105
+ #ifdef VSIMU
106
+ FILE *f;
107
+ if ((j<0)||(i<0)||(len<=0))
108
+ {
109
+ return 0;
110
+ }
111
+ if (i+len>ldst)
112
+ {
113
+ len=ldst-i;
114
+ }
115
+ if (len<=0)
116
+ {
117
+ return 0;
118
+ }
119
+ f=fopen(filename,"rb");
120
+ if (!f)
121
+ {
122
+ return 0;
123
+ }
124
+ fseek(f,j,SEEK_SET);
125
+ len=fread(dst,1,len,f);
126
+ fclose(f);
127
+ return len;
128
+ #endif
129
+ #ifdef VREAL
130
+ /* set_vlsi_volume(0); //volume on 8bits, 0x00 => maximum
131
+ encode_adpcm((UBYTE*)dst+i,(ldst-i-256)>>8, j);
132
+ return 0;
133
+ */
134
+ if ((j<0)||(i<0)||(len<=0))
135
+ {
136
+ return 0;
137
+ }
138
+ if (i+len>ldst)
139
+ {
140
+ len=ldst-i;
141
+ }
142
+ if (len<=0)
143
+ {
144
+ return 0;
145
+ }
146
+ if (j+len>4096)
147
+ {
148
+ len=4096-j;
149
+ }
150
+ if (len<=0)
151
+ {
152
+ return 0;
153
+ }
154
+ read_uc_flash(j,(unsigned char*)dst,len);
155
+ return len;
156
+ #endif
157
+ }
158
+
159
+ uchar buffer_temp[4096];
160
+
161
+ // pour le firmware, le "fichier" ouvert est toujours l'eeprom
162
+ int sysSave(char *dst,int i,int ldst,char *filename,int j,int len)
163
+ {
164
+ #ifdef VSIMU
165
+ FILE *f;
166
+ if ((j<0)||(i<0)||(len<=0))
167
+ {
168
+ return 0;
169
+ }
170
+ if (i+len>ldst)
171
+ {
172
+ len=ldst-i;
173
+ }
174
+ if (len<=0)
175
+ {
176
+ return 0;
177
+ }
178
+ f=fopen(filename,"rb+");
179
+ if (!f)
180
+ {
181
+ f=fopen(filename,"wb+");
182
+ }
183
+ if (!f)
184
+ {
185
+ return 0;
186
+ }
187
+ fseek(f,j,SEEK_SET);
188
+ len=fwrite(dst,1,len,f);
189
+ fclose(f);
190
+ return len;
191
+ #endif
192
+ #ifdef VREAL
193
+ if ((j<0)||(i<0)||(len<=0))
194
+ {
195
+ return 0;
196
+ }
197
+ if (i+len>ldst)
198
+ {
199
+ len=ldst-i;
200
+ }
201
+ if (len<=0)
202
+ {
203
+ return 0;
204
+ }
205
+ if (j+len>4096)
206
+ {
207
+ len=4096-j;
208
+ }
209
+ if (len<=0)
210
+ {
211
+ return 0;
212
+ }
213
+ __disable_interrupt();
214
+ write_uc_flash(j,(unsigned char*)dst,len,buffer_temp);
215
+ __enable_interrupt();
216
+ return len;
217
+ #endif
218
+ }
219
+
220
+ static int firstTimeSet = 0;
221
+ static struct timeval firstTime;
222
+
223
+ int sysTimems()
224
+ {
225
+ #ifdef VSIMU
226
+ int res;
227
+ struct timeval tv;
228
+ if (firstTimeSet)
229
+ {
230
+ gettimeofday(&tv, NULL);
231
+ res = (tv.tv_sec - firstTime.tv_sec) * 1000;
232
+ res += (tv.tv_usec - firstTime.tv_usec) / 1000;
233
+ }
234
+ else
235
+ {
236
+ gettimeofday(&firstTime, NULL);
237
+ firstTimeSet = 1;
238
+ res = 0;
239
+ }
240
+ return res;
241
+ #endif // ! VSIMU
242
+ #ifdef VREAL
243
+ return counter_timer;
244
+ #endif
245
+ }
246
+
247
+ int sysTime()
248
+ {
249
+ #ifdef VSIMU
250
+ int res;
251
+ static int firstTimeSet = 0;
252
+ static struct timeval firstTime;
253
+ struct timeval tv;
254
+ if (firstTimeSet)
255
+ {
256
+ gettimeofday(&tv, NULL);
257
+ res = (tv.tv_sec - firstTime.tv_sec);
258
+ res += (tv.tv_usec >= firstTime.tv_usec) ? 0 : -1;
259
+ }
260
+ else
261
+ {
262
+ gettimeofday(&firstTime, NULL);
263
+ firstTimeSet = 1;
264
+ res = 0;
265
+ }
266
+ return res;
267
+ #endif
268
+ #ifdef VREAL
269
+ return counter_timer_s;
270
+ #endif
271
+ }
272
+
273
+
274
+ int rndval;
275
+
276
+ // retourne une valeur al�atoire entre 0 et 65535
277
+ int sysRand()
278
+ {
279
+ rndval=rndval*0x1234567+11;
280
+ return (rndval>>8)&0xffff;
281
+ }
282
+ void sysSrand(int seed)
283
+ {
284
+ rndval=seed;
285
+ }
286
+
287
+
288
+ void sysCpy(char *dst,int i,int ldst,char *src,int j,int lsrc,int len)
289
+ {
290
+ if ((i<0)||(j<0)||(len<=0))
291
+ {
292
+ return;
293
+ }
294
+ if (i+len>ldst)
295
+ {
296
+ len=ldst-i;
297
+ }
298
+ if (len<=0)
299
+ {
300
+ return;
301
+ }
302
+ if (j+len>lsrc)
303
+ {
304
+ len=lsrc-j;
305
+ }
306
+ if (len<=0)
307
+ {
308
+ return;
309
+ }
310
+ dst+=i;
311
+ src+=j;
312
+ while((len--)>0)
313
+ {
314
+ *(dst++)=*(src++);
315
+ }
316
+ }
317
+
318
+ int sysCmp(char *dst,int i,int ldst,char *src,int j,int lsrc,int len)
319
+ {
320
+ if ((i<0)||(j<0)||(len<=0))
321
+ {
322
+ return 0;
323
+ }
324
+ if ((i+len>ldst)&&(j+len>lsrc))
325
+ {
326
+ if (ldst-i>lsrc-j)
327
+ {
328
+ len=ldst-i;
329
+ }
330
+ else
331
+ {
332
+ len=lsrc-j;
333
+ }
334
+ }
335
+ dst+=i;
336
+ src+=j;
337
+ while((len--)>0) if (((unsigned char)*dst)>((unsigned char)*src))
338
+ {
339
+ return 1;
340
+ }
341
+ else if (((unsigned char)*(dst++))<((unsigned char)*(src++)))
342
+ {
343
+ return -1;
344
+ }
345
+ return 0;
346
+ }
347
+
348
+ int mystrcmp(char *dst,char *src,int len)
349
+ {
350
+ while((len--)>0) if ((*(dst++))!=(*(src++)))
351
+ {
352
+ return 1;
353
+ }
354
+ return 0;
355
+ }
356
+
357
+ void mystrcpy(char *dst,char *src,int len)
358
+ {
359
+ while((len--)>0)
360
+ {
361
+ *(dst++)=*(src++);
362
+ }
363
+ }
364
+
365
+ int sysFind(char *dst,int i,int ldst,char *src,int j,int lsrc,int len)
366
+ {
367
+ if ((j<0)||(j+len>lsrc))
368
+ {
369
+ return NIL;
370
+ }
371
+ src+=j;
372
+ if (i<0)
373
+ {
374
+ i=0;
375
+ }
376
+ while(i+len<=ldst)
377
+ {
378
+ if (!mystrcmp(dst+i,src,len))
379
+ {
380
+ return INTTOVAL(i);
381
+ }
382
+ i++;
383
+ }
384
+ return NIL;
385
+ }
386
+
387
+ int sysFindrev(char *dst,int i,int ldst,char *src,int j,int lsrc,int len)
388
+ {
389
+ if ((j<0)||(j+len>lsrc))
390
+ {
391
+ return NIL;
392
+ }
393
+ src+=j;
394
+ if(i+len>ldst)
395
+ {
396
+ i=ldst-len;
397
+ }
398
+ while(i>=0)
399
+ {
400
+ if (!mystrcmp(dst+i,src,len))
401
+ {
402
+ return INTTOVAL(i);
403
+ }
404
+ i--;
405
+ }
406
+ return NIL;
407
+ }
408
+
409
+ int sysStrgetword(unsigned char *src,int len,int ind)
410
+ {
411
+ int n;
412
+ if ((ind<0)||(ind+2>len))
413
+ {
414
+ return -1;
415
+ }
416
+ n=(src[ind]<<8)+src[ind+1];
417
+ return n;
418
+ }
419
+
420
+ void sysStrputword(unsigned char *src,int len,int ind,int val)
421
+ {
422
+ if ((ind<0)||(ind+2>len))
423
+ {
424
+ return;
425
+ }
426
+ src[ind+1]=val;
427
+ val>>=8;
428
+ src[ind]=val;
429
+ }
430
+
431
+ // lecture d'une cha�ne d�cimale (s'arr�te au premier caract�re incorrect)
432
+ int sysAtoi(char* src)
433
+ {
434
+ int x,c,s;
435
+ x=s=0;
436
+ if ((*src)=='-')
437
+ {
438
+ s=1;
439
+ src++;
440
+ }
441
+ while(c=*src++)
442
+ {
443
+ if ((c>='0')&&(c<='9'))
444
+ {
445
+ x=(x*10)+c-'0';
446
+ }
447
+ else
448
+ {
449
+ return (s?(-x):x);
450
+ }
451
+ }
452
+ return (s?(-x):x);
453
+ }
454
+
455
+ // lecture d'une cha�ne hexad�cimale (s'arr�te au premier caract�re incorrect)
456
+ int sysHtoi(char* src)
457
+ {
458
+ int x,c;
459
+ x=0;
460
+ while(c=*src++)
461
+ {
462
+ if ((c>='0')&&(c<='9'))
463
+ {
464
+ x=(x<<4)+c-'0';
465
+ }
466
+ else if ((c>='A')&&(c<='F'))
467
+ {
468
+ x=(x<<4)+c-'A'+10;
469
+ }
470
+ else if ((c>='a')&&(c<='f'))
471
+ {
472
+ x=(x<<4)+c-'a'+10;
473
+ }
474
+ else
475
+ {
476
+ return x;
477
+ }
478
+ }
479
+ return x;
480
+ }
481
+ void sysCtoa(int c)
482
+ {
483
+ unsigned char res[1];
484
+ res[0]=c;
485
+ VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,1)));
486
+ }
487
+
488
+ const int itoarsc[10]=
489
+ {
490
+ 1000000000,100000000,10000000,
491
+ 1000000,100000,10000,
492
+ 1000,100,10,
493
+ 1
494
+ };;
495
+ void sysItoa(int v)
496
+ {
497
+ char res[16];
498
+ int ires=0;
499
+ if (v==0)
500
+ {
501
+ res[ires++]='0';
502
+ }
503
+ else
504
+ {
505
+ int start=1;
506
+ int imul=0;
507
+ if (v<0)
508
+ {
509
+ v=-v;
510
+ res[ires++]='-';
511
+ }
512
+ while(imul<10)
513
+ {
514
+ int k=0;
515
+ while(v>=itoarsc[imul])
516
+ {
517
+ k++;
518
+ v-=itoarsc[imul];
519
+ }
520
+ if ((k)||(!start))
521
+ {
522
+ start=0;
523
+ res[ires++]='0'+k;
524
+ }
525
+ imul++;
526
+ }
527
+ }
528
+
529
+ VPUSH(PNTTOVAL(VMALLOCSTR(res,ires)));
530
+
531
+ }
532
+
533
+ void sysItoh(int v)
534
+ {
535
+ char res[16];
536
+ int ires=0;
537
+ if (v==0)
538
+ {
539
+ res[ires++]='0';
540
+ }
541
+ else
542
+ {
543
+ int start=1;
544
+ int imul=28;
545
+ while(imul>=0)
546
+ {
547
+ int c=(v>>imul)&15;
548
+ if ((c)||(!start))
549
+ {
550
+ start=0;
551
+ res[ires++]=(c<10)?'0'+c:'a'+c-10;
552
+ }
553
+ imul-=4;
554
+ }
555
+ }
556
+ VPUSH(PNTTOVAL(VMALLOCSTR(res,ires)));
557
+ }
558
+
559
+ void sysCtoh(int c)
560
+ {
561
+ unsigned char res[2];
562
+ int v=(c>>4)&15;
563
+ res[0]=(v<10)?'0'+v:'a'+v-10;
564
+ v=c&15;
565
+ res[1]=(v<10)?'0'+v:'a'+v-10;
566
+ VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,2)));
567
+ }
568
+
569
+ void sysItobin2(int c)
570
+ {
571
+ unsigned char res[2];
572
+ res[1]=c;
573
+ c>>=8;
574
+ res[0]=c;
575
+ VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,2)));
576
+ }
577
+
578
+ int sysListswitch(int p,int key)
579
+ {
580
+ while(p!=NIL)
581
+ {
582
+ int q=VALTOPNT(VFETCH(p,0));
583
+ if ((q!=NIL)&&(VFETCH(q,0)==key))
584
+ {
585
+ return VFETCH(q,1);
586
+ }
587
+ p=VALTOPNT(VFETCH(p,1));
588
+ }
589
+ return NIL;
590
+ }
591
+
592
+ int sysListswitchstr(int p,char* key)
593
+ {
594
+ while(p!=NIL)
595
+ {
596
+ int q=VALTOPNT(VFETCH(p,0));
597
+ if (q!=NIL)
598
+ {
599
+ int r=VALTOPNT(VFETCH(q,0));
600
+ if ((r!=NIL)&&(!strcmp(VSTARTBIN(r),key)))
601
+ {
602
+ return VFETCH(q,1);
603
+ }
604
+ }
605
+ p=VALTOPNT(VFETCH(p,1));
606
+ }
607
+ return NIL;
608
+ }
609
+
610
+ void simuSetLed(int i,int val);
611
+ void set_motor_dir(int num_motor, int sens);
612
+ int get_motor_val(int i);
613
+ int getButton();
614
+ int get_button3();
615
+ char* get_rfid();
616
+
617
+ void sysLed(int led,int col)
618
+ {
619
+ #ifdef VSIMU
620
+ simuSetLed(led,col);
621
+ #endif
622
+ #ifdef VREAL
623
+ set_led((UWORD)led,(UWORD)col);
624
+ #endif
625
+ }
626
+
627
+ void sysMotorset(int motor,int sens)
628
+ {
629
+ #ifdef VSIMU
630
+ set_motor_dir(motor,sens);
631
+ #endif
632
+ #ifdef VREAL
633
+ // char buffer[256];
634
+ motor=1+(motor&1);
635
+
636
+ // sprintf(buffer,"setmotor %d sens %d\r\n",motor,sens);
637
+ // consolestr(buffer);
638
+
639
+ if (sens==0)
640
+ {
641
+ stop_motor(motor);
642
+ }
643
+ else
644
+ {
645
+ run_motor(motor,255,(sens>0)?REVERSE:FORWARD/*:REVERSE*/);
646
+ }
647
+ #endif
648
+ }
649
+
650
+ int kmotor[3];
651
+ int kvmotor[3];
652
+
653
+ int sysMotorget(int motor)
654
+ {
655
+ #ifdef VSIMU
656
+ return get_motor_val(motor);
657
+ #endif
658
+ #ifdef VREAL
659
+ // char buffer[256];
660
+ int k,kx;
661
+ motor=1+(motor&1);
662
+ kx=(int)get_motor_position(motor);
663
+ /* k=(int)get_motor_position(motor);
664
+ if (kmotor[motor]!=k)
665
+ {
666
+ kmotor[motor]=k;
667
+ kvmotor[motor]++;
668
+ }
669
+ kx=kvmotor[motor];
670
+ */
671
+
672
+ // sprintf(buffer,"getmotor %d pos %x / %x\r\n",motor,k,kx);
673
+ // if(motor==2)
674
+ // consolestr(buffer);
675
+ return kx;
676
+ #endif
677
+ }
678
+
679
+ int sysButton2()
680
+ {
681
+ #ifdef VSIMU
682
+ return getButton();
683
+ #endif
684
+ #ifdef VREAL
685
+ return push_button_value();
686
+ #endif
687
+ }
688
+
689
+ int sysButton3()
690
+ {
691
+ #ifdef VSIMU
692
+ return get_button3();
693
+ #endif
694
+ #ifdef VREAL
695
+ return 255-get_adc_value();
696
+ #endif
697
+ }
698
+
699
+ char* sysRfidget()
700
+ {
701
+ #ifdef VSIMU
702
+ return get_rfid();
703
+ #endif
704
+ #ifdef VREAL
705
+ return get_rfid_first_device();
706
+ #endif
707
+ }
708
+
709
+ void sysReboot()
710
+ {
711
+ #ifdef VSIMU
712
+ printf("REBOOT NOW.....");
713
+ getchar();
714
+ exit(0);
715
+ #endif
716
+ #ifdef VREAL
717
+ reset_uc();
718
+ #endif
719
+
720
+ }
721
+
722
+ void sysFlash(char* firmware,int len)
723
+ {
724
+ #ifdef VSIMU
725
+ printf("REBOOT AND FLASH NOW.....");
726
+ getchar();
727
+ exit(0);
728
+ #endif
729
+ #ifdef VREAL
730
+ __disable_interrupt();
731
+ flash_uc((unsigned char*)firmware,len,buffer_temp);
732
+ #endif
733
+
734
+ }
735
+
736
+ const uchar inv8[128]=
737
+ {
738
+ 1,171,205,183,57,163,197,239,241,27,61,167,41,19,53,223,225,139,173,151,25,131,165,207,209,251,29,135,9,243,21,191,193,107,141,119,249,99,133,175,177,219,253,103,233,211,245,159,161,75,109,87,217,67,101,143,145,187,221,71,201,179,213,127,129,43,77,55,185,35,69,111,113,155,189,39,169,147,181,95,97,11,45,23,153,3,37,79,81,123,157,7,137,115,149,63,65,235,13,247,121,227,5,47,49,91,125,231,105,83,117,31,33,203,237,215,89,195,229,15,17,59,93,199,73,51,85,255
739
+ };
740
+
741
+ int decode8(uchar* src,int len,uchar key,uchar alpha)
742
+ {
743
+ while(len--)
744
+ {
745
+ uchar v=((*src)-alpha)*key;
746
+ *(src++)=v;
747
+ key=v+v+1;
748
+ }
749
+ return key;
750
+ }
751
+
752
+ int encode8(uchar* src,int len,uchar key,uchar alpha)
753
+ {
754
+ while(len--)
755
+ {
756
+ uchar v=*src;
757
+ *(src++)=alpha+(v*inv8[key>>1]);
758
+ key=v+v+1;
759
+ }
760
+ return key;
761
+ }
762
+
763
+
764
+
765
+ int sysCrypt(char* src,int indexsrc,int len,int lensrc,unsigned int key,int alpha)
766
+ {
767
+ if ((indexsrc<0)||(indexsrc+len>lensrc)||(len<=0))
768
+ {
769
+ return -1;
770
+ }
771
+ return encode8(src+indexsrc,len,key,alpha);
772
+ }
773
+ int sysUncrypt(char* src,int indexsrc,int len,int lensrc,unsigned int key,int alpha)
774
+ {
775
+ if ((indexsrc<0)||(indexsrc+len>lensrc)||(len<=0))
776
+ {
777
+ return -1;
778
+ }
779
+ return decode8(src+indexsrc,len,key,alpha);
780
+ }
781
+
782
+