Monitor echo function, for better synchronization.
[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 MONITOR_ECHO:
26     //Echo back the same buffer.
27     txdata(app,verb,len);
28     break;
29   case PEEK:
30     cmddata[0]=memorybyte[cmddataword[0]];
31     txdata(app,verb,1);
32     break;
33   case POKE:
34     //Todo, make word or byte.
35     memorybyte[cmddataword[0]]=cmddata[2];
36     cmddata[0]=memorybyte[cmddataword[0]];
37     txdata(app,verb,1);
38     break;
39   case CALL:
40     //Set the program counter to cmdword[0];
41     cmddataword[0]=fncall(cmddataword[0]);
42     txdata(app,verb,2);
43     break;
44   case EXEC:
45     //Execute the argument as code from RAM.
46     cmddataword[0]=fncall((u16) cmddataword);
47     txdata(app,verb,2);
48     break;
49   case MONITOR_SIZEBUF:
50     //TODO make the data length target-specific, varying by ram.
51     cmddataword[0]=0x100;
52     txdata(app,verb,2);
53     break;
54   case MONITOR_CHANGE_BAUD:
55     //This command, and ONLY this command, does not reply.
56     setbaud(cmddata[0]);
57     //txdata(app,verb,0);
58     break;
59   case MONITOR_RAM_PATTERN:
60     monitor_ram_pattern();//reboots, will never return
61     break;
62   case MONITOR_RAM_DEPTH:
63     cmddataword[0]=monitor_ram_depth();
64     txdata(app,verb,2);
65     break;
66   case MONITOR_DIR:
67     P5DIR=cmddata[0];
68     txdata(app,verb,1);
69     break;
70   case MONITOR_IN:
71     cmddata[0]=P5IN;
72     txdata(app,verb,1);
73     break;
74   case MONITOR_OUT:
75     P5OUT=cmddata[0];
76     txdata(app,verb,1);
77     break;
78   case MONITOR_SILENT:
79     silent=cmddata[0];
80     txdata(app,verb,1);
81     break;
82   case MONITOR_CONNECTED:
83     msp430_init_dco_done();
84     txdata(app,verb,0);
85     break;
86   }
87 }
88
89 //! Overwrite all of RAM with 0xBEEF, then reboot.
90 void monitor_ram_pattern(){
91   register int *a;
92   
93   //Wipe all of ram.
94   for(a=(int*)0x1100;a<(int*)0x2500;a++){//TODO get these from the linker.
95     *((int*)a) = 0xBEEF;
96   }
97   txdata(0x00,0x90,0);
98   
99   //Reboot
100   #ifdef MSP430
101   asm("br &0xfffe");
102   #endif
103 }
104
105 //! Return the number of contiguous bytes 0xBEEF, to measure RAM usage.
106 unsigned int monitor_ram_depth(){
107   register int a;
108   register int count=0;
109   for(a=0x1100;a<0x2500;a+=2)
110     if(*((int*)a)==0xBEEF) count+=2;
111   
112   return count;
113 }