0ff9f0307b1b8348de9722f0de92ebb559740777
[goodfet] / firmware / apps / chipcon / 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,
24    the maximimum clock rates are defined on page 4 of the spec.
25    They vary, but are roughly 30MHz.  Raising this clock rate might
26    allow for clock glitching, but the GoodFET isn't sufficient fast for that.
27    Perhaps a 200MHz ARM or an FPGA in 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   ccdebuginit();
130   
131   switch(verb){
132     //CC_PEEK and CC_POKE will come later.
133   case READ:  //Write a command and return 1-byte reply.
134     cccmd(len);
135     ccread(1);
136     txdata(app,verb,1);
137     break;
138   case WRITE: //Write a command with no reply.
139     cccmd(len);
140     txdata(app,verb,0);
141     break;
142   case START://enter debugger
143     ccdebuginit();
144     txdata(app,verb,0);
145     break;
146   case STOP://exit debugger
147     //Take RST low, then high.
148     P5OUT&=~RST;
149     CCDELAY(CCSPEED);
150     P5OUT|=RST;
151     txdata(app,verb,0);
152     break;
153   case SETUP:
154     ccsetup();
155     txdata(app,verb,0);
156     break;
157     
158   //Micro commands!
159   case CC_CHIP_ERASE:
160     cc_chip_erase();
161     txdata(app,verb,1);
162     break;
163   case CC_WR_CONFIG:
164     cc_wr_config(cmddata[0]);
165     txdata(app,verb,1);
166     break;
167   case CC_RD_CONFIG:
168     cc_rd_config();
169     txdata(app,verb,1);
170     break;
171   case CC_GET_PC:
172     cc_get_pc();
173     txdata(app,verb,2);
174     break;
175   case CC_READ_STATUS:
176     cc_read_status();
177     txdata(app,verb,1);
178     break;
179   case CC_SET_HW_BRKPNT:
180     cc_set_hw_brkpnt(cmddataword[0]);
181     txdata(app,verb,1);
182     break;
183   case CC_HALT:
184     cc_halt();
185     txdata(app,verb,1);
186     break;
187   case CC_RESUME:
188     cc_resume();
189     txdata(app,verb,1);
190     break;
191   case CC_DEBUG_INSTR:
192     cc_debug_instr(len);
193     txdata(app,verb,1);
194     break;
195   case CC_STEP_INSTR:
196     cc_step_instr();
197     txdata(app,verb,1);
198     break;
199   case CC_STEP_REPLACE:
200     txdata(app,NOK,0);//TODO add me
201     break;
202   case CC_GET_CHIP_ID:
203     cc_get_chip_id();
204     txdata(app,verb,2);
205     break;
206
207
208   //Macro commands
209   case CC_READ_CODE_MEMORY:
210     cmddata[0]=cc_peekcodebyte(cmddataword[0]);
211     txdata(app,verb,1);
212     break;
213   case CC_READ_XDATA_MEMORY:
214     cmddata[0]=cc_peekdatabyte(cmddataword[0]);
215     txdata(app,verb,1);
216     break;
217   case CC_WRITE_XDATA_MEMORY:
218     cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
219     txdata(app,verb,1);
220     break;
221   case CC_SET_PC:
222     cc_set_pc(cmddatalong[0]);
223     txdata(app,verb,0);
224     break;
225   case CC_WRITE_FLASH_PAGE:
226     cc_write_flash_page(cmddatalong[0]);
227     txdata(app,verb,0);
228     break;
229   case CC_MASS_ERASE_FLASH:
230   case CC_CLOCK_INIT:
231   case CC_PROGRAM_FLASH:
232     debugstr("This Chipcon command is not yet implemented.");
233     txdata(app,NOK,0);//TODO implement me.
234     break;
235   }
236 }
237
238 //! Set the Chipcon's Program Counter
239 void cc_set_pc(u32 adr){
240   cmddata[0]=0x02;           //GetPC
241   cmddata[1]=(adr>>8)&0xff;  //HIBYTE
242   cmddata[2]=adr&0xff;       //LOBYTE
243   return;
244 }
245
246 //! Erase all of a Chipcon's memory.
247 void cc_chip_erase(){
248   cmddata[0]=CCCMD_CHIP_ERASE; //0x14
249   cccmd(1);
250   ccread(1);
251 }
252 //! Write the configuration byte.
253 void cc_wr_config(unsigned char config){
254   cmddata[0]=CCCMD_WR_CONFIG; //0x1D
255   cmddata[1]=config;
256   cccmd(2);
257   ccread(1);
258 }
259 //! Read the configuration byte.
260 unsigned char cc_rd_config(){
261   cmddata[0]=CCCMD_RD_CONFIG; //0x24
262   cccmd(1);
263   ccread(1);
264   return cmddata[0];
265 }
266
267
268 //! Read the status register
269 unsigned char cc_read_status(){
270   cmddata[0]=CCCMD_READ_STATUS; //0x3f
271   cccmd(1);
272   ccread(1);
273   return cmddata[0];
274 }
275
276 //! Read the CHIP ID bytes.
277 unsigned short cc_get_chip_id(){
278   unsigned short toret;
279   cmddata[0]=CCCMD_GET_CHIP_ID; //0x68
280   cccmd(1);
281   ccread(2);
282   
283   //Return the word.
284   toret=cmddata[1];
285   toret=(toret<<8)+cmddata[1];
286   return toret;
287 }
288
289 //! Populates flash buffer in xdata.
290 void cc_write_flash_buffer(u8 *data, u16 len){
291   cc_write_xdata(0xf000, data, len);
292 }
293 //! Populates flash buffer in xdata.
294 void cc_write_xdata(u16 adr, u8 *data, u16 len){
295   u16 i;
296   for(i=0; i<len; i++){
297     cc_pokedatabyte(adr+i,
298                     data[i]);
299   }
300 }
301
302
303 //256 words/page
304 #define HIBYTE_WORDS_PER_FLASH_PAGE 0x00
305 #define LOBYTE_WORDS_PER_FLASH_PAGE 0x80
306 #define FLASHPAGE_SIZE 0x100
307 //2 bytes/word
308 #define FLASH_WORD_SIZE 0x2
309
310 const u8 flash_routine[] = {
311   //MOV FADDRH, #imm; 
312   0x75, 0xAD,
313   0x00,//#imm=((address >> 8) / FLASH_WORD_SIZE) & 0x7E,
314   
315   0x75, 0xAC, 0x00,                                          //                 MOV FADDRL, #00; 
316   /* Erase page. *
317   0x75, 0xAE, 0x01,                                          //                 MOV FLC, #01H; // ERASE 
318                                                              //                 ; Wait for flash erase to complete 
319   0xE5, 0xAE,                                                // eraseWaitLoop:  MOV A, FLC; 
320   0x20, 0xE7, 0xFB,                                          //                 JB ACC_BUSY, eraseWaitLoop; 
321   */
322                                                              //                 ; Initialize the data pointer 
323   0x90, 0xF0, 0x00,                                          //                 MOV DPTR, #0F000H; 
324                                                              //                 ; Outer loops 
325   0x7F, HIBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R7, #imm; 
326   0x7E, LOBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R6, #imm; 
327   0x75, 0xAE, 0x02,                                          //                 MOV FLC, #02H; // WRITE 
328                                                              //                     ; Inner loops 
329   0x7D, FLASH_WORD_SIZE,                                     // writeLoop:          MOV R5, #imm; 
330   0xE0,                                                      // writeWordLoop:          MOVX A, @DPTR; 
331   0xA3,                                                      //                         INC DPTR; 
332   0xF5, 0xAF,                                                //                         MOV FWDATA, A;  
333   0xDD, 0xFA,                                                //                     DJNZ R5, writeWordLoop; 
334                                                              //                     ; Wait for completion 
335   0xE5, 0xAE,                                                // writeWaitLoop:      MOV A, FLC; 
336   0x20, 0xE6, 0xFB,                                          //                     JB ACC_SWBSY, writeWaitLoop; 
337   0xDE, 0xF1,                                                //                 DJNZ R6, writeLoop; 
338   0xDF, 0xEF,                                                //                 DJNZ R7, writeLoop; 
339                                                              //                 ; Done, fake a breakpoint 
340   0xA5                                                       //                 DB 0xA5; 
341 }; 
342
343 //! Copies flash buffer to flash.
344 void cc_write_flash_page(u32 adr){
345   //Assumes that page has already been written to XDATA 0xF000
346   
347   //Routine comes next
348   //WRITE_XDATA_MEMORY(IN: 0xF000 + FLASH_PAGE_SIZE, sizeof(routine), routine);
349   cc_write_xdata(0xF000+FLASHPAGE_SIZE,
350                  (u8*) flash_routine, sizeof(flash_routine));
351   //Patch routine's third byte with
352   //((address >> 8) / FLASH_WORD_SIZE) & 0x7E
353   cc_pokedatabyte(0xF000+FLASHPAGE_SIZE+2,
354                   ((adr>>8)/FLASH_WORD_SIZE)&0x7E);
355   cc_debug(3, //MOV MEMCTR, (bank * 16) + 1;
356            0x75, 0xc7, 0x51);
357   cc_set_pc(0xf000+FLASHPAGE_SIZE);//execute code fragment
358   cc_resume();
359   while(!(cc_read_status()&CC_STATUS_CPUHALTED)){
360     P1OUT^=1;//blink LED
361   }
362   
363   P1OUT&=~1;//clear LED
364 }
365
366 //! Read the PC
367 unsigned short cc_get_pc(){
368   cmddata[0]=CCCMD_GET_PC; //0x28
369   cccmd(1);
370   ccread(2);
371   
372   //Return the word.
373   return cmddataword[0];
374 }
375
376 //! Set a hardware breakpoint.
377 void cc_set_hw_brkpnt(unsigned short adr){
378   debugstr("FIXME: This certainly won't work.");
379   cmddataword[0]=adr;
380   cccmd(2);
381   ccread(1);
382   return;
383 }
384
385
386 //! Halt the CPU.
387 void cc_halt(){
388   cmddata[0]=CCCMD_HALT; //0x44
389   cccmd(1);
390   ccread(1);
391   return;
392 }
393 //! Resume the CPU.
394 void cc_resume(){
395   cmddata[0]=CCCMD_RESUME; //0x4C
396   cccmd(1);
397   ccread(1);
398   return;
399 }
400
401
402 //! Step an instruction
403 void cc_step_instr(){
404   cmddata[0]=CCCMD_STEP_INSTR; //0x5C
405   cccmd(1);
406   ccread(1);
407   return;
408 }
409
410 //! Debug an instruction.
411 void cc_debug_instr(unsigned char len){
412   //Bottom two bits of command indicate length.
413   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3); //0x54+len
414   CCWRITE;
415   cctrans8(cmd);  //Second command code
416   cccmd(len&0x3); //Command itself.
417   ccread(1);
418   return;
419 }
420
421 //! Debug an instruction, for local use.
422 unsigned char cc_debug(unsigned char len,
423               unsigned char a,
424               unsigned char b,
425               unsigned char c){
426   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3);//0x54+len
427   CCWRITE;
428   cctrans8(cmd);
429   if(len--)
430     cctrans8(a);
431   if(len--)
432     cctrans8(b);
433   if(len--)
434     cctrans8(c);
435   CCREAD;
436   return cctrans8(0x00);
437 }
438
439 //! Fetch a byte of code memory.
440 unsigned char cc_peekcodebyte(unsigned long adr){
441   /** See page 9 of SWRA124 */
442   unsigned char bank=adr>>15,
443     lb=adr&0xFF,
444     hb=(adr>>8)&0x7F,
445     toret=0;
446   adr&=0x7FFF;
447   
448   //MOV MEMCTR, (bank*16)+1
449   cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
450   //MOV DPTR, address
451   cc_debug(3, 0x90, hb, lb);
452   
453   //for each byte
454   //CLR A
455   cc_debug(2, 0xE4, 0, 0);
456   //MOVC A, @A+DPTR;
457   toret=cc_debug(3, 0x93, 0, 0);
458   //INC DPTR
459   //cc_debug(1, 0xA3, 0, 0);
460   
461   return toret;
462 }
463
464
465 //! Set a byte of data memory.
466 unsigned char cc_pokedatabyte(unsigned int adr,
467                            unsigned char val){
468   unsigned char
469     hb=(adr&0xFF00)>>8,
470     lb=adr&0xFF;
471   
472   //MOV DPTR, adr
473   cc_debug(3, 0x90, hb, lb);
474   //MOV A, val
475   cc_debug(2, 0x74, val, 0);
476   //MOVX @DPTR, A
477   cc_debug(1, 0xF0, 0, 0);
478   
479   return 0;
480   /*
481 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
482 for (n = 0; n < count; n++) {
483     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
484     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
485     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
486 }
487    */
488 }
489
490 //! Fetch a byte of data memory.
491 unsigned char cc_peekdatabyte(unsigned int adr){
492   unsigned char
493     hb=(adr&0xFF00)>>8,
494     lb=adr&0xFF,
495     toret;
496
497   //MOV DPTR, adr
498   cc_debug(3, 0x90, hb, lb);
499   //MOVX A, @DPTR
500   //Must be 2, perhaps for clocking?
501   toret=cc_debug(3, 0xE0, 0, 0);
502   return toret;
503   
504     /*
505 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
506 for (n = 0; n < count; n++) {
507     DEBUG_INSTR(IN: 0xE0, OUT: outputArray[n]);
508     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
509 }
510   */
511 }