INL-retro-progdump/firmware/source/gameboy.c

133 lines
3.2 KiB
C

#include "gameboy.h"
//only need this file if connector is present on the device
#ifdef GB_CONN
//=================================================================================================
//
// GAMEBOY operations
// This file includes all the gameboy functions possible to be called from the gameboy dictionary.
//
// See description of the commands contained here in shared/shared_dictionaries.h
//
//=================================================================================================
/* Desc:Function takes an opcode which was transmitted via USB
* then decodes it to call designated function.
* shared_dict_gameboy.h is used in both host and fw to ensure opcodes/names align
* Pre: Macros must be defined in firmware pinport.h
* opcode must be defined in shared_dict_gameboy.h
* Post:function call complete.
* Rtn: SUCCESS if opcode found and completed, error if opcode not present or other problem.
*/
uint8_t gameboy_call( uint8_t opcode, uint8_t miscdata, uint16_t operand, uint8_t *rdata )
{
#define RD_LEN 0
#define RD0 1
#define RD1 2
#define BYTE_LEN 1
#define HWORD_LEN 2
switch (opcode) {
// //no return value:
case DMG_WR:
dmg_wr( operand, miscdata );
break;
//8bit return values:
case DMG_RD:
rdata[RD_LEN] = BYTE_LEN;
rdata[RD0] = dmg_rd( operand );
break;
default:
//macro doesn't exist
return ERR_UNKN_GAMEBOY_OPCODE;
}
return SUCCESS;
}
uint8_t dmg_rd( uint16_t addr )
{
return 0xAA;
}
void dmg_wr( uint16_t addr, uint8_t data )
{
return;
}
/* Desc:GAMEBOY 8bit CPU Page Read with optional USB polling
* decode A15 from addrH to set SRAM /CE as expected
* if poll is true calls usbdrv.h usbPoll fuction
* this is needed to keep from timing out when double buffering usb data
* Pre: gameboy_init() setup of io pins
* num_bytes can't exceed 256B page boundary
* Post:address left on bus
* data bus left clear
* data buffer filled starting at first to last
* Rtn: Index of last byte read
*/
uint8_t gameboy_page_rd_poll( uint8_t *data, uint8_t addrH, uint8_t first, uint8_t len, uint8_t poll )
{
uint8_t i;
//set address bus
ADDRH(addrH);
//enable /RD pin
CSRD_LO();
//set SRAM /CS
//low for $A000-BFFF
if( (addrH >= 0xA0) && (addrH <= 0xBF) ) { //addressing cart RAM space
ROMSEL_LO(); //this is actually the SRAM /CS pin
}
//set lower address bits
ADDRL(first); //doing this prior to entry and right after latching
//extra NOP was needed on stm6 as address hadn't settled in time for the very first read
NOP();
//gives longest delay between address out and latching data
for( i=0; i<=len; i++ ) {
//testing shows that having this if statement doesn't affect overall dumping speed
if ( poll ) {
usbPoll(); //Call usbdrv.h usb polling while waiting for data
} else {
NOP(); //couple more NOP's waiting for data
NOP(); //one prob good enough considering the if/else
}
//gameboy needed some extra NOPS
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
//latch data
DATA_RD(data[i]);
//set lower address bits
//ADDRL(++first); THIS broke things, on stm adapter because macro expands it twice!
first++;
ADDRL(first);
}
//return bus to default
CSRD_HI();
ROMSEL_HI();
//return index of last byte read
return i;
}
#endif //GB_CONN