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