123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
-
-
-
- #define M100_FREE_MEMORY_DUMPER
- #define M100_FREE_MEMORY_CORRUPTOR
-
- #include "Marlin.h"
-
- #if ENABLED(M100_FREE_MEMORY_WATCHER)
- extern void* __brkval;
- extern size_t __heap_start, __heap_end, __flp;
-
-
-
-
-
-
- float code_value();
- long code_value_long();
- bool code_seen(char);
- void serial_echopair_P(const char*, float);
- void serial_echopair_P(const char*, double);
- void serial_echopair_P(const char*, unsigned long);
- void serial_echopair_P(const char*, int);
- void serial_echopair_P(const char*, long);
-
-
-
-
-
-
-
-
- unsigned char* top_of_stack();
- void prt_hex_nibble(unsigned int);
- void prt_hex_byte(unsigned int);
- void prt_hex_word(unsigned int);
- int how_many_E5s_are_here(unsigned char*);
-
-
-
-
- void gcode_M100() {
- static int m100_not_initialized = 1;
- unsigned char* sp, *ptr;
- int i, j, n;
-
-
-
-
-
-
-
-
-
- #if ENABLED(M100_FREE_MEMORY_DUMPER)
- if (code_seen('D')) {
- ptr = (unsigned char*) __brkval;
-
-
-
-
- SERIAL_ECHOPGM("\n__brkval : ");
- prt_hex_word((unsigned int) ptr);
- ptr = (unsigned char*)((unsigned long) ptr & 0xfff0);
- sp = top_of_stack();
- SERIAL_ECHOPGM("\nStack Pointer : ");
- prt_hex_word((unsigned int) sp);
- SERIAL_ECHOPGM("\n");
- sp = (unsigned char*)((unsigned long) sp | 0x000f);
- n = sp - ptr;
-
-
-
- while (ptr < sp) {
- prt_hex_word((unsigned int) ptr);
- SERIAL_ECHOPGM(":");
- for (i = 0; i < 16; i++) {
- prt_hex_byte(*(ptr + i));
- SERIAL_ECHOPGM(" ");
- delay(2);
- }
- SERIAL_ECHO("|");
- for (i = 0; i < 16; i++) {
- delay(2);
- if (*(ptr + i) == 0xe5)
- SERIAL_ECHOPGM(" ");
- else
- SERIAL_ECHOPGM("?");
- }
- SERIAL_ECHO("\n");
- ptr += 16;
- delay(2);
- }
- SERIAL_ECHOLNPGM("Done.\n");
- return;
- }
- #endif
-
-
-
-
- if (code_seen('F')) {
- #if 0
- int max_addr = (int) __brkval;
- int max_cnt = 0;
- #endif
- int block_cnt = 0;
- ptr = (unsigned char*) __brkval;
- sp = top_of_stack();
- n = sp - ptr;
-
- for (i = 0; i < n; i++) {
- if (*(ptr + i) == (unsigned char) 0xe5) {
- j = how_many_E5s_are_here((unsigned char*) ptr + i);
- if (j > 8) {
- SERIAL_ECHOPAIR("Found ", j);
- SERIAL_ECHOPGM(" bytes free at 0x");
- prt_hex_word((int) ptr + i);
- SERIAL_ECHOPGM("\n");
- i += j;
- block_cnt++;
- }
- #if 0
- if (j > max_cnt) {
- max_cnt = j;
- max_addr = (int) ptr + i;
- }
- #endif
- }
- }
- if (block_cnt > 1)
- SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.\n");
- SERIAL_ECHO("\nDone.\n");
- return;
- }
-
-
-
-
- #if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
- if (code_seen('C')) {
- int x;
- x = code_value();
- SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
- ptr = (unsigned char*) __brkval;
- SERIAL_ECHOPAIR("\n__brkval : ", ptr);
- ptr += 8;
- sp = top_of_stack();
- SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
- SERIAL_ECHOLNPGM("\n");
- n = sp - ptr - 64;
-
- j = n / (x + 1);
- for (i = 1; i <= x; i++) {
- *(ptr + (i * j)) = i;
- SERIAL_ECHO("\nCorrupting address: 0x");
- prt_hex_word((unsigned int)(ptr + (i * j)));
- }
- SERIAL_ECHOLNPGM("\n");
- return;
- }
- #endif
-
-
-
-
- if (m100_not_initialized || code_seen('I')) {
- SERIAL_ECHOLNPGM("Initializing free memory block.\n");
- ptr = (unsigned char*) __brkval;
- SERIAL_ECHOPAIR("\n__brkval : ", ptr);
- ptr += 8;
- sp = top_of_stack();
- SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
- SERIAL_ECHOLNPGM("\n");
- n = sp - ptr - 64;
-
- SERIAL_ECHO(n);
- SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
- for (i = 0; i < n; i++)
- *(ptr + i) = (unsigned char) 0xe5;
- for (i = 0; i < n; i++) {
- if (*(ptr + i) != (unsigned char) 0xe5) {
- SERIAL_ECHOPAIR("? address : ", ptr + i);
- SERIAL_ECHOPAIR("=", *(ptr + i));
- SERIAL_ECHOLNPGM("\n");
- }
- }
- m100_not_initialized = 0;
- SERIAL_ECHOLNPGM("Done.\n");
- return;
- }
- return;
- }
-
-
-
-
- unsigned char* top_of_stack() {
- unsigned char x;
- return &x + 1;
- }
-
-
-
-
-
- void prt_hex_nibble(unsigned int n) {
- if (n <= 9)
- SERIAL_ECHO(n);
- else
- SERIAL_ECHO((char)('A' + n - 10));
- delay(2);
- }
-
- void prt_hex_byte(unsigned int b) {
- prt_hex_nibble((b & 0xf0) >> 4);
- prt_hex_nibble(b & 0x0f);
- }
-
- void prt_hex_word(unsigned int w) {
- prt_hex_byte((w & 0xff00) >> 8);
- prt_hex_byte(w & 0x0ff);
- }
-
-
-
-
- int how_many_E5s_are_here(unsigned char* p) {
- int n;
- for (n = 0; n < 32000; n++) {
- if (*(p + n) != (unsigned char) 0xe5)
- return n - 1;
- }
- return -1;
- }
-
- #endif
|