/** VGB: portable GameBoy emulator ***************************/
/**                                                         **/
/**                           Z80.c                         **/
/**                                                         **/
/** This file contains implementation for the GameBoy CPU.  **/
/** See Z80.h for the relevant definitions. Please, note    **/
/** that this code can not be used to emulate a generic Z80 **/
/** because the GameBoy version of it differs from Z80 in   **/
/** many ways.                                              **/
/**                                                         **/
/** Copyright (C) Marat Fayzullin 1994,1995,1996            **/
/**     You are not allowed to distribute this software     **/
/**     commercially. Please, notify me, if you make any    **/
/**     changes to this file.                               **/
/*************************************************************/

#include <stdio.h>
#include "GB.h"
#include "Tables.h"

/*** Registers ***********************************************/
/*** Z80 registers and running flag.                       ***/
/*************************************************************/
static reg R;
byte CPURunning;

/*** Interrupts **********************************************/
/*** Interrupt-related variables.                          ***/
/*************************************************************/
int  IPeriod = 69905; /* Number of cycles between int. int-s */
byte IntSync = 1;     /* 1 to generate internal interrupts   */
byte IFlag = 0;       /* If IFlag==1, gen. int. and set to 0 */
int  ICount;          /* Variable used to count CPU cycles   */

/*** Trace and Trap ******************************************/
/*** Switches to turn tracing on and off in DEBUG mode.    ***/
/*************************************************************/
#ifdef DEBUG
byte Trace=0;       /* Tracing is on if Trace==1  */
word Trap=0xFFFF;   /* When PC==Trap, set Trace=1 */
#endif

/*** TrapBadOps **********************************************/
/*** When 1, print warnings of illegal Z80 instructions.   ***/
/*************************************************************/
byte TrapBadOps=0;


#define S(Fl)        R.AF.B.l|=Fl
#define R(Fl)        R.AF.B.l&=~(Fl)
#define FLAGS(Rg,Fl) R.AF.B.l=Fl|ZSTable[Rg];

#define M_RLC(Rg)      \
  R.AF.B.l=Rg>>7;Rg=(Rg<<1)|R.AF.B.l;R.AF.B.l|=ZSTable[Rg]
#define M_RRC(Rg)      \
  R.AF.B.l=Rg&0x01;Rg=(Rg>>1)|(R.AF.B.l<<7);R.AF.B.l|=ZSTable[Rg]

#define M_RL(Rg)       \
  if(Rg&0x80)          \
  {                    \
    Rg=(Rg<<1)|(R.AF.B.l&C_FLAG); \
    R.AF.B.l=ZSTable[Rg]|C_FLAG;  \
  }                    \
  else                 \
  {                    \
    Rg=(Rg<<1)|(R.AF.B.l&C_FLAG); \
    R.AF.B.l=ZSTable[Rg];         \
  }
#define M_RR(Rg)       \
  if(Rg&0x01)          \
  {                    \
    Rg=(Rg>>1)|(R.AF.B.l<<7);    \
    R.AF.B.l=ZSTable[Rg]|C_FLAG; \
  }                    \
  else                 \
  {                    \
    Rg=(Rg>>1)|(R.AF.B.l<<7);    \
    R.AF.B.l=ZSTable[Rg];        \
  }
  
#define M_SLA(Rg)      \
  R.AF.B.l=Rg>>7;Rg<<=1;R.AF.B.l|=ZSTable[Rg]
#define M_SRA(Rg)      \
  R.AF.B.l=Rg&C_FLAG;Rg=(Rg>>1)|(Rg&0x80);R.AF.B.l|=ZSTable[Rg]
#define M_SLL(Rg)      \
  R.AF.B.l=Rg>>7;Rg=(Rg<<1)|0x01;R.AF.B.l|=ZSTable[Rg]
#define M_SRL(Rg)      \
  R.AF.B.l=Rg&0x01;Rg>>=1;R.AF.B.l|=ZSTable[Rg]

#define M_BIT(Bit,Rg)  \
  R.AF.B.l=(R.AF.B.l&~(N_FLAG|Z_FLAG))|H_FLAG|(Rg&(1<<Bit)? 0:Z_FLAG)

#define M_SET(Bit,Rg) Rg|=1<<Bit
#define M_RES(Bit,Rg) Rg&=~(1<<Bit)

#define M_POP(Rg)      \
  R.Rg.B.l=M_RDMEM(R.SP.W++);R.Rg.B.h=M_RDMEM(R.SP.W++);
#define M_PUSH(Rg)     \
  M_WRMEM(--R.SP.W,R.Rg.B.h);M_WRMEM(--R.SP.W,R.Rg.B.l)

#define M_CALL         \
  J.B.l=M_RDMEM(R.PC.W++);J.B.h=M_RDMEM(R.PC.W++);       \
  M_WRMEM(--R.SP.W,R.PC.B.h);M_WRMEM(--R.SP.W,R.PC.B.l); \
  R.PC.W=J.W;ICount-=28

#define M_JP           \
  J.B.l=M_RDMEM(R.PC.W++);J.B.h=M_RDMEM(R.PC.W);R.PC.W=J.W;ICount-=4
#define M_JR           \
  R.PC.W+=(offset)M_RDMEM(R.PC.W)+1;ICount-=4
#define M_RET          \
  R.PC.B.l=M_RDMEM(R.SP.W++);R.PC.B.h=M_RDMEM(R.SP.W++);ICount-=8

#define M_RST(Ad)      \
  M_WRMEM(--R.SP.W,R.PC.B.h);M_WRMEM(--R.SP.W,R.PC.B.l);R.PC.W=Ad

#define M_LDWORD(Rg)   \
  R.Rg.B.l=M_RDMEM(R.PC.W++);R.Rg.B.h=M_RDMEM(R.PC.W++)

#define M_ADD(Rg)      \
  J.W=R.AF.B.h+Rg;     \
  R.AF.B.l=J.B.h|ZSTable[J.B.l]|((R.AF.B.h^Rg^J.B.l)&H_FLAG); \
  R.AF.B.h=J.B.l       

#define M_SUB(Rg)      \
  J.W=R.AF.B.h-Rg;     \
  R.AF.B.l=N_FLAG|-J.B.h|ZSTable[J.B.l]|((R.AF.B.h^Rg^J.B.l)&H_FLAG); \
  R.AF.B.h=J.B.l

#define M_ADC(Rg)      \
  J.W=R.AF.B.h+Rg+(R.AF.B.l&C_FLAG); \
  R.AF.B.l=J.B.h|ZSTable[J.B.l]|((R.AF.B.h^Rg^J.B.l)&H_FLAG); \
  R.AF.B.h=J.B.l

#define M_SBC(Rg)      \
  J.W=R.AF.B.h-Rg-(R.AF.B.l&C_FLAG); \
  R.AF.B.l=N_FLAG|-J.B.h|ZSTable[J.B.l]|((R.AF.B.h^Rg^J.B.l)&H_FLAG); \
  R.AF.B.h=J.B.l

#define M_CP(Rg)       \
  J.W=R.AF.B.h-Rg;     \
  R.AF.B.l=N_FLAG|-J.B.h|ZSTable[J.B.l]|((R.AF.B.h^Rg^J.B.l)&H_FLAG)

#define M_AND(Rg) R.AF.B.h&=Rg;R.AF.B.l=H_FLAG|ZSTable[R.AF.B.h]
#define M_OR(Rg)  R.AF.B.h|=Rg;R.AF.B.l=ZSTable[R.AF.B.h]
#define M_XOR(Rg) R.AF.B.h^=Rg;R.AF.B.l=ZSTable[R.AF.B.h]
#define M_IN(Rg)  Rg=DoIn(R.BC.B.l);R.AF.B.l=(R.AF.B.l&C_FLAG)|ZSTable[Rg]

#define M_INC(Rg)       \
  Rg++;                 \
  R.AF.B.l=(R.AF.B.l&C_FLAG)|ZSTable[Rg]|(Rg&0x0F? 0:H_FLAG)

#define M_DEC(Rg)       \
  Rg--;                 \
  R.AF.B.l=N_FLAG|(R.AF.B.l&C_FLAG)|ZSTable[Rg]|((Rg&0x0F)==0x0F? H_FLAG:0)

#define M_ADDW(Rg1,Rg2) \
  J.W=(R.Rg1.W+R.Rg2.W)&0xFFFF;                        \
  R.AF.B.l=                                            \
    (R.AF.B.l&~(H_FLAG|N_FLAG|C_FLAG))|                \
    ((R.Rg1.W^R.Rg2.W^J.W)&0x1000? H_FLAG:0)|          \
    (((long)R.Rg1.W+(long)R.Rg2.W)&0x10000? C_FLAG:0); \
  R.Rg1.W=J.W

#define M_ADCW(Rg)      \
  I=R.AF.B.l&C_FLAG;J.W=(R.HL.W+R.Rg.W+I)&0xFFFF;            \
  R.AF.B.l=                                                  \
    (((long)R.HL.W+(long)R.Rg.W+(long)I)&0x10000? C_FLAG:0)| \
    (~(R.HL.W^R.Rg.W)&(R.Rg.W^J.W)&0x8000? V_FLAG:0)|        \
    ((R.HL.W^R.Rg.W^J.W)&0x1000? H_FLAG:0)|                  \
    (J.W? 0:Z_FLAG)|(J.B.h&S_FLAG);                          \
  R.HL.W=J.W

#define M_SBCW(Rg)      \
  I=R.AF.B.l&C_FLAG;J.W=(R.HL.W-R.Rg.W-I)&0xFFFF;            \
  R.AF.B.l=                                                  \
    N_FLAG|                                                  \
    (((long)R.HL.W-(long)R.Rg.W-(long)I)&0x10000? C_FLAG:0)| \
    ((R.HL.W^R.Rg.W)&(R.HL.W^J.W)&0x8000? V_FLAG:0)|         \
    ((R.HL.W^R.Rg.W^J.W)&0x1000? H_FLAG:0)|                  \
    (J.W? 0:Z_FLAG)|(J.B.h&S_FLAG);                          \
  R.HL.W=J.W


enum Codes
{
  NOP,L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,  
  SET0_B,SET0_C,SET0_D,SET0_E,SET0_H,SET0_L,SET0_xHL,SET0_A,
  SET1_B,SET1_C,SET1_D,SET1_E,SET1_H,SET1_L,SET1_xHL,SET1_A,
  SET2_B,SET2_C,SET2_D,SET2_E,SET2_H,SET2_L,SET2_xHL,SET2_A,
  SET3_B,SET3_C,SET3_D,SET3_E,SET3_H,SET3_L,SET3_xHL,SET3_A,
  SET4_B,SET4_C,SET4_D,SET4_E,SET4_H,SET4_L,SET4_xHL,SET4_A,
  SET5_B,SET5_C,SET5_D,SET5_E,SET5_H,SET5_L,SET5_xHL,SET5_A,
  SET6_B,SET6_C,SET6_D,SET6_E,SET6_H,SET6_L,SET6_xHL,SET6_A,
  SET7_B,SET7_C,SET7_D,SET7_E,SET7_H,SET7_L,SET7_xHL,SET7_A
};


void CodesCB(void)
{
  static byte CyclesCB[256] =
  {
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,
     8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8
  };

  register byte I;

  I=M_RDMEM(R.PC.W++);
  ICount-=CyclesCB[I];
  switch(I)
  {
#include "CodesCB.h"
    default:
      ICount-=4;
      if(TrapBadOps)
        printf
        (   
          "Unrecognized instruction: CB %X at PC=%hX\n",
          M_RDMEM(R.PC.W-1),R.PC.W-2
        );
  }
}


/*** Reset Z80 registers: *********************************/
/*** This function can be used to reset the register    ***/
/*** file before starting execution with Z80(). It sets ***/
/*** the registers to their initial values.             ***/
/**********************************************************/
void ResetZ80(reg *Regs)
{
  Regs->PC.W=0x0100;Regs->SP.W=0xF000;
  Regs->AF.W=Regs->BC.W=Regs->DE.W=Regs->HL.W=0x0000;
  Regs->I=0x00;Regs->IFF=0x00;
}

/*** Interpret Z80 code: **********************************/
/*** Registers have initial values from Regs. PC value  ***/
/*** at which emulation stopped is returned by this     ***/
/*** function.                                          ***/
/**********************************************************/
word Z80(reg Regs)
{
  static byte Cycles[256] =
  {
     4,12, 8, 8, 4, 4, 8, 4,20, 8, 8, 8, 4, 4, 8, 4,
     4,12, 8, 8, 4, 4, 8, 4, 8, 8, 8, 8, 4, 4, 8, 4,
     8,12, 8, 8, 4, 4, 8, 4, 8, 8, 8, 8, 4, 4, 8, 4,
     8,12, 8, 8,12,12,12, 4, 8, 8, 8, 8, 4, 4, 8, 4,
     4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,
     4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,
     4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,
     8, 8, 8, 8, 8, 8, 4, 8, 4, 4, 4, 4, 4, 4, 8, 4,
     4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,
     4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,
     4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,
     4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,
     8,12,12,12,12,16, 8,32, 8, 8,12, 0,12,12, 8,32,
     8,12,12, 0,12,16, 8,32, 8, 8,12, 0,12, 0, 8,32,
    12,12, 8, 0, 0,16, 8,32,16, 4,16, 0, 0, 0, 8,32,
    12,12, 8, 4, 0,16, 8,32,12, 8,16, 4, 0, 0, 8,32
  };

  register byte I;
  register pair J;

  R=Regs;ICount=IPeriod;CPURunning=1;

  for(;;)
  {
#ifdef DEBUG
    if(R.PC.W==Trap) Trace=1;  /*** Turn tracing on if trapped ***/
    if(Trace) Debug(&R);       /*** Call single-step debugger  ***/
#endif

    I=M_RDMEM(R.PC.W++);
    ICount-=Cycles[I];
    switch(I)
    {
#include "Codes.h"
      case PFX_CB:
        CodesCB();break;
      case 0x10:
      case HALT:
        R.PC.W--;R.IFF|=0x80;break;
      default:
        ICount-=4;
        if(TrapBadOps)
          printf
          (
            "Unrecognized instruction: %X at PC=%hX\n",
            M_RDMEM(R.PC.W-1),R.PC.W-1
          );
    }
 
    if(ICount<=0) 
    {
      ICount+=IPeriod;
      if(!CPURunning) break;
      if(IntSync) IFlag=1;
    }

    if(IFlag)
    {
      IFlag=0;J.W=Interrupt();
      if((J.W!=0xFFFF)&&(R.IFF&0x81))
      {
        if(R.IFF&0x80) R.PC.W++;
        IFLAGS&=~IMask;IMask=0x00;
        R.IFF&=0x7E;
        M_PUSH(PC);R.PC.W=J.W;
      }
    }  
  }

  return(R.PC.W);
}
