Partial refactoring, forgot spi.h early.
[goodfet] / firmware / apps / monitor / monitor.c
1 #include "command.h"
2 #include "platform.h"
3 #include "monitor.h"
4
5 //! Handles a monitor command.
6 void monitorhandle(unsigned char app,
7                    unsigned char verb,
8                    unsigned char len){
9   switch(verb){
10   case PEEK:
11     cmddata[0]=memorybyte[cmddataword[0]];
12     txdata(app,verb,1);
13     break;
14   case POKE:
15     //Todo, make word or byte.
16     memorybyte[cmddataword[0]]=cmddata[2];
17     cmddata[0]=memorybyte[cmddataword[0]];
18     txdata(app,verb,1);
19     break;
20   case MONITOR_CHANGE_BAUD:
21     //This command, and ONLY this command, does not reply.
22     setbaud(cmddata[0]);
23     //txdata(app,verb,0);
24     break;
25   case MONITOR_RAM_PATTERN:
26     monitor_ram_pattern();//reboots, will never return
27     break;
28   case MONITOR_RAM_DEPTH:
29     cmddataword[0]=monitor_ram_depth();
30     txdata(app,verb,2);
31     break;
32   case MONITOR_DIR:
33     P5DIR=cmddata[0];
34     txdata(app,verb,1);
35     break;
36   case MONITOR_IN:
37     cmddata[0]=P5IN;
38     txdata(app,verb,1);
39     break;
40   case MONITOR_OUT:
41     P5OUT=cmddata[0];
42     txdata(app,verb,1);
43     break;
44   }
45 }
46
47 //! Overwrite all of RAM with 0xBEEF, then reboot.
48 void monitor_ram_pattern(){
49   register int *a;
50   
51   //Wipe all of ram.
52   for(a=(int*)0x1100;a<(int*)0x2500;a++){//TODO get these from the linker.
53     *((int*)a) = 0xBEEF;
54   }
55   txdata(0x00,0x90,0);
56   
57   //Reboot
58   asm("br &0xfffe");
59 }
60
61 //! Return the number of contiguous bytes 0xBEEF, to measure RAM usage.
62 unsigned int monitor_ram_depth(){
63   register int a;
64   register int count=0;
65   for(a=0x1100;a<0x2500;a+=2)
66     if(*((int*)a)==0xBEEF) count+=2;
67   
68   return count;
69 }