# SPDX-License-Identifier: MIT """Mapper 559 built from the comparison with the licensed Namco 163 release. Sangokushi II - Haou no Tairiku (Subor) is the Japanese mapper-19 cart with 968 bytes changed (audit.md, entry 16). Each patched store lines up a Namco 163 register (INES_Mapper_019.wiki, public domain) with the address the pirate board uses instead. Every line below is HYPOTHESIS until the game runs on it, then FITTED; nothing here has been measured on a cart. - $8000, $A000: PRG at $8000, $A000 (the 163's $E000, $E800). D4-D0 are used: the game still ORs $C0 into the $A000 value. - $9C00: PRG at $C000 (the 163's $F000). The last 8 KiB is fixed. - $9C0C-$9C0F: the four nametables (the 163's $C000-$D800). The game writes only $E0/$E1, so D0 is taken as CIRAM A10. - $B000-$EC00 in steps of $400: the 1 KiB CHR banks, two addresses per bank (the 163's $8000-$B800). The game writes the same full value to both, so either may set the bank; whether they are low/high halves is open. - $F000/$F400: the IRQ latch (the game writes the same value to both). - $F800: IRQ control. 2 starts the counter from the latch; 0 stops it and acknowledges. The game's conversion tables (bank 31, $F700-$F7FF) assume the counter steps once per 115 CPU cycles and asserts /IRQ when it wraps past $FF: (127 - h) * 256 = count * 115 + 8 * fine + (0..7) for all 128 entries exactly. Against the licensed cart a step of one scanline period fits far better (see PRESCALE). - $9800, written 1 at reset where the original wrote $2001: unknown, ignored. - PRG-RAM at $6000-$7FFF always enabled: the 163's protection port was $F800, now the IRQ control. """ from __future__ import annotations from famicom_python.cartridge import MAPPERS from famicom_python.cartridge.banked import Banked class Subor559(Banked): counts_m2 = True # The counter details the ROM cannot show, kept as class settings so a # fit can try them: cycles per step; the prescaler value a start loads # (0: the first step comes PRESCALE cycles later); the count that asserts # /IRQ (0: on the wrap past $FF). FITTED (audit.md, entry 16): one step # per scanline period, 341 PPU dots = 341/3 CPU cycles, against the # licensed cart's picture; the game's own tables assume 115, which fits # worse (7 against 1,782 differing pixels per frame). Whether the board # counts PPU scanlines or divides M2 is open: they differ only with # rendering off. PRESCALE = 341 / 3 START_PHASE = 0 FIRE_AT = 0 def __init__(self, image, cpu_cycle_ticks): super().__init__(image, cpu_cycle_ticks) self.nametables = [0, 1, 0, 1] self.latch = 0 self.counter = 0 self.prescaler = 0 self.counting = False self.map_prg(3, 8, len(self.prg) // 0x2000 - 1) def cpu_write(self, address, value, t): if address < 0x8000: super().cpu_write(address, value, t) return region = address & 0xFC00 if region == 0x8000: self.map_prg(0, 8, value & 0x1F) elif region == 0xA000: self.map_prg(1, 8, value & 0x1F) elif region == 0x9C00: low = address & 0x0F if low == 0: self.map_prg(2, 8, value & 0x1F) elif low >= 0x0C: self.nametables[low - 0x0C] = value & 1 elif 0xB000 <= region < 0xF000: self.map_chr((region - 0xB000) >> 11, 1, value) elif region in (0xF000, 0xF400): self.latch = value elif region == 0xF800: self.irq = False self.counting = bool(value & 2) if self.counting: self.counter = self.latch self.prescaler = self.START_PHASE def m2(self, t): if self.counting: self.prescaler += 1 if self.prescaler >= self.PRESCALE: self.prescaler -= self.PRESCALE self.counter = (self.counter + 1) & 0xFF if self.counter == self.FIRE_AT: self.irq = True def ciram_a10(self, address): return self.nametables[(address >> 10) & 3] def install(): MAPPERS[559] = Subor559