Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. // This file is part of the Cyclone 68000 Emulator
  3.  
  4. // Copyright (c) 2004,2011 FinalDave (emudave (at) gmail.com)
  5. // Copyright (c) 2005-2011 GraÅžvydas "notaz" Ignotas (notasas (at) gmail.com)
  6.  
  7. // This code is licensed under the GNU General Public License version 2.0 and the MAME License.
  8. // You can choose the license that has the most advantages for you.
  9.  
  10. // SVN repository can be found at http://code.google.com/p/cyclone68000/
  11.  
  12.  
  13. #include "app.h"
  14.  
  15. int opend_op_changes_cycles, opend_check_interrupt, opend_check_trace;
  16.  
  17. static unsigned char OpData[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  18.  
  19. static unsigned short OpRead16(unsigned int a)
  20. {
  21.   return (unsigned short)( (OpData[a&15]<<8) | OpData[(a+1)&15] );
  22. }
  23.  
  24. // For opcode 'op' use handler 'use'
  25. void OpUse(int op,int use)
  26. {
  27.   char text[64]="";
  28.   CyJump[op]=use;
  29.  
  30.   if (op!=use) return;
  31.  
  32.   // Disassemble opcode
  33.   DisaPc=0;
  34.   DisaText=text;
  35.   DisaWord=OpRead16;
  36.  
  37.   DisaGet();
  38.   ot(";@ ---------- [%.4x] %s uses Op%.4x ----------\n",op,text,use);
  39. }
  40.  
  41. void OpStart(int op, int sea, int tea, int op_changes_cycles, int supervisor_check)
  42. {
  43.   int last_op_count=arm_op_count;
  44.  
  45.   Cycles=0;
  46.   OpUse(op,op); // This opcode obviously uses this handler
  47.   ot("Op%.4x%s\n", op, ms?"":":");
  48.  
  49.   if (supervisor_check)
  50.   {
  51.     // checks for supervisor bit, if not set, jumps to SuperEnd()
  52.     // also sets r11 to SR high value, SuperChange() uses this
  53.     ot("  ldr r11,[r7,#0x44] ;@ Get SR high\n");
  54.   }
  55.   if ((sea >= 0x10 && sea != 0x3c) || (tea >= 0x10 && tea != 0x3c))
  56.   {
  57. #if MEMHANDLERS_NEED_PREV_PC
  58.     ot("  str r4,[r7,#0x50] ;@ Save prev PC + 2\n");
  59. #endif
  60. #if MEMHANDLERS_NEED_CYCLES
  61.     ot("  str r5,[r7,#0x5c] ;@ Save Cycles\n");
  62. #endif
  63.   }
  64.   if (supervisor_check)
  65.   {
  66.     ot("  tst r11,#0x20 ;@ Check we are in supervisor mode\n");
  67.     ot("  beq WrongPrivilegeMode ;@ No\n");
  68.   }
  69.   if ((sea >= 0x10 && sea != 0x3c) || (tea >= 0x10 && tea != 0x3c)) {
  70. #if MEMHANDLERS_CHANGE_CYCLES
  71.     if (op_changes_cycles)
  72.       ot("  mov r5,#0\n");
  73. #else
  74.     (void)op_changes_cycles;
  75. #endif
  76.   }
  77.   if (last_op_count!=arm_op_count)
  78.     ot("\n");
  79.   pc_dirty = 1;
  80.   opend_op_changes_cycles = opend_check_interrupt = opend_check_trace = 0;
  81. }
  82.  
  83. void OpEnd(int sea, int tea)
  84. {
  85.   int did_fetch=0;
  86.   opend_check_trace = opend_check_trace && EMULATE_TRACE;
  87. #if MEMHANDLERS_CHANGE_CYCLES
  88.   if ((sea >= 0x10 && sea != 0x3c) || (tea >= 0x10 && tea != 0x3c))
  89.   {
  90.     if (opend_op_changes_cycles)
  91.     {
  92.       ot("  ldr r0,[r7,#0x5c] ;@ Load Cycles\n");
  93.       ot("  ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
  94.       ot("  add r5,r0,r5\n");
  95.       did_fetch=1;
  96.     }
  97.     else
  98.     {
  99.       ot("  ldr r5,[r7,#0x5c] ;@ Load Cycles\n");
  100.     }
  101.   }
  102. #else
  103.   (void)sea;
  104.   (void)tea;
  105. #endif
  106.   if (!did_fetch)
  107.     ot("  ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
  108.   if (opend_check_trace)
  109.     ot("  ldr r1,[r7,#0x44]\n");
  110.   ot("  subs r5,r5,#%d ;@ Subtract cycles\n",Cycles);
  111.   if (opend_check_trace)
  112.   {
  113.     ot(";@ CheckTrace:\n");
  114.     ot("  tst r1,#0x80\n");
  115.     ot("  bne CycloneDoTraceWithChecks\n");
  116.     ot("  cmp r5,#0\n");
  117.   }
  118.   if (opend_check_interrupt)
  119.   {
  120.     ot("  blt CycloneEnd\n");
  121.     ot(";@ CheckInterrupt:\n");
  122.     if (!opend_check_trace)
  123.       ot("  ldr r1,[r7,#0x44]\n");
  124.     ot("  movs r0,r1,lsr #24 ;@ Get IRQ level\n"); // same as  ldrb r0,[r7,#0x47]
  125.     ot("  ldreq pc,[r6,r8,asl #2] ;@ Jump to next opcode handler\n");
  126.     ot("  cmp r0,#6 ;@ irq>6 ?\n");
  127.     ot("  andle r1,r1,#7 ;@ Get interrupt mask\n");
  128.     ot("  cmple r0,r1 ;@ irq<=6: Is irq<=mask ?\n");
  129.     ot("  ldrle pc,[r6,r8,asl #2] ;@ Jump to next opcode handler\n");
  130.     ot("  b CycloneDoInterruptGoBack\n");
  131.   }
  132.   else
  133.   {
  134.     ot("  ldrge pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
  135.     ot("  b CycloneEnd\n");
  136.   }
  137.   ot("\n");
  138. }
  139.  
  140. int OpBase(int op,int size,int sepa)
  141. {
  142.   int ea=op&0x3f; // Get Effective Address
  143.   if (ea<0x10) return sepa?(op&~0x7):(op&~0xf); // Use 1 handler for d0-d7 and a0-a7
  144.   if (size==0&&(ea==0x1f || ea==0x27)) return op; // Specific handler for (a7)+ and -(a7)
  145.   if (ea<0x38) return op&~7;   // Use 1 handler for (a0)-(a7), etc...
  146.   return op;
  147. }
  148.  
  149. // Get flags, trashes r2
  150. int OpGetFlags(int subtract,int xbit,int specialz)
  151. {
  152.   if (specialz) ot("  orr r2,r10,#0xb0000000 ;@ for old Z\n");
  153.  
  154.   ot("  mrs r10,cpsr ;@ r10=flags\n");
  155.  
  156.   if (specialz) ot("  andeq r10,r10,r2 ;@ fix Z\n");
  157.  
  158.   if (subtract) ot("  eor r10,r10,#0x20000000 ;@ Invert carry\n");
  159.  
  160.   if (xbit)
  161.   {
  162.     ot("  str r10,[r7,#0x4c] ;@ Save X bit\n");
  163.   }
  164.   return 0;
  165. }
  166.  
  167. // -----------------------------------------------------------------
  168.  
  169. int g_op;
  170.  
  171. void OpAny(int op)
  172. {
  173.   memset(OpData,0x33,sizeof(OpData));
  174.   OpData[0]=(unsigned char)(op>>8);
  175.   OpData[1]=(unsigned char)op;
  176.   g_op=op;
  177.  
  178.   if ((op&0xf100)==0x0000) OpArith(op);    // +
  179.   if ((op&0xc000)==0x0000) OpMove(op);     // +
  180.   if ((op&0xf5bf)==0x003c) OpArithSr(op);  // + Ori/Andi/Eori $nnnn,sr
  181.   if ((op&0xf100)==0x0100) OpBtstReg(op);  // +
  182.   if ((op&0xf138)==0x0108) OpMovep(op);    // +
  183.   if ((op&0xff00)==0x0800) OpBtstImm(op);  // +
  184.   if ((op&0xf900)==0x4000) OpNeg(op);      // +
  185.   if ((op&0xf140)==0x4100) OpChk(op);      // +
  186.   if ((op&0xf1c0)==0x41c0) OpLea(op);      // +
  187.   if ((op&0xf9c0)==0x40c0) OpMoveSr(op);   // +
  188.   if ((op&0xffc0)==0x4800) OpNbcd(op);     // +
  189.   if ((op&0xfff8)==0x4840) OpSwap(op);     // +
  190.   if ((op&0xffc0)==0x4840) OpPea(op);      // +
  191.   if ((op&0xffb8)==0x4880) OpExt(op);      // +
  192.   if ((op&0xfb80)==0x4880) OpMovem(op);    // +
  193.   if ((op&0xff00)==0x4a00) OpTst(op);      // +
  194.   if ((op&0xffc0)==0x4ac0) OpTas(op);      // +
  195.   if ((op&0xfff0)==0x4e40) OpTrap(op);     // +
  196.   if ((op&0xfff8)==0x4e50) OpLink(op);     // +
  197.   if ((op&0xfff8)==0x4e58) OpUnlk(op);     // +
  198.   if ((op&0xfff0)==0x4e60) OpMoveUsp(op);  // +
  199.   if ((op&0xfff8)==0x4e70) Op4E70(op);     // + Reset/Rts etc
  200.   if ((op&0xfffd)==0x4e70) OpStopReset(op);// +
  201.   if ((op&0xff80)==0x4e80) OpJsr(op);      // +
  202.   if ((op&0xf000)==0x5000) OpAddq(op);     // +
  203.   if ((op&0xf0c0)==0x50c0) OpSet(op);      // +
  204.   if ((op&0xf0f8)==0x50c8) OpDbra(op);     // +
  205.   if ((op&0xf000)==0x6000) OpBranch(op);   // +
  206.   if ((op&0xf100)==0x7000) OpMoveq(op);    // +
  207.   if ((op&0xa000)==0x8000) OpArithReg(op); // + Or/Sub/And/Add
  208.   if ((op&0xb1f0)==0x8100) OpAbcd(op);     // +
  209.   if ((op&0xb0c0)==0x80c0) OpMul(op);      // +
  210.   if ((op&0x90c0)==0x90c0) OpAritha(op);   // +
  211.   if ((op&0xb130)==0x9100) OpAddx(op);     // +
  212.   if ((op&0xf000)==0xb000) OpCmpEor(op);   // +
  213.   if ((op&0xf138)==0xb108) OpCmpm(op);     // +
  214.   if ((op&0xf130)==0xc100) OpExg(op);      // +
  215.   if ((op&0xf000)==0xe000) OpAsr(op);      // + Asr/l/Ror/l etc
  216.   if ((op&0xf8c0)==0xe0c0) OpAsrEa(op);    // +
  217.  
  218.   if (op==0xffff)
  219.   {
  220.     SuperEnd();
  221.   }
  222. }
  223.  
  224.