Chipcon refactoring, debugging.
[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   case CC_CLOCK_INIT:
223   case CC_WRITE_FLASH_PAGE:
224   case CC_MASS_ERASE_FLASH:
225   case CC_PROGRAM_FLASH:
226     txdata(app,NOK,0);//TODO implement me.
227     break;
228   }
229 }
230
231 //! Erase all of a Chipcon's memory.
232 void cc_chip_erase(){
233   cmddata[0]=CCCMD_CHIP_ERASE; //0x14
234   cccmd(1);
235   ccread(1);
236 }
237 //! Write the configuration byte.
238 void cc_wr_config(unsigned char config){
239   cmddata[0]=CCCMD_WR_CONFIG; //0x1D
240   cmddata[1]=config;
241   cccmd(2);
242   ccread(1);
243 }
244 //! Read the configuration byte.
245 unsigned char cc_rd_config(){
246   cmddata[0]=CCCMD_RD_CONFIG; //0x24
247   cccmd(1);
248   ccread(1);
249   return cmddata[0];
250 }
251
252
253 //! Read the status register
254 unsigned char cc_read_status(){
255   cmddata[0]=CCCMD_READ_STATUS; //0x3f
256   cccmd(1);
257   ccread(1);
258   return cmddata[0];
259 }
260
261 //! Read the CHIP ID bytes.
262 unsigned short cc_get_chip_id(){
263   unsigned short toret;
264   cmddata[0]=CCCMD_GET_CHIP_ID; //0x68
265   cccmd(1);
266   ccread(2);
267   
268   //Return the word.
269   toret=cmddata[1];
270   toret=(toret<<8)+cmddata[1];
271   return toret;
272 }
273
274
275 //! Read the PC
276 unsigned short cc_get_pc(){
277   cmddata[0]=CCCMD_GET_PC; //0x28
278   cccmd(1);
279   ccread(2);
280   
281   //Return the word.
282   return cmddataword[0];
283 }
284
285 //! Set a hardware breakpoint.
286 void cc_set_hw_brkpnt(unsigned short adr){
287   debugstr("FIXME: This certainly won't work.");
288   cmddataword[0]=adr;
289   cccmd(2);
290   ccread(1);
291   return;
292 }
293
294
295 //! Halt the CPU.
296 void cc_halt(){
297   cmddata[0]=CCCMD_HALT; //0x44
298   cccmd(1);
299   ccread(1);
300   return;
301 }
302 //! Resume the CPU.
303 void cc_resume(){
304   cmddata[0]=CCCMD_RESUME; //0x4C
305   cccmd(1);
306   ccread(1);
307   return;
308 }
309
310
311 //! Step an instruction
312 void cc_step_instr(){
313   cmddata[0]=CCCMD_STEP_INSTR; //0x5C
314   cccmd(1);
315   ccread(1);
316   return;
317 }
318
319 //! Debug an instruction.
320 void cc_debug_instr(unsigned char len){
321   //Bottom two bits of command indicate length.
322   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3); //0x54+len
323   CCWRITE;
324   cctrans8(cmd);  //Second command code
325   cccmd(len&0x3); //Command itself.
326   ccread(1);
327   return;
328 }
329
330 //! Debug an instruction, for local use.
331 unsigned char cc_debug(unsigned char len,
332               unsigned char a,
333               unsigned char b,
334               unsigned char c){
335   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3);//0x54+len
336   CCWRITE;
337   cctrans8(cmd);
338   if(len--)
339     cctrans8(a);
340   if(len--)
341     cctrans8(b);
342   if(len--)
343     cctrans8(c);
344   CCREAD;
345   return cctrans8(0x00);
346 }
347
348 //! Fetch a byte of code memory.
349 unsigned char cc_peekcodebyte(unsigned long adr){
350   /** See page 9 of SWRA124 */
351   unsigned char bank=adr>>15,
352     lb=adr&0xFF,
353     hb=(adr>>8)&0x7F,
354     toret=0;
355   adr&=0x7FFF;
356   
357   //MOV MEMCTR, (bank*16)+1
358   cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
359   //MOV DPTR, address
360   cc_debug(3, 0x90, hb, lb);
361   
362   //for each byte
363   //CLR A
364   cc_debug(2, 0xE4, 0, 0);
365   //MOVC A, @A+DPTR;
366   toret=cc_debug(3, 0x93, 0, 0);
367   //INC DPTR
368   //cc_debug(1, 0xA3, 0, 0);
369   
370   return toret;
371 }
372
373
374 //! Set a byte of data memory.
375 unsigned char cc_pokedatabyte(unsigned int adr,
376                            unsigned char val){
377   unsigned char
378     hb=(adr&0xFF00)>>8,
379     lb=adr&0xFF;
380   
381   //MOV DPTR, adr
382   cc_debug(3, 0x90, hb, lb);
383   //MOV A, val
384   cc_debug(2, 0x74, val, 0);
385   //MOVX @DPTR, A
386   cc_debug(1, 0xF0, 0, 0);
387   
388   return 0;
389   /*
390 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
391 for (n = 0; n < count; n++) {
392     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
393     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
394     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
395 }
396    */
397 }
398
399 //! Fetch a byte of data memory.
400 unsigned char cc_peekdatabyte(unsigned int adr){
401   unsigned char
402     hb=(adr&0xFF00)>>8,
403     lb=adr&0xFF,
404     toret;
405
406   //MOV DPTR, adr
407   cc_debug(3, 0x90, hb, lb);
408   //MOVX A, @DPTR
409   //Must be 2, perhaps for clocking?
410   toret=cc_debug(3, 0xE0, 0, 0);
411   return toret;
412   
413     /*
414 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
415 for (n = 0; n < count; n++) {
416     DEBUG_INSTR(IN: 0xE0, OUT: outputArray[n]);
417     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
418 }
419   */
420 }