//__BIGPEMU_SCRIPT_MODULE__ //__BIGPEMU_META_DESC__ "A Hello World BigPEmu scripting sample." //__BIGPEMU_META_AUTHOR__ "Rich Whitehouse" //the line "//__BIGPEMU_SCRIPT_MODULE__" must be the very first line of the script, or it won't be built as a unique module. //all subsequent META lines must follow directly, or they'll be ignored. //for large-project organization, it's generally wise to have a single module which begins with //__BIGPEMU_SCRIPT_MODULE__, //and which #includes additional modules which do not begin with //__BIGPEMU_SCRIPT_MODULE__. #include "bigpcrt.h" //the BigPEmu VM CRT expects the following functions to be implemented for every individually-built script module: /* void bigp_init(); void bigp_shutdown(); */ static int sOnLoadEvent = -1; static int sOnUnloadEvent = -1; static int sOnVideoEvent = -1; static int sPrintEventsSettingHandle = -1; static int sLastPrintEvents = 0; #define ROM_PATH_BUFFER_SIZE 1024 static uint32_t on_sw_loaded(const int eventHandle, void *pEventData) { if (sLastPrintEvents) { const uint64_t hash = bigpemu_get_loaded_fnv1a64(); //we could, for example, add some breakpoints here if the hash matches some particular software title we're after printf_notify("hello_pussycat loaded event: 0x%08X%08X", (uint32_t)(hash >> 32ULL), (uint32_t)(hash & 0xFFFFFFFFULL)); char romPath[ROM_PATH_BUFFER_SIZE]; bigpemu_get_loaded_software_path(romPath, ROM_PATH_BUFFER_SIZE); printf("Software path: %s\n", romPath); } return 0; } static uint32_t on_sw_unloaded(const int eventHandle, void *pEventData) { if (sLastPrintEvents) { //you won't typically have much to do here, since breakpoints are auto-cleared as software is unloaded. printf_notify("hello_pussycat unloaded event."); } return 0; } //note that the interpreter will never be running on multiple threads concurrently, so we don't need to worry about thread safety between script functions. static uint32_t on_video_frame(const int eventHandle, void *pEventData) { //the video frame event is a good place to do any global state refresh which doesn't involve messing with emulated state. //anything which directly manipulates/reads jaguar state should instead be done in an emulation frame event. //failure to handle this correctly can cause threading-based issues which may be difficult to debug/reproduce. bigpemu_get_setting_value(&sLastPrintEvents, sPrintEventsSettingHandle); } void bigp_init() { void *pMod = bigpemu_get_module_handle(); const int catHandle = bigpemu_register_setting_category(pMod, "Hello Pussycat"); sPrintEventsSettingHandle = bigpemu_register_setting_bool(catHandle, "Print Events", 1); sOnLoadEvent = bigpemu_register_event_sw_loaded(pMod, on_sw_loaded); sOnUnloadEvent = bigpemu_register_event_sw_unloaded(pMod, on_sw_unloaded); sOnVideoEvent = bigpemu_register_event_video_thread_frame(pMod, on_video_frame); printf("hello_pussycat init\nScript API version: %i\nModule handle: %08X\n", bigpemu_get_api_version(), (uint32_t)pMod); //fs sample snippet /* { const uint64_t fh = fs_open("testfile.txt", 1); const char *pTest = "booty butt cheeks!"; fs_write(pTest, strlen(pTest), fh); fs_close(fh); } */ //this demonstrates how to call the following native module function: /* extern "C" __declspec(dllexport) int hello_pussy_test_buffer(char *pOutMsg) { strcpy(pOutMsg, "hello pussy from native dll!\n"); return 666; } */ //as compiled into a 64-bit Windows DLL. //IMPORTANT - this method only supports functions with the CDECL calling convention! we're not tracking anything about the function being called, //all we do is check the stack delta to see how much data the function being called expects on the stack. we then transfer it to the native stack //to make the call. naturally, we can only remain blind about function protos with CDECL. if you need to call non-CDECL methods, you should use a //wrapper module. /* const uint64_t modHandle = bigpemu_nativemod_load("hello_pussy_dll"); if (modHandle) { //since we're expecting a 64-bit module here, we don't define the function with a char ptr, and instead explicitly use a 64-bit type. //we could potentially check bigpemu_get_platform here to determine which protoype to employ. typedef int (*THelloPussyTestBuffer)(uint64_t pOutMsg); const uint64_t funcAddr = bigpemu_nativemod_getfuncaddr(modHandle, "hello_pussy_test_buffer"); if (funcAddr) { char buffer[256]; const uint64_t bufPtr = bigpemu_process_memory_alias(buffer); int retVal; THelloPussyTestBuffer pLocalFuncPtr; buffer[0] = 0; //take care to not have function arguments evaluated in the macro call, might be a tcc bug bigpemu_nativemod_call_ret(&retVal, pLocalFuncPtr, modHandle, funcAddr, bufPtr); printf("Native module function returns %i and says: %s\n", retVal, buffer); } bigpemu_nativemod_free(modHandle); } */ } void bigp_shutdown() { void *pMod = bigpemu_get_module_handle(); //not strictly necessary since these events will be destroyed with the module, but we're establishing good practices here. bigpemu_unregister_event(pMod, sOnLoadEvent); bigpemu_unregister_event(pMod, sOnUnloadEvent); bigpemu_unregister_event(pMod, sOnVideoEvent); sOnLoadEvent = -1; sOnUnloadEvent = -1; sOnVideoEvent = -1; sPrintEventsSettingHandle = -1; sLastPrintEvents = 0; printf("hello_pussycat shutdown\n"); }