ad701beefda8585e528855bb0a0e633c8e6a79bb
[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   case CC_CLOCK_INIT:
219   case CC_WRITE_FLASH_PAGE:
220   case CC_MASS_ERASE_FLASH:
221   case CC_PROGRAM_FLASH:
222     txdata(app,NOK,0);//TODO implement me.
223     break;
224   }
225 }
226
227 //! Erase all of a Chipcon's memory.
228 void cc_chip_erase(){
229   cmddata[0]=0x14;
230   cccmd(1);
231   ccread(1);
232 }
233 //! Write the configuration byte.
234 void cc_wr_config(unsigned char config){
235   cmddata[0]=0x1d;
236   cmddata[1]=config;
237   cccmd(2);
238   ccread(1);
239 }
240 //! Read the configuration byte.
241 unsigned char cc_rd_config(){
242   cmddata[0]=0x24;
243   cccmd(1);
244   ccread(1);
245   return cmddata[0];
246 }
247
248
249
250 //! Read the status register
251 unsigned char cc_read_status(){
252   cmddata[0]=0x34;
253   cccmd(1);
254   ccread(1);
255   return cmddata[0];
256 }
257
258 //! Read the CHIP ID bytes.
259 unsigned short cc_get_chip_id(){
260   unsigned short toret;
261   cmddata[0]=0x68;
262   cccmd(1);
263   ccread(2);
264   
265   //Return the word.
266   toret=cmddata[1];
267   toret=(toret<<8)+cmddata[1];
268   return toret;
269 }
270
271
272 //! Read the PC
273 unsigned short cc_get_pc(){
274   cmddata[0]=0x28;
275   cccmd(1);
276   ccread(2);
277   
278   //Return the word.
279   return cmddataword[0];
280 }
281
282 //! Set a hardware breakpoint.
283 void cc_set_hw_brkpnt(unsigned short adr){
284   cmddataword[0]=adr;
285   cccmd(2);
286   ccread(1);
287   return;
288 }
289
290
291 //! Halt the CPU.
292 void cc_halt(){
293   cmddata[0]=0x44;
294   cccmd(1);
295   ccread(1);
296   return;
297 }
298 //! Resume the CPU.
299 void cc_resume(){
300   cmddata[0]=0x4C;
301   cccmd(1);
302   ccread(1);
303   return;
304 }
305
306
307 //! Step an instruction
308 void cc_step_instr(){
309   cmddata[0]=0x5C;
310   cccmd(1);
311   ccread(1);
312   return;
313 }
314
315 //! Debug an instruction.
316 void cc_debug_instr(unsigned char len){
317   //Bottom two bits of command indicate length.
318   unsigned char cmd=0x54+(len&0x3);
319   CCWRITE;
320   cctrans8(cmd);  //Second command code
321   cccmd(len&0x3); //Command itself.
322   ccread(1);
323   return;
324 }
325
326 //! Debug an instruction, for local use.
327 unsigned char cc_debug(unsigned char len,
328               unsigned char a,
329               unsigned char b,
330               unsigned char c){
331   unsigned char cmd=0x54+(len&0x3);//(len&0x3);
332   CCWRITE;
333   cctrans8(cmd);
334   if(len--)
335     cctrans8(a);
336   if(len--)
337     cctrans8(b);
338   if(len--)
339     cctrans8(c);
340   CCREAD;
341   return cctrans8(0x00);
342 }
343
344 //! Fetch a byte of code memory.
345 unsigned char cc_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, (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(2, 0xE4, 0, 0);
361   //MOVC A, @A+DPTR;
362   toret=cc_debug(3, 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 cc_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 0;
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 cc_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 }