7b55db8bfdc86bcbfeeb9ad77570469e88cc9d63
[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 0
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 char len){
128   switch(verb){
129     //CC_PEEK and CC_POKE will come later.
130   case READ:  //Write a command and return 1-byte reply.
131     cccmd(len);
132     ccread(1);
133     txdata(app,verb,1);
134     break;
135   case WRITE: //Write a command with no reply.
136     cccmd(len);
137     txdata(app,verb,0);
138     break;
139   case START://enter debugger
140     ccdebuginit();
141     txdata(app,verb,0);
142     break;
143   case STOP://exit debugger
144     //Take RST low, then high.
145     P5OUT&=~RST;
146     CCDELAY(CCSPEED);
147     P5OUT|=RST;
148     txdata(app,verb,0);
149     break;
150   case SETUP:
151     ccsetup();
152     txdata(app,verb,0);
153     break;
154     
155   //Micro commands!
156   case CC_CHIP_ERASE:
157     cc_chip_erase();
158     txdata(app,verb,1);
159     break;
160   case CC_WR_CONFIG:
161     cc_wr_config(cmddata[0]);
162     txdata(app,verb,1);
163     break;
164   case CC_RD_CONFIG:
165     cc_rd_config();
166     txdata(app,verb,1);
167     break;
168   case CC_GET_PC:
169     cc_get_pc();
170     txdata(app,verb,2);
171     break;
172   case CC_READ_STATUS:
173     cc_read_status();
174     txdata(app,verb,1);
175     break;
176   case CC_SET_HW_BRKPNT:
177     cc_set_hw_brkpnt(cmddataword[0]);
178     txdata(app,verb,1);
179     break;
180   case CC_HALT:
181     cc_halt();
182     txdata(app,verb,1);
183     break;
184   case CC_RESUME:
185     cc_resume();
186     txdata(app,verb,1);
187     break;
188   case CC_DEBUG_INSTR:
189     cc_debug_instr(len);
190     txdata(app,verb,1);
191     break;
192   case CC_STEP_INSTR:
193     cc_step_instr();
194     txdata(app,verb,1);
195     break;
196   case CC_STEP_REPLACE:
197     txdata(app,NOK,0);//TODO add me
198     break;
199   case CC_GET_CHIP_ID:
200     cc_get_chip_id();
201     txdata(app,verb,2);
202     break;
203
204
205   //Macro commands
206   case CC_READ_CODE_MEMORY:
207     cmddata[0]=cc_peekcodebyte(cmddataword[0]);
208     txdata(app,verb,1);
209     break;
210   case CC_READ_XDATA_MEMORY:
211     cmddata[0]=cc_peekdatabyte(cmddataword[0]);
212     txdata(app,verb,1);
213     break;
214   case CC_WRITE_XDATA_MEMORY:
215     cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
216     txdata(app,verb,1);
217     break;
218   case CC_SET_PC:
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(3, 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 }