/******************************************************************************
*
* Copyright (C) 2004-2017  Brian Berlin
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************
*
* FILENAME: memory.h
*
* DESCRIPTION:  This contains function declartions for memory functions
*
* AUTHORS:  bberlin
*
* CHANGE LOG
*
*  VER     DATE     CHANGED BY   DESCRIPTION OF CHANGE
* ------  --------  ----------   ------------------------------------
* 0.1     07/18/04  bberlin      Creation
******************************************************************************/
#ifndef memory_h
#define memory_h

#include "console.h"
#include "debug.h"

/*
 * Memory functions
 */
#define rd_6502(addr)    ((cpu6502.read_mem[((addr) >> 8) & 0xff])(addr))
#define rd_antic(addr)    ((antic.read_mem[((addr) >> 8) & 0xff])(addr))
#define rd_z_6502(addr)    ((cpu6502.read_mem[((addr) >> 8) & 0xff])(addr))
#define wr_6502(addr,value)    ((cpu6502.write_mem[(addr >> 8) & 0xff])(addr, value))

/*
 * Defines for memory functions
 *   Odd numbers are debug...
 */
#define FUNC_READ_RAM     0
#define FUNC_READ_ROM     2
#define FUNC_READ_STACK   4
#define FUNC_READ_OS      6
#define FUNC_READ_BASIC   8
#define FUNC_READ_SELF    10
#define FUNC_READ_MEM_IMG 12
#define FUNC_READ_XE_BANK 14
#define FUNC_READ_BB      16
#define FUNC_READ_BB_PC   18
#define FUNC_READ_64K     20
#define FUNC_READ_32K_DB  22
#define FUNC_READ_XEGS    24
#define FUNC_READ_SWXEGS  26
#define FUNC_READ_MEGA    28
#define FUNC_READ_OSS     30
#define FUNC_READ_ATMAX   32
#define FUNC_READ_WILL    34
#define FUNC_READ_SDX     36
#define FUNC_READ_BLIZ    38
#define FUNC_READ_ATRAX   40
#define FUNC_READ_OSS2    42
#define FUNC_READ_16KBE   44
#define FUNC_READ_32KBE   46
#define FUNC_READ_512_HYB_BE     48
#define FUNC_READ_512_HYB_BF     50
#define FUNC_READ_512_HYB_BANKED 52
#define FUNC_READ_ANTIC          54
#define FUNC_READ_GTIA           56
#define FUNC_READ_POKEY          58
#define FUNC_READ_PIA            60

#define FUNC_WRITE_RAM     0
#define FUNC_WRITE_ROM     2
#define FUNC_WRITE_STACK   4
#define FUNC_WRITE_OS      6
#define FUNC_WRITE_BASIC   8
#define FUNC_WRITE_SELF    10
#define FUNC_WRITE_MEM_IMG 12
#define FUNC_WRITE_XE_BANK 14
#define FUNC_WRITE_BB      16
#define FUNC_WRITE_BB_PC   18
#define FUNC_WRITE_64K     20
#define FUNC_WRITE_32K_DB  22
#define FUNC_WRITE_XEGS    24
#define FUNC_WRITE_SWXEGS  26
#define FUNC_WRITE_MEGA    28
#define FUNC_WRITE_OSS     30
#define FUNC_WRITE_ATMAX   32
#define FUNC_WRITE_WILL    34
#define FUNC_WRITE_SDX     36
#define FUNC_WRITE_BLIZ    38
#define FUNC_WRITE_ATRAX   40
#define FUNC_WRITE_OSS2    42
#define FUNC_WRITE_16KBE   44
#define FUNC_WRITE_32KBE   46
#define FUNC_WRITE_512_HYB_BF   48
#define FUNC_WRITE_512_HYB_SRAM 50
#define FUNC_WRITE_ANTIC        52
#define FUNC_WRITE_GTIA         54
#define FUNC_WRITE_POKEY        56
#define FUNC_WRITE_PIA          58

#define FUNC_MEM_MAX       62

void mem_init ( void );
void mem_get_state_values ( KBYTE *portb, KBYTE *mem_image, KBYTE *be_switch  );
void mem_set_state_values ( KBYTE portb, KBYTE mem_image, KBYTE be_switch  );
KBYTE * mem_get_bank_image ( void );
int mem_get_bank_image_size ( void );
KBYTE * mem_get_mem_image ( void );
int mem_get_mem_image_size ( void );
void mem_get_hyb_state ( int *mode, int *bank8, int *bank16 );
void mem_set_hyb_state ( int mode, int bank8, int bank16 );
KBYTE * mem_get_sd_image ( void );
int mem_get_sd_image_size ( void );
void mem_get_sd_cmd_state ( KBYTE *cmd_buf, int *count, int *phase );
void mem_set_sd_cmd_state ( const KBYTE *cmd_buf, int count, int phase );
void mem_reset ( void  );
void mem_assign_ops ( t_cpu *cpu  );
void mem_assign_ops_debug ( t_cpu *cpu, t_debug *p_debug  );
void mem_assign_portb_ops ( t_cpu *cpu,  KBYTE portb  );
KBYTE mem_get_portb_value ( void );

void memory_init_bank ( int size );
void memory_exit ( void );
void memory_write_rom_bytes ( KWORD addr, unsigned char *buffer, int size );
void memory_write_bank_bytes ( KWORD addr, unsigned char *buffer, int size );
void memory_write_os_bytes ( KWORD addr, unsigned char *buffer, int size );
void memory_write_basic_bytes ( KWORD addr, unsigned char *buffer, int size );
void mem_assign_antic_hyb_ops ( void );
void memory_clear_sd ( void );
void memory_init_sd ( int size );
void memory_write_sd_bytes ( int offset, unsigned char *buffer, int size );

KBYTE read_5200_ram ( KWORD addr );
KBYTE read_5200_bank_switched ( KWORD addr );
KBYTE read_5200_stack ( KWORD addr );
KBYTE read_5200_rom ( KWORD addr );
KBYTE read_5200_rom_bb ( KWORD addr );
KBYTE read_5200_rom_bb_pc ( KWORD addr );
KBYTE read_5200_rom_64k ( KWORD addr );
KBYTE read_5200_rom_be16k ( KWORD addr );
KBYTE read_5200_rom_be32k ( KWORD addr );
KBYTE read_5200_hyb_banked ( KWORD addr );
KBYTE read_5200_hyb_banked_debug ( KWORD addr );
KBYTE read_5200_rom_512k_hyb ( KWORD addr );
KBYTE read_cart_32k_db ( KWORD addr );
KBYTE read_cart_xegs ( KWORD addr );
KBYTE read_cart_will ( KWORD addr );
KBYTE read_cart_sdx ( KWORD addr );
KBYTE read_cart_oss ( KWORD addr );
KBYTE read_cart_oss2 ( KWORD addr );
KBYTE read_cart_blizzard ( KWORD addr );
KBYTE read_os_rom ( KWORD addr );
KBYTE read_basic_rom ( KWORD addr );
KBYTE read_self_test_rom ( KWORD addr );
KBYTE read_xe_bank_ram ( KWORD addr );
KBYTE read_mem_image_ram ( KWORD addr );

KBYTE read_5200_ram_debug ( KWORD addr );
KBYTE read_5200_rom_debug ( KWORD addr );
KBYTE read_5200_stack_debug ( KWORD addr );
KBYTE read_5200_rom_bb_debug ( KWORD addr );
KBYTE read_5200_rom_bb_pc_debug ( KWORD addr );
KBYTE read_5200_rom_64k_debug ( KWORD addr );
KBYTE read_5200_rom_be16k_debug ( KWORD addr );
KBYTE read_5200_rom_be32k_debug ( KWORD addr );
KBYTE read_5200_rom_512k_hyb_debug ( KWORD addr );
KBYTE read_cart_32k_db_debug ( KWORD addr );
KBYTE read_cart_xegs_debug ( KWORD addr );
KBYTE read_cart_will_debug ( KWORD addr );
KBYTE read_cart_sdx_debug ( KWORD addr );
KBYTE read_cart_oss_debug ( KWORD addr );
KBYTE read_cart_oss2_debug ( KWORD addr );
KBYTE read_cart_blizzard_debug ( KWORD addr );
KBYTE read_os_rom_debug ( KWORD addr );
KBYTE read_basic_rom_debug ( KWORD addr );
KBYTE read_self_test_rom_debug ( KWORD addr );
KBYTE read_xe_bank_ram_debug ( KWORD addr );
KBYTE read_mem_image_ram_debug ( KWORD addr );

void write_5200_ram ( KWORD addr, KBYTE value );
void write_5200_bank_switched ( KWORD addr, KBYTE value );
void write_os_rom ( KWORD addr, KBYTE value );
void write_basic_rom ( KWORD addr, KBYTE value );
void write_xe_bank_state ( KWORD addr, KBYTE value );
KBYTE read_xe_bank_state ( KWORD addr );
void write_5200_stack ( KWORD addr, KBYTE value );
void write_5200_stack_watch ( KWORD addr, KBYTE value );
void write_xe_bank_ram ( KWORD addr, KBYTE value );
void write_mem_image_ram ( KWORD addr, KBYTE value );
void write_5200_rom ( KWORD addr, KBYTE value );
void write_5200_rom_bb ( KWORD addr, KBYTE value );
void write_5200_rom_bb_pc ( KWORD addr, KBYTE value );
void write_5200_rom_64k ( KWORD addr, KBYTE value );
void write_5200_rom_be16k ( KWORD addr, KBYTE value );
void write_5200_rom_be32k ( KWORD addr, KBYTE value );
void write_5200_rom_512k_hyb ( KWORD addr, KBYTE value );
void write_5200_rom_512k_hyb_debug ( KWORD addr, KBYTE value );
void write_5200_sram_hyb ( KWORD addr, KBYTE value );
void write_5200_sram_hyb_debug ( KWORD addr, KBYTE value );
void write_5200_sram_hyb_watch ( KWORD addr, KBYTE value );
KBYTE read_5200_rom_be_hyb ( KWORD addr );
KBYTE read_5200_rom_be_hyb_debug ( KWORD addr );
void write_cart_32k_db ( KWORD addr, KBYTE value );
void write_cart_xegs ( KWORD addr, KBYTE value );
void write_cart_swxegs ( KWORD addr, KBYTE value );
void write_cart_mega ( KWORD addr, KBYTE value );
void write_cart_atmax ( KWORD addr, KBYTE value );
void write_cart_will ( KWORD addr, KBYTE value );
void write_cart_sdx ( KWORD addr, KBYTE value );
void write_cart_oss ( KWORD addr, KBYTE value );
void write_cart_oss2 ( KWORD addr, KBYTE value );
void write_cart_blizzard ( KWORD addr, KBYTE value );
void write_cart_atrax ( KWORD addr, KBYTE value );

void write_5200_ram_debug ( KWORD addr, KBYTE value );
void write_5200_stack_debug ( KWORD addr, KBYTE value );
void write_xe_bank_ram_debug ( KWORD addr, KBYTE value );
void write_mem_image_ram_debug ( KWORD addr, KBYTE value );
void write_5200_rom_bb_debug ( KWORD addr, KBYTE value );
void write_5200_rom_bb_pc_debug ( KWORD addr, KBYTE value );
void write_5200_rom_64k_debug ( KWORD addr, KBYTE value );
void write_5200_rom_be16k_debug ( KWORD addr, KBYTE value );
void write_5200_rom_be32k_debug ( KWORD addr, KBYTE value );
void write_cart_32k_db_debug ( KWORD addr, KBYTE value );
void write_cart_xegs_debug ( KWORD addr, KBYTE value );
void write_cart_swxegs_debug ( KWORD addr, KBYTE value );
void write_cart_mega_debug ( KWORD addr, KBYTE value );
void write_cart_atmax_debug ( KWORD addr, KBYTE value );
void write_cart_will_debug ( KWORD addr, KBYTE value );
void write_cart_sdx_debug ( KWORD addr, KBYTE value );
void write_cart_oss_debug ( KWORD addr, KBYTE value );
void write_cart_oss2_debug ( KWORD addr, KBYTE value );
void write_cart_blizzard_debug ( KWORD addr, KBYTE value );
void write_cart_atrax_debug ( KWORD addr, KBYTE value );
void write_5200_ram_watch ( KWORD addr, KBYTE value );
void write_5200_rom_debug ( KWORD addr, KBYTE value );

#endif
