mips32.c

Go to the documentation of this file.
00001 //
00002 // MIPS32.C
00003 //
00004 // Target CPU specific interface code for MIPS32 core
00005 //
00006 // Copyright (c) 2002, Jason Riffel - TotalEmbedded LLC.
00007 // All rights reserved.
00008 //
00009 // Redistribution and use in source and binary forms, with or without 
00010 // modification, are permitted provided that the following conditions 
00011 // are met:
00012 //
00013 // Redistributions of source code must retain the above copyright 
00014 // notice, this list of conditions and the following disclaimer. 
00015 //
00016 // Redistributions in binary form must reproduce the above copyright
00017 // notice, this list of conditions and the following disclaimer in 
00018 // the documentation and/or other materials provided with the
00019 // distribution. 
00020 //
00021 // Neither the name of TotalEmbedded nor the names of its 
00022 // contributors may be used to endorse or promote products derived 
00023 // from this software without specific prior written permission.
00024 // 
00025 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00026 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00027 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
00028 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00029 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
00030 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
00031 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00032 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00033 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00034 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
00035 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00036 // POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 
00039 //
00040 // INCLUDES
00041 //
00042 #include <errno.h>
00043 #include <signal.h>
00044 #include <stdio.h>
00045 #include <unistd.h>
00046 #include <sys/time.h>
00047 #include <sys/types.h>
00048 #include <sys/socket.h>
00049 #include <sys/wait.h>
00050 #include <netinet/in.h>
00051 #include <netdb.h>
00052 //#include <signal.h>
00053 #include "mips32.h"
00054 #include "jtag.h"
00055 #include <sys/types.h>
00056 #include <sys/stat.h>
00057 #include <fcntl.h>
00058 #include "ejtag.h"
00059 #include <time.h>
00060 #include <string.h>
00061 #include "singlestepclear.h"
00062 #include <sys/time.h>
00063 #include <signal.h>
00064 #include <cmdparser.h>
00065 extern int log_disasm;
00066 
00067 void * md_disasm ( char *dest, void *addr, unsigned int inst);
00068 //#include <curses.h>
00069 
00070 //
00071 // PROTOTYPES
00072 //
00073 #ifndef EXEC_DEBUG_Z
00074 #define EXEC_DEBUG_Z
00075 #endif
00076 
00077 //#undef EXEC_DEBUG_Z
00078 static void         fMIPS32_HandleWrite  (unsigned int ui_address, unsigned int ui_data,int accesstype);
00079 static unsigned int fMIPS32_HandleRead   (unsigned int ui_address);
00080 
00081 //
00082 // GLOBALS
00083 //
00084 unsigned int  ui_ejtag_state;
00085 unsigned int  ui_last_signal;
00086 unsigned int  ui_datamem[0x80];
00087 unsigned int  *ui_fifomem;
00088 unsigned int  ui_fifomem_size=0x2000;
00089 unsigned int  uififo_raddr,uififo_waddr;
00090 #define CP0_DEBUG_SST_EN (1<<8)             /* single step enable */
00091 
00092 unsigned int  context_reg[dMIPS32_NUM_DEBUG_REGISTERS]={0};
00093 //
00094 // MIPS32_Init
00095 //
00096 // Performs intialization specific to the target CPU MIPS32
00097 //
00098 int Ejtag_boot_flag=0;
00099 static int  fMIPS32_inited=0;
00100 int enable_timer;
00101 unsigned int timer_val=1000;
00102 int ejtag_in_debug_mode = 0;
00103 
00104 int check_ejtag(char *buf,int *debug_reg)
00105 {
00106         unsigned int ui_result,debug_reg1;
00107         char *p = buf;
00108         *p=0;
00109 
00110         ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
00111                 dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
00112 
00113         fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00114         ui_result = fJTAG_Data(ui_result);
00115 
00116         // Check for debug mode
00117         if (ui_result & dMIPS32_ECR_IN_DEBUG_MODE)
00118         {
00119 
00120                 // Read out the debug register to determine the cause
00121                 static int codebuf[100] =tgt_compile(
00122                                 "1:\n" 
00123                                 "mtc0   ra, $31;\n" 
00124                                 "bal 2f;\n"
00125                                 "nop;\n"
00126                                 ".word 0;\n"  /*v0*/
00127                                 ".word 0;\n"  /*debug reg*/
00128                                 ".word 0;\n"  /*depc*/
00129                                 ".word 0;\n"  /*inst*/
00130                                 "2:sw   v0,  0(ra);\n" 
00131                                 "mfc0   v0,  $23;\n" 
00132                                 "sw     v0,  4(ra);\n" 
00133                                 "mfc0   v0, $24;\n" 
00134                                 "sw     v0,  8(ra);\n" 
00135                                 "mfc0   v0, $24;\n" 
00136                                 "lw             v0,(v0)\n"
00137                                 "sw     v0,  12(ra);\n" 
00138                                 "lw     v0,  0(ra);\n" 
00139                                 "mfc0   ra, $31 ;\n" 
00140                                 "b 1b;\n" 
00141                                 "nop;\n" 
00142                                 );
00143 
00144                 fMIPS32_ExecuteDebugModule(codebuf,0x8000,1);
00145                 debug_reg1 = codebuf[4];
00146 
00147                 p += sprintf(p,"debug:%08x #",debug_reg1);
00148 
00149                 if (debug_reg1 & dMIPS32_DEBUG_SW_BKPT)
00150                         p += sprintf(p,"i");
00151 
00152                 if (debug_reg1 & dMIPS32_DEBUG_HW_INST_BKPT)
00153                         p += sprintf(p,"I ");
00154 
00155                 if (debug_reg1 & dMIPS32_DEBUG_HW_DATA_LD_BKPT)
00156                         p += sprintf(p,"l");
00157 
00158                 if (debug_reg1 & dMIPS32_DEBUG_HW_DATA_ST_BKPT)
00159                         p += sprintf(p,"s");
00160 
00161                 if (debug_reg1 & dMIPS32_DEBUG_HW_DATA_LD_IMP_BKPT)
00162                         p += sprintf(p,"L");
00163 
00164                 if (debug_reg1 & dMIPS32_DEBUG_HW_DATA_ST_IMP_BKPT)
00165                         p += sprintf(p,"S");
00166 
00167                 if(debug_reg1 & (dMIPS32_DEBUG_HW_DATA_LD_BKPT|dMIPS32_DEBUG_HW_DATA_ST_BKPT|dMIPS32_DEBUG_HW_DATA_LD_IMP_BKPT|dMIPS32_DEBUG_HW_DATA_ST_IMP_BKPT))
00168                 {
00169                 /*clear data breakpoint*/
00170                         tgt_exec(
00171                                         "1:\n" 
00172                                         "mtc0    $15, $31;\n" 
00173                                         "lui    $15,0xFF30;\n" 
00174                                         "sw $0,0x2000($15);\n"
00175                                         "mfc0    $15, $31;\n" 
00176                                         "b 1b;\n" \
00177                                         "nop;\n" \
00178                                         ,0x80000);
00179                 }
00180 
00181                 if (debug_reg1 & dMIPS32_DEBUG_SINGLE_STEP_BKPT)
00182                         p += sprintf(p," step");
00183 
00184                 if (debug_reg1 & dMIPS32_DEBUG_JTAG_BKPT)
00185                         p += sprintf(p,"user ");
00186                 if (debug_reg1 & 0x100000)
00187                         p += sprintf(p," exp");
00188 
00189                 p += sprintf(p,"\n");
00190                 md_disasm (p, codebuf[5], codebuf[6]);
00191         *debug_reg = debug_reg1;
00192 
00193         }
00194         else 
00195         {
00196                 p += sprintf(p,"not in debug mode");
00197         *debug_reg = 0;
00198         }
00199 
00200         
00201         return ui_result;
00202 }
00203 
00204 unsigned int debug_reg = 0;
00205 static void timer_handler(int sig)
00206 {
00207         static char buf[256];
00208         static int ui_result;
00209         int debug_reg1;
00210 
00211         ejtag_in_debug_mode = !!(ui_result & dMIPS32_ECR_IN_DEBUG_MODE);
00212         if(enable_timer == 1)
00213         {
00214                 check_ejtag(buf,&debug_reg1);
00215 
00216         if(debug_reg1 != debug_reg )
00217                 printf("\n%s\n",buf);
00218         debug_reg = debug_reg1;
00219         }
00220 }
00221 
00222 static int set_timer(int ms);
00223 
00224 void fMIPS32_Init(void)
00225 {
00226 if(fMIPS32_inited)return;
00227 
00228   fJTAG_Init();
00229 
00230   fMIPS32_inited=1;
00231 
00232   signal(SIGALRM, timer_handler);
00233 
00234   if(enable_timer != -1)
00235     set_timer(timer_val);
00236 
00237   return;
00238 }
00239 
00240 static int set_timer(int ms)
00241 {
00242           struct itimerval value,oval;
00243           value.it_interval.tv_sec = ms/1000;
00244           value.it_interval.tv_usec = (ms%1000)*1000;
00245           value.it_value = value.it_interval;
00246           setitimer(ITIMER_REAL,&value,&oval);
00247 }
00248 
00249 static int cmd_timer(int argc,char **argv)
00250 {
00251         if(argc != 2)return -1;
00252         set_timer(strtoul(argv[1],0,0));
00253         return 0;
00254 }
00255 
00256 mycmd_init(timer,cmd_timer,"timer ms","set timer");
00257 
00258 //
00259 // fMIPS32_ExecuteDebugModule
00260 //
00261 // This will take an array of 32 bit values and feed them into the 
00262 // processor via the EJTAG port as instructions.
00263 //
00264   unsigned int ui_stack[32];
00265   unsigned int ui_stack_index=0;
00266 
00267 #define STATE_CONNECTING 0
00268 #define STATE_WAITING 1
00269 #define STATE_RUN 2
00270 //#define USE_FASTDATA
00271 
00272 unsigned int force_acc()
00273 {
00274         unsigned int ui_result;
00275         ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
00276               dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR |
00277               dMIPS32_ECR_EJTAG_BREAK;
00278         fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00279         ui_result = fJTAG_Data(ui_result);
00280         return ui_result;
00281 }
00282 
00283 unsigned int get_acc(unsigned int *op,int *address,int *data)
00284 {
00285         unsigned int ui_result;
00286         unsigned int ui_address;
00287         unsigned int ui_data;
00288         int ret;
00289         int connecting = 0;
00290 
00291         // Read the control register.  Make sure an access is requested, then 
00292         // do it.
00293         ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
00294                 dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
00295 
00296         fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00297         ui_result = fJTAG_Data(ui_result);
00298 
00299         *op = ui_result;
00300 
00301         if(!(ui_result&dMIPS32_ECR_IN_DEBUG_MODE))  {
00302                 return 0;
00303         }
00304 
00305 
00306         if (!(ui_result & dMIPS32_ECR_ACCESS_PENDING))
00307         {
00308                 return 1;
00309         }
00310 
00311         fJTAG_Instruction(dJTAG_REGISTER_ADDRESS);
00312         ui_address = fJTAG_Data(0x00)|0xff000000;
00313         fJTAG_Data(ui_address);
00314 
00315         *address = ui_address;
00316 
00317         fJTAG_Instruction(dJTAG_REGISTER_DATA);
00318         ui_data = fJTAG_Data(0x00);
00319         fJTAG_Data(ui_data);
00320 
00321         *data = ui_data;
00322         return 3;
00323 }
00324 
00325 unsigned int feed_acc(int data)
00326 {
00327         int ui_result,ui_data = data;
00328         fJTAG_Instruction(dJTAG_REGISTER_DATA);
00329         ui_data = fJTAG_Data(ui_data);
00330         // Clear the access pending bit (let the processor eat!)
00331         ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | 
00332                 dMIPS32_ECR_PROBE_VECTOR;
00333 
00334         fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00335         ui_result = fJTAG_Data(ui_result);
00336         return ui_result;
00337 }
00338 
00339 unsigned int wait_address(unsigned int address,int write,int waitacc)
00340 {
00341   unsigned int ui_result;
00342   unsigned int ui_address;
00343   unsigned int ui_data;
00344   int accesstype;
00345   while (1)
00346   {
00347     // Read the control register.  Make sure an access is requested, then 
00348     // do it.
00349     ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
00350                 dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
00351   
00352     fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00353     ui_result = fJTAG_Data(ui_result);
00354     accesstype=(ui_result>>29)&3;
00355         if(!(ui_result & dMIPS32_ECR_IN_DEBUG_MODE))
00356         {
00357       fprintf(stderr,"not in debug mode!\n");
00358     //            printf("not in debug mode!\n");
00359           return 0;
00360         }
00361     
00362     if (!(ui_result & dMIPS32_ECR_ACCESS_PENDING))
00363     {
00364           if(!waitacc)return -1;
00365           continue;
00366     }
00367 
00368         
00369     fJTAG_Instruction(dJTAG_REGISTER_ADDRESS);
00370     ui_address = fJTAG_Data(0x00)|0xff000000;
00371     fJTAG_Instruction(dJTAG_REGISTER_DATA);
00372     ui_data = fJTAG_Data(0x00);
00373 //      printf("ui_address =%08x\n",ui_address);
00374     if (ui_result & dMIPS32_ECR_ACCESS_RW) // Bit set for a WRITE
00375     {
00376         // Clear the access pending bit (let the processor eat!)
00377         ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | 
00378                     dMIPS32_ECR_PROBE_VECTOR;
00379         if (ui_address < 0xFF200200)
00380         fMIPS32_HandleWrite(ui_address, ui_data, accesstype);
00381         
00382                 if(address!=ui_address || !write || ui_address < 0xFF200200)
00383                 {
00384         fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00385         ui_result = fJTAG_Data(ui_result);
00386                 }
00387 
00388                 if(address==ui_address && write)break;
00389         }
00390         else
00391         {
00392         // Clear the access pending bit (let the processor eat!)
00393         ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | 
00394                     dMIPS32_ECR_PROBE_VECTOR;
00395 
00396           fJTAG_Instruction(dJTAG_REGISTER_DATA);
00397       if (ui_address >= 0xFF200200)
00398                 ui_data = 0;
00399           else
00400         ui_data = fMIPS32_HandleRead(ui_address);
00401         ui_data = fJTAG_Data(ui_data);
00402 
00403                 if(address!=ui_address || write || ui_address < 0xFF200200)
00404                 {
00405         fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00406         ui_result = fJTAG_Data(ui_result);
00407                 }
00408 
00409                 if(address==ui_address && !write)break;
00410         }
00411 
00412 }
00413 
00414     fJTAG_Instruction(dJTAG_REGISTER_ADDRESS);
00415     fJTAG_Data(ui_address);
00416 
00417         return 0;
00418   
00419 
00420 }
00421 
00422 unsigned int fMIPS32_ExecuteDebugModule_safe(unsigned int *pui_module,unsigned int len,unsigned int flags)
00423 {
00424         /*safe exec jtag code,if exception report,revert depc*/
00425         unsigned int depc;
00426         unsigned int volatile codebuf[] = tgt_compile(
00427                         "1:mtc0 ra,$31;\n"
00428                         "bal 2f;\n"
00429                         "nop;\n"
00430                         ".space 12;\n"
00431                         "2:sw v0,(ra);\n"
00432                         "mfc0 v0,$24;\n"
00433                         "sw v0,4(ra);\n"
00434                         "lw v0,(ra);\n"
00435                         "mfc0 ra,$31;\n"
00436                         "b 1b;\n"
00437                         "nop;\n"
00438                         );      
00439 
00440         volatile unsigned int codebuf1[] = tgt_compile(
00441                         "1:mtc0 ra,c0_desave;\n"
00442                         "bal 2f;\n"
00443                         "nop;\n"
00444                         ".space 16;\n"
00445                         "2:sw v0,(ra);\n"
00446                         "sw v1,4(ra);\n"
00447                         "mfc0 v0,c0_debug;\n"
00448                         "sw v0,8(ra);\n"
00449                         "lui v1,1<<4;\n"
00450                         "and v1,v0;\n"
00451                         "beqz v1,3f;\n"
00452                         "nop;\n"
00453                         "lw v0,12(ra);\n"
00454                         "mtc0 v0,c0_depc;\n"
00455                         "3:lw v1,4(ra);\n"
00456                         "lw v0,(ra);\n"
00457                         "mfc0 ra,c0_desave;\n"
00458                         "b 1b;\n"
00459                         "nop;\n"
00460                         );      
00461 
00462         fMIPS32_ExecuteDebugModule(codebuf,0x8000,1);
00463         depc = codebuf[4];
00464         fMIPS32_ExecuteDebugModule(pui_module,len,flags);
00465         codebuf1[6] = depc;
00466         fMIPS32_ExecuteDebugModule(codebuf1,0x8000,1);
00467         if(codebuf1[5]&(1<<20))
00468                 printf("exception in ejtag code!!!\n");
00469 
00470         return (codebuf1[5]&(1<<20))?1:0;
00471 }
00472 
00473 unsigned int fMIPS32_ExecuteDebugModule(unsigned int *pui_module,unsigned int len,unsigned int flags)
00474 {
00475   unsigned int ui_result;
00476   unsigned int ui_address;
00477   unsigned int ui_data;
00478   unsigned int ui_finished;
00479   unsigned int start_address;
00480   unsigned int end_address;
00481   unsigned int state=STATE_CONNECTING;
00482   unsigned int download_size=0;
00483   unsigned int init_do=0;
00484     //time_t *time_start=malloc(sizeof(time_t));
00485     //time_t *time_end=malloc(sizeof(time_t));
00486     time_t time_start;
00487     time_t time_end;
00488     time_t  time_deta=0;
00489         int runonce=flags&1;
00490         int waitacc=!(flags&2);
00491         int allowbefore=flags&4;
00492     
00493 
00494   fprintf(stderr,"JTAGEMUL: Executing a module.\n");
00495   fMIPS32_Init();
00496 
00497   fprintf(stderr,"connecting...\n");
00498 #if 0
00499   if(!Ejtag_boot_flag)
00500   {
00501         fprintf(stderr,"===============dJTAG_REGISTER_EJTAG_BOOT ==========\n");
00502         Ejtag_boot_flag=1;      
00503         fJTAG_Instruction(dJTAG_REGISTER_EJTAG_BOOT);
00504   }
00505          fprintf(stderr,"dJTAG_REGISTER_EJTAG_BOOT done\n");
00506 #endif
00507   
00508 while(1)
00509 {
00510   ui_result = dMIPS32_ECR_CPU_RESET_OCCURED|dMIPS32_ECR_ACCESS_PENDING|dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR ;
00511   fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00512   ui_result = fJTAG_Data(ui_result);
00513 
00514 if(!(ui_result&dMIPS32_ECR_IN_DEBUG_MODE))  {
00515     ui_result = dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR |dMIPS32_ECR_EJTAG_BREAK;
00516   fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00517   ui_result = fJTAG_Data(ui_result);
00518   continue;
00519   }
00520   fprintf(stderr,"connected\n");
00521   state=STATE_RUN;
00522   break;
00523 }
00524 
00525     fJTAG_Instruction(dJTAG_REGISTER_ADDRESS);
00526     start_address = fJTAG_Data(0x00)|0xff000000;
00527         end_address=start_address+len;
00528     ui_finished    = 0x00;
00529           fprintf(stderr,"start_address=%x,end_address=%x\n",start_address,end_address);
00530 
00531 
00532   // Feed that chip
00533   while (1)
00534   {
00535     // Read the control register.  Make sure an access is requested, then 
00536     // do it.
00537     ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
00538                 dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
00539   
00540     fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00541     ui_result = fJTAG_Data(ui_result);
00542         if(!(ui_result & dMIPS32_ECR_IN_DEBUG_MODE))
00543         {
00544       fprintf(stderr,"not in debug mode!\n");
00545     //            printf("not in debug mode!\n");
00546           return 0;
00547         }
00548     
00549     if (!(ui_result & dMIPS32_ECR_ACCESS_PENDING))
00550     {
00551           if(!waitacc)return -1;
00552           if(state!=STATE_WAITING)printf("waiting\n");
00553           state=STATE_WAITING;
00554           continue;
00555     }
00556         state=STATE_RUN;
00557 
00558         
00559     fJTAG_Instruction(dJTAG_REGISTER_ADDRESS);
00560     ui_address = fJTAG_Data(0x00)|0xff000000;
00561    
00562      if(ui_address<0xff200000|| ui_address>=0xff300000)
00563      {
00564      fprintf(stderr,"JTAGEMUL: %s to unknown location 0x%08X.\n", (ui_result & dMIPS32_ECR_ACCESS_RW)?"write":"read",ui_address);
00565      return 0;
00566      }
00567 
00568    //fprintf(stderr,"%s ui_address=%x,accesstype=%x\n",(ui_result & dMIPS32_ECR_ACCESS_RW)?"write":"read",ui_address,(ui_result>>29)&3);
00569     // Check for read or write
00570     if (ui_result & dMIPS32_ECR_ACCESS_RW) // Bit set for a WRITE
00571     {
00572       unsigned data0,data1,mask;
00573       int accesstype=(ui_result>>29)&3;
00574       // Read the data out
00575 #ifdef USE_FASTDATA
00576       fJTAG_Instruction(dJTAG_REGISTER_FASTDATA);
00577       ui_data = fJTAG_FastData(0x00);
00578 #else
00579           fJTAG_Instruction(dJTAG_REGISTER_DATA);
00580       ui_data = fJTAG_Data(0x00);
00581       // Clear the access pending bit (let the processor eat!)
00582       ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | 
00583                   dMIPS32_ECR_PROBE_VECTOR;
00584   
00585       fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00586       ui_result = fJTAG_Data(ui_result);
00587 #endif
00588 
00589         if(ui_address >= 0xFF200200)
00590         {
00591        data0=pui_module[(ui_address - start_address) / 4];
00592        data1=ui_data;
00593 
00594         switch(accesstype)
00595         {
00596         case 0:
00597         mask=0xff<<((ui_address&3)*8); break;
00598         case 1:
00599         mask=0xffff<<(((ui_address>>1)&1)*16); break;
00600         case 2: mask=0xffffffff;break;
00601         default: mask=0xffffff<<((ui_address&1)*8);break;
00602         }
00603 
00604         ui_data=(data1&mask)|(data0&~mask);
00605         }
00606 
00607       // Processor is writing to us
00608       if (ui_address >= 0xFF200200)
00609         pui_module[(ui_address - start_address) / 4]=ui_data;
00610       else
00611       {
00612         // Data out to the real world
00613         //fprintf(stderr,"JTAGEMUL: Write 0x%08X to address 0x%08X.\n", ui_data, ui_address);
00614         fMIPS32_HandleWrite(ui_address, ui_data, accesstype);
00615       }
00616     }
00617     else
00618     {
00619       // Check to see if its reading at the debug vector.  The first pass through
00620       // the module is always read at the vector, so the first one we allow.  When
00621       // the second read from the vector occurs we are done and just exit.
00622       if ((start_address == ui_address)&&runonce)
00623       {
00624         if (ui_finished++) // Allows ONE pass
00625         {
00626                 ui_finished=0;
00627          // fprintf(stderr,"JTAGEMUL: Finished module.\n");
00628           return ui_address;
00629         }
00630       }
00631 
00632       // Processor is reading from us
00633       if (ui_address >= 0xFF200200)
00634       {
00635             if(ui_address>=end_address|| ( ui_address<start_address && !allowbefore))return ui_address;
00636         // Reading an instruction from our module
00637 
00638         // Fetch the instruction from the module
00639         //fprintf(stderr,"JTAGEMUL: Instruction read at 0x%08X ", ui_address); fflush(stdout);
00640         ui_data = (ui_address - start_address) / 4;
00641         //fprintf(stderr,"offset -> %04d ", ui_data); fflush(stdout);
00642         ui_data = *(unsigned int *)(pui_module + ui_data);
00643         //fprintf(stderr,"data -> 0x%08X\n", ui_data); fflush(stdout);
00644         
00645         if(log_disasm)
00646         {
00647         char buf[256];
00648         md_disasm (buf, ui_address, ui_data);
00649         fprintf(stderr,"%s\n",buf);
00650         }
00651 
00652         // Send the data out
00653 #ifdef USE_FASTDATA
00654         fJTAG_Instruction(dJTAG_REGISTER_FASTDATA);
00655         ui_data = fJTAG_Data(ui_data);
00656 #else
00657           fJTAG_Instruction(dJTAG_REGISTER_DATA);
00658       ui_data = fJTAG_Data(ui_data);
00659         // Clear the access pending bit (let the processor eat!)
00660         ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | 
00661                     dMIPS32_ECR_PROBE_VECTOR;
00662   
00663         fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00664         ui_result = fJTAG_Data(ui_result);
00665 #endif
00666       }
00667       else
00668       {
00669         // A read from dmseg virtual registers
00670         ui_data = fMIPS32_HandleRead(ui_address);
00671       
00672 
00673         //fprintf(stderr,"JTAGEMUL: Read address 0x%08X result = 0x%08X.\n", ui_address, ui_data);
00674 
00675         // Send the data out
00676 #ifdef USE_FASTDATA
00677         fJTAG_Instruction(dJTAG_REGISTER_FASTDATA);
00678         ui_data = fJTAG_Data(ui_data);
00679 #else
00680           fJTAG_Instruction(dJTAG_REGISTER_DATA);
00681       ui_data = fJTAG_Data(ui_data);
00682         // Clear the access pending bit (let the processor eat!)
00683         ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | 
00684                     dMIPS32_ECR_PROBE_VECTOR;
00685   
00686         fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00687         ui_result = fJTAG_Data(ui_result);
00688 #endif
00689       }
00690     }    
00691   }  
00692 }
00693 
00694 unsigned int fMIPS32_Transfer(unsigned int *pui_module,unsigned int len)
00695 {
00696   unsigned int ui_result;
00697   unsigned int ui_address;
00698   unsigned int ui_data;
00699   unsigned int ui_finished;
00700   unsigned int start_address;
00701   unsigned int end_address;
00702   unsigned int state=STATE_CONNECTING;
00703   int i;
00704 
00705   fMIPS32_Init();
00706 
00707   
00708 while(1)
00709 {
00710   ui_result = dMIPS32_ECR_CPU_RESET_OCCURED|dMIPS32_ECR_ACCESS_PENDING|dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR ;
00711   fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00712   ui_result = fJTAG_Data(ui_result);
00713 
00714 if(!(ui_result&dMIPS32_ECR_IN_DEBUG_MODE))  {
00715     ui_result = dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR |dMIPS32_ECR_EJTAG_BREAK;
00716   fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00717   ui_result = fJTAG_Data(ui_result);
00718   continue;
00719   }
00720   fprintf(stderr,"connected\n");
00721   state=STATE_RUN;
00722   break;
00723 }
00724 
00725 
00726   // Feed that chip
00727   for(i=0;len;)
00728   {
00729     // Read the control register.  Make sure an access is requested, then 
00730     // do it.
00731     ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
00732                 dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
00733   
00734     fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00735     ui_result = fJTAG_Data(ui_result);
00736         if(!(ui_result & dMIPS32_ECR_IN_DEBUG_MODE))
00737         {
00738       fprintf(stderr,"not in debug mode!\n");
00739           return 0;
00740         }
00741     
00742     if (!(ui_result & dMIPS32_ECR_ACCESS_PENDING))
00743     {
00744           if(state!=STATE_WAITING)printf("waiting\n");
00745           state=STATE_WAITING;
00746           continue;
00747     }
00748         state=STATE_RUN;
00749 
00750 #if 0
00751     fJTAG_Instruction(dJTAG_REGISTER_ADDRESS);
00752     start_address = fJTAG_Data(0x00)|0xff000000;
00753     printf("start_address=0x%x\n",start_address);
00754 #endif
00755         
00756     if (ui_result & dMIPS32_ECR_ACCESS_RW) // Bit set for a WRITE
00757     {
00758       // Read the data out
00759 #ifdef USE_FASTDATA
00760       fJTAG_Instruction(dJTAG_REGISTER_FASTDATA);
00761       ui_data = fJTAG_FastData(0x00);
00762 #else
00763           fJTAG_Instruction(dJTAG_REGISTER_DATA);
00764       ui_data = fJTAG_Data(0x00);
00765       // Clear the access pending bit (let the processor eat!)
00766       ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | 
00767                   dMIPS32_ECR_PROBE_VECTOR;
00768   
00769       fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00770       ui_result = fJTAG_Data(ui_result);
00771 #endif
00772 
00773         pui_module[i++]=ui_data;
00774         len-=4;
00775     }
00776     else
00777     {
00778         ui_data = pui_module[i++];
00779         len -= 4;
00780         // Send the data out
00781 #ifdef USE_FASTDATA
00782         fJTAG_Instruction(dJTAG_REGISTER_FASTDATA);
00783         ui_data = fJTAG_Data(ui_data);
00784 #else
00785           fJTAG_Instruction(dJTAG_REGISTER_DATA);
00786       ui_data = fJTAG_Data(ui_data);
00787         // Clear the access pending bit (let the processor eat!)
00788         ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | 
00789                     dMIPS32_ECR_PROBE_VECTOR;
00790   
00791         fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00792         ui_result = fJTAG_Data(ui_result);
00793 #endif
00794       }
00795     }    
00796 }
00797 
00798 #if 1
00799 
00800 #define PACK_SIZE 0x100000
00801 unsigned int fMIPS32_USB_DownloadModule(unsigned int *pui_module,unsigned int len)
00802 {
00803   unsigned int ui_result;
00804   unsigned int ui_address;
00805   unsigned int ui_data;
00806   unsigned int ui_finished;
00807   unsigned int start_address;
00808   unsigned int end_address;
00809   unsigned int download_ram_size=0;
00810   unsigned int init_do=0;
00811   unsigned int pack_num=0;
00812   unsigned int pack_num_1=0;
00813   unsigned int i=0;
00814     //time_t *time_start=malloc(sizeof(time_t));
00815     //time_t *time_end=malloc(sizeof(time_t));
00816     time_t time_start;
00817     time_t time_end;
00818     time_t  time_deta=0;
00819     
00820 
00821   fprintf(stderr,"JTAGEMUL: Executing a module.\n");
00822   fMIPS32_Init();
00823 
00824 
00825   fprintf(stderr,"connecting...\n");
00826 #if 1
00827         if(!init_do)
00828         {
00829                 time_start = (time(&time_start))&0xffffffff;
00830                 printf("time_start : %x \n",time_start);
00831                 init_do = 1;
00832                 download_ram_size=0;
00833         }
00834         else
00835         {
00836                 if(!(download_ram_size%0x10000))
00837                 {
00838                 time_end = (time(&time_end))&0xffffffff;
00839                 printf("time_end : %x \n",time_end);
00840                 time_deta= time_end - time_start;
00841                 if(time_deta == 0)
00842                         time_deta++ ;
00843                 printf("time : 0x%x, download_size : %x, download rate=%d B/S\n",time_deta,download_ram_size,((download_ram_size/time_deta)));
00844                 }
00845         }
00846 #endif
00847         pack_num = len/PACK_SIZE ;
00848         printf("len : %d , pack_num : %d \n",len,pack_num);
00849         if(len % PACK_SIZE)
00850                 pack_num_1=1;
00851         //zgj-20090627  fJTAG_Instruction(dJTAG_REGISTER_DBTX);
00852         fJTAG_Instruction(dJTAG_REGISTER_FASTDATA);
00853          // ui_data = fJTAG_DBTX(ui_data);
00854 #if 1
00855         for(i=0;i<pack_num;i++)//20090628
00856         { //20090628
00857                 printf("pack_num : %d \n",i);
00858                 printf("pui_module addr 0x%x :%x \n", pui_module,*pui_module);
00859                 fJTAG_DBTX(PACK_SIZE,pui_module+i*(PACK_SIZE/4));
00860                 //fJTAG_DBTX(len,pui_module);
00861         }//20090628
00862         if(pack_num_1)
00863         {       
00864                 printf("LAST pui_module addr 0x%x :%x  i= %d \n", pui_module+i*PACK_SIZE,*(pui_module+i*PACK_SIZE/4),i);
00865                 fJTAG_DBTX(len - i*PACK_SIZE , pui_module + i*(PACK_SIZE/4));
00866         }
00867 #else
00868         printf("pui_module addr 0x%x :%x \n", pui_module,*pui_module);
00869         fJTAG_DBTX(len,pui_module);
00870 #endif    
00871          download_ram_size = len;
00872                 time_end = (time(&time_end))&0xffffffff;
00873                 printf("=<<<==time_end : %x \n",time_end);
00874                 time_deta= time_end - time_start;
00875                 if(time_deta == 0)
00876                         time_deta++ ;
00877                 printf("=<<<==time : %d, download_size : %x, download rate=%d B/S\n",time_deta,download_ram_size,((download_ram_size/time_deta)));
00878 }
00879 #endif
00880 
00881 
00882 
00883 
00884 //
00885 // fMIPS32_HandleWrite
00886 //
00887 // This routine takes the data and address from a write and puts it in the 
00888 // correct location.
00889 //
00890 static void fMIPS32_HandleWrite(unsigned int ui_address, unsigned int ui_data, int accesstype)
00891 {
00892   unsigned int data0,data1,mask;
00893   int index;
00894   switch(ui_address)
00895   {
00896     case dMIPS32_PSEUDO_SERIAL_ADDR:   /*simple put char*/
00897             putchar(ui_data);
00898             break;
00899     case dMIPS32_PSEUDO_SERIAL_ADDR+1:  /*printf predefined strings with argument*/
00900             index = (ui_data>>8)&0xff;
00901             printf(ui_datamem[index],ui_datamem[index+1],ui_datamem[index+2],ui_datamem[index+3],ui_datamem[index+4],ui_datamem[index+5],ui_datamem[index+6],ui_datamem[index+7],ui_datamem[index+8],ui_datamem[index+9],ui_datamem[index+10]);
00902             break;
00903     case dMIPS32_PSEUDO_FIFO_ADDR: 
00904             ui_fifomem[uififo_waddr++]=ui_data;        break;
00905     case dMIPS32_PSEUDO_STACK_ADDR:
00906                 ui_stack[ui_stack_index] = ui_data;
00907                 ui_stack_index=(ui_stack_index+1)&0x1f;
00908                 break;
00909     default:
00910                 data0=ui_datamem[(ui_address-dMIPS32_DATAMEM)>>2];
00911                 data1=ui_data;
00912 
00913                 switch(accesstype)
00914                 {
00915                         case 0:
00916                                 mask=0xff<<((ui_address&3)*8); break;
00917                         case 1:
00918                                 mask=0xffff<<(((ui_address>>1)&1)*16); break;
00919                         case 2: mask=0xffffffff;break;
00920                         default: mask=0xffffff<<((ui_address&1)*8);break;
00921                 }
00922 
00923                 ui_data=(data1&mask)|(data0&~mask);
00924                 ui_datamem[(ui_address-dMIPS32_DATAMEM)>>2]=ui_data;
00925       break;
00926   }
00927 }
00928 
00929 //
00930 // fMIPS32_HandleRead
00931 //
00932 // This routine takes the address from a read and returns the correct data
00933 //
00934 static unsigned int fMIPS32_HandleRead(unsigned int ui_address)
00935 {
00936   switch(ui_address)
00937   {
00938     case dMIPS32_PSEUDO_FIFO_ADDR: return ui_fifomem[uififo_raddr++];        break;
00939     case dMIPS32_PSEUDO_STACK_ADDR: return ui_stack[(--ui_stack_index)&0x1f]; break;
00940     default:
00941           if((ui_address>=dMIPS32_DATAMEM)&&(ui_address<dMIPS32_DATAMEM+0x400))
00942             return ui_datamem[(ui_address-dMIPS32_DATAMEM)>>2];
00943            else
00944       fprintf(stderr,"JTAGEMUL: Read to unknown location 0x%08X.\n", ui_address);
00945       break;
00946   }
00947   
00948   return(0);
00949 }
00950 
00951 //
00952 // fMIPS32_ExitDebugMode
00953 //
00954 // Exits debug mode using the DERET instruction
00955 static unsigned int fMIPS32_ExitDebugMode (void)
00956 {
00957   unsigned int ui_result;
00958 
00959   // Feed the processor a DERET instruction.
00960 
00961   // Read the control register and make sure processor is attempting to 
00962   // read from dmseg.
00963   ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
00964               dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
00965 
00966   fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00967   ui_result = fJTAG_Data(ui_result);
00968 
00969   if (ui_result & dMIPS32_ECR_ACCESS_PENDING)
00970   {
00971     // Make sure processor is not writing
00972     if (ui_result & dMIPS32_ECR_ACCESS_RW) // Bit set for a WRITE
00973     {
00974       // Chip is writing, need to clear the write
00975       printf("EJTAGEMUL ERROR: Processor writing on debug exit.\n");  
00976       return(0);
00977     }
00978 
00979     // Chip is reading from instruction space - feed it a DERET
00980     // Write the DERET instruction into the data register            
00981     fJTAG_Instruction(dJTAG_REGISTER_DATA);
00982     fJTAG_Data(dMIPS32_DERET_INSTRUCTION);
00983 
00984     // Now finish the processor access by clearing the access pending bit
00985     fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
00986 
00987     ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | 
00988                 dMIPS32_ECR_PROBE_VECTOR;
00989 
00990     ui_result = fJTAG_Data(ui_result);
00991 
00992     // At this point we are done.  Even if the chip didn't exit debug mode
00993     // the debug mode catcher will grab it and report the reason to GDB.
00994 //    printf("JTAGEMUL: Processor exited debug mode.\n");
00995     return(1);
00996   }
00997   else
00998   {
00999     // ERROR!  What to do if the processor is not accessing?
01000     printf("EJTAGEMUL ERROR: Processor not accessing for debug exit.\n");
01001     return(0);
01002   }
01003 
01004   // Should never get here!  
01005   return(0);
01006 }
01007 
01008 //
01009 // fMIPS32_EJTAG_Machine
01010 //
01011 // State machine that manages the the EJTAG processor module.
01012 // This state machine hands instructions and memory accesses
01013 // over the EJTAG port.
01014 //
01015 int fMIPS32_EJTAG_Machine(void)
01016 {
01017   unsigned int ui_result;
01018 
01019   // Need to handle resets here on each execution.
01020   ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
01021               dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
01022 
01023   fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
01024   ui_result = fJTAG_Data(ui_result);
01025 
01026   // Check for reset
01027   if (ui_result & dMIPS32_ECR_CPU_RESET_OCCURED)
01028   {
01029     // Reset occured - Must acknowlege this before EJTAG will work
01030     printf("JTAGEMUL: Processor was reset.\n");
01031     
01032     // Clear the reset and set our bits
01033     ui_result = dMIPS32_ECR_ACCESS_PENDING | dMIPS32_ECR_PROBE_ENABLE | 
01034                 dMIPS32_ECR_PROBE_VECTOR;
01035 
01036     ui_result = fJTAG_Data(ui_result);
01037   }
01038   
01039   switch(ui_ejtag_state)
01040   {
01041     case dMIPS32_EJTAG_WAIT_FOR_DEBUG_MODE:
01042       // Check to see if we have entered debug mode
01043 
01044       ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
01045                   dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
01046 
01047       fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
01048       ui_result = fJTAG_Data(ui_result);
01049       
01050       // Check for debug mode
01051       if (ui_result & dMIPS32_ECR_IN_DEBUG_MODE)
01052       {
01053         // We have entered debug mode.
01054         ui_ejtag_state = dMIPS32_EJTAG_WAIT_FOR_CONTINUE;
01055         
01056         printf("JTAGEMUL: Debug exception!\n");
01057 
01058         // Turn on the debug mode LED
01059 //        fMIPS32_ExecuteDebugModule(aui_led_code,0x80000);
01060 
01061         // Read out the debug register to determine the cause
01062  tgt_exec(\
01063 "1:\n" \
01064 "       mtc0    $15, $31;\n" \
01065 "       lui     $15, 0xFF20;\n" \
01066 "       sw      $2,  0x1fc($15);\n" \
01067 "       mfc0    $2,  $23;\n" \
01068 "       sw      $2,  0($15);\n" \
01069 "       lw      $2,  0x1fc($15);\n" \
01070 "       mfc0    $15, $31 ;\n" \
01071 "       b 1b;\n" \
01072 "       nop;\n" \
01073 ,0x8000);
01074 
01075         // We're done with ui_result at this point - Use it as a count
01076         // of how many exceptions matched.  We only ever want to match
01077         // one exception at any given time.
01078         ui_result = 0x00;
01079                 printf("ui_datamem[0]=%x\n",ui_datamem[0]);
01080 
01081         if (ui_datamem[0] & dMIPS32_DEBUG_SW_BKPT)
01082         {
01083           ui_result++;
01084           ui_last_signal=SIGTRAP; // Breakpoint
01085         }
01086         if (ui_datamem[0] & dMIPS32_DEBUG_HW_INST_BKPT)
01087         {
01088           ui_result++;
01089           ui_last_signal=SIGTRAP; // Breakpoint
01090         }
01091         if (ui_datamem[0] & dMIPS32_DEBUG_HW_DATA_LD_BKPT)
01092         {
01093           ui_result++;
01094           ui_last_signal=SIGTRAP; // Breakpoint
01095         }
01096         if (ui_datamem[0] & dMIPS32_DEBUG_HW_DATA_ST_BKPT)
01097         {
01098           ui_result++;
01099           ui_last_signal=SIGTRAP; // Breakpoint
01100         }
01101         if (ui_datamem[0] & dMIPS32_DEBUG_HW_DATA_LD_IMP_BKPT)
01102         {
01103           ui_result++;
01104           ui_last_signal=SIGTRAP; // Breakpoint
01105         }
01106         if (ui_datamem[0] & dMIPS32_DEBUG_HW_DATA_ST_IMP_BKPT)
01107         {
01108           ui_result++;
01109           ui_last_signal=SIGTRAP; // Breakpoint
01110         }
01111         if (ui_datamem[0] & dMIPS32_DEBUG_SINGLE_STEP_BKPT)
01112         {
01113           ui_result++;
01114           ui_last_signal=SIGTRAP; // Breakpoint
01115           
01116           // We need to clear the single step bit
01117           fMIPS32_ExecuteDebugModule(aui_singlestepclear_code,0x80000,1);
01118         }
01119         if (ui_datamem[0] & dMIPS32_DEBUG_JTAG_BKPT)
01120         {
01121           ui_result++;
01122           ui_last_signal=SIGTSTP; // User stop
01123         }
01124 
01125         // Check for AU1500 specific hardware breakpoint
01126         ui_datamem[0] = ui_datamem[0] >> 10;
01127         ui_datamem[0] &= 0x1F;
01128         if (dMIPS32_AU1500_HW_BKPT == ui_datamem[0])
01129         {
01130           ui_result++;
01131           ui_last_signal=SIGTRAP; // Breakpoint
01132         }
01133 
01134         if (ui_result == 0x00)
01135         {
01136           printf("JTAGEMUL ERROR: Unknown debug exception = 0x%08X\n", ui_datamem[0]);
01137         }
01138         if (ui_result > 0x01)
01139         {
01140           printf("JTAGEMUL ERROR: Multiple debug exceptions = 0x%08X\n", ui_datamem[0]);
01141         }
01142 
01143         return(-1); // Tell GDBRDP we entered debug mode
01144       }
01145       
01146       break;
01147 
01148     case dMIPS32_EJTAG_WAIT_FOR_CONTINUE:
01149       // Check to see if we have stayed in debug mode
01150 
01151       ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
01152                   dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
01153 
01154       fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
01155       ui_result = fJTAG_Data(ui_result);
01156       
01157       // Check for debug mode
01158       if (!(ui_result & dMIPS32_ECR_IN_DEBUG_MODE))
01159       {
01160         // Program got away from us and is RUNNING!?!?!?! ARGH!
01161         printf("JTAGEMUL: Program got away, currently running!\n");
01162 
01163         ui_ejtag_state = dMIPS32_EJTAG_WAIT_FOR_DEBUG_MODE;
01164       }
01165       
01166       break;
01167       
01168     case dMIPS32_EJTAG_EXIT_DEBUG_MODE:
01169       if (!fMIPS32_ExitDebugMode()) 
01170       {
01171         printf("JTAGEMUL ERROR: Could not resume!\n");
01172       }
01173 
01174       ui_ejtag_state = dMIPS32_EJTAG_WAIT_FOR_DEBUG_MODE;
01175       break;
01176 
01177     case dMIPS32_EJTAG_FORCE_DEBUG_MODE:
01178       // Force debug break  
01179       ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_ACCESS_PENDING |
01180                   dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR |
01181                   dMIPS32_ECR_EJTAG_BREAK;
01182   
01183       fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
01184       ui_result = fJTAG_Data(ui_result);
01185 
01186       ui_ejtag_state = dMIPS32_EJTAG_WAIT_FOR_DEBUG_MODE;
01187       break;
01188     
01189     default:
01190       // Should never get here
01191       ui_ejtag_state = dMIPS32_EJTAG_WAIT_FOR_DEBUG_MODE;
01192       printf("JTAGEMUL ERROR: Corrupted debug mode catcher state!\n");
01193       break;  
01194   }
01195   
01196   return(1);
01197 }
01198 
01199 unsigned char jtag_inb(unsigned long addr)
01200 {
01201       ui_datamem[0]=addr;
01202           tgt_exec(\
01203           ".set noat;\n" \
01204           "1:\n" \
01205           "mtc0    $15, $31;\n" \
01206           "li    $15,0xFF200000;\n" \
01207           "sw    $2, 0x1fc($15);\n" \
01208           "sw    $3, 0x1fc($15);\n" \
01209           "lw    $2, ($15);\n" \
01210           "lb    $3, ($2);\n" \
01211           "sb    $3, 4($15);\n" \
01212           "lw    $3, 0x1fc($15);\n" \
01213           "lw    $2, 0x1fc($15);\n" \
01214           "mfc0    $15, $31;\n" \
01215           "b 1b;\n" \
01216           "nop;\n" \
01217           ,0x80000);
01218           return (unsigned char)(ui_datamem[1]&0xff);
01219 }
01220 
01221 unsigned short jtag_inw(unsigned long addr)
01222 {
01223       ui_datamem[0]=addr;
01224           tgt_exec(\
01225           ".set noat;\n" \
01226           "1:\n" \
01227           "mtc0    $15, $31;\n" \
01228           "li    $15,0xFF200000;\n" \
01229           "sw    $2, 0x1fc($15);\n" \
01230           "sw    $3, 0x1fc($15);\n" \
01231           "lw    $2, ($15);\n" \
01232           "lh    $3, ($2);\n" \
01233           "sh    $3, 4($15);\n" \
01234           "lw    $3, 0x1fc($15);\n" \
01235           "lw    $2, 0x1fc($15);\n" \
01236           "mfc0    $15, $31;\n" \
01237           "b 1b;\n" \
01238           "nop;\n" \
01239           ,0x80000);
01240           return (unsigned short)(ui_datamem[1]&0xffff);
01241 }
01242 
01243 unsigned int jtag_inl(unsigned long addr)
01244 {
01245       ui_datamem[0]=addr;
01246           tgt_exec(\
01247           ".set noat;\n" \
01248           "1:\n" \
01249           "mtc0    $15, $31;\n" \
01250           "li    $15,0xFF200000;\n" \
01251           "sw    $2, 0x1fc($15);\n" \
01252           "sw    $3, 0x1fc($15);\n" \
01253           "lw    $2, ($15);\n" \
01254           "lw    $3, ($2);\n" \
01255           "sw    $3, 4($15);\n" \
01256           "lw    $3, 0x1fc($15);\n" \
01257           "lw    $2, 0x1fc($15);\n" \
01258           "mfc0    $15, $31;\n" \
01259           "b 1b;\n" \
01260           "nop;\n" \
01261           ,0x80000);
01262           return (unsigned int)ui_datamem[1];
01263 }
01264 
01265 void jtag_outb(unsigned long addr,unsigned char val)
01266 {
01267       ui_datamem[0]=addr;
01268       ui_datamem[1]=val;
01269 
01270           tgt_exec(\
01271           ".set noat;\n" \
01272           "1:\n" \
01273           "mtc0    $15, $31;\n" \
01274           "li    $15,0xFF200000;\n" \
01275           "sw    $2, 0x1fc($15);\n" \
01276           "sw    $3, 0x1fc($15);\n" \
01277           "lw    $2, ($15);\n" \
01278           "lw    $3, 0x4($15);\n" \
01279           "sb    $3, ($2);\n" \
01280           "lw    $3, 0x1fc($15);\n" \
01281           "lw    $2, 0x1fc($15);\n" \
01282           "mfc0    $15, $31;\n" \
01283           "b 1b;\n" \
01284           "nop;\n" \
01285           ,0x80000);
01286 }
01287 
01288 void jtag_outw(unsigned long addr,unsigned short val)
01289 {
01290       ui_datamem[0]=addr;
01291       ui_datamem[1]=val;
01292           tgt_exec(\
01293           ".set noat;\n" \
01294           "1:\n" \
01295           "mtc0    $15, $31;\n" \
01296           "li    $15,0xFF200000;\n" \
01297           "sw    $2, 0x1fc($15);\n" \
01298           "sw    $3, 0x1fc($15);\n" \
01299           "lw    $2, ($15);\n" \
01300           "lw    $3, 0x4($15);\n" \
01301           "sh    $3, ($2);\n" \
01302           "lw    $3, 0x1fc($15);\n" \
01303           "lw    $2, 0x1fc($15);\n" \
01304           "mfc0    $15, $31;\n" \
01305           "b 1b;\n" \
01306           "nop;\n" \
01307           ,0x80000);
01308 }
01309 
01310 void jtag_outl(unsigned long addr,unsigned int val)
01311 {
01312       ui_datamem[0]=addr;
01313       ui_datamem[1]=val;
01314 
01315           tgt_exec(\
01316           ".set noat;\n" \
01317           "1:\n" \
01318           "mtc0    $15, $31;\n" \
01319           "li    $15,0xFF200000;\n" \
01320           "sw    $2, 0x1fc($15);\n" \
01321           "sw    $3, 0x1fc($15);\n" \
01322           "lw    $2, ($15);\n" \
01323           "lw    $3, 0x4($15);\n" \
01324           "sw    $3, ($2);\n" \
01325           "lw    $3, 0x1fc($15);\n" \
01326           "lw    $2, 0x1fc($15);\n" \
01327           "mfc0    $15, $31;\n" \
01328           "b 1b;\n" \
01329           "nop;\n" \
01330           ,0x80000);
01331 
01332 }
01333 
01334 #define min(a,b) (a<b?a:b)
01335 void jtag_getmem(char *dst,unsigned long addr,unsigned int len)
01336 {
01337           int once;
01338      while(len)
01339      {
01340           uififo_raddr=0;
01341           uififo_waddr=0;
01342           once=min(len,ui_fifomem_size);
01343           if(once<4)once=4;
01344       ui_datamem[0]=addr&~3;
01345       ui_datamem[1]=(addr+once-4)&~3;
01346       once=ui_datamem[1]-ui_datamem[0]+4-(addr&3);
01347 
01348           tgt_exec(\
01349           ".set noat;\n" \
01350           ".set noreorder;\n" \
01351           "1:\n" \
01352           "mtc0    $15, $31;\n" \
01353           "li    $15,0xFF200000;\n" \
01354           "sw    $1, 0x1FC($15);\n" \
01355           "sw    $2, 0x1FC($15);\n" \
01356           "sw    $3, 0x1FC($15);\n" \
01357           "lw    $1, ($15);\n" \
01358           "lw    $2, 0x4($15);\n" \
01359           "2:\n" \
01360           "lw    $3, ($1);\n" \
01361           "sw    $3, 0x1F8($15);\n" \
01362           "bne   $1,$2,2b;\n" \
01363           "addu  $1, 4;\n" \
01364           "lw    $3, ($15);\n" \
01365           "lw    $2, ($15);\n" \
01366           "lw    $1, ($15);\n" \
01367           "mfc0    $15, $31;\n" \
01368           "b 1b;\n" \
01369           "nop;\n" \
01370           ,0x80000);
01371           once=min(once,len);
01372           memcpy(dst,(char *)ui_fifomem+(addr&3),once);
01373           len-=once;
01374           dst+=once;
01375           addr+=once;
01376    }
01377 
01378 }
01379 
01380 void jtag_putmem(char *src,unsigned long addr,unsigned int len)
01381 {
01382           int once;
01383      while(len)
01384      {
01385           uififo_raddr=0;
01386           uififo_waddr=0;
01387           once=min(len,ui_fifomem_size);
01388           if(once<4)once=4;
01389       ui_datamem[0]=addr&~3;
01390       ui_datamem[1]=(addr+once-4)&~3;
01391       once=ui_datamem[1]-ui_datamem[0]+4-(addr&3);
01392       printf("addr=%x,len=%x\n",addr,len);
01393 
01394           memcpy((char *)ui_fifomem+(addr&3),src,once);
01395 
01396           tgt_exec(\
01397           ".set noat;\n" \
01398           ".set noreorder;\n" \
01399           "1:\n" \
01400           "mtc0    $15, $31;\n" \
01401           "li    $15,0xFF200000;\n" \
01402           "sw    $1, 0x1FC($15);\n" \
01403           "sw    $2, 0x1FC($15);\n" \
01404           "sw    $3, 0x1FC($15);\n" \
01405           "lw    $1, ($15);\n" \
01406           "lw    $2, 0x4($15);\n" \
01407           "2:\n" \
01408           "lw    $3, 0x1F8($15);\n" \
01409           "sw    $3, ($1);\n" \
01410           "bne   $1,$2,2b;\n" \
01411           "addu  $1, 4;\n" \
01412           "lw    $3, ($15);\n" \
01413           "lw    $2, ($15);\n" \
01414           "lw    $1, ($15);\n" \
01415           "mfc0    $15, $31;\n" \
01416           "b 1b;\n" \
01417           "nop;\n" \
01418           ,0x80000);
01419           once=min(once,len);
01420           len-=once;
01421           src+=once;
01422           addr+=once;
01423         }
01424 }
01425 
01426 void jtag_quit()
01427 {
01428 tgt_exec("deret;",4); 
01429 debug_reg = 0;
01430 }
01431 
01432 #include "readregisters.h"
01433 #include "writeregisters.h"
01434 
01435 #if 1
01436 
01437 unsigned int code_inst_download_crc[100]=
01438 tgt_compile(\
01439 ".set mips32\n" \
01440 "mfc0 t5,$16,6;\n" \
01441 "li t7,512;\n" \
01442 "nor t7,t7,zero;\n" \
01443 "and t5,t5,t7;\n" \
01444 "1:mtc0 t5,$16,6;\n" \
01445 "lui t5,0xa530;\n" \
01446 "mfc0 t6,$16,6;\n" \
01447 "andi t6,t6,0x200;\n" \
01448 "bnez t6,2f;\n" \
01449 "nop ;\n" \
01450 "mfc0 t6,$16,6;\n" \
01451 "andi t6,t6,0x100;\n" \
01452 "beq zero,t6,1b;\n" \
01453 "nop ;\n" \
01454 "mfc0 t7,$22,6;\n" \
01455 "sw t7,0(t5);\n" \
01456 "addi t5,t5,4;\n" \
01457 "mfc0 t6,$16,6;\n" \
01458 "li t7,256;\n" \
01459 "nor t7,t7,zero;\n" \
01460 "and t6,t6,t7;\n" \
01461 "mtc0 t6,$16,6;\n" \
01462 "nop ;\n" \
01463 "mfc0 t6,$16,6;\n" \
01464 "andi t6,t6,0x200;\n" \
01465 "beqz t6,1b;\n" \
01466 "nop ;\n" \
01467 "2:mfc0 t5,$16,6;\n" \
01468 "li t7,512;\n" \
01469 "nor t7,t7,zero;\n" \
01470 "and t5,t5,t7;\n" \
01471 "mtc0 t5,$16,6;\n" \
01472 "nop ;\n" \
01473 "nop ;\n" \
01474 "mfc0 t7,c0_taghi;\n" \
01475 "jr t7;\n" \
01476 "nop ;\n" \
01477 );
01478 
01479 
01480 void jtag_self_down_crc(unsigned long addr_download)
01481 {
01482         printf("download self download to %p\n",addr_download);
01483         jtag_putmem(code_inst_download_crc,addr_download,sizeof(code_inst_download_crc));
01484         printf("jtag client len and crc download over!\n");
01485 }
01486 #endif
01487 
01488 #if 1
01489 
01490 void jtag_download_crc_checksum(unsigned long addr_download,unsigned int buf_crc_checksum)
01491 {
01492                 printf("download buf CRC32 checksum download to %p , CRC :0x%x \n",addr_download,buf_crc_checksum);
01493                 jtag_outl(addr_download,buf_crc_checksum);
01494 }
01495 #endif
01496 
01497 void jtag_download_length(unsigned long addr_download,unsigned int buf_length)
01498 {
01499                 jtag_outl(addr_download,buf_length);
01500 }
01501 
01502 /*
01503  * param +0: dest address
01504  * param +4: length
01505  *
01506  */
01507 unsigned int code_inst_download[]=
01508 tgt_compile(\
01509 "bal 1f;\n" \
01510 "nop;\n" \
01511 "param:\n" \
01512 ".space 40 \n" \
01513 "1:\n" \
01514 "lw t4,0(ra);\n" \
01515 "lw t5,4(ra);\n" \
01516 "move t3,zero;\n" \
01517 "lui t6,0xff20;\n" \
01518 "ori t6,t6,0xfe04;\n" \
01519 "1:lw t7,0(t6);\n" \
01520 "sw t7,0(t5);\n" \
01521 "addu t3,t3,4;\n" \
01522 "slt at,t3,t4;\n" \
01523 "bnez at,1b;\n" \
01524 "addi t5,t5,4;\n" \
01525 "nop ;\n" \
01526 );
01527 
01528 void jtag_self_down(unsigned long addr_download)
01529 {
01530         int i=0;
01531         unsigned long ui_address=addr_download;
01532         printf("download self download to %p\n",addr_download);
01533         jtag_putmem(code_inst_download,addr_download,sizeof(code_inst_download));
01534         printf("jtag client download over!\n");
01535 }
01536 
01537 
01538 unsigned int code_inst_upload[100]=
01539 tgt_compile(\
01540 "lui t6,0xa530;\n" \
01541 "lw t4,0(t6);\n" \
01542 "lui t5,0xbfc0;\n" \
01543 "add t4,t4,t5;\n" \
01544 "lui t6,0xff2f;\n" \
01545 "ori t6,t6,0xfe04;\n" \
01546 "1:lw t7,0(t5);\n" \
01547 "sw t7,0(t6);\n" \
01548 "bne t4,t5,1b;\n" \
01549 "addi t5,t5,4;\n" \
01550 "nop ;\n" \
01551 );
01552 
01553 
01554 void jtag_self_down_up(unsigned long addr_download)
01555 {
01556         printf("download self download to %p\n",addr_download);
01557         jtag_putmem(code_inst_upload,addr_download,sizeof(code_inst_upload));
01558 
01559         printf("jtag client download over!\n");
01560 }
01561 
01562 #if 1
01563 
01564 unsigned int code_inst_crc[100]=
01565 tgt_compile(\
01566 ".set mips32\n" \
01567 "lui k0,0x3333;\n" \
01568 "ori k0,k0,0x3333;\n" \
01569 "lui k1,0x0;\n" \
01570 "ori k1,k1,0x0;\n" \
01571 "move a0,zero;\n" \
01572 "lui a1,0xa020;\n" \
01573 "ori a1,a1,0x0;\n" \
01574 "lui t7,0xa530;\n" \
01575 "ori t7,t7,0x0;\n" \
01576 "lw a2,0(t7);\n" \
01577 "addu a2,a1,a2;\n" \
01578 "sltu v0,a1,a2;\n" \
01579 "beqz v0,1f;\n" \
01580 "nor a3,zero,a0;\n" \
01581 "lui v0,0xa500;\n" \
01582 "ori t0,v0,0x100;\n" \
01583 "2:lbu v0,0(a1);\n" \
01584 "addiu a1,a1,1;\n" \
01585 "xor v0,a3,v0;\n" \
01586 "andi v0,v0,0xff;\n" \
01587 "sll v0,v0,0x2;\n" \
01588 "addu v0,v0,t0;\n" \
01589 "lw a0,0(v0);\n" \
01590 "sltu v1,a1,a2;\n" \
01591 "srl v0,a3,0x8;\n" \
01592 "bnez v1,2b;\n" \
01593 "xor a3,a0,v0;\n" \
01594 "1:nor v0,zero,a3;\n" \
01595 "lui k0,0x8680;\n" \
01596 "ori k0,k0,0x1125;\n" \
01597 "move k1,v0;\n" \
01598 "lui t7,0xa030;\n" \
01599 "ori t7,t7,0x0;\n" \
01600 "nop ;\n" \
01601 "sdbbp ;\n" \
01602 "nop ;\n" \
01603 "nop ;\n" \
01604 );
01605 
01606 unsigned int code_inst_crc_old[100]=
01607 tgt_compile(\
01608 ".set mips32\n" \
01609 "lui k0,0x3333;\n" \
01610 "ori k0,k0,0x3333;\n" \
01611 "lui t7,0xa530;\n" \
01612 "ori t7,t7,0x0;\n" \
01613 "lw t6,0(t7);\n" \
01614 "lui t5,0xa030;\n" \
01615 "ori t5,t5,0x0;\n" \
01616 "lui a3,0xa500;\n" \
01617 "ori a3,a3,0x100;\n" \
01618 "li a0,0;\n" \
01619 "nor a1,a0,zero;\n" \
01620 "add t4,t5,t6;\n" \
01621 "sltu t0,t4,t5;\n" \
01622 "bnez t0,1f;\n" \
01623 "nop ;\n" \
01624 "2:lw t3,0(t5);\n" \
01625 "addi t3,t3,1;\n" \
01626 "xor a0,a1,t3;\n" \
01627 "andi a0,a0,0xff;\n" \
01628 "sll a0,a0,0x2;\n" \
01629 "add a0,a0,a3;\n" \
01630 "lw t2,0(a0);\n" \
01631 "sltu t1,t4,t5;\n" \
01632 "srl a1,a1,0x3;\n" \
01633 "beqz t1,2b;\n" \
01634 "xor a1,a1,t2;\n" \
01635 "1:lui k0,0x8680;\n" \
01636 "ori k0,k0,0x1125;\n" \
01637 "move k1,a1;\n" \
01638 "nop ;\n" \
01639 "lui t7,0xa030;\n" \
01640 "ori t7,t7,0x0;\n" \
01641 "nop ;\n" \
01642 );
01643 
01644 void jtag_crc_down(unsigned long addr_download)
01645 {
01646         int i=0;
01647         unsigned long ui_address=addr_download;
01648         printf("download self download to %p\n",addr_download);
01649         jtag_putmem(code_inst_crc,addr_download,sizeof(code_inst_crc));
01650         printf("jtag CRC code download over!\n");
01651 }
01652 
01653 #endif
01654 
01655 #if 1
01656 
01657 /* Table computed with Mark Adler's makecrc.c utility.  */
01658 static const unsigned int crc32_table_down[256] =
01659 {
01660   0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
01661   0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
01662   0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
01663   0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
01664   0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
01665   0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
01666   0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
01667   0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
01668   0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
01669   0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
01670   0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
01671   0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
01672   0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
01673   0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
01674   0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
01675   0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
01676   0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
01677   0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
01678   0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
01679   0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
01680   0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
01681   0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
01682   0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
01683   0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
01684   0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
01685   0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
01686   0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
01687   0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
01688   0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
01689   0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
01690   0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
01691   0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
01692   0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
01693   0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
01694   0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
01695   0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
01696   0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
01697   0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
01698   0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
01699   0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
01700   0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
01701   0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
01702   0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
01703   0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
01704   0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
01705   0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
01706   0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
01707   0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
01708   0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
01709   0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
01710   0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
01711   0x2d02ef8d
01712 };
01713 
01714 void jtag_crc_table_down(unsigned long addr_download)
01715 {
01716         int i=0;
01717         unsigned long ui_address=addr_download;
01718         printf("download self download to %p\n",addr_download);
01719         jtag_putmem(crc32_table_down,addr_download,sizeof(crc32_table_down));
01720         printf("jtag CRC code download over!\n");
01721 }
01722 #endif
01723 
01724 #if 1
01725 unsigned int jtag_wait_k0_flag(unsigned long addr_readreg)
01726 {
01727         unsigned int k0_val=0;
01728         unsigned int k1_val=0;
01729         int i=0;
01730         unsigned long ui_address = addr_readreg;
01731 
01732         printf("======>>>>k0_val : 0x%x \n",k0_val);
01733         do
01734         {
01735         tgt_exec(\
01736           ".set noat;\n" \
01737           "1:\n" \
01738           "mtc0    $15, $31;\n" \
01739           "li    $15,0xFF200000;\n" \
01740           "sw    k0, ($15);\n" \
01741           "sw    k1,4($15);\n" \
01742           "mfc0    $15, $31;\n" \
01743           "b 1b;\n" \
01744           "nop;\n" \
01745           ,0x80000);
01746           jtag_quit();
01747 
01748       k0_val=ui_datamem[0];
01749                 sleep(2);
01750                 printf("======>>>>>>>>>>>>>>>>k0_val : 0x%x \n",k0_val);
01751         }while(k0_val != 0x86801125) ;
01752 
01753         k1_val = ui_datamem[1];
01754         return k1_val;
01755 }
01756 #endif
01757 
01758 unsigned int context_save(int *context_reg)
01759 {
01760         int i=0;
01761           uififo_raddr=0;
01762           uififo_waddr=0;
01763           memset(ui_fifomem,0,38*4);
01764         fMIPS32_ExecuteDebugModule(aui_readregisters_code,0x80000,1);
01765         
01766         for(i=0;i<dMIPS32_NUM_DEBUG_REGISTERS;i++)
01767         {
01768                 context_reg[i]=ui_fifomem[i];
01769         }
01770         return 0;
01771         
01772 }
01773 
01774 unsigned int context_restore(int *context_reg)
01775 {
01776         int i=0;
01777           uififo_raddr=0;
01778           uififo_waddr=0;
01779         for(i=0;i<dMIPS32_NUM_DEBUG_REGISTERS;i++)
01780         {
01781                 ui_fifomem[i]=context_reg[i];
01782         }
01783         fMIPS32_ExecuteDebugModule(aui_writeregisters_code,0x80000,1);
01784         return 0;
01785         
01786 }
01787 
01788 
01789 unsigned int get_DR2(void)
01790 {
01791       unsigned int ui_data=0 ;
01792       unsigned int ui_result=0 ;
01793 
01794 #if 1
01795       // Clear the access pending bit (let the processor eat!)
01796 
01797       ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
01798       fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
01799       ui_result = fJTAG_Data(ui_result);
01800 #endif
01801 }
01802 
01803 unsigned int get_DR(unsigned int upload_len)
01804 {
01805       unsigned int ui_data=0 ;
01806       unsigned int ui_result=0 ;
01807 
01808 #if 1 //20090703-zgj
01809 
01810       // Clear the access pending bit (let the processor eat!)
01811 
01812       ui_result = dMIPS32_ECR_CPU_RESET_OCCURED | dMIPS32_ECR_PROBE_ENABLE | dMIPS32_ECR_PROBE_VECTOR;
01813 
01814       fJTAG_Instruction(dJTAG_REGISTER_CONTROL);
01815       ui_result = fJTAG_Data(ui_result);
01816 #endif
01817 
01818        fJTAG_Instruction(dJTAG_REGISTER_DATA);
01819        ui_data = fJTAG_Data(0x00);
01820 
01821 
01822       return ui_data;
01823 }
01824 
01825 void USB_UPLOAD_FAST(unsigned int upload_len)
01826 {
01827         fJTAG_Instruction(dJTAG_REGISTER_FASTDATA);
01828         fJTAG_FAST_UPLOAD(upload_len);
01829 }
01830 
01831 unsigned int miro_inst[]=
01832 tgt_compile(\
01833 ".set mips32\n" \
01834 "1:mtc0 t7,c0_desave;\n" \
01835 "mfc0 t7,$16,6;\n" \
01836 "xori t7,t7,0x220;\n" \
01837 "mtc0 t7,$16,6;\n" \
01838 "mfc0 t7,c0_desave;\n" \
01839 "nop ;\n" \
01840 "b 1b;\n" \
01841 "nop ;\n" \
01842 );
01843 
01844 
01845 unsigned int miro_inst_2[]=
01846 tgt_compile(\
01847 ".set mips32;\n" \
01848 "mtc0 t7,c0_desave;\n" \
01849 "lui t7,0xa500;\n" \
01850 "mtc0 t7,c0_depc;\n" \
01851 "nop ;\n" \
01852 "mfc0 t7,c0_desave;\n" \
01853 "mfc0 t7,c0_depc;\n" \
01854 "nop ;\n" \
01855 "jr t7;\n" \
01856 "nop ;\n" \
01857 );
01858 
01859 
01860 unsigned int miro_initsdram_inst[]=
01861 tgt_compile(\
01862 "1:mtc0 t7,c0_desave;\n" \
01863 "lui a2,0xbf00;\n" \
01864 "lui a1,0xc40;\n" \
01865 "ori a1,a1,0x2d4;\n" \
01866 "sw a1,0(a2);\n" \
01867 "lui a1,0x2098;\n" \
01868 "ori a1,a1,0xcd;\n" \
01869 "sw a1,4(a2);\n" \
01870 "mfc0 t7,c0_desave;\n" \
01871 "nop ;\n" \
01872 "b 1b;\n" \
01873 "nop ;\n" \
01874 "nop ;\n" \
01875 );
01876 
01877 
01878 unsigned int miro_initsdram_inst_2[]=
01879 tgt_compile(\
01880 "mtc0 t7,c0_desave;\n" \
01881 "mfc0 t7,c0_depc;\n" \
01882 "mtc0 t7,c0_taghi;\n" \
01883 "lui t7,0xa020;\n" \
01884 "mtc0 t7,c0_depc;\n" \
01885 "nop ;\n" \
01886 "mfc0 t7,c0_desave;\n" \
01887 "nop ;\n" \
01888 "deret ;\n" \
01889 "nop ;\n" \
01890 );
01891 
01892 
01893 unsigned int miro_download_os_ret_inst[]=
01894 tgt_compile(\
01895 "mtc0 t7,c0_desave;\n" \
01896 "mfc0 t7,c0_taghi;\n" \
01897 "mtc0 t7,c0_depc;\n" \
01898 "nop ;\n" \
01899 "mfc0 t7,c0_desave;\n" \
01900 "nop ;\n" \
01901 "deret ;\n" \
01902 "nop ;\n" \
01903 );
01904 
01905 unsigned int miro_savedepc_inst[]=
01906 tgt_compile(\
01907 "mtc0 t7,c0_desave;\n" \
01908 "mfc0 t7,c0_depc;\n" \
01909 "mtc0 t7,c0_taghi;\n" \
01910 "nop ;\n" \
01911 "mfc0 t7,c0_desave;\n" \
01912 "nop ;\n" \
01913 "deret ;\n" \
01914 "nop ;\n" \
01915 );
01916 
01917 unsigned int miro_runcrc_inst[]=
01918 tgt_compile(\
01919 "mtc0 t7,c0_desave;\n" \
01920 "lui t7,0xa540;\n" \
01921 "mtc0 t7,c0_depc;\n" \
01922 "nop ;\n" \
01923 "mfc0 t7,c0_desave;\n" \
01924 "nop ;\n" \
01925 "deret ;\n" \
01926 "nop ;\n" \
01927 );
01928 
01929 
01930 unsigned int miro_initcp0_inst[]=
01931 tgt_compile(\
01932 "1:mtc0 zero,c0_status;\n" \
01933 "mtc0 zero,c0_cause;\n" \
01934 "lui t0,0x40;\n" \
01935 "mtc0 t0,c0_status;\n" \
01936 "lui sp,0x8100;\n" \
01937 "addiu sp,sp,-16384;\n" \
01938 "lui gp,0x8106;\n" \
01939 "addiu gp,gp,-11248;\n" \
01940 "b 1b;\n" \
01941 "nop ;\n" \
01942 );
01943 

Generated on Fri Nov 5 07:24:46 2010 for ejtag debug tools for loongson 232 cpu by  doxygen 1.5.6