#include <stdio.h>
#include <stdlib.h>
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>
#include <SDL.h>
#include "emu.h"
#include "sound.h"
#include "memory.h"
#include "interrupt.h"
#include "cpu.h"
#include "sgb.h"
#include "rom.h"
#include "cpu.h"
#include "serial.h"
#include "video_std.h"
#include "video_yuv.h"
#include "menu.h"

// GB_CPU *gbcpu;

GNGB_CONF conf;
SDL_Joystick *sdl_joy;
Uint16 key[SDLK_LAST];
// Sint16 *joy_axis;
// Uint8 *joy_but;
// Uint8 jmap[8];
// Uint16 kmap[8];
// GNGB_MOVIE gngb_movie;

// Uint8 rom_mask;
// Uint16 nb_rom_page;
// Uint16 nb_ram_page;
// Uint16 nb_vram_page;
// Uint16 nb_wram_page;
// Uint16 active_rom_page;
// Uint16 active_ram_page;
// Uint16 active_vram_page;
// Uint16 active_wram_page;

Uint32 sample_rate;
Uint8  bit_per_sample;
Uint16 sample_per_update;
SoundM1 snd_m1;
SoundM2 snd_m2;
SoundM3 snd_m3;
SoundM4 snd_m4;
SoundG snd_g;

// Uint8 **rom_page;       // 0000 - 4000 bank #0 4000 - 8000 bank #n
// Uint8 **ram_page;       // a000 - c000
// Uint8 **vram_page;      // 8000 - a000
// Uint8 **wram_page;      // c000 - fe00
// Uint8 oam_space[0xa0];  // fe00 - fea0
// Uint8 himem[0x160];     // fea0 - ffff

// Uint8 mbc1_mem_mode;
// Uint8 mbc1_line;

//Uint8 mbc5_lower;
//Uint8 mbc5_upper;

//Uint8 ram_mask;

//MEM_READ_ENTRY mem_read_tab[0x10];
//MEM_WRITE_ENTRY mem_write_tab[0x10];
//DMA_INFO dma_info;


char *rom_name;
//Sint16 rom_type;
ROM_TIMER *rom_timer;


struct stgbserial gbserial;
Sint16 serial_cycle_todo;
Sint8 gblisten;

SGB sgb;
Uint16 sgb_pal[4][4];
Uint8 sgb_pal_map[20][18];
Uint8 sgb_mask;
//SDL_Surface *sgb_buf;

// Uint32 nb_cycle;
//Uint32 key_cycle;
GBLCDC *gblcdc;
Uint8 vram_pal_line_temp[160][4];
//Uint8 vram_init_pal;
GBTIMER *gbtimer;

//Uint8 back_col[170][170];
//struct mask_shift tab_ms[8];
Uint32 video_flag;
int scxoff,scyoff;  /* shift of the screen */
//Uint16 grey[4];
//Uint8 pal_bck[4];
//Uint8 pal_obj[2][4];
//Uint16 pal_col_bck_gb[8][4]; // 0BBBBBGGGGGRRRRR
//Uint16 pal_col_obj_gb[8][4];
//Uint16 pal_col_bck[8][4];    // RRRRRGGGGG1BBBBB avec filtre
//Uint16 pal_col_obj[8][4];
// Uint16 Filter[32768];
// SDL_Surface *gb_screen;
// SDL_Surface *back_save;
// VIDEO_MODE *cur_mode;
GB_SPRITE gb_spr[40];
// Uint8 nb_spr;
Uint8 rb_on;

// Sint8 rb_tab[2][RB_SIZE];
// SDL_Surface *back;
SDL_Overlay *overlay;
SDL_Rect ov_rect;
Uint32 yuv_flag;

int wl,hl,xm,ym;
// SDL_Surface *fontbuf;

// MENU main_menu;




Uint8 rb_shift;
SDL_Rect dstR;
SDL_Rect scrR;


MENU *current_menu;

struct yuv rgb2yuv[65536];
