peanut_gb.h
uint8_t __gb_read(struct gb_s *gb, const uint_fast16_t addr)
{
	switch(addr >> 12)
	{
	case 0x0:

	/* TODO: BIOS support. */
	case 0x1:
	case 0x2:
	case 0x3:
		return gb->gb_rom_read(gb, addr);

	case 0x4:
	case 0x5:
	case 0x6:
	case 0x7:
		if(gb->mbc == 1 && gb->cart_mode_select)
			return gb->gb_rom_read(gb,
					       addr + ((gb->selected_rom_bank & 0x1F) - 1) * ROM_BANK_SIZE);
		else
			return gb->gb_rom_read(gb, addr + (gb->selected_rom_bank - 1) * ROM_BANK_SIZE);

	case 0x8:
	case 0x9:
		return gb->vram[addr - VRAM_ADDR];

	case 0xA:
	case 0xB:
		if(gb->cart_ram && gb->enable_cart_ram)
		{
			if(gb->mbc == 3 && gb->cart_ram_bank >= 0x08)
				return gb->cart_rtc[gb->cart_ram_bank - 0x08];
			else if((gb->cart_mode_select || gb->mbc != 1) &&
					gb->cart_ram_bank < gb->num_ram_banks)
			{
				return gb->gb_cart_ram_read(gb, addr - CART_RAM_ADDR +
							    (gb->cart_ram_bank * CRAM_BANK_SIZE));
			}
			else
				return gb->gb_cart_ram_read(gb, addr - CART_RAM_ADDR);
		}

		return 0xFF;

	case 0xC:
		return gb->wram[addr - WRAM_0_ADDR];

	case 0xD:
		return gb->wram[addr - WRAM_1_ADDR + WRAM_BANK_SIZE];

	case 0xE:
		return gb->wram[addr - ECHO_ADDR];

	case 0xF:
		if(addr < OAM_ADDR)
			return gb->wram[addr - ECHO_ADDR];

		if(addr < UNUSED_ADDR)
			return gb->oam[addr - OAM_ADDR];

		/* Unusable memory area. Reading from this area returns 0.*/
		if(addr < IO_ADDR)
			return 0xFF;

		/* HRAM */
		if(HRAM_ADDR <= addr && addr < INTR_EN_ADDR)
			return gb->hram[addr - HRAM_ADDR];

		if((addr >= 0xFF10) && (addr <= 0xFF3F))
		{
#if ENABLE_SOUND
			return audio_read(addr);
#else
			return 1;
#endif
		}

		/* IO and Interrupts. */
		switch(addr & 0xFF)
		{
		/* IO Registers */
		case 0x00:
			return 0xC0 | gb->gb_reg.P1;

		case 0x01:
			return gb->gb_reg.SB;

		case 0x02:
			return gb->gb_reg.SC;

		/* Timer Registers */
		case 0x04:
			return gb->gb_reg.DIV;

		case 0x05:
			return gb->gb_reg.TIMA;

		case 0x06:
			return gb->gb_reg.TMA;

		case 0x07:
			return gb->gb_reg.TAC;

		/* Interrupt Flag Register */
		case 0x0F:
			return gb->gb_reg.IF;

		/* LCD Registers */
		case 0x40:
			return gb->gb_reg.LCDC;

		case 0x41:
			return (gb->gb_reg.STAT & STAT_USER_BITS) |
			       (gb->gb_reg.LCDC & LCDC_ENABLE ? gb->lcd_mode : LCD_VBLANK);

		case 0x42:
			return gb->gb_reg.SCY;

		case 0x43:
			return gb->gb_reg.SCX;

		case 0x44:
			return gb->gb_reg.LY;

		case 0x45:
			return gb->gb_reg.LYC;

		/* DMA Register */
		case 0x46:
			return gb->gb_reg.DMA;

		/* DMG Palette Registers */
		case 0x47:
			return gb->gb_reg.BGP;

		case 0x48:
			return gb->gb_reg.OBP0;

		case 0x49:
			return gb->gb_reg.OBP1;

		/* Window Position Registers */
		case 0x4A:
			return gb->gb_reg.WY;

		case 0x4B:
			return gb->gb_reg.WX;

		/* Interrupt Enable Register */
		case 0xFF:
			return gb->gb_reg.IE;

		/* Unused registers return 1 */
		default:
			return 0xFF;
		}
	}

	(gb->gb_error)(gb, GB_INVALID_READ, addr);
	return 0xFF;
}
/**
 * Internal function used to read bytes.
 */
Internal function used to read bytes.
Internal function used to read bytes .
uint8_t __gb_read(struct gb_s *gb, const uint_fast16_t addr)
{
	switch(addr >> 12)
	{
	case 0x0:
	case 0x1:
	case 0x2:
	case 0x3:
		return gb->gb_rom_read(gb, addr);
	case 0x4:
	case 0x5:
	case 0x6:
	case 0x7:
		if(gb->mbc == 1 && gb->cart_mode_select)
			return gb->gb_rom_read(gb,
					       addr + ((gb->selected_rom_bank & 0x1F) - 1) * ROM_BANK_SIZE);
		else
			return gb->gb_rom_read(gb, addr + (gb->selected_rom_bank - 1) * ROM_BANK_SIZE);
	case 0x8:
	case 0x9:
		return gb->vram[addr - VRAM_ADDR];
	case 0xA:
	case 0xB:
		if(gb->cart_ram && gb->enable_cart_ram)
		{
			if(gb->mbc == 3 && gb->cart_ram_bank >= 0x08)
				return gb->cart_rtc[gb->cart_ram_bank - 0x08];
			else if((gb->cart_mode_select || gb->mbc != 1) &&
					gb->cart_ram_bank < gb->num_ram_banks)
			{
				return gb->gb_cart_ram_read(gb, addr - CART_RAM_ADDR +
							    (gb->cart_ram_bank * CRAM_BANK_SIZE));
			}
			else
				return gb->gb_cart_ram_read(gb, addr - CART_RAM_ADDR);
		}
		return 0xFF;
	case 0xC:
		return gb->wram[addr - WRAM_0_ADDR];
	case 0xD:
		return gb->wram[addr - WRAM_1_ADDR + WRAM_BANK_SIZE];
	case 0xE:
		return gb->wram[addr - ECHO_ADDR];
	case 0xF:
		if(addr < OAM_ADDR)
			return gb->wram[addr - ECHO_ADDR];
		if(addr < UNUSED_ADDR)
			return gb->oam[addr - OAM_ADDR];
		if(addr < IO_ADDR)
			return 0xFF;
		if(HRAM_ADDR <= addr && addr < INTR_EN_ADDR)
			return gb->hram[addr - HRAM_ADDR];
		if((addr >= 0xFF10) && (addr <= 0xFF3F))
		{
#if ENABLE_SOUND
			return audio_read(addr);
#else
			return 1;
#endif
		}
		switch(addr & 0xFF)
		{
		case 0x00:
			return 0xC0 | gb->gb_reg.P1;
		case 0x01:
			return gb->gb_reg.SB;
		case 0x02:
			return gb->gb_reg.SC;
		case 0x04:
			return gb->gb_reg.DIV;
		case 0x05:
			return gb->gb_reg.TIMA;
		case 0x06:
			return gb->gb_reg.TMA;
		case 0x07:
			return gb->gb_reg.TAC;
		case 0x0F:
			return gb->gb_reg.IF;
		case 0x40:
			return gb->gb_reg.LCDC;
		case 0x41:
			return (gb->gb_reg.STAT & STAT_USER_BITS) |
			       (gb->gb_reg.LCDC & LCDC_ENABLE ? gb->lcd_mode : LCD_VBLANK);
		case 0x42:
			return gb->gb_reg.SCY;
		case 0x43:
			return gb->gb_reg.SCX;
		case 0x44:
			return gb->gb_reg.LY;
		case 0x45:
			return gb->gb_reg.LYC;
		case 0x46:
			return gb->gb_reg.DMA;
		case 0x47:
			return gb->gb_reg.BGP;
		case 0x48:
			return gb->gb_reg.OBP0;
		case 0x49:
			return gb->gb_reg.OBP1;
		case 0x4A:
			return gb->gb_reg.WY;
		case 0x4B:
			return gb->gb_reg.WX;
		case 0xFF:
			return gb->gb_reg.IE;
		default:
			return 0xFF;
		}
	}
	(gb->gb_error)(gb, GB_INVALID_READ, addr);
	return 0xFF;
}
uint8_t __gb_read ( struct gb_s * gb , const uint_fast16_t addr ) { switch ( addr >> 12 ) { case 0x0 : case 0x1 : case 0x2 : case 0x3 : return gb -> gb_rom_read ( gb , addr ) ; case 0x4 : case 0x5 : case 0x6 : case 0x7 : if ( gb -> mbc == 1 && gb -> cart_mode_select ) return gb -> gb_rom_read ( gb , addr + ( ( gb -> selected_rom_bank & 0x1F ) - 1 ) * ROM_BANK_SIZE ) ; else return gb -> gb_rom_read ( gb , addr + ( gb -> selected_rom_bank - 1 ) * ROM_BANK_SIZE ) ; case 0x8 : case 0x9 : return gb -> vram [ addr - VRAM_ADDR ] ; case 0xA : case 0xB : if ( gb -> cart_ram && gb -> enable_cart_ram ) { if ( gb -> mbc == 3 && gb -> cart_ram_bank >= 0x08 ) return gb -> cart_rtc [ gb -> cart_ram_bank - 0x08 ] ; else if ( ( gb -> cart_mode_select || gb -> mbc != 1 ) && gb -> cart_ram_bank < gb -> num_ram_banks ) { return gb -> gb_cart_ram_read ( gb , addr - CART_RAM_ADDR + ( gb -> cart_ram_bank * CRAM_BANK_SIZE ) ) ; } else return gb -> gb_cart_ram_read ( gb , addr - CART_RAM_ADDR ) ; } return 0xFF ; case 0xC : return gb -> wram [ addr - WRAM_0_ADDR ] ; case 0xD : return gb -> wram [ addr - WRAM_1_ADDR + WRAM_BANK_SIZE ] ; case 0xE : return gb -> wram [ addr - ECHO_ADDR ] ; case 0xF : if ( addr < OAM_ADDR ) return gb -> wram [ addr - ECHO_ADDR ] ; if ( addr < UNUSED_ADDR ) return gb -> oam [ addr - OAM_ADDR ] ; if ( addr < IO_ADDR ) return 0xFF ; if ( HRAM_ADDR <= addr && addr < INTR_EN_ADDR ) return gb -> hram [ addr - HRAM_ADDR ] ; if ( ( addr >= 0xFF10 ) && ( addr <= 0xFF3F ) ) { #if ENABLE_SOUND return audio_read ( addr ) ; #else return 1 ; #endif } switch ( addr & 0xFF ) { case 0x00 : return 0xC0 | gb -> gb_reg . P1 ; case 0x01 : return gb -> gb_reg . SB ; case 0x02 : return gb -> gb_reg . SC ; case 0x04 : return gb -> gb_reg . DIV ; case 0x05 : return gb -> gb_reg . TIMA ; case 0x06 : return gb -> gb_reg . TMA ; case 0x07 : return gb -> gb_reg . TAC ; case 0x0F : return gb -> gb_reg . IF ; case 0x40 : return gb -> gb_reg . LCDC ; case 0x41 : return ( gb -> gb_reg . STAT & STAT_USER_BITS ) | ( gb -> gb_reg . LCDC & LCDC_ENABLE ? gb -> lcd_mode : LCD_VBLANK ) ; case 0x42 : return gb -> gb_reg . SCY ; case 0x43 : return gb -> gb_reg . SCX ; case 0x44 : return gb -> gb_reg . LY ; case 0x45 : return gb -> gb_reg . LYC ; case 0x46 : return gb -> gb_reg . DMA ; case 0x47 : return gb -> gb_reg . BGP ; case 0x48 : return gb -> gb_reg . OBP0 ; case 0x49 : return gb -> gb_reg . OBP1 ; case 0x4A : return gb -> gb_reg . WY ; case 0x4B : return gb -> gb_reg . WX ; case 0xFF : return gb -> gb_reg . IE ; default : return 0xFF ; } } ( gb -> gb_error ) ( gb , GB_INVALID_READ , addr ) ; return 0xFF ; }
Internal function used to read bytes.
Internal function used to read bytes .
/* TODO: BIOS support. */ /* Unusable memory area. Reading from this area returns 0.*/ /* HRAM */ /* IO and Interrupts. */ /* IO Registers */ /* Timer Registers */ /* Interrupt Flag Register */ /* LCD Registers */ /* DMA Register */ /* DMG Palette Registers */ /* Window Position Registers */ /* Interrupt Enable Register */ /* Unused registers return 1 */