-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathTinybasicArduino.ino
More file actions
8399 lines (7424 loc) · 172 KB
/
TinybasicArduino.ino
File metadata and controls
8399 lines (7424 loc) · 172 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* $Id: basic.c,v 1.144 2023/07/16 14:17:08 stefan Exp stefan $
*
* Stefan's IoT BASIC interpreter
*
* See the licence file on
* https://github.com/slviajero/tinybasic for copyright/left.
* (GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007)
*
* Author: Stefan Lenz, sl001@serverfabrik.de
*
* The first set of definions define the target.
* - MINGW switches on Windows calls - this is the mingw.org version
* _ MINGW64 like MINGW but with the mingw 64 support
* - MSDOS for MSDOS file access.
* - MAC doesn't need more settings here
* - RASPPI activates wiring code
* - Review hardware-*.h for settings specific Arduino hardware settings
* - HAS* activates or deactives features of the interpreter
* - the extension flags control features and code size
*
* MEMSIZE sets the BASIC main memory to a fixed value,
* if MEMSIZE=0 a heuristic is used based on free heap
* size and architecture parameters
*
* USEMEMINTERFACE controls the way memory is accessed. Don't change
* this here. It is a parameter set by hardware-arduino.h.
* This feature is experimental.
*
*/
#undef MINGW
#undef MSDOS
#undef RASPPI
/*
interpreter feature sets, choose one of the predefines
or undefine all predefines and set the features in custom settings
*/
/*
* BASICFULL: full language set, use this with flash >32kB - ESPs, MKRs, Mega2560, RP2040, UNO R4
* BASICINTEGER: integer BASIC with full language, use this with flash >32kB
* BASICSIMPLE: integer BASIC with reduced language set, 32kB capable - for UNOs with a lot of device drivers
* BASICSIMPLEWITHFLOAT: a small floating point BASIC, 32kB capable, for UNOs - good for UNOs with the need of float
* BASICTINYWITHFLOAT: a floating point tinybasic, if you have 32kB and need complex device drivers
* BASICMINIMAL: minimal language, just Palo Alto plus Arduino I/O, works on 168 with 1kB RAM and 16kB flash
*/
#undef BASICFULL
#undef BASICINTEGER
#define BASICSIMPLE
#undef BASICMINIMAL
#undef BASICSIMPLEWITHFLOAT
#undef BASICTINYWITHFLOAT
/*
* custom settings undef all the the language sets
* when you def here
*/
#define HASAPPLE1
#define HASARDUINOIO
#define HASFILEIO
#define HASTONE
#define HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#define HASVT52
#define HASFLOAT
#define HASGRAPH
#define HASDARTMOUTH
#define HASDARKARTS
#define HASIOT
#define HASMULTIDIM
#define HASSTRINGARRAYS
#define HASTIMER
#define HASEVENTS
#define HASERRORHANDLING
#define HASMSTAB
#define HASARRAYLIMIT
#define HASSTRUCT
/* Palo Alto plus Arduino functions */
#ifdef BASICMINIMAL
#undef HASAPPLE1
#define HASARDUINOIO
#undef HASFILEIO
#undef HASTONE
#undef HASPULSE
#undef HASSTEFANSEXT
#undef HASERRORMSG
#undef HASVT52
#undef HASFLOAT
#undef HASGRAPH
#undef HASDARTMOUTH
#undef HASDARKARTS
#undef HASIOT
#undef HASMULTIDIM
#undef HASSTRINGARRAYS
#undef HASTIMER
#undef HASEVENTS
#undef HASERRORHANDLING
#undef HASMSTAB
#undef HASARRAYLIMIT
#undef HASSTRUCT
#endif
/* all features minus float and tone */
#ifdef BASICINTEGER
#define HASAPPLE1
#define HASARDUINOIO
#define HASFILEIO
#define HASTONE
#define HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#define HASVT52
#undef HASFLOAT
#define HASGRAPH
#define HASDARTMOUTH
#define HASDARKARTS
#define HASIOT
#define HASMULTIDIM
#define HASSTRINGARRAYS
#define HASTIMER
#define HASEVENTS
#define HASERRORHANDLING
#define HASMSTAB
#define HASARRAYLIMIT
#define HASSTRUCT
#endif
/* a simple integer basic for small systems (UNO etc) */
#ifdef BASICSIMPLE
#define HASAPPLE1
#define HASARDUINOIO
#define HASFILEIO
#define HASTONE
#define HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#define HASVT52
#undef HASFLOAT
#undef HASGRAPH
#define HASDARTMOUTH
#undef HASDARKARTS
#define HASIOT
#undef HASMULTIDIM
#undef HASSTRINGARRAYS
#define HASTIMER
#define HASEVENTS
#define HASERRORHANDLING
#undef HASMSTAB
#undef HASARRAYLIMIT
#undef HASSTRUCT
#endif
/* all features activated */
#ifdef BASICFULL
#define HASAPPLE1
#define HASARDUINOIO
#define HASFILEIO
#define HASTONE
#define HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#define HASVT52
#define HASFLOAT
#define HASGRAPH
#define HASDARTMOUTH
#define HASDARKARTS
#define HASIOT
#define HASMULTIDIM
#define HASSTRINGARRAYS
#define HASTIMER
#define HASEVENTS
#define HASERRORHANDLING
#define HASMSTAB
#define HASARRAYLIMIT
#define HASSTRUCT
#endif
/* a simple BASIC with float support */
#ifdef BASICSIMPLEWITHFLOAT
#define HASAPPLE1
#define HASARDUINOIO
#undef HASFILEIO
#undef HASTONE
#undef HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#undef HASVT52
#define HASFLOAT
#undef HASGRAPH
#define HASDARTMOUTH
#undef HASDARKARTS
#undef HASIOT
#undef HASMULTIDIM
#undef HASSTRINGARRAYS
#undef HASTIMER
#undef HASEVENTS
#undef HASERRORHANDLING
#undef HASMSTAB
#undef HASARRAYLIMIT
#undef HASSTRUCT
#endif
/* a Tinybasic with float support */
#ifdef BASICTINYWITHFLOAT
#define HASAPPLE1
#define HASARDUINOIO
#undef HASFILEIO
#undef HASTONE
#undef HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#undef HASVT52
#define HASFLOAT
#undef HASGRAPH
#undef HASDARTMOUTH
#undef HASDARKARTS
#undef HASIOT
#undef HASMULTIDIM
#undef HASSTRINGARRAYS
#undef HASTIMER
#undef HASEVENTS
#undef HASERRORHANDLING
#undef HASMSTAB
#undef HASARRAYLIMIT
#undef HASSTRUCT
#endif
/*
* hardcoded memory size, set 0 for automatic malloc, don't redefine this beyond this point
*/
#define MEMSIZE 0
/* debug mode switch, set to 1 for hard debug mode with many messages */
#define DEBUG 0
/*
* Language feature dependencies
*
* Dartmouth and darkarts needs the heap which is in Apple 1
* IoT needs strings and the heap, also Apple 1
*
* String arrays need multi dimensional capabilities
*
* The structured language set needs ELSE from STEFANSEXT
*
*/
#if defined(HASDARTMOUTH) || defined(HASDARKARTS) || defined(HASIOT)
#define HASAPPLE1
#endif
#if defined(HASSTRINGARRAYS)
#define HASMULTIDIM
#endif
#ifdef HASSTRUCT
#define HASSTEFANSEXT
#endif
/*
* if there are many commands we need the token extension i.e. 2 byte tokens
* #undef HASLONGTOKENS
* this is normally done in basic.h where the tokens are set
*/
/*
* the core basic language headers including some Arduino device stuff
*/
#include "basic.h"
/*
* Hardware dependend definitions code are isolated in hardware-*.h
* Currently there are two versions
* hardware-arduino.h contains all platforms compiled in the Arduino IDE
* (ESP8266, ESP32, AVR, MEGAAVR, SAM*, RP2040)
* hardware-posix.h contains all platforms compiled in gcc with a POSIX OS
* (Mac, Raspberry, Windows/MINGW) plus rudimentary MSDOS with tc2.0. The
* latter will be removed soon.
*/
#ifdef ARDUINO
#include "hardware-arduino.h"
#else
#include "hardware-posix.h"
#endif
/*
*
* BASIC timer stuff, this is a core interpreter function now
*
*/
/*
* the byield function is called after every statement
* it allows four levels of background tasks.
*
* fastticker() is used to trigger all fast timing functions of
* the core interpreter. It can also be used to implement a
* "tone without timer solution"
*
* yieldfunction() triggers a 32ms timer which calls network
* and USB handling functions
*
* longyielfunction() is a 1s timer for long termin maintance
* functions
*
* yieldscheduler() is the hardware scheduler of some platforms
*
*/
void byield() {
/* the fast ticker for all fast timing functions */
fastticker();
/* the loop function for non BASIC stuff */
bloop();
#if defined(BASICBGTASK)
/* yield all 32 milliseconds */
if (millis()-lastyield > YIELDINTERVAL-1) {
yieldfunction();
lastyield=millis();
}
/* yield every second */
if (millis()-lastlongyield > LONGYIELDINTERVAL-1) {
longyieldfunction();
lastlongyield=millis();
}
#endif
/* call the background task scheduler on some platforms implemented in hardware-* */
yieldschedule();
}
/* delay must be implemented to use byield() while waiting */
void bdelay(unsigned long t) {
unsigned long i;
if (t>0) {
i=millis();
while (millis() < i+t) byield();
}
}
/* fastticker is the hook for all timing functions */
void fastticker() {
/* fastticker profiling test code */
#ifdef FASTTICKERPROFILE
fasttickerprofile();
#endif
/* toggle the tone pin */
#ifdef ARDUINOTONEEMULATION
tonetoggle();
#endif
}
/* the millis function for BASIC */
void bmillis() {
number_t m;
/* millis is processed as integer and is cyclic mod maxnumber and not cast to float!! */
m=(number_t) (millis()/(unsigned long)pop() % (unsigned long)maxnum);
push(m);
}
/*
* Determine the possible basic memory size.
* using malloc causes some overhead which can be relevant on the smaller
* boards. set MEMSIZE instead to a static value. In this case ballocmem
* just returns the static MEMSIZE.
*
* if SPIRAMINTERFACE is defined, we use the memory from a serial RAM and dont
* allocate it here at all.
*
*/
#if MEMSIZE == 0 && !(defined(SPIRAMINTERFACE))
address_t ballocmem() {
/* on most platforms we know the free memory for BASIC */
long m=freememorysize();
/* we allocate as much as address_t can handle */
if (m>maxaddr) m=maxaddr;
/* try to allocate the memory */
mem=(mem_t*)malloc(m);
if (mem != 0) return m-1;
/* fallback if allocation failed, 128 bytes */
mem=(mem_t*)malloc(128);
if (mem != 0) return 128; else return 0;
}
#else
address_t ballocmem(){ return MEMSIZE-1; };
#endif
/*
* Layer 0 function - variable handling.
*
* These function access variables and data
*/
/*
* eeprom load / save / autorun functions
* needed for SAVE and LOAD to an EEPROM
* autorun is generic
*/
/* save a file to EEPROM, disabled if we use the EEPROM directly */
void esave() {
#ifndef EEPROMMEMINTERFACE
address_t a=0;
if (top+eheadersize < elength()) {
a=0;
/* EEPROM per default is 255, 0 indicates that there is a program */
eupdate(a++, 0);
/* store the size of the program in byte 1,2,... of the EEPROM*/
z.a=top;
esetnumber(a, addrsize);
a+=addrsize;
while (a < top+eheadersize){
eupdate(a, memread2(a-eheadersize));
a++;
}
eupdate(a++,0);
} else {
error(EOUTOFMEMORY);
er=0;
}
/* needed on I2C EEPROM and other platforms where we buffer */
eflush();
#endif
}
/* load a file from EEPROM, disabled if the use the EEPROM directly */
void eload() {
#ifndef EEPROMMEMINTERFACE
address_t a=0;
if (elength()>0 && (eread(a) == 0 || eread(a) == 1)) {
/* have we stored a program */
a++;
/* how long is it? */
egetnumber(a, addrsize);
top=z.a;
a+=addrsize;
while (a < top+eheadersize){
memwrite2(a-eheadersize, eread(a));
a++;
}
} else {
/* no valid program data is stored */
error(EEEPROM);
}
#endif
}
/* autorun something from EEPROM or a filesystem */
char autorun() {
#if defined(ARDUINOEEPROM) || defined(ARDUINOI2CEEPROM) || ! defined(ARDUINO)
if (eread(0) == 1){ /* autorun from the EEPROM */
egetnumber(1, addrsize);
top=z.a;
st=SERUN;
return 1; /* EEPROM autorun overrules filesystem autorun */
}
#endif
#if defined(FILESYSTEMDRIVER) || !defined(ARDUINO)
/* on a POSIX or DOS platform, we check the command line first and the autoexec */
#ifndef ARDUINO
if (bargc > 0) {
if (ifileopen(bargv[1])) {
xload(bargv[1]);
st=SRUN;
ifileclose();
bnointafterrun=TERMINATEAFTERRUN;
return 1;
}
}
#endif
if (ifileopen("autoexec.bas")) {
xload("autoexec.bas");
st=SRUN;
ifileclose();
return 1;
}
#endif
return 0;
}
#ifdef HASAPPLE1
/*
* bmalloc() allocates a junk of memory for a variable on the
* heap, every objects is identified by name (c,d) and type t
* 3 bytes are used here.
*/
address_t bmalloc(mem_t t, mem_t c, mem_t d, address_t l) {
address_t vsize; /* the length of the header on the heap*/
address_t b;
if (DEBUG) { outsc("** bmalloc with token "); outnumber(t); outcr(); }
/* check if the object already exists */
if (bfind(t, c, d) != 0 ) { error(EVARIABLE); return 0; };
/*
* how much space is needed
* 3 bytes for the token and the 2 name characters
* numsize for every number including array length
* one byte for every string character
*/
switch(t) {
case VARIABLE:
vsize=numsize+3;
break;
#ifndef HASMULTIDIM
case ARRAYVAR:
vsize=numsize*l+addrsize+3;
break;
#else
/* multidim implementation for n=2 */
case ARRAYVAR:
vsize=numsize*l+addrsize*2+3;
break;
#endif
#ifdef HASDARTMOUTH
case TFN:
vsize=addrsize+2+3;
break;
#endif
default:
vsize=l+addrsize+3;
}
/* enough memory ?, on an EEPROM system we limit the heap to the RAM */
#ifndef EEPROMMEMINTERFACE
if ((himem-top) < vsize) { error(EOUTOFMEMORY); return 0;}
#else
if (himem-(elength()-eheadersize) < vsize) { error(EOUTOFMEMORY); return 0;}
#endif
/* store the name of the objects (2 chars) plus the type (1 char) as identifier */
b=himem;
memwrite2(b--, c);
memwrite2(b--, d);
memwrite2(b--, t);
/* for strings, arrays and buffers write the (maximum) length
directly after the header */
if (t == ARRAYVAR || t == STRINGVAR || t == TBUFFER) {
b=b-addrsize+1;
z.a=vsize-(addrsize+3);
setnumber(b, addrsize);
b--;
}
/* remark on multidim, the byte length is after the header and the following
address_t bytes are then reserved for the first dimension */
/* reserve space for the payload */
himem-=vsize;
nvars++;
return himem+1;
}
/*
* bfind() passes back the location of the object as result
* the length of the object is in z.a as a side effect
* rememers the last search
*
*/
address_t bfind(mem_t t, mem_t c, mem_t d) {
address_t b = memsize;
mem_t t1;
mem_t c1, d1;
address_t i=0;
/* the bfind cache, did we ask for that object before? -
needed to make the string code efficient */
if (t == bfindt && c == bfindc && d == bfindd) {
z.a=bfindz;
return bfinda;
}
/* walk through the heap now */
while (i < nvars) {
c1=memread2(b--);
d1=memread2(b--);
t1=memread2(b--);
switch(t1) {
case VARIABLE:
z.a=numsize;
break;
case TFN:
z.a=addrsize+2;
break;
default:
b=b-addrsize+1;
getnumber(b, addrsize);
b--;
}
b-=z.a;
/* once we found we cache and return the location */
if (c1 == c && d1 == d && t1 == t) {
bfindc=c;
bfindd=d;
bfindt=t;
bfindz=z.a;
bfinda=b+1;
return b+1;
}
i++;
}
return 0;
}
/* finds an object and deletes the heap from this object on
including the object itself */
address_t bfree(mem_t t, mem_t c, mem_t d) {
address_t b = memsize;
mem_t t1;
mem_t c1, d1;
address_t i=0;
if (DEBUG) { outsc("*** bfree called for "); outch(c); outch(d); outsc(" on heap with token "); outnumber(t); outcr(); }
/* walk through the heap to find the location */
while (i < nvars) {
c1=memread2(b--);
d1=memread2(b--);
t1=memread2(b--);
/* found it */
if (t == t1 && c == c1 && d == d1) {
/* set the number of variables to the new value */
nvars=i;
if (DEBUG) { outsc("*** bfree setting nvars to "); outnumber(nvars); outcr(); }
/* clean up - this is somehow optional, one could drop this */
if (DEBUG) { outsc("*** bfree clearing "); outnumber(himem); outspc(); outnumber(b+3); outcr(); }
for (i=himem; i<=b+3; i++) memwrite2(i, 0);
/* now set the memory to the right address */
himem=b+3;
/* forget the chache, because heap structure has changed !! */
bfindc=0;
bfindd=0;
bfindt=0;
bfindz=0;
bfinda=0;
return himem;
}
switch(t1) {
case VARIABLE:
z.a=numsize;
break;
#ifdef HASDARTMOUTH
case TFN:
z.a=addrsize+2;
break;
#endif
default:
b=b-addrsize+1;
getnumber(b, addrsize);
b--;
}
b-=z.a;
i++;
}
return 0;
}
/* the length of an object */
address_t blength (mem_t t, mem_t c, mem_t d) {
if (bfind(t, c, d)) return z.a; else return 0;
}
#endif
/* get and create a variable */
number_t getvar(mem_t c, mem_t d){
address_t a;
if (DEBUG) { outsc("* getvar "); outch(c); outch(d); outspc(); outcr(); }
/* the static variable array */
if (c >= 65 && c <= 91 && d == 0) return vars[c-65];
/* the special variables */
if ( c == '@' )
switch (d) {
case 'A':
return availch();
case 'S':
return ert;
case 'I':
return id;
case 'O':
return od;
case 'C':
if (availch()) return inch(); else return 0;
case 'E':
return elength()/numsize;
case 0:
return (himem-top)/numsize;
case 'R':
return rd;
case 'U':
return getusrvar();
#ifdef HASIOT
case 'V':
return vlength;
#endif
#if defined(DISPLAYDRIVER) || defined (GRAPHDISPLAYDRIVER)
case 'X':
return dspgetcursorx();
case 'Y':
return dspgetcursory();
#endif
}
#ifdef HASAPPLE1
/* dynamically allocated vars, create them on the fly if needed */
if (!(a=bfind(VARIABLE, c, d))) a=bmalloc(VARIABLE, c, d, 0);
if (er != 0) return 0;
/* retrieve the value */
getnumber(a, numsize);
return z.i;
#else
/* systems without Apple1 extension i.e. HEAP throw an error */
error(EVARIABLE);
return 0;
#endif
}
/* set and create a variable */
void setvar(mem_t c, mem_t d, number_t v){
address_t a;
if (DEBUG) { outsc("* setvar "); outch(c); outch(d); outspc(); outnumber(v); outcr(); }
/* the static variable array */
if (c >= 65 && c <= 91 && d == 0) {
vars[c-65]=v;
return;
}
/* the special variables */
if ( c == '@' )
switch (d) {
case 'S':
ert=v;
return;
case 'I':
id=v;
return;
case 'O':
od=v;
return;
case 'C':
outch(v);
return;
case 'R':
rd=v;
return;
case 'U':
setusrvar(v);
return;
#ifdef HASIOT
case 'V':
return;
#endif
#if defined(DISPLAYDRIVER) || defined(GRAPHDISPLAYDRIVER)
case 'X':
dspsetcursorx((int)v);
/* set the charcount, this is half broken but works */
#ifdef HASMSTAB
if (od > 0 && od <= OPRT) charcount[od-1]=v;
#endif
return;
case 'Y':
dspsetcursory((int)v);
return;
#endif
}
#ifdef HASAPPLE1
/* dynamically allocated vars */
if (!(a=bfind(VARIABLE, c, d))) a=bmalloc(VARIABLE, c, d, 0);
if (er != 0) return;
/* set the value */
z.i=v;
setnumber(a, numsize);
#else
error(EVARIABLE);
#endif
}
/* clr all variables */
void clrvars() {
address_t i;
/* delete all statics */
for (i=0; i<VARSIZE; i++) vars[i]=0;
#ifdef HASAPPLE1
/* clear the heap */
nvars=0;
/* then set the entire mem area to zero */
for (i=himem; i<memsize; i++) memwrite2(i, 0);
/* reset the heap start*/
himem=memsize;
/* and clear the cache */
bfindc=bfindd=bfindt=0;
bfinda=bfindz=0;
#endif
}
/*
* The BASIC memory access function
*
* the functions getnumber(), pgetnumber() and egetnumber()
* retrieve a number from memory. This can be a a byte, an address
* or a real number. setnumber() and esetnumber() store the object.
*
* All operations are always going through the structure z.
*/
void getnumber(address_t m, mem_t n){
mem_t i;
z.i=0;
for (i=0; i<n; i++) z.c[i]=memread2(m++);
}
/* a generic memory reader for numbers - will replace getnumber in future */
number_t getnumber2(address_t m, memreader_t f) {
mem_t i;
accu_t z;
for (i=0; i<numsize; i++) z.c[i]=f(m++);
return z.i;
}
/* same for addresses - will replace getnumber in future */
address_t getaddress(address_t m, memreader_t f) {
mem_t i;
accu_t z;
for (i=0; i<addrsize; i++) z.c[i]=f(m++);
return z.a;
}
/* same for strings - currently identical to address but this will change */
address_t getstringlength(address_t m, memreader_t f) {
mem_t i;
accu_t z;
for (i=0; i<addrsize; i++) z.c[i]=f(m++);
return z.a;
}
/* code only for the USEMEMINTERFACE code, this function goes through the
ro buffer to avoit rw buffer page faults, do not use this unless
you understand the USEMEMINTERFACE mechanism completely */
void pgetnumber(address_t m, mem_t n){
mem_t i;
z.i=0;
for (i=0; i<n; i++) z.c[i]=memread(m++);
}
/* the eeprom memory access */
void egetnumber(address_t m, mem_t n){
mem_t i;
z.i=0;
for (i=0; i<n; i++) z.c[i]=eread(m++);
}
/* set a number at a memory location */
void setnumber(address_t m, mem_t n){
mem_t i;
for (i=0; i<n; i++) memwrite2(m++, z.c[i]);
}
/* set a number at a eepromlocation */
void esetnumber(address_t m, mem_t n){
mem_t i;
for (i=0; i<n; i++) eupdate(m++, z.c[i]);
}
/* create an array */
address_t createarray(mem_t c, mem_t d, address_t i, address_t j) {
address_t a, zat, at;
#ifdef HASAPPLE1
if (DEBUG) { outsc("* create array "); outch(c); outch(d); outspc(); outnumber(i); outcr(); }
#ifndef HASMULTIDIM
if (bfind(ARRAYVAR, c, d)) error(EVARIABLE); else return bmalloc(ARRAYVAR, c, d, i);
#else
if (bfind(ARRAYVAR, c, d))
error(EVARIABLE);
else {
a=bmalloc(ARRAYVAR, c, d, i*j);
zat=z.a; /* preserve z.a because it is needed on autocreate later */
z.a=j;
at=a+i*j*numsize;
memwrite2(at++, z.b.l);
memwrite2(at, z.b.h);
z.a=zat;
return a;
}
#endif
#endif
return 0;
}
/* finds if an array is twodimensional and what the second dimension structure is
test code, should be rewritten to properly use getnumber*/
address_t getarrayseconddim(address_t a, address_t za) {
#ifdef HASMULTIDIM
address_t zat1, zat2;
zat1=z.a;
z.b.l=memread2(a+za-2); /* test code, assuming 16 bit address_t here, should be ported to setnumber */
z.b.h=memread2(a+za-1);
zat2=z.a;
z.a=zat1;
return zat2;
#else
return 1;
#endif
}
/* generic array access function */
void array(mem_t m, mem_t c, mem_t d, address_t i, address_t j, number_t* v) {
address_t a;
address_t h;
mem_t e = 0;
#ifdef HASMULTIDIM
address_t dim=1, zat;
#endif
if (DEBUG) { outsc("* get/set array "); outch(c); outspc(); outnumber(i); outcr(); }
/* special arrays EEPROM, Display and Wang, dimension j is ignored here and not handled */
if (c == '@') {
switch(d) {
case 'E':
h=elength()/numsize;
a=elength()-numsize*i;
e=1;
break;
#if defined(DISPLAYDRIVER) && defined(DISPLAYCANSCROLL)
case 'D':
if (m == 'g') *v=dspget(i-1);
else if (m == 's') dspset(i-1, *v);
return;
#endif
#if defined(HASCLOCK)
case 'T':
if (m == 'g') *v=rtcget(i);
else if (m == 's') rtcset(i, *v);
return;
#endif
#if defined(ARDUINO) && defined(ARDUINOSENSORS)
case 'S':
if (m == 'g') *v=sensorread(i, 0);
return;
#endif
case 'U':