|
| 1 | +#include <assert.h> |
| 2 | +#include <stdbool.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#include <ti/getcsc.h> |
| 7 | +#include <ti/screen.h> |
| 8 | +#include <ti/vars.h> |
| 9 | +#include <debug.h> |
| 10 | + |
| 11 | +struct Counters { |
| 12 | + unsigned int ram; |
| 13 | + unsigned int flash; |
| 14 | +}; |
| 15 | + |
| 16 | +// Those can't be deleted. |
| 17 | +static bool isInternalVar(uint8_t varType, const char* name) { |
| 18 | + return !strcmp(name, "#") |
| 19 | + || (varType == OS_TYPE_EQU && !strcmp(name, ".")) |
| 20 | + || (varType == OS_TYPE_PRGM && !strcmp(name, "!")); |
| 21 | +} |
| 22 | + |
| 23 | +// Walk through the VAT, debug-prints some info about the entries, and returns the number of entries seen |
| 24 | +static struct Counters walkVAT(void) |
| 25 | +{ |
| 26 | + void* vatStart = os_GetSymTablePtr(); |
| 27 | + void* nextEntry = NULL; |
| 28 | + void* entry = vatStart; |
| 29 | + uint24_t varType = 0, nameLen = 0; |
| 30 | + char name[9] = {0}; |
| 31 | + void* data = NULL; |
| 32 | + struct Counters counters = { 0, 0 }; |
| 33 | + |
| 34 | + while (entry) |
| 35 | + { |
| 36 | + nextEntry = os_NextSymEntry(entry, &varType, &nameLen, name, &data); |
| 37 | + |
| 38 | + const bool isArchived = (uintptr_t)data < (uintptr_t)os_RamStart; |
| 39 | + const bool isNamed = entry <= *(void**)0xD0259D; // progPtr |
| 40 | + const bool isInternal = isInternalVar(varType, name); |
| 41 | + const uint8_t* actualDataAddr = isArchived ? (data + 9 + isNamed + nameLen) : data; |
| 42 | + |
| 43 | + if (isArchived) { |
| 44 | + counters.flash++; |
| 45 | + } else { |
| 46 | + counters.ram++; |
| 47 | + } |
| 48 | + |
| 49 | + dbg_printf("%s%s entry %p (data @ %06X ; actual data @ %06X) : [type %02X] [namelen %d] \"%s\"\n", |
| 50 | + isArchived ? "Archived" : "RAM", isInternal ? " internal" : "", entry, (uintptr_t)data, |
| 51 | + (uintptr_t)actualDataAddr, varType, nameLen, name); |
| 52 | + |
| 53 | + entry = nextEntry; |
| 54 | + } |
| 55 | + |
| 56 | + return counters; |
| 57 | +} |
| 58 | + |
| 59 | +int main(void) |
| 60 | +{ |
| 61 | + char buf[31] = {0}; |
| 62 | + struct Counters counters = { 0, 0 }; |
| 63 | + |
| 64 | + os_ClrHome(); |
| 65 | + |
| 66 | + counters = walkVAT(); |
| 67 | + snprintf(buf, 30, "before: %d R / %d F", counters.ram, counters.flash); |
| 68 | + os_PutStrFull(buf); |
| 69 | + os_NewLine(); |
| 70 | + |
| 71 | + int ret = 0; |
| 72 | + ret = os_SetListDim("\x5DTESTL", 10); |
| 73 | + dbg_printf("os_SetListDim ret = %d\n", ret); |
| 74 | + assert(ret == 0); |
| 75 | + |
| 76 | + void *entry, *data; |
| 77 | + ret = os_ChkFindSym(OS_TYPE_REAL_LIST, "\x5DTESTL", &entry, &data); |
| 78 | + dbg_printf("os_ChkFindSym ret = %d. entry = %p, data = %p\n", ret, entry, data); |
| 79 | + assert(ret == 1); |
| 80 | + |
| 81 | + counters = walkVAT(); |
| 82 | + snprintf(buf, 30, "created: %d R / %d F", counters.ram, counters.flash); |
| 83 | + os_PutStrFull(buf); |
| 84 | + os_NewLine(); |
| 85 | + |
| 86 | + bool archived = arcUnarcVariable(OS_TYPE_REAL_LIST, 6, "\x5DTESTL"); |
| 87 | + assert(archived); |
| 88 | + |
| 89 | + counters = walkVAT(); |
| 90 | + snprintf(buf, 30, "archived: %d R / %d F", counters.ram, counters.flash); |
| 91 | + os_PutStrFull(buf); |
| 92 | + os_NewLine(); |
| 93 | + |
| 94 | + ret = os_ChkFindSym(OS_TYPE_REAL_LIST, "\x5DTESTL", &entry, &data); |
| 95 | + dbg_printf("os_ChkFindSym ret = %d. entry = %p, data = %p\n", ret, entry, data); |
| 96 | + assert(ret == 1); |
| 97 | + |
| 98 | + os_DelVar(entry); |
| 99 | + |
| 100 | + ret = os_ChkFindSym(OS_TYPE_REAL_LIST, "\x5DTESTL", &entry, &data); |
| 101 | + dbg_printf("os_ChkFindSym ret = %d. entry = %p, data = %p\n", ret, entry, data); |
| 102 | + assert(ret == 0); |
| 103 | + |
| 104 | + counters = walkVAT(); |
| 105 | + snprintf(buf, 30, "removed: %d R / %d F", counters.ram, counters.flash); |
| 106 | + os_PutStrFull(buf); |
| 107 | + |
| 108 | + while (!os_GetCSC()); |
| 109 | + return 0; |
| 110 | +} |
0 commit comments