135d2a608e53823ae204fdb660cd5eccefb8f79a
[goodfet] / chipcon.c
1 /*! \file chipcon.c
2   \author Travis Goodspeed
3   \brief Chipcon 8051 debugging.
4 */
5
6
7 //This is like SPI, except that you read or write, not both.
8
9 /* N.B. The READ verb performs a write of all (any) supplied data,
10    then reads a single byte reply from the target.  The WRITE verb
11    only writes.
12 */
13
14 #include "platform.h"
15 #include "command.h"
16 #include "chipcon.h"
17
18 #include <signal.h>
19 #include <io.h>
20 #include <iomacros.h>
21
22
23 /* Concerning clock rates, the maximimum clock rates are defined on
24    page 4 of the spec.  They vary, but are roughly 30MHz.  Raising
25    this clock rate might allow for clock glitching, but the GoodFET
26    isn't sufficient fast for that.  Perhaps a 200MHz ARM or an FPGA in
27    the BadassFET?
28 */
29
30 //Pins and I/O
31 //MISO and MOSI are the same pin, direction changes.
32 #define RST  BIT0
33 #define MOSI BIT2
34 #define MISO BIT2
35 #define SCK  BIT3
36
37 //This could be more accurate.
38 //Does it ever need to be?
39 #define CCSPEED 3
40 #define CCDELAY(x) delay(x)
41
42 #define SETMOSI P5OUT|=MOSI
43 #define CLRMOSI P5OUT&=~MOSI
44 #define SETCLK P5OUT|=SCK
45 #define CLRCLK P5OUT&=~SCK
46 #define READMISO (P5IN&MISO?1:0)
47
48 #define CCWRITE P5DIR|=MOSI
49 #define CCREAD P5DIR&=~MISO
50
51 //! Set up the pins for CC mode.  Does not init debugger.
52 void ccsetup(){
53   P5OUT|=MOSI+SCK+RST;
54   P5DIR|=MOSI+SCK+RST;
55   //P5DIR&=~MISO;  //MOSI is MISO
56 }
57
58 //! Initialize the debugger
59 void ccdebuginit(){
60   //Two positive debug clock pulses while !RST is low.
61   //Take RST low, pulse twice, then high.
62   P5OUT&=~SCK;
63   P5OUT&=~RST;
64   
65   //pulse twice
66   CCDELAY(CCSPEED);
67   P5OUT|=SCK;  //up
68   CCDELAY(CCSPEED);
69   P5OUT&=~SCK; //down
70   CCDELAY(CCSPEED);
71   P5OUT|=SCK;  //up
72   CCDELAY(CCSPEED);
73   P5OUT&=~SCK; //down
74   
75   //Raise !RST.
76   P5OUT|=RST;
77 }
78
79 //! Read and write a CC bit.
80 unsigned char cctrans8(unsigned char byte){
81   unsigned int bit;
82   //This function came from the SPI Wikipedia article.
83   //Minor alterations.
84   
85   for (bit = 0; bit < 8; bit++) {
86     /* write MOSI on trailing edge of previous clock */
87     if (byte & 0x80)
88       SETMOSI;
89     else
90       CLRMOSI;
91     byte <<= 1;
92  
93     /* half a clock cycle before leading/rising edge */
94     CCDELAY(CCSPEED/2);
95     SETCLK;
96  
97     /* half a clock cycle before trailing/falling edge */
98     CCDELAY(CCSPEED/2);
99  
100     /* read MISO on trailing edge */
101     byte |= READMISO;
102     CLRCLK;
103   }
104   
105   return byte;
106 }
107
108 //! Send a command from txbytes.
109 void cccmd(unsigned char len){
110   unsigned char i;
111   CCWRITE;
112   for(i=0;i<len;i++)
113     cctrans8(cmddata[i]);
114 }
115
116 //! Fetch a reply, usually 1 byte.
117 void ccread(unsigned char len){
118   unsigned char i;
119   CCREAD;
120   for(i=0;i<len;i++)
121     cmddata[i]=cctrans8(0);
122 }
123
124 //! Handles a monitor command.
125 void cchandle(unsigned char app,
126                unsigned char verb,
127                unsigned long len){
128   //Always init.  Might help with buggy lines.
129   //Might hurt too.
130   //ccdebuginit();
131   long i;
132   
133   switch(verb){
134     //CC_PEEK and CC_POKE will come later.
135   case PEEK:
136     cmddata[0]=cc_peekirambyte(cmddata[0]);
137     txdata(app,verb,1);
138     break;
139   case POKE:
140     cmddata[0]=cc_pokeirambyte(cmddata[0],cmddata[1]);
141     txdata(app,verb,0);
142     break;
143   case READ:  //Write a command and return 1-byte reply.
144     cccmd(len);
145     ccread(1);
146     txdata(app,verb,1);
147     break;
148   case WRITE: //Write a command with no reply.
149     cccmd(len);
150     txdata(app,verb,0);
151     break;
152   case START://enter debugger
153     ccsetup();
154     ccdebuginit();
155     txdata(app,verb,0);
156     break;
157   case STOP://exit debugger
158     //Take RST low, then high.
159     P5OUT&=~RST;
160     CCDELAY(CCSPEED);
161     P5OUT|=RST;
162     txdata(app,verb,0);
163     break;
164   case SETUP:
165     ccsetup();
166     txdata(app,verb,0);
167     break;
168     
169   //Micro commands!
170   case CC_CHIP_ERASE:
171     cc_chip_erase();
172     txdata(app,verb,1);
173     break;
174   case CC_WR_CONFIG:
175     cc_wr_config(cmddata[0]);
176     txdata(app,verb,1);
177     break;
178   case CC_RD_CONFIG:
179     cc_rd_config();
180     txdata(app,verb,1);
181     break;
182   case CC_GET_PC:
183     cc_get_pc();
184     txdata(app,verb,2);
185     break;
186   case CC_LOCKCHIP:
187     cc_lockchip();
188     //no break, return status
189   case CC_READ_STATUS:
190     cc_read_status();
191     txdata(app,verb,1);
192     break;
193   case CC_SET_HW_BRKPNT:
194     cc_set_hw_brkpnt(cmddataword[0]);
195     txdata(app,verb,1);
196     break;
197   case CC_HALT:
198     cc_halt();
199     txdata(app,verb,1);
200     break;
201   case CC_RESUME:
202     cc_resume();
203     txdata(app,verb,1);
204     break;
205   case CC_DEBUG_INSTR:
206     cc_debug_instr(len);
207     txdata(app,verb,1);
208     break;
209   case CC_STEP_INSTR:
210     cc_step_instr();
211     txdata(app,verb,1);
212     break;
213   case CC_STEP_REPLACE:
214     txdata(app,NOK,0);//TODO add me
215     break;
216   case CC_GET_CHIP_ID:
217     cc_get_chip_id();
218     txdata(app,verb,2);
219     break;
220
221
222   //Macro commands
223   case CC_READ_CODE_MEMORY:
224     cmddata[0]=cc_peekcodebyte(cmddataword[0]);
225     txdata(app,verb,1);
226     break;
227   case CC_READ_XDATA_MEMORY:
228     cmddata[0]=cc_peekdatabyte(cmddataword[0]);
229     txdata(app,verb,1);
230     break;
231   case CC_WRITE_XDATA_MEMORY:
232     cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
233     txdata(app,verb,1);
234     break;
235   case CC_SET_PC:
236     cc_set_pc(cmddatalong[0]);
237     txdata(app,verb,0);
238     break;
239   case CC_WRITE_FLASH_PAGE:
240     cc_write_flash_page(cmddatalong[0]);
241     txdata(app,verb,0);
242     break;
243   case CC_WIPEFLASHBUFFER:
244     for(i=0xf000;i<0xf800;i++)
245       cc_pokedatabyte(i,0xFF);
246     txdata(app,verb,0);
247     break;
248   case CC_MASS_ERASE_FLASH:
249   case CC_CLOCK_INIT:
250   case CC_PROGRAM_FLASH:
251     debugstr("This Chipcon command is not yet implemented.");
252     txdata(app,NOK,0);//TODO implement me.
253     break;
254   }
255 }
256
257 //! Set the Chipcon's Program Counter
258 void cc_set_pc(u32 adr){
259   cmddata[0]=0x02;             //SetPC
260   cmddata[1]=((adr>>8)&0xff);  //HIBYTE
261   cmddata[2]=adr&0xff;         //LOBYTE
262   cc_debug_instr(3);
263   return;
264 }
265
266 //! Erase all of a Chipcon's memory.
267 void cc_chip_erase(){
268   cmddata[0]=CCCMD_CHIP_ERASE; //0x14
269   cccmd(1);
270   ccread(1);
271 }
272 //! Write the configuration byte.
273 void cc_wr_config(unsigned char config){
274   cmddata[0]=CCCMD_WR_CONFIG; //0x1D
275   cmddata[1]=config;
276   cccmd(2);
277   ccread(1);
278 }
279
280 //! Locks the chip.
281 void cc_lockchip(){
282   register int i;
283   
284   debugstr("Locking chip.");
285   cc_wr_config(1);//Select Info Flash 
286   if(!(cc_rd_config()&1))
287     debugstr("Config forgotten!");
288   
289   //Clear config page.
290   for(i=0;i<2048;i++)
291     cc_pokedatabyte(0xf000+i,0);
292   cc_write_flash_page(0);
293   if(cc_peekcodebyte(0))
294     debugstr("Failed to clear info flash byte.");
295   
296   cc_wr_config(0);  
297   if(cc_rd_config()&1)
298     debugstr("Stuck in info flash mode!");
299 }
300
301 //! Read the configuration byte.
302 unsigned char cc_rd_config(){
303   cmddata[0]=CCCMD_RD_CONFIG; //0x24
304   cccmd(1);
305   ccread(1);
306   return cmddata[0];
307 }
308
309
310 //! Read the status register
311 unsigned char cc_read_status(){
312   cmddata[0]=CCCMD_READ_STATUS; //0x3f
313   cccmd(1);
314   ccread(1);
315   return cmddata[0];
316 }
317
318 //! Read the CHIP ID bytes.
319 unsigned short cc_get_chip_id(){
320   unsigned short toret;
321   cmddata[0]=CCCMD_GET_CHIP_ID; //0x68
322   cccmd(1);
323   ccread(2);
324   
325   //Return the word.
326   toret=cmddata[1];
327   toret=(toret<<8)+cmddata[1];
328   return toret;
329 }
330
331 //! Populates flash buffer in xdata.
332 void cc_write_flash_buffer(u8 *data, u16 len){
333   cc_write_xdata(0xf000, data, len);
334 }
335 //! Populates flash buffer in xdata.
336 void cc_write_xdata(u16 adr, u8 *data, u16 len){
337   u16 i;
338   for(i=0; i<len; i++){
339     cc_pokedatabyte(adr+i,
340                     data[i]);
341   }
342 }
343
344
345 //32-bit words, 2KB pages
346 #define HIBYTE_WORDS_PER_FLASH_PAGE 0x02
347 #define LOBYTE_WORDS_PER_FLASH_PAGE 0x00
348 #define FLASHPAGE_SIZE 0x800
349
350 //32 bit words
351 #define FLASH_WORD_SIZE 0x4
352
353 const u8 flash_routine[] = {
354   //MOV FADDRH, #imm; 
355   0x75, 0xAD,
356   0x00,//#imm=((address >> 8) / FLASH_WORD_SIZE) & 0x7E,
357   
358   0x75, 0xAC, 0x00,                                          //                 MOV FADDRL, #00; 
359   /* Erase page. */
360   0x75, 0xAE, 0x01,                                          //                 MOV FLC, #01H; // ERASE 
361                                                              //                 ; Wait for flash erase to complete 
362   0xE5, 0xAE,                                                // eraseWaitLoop:  MOV A, FLC; 
363   0x20, 0xE7, 0xFB,                                          //                 JB ACC_BUSY, eraseWaitLoop; 
364   /* End erase page. */
365                                                              //                 ; Initialize the data pointer 
366   0x90, 0xF0, 0x00,                                          //                 MOV DPTR, #0F000H; 
367                                                              //                 ; Outer loops 
368   0x7F, HIBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R7, #imm; 
369   0x7E, LOBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R6, #imm; 
370   0x75, 0xAE, 0x02,                                          //                 MOV FLC, #02H; // WRITE 
371                                                              //                     ; Inner loops 
372   0x7D, FLASH_WORD_SIZE,                                     // writeLoop:          MOV R5, #imm; 
373   0xE0,                                                      // writeWordLoop:          MOVX A, @DPTR; 
374   0xA3,                                                      //                         INC DPTR; 
375   0xF5, 0xAF,                                                //                         MOV FWDATA, A;  
376   0xDD, 0xFA,                                                //                     DJNZ R5, writeWordLoop; 
377                                                              //                     ; Wait for completion 
378   0xE5, 0xAE,                                                // writeWaitLoop:      MOV A, FLC; 
379   0x20, 0xE6, 0xFB,                                          //                     JB ACC_SWBSY, writeWaitLoop; 
380   0xDE, 0xF1,                                                //                 DJNZ R6, writeLoop; 
381   0xDF, 0xEF,                                                //                 DJNZ R7, writeLoop; 
382                                                              //                 ; Done, fake a breakpoint 
383   0xA5                                                       //                 DB 0xA5; 
384 }; 
385
386
387 //! Copies flash buffer to flash.
388 void cc_write_flash_page(u32 adr){
389   //Assumes that page has already been written to XDATA 0xF000
390   //debugstr("Flashing 2kb at 0xF000 to given adr.");
391   
392   if(adr&(FLASHPAGE_SIZE-1)){
393     debugstr("Flash page address is not on a multiple of 2kB.  Aborting.");
394     return;
395   }
396   
397   //Routine comes next
398   //WRITE_XDATA_MEMORY(IN: 0xF000 + FLASH_PAGE_SIZE, sizeof(routine), routine);
399   cc_write_xdata(0xF000+FLASHPAGE_SIZE,
400                  (u8*) flash_routine, sizeof(flash_routine));
401   //Patch routine's third byte with
402   //((address >> 8) / FLASH_WORD_SIZE) & 0x7E
403   cc_pokedatabyte(0xF000+FLASHPAGE_SIZE+2,
404                   ((adr>>8)/FLASH_WORD_SIZE)&0x7E);
405   //debugstr("Wrote flash routine.");
406   
407   
408   //MOV MEMCTR, (bank * 16) + 1;
409   cmddata[0]=0x75;
410   cmddata[1]=0xc7;
411   cmddata[2]=0x51;
412   cc_debug_instr(3);
413   debugstr("Loaded bank info.");
414   
415   cc_set_pc(0xf000+FLASHPAGE_SIZE);//execute code fragment
416   cc_resume();
417   
418   debugstr("Executing.");
419   
420   
421   while(!(cc_read_status()&CC_STATUS_CPUHALTED)){
422     P1OUT^=1;//blink LED while flashing
423   }
424   
425   
426   debugstr("Done flashing.");
427   
428   P1OUT&=~1;//clear LED
429 }
430
431 //! Read the PC
432 unsigned short cc_get_pc(){
433   cmddata[0]=CCCMD_GET_PC; //0x28
434   cccmd(1);
435   ccread(2);
436   
437   //Return the word.
438   return cmddataword[0];
439 }
440
441 //! Set a hardware breakpoint.
442 void cc_set_hw_brkpnt(unsigned short adr){
443   debugstr("FIXME: This certainly won't work.");
444   cmddataword[0]=adr;
445   cccmd(2);
446   ccread(1);
447   return;
448 }
449
450
451 //! Halt the CPU.
452 void cc_halt(){
453   cmddata[0]=CCCMD_HALT; //0x44
454   cccmd(1);
455   ccread(1);
456   return;
457 }
458 //! Resume the CPU.
459 void cc_resume(){
460   cmddata[0]=CCCMD_RESUME; //0x4C
461   cccmd(1);
462   ccread(1);
463   return;
464 }
465
466
467 //! Step an instruction
468 void cc_step_instr(){
469   cmddata[0]=CCCMD_STEP_INSTR; //0x5C
470   cccmd(1);
471   ccread(1);
472   return;
473 }
474
475 //! Debug an instruction.
476 void cc_debug_instr(unsigned char len){
477   //Bottom two bits of command indicate length.
478   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3); //0x54+len
479   CCWRITE;
480   cctrans8(cmd);  //Second command code
481   cccmd(len&0x3); //Command itself.
482   ccread(1);
483   return;
484 }
485
486 //! Debug an instruction, for local use.
487 unsigned char cc_debug(unsigned char len,
488               unsigned char a,
489               unsigned char b,
490               unsigned char c){
491   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3);//0x54+len
492   CCWRITE;
493   cctrans8(cmd);
494   if(len>0)
495     cctrans8(a);
496   if(len>1)
497     cctrans8(b);
498   if(len>2)
499     cctrans8(c);
500   CCREAD;
501   return cctrans8(0x00);
502 }
503
504 //! Fetch a byte of code memory.
505 unsigned char cc_peekcodebyte(unsigned long adr){
506   /** See page 9 of SWRA124 */
507   unsigned char bank=adr>>15,
508     lb=adr&0xFF,
509     hb=(adr>>8)&0x7F,
510     toret=0;
511   adr&=0x7FFF;
512   
513   //MOV MEMCTR, (bank*16)+1
514   cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
515   //MOV DPTR, address
516   cc_debug(3, 0x90, hb, lb);
517   
518   //for each byte
519   //CLR A
520   cc_debug(2, 0xE4, 0, 0);
521   //MOVC A, @A+DPTR;
522   toret=cc_debug(3, 0x93, 0, 0);
523   //INC DPTR
524   //cc_debug(1, 0xA3, 0, 0);
525   
526   return toret;
527 }
528
529
530 //! Set a byte of data memory.
531 unsigned char cc_pokedatabyte(unsigned int adr,
532                            unsigned char val){
533   unsigned char
534     hb=(adr&0xFF00)>>8,
535     lb=adr&0xFF;
536   
537   //MOV DPTR, adr
538   cc_debug(3, 0x90, hb, lb);
539   //MOV A, val
540   cc_debug(2, 0x74, val, 0);
541   //MOVX @DPTR, A
542   cc_debug(1, 0xF0, 0, 0);
543   
544   return 0;
545   /*
546 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
547 for (n = 0; n < count; n++) {
548     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
549     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
550     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
551 }
552    */
553 }
554
555 //! Fetch a byte of data memory.
556 unsigned char cc_peekdatabyte(unsigned int adr){
557   unsigned char
558     hb=(adr&0xFF00)>>8,
559     lb=adr&0xFF;
560   
561   //MOV DPTR, adr
562   cc_debug(3, 0x90, hb, lb);
563   //MOVX A, @DPTR
564   //Must be 2, perhaps for clocking?
565   return cc_debug(3, 0xE0, 0, 0);
566 }
567
568
569 //! Fetch a byte of IRAM.
570 u8 cc_peekirambyte(u8 adr){
571   //CLR A
572   cc_debug(2, 0xE4, 0, 0);
573   //MOV A, #iram
574   return cc_debug(3, 0xE5, adr, 0);
575 }
576
577 //! Write a byte of IRAM.
578 u8 cc_pokeirambyte(u8 adr, u8 val){
579   //CLR A
580   cc_debug(2, 0xE4, 0, 0);
581   //MOV #iram, #val
582   return cc_debug(3, 0x75, adr, val);
583   //return cc_debug(3, 0x75, val, adr);
584 }
585
586