maxusb is now default.
[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 //! Handles a chipcon command.
19 void cc_handle_fn( uint8_t const app,
20                                    uint8_t const verb,
21                                    uint32_t const len);
22
23 // define the jtag app's app_t
24 app_t const chipcon_app = {
25
26         /* app number */
27         CHIPCON,
28
29         /* handle fn */
30         cc_handle_fn,
31
32         /* name */
33         "CHIPCON",
34
35         /* desc */
36         "\tThe CHIPCON app adds support for debugging the chipcon\n"
37         "\t8051 processor.\n"
38 };
39
40 /* Concerning clock rates, the maximimum clock rates are defined on
41    page 4 of the spec.  They vary, but are roughly 30MHz.  Raising
42    this clock rate might allow for clock glitching, but the GoodFET
43    isn't sufficient fast for that.  Perhaps a 200MHz ARM or an FPGA in
44    the BadassFET?
45 */
46
47 //Pins and I/O
48 //MISO and MOSI are the same pin, direction changes.
49
50 #if (platform == tilaunchpad)
51 /*
52  * The Launchpad has only pins easily available
53  * P5.3 TCK     SCK             (labeled TEST J3-10 J2-17)      DC closest to antenna           (blue)
54  * P5.2 IO      MISO MOSI       (labeled RST  J3-8  J2-16)      DD next to closer to USB                (yellow)
55  * P3.6 txd1    RST             (labeled RXD  J3-6  J1-4)       next to GND, which is closest to USB    (orange)
56  * P3.7 rxd1    RST             (labeled TXD  J3-4  J1-3)       connect to led1 J1-2
57  *
58  * for a permanent marriage between a TI-Launchpad, move RST to pin48 P5.4
59  * (requeries soldering) and use rxd/txd for direct communication with IM-ME dongle.
60  */
61
62 #define RST  BIT6       // P3.7
63 #include <msp430_serial.h>
64 #else   // tilaunchpad
65 #define RST  BIT0
66 #define dputs(s)
67 #endif  // ! tilaunchad
68
69 #define MOSI BIT2
70 #define MISO BIT2
71 #define SCK  BIT3
72
73
74 //This could be more accurate.
75 //Does it ever need to be?
76 #define CCSPEED 3
77 //#define CCSPEED 3
78 //#define CCDELAY(x) delay(x)
79 #define CCDELAY(x)
80
81 #define SETMOSI SPIOUT|=MOSI
82 #define CLRMOSI SPIOUT&=~MOSI
83 #define SETCLK SPIOUT|=SCK
84 #define CLRCLK SPIOUT&=~SCK
85 #define READMISO (SPIIN&MISO?1:0)
86
87 #if (platform == tilaunchpad)
88 #  if (SPIDIR != P5DIR)
89 #    error "SPIDIR != P5DIR"
90 #  endif
91 #  if (SPIOUT != P5OUT)
92 #    error "SPIOUT != P5OUT"
93 #  endif
94 #  define SETRST  P3OUT|=RST
95 #  define CLRRST  P3OUT&=~RST
96 #else
97 #  define SETRST  P3OUT|=RST
98 #  define CLRRST  P3OUT&=~RST
99 #endif
100
101 #define CCWRITE SPIDIR|=MOSI
102 #define CCREAD SPIDIR&=~MISO
103
104 //! Set up the pins for CC mode.  Does not init debugger.
105 void ccsetup(){
106 #if (platform == tilaunchpad)
107         dputs("ccsetup");
108   SPIOUT|=MOSI+SCK;
109   SPIDIR|=MOSI+SCK;
110   P3OUT|=RST;
111   P3DIR|=RST;
112         dputs("done ccsetup");
113 #else
114   SPIOUT|=MOSI+SCK+RST;
115   SPIDIR|=MOSI+SCK+RST;
116 #endif
117   //P5REN=0xFF;
118 }
119
120
121 /* 33 cycle critical region
122 0000000e <ccdebuginit>:
123    e:   f2 d0 0d 00     bis.b   #13,    &0x0031 ;5 cycles
124   12:   31 00
125   14:   f2 c2 31 00     bic.b   #8,     &0x0031 ;4 cycles
126   18:   d2 c3 31 00     bic.b   #1,     &0x0031 ;4
127   1c:   f2 e2 31 00     xor.b   #8,     &0x0031 ;4
128   20:   f2 e2 31 00     xor.b   #8,     &0x0031 ;4
129   24:   f2 e2 31 00     xor.b   #8,     &0x0031 ;4
130   28:   f2 e2 31 00     xor.b   #8,     &0x0031 ;4
131   2c:   d2 d3 31 00     bis.b   #1,     &0x0031 ;4
132   30:   30 41           ret
133 */
134
135
136 //! Initialize the debugger
137 void ccdebuginit(){
138   //Port output BUT NOT DIRECTION is set at start.
139 #if (platform == tilaunchpad)
140   dputs("ccdebuginit");
141   SPIOUT|=MOSI+SCK;
142   P3OUT|=RST;
143 #else
144   SPIOUT|=MOSI+SCK+RST;
145 #endif
146
147   delay(30); //So the beginning is ready for glitching.
148
149   //Two positive debug clock pulses while !RST is low.
150   //Take RST low, pulse twice, then high.
151   SPIOUT&=~SCK;
152   delay(10);
153   CLRRST;
154
155   delay(10);
156
157   //Two rising edges.
158   SPIOUT^=SCK; //up
159   delay(1);
160   SPIOUT^=SCK; //down
161   delay(1);
162   SPIOUT^=SCK; //up
163   delay(1);
164   SPIOUT^=SCK; //Unnecessary.
165   delay(1);
166   //delay(0);
167
168   //Raise !RST.
169   SETRST;
170 }
171
172 //! Read and write a CC bit.
173 unsigned char cctrans8(unsigned char byte){
174   unsigned int bit;
175   //This function came from the SPI Wikipedia article.
176   //Minor alterations.
177
178   for (bit = 0; bit < 8; bit++) {
179     /* write MOSI on trailing edge of previous clock */
180     if (byte & 0x80)
181       SETMOSI;
182     else
183       CLRMOSI;
184     byte <<= 1;
185
186     /* half a clock cycle before leading/rising edge */
187     CCDELAY(CCSPEED>>2);
188     SETCLK;
189
190     /* half a clock cycle before trailing/falling edge */
191     CCDELAY(CCSPEED>>2);
192
193     /* read MISO on trailing edge */
194     byte |= READMISO;
195     CLRCLK;
196   }
197
198   return byte;
199 }
200
201 //! Send a command from txbytes.
202 void cccmd(unsigned char len){
203   unsigned char i;
204   CCWRITE;
205   for(i=0;i<len;i++)
206     cctrans8(cmddata[i]);
207 }
208
209 //! Fetch a reply, usually 1 byte.
210 void ccread(unsigned char len){
211   unsigned char i;
212   CCREAD;
213   for(i=0;i<len;i++)
214     cmddata[i]=cctrans8(0);
215 }
216
217 //! Handles a chipcon command.
218 void cc_handle_fn( uint8_t const app,
219                                    uint8_t const verb,
220                                    uint32_t const len)
221 {
222   //Always init.  Might help with buggy lines.
223   //Might hurt too.
224   //ccdebuginit();
225   long i;
226   int blocklen, blockadr;
227
228   switch(verb){
229     //CC_PEEK and CC_POKE will come later.
230   case PEEK:
231     cmddata[0]=cc_peekirambyte(cmddata[0]);
232     txdata(app,verb,1);
233     break;
234   case POKE:
235     cmddata[0]=cc_pokeirambyte(cmddata[0],cmddata[1]);
236     txdata(app,verb,0);
237     break;
238   case READ:  //Write a command and return 1-byte reply.
239     cccmd(len);
240     if(cmddata[0]&0x4)
241       ccread(1);
242     txdata(app,verb,1);
243     break;
244   case WRITE: //Write a command with no reply.
245     cccmd(len);
246     txdata(app,verb,0);
247     break;
248   case START://enter debugger
249     ccdebuginit();
250     txdata(app,verb,0);
251     break;
252   case STOP://exit debugger
253     //Take RST low, then high.
254     CLRRST;
255     CCDELAY(CCSPEED);
256     SETRST;
257     txdata(app,verb,0);
258     break;
259   case SETUP:
260     ccsetup();
261     txdata(app,verb,0);
262     break;
263
264   //Micro commands!
265   case CC_CHIP_ERASE:
266   case CC_MASS_ERASE_FLASH:
267     cc_chip_erase();
268     txdata(app,verb,1);
269     break;
270   case CC_WR_CONFIG:
271     cc_wr_config(cmddata[0]);
272     txdata(app,verb,1);
273     break;
274   case CC_RD_CONFIG:
275     cc_rd_config();
276     txdata(app,verb,1);
277     break;
278   case CC_GET_PC:
279     cc_get_pc();
280     txdata(app,verb,2);
281     break;
282   case CC_LOCKCHIP:
283     cc_lockchip();
284     //no break, return status
285   case CC_READ_STATUS:
286     cc_read_status();
287     txdata(app,verb,1);
288     break;
289   case CC_SET_HW_BRKPNT:
290     cc_set_hw_brkpnt(cmddataword[0]);
291     txdata(app,verb,1);
292     break;
293   case CC_HALT:
294     cc_halt();
295     txdata(app,verb,1);
296     break;
297   case CC_RESUME:
298     cc_resume();
299     txdata(app,verb,1);
300     break;
301   case CC_DEBUG_INSTR:
302     cc_debug_instr(len);
303     txdata(app,verb,1);
304     break;
305   case CC_STEP_INSTR:
306     cc_step_instr();
307     txdata(app,verb,1);
308     break;
309   case CC_STEP_REPLACE:
310     txdata(app,NOK,0);//Don't add this; it's non-standard.
311     break;
312   case CC_GET_CHIP_ID:
313     cmddataword[0]=cc_get_chip_id();
314     txdata(app,verb,2);
315     break;
316
317
318   //Macro commands
319   case CC_READ_CODE_MEMORY:
320     cmddata[0]=cc_peekcodebyte(cmddataword[0]);
321     txdata(app,verb,1);
322     break;
323   case CC_READ_XDATA_MEMORY:
324     //Read the length.
325     blocklen=1;
326     if(len>2)
327       blocklen=cmddataword[1];
328     blockadr=cmddataword[0];
329
330     //Return that many bytes.
331     for(i=0;i<blocklen;i++)
332       cmddata[i]=cc_peekdatabyte(blockadr+i);
333     txdata(app,verb,blocklen);
334     break;
335
336   case CC_WRITE_XDATA_MEMORY:
337     cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
338     txdata(app,verb,1);
339     break;
340   case CC_SET_PC:
341     cc_set_pc(cmddatalong[0]);
342     txdata(app,verb,0);
343     break;
344   case CC_WRITE_FLASH_PAGE:
345     cc_write_flash_page(cmddatalong[0]);
346     txdata(app,verb,0);
347     break;
348   case CC_WIPEFLASHBUFFER:
349     for(i=0xf000;i<0xf800;i++)
350       cc_pokedatabyte(i,0xFF);
351     txdata(app,verb,0);
352     break;
353
354   case CC_CLOCK_INIT:
355   case CC_PROGRAM_FLASH:
356   default:
357     debugstr("This Chipcon command is not yet implemented.");
358     txdata(app,NOK,0);//TODO implement me.
359     break;
360   }
361 }
362
363 //! Set the Chipcon's Program Counter
364 void cc_set_pc(u32 adr){
365   cmddata[0]=0x02;             //SetPC
366   cmddata[1]=((adr>>8)&0xff);  //HIBYTE
367   cmddata[2]=adr&0xff;         //LOBYTE
368   cc_debug_instr(3);
369   return;
370 }
371
372 //! Erase all of a Chipcon's memory.
373 void cc_chip_erase(){
374   cmddata[0]=CCCMD_CHIP_ERASE; //0x14
375   cccmd(1);
376   ccread(1);
377 }
378 //! Write the configuration byte.
379 void cc_wr_config(unsigned char config){
380   cmddata[0]=CCCMD_WR_CONFIG; //0x1D
381   cmddata[1]=config;
382   cccmd(2);
383   ccread(1);
384 }
385
386 //! Locks the chip.
387 void cc_lockchip(){
388   register int i;
389
390   //debugstr("Locking chip.");
391   cc_wr_config(1);//Select Info Flash
392   if(!(cc_rd_config()&1))
393     debugstr("Config forgotten!");
394
395   //Clear config page.
396   for(i=0;i<2048;i++)
397     cc_pokedatabyte(0xf000+i,0);
398   cc_write_flash_page(0);
399   if(cc_peekcodebyte(0))
400     debugstr("Failed to clear info flash byte.");
401
402   cc_wr_config(0);
403   if(cc_rd_config()&1)
404     debugstr("Stuck in info flash mode!");
405 }
406
407 //! Read the configuration byte.
408 unsigned char cc_rd_config(){
409   cmddata[0]=CCCMD_RD_CONFIG; //0x24
410   cccmd(1);
411   ccread(1);
412   return cmddata[0];
413 }
414
415
416 //! Read the status register
417 unsigned char cc_read_status(){
418   cmddata[0]=CCCMD_READ_STATUS; //0x3f
419   cccmd(1);
420   ccread(1);
421   return cmddata[0];
422 }
423
424 //! Read the CHIP ID bytes.
425 unsigned short cc_get_chip_id(){
426   cmddata[0]=CCCMD_GET_CHIP_ID; //0x68
427   cccmd(1);
428   ccread(2);
429
430
431   //Find the flash word size.
432   switch(cmddata[0]){
433   case 0x01://CC1110
434   case 0x11://CC1111
435   case 0x81://CC2510
436   case 0x91://CC2511
437     //debugstr("2 bytes/flash word");
438     flash_word_size=0x02;
439     break;
440   default:
441     //debugstr("Warning: Guessing flash word size.");
442     //flash_word_size=0;
443     break;
444   case 0x85://CC2430
445   case 0x89://CC2431
446     //debugstr("4 bytes/flash word");
447     flash_word_size=0x04;
448     break;
449   }
450
451   //Return the word.
452   return cmddataword[0];
453 }
454
455 //! Populates flash buffer in xdata.
456 void cc_write_flash_buffer(u8 *data, u16 len){
457   cc_write_xdata(0xf000, data, len);
458 }
459 //! Populates flash buffer in xdata.
460 void cc_write_xdata(u16 adr, u8 *data, u16 len){
461   u16 i;
462   for(i=0; i<len; i++){
463     cc_pokedatabyte(adr+i,
464                     data[i]);
465   }
466 }
467
468
469 //32-bit words, 2KB pages
470 //0x20 0x00 for CC2430, CC1110
471 #define HIBYTE_WORDS_PER_FLASH_PAGE 0x02
472 #define LOBYTE_WORDS_PER_FLASH_PAGE 0x00
473
474 /** Ugh, this varies by chip.
475     0x800 for CC2430
476     0x400 for CC1110
477 */
478 //#define FLASHPAGE_SIZE 0x400
479 #define MAXFLASHPAGE_SIZE 0x800
480 #define MINFLASHPAGE_SIZE 0x400
481
482
483 //32 bit words on CC2430
484 //16 bit words on CC1110
485 //#define FLASH_WORD_SIZE 0x2
486 u8 flash_word_size = 0; //0x02;
487
488
489 /* Flash Write Timing
490    MHZ | FWT (0xAB)
491    12  | 0x10
492    13  | 0x11
493    16  | 0x15
494    24  | 0x20
495    26  | 0x23  (IM ME)
496    32  | 0x2A  (Modula.si)
497 */
498 //#define FWT 0x23
499
500 const u8 flash_routine[] = {
501   //0:
502   //MOV FADDRH, #imm;
503   0x75, 0xAD,
504   0x00,//#imm=((address >> 8) / FLASH_WORD_SIZE) & 0x7E,
505
506   //0x75, 0xAB, 0x23, //Set FWT per clock
507   0x75, 0xAC, 0x00,                                          //                 MOV FADDRL, #00;
508   /* Erase page. */
509   0x75, 0xAE, 0x01,                                          //                 MOV FLC, #01H; // ERASE
510                                                              //                 ; Wait for flash erase to complete
511   0xE5, 0xAE,                                                // eraseWaitLoop:  MOV A, FLC;
512   0x20, 0xE7, 0xFB,                                          //                 JB ACC_BUSY, eraseWaitLoop;
513
514   /* End erase page. */
515                                                              //                 ; Initialize the data pointer
516   0x90, 0xF0, 0x00,                                          //                 MOV DPTR, #0F000H;
517                                                              //                 ; Outer loops
518   0x7F, HIBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R7, #imm;
519   0x7E, LOBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R6, #imm;
520   0x75, 0xAE, 0x02,                                          //                 MOV FLC, #02H; // WRITE
521                                                              //                     ; Inner loops
522   //24:
523   0x7D, 0xde /*FLASH_WORD_SIZE*/,                                     // writeLoop:          MOV R5, #imm;
524   0xE0,                                                      // writeWordLoop:          MOVX A, @DPTR;
525   0xA3,                                                      //                         INC DPTR;
526   0xF5, 0xAF,                                                //                         MOV FWDATA, A;
527   0xDD, 0xFA,                                                //                     DJNZ R5, writeWordLoop;
528                                                              //                     ; Wait for completion
529   0xE5, 0xAE,                                                // writeWaitLoop:      MOV A, FLC;
530   0x20, 0xE6, 0xFB,                                          //                     JB ACC_SWBSY, writeWaitLoop;
531   0xDE, 0xF1,                                                //                 DJNZ R6, writeLoop;
532   0xDF, 0xEF,                                                //                 DJNZ R7, writeLoop;
533                                                              //                 ; Done, fake a breakpoint
534   0xA5                                                       //                 DB 0xA5;
535 };
536
537
538 //! Copies flash buffer to flash.
539 void cc_write_flash_page(u32 adr){
540   //Assumes that page has already been written to XDATA 0xF000
541   //debugstr("Flashing 2kb at 0xF000 to given adr.");
542
543   if(adr&(MINFLASHPAGE_SIZE-1)){
544     debugstr("Flash page address is not on a page boundary.  Aborting.");
545     return;
546   }
547
548   if(flash_word_size!=2 && flash_word_size!=4){
549     debugstr("Flash word size is wrong, aborting write to");
550     debughex(adr);
551     while(1);
552   }
553
554   //Routine comes next
555   //WRITE_XDATA_MEMORY(IN: 0xF000 + FLASH_PAGE_SIZE, sizeof(routine), routine);
556   cc_write_xdata(0xF000+MAXFLASHPAGE_SIZE,
557                  (u8*) flash_routine, sizeof(flash_routine));
558   //Patch routine's third byte with
559   //((address >> 8) / FLASH_WORD_SIZE) & 0x7E
560   cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+2,
561                   ((adr>>8)/flash_word_size)&0x7E);
562   //Patch routine to define FLASH_WORD_SIZE
563   if(flash_routine[25]!=0xde)
564     debugstr("Ugly patching code failing in chipcon.c");
565   cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+25,
566                   flash_word_size);
567
568   //debugstr("Wrote flash routine.");
569
570   //MOV MEMCTR, (bank * 16) + 1;
571   cmddata[0]=0x75;
572   cmddata[1]=0xc7;
573   cmddata[2]=0x51;
574   cc_debug_instr(3);
575   //debugstr("Loaded bank info.");
576
577   cc_set_pc(0xf000+MAXFLASHPAGE_SIZE);//execute code fragment
578   cc_resume();
579
580   //debugstr("Executing.");
581
582
583   while(!(cc_read_status()&CC_STATUS_CPUHALTED)){
584     led_toggle();//blink LED while flashing
585   }
586
587
588   //debugstr("Done flashing.");
589
590   led_off();
591 }
592
593 //! Read the PC
594 unsigned short cc_get_pc(){
595   cmddata[0]=CCCMD_GET_PC; //0x28
596   cccmd(1);
597   ccread(2);
598
599   //Return the word.
600   return cmddataword[0];
601 }
602
603 //! Set a hardware breakpoint.
604 void cc_set_hw_brkpnt(unsigned short adr){
605   debugstr("FIXME: This certainly won't work.");
606   cmddataword[0]=adr;
607   cccmd(2);
608   ccread(1);
609   return;
610 }
611
612
613 //! Halt the CPU.
614 void cc_halt(){
615   cmddata[0]=CCCMD_HALT; //0x44
616   cccmd(1);
617   ccread(1);
618   return;
619 }
620 //! Resume the CPU.
621 void cc_resume(){
622   cmddata[0]=CCCMD_RESUME; //0x4C
623   cccmd(1);
624   ccread(1);
625   return;
626 }
627
628
629 //! Step an instruction
630 void cc_step_instr(){
631   cmddata[0]=CCCMD_STEP_INSTR; //0x5C
632   cccmd(1);
633   ccread(1);
634   return;
635 }
636
637 //! Debug an instruction.
638 void cc_debug_instr(unsigned char len){
639   //Bottom two bits of command indicate length.
640   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3); //0x54+len
641   CCWRITE;
642   cctrans8(cmd);  //Second command code
643   cccmd(len&0x3); //Command itself.
644   ccread(1);
645   return;
646 }
647
648 //! Debug an instruction, for local use.
649 unsigned char cc_debug(unsigned char len,
650               unsigned char a,
651               unsigned char b,
652               unsigned char c){
653   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3);//0x54+len
654   CCWRITE;
655   cctrans8(cmd);
656   if(len>0)
657     cctrans8(a);
658   if(len>1)
659     cctrans8(b);
660   if(len>2)
661     cctrans8(c);
662   CCREAD;
663   return cctrans8(0x00);
664 }
665
666 //! Fetch a byte of code memory.
667 unsigned char cc_peekcodebyte(unsigned long adr){
668   /** See page 9 of SWRA124 */
669   unsigned char bank=adr>>15,
670     lb=adr&0xFF,
671     hb=(adr>>8)&0x7F,
672     toret=0;
673   adr&=0x7FFF;
674
675   //MOV MEMCTR, (bank*16)+1
676   cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
677   //MOV DPTR, address
678   cc_debug(3, 0x90, hb, lb);
679
680   //for each byte
681   //CLR A
682   cc_debug(2, 0xE4, 0, 0);
683   //MOVC A, @A+DPTR;
684   toret=cc_debug(3, 0x93, 0, 0);
685   //INC DPTR
686   //cc_debug(1, 0xA3, 0, 0);
687
688   return toret;
689 }
690
691
692 //! Set a byte of data memory.
693 unsigned char cc_pokedatabyte(unsigned int adr,
694                            unsigned char val){
695   unsigned char
696     hb=(adr&0xFF00)>>8,
697     lb=adr&0xFF;
698
699   //MOV DPTR, adr
700   cc_debug(3, 0x90, hb, lb);
701   //MOV A, val
702   cc_debug(2, 0x74, val, 0);
703   //MOVX @DPTR, A
704   cc_debug(1, 0xF0, 0, 0);
705
706   return 0;
707   /*
708 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
709 for (n = 0; n < count; n++) {
710     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
711     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
712     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
713 }
714    */
715 }
716
717 //! Fetch a byte of data memory.
718 unsigned char cc_peekdatabyte(unsigned int adr){
719   unsigned char
720     hb=(adr&0xFF00)>>8,
721     lb=adr&0xFF;
722
723   //MOV DPTR, adr
724   cc_debug(3, 0x90, hb, lb);
725   //MOVX A, @DPTR
726   //Must be 2, perhaps for clocking?
727   return cc_debug(3, 0xE0, 0, 0);
728 }
729
730
731 //! Fetch a byte of IRAM.
732 u8 cc_peekirambyte(u8 adr){
733   //CLR A
734   cc_debug(2, 0xE4, 0, 0);
735   //MOV A, #iram
736   return cc_debug(3, 0xE5, adr, 0);
737 }
738
739 //! Write a byte of IRAM.
740 u8 cc_pokeirambyte(u8 adr, u8 val){
741   //CLR A
742   cc_debug(2, 0xE4, 0, 0);
743   //MOV #iram, #val
744   return cc_debug(3, 0x75, adr, val);
745   //return cc_debug(3, 0x75, val, adr);
746 }
747
748