Cleaning up the glitching scripts.
[goodfet] / firmware / apps / monitor / monitor.c
1 /*! \file monitor.c
2   \author Travis Goodspeed
3   \brief Local debug monitor.
4 */
5
6 #include "command.h"
7 #include "platform.h"
8 #include "monitor.h"
9
10 //! Call a function by address.
11 int fncall(unsigned int adr){
12   int (*machfn)() = 0;
13   machfn= (int (*)()) adr;
14   return machfn();
15 }
16
17 //! Handles a monitor command.
18 void monitorhandle(unsigned char app,
19                    unsigned char verb,
20                    unsigned long len){
21   switch(verb){
22   default:
23     debugstr("ERROR: Command unsupported by debug monitor.");
24     break;
25   case PEEK:
26     cmddata[0]=memorybyte[cmddataword[0]];
27     txdata(app,verb,1);
28     break;
29   case POKE:
30     //Todo, make word or byte.
31     memorybyte[cmddataword[0]]=cmddata[2];
32     cmddata[0]=memorybyte[cmddataword[0]];
33     txdata(app,verb,1);
34     break;
35   case CALL:
36     //Set the program counter to cmdword[0];
37     cmddataword[0]=fncall(cmddataword[0]);
38     txdata(app,verb,2);
39     break;
40   case EXEC:
41     //Execute the argument as code from RAM.
42     cmddataword[0]=fncall((u16) cmddataword);
43     txdata(app,verb,2);
44     break;
45   case MONITOR_SIZEBUF:
46     //TODO make the data length target-specific, varying by ram.
47     cmddataword[0]=0x100;
48     txdata(app,verb,2);
49     break;
50   case MONITOR_CHANGE_BAUD:
51     //This command, and ONLY this command, does not reply.
52     setbaud(cmddata[0]);
53     //txdata(app,verb,0);
54     break;
55   case MONITOR_RAM_PATTERN:
56     monitor_ram_pattern();//reboots, will never return
57     break;
58   case MONITOR_RAM_DEPTH:
59     cmddataword[0]=monitor_ram_depth();
60     txdata(app,verb,2);
61     break;
62   case MONITOR_DIR:
63     P5DIR=cmddata[0];
64     txdata(app,verb,1);
65     break;
66   case MONITOR_IN:
67     cmddata[0]=P5IN;
68     txdata(app,verb,1);
69     break;
70   case MONITOR_OUT:
71     P5OUT=cmddata[0];
72     txdata(app,verb,1);
73     break;
74   case MONITOR_SILENT:
75     silent=cmddata[0];
76     txdata(app,verb,1);
77     break;
78   }
79 }
80
81 //! Overwrite all of RAM with 0xBEEF, then reboot.
82 void monitor_ram_pattern(){
83   register int *a;
84   
85   //Wipe all of ram.
86   for(a=(int*)0x1100;a<(int*)0x2500;a++){//TODO get these from the linker.
87     *((int*)a) = 0xBEEF;
88   }
89   txdata(0x00,0x90,0);
90   
91   //Reboot
92   #ifdef MSP430
93   asm("br &0xfffe");
94   #endif
95 }
96
97 //! Return the number of contiguous bytes 0xBEEF, to measure RAM usage.
98 unsigned int monitor_ram_depth(){
99   register int a;
100   register int count=0;
101   for(a=0x1100;a<0x2500;a+=2)
102     if(*((int*)a)==0xBEEF) count+=2;
103   
104   return count;
105 }