之前已经实现了NES模拟器玩游戏。W801学习笔记九:HLK-W801制作学习机/NES游戏机(模拟器)
现在要在新版本掌机中移植过来。
1、把NES文件都拷贝到SD卡中。
这回不会受内存大小限制了。我这里拷贝了4个,还可以拷贝更多。
2、应用初始化中,加载NES文件。
把NES文件名在菜单中进行绑定,动态加载到PSRAM中
int NesSim::scean_init(cJSON* param){
char * fileName= cJSON_GetObjectItem(param,"f")->valuestring;
int sk = cJSON_GetObjectItem(param,"k")->valueint;
size_t readsize = fatfs_readFile(fileName, &romData);
LCD_Clear(BLACK);
if(0!=InfoNES_Load((BYTE*)romData,sk))
{
printf("InfoNES_Load fail \n");
}
else
{
printf("InfoNES_Load success \n");
InfoNES_Main();
}
return 0;
}
k参数用于设置跳过的帧数,以提高最终游戏帧率。
3、修改并确认按键。
InfoNES_system.c
void InfoNES_PadState( DWORD *pdwPad1, DWORD *pdwPad2, DWORD *pdwSystem )
{
u32 keys = KEY_INPUTB;
*pdwPad1 = 0;
if(!(keys & (0x1 << KEY_GPIO_UP)))
{
*pdwPad1 |= PAD_JOY_UP;
}
if(!(keys & (0x1 << KEY_GPIO_DOWN)))
{
*pdwPad1 |= PAD_JOY_DOWN;
}
if(!(keys & (0x1 << KEY_GPIO_LEFT)))
{
*pdwPad1 |= PAD_JOY_LEFT;
}
if(!(keys & (0x1 << KEY_GPIO_RHT)))
{
*pdwPad1 |= PAD_JOY_RIGHT;
}
if(!(keys & (0x1 <<KEY_GPIO_SEL)))
{
*pdwPad1 |= PAD_JOY_START;
}
if(!(keys & (0x1 << KEY_GPIO_A)))
{
*pdwPad1 |= PAD_JOY_A;
}
if(!(keys & (0x1 << KEY_GPIO_B)))
{
*pdwPad1 |= PAD_JOY_B;
}
if(!(keys & (0x1 << KEY_GPIO_EXIT)))
{
*pdwPad1 |= PAD_JOY_SELECT;
}
}
KeyAdepter.h
#define KEY_GPIO_LEFT 27
#define KEY_GPIO_DOWN 18
#define KEY_GPIO_RHT 17
#define KEY_GPIO_UP 16
#define KEY_GPIO_A 21
#define KEY_GPIO_B 23
#define KEY_GPIO_C 22
#define KEY_GPIO_D 24
#define KEY_GPIO_SEL 26
#define KEY_GPIO_EXIT 25
#define KEY_INPUTB (*(TLS_REG*)(HR_GPIO_DATA + TLS_IO_AB_OFFSET))
4、在菜单中加入模拟器游戏
。。。
{
"t": "NES 模拟器",
"i": "-",
"d": 0,
"g": {},
"s": [
{
"t": "魂斗罗",
"i": "-",
"d": 400,
"g": {"f":"nes/hdl.nes", "k":1},
"s": []
},
{
"t": "沙罗曼蛇II",
"i": "-",
"d": 400,
"g": {"f":"nes/slms.nes", "k":1},
"s": []
},
{
"t": "马戏团",
"i": "-",
"d": 400,
"g": {"f":"nes/mxt.nes", "k":1},
"s": []
},
{
"t": "超级马里奥",
"i": "-",
"d": 400,
"g": {"f":"nes/cjmla.nes", "k":2},
"s": []
}
]
}
。。。
5、修改游戏加载
InfoNES.c
int InfoNES_Load( BYTE * rom, int sk)
{
/*
* Load a cassette
*
* Parameters
* const char *pszFileName (Read)
* File name of ROM image
*
* Return values
* 0 : It was finished normally.
* -1 : An error occurred.
*
* Remarks
* Read a ROM image in the memory.
* Reset InfoNES.
*/
// Release a memory for ROM
InfoNES_ReleaseRom();
// Read a ROM image in the memory
GameFrameSkip = sk;
if ( InfoNES_ReadRom( rom ) < 0 )
return -1;
// Reset InfoNES
if ( InfoNES_Reset() < 0 )
return -1;
// Successful
return 0;
}