/** VGB: portable GameBoy emulator ***************************/
/**                                                         **/
/**                           GB.h                          **/
/**                                                         **/
/** This file contains definitions for the GameBoy hardware **/
/** emulation.                                              **/
/**                                                         **/
/** Copyright (C) Marat Fayzullin 1995                      **/
/**     You are not allowed to distribute this software     **/
/**     commercially. Please, notify me, if you make any    **/   
/**     changes to this file.                               **/
/*************************************************************/


/* Amiga includes */
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/asl.h>
#include <dos/rdargs.h>
#include <exec/memory.h>
#include <devices/audio.h>

#include "Z80.h"

#define NORAM     0xFF
#define MAXCHEAT  256
 
#define VBL_IFLAG 0x01
#define LCD_IFLAG 0x02
#define TIM_IFLAG 0x04
#define SIO_IFLAG 0x08
#define JOY_IFLAG 0x10
 
#define JOYPAD  RAM[0xFF00] /* Joystick: 1.1.P15.P14.P13.P12.P11.P10      */
#define SIODATA RAM[0xFF01] /* Serial IO data buffer                      */
#define SIOCONT RAM[0xFF02] /* Serial IO control register                 */
#define DIVREG  RAM[0xFF04] /* Divider register (???)                     */
#define TIMECNT RAM[0xFF05] /* Timer counter. Gen. int. when it overflows */
#define TIMEMOD RAM[0xFF06] /* New value of TimeCount after it overflows  */
#define TIMEFRQ RAM[0xFF07] /* Timer frequency and start/stop switch      */
#define IFLAGS  RAM[0xFF0F] /* Interrupt flags: 0.0.0.JST.SIO.TIM.LCD.VBL */
#define ISWITCH RAM[0xFFFF] /* Switches to enable/disable interrupts      */
#define LCDCONT RAM[0xFF40] /* LCD control register                       */
#define LCDSTAT RAM[0xFF41] /* LCD status register                        */
#define SCROLLY RAM[0xFF42] /* Starting Y position of the background      */
#define SCROLLX RAM[0xFF43] /* Starting X position of the background      */
#define CURLINE RAM[0xFF44] /* Current screen line being scanned          */
#define CMPLINE RAM[0xFF45] /* Gen. int. when scan reaches this line      */
#define BGRDPAL RAM[0xFF47] /* Background palette                         */
#define SPR0PAL RAM[0xFF48] /* Sprite palette #0                          */
#define SPR1PAL RAM[0xFF49] /* Sprite palette #1                          */
#define WNDPOSY RAM[0xFF4A] /* Window Y position                          */
#define WNDPOSX RAM[0xFF4B] /* Window X position                          */
#define SNDREG10 RAM[0xFF10]
#define SNDREG11 RAM[0xFF11]
#define SNDREG12 RAM[0xFF12]
#define SNDREG13 RAM[0xFF13]
#define SNDREG14 RAM[0xFF14]
#define SNDREG21 RAM[0xFF16]
#define SNDREG22 RAM[0xFF17]
#define SNDREG23 RAM[0xFF18]
#define SNDREG24 RAM[0xFF19]
#define SNDREG30 RAM[0xFF1A]
#define SNDREG31 RAM[0xFF1B]
#define SNDREG32 RAM[0xFF1C]
#define SNDREG33 RAM[0xFF1D]
#define SNDREG34 RAM[0xFF1E]
#define SNDREG41 RAM[0xFF20]
#define SNDREG42 RAM[0xFF21]
#define SNDREG43 RAM[0xFF22]
#define SNDREG44 RAM[0xFF23]
#define SNDREG50 RAM[0xFF24]
#define SNDREG51 RAM[0xFF25]
#define SNDREG52 RAM[0xFF26]

extern byte Verbose;          /* Verboseness level                          */
 
extern byte *RAM;             /* Pointer to Z80 address space (64kB)        */
 
extern int  UPeriod;          /* Number of VBlanks per screen update        */
extern int  VPeriod;          /* Number of Z80 cycles between VBlanks       */

extern byte LineDelay;        /* When 1, CMPLINE interrupts are delayed     */

extern int  ROMBanks;         /* Total number of ROM banks                  */ 
extern int  RAMBanks;         /* Total number of RAM banks                  */
 
extern byte BPal[4];          /* Background palette                         */
extern byte SPal0[4],SPal1[4];/* Sprite palettes                            */

extern byte *ChrGen;          /* Character generator                        */
extern byte *BgdTab,*WndTab;  /* Background and window character tables     */


void M_WRMEM(word A,byte V);


/****************************************************************/
/*** Initialize and start GameBoy emulation. This function    ***/
/*** returns 0 in the case of failure.                        ***/
/****************************************************************/
int StartGB(char *CartName);

/****************************************************************/
/*** Free resources allocated by StartGB().                   ***/
/****************************************************************/
void TrashGB(void);

/****************************************************************/
/*** Add a cheat to the cheat list [call before StartGB()].   ***/
/****************************************************************/
int AddCheat(char *Cheat);

/****************************************************************/
/*** Allocate resources needed by the machine-dependent code. ***/
/************************************** TO BE WRITTEN BY USER ***/
int InitMachine(void);
 
/****************************************************************/
/*** Deallocate all resources taken by InitMachine().         ***/
/************************************** TO BE WRITTEN BY USER ***/
void TrashMachine(void);

/****************************************************************/
/*** Refresh screen.                                          ***/
/************************************** TO BE WRITTEN BY USER ***/
void RefreshScreen(void);

/****************************************************************/
/*** Refresh line Y [0-143].                                  ***/
/************************************** TO BE WRITTEN BY USER ***/
void RefreshLine(byte Y);

/****************************************************************/
/*** Refresh sprites.                                         ***/
/************************************** TO BE WRITTEN BY USER ***/
void RefreshSprites(void);

/****************************************************************/
/*** Get joystick state: START.SELECT.B.A.D.U.L.R.            ***/
/************************************** TO BE WRITTEN BY USER ***/
byte Joystick(void);

/****************************************************************/
/*** Amiga specific functions and defines                     ***/
/************************************** TO BE WRITTEN BY USER ***/
STRPTR ReqFile(void);
void __inline AllocatePens(void);
extern void __asm StartDisplay(register __d0 byte Y);
BOOL __inline InitSound(void);
void __inline FreeSound(void);
struct IOAudio *NewIOBlock(UBYTE*);
void FreeIOBlock(struct IOAudio*);
void __inline PlaySound(void);
void __inline PlaySample(UBYTE*, UWORD, UWORD, UBYTE, UBYTE);

#define OPT_VERBOSE ((ULONG*)(ArgPtrs[0]))
#define OPT_VPER ((ULONG*)(ArgPtrs[1]))
#define OPT_UPER ((ULONG*)(ArgPtrs[2]))
#define OPT_TRAP ((STRPTR)(ArgPtrs[3]))
#define OPT_HELP ((BOOL)(ArgPtrs[4]))
#define OPT_SCALEUP ((BOOL)(ArgPtrs[5]))
