
#include "app.h"

//#define DISPLAY_DISASSEMBLY

static unsigned char OpData[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

static unsigned short CPU_CALL OpRead16(unsigned int a)
{
  return (unsigned short)( (OpData[a&15]<<8) | OpData[(a+1)&15] );
}

// For opcode 'op' use handler 'use'
void OpUse(int op,int use)
{
  char text[64]="";
  CyJump[op]=use;

  if (op!=use) return;

  // Disassemble opcode
  DisaPc=0;
  DisaText=text;
  DisaWord=OpRead16;

  DisaGet();
  ot(";@ ---------- [%.4x] %s uses Op%.4x ----------\n",op,text,use);
}

void OpFirst()
{
#ifdef DIRECT_JUMP
//  ot("  cmp r4,#MaxROMSize\n");
  ot("  ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
//  ot("  ldrlo pc,[r6,r4,asl #1] ;@ Jump to opcode handler\n");
//  ot("  ldr pc,[r6,r4,asl #1]\n");
  ot("  b CycloneNotROM\n");
#else
  if (use_hwords)
    ot("  ldrh r8,[r4],#2 ;@ Fetch first opcode\n");
  else
  {
    ot("  ldr r8,[r4],#2\n ;@ Fetch first opcode\n");
    ot("  bic r8,r8,#&ff000000\n");
    ot("  bic r8,r8,#&00ff0000\n");
  }
  ot("  ldr pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
#endif
}

void OpStart(int op)
{
  Cycles=0;
  OpUse(op,op); // This opcode obviously uses this handler
  ot("Op%.4x%s\n", op, ms?"":":");
#ifdef DISPLAY_OPCODE
  char sop[8];
  sprintf(sop,"%04x",op);
  ot("  swi 0x100+%d ;@ debug!\n",sop[0]);
  ot("  swi 0x100+%d ;@ debug!\n",sop[1]);
  ot("  swi 0x100+%d ;@ debug!\n",sop[2]);
  ot("  swi 0x100+%d ;@ debug!\n",sop[3]);
#endif
#ifdef DISPLAY_DISASSEMBLY
  char text[64];
  sprintf(text,"[%04x] ",op);
  DisaPc=0;
  DisaText = text+strlen(text);
  DisaWord = OpRead16;
  DisaGet();
  ot("  swi 0x01\n");
  ot("  dcb \"%s\",13,10,0\n",text);
  ot("  align\n");
#endif
}

void OpEnd()
{
#ifdef DIRECT_JUMP
  ot("  subs r5,r5,#%d ;@ Subtract cycles\n",Cycles);
  ot("  blt CycloneEndNoBack\n");
//  ot("  cmp r4,#MaxROMSize\n");
  ot("  ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
//  ot("  ldrlo pc,[r6,r4,asl #1] ;@ Jump to opcode handler\n");
#if 0
  ot("  ldr pc,[r6,r4,asl #1] ;@ Jump to opcode handler\n");
#else
  ot("  ldr r1,=ROMOpTab\n");
  ot("  add r0,r6,r4,asl #1\n");
  ot("  sub r0,r0,r1\n");
  ot("  cmp r0,#&400000*2\n");
  ot("  ldrlo pc,[r6,r4,asl #1]\n");
  ot("  b CycloneNotROM\n");
#endif

//  ot("  b CycloneNotROM\n");
#else
  if (use_hwords)
  {
    ot("  ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
    ot("  subs r5,r5,#%d ;@ Subtract cycles\n",Cycles);
    ot("  blt CycloneEnd\n");
  }
  else
  {
    ot("  ldr r8,[r4],#2\n ;@ Fetch next opcode\n");
    ot("  subs r5,r5,#%d ;@ Subtract cycles\n",Cycles);
    ot("  bic r8,r8,#&ff000000\n");
    ot("  bic r8,r8,#&00ff0000\n");
    ot("  blt CycloneEnd\n");
  }
  ot("  ldr pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
#endif
  ltorg();   //??? this could be BAD NEWS!
  ot("\n");
}

int OpBase(int op)
{
  int ea=op&0x3f; // Get Effective Address
  if (ea<0x10) return op&~0xf; // Use 1 handler for d0-d7 and a0-a7
  if (ea>=0x18 && ea<0x28 && (ea&7)==7) return op; // Specific handler for (a7)+ and -(a7)
  if (ea<0x38) return op&~7;   // Use 1 handler for (a0)-(a7), etc...
  return op;
}

// Get flags, trashes r2
int OpGetFlags(int subtract,int xbit)
{
  ot("  mrs r9,cpsr ;@ r9=flags\n");

  if (subtract) ot("  eor r9,r9,#0x20000000 ;@ Invert carry\n");

  if (Accu&1) if (xbit)
  {
    ot("  mov r2,r9,lsr #28\n");
    ot("  strb r2,[r7,#0x45] ;@ Save X bit\n");
  }
  return 0;
}

// -----------------------------------------------------------------

void OpAny(int op)
{
  memset(OpData,0x33,sizeof(OpData));
  OpData[0]=(unsigned char)(op>>8);
  OpData[1]=(unsigned char)op;

  if ((op&0xf100)==0x0000) OpArith(op);
  if ((op&0xc000)==0x0000) OpMove(op);
  if ((op&0xf5bf)==0x003c) OpArithSr(op); // Ori/Andi/Eori $nnnn,sr
  if ((op&0xf100)==0x0100) OpBtstReg(op);
  if ((op&0xf138)==0x0108) OpMovep(op);
  if ((op&0xff00)==0x0800) OpBtstImm(op);
  if ((op&0xf900)==0x4000) OpNeg(op);
  if ((op&0xf1c0)==0x41c0) OpLea(op);
  if ((op&0xf9c0)==0x40c0) OpMoveSr(op);
  if ((op&0xfff8)==0x4840) OpSwap(op);
  if ((op&0xffc0)==0x4840) OpPea(op);
  if ((op&0xffb8)==0x4880) OpExt(op);
  if ((op&0xfb80)==0x4880) OpMovem(op);
  if ((op&0xff00)==0x4a00) OpTst(op);
  if ((op&0xfff0)==0x4e40) OpTrap(op);
  if ((op&0xfff8)==0x4e50) OpLink(op);
  if ((op&0xfff8)==0x4e58) OpUnlk(op);
  if ((op&0xfff0)==0x4e60) OpMoveUsp(op);
  if ((op&0xfff8)==0x4e70) Op4E70(op); // Reset/Rts etc
  if ((op&0xff80)==0x4e80) OpJsr(op);
  if ((op&0xf000)==0x5000) OpAddq(op);
  if ((op&0xf0c0)==0x50c0) OpSet(op);
  if ((op&0xf0f8)==0x50c8) OpDbra(op);
  if ((op&0xf000)==0x6000) OpBranch(op);
  if ((op&0xf100)==0x7000) OpMoveq(op);
  if ((op&0xa000)==0x8000) OpArithReg(op); // Or/Sub/And/Add
  if ((op&0xb1f0)==0x8100) OpAbcd(op);
  if ((op&0xb0c0)==0x80c0) OpMul(op);
  if ((op&0x90c0)==0x90c0) OpAritha(op);
  if ((op&0xb138)==0x9100) OpAddx(op);
  if ((op&0xf000)==0xb000) OpCmpEor(op);
  if ((op&0xf138)==0xb108) OpCmpm(op);
  if ((op&0xf130)==0xc100) OpExg(op);
  if ((op&0xf000)==0xe000) OpAsr(op); // Asr/l/Ror/l etc
  if ((op&0xf8c0)==0xe0c0) OpAsrEa(op);

}
