#!/usr/bin/env python3 """Build the English book intro (screen 11) and lesson menu (screen 12). The book pages are 1024-byte nametable+attribute screens stored RLE-compressed in PRG bank 3. This module decompresses the originals, adds an English 8x8 font to a copy of the 16x16 book CHR bank, rewrites the text, and recompresses the screens. """ import os, sys from PIL import Image sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from font8 import glyph HERE = os.path.dirname(os.path.abspath(__file__)) ROM = os.path.join(HERE, '..', 'roms', 'original', 'Famicom Igo Nyuumon (Japan).nes') CHR_OFF = 16 + 8 * 16384 SCREEN_TABLE = 0xC892 - 0xC000 SCREENS = {11: 0x1256, 12: 0x148D} # compressed offsets in PRG bank 3 def load(): d = open(ROM, 'rb').read() prg = d[16:16 + 8 * 16384] chr_ = d[CHR_OFF:] return d, prg, chr_ def decompress(b3, off): out = bytearray(); i = off while True: c = b3[i]; i += 1 if c == 0: break n = 0x80 - (c & 0x7F) if c & 0x80: out.extend([b3[i]] * n); i += 1 else: out.extend(b3[i:i + n]); i += n return bytes(out), i def compress(data): """Optimal RLE for the $C6C2 loader. Control c (0x80..0xFF) repeats the next byte 0x80-(c&0x7F) times; control c (0x01..0x7F) copies 0x80-c literal bytes; 0 ends. """ n = len(data) INF = 10 ** 9 dp = [INF] * (n + 1) choice = [None] * (n + 1) dp[n] = 1 # terminator for i in range(n - 1, -1, -1): # literal run lim = min(127, n - i) for l in range(1, lim + 1): cost = 1 + l + dp[i + l] if cost < dp[i]: dp[i] = cost; choice[i] = ('lit', l) # repeat run j = i + 1 while j < n and data[j] == data[i] and j - i < 128: j += 1 for l in range(1, j - i + 1): cost = 2 + dp[i + l] if cost < dp[i]: dp[i] = cost; choice[i] = ('rep', l) out = bytearray(); i = 0 while i < n: kind, l = choice[i] if kind == 'lit': out.append(0x80 - l); out.extend(data[i:i + l]) else: out.append(0x80 | (0x80 - l)); out.append(data[i]) i += l out.append(0) return bytes(out) CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-'():/" def make_font_bank(chr_): """Copy CHR 4K bank 1 (book font) and add ASCII at free tile codes.""" bank = bytearray(chr_[1 * 4096:2 * 4096]) # Free tile codes computed from the intro/menu border tiles. These # do not appear outside the replaced text areas. free = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, 0x16,0x17,0x18,0x19,0x1a,0x1b,0x26,0x27,0x28,0x29,0x2a,0x2b,0x36,0x37,0x38,0x39, 0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f, 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f, 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f, 0x70,0x71,0x72,0x73,0x74,0x75,0x7f,0x80,0x81,0x82,0x83,0x84,0x85,0x8f,0xa0,0xa1, 0xa2,0xa3,0xa4,0xa5,0xa6,0xaf,0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9, 0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9, 0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9, 0xda,0xdb,0xdc,0xdd,0xde,0xdf,0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9, 0xea,0xeb,0xec,0xed,0xee,0xef,0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9, 0xfa,0xfb,0xfc,0xff] codes = dict(zip(CHARS, free)) for ch, code in codes.items(): g = glyph(ch) for y in range(8): mask = 0 for x in range(8): if g[y][x]: mask |= 1 << (7 - x) # Book-font tiles are solid colour 3 with colour-0 glyph # pixels carved out (the page palette makes colour 0 white # and colour 3 black). inv = 0xFF ^ mask bank[code * 16 + y] = inv bank[code * 16 + 8 + y] = inv return bank, codes def clear(nt, x0, y0, x1, y1): for y in range(y0, y1 + 1): for x in range(x0, x1 + 1): nt[y * 32 + x] = 0xFE def put(nt, x, y, s, codes): for i, ch in enumerate(s): if ch != ' ': nt[y * 32 + x + i] = codes[ch] def putv(nt, x, y, s, codes): for i, ch in enumerate(s): if ch != ' ': nt[(y + i) * 32 + x] = codes[ch] def build(): raw, prg, chr_ = load() b3 = prg[3 * 16384:4 * 16384] bank9, codes = make_font_bank(chr_) # intro / course menu (screen 11) # The Japanese page is a nested menu: EXIT, ADVANCED and BASICS are # three selectable vertical entries on the left. Earlier builds # replaced the whole menu with prose, which is why the English version # jumped straight to the eight basic lessons. Keep the original entry # columns so the game's cursor and selection code still work. intro, _ = decompress(b3, SCREENS[11]) intro = bytearray(intro[:1024]) clear(intro, 3, 3, 13, 24) clear(intro, 17, 3, 28, 24) # These columns match the original label columns and the cursor sprite: # EXIT -> $04-$05, ADVANCED -> $07-$08, BASICS -> $0A-$0B. putv(intro, 5, 6, "EXIT", codes) putv(intro, 8, 6, "ADVANCED", codes) putv(intro, 11, 6, "BASICS", codes) for y, s in enumerate(["RULES BOOK", "BY SEIMOKU", "", "CHOOSE A", "COURSE.", "", "PRESS A."]): put(intro, 17, 4 + y * 2, s, codes) # menu menu, _ = decompress(b3, SCREENS[12]) menu = bytearray(menu[:1024]) # Keep the original 4 3 2 1 row: those are the bold numeral tiles. # The old build replaced them with the thinner English font digits, # which is why 1-4 looked lighter than 5-8. clear(menu, 3, 5, 13, 13) clear(menu, 13, 4, 13, 4) # remove stray 'ル' header tile beside '1' clear(menu, 3, 16, 13, 22) clear(menu, 17, 4, 28, 16) # Page-turn option at the original left-hand column. putv(menu, 3, 6, "NEXT", codes) putv(menu, 5, 6, "MOVE", codes) # 4 putv(menu, 7, 6, "TAKE", codes) # 3 putv(menu, 9, 6, "PLACE", codes) # 2 putv(menu, 11, 6, "RULES", codes) # 1 putv(menu, 5, 17, "END", codes) # 8 putv(menu, 7, 17, "LAND", codes) # 7 putv(menu, 9, 17, "LIFE", codes) # 6 putv(menu, 11, 17, "KO", codes) # 5 for y, s in enumerate(["LESSONS", "CHOOSE ONE", "BY SEIMOKU", "PRESS A"]): put(menu, 17, 4 + y * 2, s, codes) ci = compress(intro) cm = compress(menu) print('intro compressed', len(ci), 'original', SCREENS[11] and 567) print('menu compressed', len(cm), 'original 611') return bytes(intro), ci, bytes(menu), cm, bytes(bank9) def render(data, bank, fn): img = Image.new('RGB', (256, 240), (0, 0, 0)); px = img.load() pal = [(255, 255, 255), (170, 170, 170), (85, 85, 85), (0, 0, 0)] for r in range(30): for c in range(32): t = data[r * 32 + c] base = t * 16 for y in range(8): lo = bank[base + y]; hi = bank[base + 8 + y] for x in range(8): v = ((lo >> (7 - x)) & 1) | (((hi >> (7 - x)) & 1) << 1) px[c * 8 + x, r * 8 + y] = pal[v] img.save(fn) if __name__ == '__main__': build()