149962939837a68ac1fab489fca96b61e990a1ab
[goodfet] / firmware / apps / chipcon / chipcon.c
1 //GoodFET ChipCon Debugging Application
2 //Handles basic I/O for the Chipcon 8051 debugging protocol.
3
4 //Higher level left to client application.
5
6 //This is like SPI, except that you read or write, not both.
7
8 /** N.B. The READ verb performs a write of all (any) supplied data,
9     then reads a single byte reply from the target.  The WRITE verb
10     only writes.
11 */
12
13 //This is REALLY untested.
14
15 #include "platform.h"
16 #include "command.h"
17 #include "chipcon.h"
18
19 #include <signal.h>
20 #include <io.h>
21 #include <iomacros.h>
22
23
24 /** Concerning clock rates,
25     the maximimum clock rates are defined on page 4 of the spec.
26     They vary, but are roughly 30MHz.  Raising this clock rate might
27     allow for clock glitching, but the GoodFET isn't sufficient fast for that.
28     Perhaps a 200MHz ARM or an FPGA in the BadassFET?
29 */
30
31 //Pins and I/O
32 //MISO and MOSI are the same pin, direction changes.
33 #define RST  BIT0
34 #define MOSI BIT2
35 #define MISO BIT2
36 #define SCK  BIT3
37
38 //This could be more accurate.
39 //Does it ever need to be?
40 #define CCSPEED 0
41 #define CCDELAY(x) delay(x)
42
43 #define SETMOSI P5OUT|=MOSI
44 #define CLRMOSI P5OUT&=~MOSI
45 #define SETCLK P5OUT|=SCK
46 #define CLRCLK P5OUT&=~SCK
47 #define READMISO (P5IN&MISO?1:0)
48
49 #define CCWRITE P5DIR|=MOSI
50 #define CCREAD P5DIR&=~MISO
51
52 //! Set up the pins for CC mode.  Does not init debugger.
53 void ccsetup(){
54   P5OUT|=MOSI+SCK+RST;
55   P5DIR|=MOSI+SCK+RST;
56   //P5DIR&=~MISO;  //MOSI is MISO
57 }
58
59 //! Initialize the debugger
60 void ccdebuginit(){
61   //Two positive debug clock pulses while !RST is low.
62   //Take RST low, pulse twice, then high.
63   P5OUT&=~SCK;
64   P5OUT&=~RST;
65   
66   //pulse twice
67   CCDELAY(CCSPEED);
68   P5OUT|=SCK;  //up
69   CCDELAY(CCSPEED);
70   P5OUT&=~SCK; //down
71   CCDELAY(CCSPEED);
72   P5OUT|=SCK;  //up
73   CCDELAY(CCSPEED);
74   P5OUT&=~SCK; //down
75   
76   //Raise !RST.
77   P5OUT|=RST;
78 }
79
80 //! Read and write a CC bit.
81 unsigned char cctrans8(unsigned char byte){
82   unsigned int bit;
83   //This function came from the SPI Wikipedia article.
84   //Minor alterations.
85   
86   for (bit = 0; bit < 8; bit++) {
87     /* write MOSI on trailing edge of previous clock */
88     if (byte & 0x80)
89       SETMOSI;
90     else
91       CLRMOSI;
92     byte <<= 1;
93  
94     /* half a clock cycle before leading/rising edge */
95     CCDELAY(CCSPEED/2);
96     SETCLK;
97  
98     /* half a clock cycle before trailing/falling edge */
99     CCDELAY(CCSPEED/2);
100  
101     /* read MISO on trailing edge */
102     byte |= READMISO;
103     CLRCLK;
104   }
105   
106   return byte;
107 }
108
109 //! Send a command from txbytes.
110 void cccmd(unsigned char len){
111   unsigned char i;
112   CCWRITE;
113   for(i=0;i<len;i++)
114     cctrans8(cmddata[i]);
115 }
116
117 //! Fetch a reply, usually 1 byte.
118 void ccread(unsigned char len){
119   unsigned char i;
120   CCREAD;
121   for(i=0;i<len;i++)
122     cmddata[i]=cctrans8(0);
123 }
124
125 //! Handles a monitor command.
126 void cchandle(unsigned char app,
127                unsigned char verb,
128                unsigned char len){
129   switch(verb){
130     //PEEK and POKE will come later.
131   case READ:  //Write a command and return 1-byte reply.
132     cccmd(len);
133     ccread(1);
134     txdata(app,verb,1);
135     break;
136   case WRITE: //Write a command with no reply.
137     cccmd(len);
138     txdata(app,verb,0);
139     break;
140   case START://enter debugger
141     ccdebuginit();
142     txdata(app,verb,0);
143     break;
144   case STOP://exit debugger
145     //Take RST low, then high.
146     P5OUT&=~RST;
147     CCDELAY(CCSPEED);
148     P5OUT|=RST;
149     txdata(app,verb,0);
150     break;
151   case SETUP:
152     ccsetup();
153     txdata(app,verb,0);
154     break;
155     
156   //Micro commands!
157   case CC_CHIP_ERASE:
158     cc_chip_erase();
159     txdata(app,verb,1);
160     break;
161   case CC_WR_CONFIG:
162     cc_wr_config(cmddata[0]);
163     txdata(app,verb,1);
164     break;
165   case CC_RD_CONFIG:
166     cc_rd_config();
167     txdata(app,verb,1);
168     break;
169   case CC_GET_PC:
170     cc_get_pc();
171     txdata(app,verb,2);
172     break;
173   case CC_READ_STATUS:
174     cc_read_status();
175     txdata(app,verb,1);
176     break;
177   case CC_SET_HW_BRKPNT:
178     cc_set_hw_brkpnt(cmddataword[0]);
179     txdata(app,verb,1);
180     break;
181   case CC_HALT:
182     cc_halt();
183     txdata(app,verb,1);
184     break;
185   case CC_RESUME:
186     cc_resume();
187     txdata(app,verb,1);
188     break;
189   case CC_DEBUG_INSTR:
190     cc_debug_instr(len);
191     txdata(app,verb,1);
192     break;
193   case CC_STEP_INSTR:
194     cc_step_instr();
195     txdata(app,verb,1);
196     break;
197   case CC_STEP_REPLACE:
198     txdata(app,NOK,0);//TODO add me
199     break;
200   case CC_GET_CHIP_ID:
201     cc_get_chip_id();
202     txdata(app,verb,2);
203     break;
204
205
206   //Macro commands
207   case CC_READ_CODE_MEMORY:
208     cmddata[0]=peekcodebyte(cmddataword[0]);
209     txdata(app,verb,1);
210     break;
211   case CC_READ_XDATA_MEMORY:
212     cmddata[0]=peekdatabyte(cmddataword[0]);
213     txdata(app,verb,1);
214     break;
215   case CC_WRITE_XDATA_MEMORY:
216     cmddata[0]=pokedatabyte(cmddataword[0], cmddata[2]);
217     txdata(app,verb,1);
218     break;
219   case CC_SET_PC:
220   case CC_CLOCK_INIT:
221   case CC_WRITE_FLASH_PAGE:
222   case CC_MASS_ERASE_FLASH:
223   case CC_PROGRAM_FLASH:
224     txdata(app,NOK,0);//TODO implement me.
225     break;
226   }
227 }
228
229 //! Erase all of a Chipcon's memory.
230 void cc_chip_erase(){
231   cmddata[0]=0x14;
232   cccmd(1);
233   ccread(1);
234 }
235 //! Write the configuration byte.
236 void cc_wr_config(unsigned char config){
237   cmddata[0]=0x1d;
238   cmddata[1]=config;
239   cccmd(2);
240   ccread(1);
241 }
242 //! Read the configuration byte.
243 unsigned char cc_rd_config(){
244   cmddata[0]=0x24;
245   cccmd(1);
246   ccread(1);
247   return cmddata[0];
248 }
249
250
251
252 //! Read the status register
253 unsigned char cc_read_status(){
254   cmddata[0]=0x34;
255   cccmd(1);
256   ccread(1);
257   return cmddata[0];
258 }
259
260 //! Read the CHIP ID bytes.
261 unsigned short cc_get_chip_id(){
262   unsigned short toret;
263   cmddata[0]=0x68;
264   cccmd(1);
265   ccread(2);
266   
267   //Return the word.
268   toret=cmddata[1];
269   toret=(toret<<8)+cmddata[1];
270   return toret;
271 }
272
273
274 //! Read the PC
275 unsigned short cc_get_pc(){
276   cmddata[0]=0x28;
277   cccmd(1);
278   ccread(2);
279   
280   //Return the word.
281   return cmddataword[0];
282 }
283
284
285 //! Set a hardware breakpoint.
286 void cc_set_hw_brkpnt(unsigned short adr){
287   cmddataword[0]=adr;
288   cccmd(2);
289   ccread(1);
290   return;
291 }
292
293
294 //! Halt the CPU.
295 void cc_halt(){
296   cmddata[0]=0x44;
297   cccmd(1);
298   ccread(1);
299   return;
300 }
301 //! Resume the CPU.
302 void cc_resume(){
303   cmddata[0]=0x4C;
304   cccmd(1);
305   ccread(1);
306   return;
307 }
308
309
310 //! Step an instruction
311 void cc_step_instr(){
312   cmddata[0]=0x5C;
313   cccmd(1);
314   ccread(1);
315   return;
316 }
317
318 //! Debug an instruction.
319 void cc_debug_instr(unsigned char len){
320   //Bottom two bits of command indicate length.
321   unsigned char cmd=0x54+(len&0x3);
322   CCWRITE;
323   cctrans8(cmd);  //Second command code
324   cccmd(len&0x3); //Command itself.
325   ccread(1);
326   return;
327 }
328
329 //! Debug an instruction, for local use.
330 unsigned char cc_debug(unsigned char len,
331               unsigned char a,
332               unsigned char b,
333               unsigned char c){
334   unsigned char cmd=0x54+0x3;//(len&0x3);
335   CCWRITE;
336   cctrans8(cmd);
337   /*if(len--)*/ cctrans8(a);
338   if(len--) cctrans8(b);
339   if(len--) cctrans8(c);
340   CCREAD;
341   return cctrans8(0x00);
342 }
343
344 //! Fetch a byte of code memory.
345 unsigned char peekcodebyte(unsigned long adr){
346   /** See page 9 of SWRA124 */
347   unsigned char bank=adr>>15,
348     lb=adr&0xFF,
349     hb=(adr>>8)&0x7F,
350     toret=0;
351   adr&=0x7FFF;
352   
353   //MOV MEMCTR, (bank*16)+1
354   cc_debug(3, 0x75, 0xC7, 0);//(bank<<4) + 1);
355   //MOV DPTR, address
356   cc_debug(3, 0x90, hb, lb);
357   
358   //for each byte
359   //CLR A
360   cc_debug(1, 0xE4, 0, 0);
361   //MOVC A, @A+DPTR;
362   toret=cc_debug(1, 0x93, 0, 0);
363   //INC DPTR
364   //cc_debug(1, 0xA3, 0, 0);
365   
366   return toret;
367 }
368
369
370 //! Set a byte of data memory.
371 unsigned char pokedatabyte(unsigned int adr,
372                            unsigned char val){
373   unsigned char
374     hb=(adr&0xFF00)>>8,
375     lb=adr&0xFF;
376   
377   //MOV DPTR, adr
378   cc_debug(3, 0x90, hb, lb);
379   //MOV A, val
380   cc_debug(2, 0x74, val, 0);
381   //MOVX @DPTR, A
382   cc_debug(1, 0xF0, 0, 0);
383   
384   return;
385   /*
386 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
387 for (n = 0; n < count; n++) {
388     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
389     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
390     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
391 }
392    */
393 }
394
395 //! Fetch a byte of data memory.
396 unsigned char peekdatabyte(unsigned int adr){
397   unsigned char
398     hb=(adr&0xFF00)>>8,
399     lb=adr&0xFF,
400     toret;
401
402   //MOV DPTR, adr
403   cc_debug(3, 0x90, hb, lb);
404   //MOVX A, @DPTR
405   //Must be 2, perhaps for clocking?
406   toret=cc_debug(3, 0xE0, 0, 0);
407   return toret;
408   
409     /*
410 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
411 for (n = 0; n < count; n++) {
412     DEBUG_INSTR(IN: 0xE0, OUT: outputArray[n]);
413     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
414 }
415   */
416 }