1 /* 2 Copyright (C) 2006 yopyop 3 Copyright (C) 2008-2015 DeSmuME team 4 5 This file is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 2 of the License, or 8 (at your option) any later version. 9 10 This file is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with the this software. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #ifndef NDSSYSTEM_H 20 #define NDSSYSTEM_H 21 22 #include <stdio.h> 23 #include <string.h> 24 #include <string> 25 26 #include "types.h" 27 28 class BaseDriver; 29 class CFIRMWARE; 30 class EMUFILE; 31 32 #define LAYOUT_TOP_BOTTOM 0 33 #define LAYOUT_BOTTOM_TOP 1 34 #define LAYOUT_LEFT_RIGHT 2 35 #define LAYOUT_RIGHT_LEFT 3 36 #define LAYOUT_TOP_ONLY 4 37 #define LAYOUT_BOTTOM_ONLY 5 38 #define LAYOUT_HYBRID_TOP_ONLY 6 39 #define LAYOUT_HYBRID_BOTTOM_ONLY 7 40 41 template<typename Type> 42 struct buttonstruct { 43 union { 44 struct { 45 // changing the order of these fields would break stuff 46 //fRLDUTSBAYXWEg 47 Type G; // debug 48 Type E; // right shoulder 49 Type W; // left shoulder 50 Type X; 51 Type Y; 52 Type A; 53 Type B; 54 Type S; // start 55 Type T; // select 56 Type U; // up 57 Type D; // down 58 Type L; // left 59 Type R; // right 60 Type F; // lid 61 }; 62 Type array[14]; 63 }; 64 }; 65 66 extern volatile int execute; 67 extern BOOL click; 68 69 #if defined(LOG_ARM9) || defined(LOG_ARM7) 70 void emu_halt(); 71 #else 72 #define emu_halt() (execute = 0) 73 #endif 74 /* 75 * The firmware language values 76 */ 77 #define NDS_FW_LANG_JAP 0 78 #define NDS_FW_LANG_ENG 1 79 #define NDS_FW_LANG_FRE 2 80 #define NDS_FW_LANG_GER 3 81 #define NDS_FW_LANG_ITA 4 82 #define NDS_FW_LANG_SPA 5 83 #define NDS_FW_LANG_CHI 6 84 #define NDS_FW_LANG_RES 7 85 86 extern BaseDriver *driver; 87 extern CFIRMWARE *firmware; 88 89 #define DSGBA_LOADER_SIZE 512 90 enum 91 { 92 ROM_NDS = 0, 93 ROM_DSGBA 94 }; 95 96 //#define LOG_ARM9 97 //#define LOG_ARM7 98 99 #include "PACKED.h" 100 struct NDS_header 101 { 102 char gameTile[12]; // 000 - Game Title (uppercase ASCII, padded with 00h) 103 char gameCode[4]; // 00C - Gamecode (uppercase ASCII, NTR-<code>, 0=homebrew) 104 u16 makerCode; // 010 - Makercode (uppercase ASCII, 0=homebrew) 105 u8 unitCode; // 012 - Unitcode (00h=Nintendo DS) 106 u8 deviceCode; // 013 - Encryption Seed Select (00..07h, usually 00h) 107 u8 cardSize; // 014 - Devicecapacity (Chipsize = 128KB SHL nn) (eg. 7 = 16MB) 108 u8 reserved1[8]; // 015 - Must be set to 0x00 109 u8 region; // 01D - Specific region: 0x80 - China, 0x40 - Korea, 0x00 - Other 110 u8 romversion; // 01E - ROM Version (usually 00h) 111 u8 autostart; // 01F - Autostart (Bit2: Skip "Press Button" after Health and Safety) 112 // (Also skips bootmenu, even in Manual mode & even Start pressed) 113 u32 ARM9src; // 020 - ARM9 rom_offset (4000h and up, align 1000h) 114 u32 ARM9exe; // 024 - ARM9 entry_address (2000000h..23BFE00h) 115 u32 ARM9cpy; // 028 - ARM9 ram_address (2000000h..23BFE00h) 116 u32 ARM9binSize; // 02C - ARM9 size (max 3BFE00h) (3839.5KB) 117 118 u32 ARM7src; // 030 - ARM7 rom_offset (8000h and up) 119 u32 ARM7exe; // 034 - ARM7 entry_address (2000000h..23BFE00h, or 37F8000h..3807E00h) 120 u32 ARM7cpy; // 038 - ARM7 ram_address (2000000h..23BFE00h, or 37F8000h..3807E00h) 121 u32 ARM7binSize; // 03C - ARM7 size (max 3BFE00h, or FE00h) (3839.5KB, 63.5KB) 122 123 u32 FNameTblOff; // 040 - File Name Table (FNT) offset 124 u32 FNameTblSize; // 044 - File Name Table (FNT) size 125 126 u32 FATOff; // 048 - File Allocation Table (FAT) offset 127 u32 FATSize; // 04C - File Allocation Table (FAT) size 128 129 u32 ARM9OverlayOff; // 050 - File ARM9 overlay_offset 130 u32 ARM9OverlaySize; // 054 - File ARM9 overlay_size 131 u32 ARM7OverlayOff; // 058 - File ARM7 overlay_offset 132 u32 ARM7OverlaySize; // 05C - File ARM7 overlay_size 133 134 u32 normalCmd; // 060 - Port 40001A4h setting for normal commands (usually 00586000h) 135 u32 Key1Cmd; // 064 - Port 40001A4h setting for KEY1 commands (usually 001808F8h) 136 137 u32 IconOff; // 068 - Icon_title_offset (0=None) (8000h and up) 138 u16 CRC16; // 06C - Secure Area Checksum, CRC-16 of [ [20h]..7FFFh] - Calculations with this algorithm use 0xffff as the initial value 139 u16 ROMtimeout; // 06E - Secure Area Loading Timeout (usually 051Eh) 140 u32 ARM9autoload; // 070 - ARM9 Auto Load List RAM Address 141 u32 ARM7autoload; // 074 - ARM7 Auto Load List RAM Address 142 143 u8 infoResevedRegion[8]; // 078 - ROM Information Reserved Region (must be set to 0x00) 144 u32 endROMoffset; // 080 - Total Used ROM size (remaining/unused bytes usually FFh-padded) 145 u32 HeaderSize; // 084 - ROM Header Size (4000h) 146 u32 ARM9module; // 088 - ARM9 Module Parameter Address (auto-load parameters) 147 u32 ARM7module; // 08C - ARM7 Module Parameter Address (auto-load parameters) 148 u8 reserved2[48]; // 090 - Must be set to 0x00 - "PASS" is contained within here? 149 u8 logo[156]; // 0C0 - Nintendo Logo (compressed bitmap, same as in GBA Headers) 150 u16 logoCRC16; // 15C - Nintendo Logo Checksum, CRC-16 of [0C0h-15Bh], fixed CF56h 151 u16 headerCRC16; // 15E - Header Checksum, CRC-16 of [000h-15Dh] 152 u8 reserved[160]; // Must be set to 0x00 153 }; 154 #include "PACKED_END.h" 155 156 #ifdef DEBUG 157 extern void debug(); 158 #endif 159 160 extern u64 nds_timer; 161 void NDS_Reschedule(); 162 void NDS_RescheduleGXFIFO(u32 cost); 163 void NDS_RescheduleDMA(); 164 void NDS_RescheduleTimers(); 165 166 enum ENSATA_HANDSHAKE 167 { 168 ENSATA_HANDSHAKE_none = 0, 169 ENSATA_HANDSHAKE_query = 1, 170 ENSATA_HANDSHAKE_ack = 2, 171 ENSATA_HANDSHAKE_confirm = 3, 172 ENSATA_HANDSHAKE_complete = 4 173 }; 174 175 enum NDS_CONSOLE_TYPE 176 { 177 NDS_CONSOLE_TYPE_FAT = 0xFF, 178 NDS_CONSOLE_TYPE_LITE = 0x20, 179 NDS_CONSOLE_TYPE_IQUE = 0x43, 180 NDS_CONSOLE_TYPE_IQUE_LIE = 0x63, 181 NDS_CONSOLE_TYPE_DSI = 0xFE 182 }; 183 184 struct NDSSystem 185 { 186 s32 wifiCycle; 187 s32 cycles; 188 u64 timerCycle[2][4]; 189 u32 VCount; 190 u32 old; 191 192 //raw adc touch coords for old NDS 193 u16 adc_touchX; 194 u16 adc_touchY; 195 s32 adc_jitterctr; 196 BOOL stylusJitter; 197 198 //the DSI returns calibrated touch coords from its TSC (?), so we need to save these separately 199 u16 scr_touchX; 200 u16 scr_touchY; 201 202 //whether the console is using our faked-bootup process 203 BOOL isFakeBooted; 204 205 BOOL isTouch; 206 u16 pad; 207 208 u16 paddle; 209 210 u8 *FW_ARM9BootCode; 211 u8 *FW_ARM7BootCode; 212 u32 FW_ARM9BootCodeAddr; 213 u32 FW_ARM7BootCodeAddr; 214 u32 FW_ARM9BootCodeSize; 215 u32 FW_ARM7BootCodeSize; 216 217 BOOL sleeping; 218 BOOL cardEjected; 219 u32 freezeBus; 220 221 //this is not essential NDS runtime state. 222 //it was perhaps a mistake to put it here. 223 //it is far less important than the above. 224 //maybe I should move it. 225 s32 idleCycles[2]; 226 s32 runCycleCollector[2][16]; 227 s32 idleFrameCounter; 228 s32 cpuloopIterationCount; //counts the number of times during a frame that a reschedule happened 229 230 //console type must be copied in when the system boots. it can't be changed on the fly. 231 int ConsoleType; Is_DSINDSSystem232 bool Is_DSI() { return ConsoleType == NDS_CONSOLE_TYPE_DSI; } Is_DebugConsoleNDSSystem233 bool Is_DebugConsole() { return _DebugConsole!=0; } 234 BOOL _DebugConsole; 235 236 //set if the user requests ensata emulation 237 BOOL ensataEmulation; 238 239 //there is a hack in the ipc sync for ensata. this tracks its state 240 u32 ensataIpcSyncCounter; 241 242 //maintains the state of the ensata handshaking protocol 243 u32 ensataHandshake; 244 245 struct { 246 u8 lcd, gpuMain, gfx3d_render, gfx3d_geometry, gpuSub, dispswap; 247 } power1; //POWCNT1 248 249 struct { 250 u8 speakers, wifi /*(initial value=0)*/; 251 } power2; //POWCNT2 252 isInVblankNDSSystem253 bool isInVblank() const { return VCount >= 192; } isIn3dVblankNDSSystem254 bool isIn3dVblank() const { return VCount >= 192 && VCount<215; } 255 }; 256 257 /** /brief A touchscreen calibration point. 258 */ 259 struct NDS_fw_touchscreen_cal { 260 u16 adc_x; 261 u16 adc_y; 262 263 u8 screen_x; 264 u8 screen_y; 265 }; 266 267 #define MAX_FW_NICKNAME_LENGTH 10 268 #define MAX_FW_MESSAGE_LENGTH 26 269 270 struct NDS_fw_config_data 271 { 272 NDS_CONSOLE_TYPE ds_type; 273 274 u8 fav_colour; 275 u8 birth_month; 276 u8 birth_day; 277 278 u16 nickname[MAX_FW_NICKNAME_LENGTH]; 279 u8 nickname_len; 280 281 u16 message[MAX_FW_MESSAGE_LENGTH]; 282 u8 message_len; 283 284 u8 language; 285 286 //touchscreen calibration 287 NDS_fw_touchscreen_cal touch_cal[2]; 288 }; 289 290 extern NDSSystem nds; 291 292 int NDS_Init(); 293 294 void Desmume_InitOnce(); 295 296 void NDS_DeInit(void); 297 298 BOOL NDS_SetROM(u8 * rom, u32 mask); 299 NDS_header * NDS_getROMHeader(void); 300 301 struct RomBanner 302 { 303 RomBanner(bool defaultInit = true); 304 u16 version; //Version (0001h) 305 u16 crc16; //CRC16 across entries 020h..83Fh 306 u8 reserved[0x1C]; //Reserved (zero-filled) 307 u8 bitmap[0x200]; //Icon Bitmap (32x32 pix) (4x4 tiles, each 4x8 bytes, 4bit depth) 308 u16 palette[0x10]; //Icon Palette (16 colors, 16bit, range 0000h-7FFFh) (Color 0 is transparent, so the 1st palette entry is ignored) 309 enum { NUM_TITLES = 6 }; 310 union { 311 struct { 312 u16 title_jp[0x80]; //Title 0 Japanese (128 characters, 16bit Unicode) 313 u16 title_en[0x80]; //Title 1 English ("") 314 u16 title_fr[0x80]; //Title 2 French ("") 315 u16 title_de[0x80]; //Title 3 German ("") 316 u16 title_it[0x80]; //Title 4 Italian ("") 317 u16 title_es[0x80]; //Title 5 Spanish ("") 318 }; 319 u16 titles[NUM_TITLES][0x80]; 320 }; 321 u8 end0xFF[0x1C0]; 322 //840h ? (Maybe newer/chinese firmware do also support chinese title?) 323 //840h - End of Icon/Title structure (next 1C0h bytes usually FFh-filled) 324 }; 325 326 struct GameInfo 327 { 328 FILE *fROM; 329 u8 *romdata; 330 u32 romsize; 331 u32 cardSize; 332 u32 mask; 333 u32 crc; 334 u32 chipID; 335 u32 lastReadPos; 336 u32 romType; 337 u32 headerOffset; 338 char ROMserial[20]; 339 char ROMname[20]; 340 bool _isDSiEnhanced; 341 NDS_header header; 342 //a copy of the pristine secure area from the rom 343 u8 secureArea[0x4000]; 344 RomBanner banner; 345 const RomBanner& getRomBanner(); 346 GameInfoGameInfo347 GameInfo() : fROM(NULL), 348 romdata(NULL), 349 crc(0), 350 chipID(0x00000FC2), 351 romsize(0), 352 cardSize(0), 353 mask(0), 354 lastReadPos(0xFFFFFFFF), 355 romType(ROM_NDS), 356 headerOffset(0), 357 _isDSiEnhanced(false) 358 { 359 memset(&header, 0, sizeof(header)); 360 memset(&ROMserial[0], 0, sizeof(ROMserial)); 361 memset(&ROMname[0], 0, sizeof(ROMname)); 362 } 363 ~GameInfoGameInfo364 ~GameInfo() { closeROM(); } 365 366 bool loadROM(std::string fname, u32 type = ROM_NDS); 367 void closeROM(); 368 u32 readROM(u32 pos); 369 void populate(); 370 bool isDSiEnhanced(); 371 bool isHomebrew(); 372 bool hasRomBanner(); 373 374 }; 375 376 typedef struct TSCalInfo 377 { 378 struct 379 { 380 u16 x1, x2; 381 u16 y1, y2; 382 u16 width; 383 u16 height; 384 } adc; 385 386 struct 387 { 388 u8 x1, x2; 389 u8 y1, y2; 390 u16 width; 391 u16 height; 392 } scr; 393 394 } TSCalInfo; 395 396 extern GameInfo gameInfo; 397 398 399 struct UserButtons : buttonstruct<bool> 400 { 401 }; 402 struct UserTouch 403 { 404 u16 touchX; 405 u16 touchY; 406 bool isTouch; 407 }; 408 struct UserMicrophone 409 { 410 u32 micButtonPressed; 411 }; 412 struct UserInput 413 { 414 UserButtons buttons; 415 UserTouch touch; 416 UserMicrophone mic; 417 }; 418 419 // set physical user input 420 // these functions merely request the input to be changed. 421 // the actual change happens later at a specific time during the frame. 422 // this is to minimize the risk of desyncs. 423 void NDS_setTouchPos(u16 x, u16 y, u16 scale); 424 void NDS_releaseTouch(void); 425 void NDS_setPad(bool right,bool left,bool down,bool up,bool select,bool start,bool B,bool A,bool Y,bool X,bool leftShoulder,bool rightShoulder,bool debug, bool lid); 426 void NDS_setMic(bool pressed); 427 428 // get physical user input 429 // not including the results of autofire/etc. 430 // the effects of calls to "set physical user input" functions will be immediately reflected here though. 431 const UserInput& NDS_getRawUserInput(); 432 const UserInput& NDS_getPrevRawUserInput(); 433 434 // get final (fully processed) user input 435 // this should match whatever was or would be sent to the game 436 const UserInput& NDS_getFinalUserInput(); 437 438 // set/get to-be-processed or in-the-middle-of-being-processed user input 439 // to process input, simply call this function and edit the return value. 440 // (applying autofire is one example of processing the input.) 441 // (movie playback is another example.) 442 // this must be done after the raw user input is set 443 // and before that input is sent to the game's memory. 444 UserInput& NDS_getProcessingUserInput(); 445 // call once per frame to prepare input for processing 446 void NDS_beginProcessingInput(); 447 // call once per frame to copy the processed input to the final input 448 void NDS_endProcessingInput(); 449 450 // this is in case something needs reentrancy while processing input 451 void NDS_suspendProcessingInput(bool suspend); 452 453 454 455 int NDS_LoadROM(const char *filename, const char* physicalFilename=0, const char* logicalFilename=0); 456 void NDS_FreeROM(void); 457 void NDS_Reset(); 458 459 bool NDS_LegitBoot(); 460 bool NDS_FakeBoot(); 461 462 void nds_savestate(EMUFILE* os); 463 bool nds_loadstate(EMUFILE* is, int size); 464 465 void NDS_Sleep(); 466 void NDS_TriggerCardEjectIRQ(); 467 468 void NDS_SkipNextFrame(); 469 #define NDS_SkipFrame(s) if(s) NDS_SkipNext2DFrame(); 470 void NDS_OmitFrameSkip(int force=0); 471 472 void NDS_debug_break(); 473 void NDS_debug_continue(); 474 void NDS_debug_step(); 475 476 int NDS_GetCPUCoreCount(); 477 void NDS_SetupDefaultFirmware(); 478 479 //void execHardware_doAllDma(EDMAMode modeNum); 480 481 void NDS_exec(s32 nb = 560190<<1); 482 483 extern int lagframecounter; 484 485 extern struct TCommonSettings { TCommonSettingsTCommonSettings486 TCommonSettings() 487 : GFX3D_HighResolutionInterpolateColor(true) 488 , GFX3D_EdgeMark(true) 489 , GFX3D_Fog(true) 490 , GFX3D_Texture(true) 491 , GFX3D_LineHack(true) 492 , GFX3D_Zelda_Shadow_Depth_Hack(0) 493 , GFX3D_Renderer_Multisample(false) 494 , GFX3D_TXTHack(false) 495 , jit_max_block_size(100) 496 , loadToMemory(false) 497 , UseExtBIOS(false) 498 , SWIFromBIOS(false) 499 , PatchSWI3(false) 500 , UseExtFirmware(false) 501 , UseExtFirmwareSettings(false) 502 , BootFromFirmware(false) 503 , DebugConsole(false) 504 , EnsataEmulation(false) 505 , cheatsDisable(false) 506 , rigorous_timing(false) 507 , advanced_timing(true) 508 , micMode(InternalNoise) 509 , manualBackupType(0) 510 , autodetectBackupMethod(0) 511 , spu_advanced(false) 512 , StylusPressure(50) 513 , ConsoleType(NDS_CONSOLE_TYPE_FAT) 514 , StylusJitter(false) 515 , backupSave(false) 516 , SPU_sync_method(0) 517 { 518 strcpy(ARM9BIOS, "biosnds9.bin"); 519 strcpy(ARM7BIOS, "biosnds7.bin"); 520 strcpy(Firmware, "firmware.bin"); 521 522 /* WIFI mode: adhoc = 0, infrastructure = 1 */ 523 wifi.mode = 1; 524 wifi.infraBridgeAdapter = 0; 525 526 for(int g=0;g<2;g++) 527 for(int x=0;x<5;x++) 528 dispLayers[g][x]=true; 529 #ifdef HAVE_JIT 530 //zero 06-sep-2012 - shouldnt be defaulting this to true for now, since the jit is buggy. 531 //id rather have people discover a bonus speedhack than discover new bugs in a new version 532 use_jit = false; 533 #else 534 use_jit = false; 535 #endif 536 537 num_cores = NDS_GetCPUCoreCount(); 538 NDS_SetupDefaultFirmware(); 539 } 540 bool GFX3D_HighResolutionInterpolateColor; 541 bool GFX3D_EdgeMark; 542 bool GFX3D_Fog; 543 bool GFX3D_Texture; 544 bool GFX3D_LineHack; 545 int GFX3D_Zelda_Shadow_Depth_Hack; 546 bool GFX3D_Renderer_Multisample; 547 bool GFX3D_TXTHack; 548 549 bool loadToMemory; 550 551 bool UseExtBIOS; 552 char ARM9BIOS[256]; 553 char ARM7BIOS[256]; 554 bool SWIFromBIOS; 555 bool PatchSWI3; 556 557 bool UseExtFirmware; 558 bool UseExtFirmwareSettings; 559 char Firmware[256]; 560 bool BootFromFirmware; 561 NDS_fw_config_data fw_config; 562 563 NDS_CONSOLE_TYPE ConsoleType; 564 bool DebugConsole; 565 bool EnsataEmulation; 566 567 bool cheatsDisable; 568 569 int num_cores; single_coreTCommonSettings570 bool single_core() { return num_cores==1; } 571 bool rigorous_timing; 572 573 int StylusPressure; 574 bool StylusJitter; 575 576 bool dispLayers[2][5]; 577 578 FAST_ALIGN bool advanced_timing; 579 580 bool use_jit; 581 u32 jit_max_block_size; 582 583 struct _Wifi { 584 int mode; 585 int infraBridgeAdapter; 586 } wifi; 587 588 enum MicMode 589 { 590 InternalNoise = 0, 591 Sample = 1, 592 Random = 2, 593 Physical = 3 594 } micMode; 595 596 597 //this is a temporary hack until we straighten out the flushing logic and/or gxfifo 598 //int gfx3d_flushMode; 599 600 int autodetectBackupMethod; 601 //this is the user's choice of manual backup type, for cases when the autodetection can't be trusted 602 int manualBackupType; 603 bool backupSave; 604 605 int SPU_sync_method; 606 607 bool spu_advanced; 608 609 struct _ShowGpu { _ShowGpuTCommonSettings::_ShowGpu610 _ShowGpu() : main(true), sub(true) {} 611 union { 612 struct { bool main,sub; }; 613 bool screens[2]; 614 }; 615 } showGpu; 616 617 std::string run_advanscene_import; 618 619 } CommonSettings; 620 621 void NDS_RunAdvansceneAutoImport(); 622 623 extern int LagFrameFlag; 624 extern int lastLag, TotalLagFrames; 625 626 void MovieSRAM(); 627 628 bool ValidateSlot2Access(u32 procnum, u32 demandSRAMSpeed, u32 demand1stROMSpeed, u32 demand2ndROMSpeed, int clockbits); 629 630 //MUSTANG 631 //extern ADVANsCEne advsc; 632 633 #endif 634 635