3 * Copyright (C) 2006 Exophase <exophase@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program 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 GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 u32 load_file_zip(u8 *filename);
24 // This table is configured for sequential access on system defaults
26 u32 waitstate_cycles_sequential[16][3] =
29 { 1, 1, 1 }, // Invalid
30 { 3, 3, 6 }, // EWRAM (default settings)
32 { 1, 1, 1 }, // IO Registers
33 { 1, 1, 2 }, // Palette RAM
36 { 3, 3, 6 }, // Gamepak (wait 0)
37 { 3, 3, 6 }, // Gamepak (wait 0)
38 { 5, 5, 9 }, // Gamepak (wait 1)
39 { 5, 5, 9 }, // Gamepak (wait 1)
40 { 9, 9, 17 }, // Gamepak (wait 2)
41 { 9, 9, 17 }, // Gamepak (wait 2)
44 // Different settings for gamepak ws0-2 sequential (2nd) access
46 u32 gamepak_waitstate_sequential[2][3][3] =
62 u16 palette_ram_converted[512];
63 u16 io_registers[1024 * 16];
64 u8 ewram[1024 * 256 * 2];
65 u8 iwram[1024 * 32 * 2];
66 u8 vram[1024 * 96 * 2];
68 u8 bios_rom[1024 * 32];
69 u32 bios_read_protect;
71 // Up to 128kb, store SRAM, flash ROM, or EEPROM here.
72 u8 gamepak_backup[1024 * 128];
74 // Keeps us knowing how much we have left.
78 dma_transfer_type dma[4];
80 u8 *memory_regions[16];
81 u32 memory_limits[16];
87 } gamepak_swap_entry_type;
89 u32 gamepak_ram_buffer_size;
90 u32 gamepak_ram_pages;
92 // Enough to map the gamepak RAM space.
93 gamepak_swap_entry_type *gamepak_memory_map;
95 // This is global so that it can be kept open for large ROMs to swap
96 // pages from, so there's no slowdown with opening and closing the file
100 file_tag_type gamepak_file_large = -1;
104 file_tag_type gamepak_file_large = NULL;
108 u32 direct_map_vram = 0;
110 // Writes to these respective locations should trigger an update
111 // so the related subsystem may react to it.
113 // If OAM is written to:
116 // If GBC audio is written to:
117 u32 gbc_sound_update = 0;
119 // If the GBC audio waveform is modified:
120 u32 gbc_sound_wave_update = 0;
122 // If the backup space is written (only update once this hits 0)
123 u32 backup_update = 0;
125 // Write out backup file this many cycles after the most recent
127 const u32 write_backup_delay = 10;
144 // Keep it 32KB until the upper 64KB is accessed, then make it 64KB.
146 backup_type_type backup_type = BACKUP_NONE;
147 sram_size_type sram_size = SRAM_SIZE_32KB;
155 FLASH_BANKSWITCH_MODE
164 flash_mode_type flash_mode = FLASH_BASE_MODE;
165 u32 flash_command_position = 0;
166 u8 *flash_bank_ptr = gamepak_backup;
168 flash_device_id_type flash_device_id = FLASH_DEVICE_MACRONIX_64KB;
169 flash_manufacturer_id_type flash_manufacturer_id =
170 FLASH_MANUFACTURER_MACRONIX;
171 flash_size_type flash_size = FLASH_SIZE_64KB;
173 u8 read_backup(u32 address)
177 if(backup_type == BACKUP_NONE)
178 backup_type = BACKUP_SRAM;
180 if(backup_type == BACKUP_SRAM)
182 value = gamepak_backup[address];
186 if(flash_mode == FLASH_ID_MODE)
188 /* ID manufacturer type */
189 if(address == 0x0000)
190 value = flash_manufacturer_id;
194 if(address == 0x0001)
195 value = flash_device_id;
199 value = flash_bank_ptr[address];
205 #define read_backup8() \
206 value = read_backup(address & 0xFFFF) \
208 #define read_backup16() \
211 #define read_backup32() \
215 // EEPROM is 512 bytes by default; it is autodetecte as 8KB if
216 // 14bit address DMAs are made (this is done in the DMA handler).
228 EEPROM_READ_HEADER_MODE,
231 EEPROM_WRITE_ADDRESS_MODE,
232 EEPROM_ADDRESS_FOOTER_MODE,
233 EEPROM_WRITE_FOOTER_MODE
237 eeprom_size_type eeprom_size = EEPROM_512_BYTE;
238 eeprom_mode_type eeprom_mode = EEPROM_BASE_MODE;
239 u32 eeprom_address_length;
240 u32 eeprom_address = 0;
241 s32 eeprom_counter = 0;
245 void function_cc write_eeprom(u32 address, u32 value)
249 case EEPROM_BASE_MODE:
250 backup_type = BACKUP_EEPROM;
251 eeprom_buffer[0] |= (value & 0x01) << (1 - eeprom_counter);
253 if(eeprom_counter == 2)
255 if(eeprom_size == EEPROM_512_BYTE)
256 eeprom_address_length = 6;
258 eeprom_address_length = 14;
262 switch(eeprom_buffer[0] & 0x03)
265 eeprom_mode = EEPROM_WRITE_ADDRESS_MODE;
269 eeprom_mode = EEPROM_ADDRESS_MODE;
272 address16(eeprom_buffer, 0) = 0;
276 case EEPROM_ADDRESS_MODE:
277 case EEPROM_WRITE_ADDRESS_MODE:
278 eeprom_buffer[eeprom_counter / 8]
279 |= (value & 0x01) << (7 - (eeprom_counter % 8));
281 if(eeprom_counter == eeprom_address_length)
283 if(eeprom_size == EEPROM_512_BYTE)
286 (address16(eeprom_buffer, 0) >> 2) * 8;
290 eeprom_address = (((u32)eeprom_buffer[1] >> 2) |
291 ((u32)eeprom_buffer[0] << 6)) * 8;
294 address16(eeprom_buffer, 0) = 0;
297 if(eeprom_mode == EEPROM_ADDRESS_MODE)
299 eeprom_mode = EEPROM_ADDRESS_FOOTER_MODE;
303 eeprom_mode = EEPROM_WRITE_MODE;
304 memset(gamepak_backup + eeprom_address, 0, 8);
309 case EEPROM_WRITE_MODE:
310 gamepak_backup[eeprom_address + (eeprom_counter / 8)] |=
311 (value & 0x01) << (7 - (eeprom_counter % 8));
313 if(eeprom_counter == 64)
315 backup_update = write_backup_delay;
317 eeprom_mode = EEPROM_WRITE_FOOTER_MODE;
321 case EEPROM_ADDRESS_FOOTER_MODE:
322 case EEPROM_WRITE_FOOTER_MODE:
324 if(eeprom_mode == EEPROM_ADDRESS_FOOTER_MODE)
326 eeprom_mode = EEPROM_READ_HEADER_MODE;
330 eeprom_mode = EEPROM_BASE_MODE;
336 #define read_memory_gamepak(type) \
337 u32 gamepak_index = address >> 15; \
338 u8 *map = memory_map_read[gamepak_index]; \
341 map = load_gamepak_page(gamepak_index & 0x3FF); \
343 value = address##type(map, address & 0x7FFF) \
345 #define read_open8() \
346 if(!(reg[REG_CPSR] & 0x20)) \
347 value = read_memory8(reg[REG_PC] + 4 + (address & 0x03)); \
349 value = read_memory8(reg[REG_PC] + 2 + (address & 0x01)) \
351 #define read_open16() \
352 if(!(reg[REG_CPSR] & 0x20)) \
353 value = read_memory16(reg[REG_PC] + 4 + (address & 0x02)); \
355 value = read_memory16(reg[REG_PC] + 2) \
357 #define read_open32() \
358 if(!(reg[REG_CPSR] & 0x20)) \
360 value = read_memory32(reg[REG_PC] + 4); \
364 u32 current_instruction = read_memory16(reg[REG_PC] + 2); \
365 value = current_instruction | (current_instruction << 16); \
368 u32 function_cc read_eeprom()
374 case EEPROM_BASE_MODE:
378 case EEPROM_READ_MODE:
379 value = (gamepak_backup[eeprom_address + (eeprom_counter / 8)] >>
380 (7 - (eeprom_counter % 8))) & 0x01;
382 if(eeprom_counter == 64)
385 eeprom_mode = EEPROM_BASE_MODE;
389 case EEPROM_READ_HEADER_MODE:
392 if(eeprom_counter == 4)
394 eeprom_mode = EEPROM_READ_MODE;
408 #define read_memory(type) \
409 switch(address >> 24) \
413 if(reg[REG_PC] >= 0x4000) \
414 value = address##type(&bios_read_protect, address & 0x03); \
416 value = address##type(bios_rom, address & 0x3FFF); \
420 /* external work RAM */ \
421 address = (address & 0x7FFF) + ((address & 0x38000) * 2) + 0x8000; \
422 value = address##type(ewram, address); \
426 /* internal work RAM */ \
427 value = address##type(iwram, (address & 0x7FFF) + 0x8000); \
431 /* I/O registers */ \
432 value = address##type(io_registers, address & 0x3FF); \
437 value = address##type(palette_ram, address & 0x3FF); \
442 address &= 0x1FFFF; \
443 if(address > 0x18000) \
446 value = address##type(vram, address); \
451 value = address##type(oam_ram, address & 0x3FF); \
460 if((address & 0x1FFFFFF) >= gamepak_size) \
466 read_memory_gamepak(type); \
471 if((address & 0x1FFFFFF) < gamepak_size) \
473 read_memory_gamepak(type); \
477 value = read_eeprom(); \
484 read_backup##type(); \
492 #define trigger_dma(dma_number) \
495 if(dma[dma_number].start_type == DMA_INACTIVE) \
497 u32 start_type = (value >> 12) & 0x03; \
498 u32 dest_address = address32(io_registers, (dma_number * 12) + 0xB4) & \
501 dma[dma_number].dma_channel = dma_number; \
502 dma[dma_number].source_address = \
503 address32(io_registers, (dma_number * 12) + 0xB0) & 0xFFFFFFF; \
504 dma[dma_number].dest_address = dest_address; \
505 dma[dma_number].source_direction = (value >> 7) & 0x03; \
506 dma[dma_number].repeat_type = (value >> 9) & 0x01; \
507 dma[dma_number].start_type = start_type; \
508 dma[dma_number].irq = (value >> 14) & 0x01; \
510 /* If it is sound FIFO DMA make sure the settings are a certain way */ \
511 if((dma_number >= 1) && (dma_number <= 2) && \
512 (start_type == DMA_START_SPECIAL)) \
514 dma[dma_number].length_type = DMA_32BIT; \
515 dma[dma_number].length = 4; \
516 dma[dma_number].dest_direction = DMA_FIXED; \
517 if(dest_address == 0x40000A4) \
518 dma[dma_number].direct_sound_channel = DMA_DIRECT_SOUND_B; \
520 dma[dma_number].direct_sound_channel = DMA_DIRECT_SOUND_A; \
525 address16(io_registers, (dma_number * 12) + 0xB8); \
527 if((dma_number == 3) && ((dest_address >> 24) == 0x0D) && \
528 ((length & 0x1F) == 17)) \
530 eeprom_size = EEPROM_8_KBYTE; \
538 if(dma_number == 3) \
544 dma[dma_number].length = length; \
545 dma[dma_number].length_type = (value >> 10) & 0x01; \
546 dma[dma_number].dest_direction = (value >> 5) & 0x03; \
549 address16(io_registers, (dma_number * 12) + 0xBA) = value; \
550 if(start_type == DMA_START_IMMEDIATELY) \
551 return dma_transfer(dma + dma_number); \
556 dma[dma_number].start_type = DMA_INACTIVE; \
557 dma[dma_number].direct_sound_channel = DMA_NO_DIRECT_SOUND; \
558 address16(io_registers, (dma_number * 12) + 0xBA) = value; \
562 #define access_register8_high(address) \
563 value = (value << 8) | (address8(io_registers, address)) \
565 #define access_register8_low(address) \
566 value = ((address8(io_registers, address + 1)) << 8) | value \
568 #define access_register16_high(address) \
569 value = (value << 16) | (address16(io_registers, address)) \
571 #define access_register16_low(address) \
572 value = ((address16(io_registers, address + 2)) << 16) | value \
574 cpu_alert_type function_cc write_io_register8(u32 address, u32 value)
580 u32 dispcnt = io_registers[REG_DISPCNT];
582 if((value & 0x07) != (dispcnt & 0x07))
585 address8(io_registers, 0x00) = value;
589 // DISPSTAT (lower byte)
591 address8(io_registers, 0x04) =
592 (address8(io_registers, 0x04) & 0x07) | (value & ~0x07);
602 access_register8_low(0x28);
603 access_register16_low(0x28);
604 affine_reference_x[0] = (s32)(value << 4) >> 4;
605 address32(io_registers, 0x28) = value;
609 access_register8_high(0x28);
610 access_register16_low(0x28);
611 affine_reference_x[0] = (s32)(value << 4) >> 4;
612 address32(io_registers, 0x28) = value;
616 access_register8_low(0x2A);
617 access_register16_high(0x28);
618 affine_reference_x[0] = (s32)(value << 4) >> 4;
619 address32(io_registers, 0x28) = value;
623 access_register8_high(0x2A);
624 access_register16_high(0x28);
625 affine_reference_x[0] = (s32)(value << 4) >> 4;
626 address32(io_registers, 0x28) = value;
631 access_register8_low(0x2C);
632 access_register16_low(0x2C);
633 affine_reference_y[0] = (s32)(value << 4) >> 4;
634 address32(io_registers, 0x2C) = value;
638 access_register8_high(0x2C);
639 access_register16_low(0x2C);
640 affine_reference_y[0] = (s32)(value << 4) >> 4;
641 address32(io_registers, 0x2C) = value;
645 access_register8_low(0x2E);
646 access_register16_high(0x2C);
647 affine_reference_y[0] = (s32)(value << 4) >> 4;
648 address32(io_registers, 0x2C) = value;
652 access_register8_high(0x2E);
653 access_register16_high(0x2C);
654 affine_reference_y[0] = (s32)(value << 4) >> 4;
655 address32(io_registers, 0x2C) = value;
660 access_register8_low(0x38);
661 access_register16_low(0x38);
662 affine_reference_x[1] = (s32)(value << 4) >> 4;
663 address32(io_registers, 0x38) = value;
667 access_register8_high(0x38);
668 access_register16_low(0x38);
669 affine_reference_x[1] = (s32)(value << 4) >> 4;
670 address32(io_registers, 0x38) = value;
674 access_register8_low(0x3A);
675 access_register16_high(0x38);
676 affine_reference_x[1] = (s32)(value << 4) >> 4;
677 address32(io_registers, 0x38) = value;
681 access_register8_high(0x3A);
682 access_register16_high(0x38);
683 affine_reference_x[1] = (s32)(value << 4) >> 4;
684 address32(io_registers, 0x38) = value;
689 access_register8_low(0x3C);
690 access_register16_low(0x3C);
691 affine_reference_y[1] = (s32)(value << 4) >> 4;
692 address32(io_registers, 0x3C) = value;
696 access_register8_high(0x3C);
697 access_register16_low(0x3C);
698 affine_reference_y[1] = (s32)(value << 4) >> 4;
699 address32(io_registers, 0x3C) = value;
703 access_register8_low(0x3E);
704 access_register16_high(0x3C);
705 affine_reference_y[1] = (s32)(value << 4) >> 4;
706 address32(io_registers, 0x3C) = value;
710 access_register8_high(0x3E);
711 access_register16_high(0x3C);
712 affine_reference_y[1] = (s32)(value << 4) >> 4;
713 address32(io_registers, 0x3C) = value;
716 // Sound 1 control sweep
718 access_register8_low(0x60);
719 gbc_sound_tone_control_sweep();
723 access_register8_low(0x60);
724 gbc_sound_tone_control_sweep();
727 // Sound 1 control duty/length/envelope
729 access_register8_low(0x62);
730 gbc_sound_tone_control_low(0, 0x62);
734 access_register8_high(0x62);
735 gbc_sound_tone_control_low(0, 0x62);
738 // Sound 1 control frequency
740 access_register8_low(0x64);
741 gbc_sound_tone_control_high(0, 0x64);
745 access_register8_high(0x64);
746 gbc_sound_tone_control_high(0, 0x64);
749 // Sound 2 control duty/length/envelope
751 access_register8_low(0x68);
752 gbc_sound_tone_control_low(1, 0x68);
756 access_register8_high(0x68);
757 gbc_sound_tone_control_low(1, 0x68);
760 // Sound 2 control frequency
762 access_register8_low(0x6C);
763 gbc_sound_tone_control_high(1, 0x6C);
767 access_register8_high(0x6C);
768 gbc_sound_tone_control_high(1, 0x6C);
771 // Sound 3 control wave
773 access_register8_low(0x70);
774 gbc_sound_wave_control();
778 access_register8_high(0x70);
779 gbc_sound_wave_control();
782 // Sound 3 control length/volume
784 access_register8_low(0x72);
785 gbc_sound_tone_control_low_wave();
789 access_register8_high(0x72);
790 gbc_sound_tone_control_low_wave();
793 // Sound 3 control frequency
795 access_register8_low(0x74);
796 gbc_sound_tone_control_high_wave();
800 access_register8_high(0x74);
801 gbc_sound_tone_control_high_wave();
804 // Sound 4 control length/envelope
806 access_register8_low(0x78);
807 gbc_sound_tone_control_low(3, 0x78);
811 access_register8_high(0x78);
812 gbc_sound_tone_control_low(3, 0x78);
815 // Sound 4 control frequency
817 access_register8_low(0x7C);
818 gbc_sound_noise_control();
822 access_register8_high(0x7C);
823 gbc_sound_noise_control();
828 access_register8_low(0x80);
833 access_register8_high(0x80);
839 access_register8_low(0x82);
844 access_register8_high(0x82);
855 gbc_sound_wave_update = 1;
856 address8(io_registers, address) = value;
861 sound_timer_queue8(0, value);
866 sound_timer_queue8(1, value);
869 // DMA control (trigger byte)
871 access_register8_low(0xBA);
876 access_register8_low(0xC6);
881 access_register8_low(0xD2);
886 access_register8_low(0xDE);
892 access_register8_low(0x100);
897 access_register8_high(0x100);
902 access_register8_low(0x104);
907 access_register8_high(0x104);
912 access_register8_low(0x108);
917 access_register8_high(0x108);
922 access_register8_low(0x10C);
927 access_register8_high(0x10C);
931 // Timer control (trigger byte)
933 access_register8_low(0x102);
938 access_register8_low(0x106);
943 access_register8_low(0x10A);
948 access_register8_low(0x10E);
954 address8(io_registers, 0x202) &= ~value;
958 address8(io_registers, 0x203) &= ~value;
963 if((value & 0x01) == 0)
964 reg[CPU_HALT_STATE] = CPU_HALT;
966 reg[CPU_HALT_STATE] = CPU_STOP;
968 return CPU_ALERT_HALT;
972 address8(io_registers, address) = value;
976 return CPU_ALERT_NONE;
979 cpu_alert_type function_cc write_io_register16(u32 address, u32 value)
985 u32 dispcnt = io_registers[REG_DISPCNT];
986 if((value & 0x07) != (dispcnt & 0x07))
989 address16(io_registers, 0x00) = value;
995 address16(io_registers, 0x04) =
996 (address16(io_registers, 0x04) & 0x07) | (value & ~0x07);
1005 access_register16_low(0x28);
1006 affine_reference_x[0] = (s32)(value << 4) >> 4;
1007 address32(io_registers, 0x28) = value;
1011 access_register16_high(0x28);
1012 affine_reference_x[0] = (s32)(value << 4) >> 4;
1013 address32(io_registers, 0x28) = value;
1018 access_register16_low(0x2C);
1019 affine_reference_y[0] = (s32)(value << 4) >> 4;
1020 address32(io_registers, 0x2C) = value;
1024 access_register16_high(0x2C);
1025 affine_reference_y[0] = (s32)(value << 4) >> 4;
1026 address32(io_registers, 0x2C) = value;
1032 access_register16_low(0x38);
1033 affine_reference_x[1] = (s32)(value << 4) >> 4;
1034 address32(io_registers, 0x38) = value;
1038 access_register16_high(0x38);
1039 affine_reference_x[1] = (s32)(value << 4) >> 4;
1040 address32(io_registers, 0x38) = value;
1045 access_register16_low(0x3C);
1046 affine_reference_y[1] = (s32)(value << 4) >> 4;
1047 address32(io_registers, 0x3C) = value;
1051 access_register16_high(0x3C);
1052 affine_reference_y[1] = (s32)(value << 4) >> 4;
1053 address32(io_registers, 0x3C) = value;
1056 // Sound 1 control sweep
1058 gbc_sound_tone_control_sweep();
1061 // Sound 1 control duty/length/envelope
1063 gbc_sound_tone_control_low(0, 0x62);
1066 // Sound 1 control frequency
1068 gbc_sound_tone_control_high(0, 0x64);
1071 // Sound 2 control duty/length/envelope
1073 gbc_sound_tone_control_low(1, 0x68);
1076 // Sound 2 control frequency
1078 gbc_sound_tone_control_high(1, 0x6C);
1081 // Sound 3 control wave
1083 gbc_sound_wave_control();
1086 // Sound 3 control length/volume
1088 gbc_sound_tone_control_low_wave();
1091 // Sound 3 control frequency
1093 gbc_sound_tone_control_high_wave();
1096 // Sound 4 control length/envelope
1098 gbc_sound_tone_control_low(3, 0x78);
1101 // Sound 4 control frequency
1103 gbc_sound_noise_control();
1108 gbc_trigger_sound();
1123 gbc_sound_wave_update = 1;
1124 address16(io_registers, address) = value;
1129 sound_timer_queue16(0, value);
1134 sound_timer_queue16(1, value);
1194 address16(io_registers, 0x202) &= ~value;
1203 if(((value >> 8) & 0x01) == 0)
1204 reg[CPU_HALT_STATE] = CPU_HALT;
1206 reg[CPU_HALT_STATE] = CPU_STOP;
1208 return CPU_ALERT_HALT;
1211 address16(io_registers, address) = value;
1215 return CPU_ALERT_NONE;
1219 cpu_alert_type function_cc write_io_register32(u32 address, u32 value)
1225 affine_reference_x[0] = (s32)(value << 4) >> 4;
1226 address32(io_registers, 0x28) = value;
1231 affine_reference_y[0] = (s32)(value << 4) >> 4;
1232 address32(io_registers, 0x2C) = value;
1237 affine_reference_x[1] = (s32)(value << 4) >> 4;
1238 address32(io_registers, 0x38) = value;
1243 affine_reference_y[1] = (s32)(value << 4) >> 4;
1244 address32(io_registers, 0x3C) = value;
1249 sound_timer_queue32(0, value);
1254 sound_timer_queue32(1, value);
1259 cpu_alert_type alert_low =
1260 write_io_register16(address, value & 0xFFFF);
1262 cpu_alert_type alert_high =
1263 write_io_register16(address + 2, value >> 16);
1272 return CPU_ALERT_NONE;
1275 #define write_palette8(address, value) \
1277 #define write_palette16(address, value) \
1279 u32 palette_address = address; \
1280 address16(palette_ram, palette_address) = value; \
1281 convert_palette(value); \
1282 address16(palette_ram_converted, palette_address) = value; \
1285 #define write_palette32(address, value) \
1287 u32 palette_address = address; \
1288 u32 value_high = value >> 16; \
1289 u32 value_low = value & 0xFFFF; \
1290 address32(palette_ram, palette_address) = value; \
1291 convert_palette(value_high); \
1292 convert_palette(value_low); \
1293 value = (value_high << 16) | value_low; \
1294 address32(palette_ram_converted, palette_address) = value; \
1298 void function_cc write_backup(u32 address, u32 value)
1302 if(backup_type == BACKUP_NONE)
1303 backup_type = BACKUP_SRAM;
1306 // gamepak SRAM or Flash ROM
1307 if((address == 0x5555) && (flash_mode != FLASH_WRITE_MODE))
1309 if((flash_command_position == 0) && (value == 0xAA))
1311 backup_type = BACKUP_FLASH;
1312 flash_command_position = 1;
1315 if(flash_command_position == 2)
1320 // Enter ID mode, this also tells the emulator that we're using
1323 if(flash_mode == FLASH_BASE_MODE)
1324 flash_mode = FLASH_ID_MODE;
1330 if(flash_mode == FLASH_BASE_MODE)
1331 flash_mode = FLASH_ERASE_MODE;
1335 // Terminate ID mode
1336 if(flash_mode == FLASH_ID_MODE)
1337 flash_mode = FLASH_BASE_MODE;
1342 if(flash_mode == FLASH_BASE_MODE)
1343 flash_mode = FLASH_WRITE_MODE;
1348 // Here the chip is now officially 128KB.
1349 flash_size = FLASH_SIZE_128KB;
1350 if(flash_mode == FLASH_BASE_MODE)
1351 flash_mode = FLASH_BANKSWITCH_MODE;
1356 if(flash_mode == FLASH_ERASE_MODE)
1358 if(flash_size == FLASH_SIZE_64KB)
1359 memset(gamepak_backup, 0xFF, 1024 * 64);
1361 memset(gamepak_backup, 0xFF, 1024 * 128);
1362 backup_update = write_backup_delay;
1363 flash_mode = FLASH_BASE_MODE;
1370 flash_command_position = 0;
1372 if(backup_type == BACKUP_SRAM)
1373 gamepak_backup[0x5555] = value;
1377 if((address == 0x2AAA) && (value == 0x55) &&
1378 (flash_command_position == 1))
1380 flash_command_position = 2;
1384 if((flash_command_position == 2) &&
1385 (flash_mode == FLASH_ERASE_MODE) && (value == 0x30))
1388 memset(flash_bank_ptr + (address & 0xF000), 0xFF, 1024 * 4);
1389 backup_update = write_backup_delay;
1390 flash_mode = FLASH_BASE_MODE;
1391 flash_command_position = 0;
1395 if((flash_command_position == 0) &&
1396 (flash_mode == FLASH_BANKSWITCH_MODE) && (address == 0x0000) &&
1397 (flash_size == FLASH_SIZE_128KB))
1399 flash_bank_ptr = gamepak_backup + ((value & 0x01) * (1024 * 64));
1400 flash_mode = FLASH_BASE_MODE;
1404 if((flash_command_position == 0) && (flash_mode == FLASH_WRITE_MODE))
1406 // Write value to flash ROM
1407 backup_update = write_backup_delay;
1408 flash_bank_ptr[address] = value;
1409 flash_mode = FLASH_BASE_MODE;
1413 if(backup_type == BACKUP_SRAM)
1415 // Write value to SRAM
1416 backup_update = write_backup_delay;
1417 // Hit 64KB territory?
1418 if(address >= 0x8000)
1419 sram_size = SRAM_SIZE_64KB;
1420 gamepak_backup[address] = value;
1425 #define write_backup8() \
1426 write_backup(address & 0xFFFF, value) \
1428 #define write_backup16() \
1430 #define write_backup32() \
1432 #define write_vram8() \
1434 address16(vram, address) = ((value << 8) | value) \
1436 #define write_vram16() \
1437 address16(vram, address) = value \
1439 #define write_vram32() \
1440 address32(vram, address) = value \
1442 // RTC code derived from VBA's (due to lack of any real publically available
1443 // documentation...)
1456 RTC_COMMAND_RESET = 0x60,
1457 RTC_COMMAND_WRITE_STATUS = 0x62,
1458 RTC_COMMAND_READ_STATUS = 0x63,
1459 RTC_COMMAND_OUTPUT_TIME_FULL = 0x65,
1460 RTC_COMMAND_OUTPUT_TIME = 0x67
1466 RTC_WRITE_TIME_FULL,
1468 } rtc_write_mode_type;
1470 rtc_state_type rtc_state = RTC_DISABLED;
1471 rtc_write_mode_type rtc_write_mode;
1472 u8 rtc_registers[3];
1475 u32 rtc_status = 0x40;
1479 u32 encode_bcd(u8 value)
1481 return ((value / 10) << 4) | (value % 10);
1484 #define write_rtc_register(index, _value) \
1485 update_address = 0x80000C4 + (index * 2); \
1486 rtc_registers[index] = _value; \
1487 rtc_page_index = update_address >> 15; \
1488 map = memory_map_read[rtc_page_index]; \
1491 map = load_gamepak_page(rtc_page_index & 0x3FF); \
1493 address16(map, update_address & 0x7FFF) = _value \
1495 void function_cc write_rtc(u32 address, u32 value)
1506 // Bit 0: SCHK, perform action
1507 // Bit 1: IO, input/output command data
1508 // Bit 2: CS, select input/output? If high make I/O write only
1510 if(rtc_state == RTC_DISABLED)
1511 rtc_state = RTC_IDLE;
1512 if(!(rtc_registers[0] & 0x04))
1513 value = (rtc_registers[0] & 0x02) | (value & ~0x02);
1514 if(rtc_registers[2] & 0x01)
1516 // To begin writing a command 1, 5 must be written to the command
1518 if((rtc_state == RTC_IDLE) && (rtc_registers[0] == 0x01) &&
1521 // We're now ready to begin receiving a command.
1522 write_rtc_register(0, value);
1523 rtc_state = RTC_COMMAND;
1529 write_rtc_register(0, value);
1532 // Accumulate RTC command by receiving the next bit, and if we
1533 // have accumulated enough bits to form a complete command
1536 if(rtc_registers[0] & 0x01)
1538 rtc_command |= ((value & 0x02) >> 1) << rtc_bit_count;
1542 // Have we received a full RTC command? If so execute it.
1543 if(rtc_bit_count < 0)
1548 case RTC_COMMAND_RESET:
1549 rtc_state = RTC_IDLE;
1550 memset(rtc_registers, 0, sizeof(rtc_registers));
1553 // Sets status of RTC
1554 case RTC_COMMAND_WRITE_STATUS:
1555 rtc_state = RTC_INPUT_DATA;
1557 rtc_write_mode = RTC_WRITE_STATUS;
1560 // Outputs current status of RTC
1561 case RTC_COMMAND_READ_STATUS:
1562 rtc_state = RTC_OUTPUT_DATA;
1564 rtc_data[0] = rtc_status;
1567 // Actually outputs the time, all of it
1568 case RTC_COMMAND_OUTPUT_TIME_FULL:
1570 struct tm *current_time;
1571 time_t current_time_flat;
1574 time(¤t_time_flat);
1575 current_time = localtime(¤t_time_flat);
1577 day_of_week = current_time->tm_wday;
1578 if(day_of_week == 0)
1583 rtc_state = RTC_OUTPUT_DATA;
1585 rtc_data[0] = encode_bcd(current_time->tm_year % 100);
1586 rtc_data[1] = encode_bcd(current_time->tm_mon + 1);
1587 rtc_data[2] = encode_bcd(current_time->tm_mday);
1588 rtc_data[3] = encode_bcd(day_of_week);
1589 rtc_data[4] = encode_bcd(current_time->tm_hour);
1590 rtc_data[5] = encode_bcd(current_time->tm_min);
1591 rtc_data[6] = encode_bcd(current_time->tm_sec);
1596 // Only outputs the current time of day.
1597 case RTC_COMMAND_OUTPUT_TIME:
1599 struct tm *current_time;
1600 time_t current_time_flat;
1602 time(¤t_time_flat);
1603 current_time = localtime(¤t_time_flat);
1605 rtc_state = RTC_OUTPUT_DATA;
1607 rtc_data[0] = encode_bcd(current_time->tm_hour);
1608 rtc_data[1] = encode_bcd(current_time->tm_min);
1609 rtc_data[2] = encode_bcd(current_time->tm_sec);
1617 // Receive parameters from the game as input to the RTC
1618 // for a given command. Read one bit at a time.
1619 case RTC_INPUT_DATA:
1620 // Bit 1 of parameter A must be high for input
1621 if(rtc_registers[1] & 0x02)
1623 // Read next bit for input
1626 rtc_data[rtc_bit_count >> 3] |=
1627 ((value & 0x01) << (7 - (rtc_bit_count & 0x07)));
1633 if(rtc_bit_count == (rtc_data_bytes * 8))
1635 rtc_state = RTC_IDLE;
1636 switch(rtc_write_mode)
1638 case RTC_WRITE_STATUS:
1639 rtc_status = rtc_data[0];
1647 case RTC_OUTPUT_DATA:
1648 // Bit 1 of parameter A must be low for output
1649 if(!(rtc_registers[1] & 0x02))
1651 // Write next bit to output, on bit 1 of parameter B
1654 u8 current_output_byte = rtc_registers[2];
1656 current_output_byte =
1657 (current_output_byte & ~0x02) |
1658 (((rtc_data[rtc_bit_count >> 3] >>
1659 (rtc_bit_count & 0x07)) & 0x01) << 1);
1661 write_rtc_register(0, current_output_byte);
1668 if(rtc_bit_count == (rtc_data_bytes * 8))
1670 rtc_state = RTC_IDLE;
1671 memset(rtc_registers, 0, sizeof(rtc_registers));
1681 write_rtc_register(2, value);
1685 // Write parameter A
1687 write_rtc_register(1, value);
1690 // Write parameter B
1692 write_rtc_register(2, value);
1697 #define write_rtc8() \
1699 #define write_rtc16() \
1700 write_rtc(address & 0xFF, value) \
1702 #define write_rtc32() \
1704 #define write_memory(type) \
1705 switch(address >> 24) \
1708 /* external work RAM */ \
1709 address = (address & 0x7FFF) + ((address & 0x38000) * 2) + 0x8000; \
1710 address##type(ewram, address) = value; \
1714 /* internal work RAM */ \
1715 address##type(iwram, (address & 0x7FFF) + 0x8000) = value; \
1719 /* I/O registers */ \
1720 return write_io_register##type(address & 0x3FF, value); \
1724 write_palette##type(address & 0x3FF, value); \
1729 address &= 0x1FFFF; \
1730 if(address >= 0x18000) \
1731 address -= 0x8000; \
1733 write_vram##type(); \
1739 address##type(oam_ram, address & 0x3FF) = value; \
1743 /* gamepak ROM or RTC */ \
1744 write_rtc##type(); \
1747 case 0x09 ... 0x0C: \
1748 /* gamepak ROM space */ \
1752 write_eeprom(address, value); \
1756 write_backup##type(); \
1760 u8 function_cc read_memory8(u32 address)
1767 u16 function_cc read_memory16_signed(u32 address)
1773 return (s8)read_memory8(address);
1783 // unaligned reads are actually 32bit
1785 u32 function_cc read_memory16(u32 address)
1793 ror(value, value, 8);
1804 u32 function_cc read_memory32(u32 address)
1809 u32 rotate = (address & 0x03) * 8;
1812 ror(value, value, rotate);
1822 cpu_alert_type function_cc write_memory8(u32 address, u8 value)
1825 return CPU_ALERT_NONE;
1828 cpu_alert_type function_cc write_memory16(u32 address, u16 value)
1831 return CPU_ALERT_NONE;
1834 cpu_alert_type function_cc write_memory32(u32 address, u32 value)
1837 return CPU_ALERT_NONE;
1840 char backup_filename[512];
1842 u32 load_backup(char *name)
1844 file_open(backup_file, name, read);
1846 if(file_check_valid(backup_file))
1848 u32 backup_size = file_length(name, backup_file);
1850 file_read(backup_file, gamepak_backup, backup_size);
1852 file_close(backup_file);
1854 // The size might give away what kind of backup it is.
1858 backup_type = BACKUP_EEPROM;
1859 eeprom_size = EEPROM_512_BYTE;
1863 backup_type = BACKUP_EEPROM;
1864 eeprom_size = EEPROM_8_KBYTE;
1868 backup_type = BACKUP_SRAM;
1869 sram_size = SRAM_SIZE_32KB;
1872 // Could be either flash or SRAM, go with flash
1874 backup_type = BACKUP_FLASH;
1875 sram_size = FLASH_SIZE_64KB;
1879 backup_type = BACKUP_FLASH;
1880 flash_size = FLASH_SIZE_128KB;
1887 backup_type = BACKUP_NONE;
1888 memset(gamepak_backup, 0xFF, 1024 * 128);
1894 u32 save_backup(char *name)
1896 if(backup_type != BACKUP_NONE)
1898 file_open(backup_file, name, write);
1900 if(file_check_valid(backup_file))
1907 if(sram_size == SRAM_SIZE_32KB)
1908 backup_size = 0x8000;
1910 backup_size = 0x10000;
1914 if(flash_size == FLASH_SIZE_64KB)
1915 backup_size = 0x10000;
1917 backup_size = 0x20000;
1921 if(eeprom_size == EEPROM_512_BYTE)
1922 backup_size = 0x200;
1924 backup_size = 0x2000;
1928 file_write(backup_file, gamepak_backup, backup_size);
1930 file_close(backup_file);
1938 void update_backup()
1940 if(backup_update != (write_backup_delay + 1))
1943 if(backup_update == 0)
1945 save_backup(backup_filename);
1946 backup_update = write_backup_delay + 1;
1950 void update_backup_force()
1952 save_backup(backup_filename);
1955 #define CONFIG_FILENAME "game_config.txt"
1957 u8 *skip_spaces(u8 *line_ptr)
1959 while(*line_ptr == ' ')
1965 s32 parse_config_line(u8 *current_line, u8 *current_variable, u8 *current_value)
1967 u8 *line_ptr = current_line;
1970 if((current_line[0] == 0) || (current_line[0] == '#'))
1973 line_ptr_new = strchr(line_ptr, ' ');
1974 if(line_ptr_new == NULL)
1978 strcpy(current_variable, line_ptr);
1979 line_ptr_new = skip_spaces(line_ptr_new + 1);
1981 if(*line_ptr_new != '=')
1984 line_ptr_new = skip_spaces(line_ptr_new + 1);
1985 strcpy(current_value, line_ptr_new);
1986 line_ptr_new = current_value + strlen(current_value) - 1;
1987 if(*line_ptr_new == '\n')
1993 if(*line_ptr_new == '\r')
1999 s32 load_game_config(u8 *gamepak_title, u8 *gamepak_code, u8 *gamepak_maker)
2001 u8 current_line[256];
2002 u8 current_variable[256];
2003 u8 current_value[256];
2004 u8 config_path[512];
2009 idle_loop_target_pc = 0xFFFFFFFF;
2010 iwram_stack_optimize = 1;
2011 bios_rom[0x39] = 0x00;
2012 bios_rom[0x2C] = 0x00;
2013 translation_gate_targets = 0;
2014 flash_device_id = FLASH_DEVICE_MACRONIX_64KB;
2016 #if (defined(PSP_BUILD) || defined(ARM_ARCH)) && !defined(_WIN32_WCE)
2017 sprintf(config_path, "%s/%s", main_path, CONFIG_FILENAME);
2019 sprintf(config_path, "%s\\%s", main_path, CONFIG_FILENAME);
2022 config_file = fopen(config_path, "rb");
2026 while(fgets(current_line, 256, config_file))
2028 if(parse_config_line(current_line, current_variable, current_value)
2031 if(strcmp(current_variable, "game_name") ||
2032 strcmp(current_value, gamepak_title))
2035 if(!fgets(current_line, 256, config_file) ||
2036 (parse_config_line(current_line, current_variable,
2037 current_value) == -1) ||
2038 strcmp(current_variable, "game_code") ||
2039 strcmp(current_value, gamepak_code))
2042 if(!fgets(current_line, 256, config_file) ||
2043 (parse_config_line(current_line, current_variable,
2044 current_value) == -1) ||
2045 strcmp(current_variable, "vender_code") ||
2046 strcmp(current_value, gamepak_maker))
2049 while(fgets(current_line, 256, config_file))
2051 if(parse_config_line(current_line, current_variable, current_value)
2054 if(!strcmp(current_variable, "game_name"))
2056 fclose(config_file);
2060 if(!strcmp(current_variable, "idle_loop_eliminate_target"))
2061 idle_loop_target_pc = strtol(current_value, NULL, 16);
2063 if(!strcmp(current_variable, "translation_gate_target"))
2065 if(translation_gate_targets < MAX_TRANSLATION_GATES)
2067 translation_gate_target_pc[translation_gate_targets] =
2068 strtol(current_value, NULL, 16);
2069 translation_gate_targets++;
2073 if(!strcmp(current_variable, "iwram_stack_optimize") &&
2074 !strcmp(current_value, "no"))
2076 iwram_stack_optimize = 0;
2079 if(!strcmp(current_variable, "flash_rom_type") &&
2080 !strcmp(current_value, "128KB"))
2082 flash_device_id = FLASH_DEVICE_MACRONIX_128KB;
2085 if(!strcmp(current_variable, "bios_rom_hack_39") &&
2086 !strcmp(current_value, "yes"))
2088 bios_rom[0x39] = 0xC0;
2091 if(!strcmp(current_variable, "bios_rom_hack_2C") &&
2092 !strcmp(current_value, "yes"))
2094 bios_rom[0x2C] = 0x02;
2099 fclose(config_file);
2104 fclose(config_file);
2110 s32 load_gamepak_raw(char *name)
2112 file_open(gamepak_file, name, read);
2114 if(file_check_valid(gamepak_file))
2116 u32 file_size = file_length(name, gamepak_file);
2118 // First, close the last one if it was open, we won't
2119 // be needing it anymore.
2120 if(file_check_valid(gamepak_file_large))
2121 file_close(gamepak_file_large);
2123 // If it's a big file size keep it, don't close it, we'll
2124 // probably want to load from it more later.
2125 if(file_size <= gamepak_ram_buffer_size)
2127 file_read(gamepak_file, gamepak_rom, file_size);
2129 file_close(gamepak_file);
2132 gamepak_file_large = -1;
2134 gamepak_file_large = NULL;
2139 // Read in just enough for the header
2140 file_read(gamepak_file, gamepak_rom, 0x100);
2141 gamepak_file_large = gamepak_file;
2150 u8 gamepak_title[13];
2152 u8 gamepak_maker[3];
2153 u8 gamepak_filename[512];
2155 u32 load_gamepak(char *name)
2157 char *dot_position = strrchr(name, '.');
2159 u8 cheats_filename[256];
2161 if(!strcmp(dot_position, ".zip"))
2162 file_size = load_file_zip(name);
2164 file_size = load_gamepak_raw(name);
2166 // A dumb April fool's joke was here once :o
2170 gamepak_size = (file_size + 0x7FFF) & ~0x7FFF;
2172 strcpy(backup_filename, name);
2173 strncpy(gamepak_filename, name, 512);
2174 change_ext(gamepak_filename, backup_filename, ".sav");
2176 load_backup(backup_filename);
2178 memcpy(gamepak_title, gamepak_rom + 0xA0, 12);
2179 memcpy(gamepak_code, gamepak_rom + 0xAC, 4);
2180 memcpy(gamepak_maker, gamepak_rom + 0xB0, 2);
2181 gamepak_title[12] = 0;
2182 gamepak_code[4] = 0;
2183 gamepak_maker[2] = 0;
2185 load_game_config(gamepak_title, gamepak_code, gamepak_maker);
2186 load_game_config_file();
2188 change_ext(gamepak_filename, cheats_filename, ".cht");
2189 add_cheats(cheats_filename);
2197 s32 load_bios(char *name)
2199 file_open(bios_file, name, read);
2201 if(file_check_valid(bios_file))
2203 file_read(bios_file, bios_rom, 0x4000);
2205 // This is a hack to get Zelda working, because emulating
2206 // the proper memory read behavior here is much too expensive.
2207 file_close(bios_file);
2214 // DMA memory regions can be one of the following:
2215 // IWRAM - 32kb offset from the contiguous iwram region.
2216 // EWRAM - like segmented but with self modifying code check.
2217 // VRAM - 96kb offset from the contiguous vram region, should take care
2218 // Palette RAM - Converts palette entries when written to.
2219 // OAM RAM - Sets OAM modified flag to true.
2220 // I/O registers - Uses the I/O register function.
2221 // of mirroring properly.
2222 // Segmented RAM/ROM - a region >= 32kb, the translated address has to
2223 // be reloaded if it wraps around the limit (cartride ROM)
2224 // Ext - should be handled by the memory read/write function.
2226 // The following map determines the region of each (assumes DMA access
2227 // is not done out of bounds)
2234 DMA_REGION_PALETTE_RAM,
2243 dma_region_type dma_region_map[16] =
2245 DMA_REGION_BIOS, // 0x00 - BIOS
2246 DMA_REGION_NULL, // 0x01 - Nothing
2247 DMA_REGION_EWRAM, // 0x02 - EWRAM
2248 DMA_REGION_IWRAM, // 0x03 - IWRAM
2249 DMA_REGION_IO, // 0x04 - I/O registers
2250 DMA_REGION_PALETTE_RAM, // 0x05 - palette RAM
2251 DMA_REGION_VRAM, // 0x06 - VRAM
2252 DMA_REGION_OAM_RAM, // 0x07 - OAM RAM
2253 DMA_REGION_GAMEPAK, // 0x08 - gamepak ROM
2254 DMA_REGION_GAMEPAK, // 0x09 - gamepak ROM
2255 DMA_REGION_GAMEPAK, // 0x0A - gamepak ROM
2256 DMA_REGION_GAMEPAK, // 0x0B - gamepak ROM
2257 DMA_REGION_GAMEPAK, // 0x0C - gamepak ROM
2258 DMA_REGION_EXT, // 0x0D - EEPROM
2259 DMA_REGION_EXT, // 0x0E - gamepak SRAM/flash ROM
2260 DMA_REGION_EXT // 0x0F - gamepak SRAM/flash ROM
2263 #define dma_adjust_ptr_inc(ptr, size) \
2266 #define dma_adjust_ptr_dec(ptr, size) \
2269 #define dma_adjust_ptr_fix(ptr, size) \
2271 #define dma_adjust_ptr_writeback() \
2272 dma->dest_address = dest_ptr \
2274 #define dma_adjust_ptr_reload() \
2276 #define dma_print(src_op, dest_op, transfer_size, wb) \
2277 printf("dma from %x (%s) to %x (%s) for %x (%s) (%s) (%d) (pc %x)\n", \
2278 src_ptr, #src_op, dest_ptr, #dest_op, length, #transfer_size, #wb, \
2279 dma->irq, reg[15]); \
2281 #define dma_smc_vars_src() \
2283 #define dma_smc_vars_dest() \
2284 u32 smc_trigger = 0 \
2286 #define dma_vars_iwram(type) \
2287 dma_smc_vars_##type() \
2289 #define dma_vars_vram(type) \
2291 #define dma_vars_palette_ram(type) \
2293 #define dma_oam_ram_src() \
2295 #define dma_oam_ram_dest() \
2298 #define dma_vars_oam_ram(type) \
2299 dma_oam_ram_##type() \
2301 #define dma_vars_io(type) \
2303 #define dma_segmented_load_src() \
2304 memory_map_read[src_current_region] \
2306 #define dma_segmented_load_dest() \
2307 memory_map_write[dest_current_region] \
2309 #define dma_vars_gamepak(type) \
2310 u32 type##_new_region; \
2311 u32 type##_current_region = type##_ptr >> 15; \
2312 u8 *type##_address_block = dma_segmented_load_##type(); \
2313 if(type##_address_block == NULL) \
2315 if((type##_ptr & 0x1FFFFFF) >= gamepak_size) \
2317 type##_address_block = load_gamepak_page(type##_current_region & 0x3FF); \
2320 #define dma_vars_ewram(type) \
2321 dma_smc_vars_##type(); \
2322 u32 type##_new_region; \
2323 u32 type##_current_region = type##_ptr >> 15; \
2324 u8 *type##_address_block = dma_segmented_load_##type() \
2326 #define dma_vars_bios(type) \
2328 #define dma_vars_ext(type) \
2330 #define dma_ewram_check_region(type) \
2331 type##_new_region = (type##_ptr >> 15); \
2332 if(type##_new_region != type##_current_region) \
2334 type##_current_region = type##_new_region; \
2335 type##_address_block = dma_segmented_load_##type(); \
2338 #define dma_gamepak_check_region(type) \
2339 type##_new_region = (type##_ptr >> 15); \
2340 if(type##_new_region != type##_current_region) \
2342 type##_current_region = type##_new_region; \
2343 type##_address_block = dma_segmented_load_##type(); \
2344 if(type##_address_block == NULL) \
2346 type##_address_block = \
2347 load_gamepak_page(type##_current_region & 0x3FF); \
2351 #define dma_read_iwram(type, transfer_size) \
2352 read_value = address##transfer_size(iwram + 0x8000, type##_ptr & 0x7FFF) \
2354 #define dma_read_vram(type, transfer_size) \
2355 read_value = address##transfer_size(vram, type##_ptr & 0x1FFFF) \
2357 #define dma_read_io(type, transfer_size) \
2358 read_value = address##transfer_size(io_registers, type##_ptr & 0x7FFF) \
2360 #define dma_read_oam_ram(type, transfer_size) \
2361 read_value = address##transfer_size(oam_ram, type##_ptr & 0x3FF) \
2363 #define dma_read_palette_ram(type, transfer_size) \
2364 read_value = address##transfer_size(palette_ram, type##_ptr & 0x3FF) \
2366 #define dma_read_ewram(type, transfer_size) \
2367 dma_ewram_check_region(type); \
2368 read_value = address##transfer_size(type##_address_block, \
2369 type##_ptr & 0x7FFF) \
2371 #define dma_read_gamepak(type, transfer_size) \
2372 dma_gamepak_check_region(type); \
2373 read_value = address##transfer_size(type##_address_block, \
2374 type##_ptr & 0x7FFF) \
2376 // DMAing from the BIOS is funny, just returns 0..
2378 #define dma_read_bios(type, transfer_size) \
2381 #define dma_read_ext(type, transfer_size) \
2382 read_value = read_memory##transfer_size(type##_ptr) \
2384 #define dma_write_iwram(type, transfer_size) \
2385 address##transfer_size(iwram + 0x8000, type##_ptr & 0x7FFF) = read_value; \
2386 smc_trigger |= address##transfer_size(iwram, type##_ptr & 0x7FFF) \
2388 #define dma_write_vram(type, transfer_size) \
2389 address##transfer_size(vram, type##_ptr & 0x1FFFF) = read_value \
2391 #define dma_write_io(type, transfer_size) \
2392 write_io_register##transfer_size(type##_ptr & 0x3FF, read_value) \
2394 #define dma_write_oam_ram(type, transfer_size) \
2395 address##transfer_size(oam_ram, type##_ptr & 0x3FF) = read_value \
2397 #define dma_write_palette_ram(type, transfer_size) \
2398 write_palette##transfer_size(type##_ptr & 0x3FF, read_value) \
2400 #define dma_write_ext(type, transfer_size) \
2401 write_memory##transfer_size(type##_ptr, read_value) \
2403 #define dma_write_ewram(type, transfer_size) \
2404 dma_ewram_check_region(type); \
2406 address##transfer_size(type##_address_block, type##_ptr & 0x7FFF) = \
2408 smc_trigger |= address##transfer_size(type##_address_block, \
2409 (type##_ptr & 0x7FFF) - 0x8000) \
2411 #define dma_epilogue_iwram() \
2414 /* Special return code indicating to retranslate to the CPU code */ \
2415 return_value = CPU_ALERT_SMC; \
2418 #define dma_epilogue_ewram() \
2421 /* Special return code indicating to retranslate to the CPU code */ \
2422 return_value = CPU_ALERT_SMC; \
2425 #define dma_epilogue_vram() \
2427 #define dma_epilogue_io() \
2429 #define dma_epilogue_oam_ram() \
2431 #define dma_epilogue_palette_ram() \
2433 #define dma_epilogue_GAMEPAK() \
2435 #define dma_epilogue_ext() \
2437 #define print_line() \
2438 dma_print(src_op, dest_op, transfer_size, wb); \
2440 #define dma_transfer_loop_region(src_region_type, dest_region_type, src_op, \
2441 dest_op, transfer_size, wb) \
2443 dma_vars_##src_region_type(src); \
2444 dma_vars_##dest_region_type(dest); \
2446 for(i = 0; i < length; i++) \
2448 dma_read_##src_region_type(src, transfer_size); \
2449 dma_write_##dest_region_type(dest, transfer_size); \
2450 dma_adjust_ptr_##src_op(src_ptr, transfer_size); \
2451 dma_adjust_ptr_##dest_op(dest_ptr, transfer_size); \
2453 dma->source_address = src_ptr; \
2454 dma_adjust_ptr_##wb(); \
2455 dma_epilogue_##dest_region_type(); \
2459 #define dma_transfer_loop(src_op, dest_op, transfer_size, wb); \
2461 u32 src_region = src_ptr >> 24; \
2462 u32 dest_region = dest_ptr >> 24; \
2463 dma_region_type src_region_type = dma_region_map[src_region]; \
2464 dma_region_type dest_region_type = dma_region_map[dest_region]; \
2466 switch(src_region_type | (dest_region_type << 4)) \
2468 case (DMA_REGION_BIOS | (DMA_REGION_IWRAM << 4)): \
2469 dma_transfer_loop_region(bios, iwram, src_op, dest_op, \
2470 transfer_size, wb); \
2472 case (DMA_REGION_IWRAM | (DMA_REGION_IWRAM << 4)): \
2473 dma_transfer_loop_region(iwram, iwram, src_op, dest_op, \
2474 transfer_size, wb); \
2476 case (DMA_REGION_EWRAM | (DMA_REGION_IWRAM << 4)): \
2477 dma_transfer_loop_region(ewram, iwram, src_op, dest_op, \
2478 transfer_size, wb); \
2480 case (DMA_REGION_VRAM | (DMA_REGION_IWRAM << 4)): \
2481 dma_transfer_loop_region(vram, iwram, src_op, dest_op, \
2482 transfer_size, wb); \
2484 case (DMA_REGION_PALETTE_RAM | (DMA_REGION_IWRAM << 4)): \
2485 dma_transfer_loop_region(palette_ram, iwram, src_op, dest_op, \
2486 transfer_size, wb); \
2488 case (DMA_REGION_OAM_RAM | (DMA_REGION_IWRAM << 4)): \
2489 dma_transfer_loop_region(oam_ram, iwram, src_op, dest_op, \
2490 transfer_size, wb); \
2492 case (DMA_REGION_IO | (DMA_REGION_IWRAM << 4)): \
2493 dma_transfer_loop_region(io, iwram, src_op, dest_op, \
2494 transfer_size, wb); \
2496 case (DMA_REGION_GAMEPAK | (DMA_REGION_IWRAM << 4)): \
2497 dma_transfer_loop_region(gamepak, iwram, src_op, dest_op, \
2498 transfer_size, wb); \
2500 case (DMA_REGION_EXT | (DMA_REGION_IWRAM << 4)): \
2501 dma_transfer_loop_region(ext, iwram, src_op, dest_op, \
2502 transfer_size, wb); \
2504 case (DMA_REGION_BIOS | (DMA_REGION_EWRAM << 4)): \
2505 dma_transfer_loop_region(bios, ewram, src_op, dest_op, \
2506 transfer_size, wb); \
2508 case (DMA_REGION_IWRAM | (DMA_REGION_EWRAM << 4)): \
2509 dma_transfer_loop_region(iwram, ewram, src_op, dest_op, \
2510 transfer_size, wb); \
2512 case (DMA_REGION_EWRAM | (DMA_REGION_EWRAM << 4)): \
2513 dma_transfer_loop_region(ewram, ewram, src_op, dest_op, \
2514 transfer_size, wb); \
2516 case (DMA_REGION_VRAM | (DMA_REGION_EWRAM << 4)): \
2517 dma_transfer_loop_region(vram, ewram, src_op, dest_op, \
2518 transfer_size, wb); \
2520 case (DMA_REGION_PALETTE_RAM | (DMA_REGION_EWRAM << 4)): \
2521 dma_transfer_loop_region(palette_ram, ewram, src_op, dest_op, \
2522 transfer_size, wb); \
2524 case (DMA_REGION_OAM_RAM | (DMA_REGION_EWRAM << 4)): \
2525 dma_transfer_loop_region(oam_ram, ewram, src_op, dest_op, \
2526 transfer_size, wb); \
2528 case (DMA_REGION_IO | (DMA_REGION_EWRAM << 4)): \
2529 dma_transfer_loop_region(io, ewram, src_op, dest_op, \
2530 transfer_size, wb); \
2532 case (DMA_REGION_GAMEPAK | (DMA_REGION_EWRAM << 4)): \
2533 dma_transfer_loop_region(gamepak, ewram, src_op, dest_op, \
2534 transfer_size, wb); \
2536 case (DMA_REGION_EXT | (DMA_REGION_EWRAM << 4)): \
2537 dma_transfer_loop_region(ext, ewram, src_op, dest_op, \
2538 transfer_size, wb); \
2540 case (DMA_REGION_BIOS | (DMA_REGION_VRAM << 4)): \
2541 dma_transfer_loop_region(bios, vram, src_op, dest_op, \
2542 transfer_size, wb); \
2544 case (DMA_REGION_IWRAM | (DMA_REGION_VRAM << 4)): \
2545 dma_transfer_loop_region(iwram, vram, src_op, dest_op, \
2546 transfer_size, wb); \
2548 case (DMA_REGION_EWRAM | (DMA_REGION_VRAM << 4)): \
2549 dma_transfer_loop_region(ewram, vram, src_op, dest_op, \
2550 transfer_size, wb); \
2552 case (DMA_REGION_VRAM | (DMA_REGION_VRAM << 4)): \
2553 dma_transfer_loop_region(vram, vram, src_op, dest_op, \
2554 transfer_size, wb); \
2556 case (DMA_REGION_PALETTE_RAM | (DMA_REGION_VRAM << 4)): \
2557 dma_transfer_loop_region(palette_ram, vram, src_op, dest_op, \
2558 transfer_size, wb); \
2560 case (DMA_REGION_OAM_RAM | (DMA_REGION_VRAM << 4)): \
2561 dma_transfer_loop_region(oam_ram, vram, src_op, dest_op, \
2562 transfer_size, wb); \
2564 case (DMA_REGION_IO | (DMA_REGION_VRAM << 4)): \
2565 dma_transfer_loop_region(io, vram, src_op, dest_op, \
2566 transfer_size, wb); \
2568 case (DMA_REGION_GAMEPAK | (DMA_REGION_VRAM << 4)): \
2569 dma_transfer_loop_region(gamepak, vram, src_op, dest_op, \
2570 transfer_size, wb); \
2572 case (DMA_REGION_EXT | (DMA_REGION_VRAM << 4)): \
2573 dma_transfer_loop_region(ext, vram, src_op, dest_op, \
2574 transfer_size, wb); \
2576 case (DMA_REGION_BIOS | (DMA_REGION_PALETTE_RAM << 4)): \
2577 dma_transfer_loop_region(bios, palette_ram, src_op, dest_op, \
2578 transfer_size, wb); \
2580 case (DMA_REGION_IWRAM | (DMA_REGION_PALETTE_RAM << 4)): \
2581 dma_transfer_loop_region(iwram, palette_ram, src_op, dest_op, \
2582 transfer_size, wb); \
2584 case (DMA_REGION_EWRAM | (DMA_REGION_PALETTE_RAM << 4)): \
2585 dma_transfer_loop_region(ewram, palette_ram, src_op, dest_op, \
2586 transfer_size, wb); \
2588 case (DMA_REGION_VRAM | (DMA_REGION_PALETTE_RAM << 4)): \
2589 dma_transfer_loop_region(vram, palette_ram, src_op, dest_op, \
2590 transfer_size, wb); \
2592 case (DMA_REGION_PALETTE_RAM | (DMA_REGION_PALETTE_RAM << 4)): \
2593 dma_transfer_loop_region(palette_ram, palette_ram, src_op, dest_op, \
2594 transfer_size, wb); \
2596 case (DMA_REGION_OAM_RAM | (DMA_REGION_PALETTE_RAM << 4)): \
2597 dma_transfer_loop_region(oam_ram, palette_ram, src_op, dest_op, \
2598 transfer_size, wb); \
2600 case (DMA_REGION_IO | (DMA_REGION_PALETTE_RAM << 4)): \
2601 dma_transfer_loop_region(io, palette_ram, src_op, dest_op, \
2602 transfer_size, wb); \
2604 case (DMA_REGION_GAMEPAK | (DMA_REGION_PALETTE_RAM << 4)): \
2605 dma_transfer_loop_region(gamepak, palette_ram, src_op, dest_op, \
2606 transfer_size, wb); \
2608 case (DMA_REGION_EXT | (DMA_REGION_PALETTE_RAM << 4)): \
2609 dma_transfer_loop_region(ext, palette_ram, src_op, dest_op, \
2610 transfer_size, wb); \
2612 case (DMA_REGION_BIOS | (DMA_REGION_OAM_RAM << 4)): \
2613 dma_transfer_loop_region(bios, oam_ram, src_op, dest_op, \
2614 transfer_size, wb); \
2616 case (DMA_REGION_IWRAM | (DMA_REGION_OAM_RAM << 4)): \
2617 dma_transfer_loop_region(iwram, oam_ram, src_op, dest_op, \
2618 transfer_size, wb); \
2620 case (DMA_REGION_EWRAM | (DMA_REGION_OAM_RAM << 4)): \
2621 dma_transfer_loop_region(ewram, oam_ram, src_op, dest_op, \
2622 transfer_size, wb); \
2624 case (DMA_REGION_VRAM | (DMA_REGION_OAM_RAM << 4)): \
2625 dma_transfer_loop_region(vram, oam_ram, src_op, dest_op, \
2626 transfer_size, wb); \
2628 case (DMA_REGION_PALETTE_RAM | (DMA_REGION_OAM_RAM << 4)): \
2629 dma_transfer_loop_region(palette_ram, oam_ram, src_op, dest_op, \
2630 transfer_size, wb); \
2632 case (DMA_REGION_OAM_RAM | (DMA_REGION_OAM_RAM << 4)): \
2633 dma_transfer_loop_region(oam_ram, oam_ram, src_op, dest_op, \
2634 transfer_size, wb); \
2636 case (DMA_REGION_IO | (DMA_REGION_OAM_RAM << 4)): \
2637 dma_transfer_loop_region(io, oam_ram, src_op, dest_op, \
2638 transfer_size, wb); \
2640 case (DMA_REGION_GAMEPAK | (DMA_REGION_OAM_RAM << 4)): \
2641 dma_transfer_loop_region(gamepak, oam_ram, src_op, dest_op, \
2642 transfer_size, wb); \
2644 case (DMA_REGION_EXT | (DMA_REGION_OAM_RAM << 4)): \
2645 dma_transfer_loop_region(ext, oam_ram, src_op, dest_op, \
2646 transfer_size, wb); \
2648 case (DMA_REGION_BIOS | (DMA_REGION_IO << 4)): \
2649 dma_transfer_loop_region(bios, io, src_op, dest_op, \
2650 transfer_size, wb); \
2652 case (DMA_REGION_IWRAM | (DMA_REGION_IO << 4)): \
2653 dma_transfer_loop_region(iwram, io, src_op, dest_op, \
2654 transfer_size, wb); \
2656 case (DMA_REGION_EWRAM | (DMA_REGION_IO << 4)): \
2657 dma_transfer_loop_region(ewram, io, src_op, dest_op, \
2658 transfer_size, wb); \
2660 case (DMA_REGION_VRAM | (DMA_REGION_IO << 4)): \
2661 dma_transfer_loop_region(vram, io, src_op, dest_op, \
2662 transfer_size, wb); \
2664 case (DMA_REGION_PALETTE_RAM | (DMA_REGION_IO << 4)): \
2665 dma_transfer_loop_region(palette_ram, io, src_op, dest_op, \
2666 transfer_size, wb); \
2668 case (DMA_REGION_OAM_RAM | (DMA_REGION_IO << 4)): \
2669 dma_transfer_loop_region(oam_ram, io, src_op, dest_op, \
2670 transfer_size, wb); \
2672 case (DMA_REGION_IO | (DMA_REGION_IO << 4)): \
2673 dma_transfer_loop_region(io, io, src_op, dest_op, \
2674 transfer_size, wb); \
2676 case (DMA_REGION_GAMEPAK | (DMA_REGION_IO << 4)): \
2677 dma_transfer_loop_region(gamepak, io, src_op, dest_op, \
2678 transfer_size, wb); \
2680 case (DMA_REGION_EXT | (DMA_REGION_IO << 4)): \
2681 dma_transfer_loop_region(ext, io, src_op, dest_op, \
2682 transfer_size, wb); \
2684 case (DMA_REGION_BIOS | (DMA_REGION_EXT << 4)): \
2685 dma_transfer_loop_region(bios, ext, src_op, dest_op, \
2686 transfer_size, wb); \
2688 case (DMA_REGION_IWRAM | (DMA_REGION_EXT << 4)): \
2689 dma_transfer_loop_region(iwram, ext, src_op, dest_op, \
2690 transfer_size, wb); \
2692 case (DMA_REGION_EWRAM | (DMA_REGION_EXT << 4)): \
2693 dma_transfer_loop_region(ewram, ext, src_op, dest_op, \
2694 transfer_size, wb); \
2696 case (DMA_REGION_VRAM | (DMA_REGION_EXT << 4)): \
2697 dma_transfer_loop_region(vram, ext, src_op, dest_op, \
2698 transfer_size, wb); \
2700 case (DMA_REGION_PALETTE_RAM | (DMA_REGION_EXT << 4)): \
2701 dma_transfer_loop_region(palette_ram, ext, src_op, dest_op, \
2702 transfer_size, wb); \
2704 case (DMA_REGION_OAM_RAM | (DMA_REGION_EXT << 4)): \
2705 dma_transfer_loop_region(oam_ram, ext, src_op, dest_op, \
2706 transfer_size, wb); \
2708 case (DMA_REGION_IO | (DMA_REGION_EXT << 4)): \
2709 dma_transfer_loop_region(io, ext, src_op, dest_op, \
2710 transfer_size, wb); \
2712 case (DMA_REGION_GAMEPAK | (DMA_REGION_EXT << 4)): \
2713 dma_transfer_loop_region(gamepak, ext, src_op, dest_op, \
2714 transfer_size, wb); \
2716 case (DMA_REGION_EXT | (DMA_REGION_EXT << 3)): \
2717 dma_transfer_loop_region(ext, ext, src_op, dest_op, \
2718 transfer_size, wb); \
2723 #define dma_transfer_expand(transfer_size) \
2724 switch((dma->dest_direction << 2) | dma->source_direction) \
2727 dma_transfer_loop(inc, inc, transfer_size, writeback); \
2730 dma_transfer_loop(dec, inc, transfer_size, writeback); \
2733 dma_transfer_loop(fix, inc, transfer_size, writeback); \
2739 dma_transfer_loop(inc, dec, transfer_size, writeback); \
2742 dma_transfer_loop(dec, dec, transfer_size, writeback); \
2745 dma_transfer_loop(fix, dec, transfer_size, writeback); \
2751 dma_transfer_loop(inc, fix, transfer_size, writeback); \
2754 dma_transfer_loop(dec, fix, transfer_size, writeback); \
2757 dma_transfer_loop(fix, fix, transfer_size, writeback); \
2763 dma_transfer_loop(inc, inc, transfer_size, reload); \
2766 dma_transfer_loop(dec, inc, transfer_size, reload); \
2769 dma_transfer_loop(fix, inc, transfer_size, reload); \
2775 cpu_alert_type dma_transfer(dma_transfer_type *dma)
2778 u32 length = dma->length;
2780 u32 src_ptr = dma->source_address;
2781 u32 dest_ptr = dma->dest_address;
2782 cpu_alert_type return_value = CPU_ALERT_NONE;
2784 // Technically this should be done for source and destination, but
2785 // chances are this is only ever used (probably mistakingly!) for dest.
2786 // The only game I know of that requires this is Lucky Luke.
2787 if((dest_ptr >> 24) != ((dest_ptr + length - 1) >> 24))
2789 u32 first_length = ((dest_ptr & 0xFF000000) + 0x1000000) - dest_ptr;
2790 u32 second_length = length - first_length;
2791 dma->length = first_length;
2795 dma->length = length;
2797 length = second_length;
2798 dest_ptr += first_length;
2799 src_ptr += first_length;
2802 if(dma->length_type == DMA_16BIT)
2806 cycle_dma16_words += length;
2807 dma_transfer_expand(16);
2813 cycle_dma32_words += length;
2814 dma_transfer_expand(32);
2817 if((dma->repeat_type == DMA_NO_REPEAT) ||
2818 (dma->start_type == DMA_START_IMMEDIATELY))
2820 dma->start_type = DMA_INACTIVE;
2821 address16(io_registers, (dma->dma_channel * 12) + 0xBA) &=
2827 raise_interrupt(IRQ_DMA0 << dma->dma_channel);
2828 return_value = CPU_ALERT_IRQ;
2831 return return_value;
2834 // Be sure to do this after loading ROMs.
2836 #define map_region(type, start, end, mirror_blocks, region) \
2837 for(map_offset = (start) / 0x8000; map_offset < \
2838 ((end) / 0x8000); map_offset++) \
2840 memory_map_##type[map_offset] = \
2841 ((u8 *)region) + ((map_offset % mirror_blocks) * 0x8000); \
2844 #define map_null(type, start, end) \
2845 for(map_offset = start / 0x8000; map_offset < (end / 0x8000); \
2848 memory_map_##type[map_offset] = NULL; \
2851 #define map_ram_region(type, start, end, mirror_blocks, region) \
2852 for(map_offset = (start) / 0x8000; map_offset < \
2853 ((end) / 0x8000); map_offset++) \
2855 memory_map_##type[map_offset] = \
2856 ((u8 *)region) + ((map_offset % mirror_blocks) * 0x10000) + 0x8000; \
2859 #define map_vram(type) \
2860 for(map_offset = 0x6000000 / 0x8000; map_offset < (0x7000000 / 0x8000); \
2863 memory_map_##type[map_offset] = vram; \
2864 memory_map_##type[map_offset + 1] = vram + 0x8000; \
2865 memory_map_##type[map_offset + 2] = vram + (0x8000 * 2); \
2866 memory_map_##type[map_offset + 3] = vram + (0x8000 * 2); \
2869 #define map_vram_firstpage(type) \
2870 for(map_offset = 0x6000000 / 0x8000; map_offset < (0x7000000 / 0x8000); \
2873 memory_map_##type[map_offset] = vram; \
2874 memory_map_##type[map_offset + 1] = NULL; \
2875 memory_map_##type[map_offset + 2] = NULL; \
2876 memory_map_##type[map_offset + 3] = NULL; \
2880 // Picks a page to evict
2883 u32 evict_gamepak_page()
2885 // Find the one with the smallest frame timestamp
2888 u32 smallest = gamepak_memory_map[0].page_timestamp;
2891 for(i = 1; i < gamepak_ram_pages; i++)
2893 if(gamepak_memory_map[i].page_timestamp <= smallest)
2895 smallest = gamepak_memory_map[i].page_timestamp;
2900 physical_index = gamepak_memory_map[page_index].physical_index;
2902 memory_map_read[(0x8000000 / (32 * 1024)) + physical_index] = NULL;
2903 memory_map_read[(0xA000000 / (32 * 1024)) + physical_index] = NULL;
2904 memory_map_read[(0xC000000 / (32 * 1024)) + physical_index] = NULL;
2909 u8 *load_gamepak_page(u32 physical_index)
2911 if(physical_index >= (gamepak_size >> 15))
2914 u32 page_index = evict_gamepak_page();
2915 u32 page_offset = page_index * (32 * 1024);
2916 u8 *swap_location = gamepak_rom + page_offset;
2918 gamepak_memory_map[page_index].page_timestamp = page_time;
2919 gamepak_memory_map[page_index].physical_index = physical_index;
2922 file_seek(gamepak_file_large, physical_index * (32 * 1024), SEEK_SET);
2923 file_read(gamepak_file_large, swap_location, (32 * 1024));
2924 memory_map_read[(0x8000000 / (32 * 1024)) + physical_index] = swap_location;
2925 memory_map_read[(0xA000000 / (32 * 1024)) + physical_index] = swap_location;
2926 memory_map_read[(0xC000000 / (32 * 1024)) + physical_index] = swap_location;
2928 // If RTC is active page the RTC register bytes so they can be read
2929 if((rtc_state != RTC_DISABLED) && (physical_index == 0))
2931 memcpy(swap_location + 0xC4, rtc_registers, sizeof(rtc_registers));
2934 return swap_location;
2937 void init_memory_gamepak()
2941 if(gamepak_size > gamepak_ram_buffer_size)
2943 // Large ROMs get special treatment because they
2944 // can't fit into the 16MB ROM buffer.
2946 for(i = 0; i < gamepak_ram_pages; i++)
2948 gamepak_memory_map[i].page_timestamp = 0;
2949 gamepak_memory_map[i].physical_index = 0;
2952 map_null(read, 0x8000000, 0xD000000);
2956 map_region(read, 0x8000000, 0x8000000 + gamepak_size, 1024, gamepak_rom);
2957 map_null(read, 0x8000000 + gamepak_size, 0xA000000);
2958 map_region(read, 0xA000000, 0xA000000 + gamepak_size, 1024, gamepak_rom);
2959 map_null(read, 0xA000000 + gamepak_size, 0xC000000);
2960 map_region(read, 0xC000000, 0xC000000 + gamepak_size, 1024, gamepak_rom);
2961 map_null(read, 0xC000000 + gamepak_size, 0xE000000);
2965 void init_gamepak_buffer()
2967 // Try to initialize 32MB (this is mainly for non-PSP platforms)
2970 gamepak_ram_buffer_size = 32 * 1024 * 1024;
2971 gamepak_rom = malloc(gamepak_ram_buffer_size);
2973 if(gamepak_rom == NULL)
2975 // Try 16MB, for PSP, then lower in 2MB increments
2976 gamepak_ram_buffer_size = 16 * 1024 * 1024;
2977 gamepak_rom = malloc(gamepak_ram_buffer_size);
2979 while(gamepak_rom == NULL)
2981 gamepak_ram_buffer_size -= (2 * 1024 * 1024);
2982 gamepak_rom = malloc(gamepak_ram_buffer_size);
2986 // Here's assuming we'll have enough memory left over for this,
2987 // and that the above succeeded (if not we're in trouble all around)
2988 gamepak_ram_pages = gamepak_ram_buffer_size / (32 * 1024);
2989 gamepak_memory_map = malloc(sizeof(gamepak_swap_entry_type) *
2998 memory_regions[0x00] = (u8 *)bios_rom;
2999 memory_regions[0x01] = (u8 *)bios_rom;
3000 memory_regions[0x02] = (u8 *)ewram;
3001 memory_regions[0x03] = (u8 *)iwram + 0x8000;
3002 memory_regions[0x04] = (u8 *)io_registers;
3003 memory_regions[0x05] = (u8 *)palette_ram;
3004 memory_regions[0x06] = (u8 *)vram;
3005 memory_regions[0x07] = (u8 *)oam_ram;
3006 memory_regions[0x08] = (u8 *)gamepak_rom;
3007 memory_regions[0x09] = (u8 *)(gamepak_rom + 0xFFFFFF);
3008 memory_regions[0x0A] = (u8 *)gamepak_rom;
3009 memory_regions[0x0B] = (u8 *)(gamepak_rom + 0xFFFFFF);
3010 memory_regions[0x0C] = (u8 *)gamepak_rom;
3011 memory_regions[0x0D] = (u8 *)(gamepak_rom + 0xFFFFFF);
3012 memory_regions[0x0E] = (u8 *)gamepak_backup;
3014 memory_limits[0x00] = 0x3FFF;
3015 memory_limits[0x01] = 0x3FFF;
3016 memory_limits[0x02] = 0x3FFFF;
3017 memory_limits[0x03] = 0x7FFF;
3018 memory_limits[0x04] = 0x7FFF;
3019 memory_limits[0x05] = 0x3FF;
3020 memory_limits[0x06] = 0x17FFF;
3021 memory_limits[0x07] = 0x3FF;
3022 memory_limits[0x08] = 0x1FFFFFF;
3023 memory_limits[0x09] = 0x1FFFFFF;
3024 memory_limits[0x0A] = 0x1FFFFFF;
3025 memory_limits[0x0B] = 0x1FFFFFF;
3026 memory_limits[0x0C] = 0x1FFFFFF;
3027 memory_limits[0x0D] = 0x1FFFFFF;
3028 memory_limits[0x0E] = 0xFFFF;
3030 // Fill memory map regions, areas marked as NULL must be checked directly
3031 map_region(read, 0x0000000, 0x1000000, 1, bios_rom);
3032 map_null(read, 0x1000000, 0x2000000);
3033 map_ram_region(read, 0x2000000, 0x3000000, 8, ewram);
3034 map_ram_region(read, 0x3000000, 0x4000000, 1, iwram);
3035 map_region(read, 0x4000000, 0x5000000, 1, io_registers);
3036 map_null(read, 0x5000000, 0x6000000);
3037 map_null(read, 0x6000000, 0x7000000);
3039 map_null(read, 0x7000000, 0x8000000);
3040 init_memory_gamepak();
3041 map_null(read, 0xE000000, 0x10000000);
3043 // Fill memory map regions, areas marked as NULL must be checked directly
3044 map_null(write, 0x0000000, 0x2000000);
3045 map_ram_region(write, 0x2000000, 0x3000000, 8, ewram);
3046 map_ram_region(write, 0x3000000, 0x4000000, 1, iwram);
3047 map_null(write, 0x4000000, 0x5000000);
3048 map_null(write, 0x5000000, 0x6000000);
3050 // The problem here is that the current method of handling self-modifying code
3051 // requires writeable memory to be proceeded by 32KB SMC data areas or be
3052 // indirectly writeable. It's possible to get around this if you turn off the SMC
3053 // check altogether, but this will make a good number of ROMs crash (perhaps most
3054 // of the ones that actually need it? This has yet to be determined).
3056 // This is because VRAM cannot be efficiently made incontiguous, and still allow
3057 // the renderer to work as efficiently. It would, at the very least, require a
3058 // lot of hacking of the renderer which I'm not prepared to do.
3060 // However, it IS possible to directly map the first page no matter what because
3061 // there's 32kb of blank stuff sitting beneath it.
3068 map_null(write, 0x6000000, 0x7000000);
3071 map_null(write, 0x7000000, 0x8000000);
3072 map_null(write, 0x8000000, 0xE000000);
3073 map_null(write, 0xE000000, 0x10000000);
3075 memset(io_registers, 0, 0x8000);
3076 memset(oam_ram, 0, 0x400);
3077 memset(palette_ram, 0, 0x400);
3078 memset(iwram, 0, 0x10000);
3079 memset(ewram, 0, 0x80000);
3080 memset(vram, 0, 0x18000);
3082 io_registers[REG_DISPCNT] = 0x80;
3083 io_registers[REG_P1] = 0x3FF;
3084 io_registers[REG_BG2PA] = 0x100;
3085 io_registers[REG_BG2PD] = 0x100;
3086 io_registers[REG_BG3PA] = 0x100;
3087 io_registers[REG_BG3PD] = 0x100;
3088 io_registers[REG_RCNT] = 0x8000;
3090 backup_type = BACKUP_NONE;
3092 sram_size = SRAM_SIZE_32KB;
3093 flash_size = FLASH_SIZE_64KB;
3095 flash_bank_ptr = gamepak_backup;
3096 flash_command_position = 0;
3097 eeprom_size = EEPROM_512_BYTE;
3098 eeprom_mode = EEPROM_BASE_MODE;
3102 flash_mode = FLASH_BASE_MODE;
3104 rtc_state = RTC_DISABLED;
3105 memset(rtc_registers, 0, sizeof(rtc_registers));
3106 bios_read_protect = 0xe129f000;
3109 void bios_region_read_allow()
3111 memory_map_read[0] = bios_rom;
3114 void bios_region_read_protect()
3117 memory_map_read[0] = NULL;
3122 #define savestate_block(type) \
3123 cpu_##type##_savestate(savestate_file); \
3124 input_##type##_savestate(savestate_file); \
3125 main_##type##_savestate(savestate_file); \
3126 memory_##type##_savestate(savestate_file); \
3127 sound_##type##_savestate(savestate_file); \
3128 video_##type##_savestate(savestate_file) \
3130 void load_state(char *savestate_filename)
3132 file_open(savestate_file, savestate_filename, read);
3133 if(file_check_valid(savestate_file))
3135 char current_gamepak_filename[512];
3136 char savestate_gamepak_filename[512];
3140 file_seek(savestate_file, (240 * 160 * 2) + sizeof(time_t), SEEK_SET);
3142 strcpy(current_gamepak_filename, gamepak_filename);
3144 savestate_block(read);
3146 file_close(savestate_file);
3148 flush_translation_cache_ram();
3149 flush_translation_cache_rom();
3150 flush_translation_cache_bios();
3153 gbc_sound_update = 1;
3154 if(strcmp(current_gamepak_filename, gamepak_filename))
3156 u32 dot_position = strcspn(current_gamepak_filename, ".");
3158 // We'll let it slide if the filenames of the savestate and
3159 // the gamepak are similar enough.
3160 strcpy(gamepak_filename, current_gamepak_filename);
3161 if(strncmp(savestate_filename, current_gamepak_filename, dot_position))
3163 if(load_gamepak(gamepak_filename) != -1)
3166 // Okay, so this takes a while, but for now it works.
3167 load_state(savestate_filename);
3178 for(i = 0; i < 512; i++)
3180 current_color = palette_ram[i];
3181 palette_ram_converted[i] =
3182 convert_palette(current_color);
3185 // Oops, these contain raw pointers
3186 for(i = 0; i < 4; i++)
3188 gbc_sound_channel[i].sample_data = square_pattern_duty[2];
3190 current_debug_state = STEP;
3191 instruction_count = 0;
3193 reg[CHANGED_PC_STATUS] = 1;
3197 u8 savestate_write_buffer[506947];
3200 void save_state(char *savestate_filename, u16 *screen_capture)
3202 write_mem_ptr = savestate_write_buffer;
3203 file_open(savestate_file, savestate_filename, write);
3204 if(file_check_valid(savestate_file))
3206 time_t current_time;
3207 file_write_mem(savestate_file, screen_capture, 240 * 160 * 2);
3209 time(¤t_time);
3210 file_write_mem_variable(savestate_file, current_time);
3212 savestate_block(write_mem);
3213 file_write(savestate_file, savestate_write_buffer,
3214 sizeof(savestate_write_buffer));
3216 file_close(savestate_file);
3221 #define memory_savestate_builder(type) \
3222 void memory_##type##_savestate(file_tag_type savestate_file) \
3226 file_##type##_variable(savestate_file, backup_type); \
3227 file_##type##_variable(savestate_file, sram_size); \
3228 file_##type##_variable(savestate_file, flash_mode); \
3229 file_##type##_variable(savestate_file, flash_command_position); \
3230 file_##type##_variable(savestate_file, flash_bank_ptr); \
3231 file_##type##_variable(savestate_file, flash_device_id); \
3232 file_##type##_variable(savestate_file, flash_manufacturer_id); \
3233 file_##type##_variable(savestate_file, flash_size); \
3234 file_##type##_variable(savestate_file, eeprom_size); \
3235 file_##type##_variable(savestate_file, eeprom_mode); \
3236 file_##type##_variable(savestate_file, eeprom_address_length); \
3237 file_##type##_variable(savestate_file, eeprom_address); \
3238 file_##type##_variable(savestate_file, eeprom_counter); \
3239 file_##type##_variable(savestate_file, rtc_state); \
3240 file_##type##_variable(savestate_file, rtc_write_mode); \
3241 file_##type##_array(savestate_file, rtc_registers); \
3242 file_##type##_variable(savestate_file, rtc_command); \
3243 file_##type##_array(savestate_file, rtc_data); \
3244 file_##type##_variable(savestate_file, rtc_status); \
3245 file_##type##_variable(savestate_file, rtc_data_bytes); \
3246 file_##type##_variable(savestate_file, rtc_bit_count); \
3247 file_##type##_array(savestate_file, eeprom_buffer); \
3248 file_##type##_array(savestate_file, gamepak_filename); \
3249 file_##type##_array(savestate_file, dma); \
3251 file_##type(savestate_file, iwram + 0x8000, 0x8000); \
3252 for(i = 0; i < 8; i++) \
3254 file_##type(savestate_file, ewram + (i * 0x10000) + 0x8000, 0x8000); \
3256 file_##type(savestate_file, vram, 0x18000); \
3257 file_##type(savestate_file, oam_ram, 0x400); \
3258 file_##type(savestate_file, palette_ram, 0x400); \
3259 file_##type(savestate_file, io_registers, 0x8000); \
3261 /* This is a hack, for now. */ \
3262 if((flash_bank_ptr < gamepak_backup) || \
3263 (flash_bank_ptr > (gamepak_backup + (1024 * 64)))) \
3265 flash_bank_ptr = gamepak_backup; \
3269 memory_savestate_builder(read);
3270 memory_savestate_builder(write_mem);