forked from farmergreg/ti-command-post-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ram.c
574 lines (456 loc) · 13.2 KB
/
ram.c
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
/**************************************************
*** Project Title: Command Post (KFLASH) ***
*** Author: Greg Dietsche ***
*** Date: 07/23/2002 ***
*** Description: An application designed for ***
*** use with the TI-89 and TI-92 Plus Graphing ***
*** calculators during the testing and ***
*** debugging phases of FLASH APPS and RAM ***
*** programs ***
***************************************************/
// $Id: ram.c 4 2004-08-05 19:06:59Z dietsche $
#include <tiams.h>
#include "KFLASH.h"
#include "KFLASHr1.h"
#include "ram.h"
#include "undoc.h"
void do_LowMemStressTest(void)
{
HSYM sym;
volatile HANDLE h;
STRESS_DAT *gdat=&global.stress_dat;
DWORD freemem;
char varbuff[MAX_SYM_LEN];
char tvarbuff[MAX_SYM_LEN];
BYTE *ptr;
unsigned short counter;
unsigned short alloc;
for(counter=0;counter<STRESS_MAX_VARS;counter++)
{//remove any previously created files
// this has a dual purpose: 1 handle STRESS_DISABLE
// and two: prepare to handle STRESS_ENABLE if applicable
sprintf(varbuff, OO_AbsoluteGet(OO_FIRST_APP_STRING + XR_LowMemFmtStr), counter);
ptr=StrToTokN((BYTE*)varbuff, (BYTE*)tvarbuff);
SymDel(ptr);
}
//rezero counter
counter=0;
if(gdat->optList[OPT_STATUS]==STRESS_ENABLE)
{
freemem=StrToEStackToLong(gdat->txtBytes);
if(gdat->optList[OPT_MODE]==STRESS_MODE_USE)
{//internally, the app operates in STRESS_MODE_FILL
freemem=HeapAvail()-freemem;
}
else
freemem+=2;//adjust by two to get correct debugging HeapAvail results (before vs after);
while(freemem<=HeapAvail())
{
sprintf(varbuff, OO_AbsoluteGet(OO_FIRST_APP_STRING + XR_LowMemFmtStr), counter++);
ptr=StrToTokN((BYTE*)varbuff, (BYTE*)tvarbuff);
//create the variable
if(!(sym=SymAdd(ptr)))
break;
//Calculate amount to allocate
alloc=HeapMax();
if(HeapAvail()-alloc<=freemem) //if projected mem usage less than wanted
alloc=HeapAvail()-freemem; //then allocate the difference to get exact amount
if(!(h=HeapAlloc(alloc))) //allocate the memory
break; //just exit on error--no cleanup required
ptr=HeapDeref(h);
alloc-=2; //prepare to set variable size (2 less than actual)
ptr[0]=alloc >> 8; //set the variable size
ptr[1]=alloc & 0xff; //set the variable size
ptr+=(alloc-4); //move to 6 before var end and set the variable type
ptr[0]= 0;
ptr[1]='t';
ptr[2]='m';
ptr[3]='p';
ptr[4]= 0;
ptr[5]=GEN_DATA_TAG;
DerefSym(sym)->hVal=h; //Attach the memory to the previously created variable
if(counter>=STRESS_MAX_VARS)break;
}
}
}
//Implements thisapp.LowMem
//returns previous settings as a list
//if no input, returns current settings
void ext_StressTest(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr, topstack;
STRESS_DAT *gdat=&global.stress_dat;
STRESS_DAT s_dat=*gdat;
unsigned long args;
short argcount;
//Init estack variables
oldtop=argptr=top_estack;
argcount=remaining_element_count(argptr);
//Error Checking
if(argcount>3) ER_throw(TOO_MANY_ARGS_ERROR);
TRY
//push the current info onto the estack as the return value
push_quantum(END_TAG);
push_long_to_integer(strtol(s_dat.txtBytes, NULL, 0));
push_ushort_to_integer(s_dat.optList[OPT_MODE]);
push_ushort_to_integer(s_dat.optList[OPT_STATUS] - 1);
push_quantum(LIST_TAG);
//no arguments? do not process any further
if(argcount==0)
return;
topstack=top_estack;
//Get arg1 and check for immediate error
push_simplify(argptr);
args=GetValue(top_estack, 0, 3);
//handle arg1
switch(args)
{//used a switch in case i want to use 1 instead of default for the STRESS_ENABLE trigger
case 0: //Turn Off Stress Test
s_dat.optList[OPT_STATUS]=STRESS_DISABLE;
break;
//case 1:
default: //Turn ON Stress Test (any number except zero)
s_dat.optList[OPT_STATUS]=STRESS_ENABLE;
//break;
//case 2:
//case 3:
};
if(argcount>1)//if more than one argument, then all three must be present
{
push_simplify(argptr=next_expression_index(argptr));
args=GetValue(top_estack, 1, 2);
//handle arg2
switch(args)
{
case 1: //leave x bytes free
s_dat.optList[OPT_MODE]=STRESS_MODE_FILL;
break;
case 2: //use at most x bytes total
s_dat.optList[OPT_MODE]=STRESS_MODE_USE;
break;
};
}//end if(argcount>1)
if(argcount>2)
{
push_simplify(argptr=next_expression_index(argptr));
args=GetValue(top_estack, 0, LONG_MAX);
sprintf(s_dat.txtBytes, "%ld", args);
}// end if(argcount>2)
//restore the estack
top_estack=topstack;
//copy the updated local struct to the global struct
*gdat=s_dat;
//Activate any changes
do_LowMemStressTest();
ONERR
top_estack=oldtop; //is this right...
PASS;
ENDTRY
}
void DoStressTestDialog(void)
{
Access_AMS_Global_Variables;
STRESS_DAT *gdat=&global.stress_dat;
STRESS_DAT localdat=*gdat; //make a local backup copy
switch(Dialog(&dlgStressTest, -1, -1, gdat->txtBytes, gdat->optList))
{
case KB_ESC: //discard the changes
*gdat=localdat;
break;
case DB_MEMFULL: //handle low memory error
EV_errorCode=ER_MEMORY;
break;
}
//Activate any changes
do_LowMemStressTest();
}
//callback routine for dlgStressTest
DWORD cbStressTest(WORD DlgId, DWORD dValue)
{
STRESS_DAT *gdat=&global.stress_dat; //pointer to the global data (saves space)
long val;
switch(DlgId)
{
case DB_QACTIVE: //is (dValue) active?
if(gdat->optList[0]==STRESS_DISABLE && dValue!=STRESS_POP_STATUS && dValue!=STRESS_HEADER) //disable every item except STRESS_POP_STATUS and Header
return FALSE;
/*else
return TRUE;*/
default:
return DB_REDRAW_AND_CONTINUE;
break;
};
}
//Displays The largest allocatable block of ram, and amount of available ram
void DoRAMStatsDialog(void)
{
Access_AMS_Global_Variables;
char buff[500];
//>>10 to get (K) by integer division
sprintf(buff,
XR_stringPtr(XR_MemoryInfo),
HeapAvail(),
HeapMax(),
FreeHandles(),
HeapWalk(H_WALK_VERIFY)?XR_stringPtr(XR_HeapVaild):XR_stringPtr(XR_HeapInvalid),
FlashMemoryEnd);
DlgNotice(XR_stringPtr(XR_LongAppName),buff);
}
void ext_HeapShuffle(void)
{
HeapShuffle();
}
void ext_HeapDeref(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr;
HANDLE h;
void *addr;
short argcount;
HeapCompress();
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>1) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<1) ER_throw(TOO_FEW_ARGS_ERROR);
push_quantum(END_TAG);
push_simplify(argptr);
h=(HANDLE)GetLongArg(top_estack, 0, ULONG_MAX);
top_estack=oldtop;
addr=HeapDeref(h);
if(CheckInvalidHandle(addr)) ER_throwFrame(ER_InvalidHandle, pAppObj);
push_ulong_to_integer((ULONG)addr);
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}
void ext_PeekB(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr;
unsigned char *address;
short argcount;
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>1) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<1) ER_throw(TOO_FEW_ARGS_ERROR);
push_quantum(END_TAG);
push_simplify(argptr);
address=(UCHAR*)(long)GetLongArg(top_estack, 0, ULONG_MAX);
top_estack=oldtop;
push_ushort_to_integer(*address&0xff);
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}
void ext_PeekW(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr;
short *address;
short argcount;
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>1) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<1) ER_throw(TOO_FEW_ARGS_ERROR);
push_simplify(argptr);
address=(short*)(long)GetLongArg(top_estack, 0, ULONG_MAX);
top_estack=oldtop;
if((long)address%2)ER_throwFrame(ER_Address, pAppObj); //ER_throw(ER_ARGUMENT);
push_ushort_to_integer(*address);
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}
void ext_PeekL(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr;
ULONG *address;
short argcount;
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>1) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<1) ER_throw(TOO_FEW_ARGS_ERROR);
push_simplify(argptr);
address=(ULONG*)(long)GetLongArg(top_estack, 0, ULONG_MAX);
top_estack=oldtop;
if((long)address%2)ER_throwFrame(ER_Address, pAppObj); //ER_throw(ER_ARGUMENT);
push_ulong_to_integer(*address);
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}
void ext_PokeB(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr, top;
char *address;
char value;
short argcount;
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>2) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<2) ER_throw(TOO_FEW_ARGS_ERROR);
push_simplify(argptr);
address=(char*)(long)GetLongArg(top_estack, 0, ULONG_MAX);
push_simplify(next_expression_index(argptr));
value=(char)GetLongArg(top_estack, 0, USHRT_MAX);
top_estack=oldtop;
push_ushort_to_integer(*address&0xff);
*address=value;
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}
void ext_PokeW(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr, top;
short *address;
short value;
short argcount;
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>2) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<2) ER_throw(TOO_FEW_ARGS_ERROR);
push_simplify(argptr);
address=(short*)(long)GetLongArg(top_estack, 0, LONG_MAX);
push_simplify(next_expression_index(argptr));
value=(short)GetLongArg(top_estack, 0, USHRT_MAX);
top_estack=oldtop;
if((long)address%2)ER_throwFrame(ER_Address, pAppObj); //ER_throw(ER_ARGUMENT);
push_ushort_to_integer(*address);
*address=value;
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}
void ext_PokeL(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr, top;
ULONG *address;
ULONG value;
short argcount;
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>2) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<2) ER_throw(TOO_FEW_ARGS_ERROR);
push_simplify(argptr);
address=(ULONG*)(long)GetLongArg(top_estack, 0, LONG_MAX);
push_simplify(next_expression_index(argptr));
value=(ULONG)GetLongArg(top_estack, 0, ULONG_MAX);
top_estack=oldtop;
if((long)address%2)ER_throwFrame(ER_Address, pAppObj); //ER_throw(ER_ARGUMENT);
push_ulong_to_integer(*address);
*address=value;
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}
void ext_bclr(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr, top;
char *address;
char bitnum;
short argcount;
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>2) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<2) ER_throw(TOO_FEW_ARGS_ERROR);
push_simplify(argptr);
address=(char*)(long)GetLongArg(top_estack, 0, ULONG_MAX);
push_simplify(next_expression_index(argptr));
bitnum=(char)GetLongArg(top_estack, 0, 7);
top_estack=oldtop;
if(*address&(1<<bitnum))
push_ushort_to_integer(1);
else
push_ushort_to_integer(0);
*address&= ~(1<<bitnum);
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}
void ext_bset(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr, top;
char *address;
char bitnum;
short argcount;
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>2) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<2) ER_throw(TOO_FEW_ARGS_ERROR);
push_simplify(argptr);
address=(char*)(long)GetLongArg(top_estack, 0, ULONG_MAX);
push_simplify(next_expression_index(argptr));
bitnum=(char)GetLongArg(top_estack, 0, 7);
top_estack=oldtop;
if(*address&(1<<bitnum))
push_ushort_to_integer(1);
else
push_ushort_to_integer(0);
*address|=(1<<bitnum);
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}
void ext_btst(void)
{
Access_AMS_Global_Variables;
EStackIndex oldtop;
EStackIndex argptr, top;
char *address;
char bitnum;
short argcount;
oldtop=argptr=top_estack;
TRY
argcount=remaining_element_count(argptr);
if(argcount>2) ER_throw(TOO_MANY_ARGS_ERROR);
if(argcount<2) ER_throw(TOO_FEW_ARGS_ERROR);
push_simplify(argptr);
address=(char*)(long)GetLongArg(top_estack, 0, ULONG_MAX);
push_simplify(next_expression_index(argptr));
bitnum=(char)GetLongArg(top_estack, 0, 7);
top_estack=oldtop;
if(*address&(1<<bitnum))
push_ushort_to_integer(1);
else
push_ushort_to_integer(0);
ONERR
top_estack=oldtop;
PASS;
ENDTRY
}