|
| 1 | +import Foundation |
| 2 | + |
| 3 | +import LR35902 |
| 4 | + |
| 5 | +extension Project { |
| 6 | + public convenience init(rom: Data?) { |
| 7 | + var regions: [Region] = [] |
| 8 | + var dataTypes: [DataType] = [] |
| 9 | + var globals: [Global] = [] |
| 10 | + |
| 11 | + let numberOfRestartAddresses: LR35902.Address = 8 |
| 12 | + let restartSize: LR35902.Address = 8 |
| 13 | + let rstAddresses = (0..<numberOfRestartAddresses).map { ($0 * restartSize)..<($0 * restartSize + restartSize) } |
| 14 | + rstAddresses.forEach { |
| 15 | + regions.append(Region(regionType: Windfish.Project.Region.Kind.region, name: "RST_\($0.lowerBound.hexString)", bank: 0, address: $0.lowerBound, length: LR35902.Address($0.count))) |
| 16 | + } |
| 17 | + |
| 18 | + regions.append(contentsOf: [ |
| 19 | + Region(regionType: Windfish.Project.Region.Kind.region, name: "VBlankInterrupt", bank: 0, address: 0x0040, length: 8), |
| 20 | + Region(regionType: Windfish.Project.Region.Kind.region, name: "LCDCInterrupt", bank: 0, address: 0x0048, length: 8), |
| 21 | + Region(regionType: Windfish.Project.Region.Kind.region, name: "TimerOverflowInterrupt", bank: 0, address: 0x0050, length: 8), |
| 22 | + Region(regionType: Windfish.Project.Region.Kind.region, name: "SerialTransferCompleteInterrupt", bank: 0, address: 0x0058, length: 8), |
| 23 | + Region(regionType: Windfish.Project.Region.Kind.region, name: "JoypadTransitionInterrupt", bank: 0, address: 0x0060, length: 8), |
| 24 | + Region(regionType: Windfish.Project.Region.Kind.region, name: "Boot", bank: 0, address: 0x0100, length: 4), |
| 25 | + Region(regionType: Windfish.Project.Region.Kind.image1bpp, name: "HeaderLogo", bank: 0, address: 0x0104, length: 0x0134 - 0x0104), |
| 26 | + Region(regionType: Windfish.Project.Region.Kind.string, name: "HeaderTitle", bank: 0, address: 0x0134, length: 0x0143 - 0x0134), |
| 27 | + Region(regionType: Windfish.Project.Region.Kind.label, name: "HeaderNewLicenseeCode", bank: 0, address: 0x0144, length: 0), |
| 28 | + Region(regionType: Windfish.Project.Region.Kind.label, name: "HeaderOldLicenseeCode", bank: 0, address: 0x014B, length: 0), |
| 29 | + Region(regionType: Windfish.Project.Region.Kind.label, name: "HeaderMaskROMVersion", bank: 0, address: 0x014C, length: 0), |
| 30 | + Region(regionType: Windfish.Project.Region.Kind.label, name: "HeaderComplementCheck", bank: 0, address: 0x014D, length: 0), |
| 31 | + Region(regionType: Windfish.Project.Region.Kind.label, name: "HeaderGlobalChecksum", bank: 0, address: 0x014E, length: 0), |
| 32 | + ]) |
| 33 | + |
| 34 | + dataTypes.append(DataType(name: "hex", |
| 35 | + representation: Windfish.Project.DataType.Representation.hexadecimal, |
| 36 | + interpretation: Windfish.Project.DataType.Interpretation.any, |
| 37 | + mappings: [])) |
| 38 | + dataTypes.append(DataType(name: "decimal", |
| 39 | + representation: Windfish.Project.DataType.Representation.decimal, |
| 40 | + interpretation: Windfish.Project.DataType.Interpretation.any, |
| 41 | + mappings: [])) |
| 42 | + dataTypes.append(DataType(name: "binary", |
| 43 | + representation: Windfish.Project.DataType.Representation.binary, |
| 44 | + interpretation: Windfish.Project.DataType.Interpretation.any, |
| 45 | + mappings: [])) |
| 46 | + dataTypes.append(DataType(name: "bool", |
| 47 | + representation: Windfish.Project.DataType.Representation.decimal, |
| 48 | + interpretation: Windfish.Project.DataType.Interpretation.enumerated, |
| 49 | + mappings: [ |
| 50 | + DataType.Mapping(name: "false", value: 0), |
| 51 | + DataType.Mapping(name: "true", value: 1) |
| 52 | + ])) |
| 53 | + |
| 54 | + dataTypes.append(DataType(name: "HW_COLORGAMEBOY", |
| 55 | + representation: Windfish.Project.DataType.Representation.hexadecimal, |
| 56 | + interpretation: Windfish.Project.DataType.Interpretation.enumerated, |
| 57 | + mappings: [ |
| 58 | + DataType.Mapping(name: "not_color_gameboy", value: 0x00), |
| 59 | + DataType.Mapping(name: "is_color_gameboy", value: 0x80), |
| 60 | + ])) |
| 61 | + dataTypes.append(DataType(name: "HW_SUPERGAMEBOY", |
| 62 | + representation: Windfish.Project.DataType.Representation.hexadecimal, |
| 63 | + interpretation: Windfish.Project.DataType.Interpretation.enumerated, |
| 64 | + mappings: [ |
| 65 | + DataType.Mapping(name: "not_super_gameboy", value: 0x00), |
| 66 | + DataType.Mapping(name: "is_super_gameboy", value: 0x80), |
| 67 | + ])) |
| 68 | + dataTypes.append(DataType(name: "HW_ROMSIZE", |
| 69 | + representation: Windfish.Project.DataType.Representation.hexadecimal, |
| 70 | + interpretation: Windfish.Project.DataType.Interpretation.enumerated, |
| 71 | + mappings: [ |
| 72 | + DataType.Mapping(name: "romsize_2banks", value: 0), |
| 73 | + DataType.Mapping(name: "romsize_4banks", value: 1), |
| 74 | + DataType.Mapping(name: "romsize_8banks", value: 2), |
| 75 | + DataType.Mapping(name: "romsize_16banks", value: 3), |
| 76 | + DataType.Mapping(name: "romsize_32banks", value: 4), |
| 77 | + DataType.Mapping(name: "romsize_64banks", value: 5), |
| 78 | + DataType.Mapping(name: "romsize_128banks", value: 6), |
| 79 | + DataType.Mapping(name: "romsize_72banks", value: 0x52), |
| 80 | + DataType.Mapping(name: "romsize_80banks", value: 0x53), |
| 81 | + DataType.Mapping(name: "romsize_96banks", value: 0x54), |
| 82 | + ])) |
| 83 | + dataTypes.append(DataType(name: "HW_CARTRIDGETYPE", |
| 84 | + representation: Windfish.Project.DataType.Representation.hexadecimal, |
| 85 | + interpretation: Windfish.Project.DataType.Interpretation.enumerated, |
| 86 | + mappings: [ |
| 87 | + DataType.Mapping(name: "cartridge_romonly", value: 0), |
| 88 | + DataType.Mapping(name: "cartridge_mbc1", value: 1), |
| 89 | + DataType.Mapping(name: "cartridge_mbc1_ram", value: 2), |
| 90 | + DataType.Mapping(name: "cartridge_mbc1_ram_battery", value: 3), |
| 91 | + DataType.Mapping(name: "cartridge_mbc2", value: 5), |
| 92 | + DataType.Mapping(name: "cartridge_mbc2_battery", value: 6), |
| 93 | + DataType.Mapping(name: "cartridge_rom_ram", value: 8), |
| 94 | + DataType.Mapping(name: "cartridge_rom_ram_battery", value: 9), |
| 95 | + ])) |
| 96 | + dataTypes.append(DataType(name: "HW_RAMSIZE", |
| 97 | + representation: Windfish.Project.DataType.Representation.hexadecimal, |
| 98 | + interpretation: Windfish.Project.DataType.Interpretation.enumerated, |
| 99 | + mappings: [ |
| 100 | + DataType.Mapping(name: "ramsize_none", value: 0), |
| 101 | + DataType.Mapping(name: "ramsize_1bank", value: 1), |
| 102 | + DataType.Mapping(name: "ramsize_1bank_", value: 2), |
| 103 | + DataType.Mapping(name: "ramsize_4banks", value: 3), |
| 104 | + DataType.Mapping(name: "ramsize_16banks", value: 4), |
| 105 | + ])) |
| 106 | + dataTypes.append(DataType(name: "HW_DESTINATIONCODE", |
| 107 | + representation: Windfish.Project.DataType.Representation.hexadecimal, |
| 108 | + interpretation: Windfish.Project.DataType.Interpretation.enumerated, |
| 109 | + mappings: [ |
| 110 | + DataType.Mapping(name: "destination_japanese", value: 0), |
| 111 | + DataType.Mapping(name: "destination_nonjapanese", value: 1), |
| 112 | + ])) |
| 113 | + dataTypes.append(DataType(name: "HW_IE", |
| 114 | + representation: Windfish.Project.DataType.Representation.binary, |
| 115 | + interpretation: Windfish.Project.DataType.Interpretation.bitmask, |
| 116 | + mappings: [ |
| 117 | + DataType.Mapping(name: "IE_VBLANK", value: 0b0000_0001), |
| 118 | + DataType.Mapping(name: "IE_LCDC", value: 0b0000_0010), |
| 119 | + DataType.Mapping(name: "IE_TIMEROVERFLOW", value: 0b0000_0100), |
| 120 | + DataType.Mapping(name: "IE_SERIALIO", value: 0b0000_1000), |
| 121 | + DataType.Mapping(name: "IE_PIN1013TRANSITION", value: 0b0001_0000), |
| 122 | + ])) |
| 123 | + dataTypes.append(DataType(name: "HW_AUDIO_ENABLE", |
| 124 | + representation: Windfish.Project.DataType.Representation.binary, |
| 125 | + interpretation: Windfish.Project.DataType.Interpretation.bitmask, |
| 126 | + mappings: [ |
| 127 | + DataType.Mapping(name: "HW_AUDIO_ENABLE", value: 0b1000_0000), |
| 128 | + ])) |
| 129 | + dataTypes.append(DataType(name: "LCDCF", |
| 130 | + representation: Windfish.Project.DataType.Representation.binary, |
| 131 | + interpretation: Windfish.Project.DataType.Interpretation.bitmask, |
| 132 | + mappings: [ |
| 133 | + DataType.Mapping(name: "LCDCF_OFF", value: 0b0000_0000), |
| 134 | + DataType.Mapping(name: "LCDCF_ON", value: 0b1000_0000), |
| 135 | + DataType.Mapping(name: "LCDCF_TILEMAP_9C00", value: 0b0100_0000), |
| 136 | + DataType.Mapping(name: "LCDCF_WINDOW_ON", value: 0b0010_0000), |
| 137 | + DataType.Mapping(name: "LCDCF_BG_CHAR_8000", value: 0b0001_0000), |
| 138 | + DataType.Mapping(name: "LCDCF_BG_TILE_9C00", value: 0b0000_1000), |
| 139 | + DataType.Mapping(name: "LCDCF_OBJ_16_16", value: 0b0000_0100), |
| 140 | + DataType.Mapping(name: "LCDCF_OBJ_DISPLAY", value: 0b0000_0010), |
| 141 | + DataType.Mapping(name: "LCDCF_BG_DISPLAY", value: 0b0000_0001), |
| 142 | + ])) |
| 143 | + dataTypes.append(DataType(name: "STATF", |
| 144 | + representation: Windfish.Project.DataType.Representation.binary, |
| 145 | + interpretation: Windfish.Project.DataType.Interpretation.bitmask, |
| 146 | + mappings: [ |
| 147 | + DataType.Mapping(name: "STATF_LYC", value: 0b0100_0000), |
| 148 | + DataType.Mapping(name: "STATF_MODE10", value: 0b0010_0000), |
| 149 | + DataType.Mapping(name: "STATF_MODE01", value: 0b0001_0000), |
| 150 | + DataType.Mapping(name: "STATF_MODE00", value: 0b0000_1000), |
| 151 | + DataType.Mapping(name: "STATF_LYCF", value: 0b0000_0100), |
| 152 | + DataType.Mapping(name: "STATF_OAM", value: 0b0000_0010), |
| 153 | + DataType.Mapping(name: "STATF_VB", value: 0b0000_0001), |
| 154 | + DataType.Mapping(name: "STATF_HB", value: 0b0000_0000), |
| 155 | + ])) |
| 156 | + dataTypes.append(DataType(name: "BUTTON", |
| 157 | + representation: Windfish.Project.DataType.Representation.binary, |
| 158 | + interpretation: Windfish.Project.DataType.Interpretation.bitmask, |
| 159 | + mappings: [ |
| 160 | + DataType.Mapping(name: "J_RIGHT", value: 0b0000_0001), |
| 161 | + DataType.Mapping(name: "J_LEFT", value: 0b0000_0010), |
| 162 | + DataType.Mapping(name: "J_UP", value: 0b0000_0100), |
| 163 | + DataType.Mapping(name: "J_DOWN", value: 0b0000_1000), |
| 164 | + DataType.Mapping(name: "J_A", value: 0b0001_0000), |
| 165 | + DataType.Mapping(name: "J_B", value: 0b0010_0000), |
| 166 | + DataType.Mapping(name: "J_SELECT", value: 0b0100_0000), |
| 167 | + DataType.Mapping(name: "J_START", value: 0b1000_0000), |
| 168 | + ])) |
| 169 | + dataTypes.append(DataType(name: "JOYPAD", |
| 170 | + representation: Windfish.Project.DataType.Representation.binary, |
| 171 | + interpretation: Windfish.Project.DataType.Interpretation.bitmask, |
| 172 | + mappings: [ |
| 173 | + DataType.Mapping(name: "JOYPAD_DIRECTIONS", value: 0b0001_0000), |
| 174 | + DataType.Mapping(name: "JOYPAD_BUTTONS", value: 0b0010_0000), |
| 175 | + ])) |
| 176 | + |
| 177 | + globals.append(contentsOf: [ |
| 178 | + Global(name: "HeaderIsColorGB", address: 0x0143, dataType: "HW_COLORGAMEBOY"), |
| 179 | + Global(name: "HeaderSGBFlag", address: 0x0146, dataType: "HW_SUPERGAMEBOY"), |
| 180 | + Global(name: "HeaderCartridgeType", address: 0x0147, dataType: "HW_CARTRIDGETYPE"), |
| 181 | + Global(name: "HeaderROMSize", address: 0x0148, dataType: "HW_ROMSIZE"), |
| 182 | + Global(name: "HeaderRAMSize", address: 0x0149, dataType: "HW_RAMSIZE"), |
| 183 | + Global(name: "HeaderDestinationCode", address: 0x014A, dataType: "HW_DESTINATIONCODE"), |
| 184 | + Global(name: "gbVRAM", address: 0x8000, dataType: "hex"), |
| 185 | + Global(name: "gbBGCHARDAT", address: 0x8800, dataType: "hex"), |
| 186 | + Global(name: "gbBGDAT0", address: 0x9800, dataType: "hex"), |
| 187 | + Global(name: "gbBGDAT1", address: 0x9c00, dataType: "hex"), |
| 188 | + Global(name: "gbCARTRAM", address: 0xa000, dataType: "hex"), |
| 189 | + Global(name: "gbRAM", address: 0xc000, dataType: "hex"), |
| 190 | + Global(name: "gbOAMRAM", address: 0xfe00, dataType: "hex"), |
| 191 | + Global(name: "gbP1", address: 0xff00, dataType: "JOYPAD"), |
| 192 | + Global(name: "gbSB", address: 0xff01, dataType: "hex"), |
| 193 | + Global(name: "gbSC", address: 0xff02, dataType: "hex"), |
| 194 | + Global(name: "gbDIV", address: 0xff04, dataType: "hex"), |
| 195 | + Global(name: "gbTIMA", address: 0xff05, dataType: "hex"), |
| 196 | + Global(name: "gbTMA", address: 0xff06, dataType: "hex"), |
| 197 | + Global(name: "gbTAC", address: 0xff07, dataType: "hex"), |
| 198 | + Global(name: "gbIF", address: 0xff0f, dataType: "hex"), |
| 199 | + Global(name: "gbAUD1SWEEP", address: 0xff10, dataType: "hex"), |
| 200 | + Global(name: "gbAUD1LEN", address: 0xff11, dataType: "hex"), |
| 201 | + Global(name: "gbAUD1ENV", address: 0xff12, dataType: "hex"), |
| 202 | + Global(name: "gbAUD1LOW", address: 0xff13, dataType: "hex"), |
| 203 | + Global(name: "gbAUD1HIGH", address: 0xff14, dataType: "hex"), |
| 204 | + Global(name: "gbAUD2LEN", address: 0xff16, dataType: "hex"), |
| 205 | + Global(name: "gbAUD2ENV", address: 0xff17, dataType: "hex"), |
| 206 | + Global(name: "gbAUD2LOW", address: 0xff18, dataType: "hex"), |
| 207 | + Global(name: "gbAUD2HIGH", address: 0xff19, dataType: "hex"), |
| 208 | + Global(name: "gbAUD3ENA", address: 0xff1a, dataType: "hex"), |
| 209 | + Global(name: "gbAUD3LEN", address: 0xff1b, dataType: "hex"), |
| 210 | + Global(name: "gbAUD3LEVEL", address: 0xff1c, dataType: "hex"), |
| 211 | + Global(name: "gbAUD3LOW", address: 0xff1d, dataType: "hex"), |
| 212 | + Global(name: "gbAUD3HIGH", address: 0xff1e, dataType: "hex"), |
| 213 | + Global(name: "gbAUD4LEN", address: 0xff20, dataType: "hex"), |
| 214 | + Global(name: "gbAUD4ENV", address: 0xff21, dataType: "hex"), |
| 215 | + Global(name: "gbAUD4POLY", address: 0xff22, dataType: "hex"), |
| 216 | + Global(name: "gbAUD4CONSEC", address: 0xff23, dataType: "hex"), |
| 217 | + Global(name: "gbAUDVOL", address: 0xff24, dataType: "binary"), |
| 218 | + Global(name: "gbAUDTERM", address: 0xff25, dataType: "binary"), |
| 219 | + Global(name: "gbAUDENA", address: 0xff26, dataType: "HW_AUDIO_ENABLE"), |
| 220 | + Global(name: "gbAUD3WAVERAM", address: 0xff30, dataType: "hex"), |
| 221 | + Global(name: "gbLCDC", address: 0xff40, dataType: "LCDCF"), |
| 222 | + Global(name: "gbSTAT", address: 0xff41, dataType: "STATF"), |
| 223 | + Global(name: "gbSCY", address: 0xff42, dataType: "decimal"), |
| 224 | + Global(name: "gbSCX", address: 0xff43, dataType: "decimal"), |
| 225 | + Global(name: "gbLY", address: 0xff44, dataType: "decimal"), |
| 226 | + Global(name: "gbLYC", address: 0xff45, dataType: "decimal"), |
| 227 | + Global(name: "gbDMA", address: 0xff46, dataType: "hex"), |
| 228 | + Global(name: "gbBGP", address: 0xff47, dataType: "hex"), |
| 229 | + Global(name: "gbOBP0", address: 0xff48, dataType: "hex"), |
| 230 | + Global(name: "gbOBP1", address: 0xff49, dataType: "hex"), |
| 231 | + Global(name: "gbWY", address: 0xff4a, dataType: "hex"), |
| 232 | + Global(name: "gbWX", address: 0xff4b, dataType: "hex"), |
| 233 | + Global(name: "gbKEY1", address: 0xff4d, dataType: "hex"), |
| 234 | + Global(name: "gbVBK", address: 0xff4f, dataType: "hex"), |
| 235 | + Global(name: "gbHDMA1", address: 0xff51, dataType: "hex"), |
| 236 | + Global(name: "gbHDMA2", address: 0xff52, dataType: "hex"), |
| 237 | + Global(name: "gbHDMA3", address: 0xff53, dataType: "hex"), |
| 238 | + Global(name: "gbHDMA4", address: 0xff54, dataType: "hex"), |
| 239 | + Global(name: "gbHDMA5", address: 0xff55, dataType: "hex"), |
| 240 | + Global(name: "gbRP", address: 0xff56, dataType: "hex"), |
| 241 | + Global(name: "gbBCPS", address: 0xff68, dataType: "hex"), |
| 242 | + Global(name: "gbBCPD", address: 0xff69, dataType: "hex"), |
| 243 | + Global(name: "gbOCPS", address: 0xff6a, dataType: "hex"), |
| 244 | + Global(name: "gbOCPD", address: 0xff6b, dataType: "hex"), |
| 245 | + Global(name: "gbSVBK", address: 0xff70, dataType: "hex"), |
| 246 | + Global(name: "gbPCM12", address: 0xff76, dataType: "hex"), |
| 247 | + Global(name: "gbPCM34", address: 0xff77, dataType: "hex"), |
| 248 | + Global(name: "gbIE", address: 0xffff, dataType: "HW_IE"), |
| 249 | + ]) |
| 250 | + |
| 251 | + self.init(rom: rom, scripts: [], macros: [], globals: globals, dataTypes: dataTypes, regions: regions) |
| 252 | + } |
| 253 | +} |
0 commit comments