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