다마고치 연구소 한국어판 소스 공개
This commit is contained in:
commit
db23855630
661 files changed
+503597
No files matched your search
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Install download content (TAMAGO records from .jpg) into an 8 MB image.
|
||||
dlc.py <image_in> <file.jpg> <image_out> [--fach N] (games: slot 0 or 1)
|
||||
dlc.py <image> --liste"""
|
||||
import sys
|
||||
GAMES = (0x7A0000, 0x8000, 2)
|
||||
KINDS = [('Essen', 0x590000, 0x800, 20), ('Snacks', 0x5A0000, 0x800, 20), ('Kleidung', 0x600000, 0x4000, 5),
|
||||
('Zubehoer', 0x650000, 0x2000, 10), ('Sachen', 0x6A0000, 0x2000, 10), ('Alltag', 0x740000, 0x8000, 5),
|
||||
('Tapeten', 0x780000, 0x2000, 10), ('Spiele', 0x7A0000, 0x8000, 2), ('Reiseziele', 0x7B0000, 0x8000, 2)]
|
||||
|
||||
def payload(data):
|
||||
if data[:6] == b'TAMAGO': blob = data
|
||||
else:
|
||||
e = data.find(b'\xff\xd9'); m = data.find(b'TAMAGO', e + 2 if e >= 0 else 0)
|
||||
if m < 0: raise SystemExit('no TAMAGO record in the file')
|
||||
blob = data[m:]
|
||||
def ok(n): return n >= 3 and (sum(blob[:n - 2]) & 0xFFFF) == int.from_bytes(blob[n - 2:n], 'big')
|
||||
ln = int.from_bytes(blob[0x48:0x4C], 'big') if len(blob) >= 0x4C else 0
|
||||
if 0x60 <= ln <= len(blob) and ok(ln): return blob[:ln]
|
||||
for s in range(17):
|
||||
n = len(blob) - s
|
||||
if n >= max(0x60, ln) and ok(n): return blob[:n]
|
||||
raise SystemExit('record checksum does not match')
|
||||
|
||||
def info(rec):
|
||||
name = rec[6:0x34].decode('utf-16-be', 'replace').rstrip('\0').strip()
|
||||
return rec[0x34:0x48].split(b'\0')[0].decode('latin1'), name
|
||||
|
||||
def liste(img):
|
||||
for l, b, sz, n in KINDS:
|
||||
for s in range(n):
|
||||
o = b + s * sz
|
||||
if img[o:o + 6] == b'TAMAGO': print('%-10s Fach %d %s %s' % (l, s, *info(img[o:o + sz])))
|
||||
|
||||
def main():
|
||||
img = bytearray(open(sys.argv[1], 'rb').read()); img += b'\xff' * (0x800000 - len(img))
|
||||
if '--liste' in sys.argv: liste(img); return
|
||||
rec = payload(open(sys.argv[2], 'rb').read())
|
||||
if rec[0x4E] != 0x94 or rec[0x50] != 0x5B: raise SystemExit('not a game (type %02x cat %02x)' % (rec[0x4E], rec[0x50]))
|
||||
base, sz, n = GAMES
|
||||
fach = int(sys.argv[sys.argv.index('--fach') + 1]) if '--fach' in sys.argv else n - 1
|
||||
o = base + fach * sz
|
||||
print('ersetze', info(img[o:o + sz]) if img[o:o + 6] == b'TAMAGO' else 'leer', '->', info(rec))
|
||||
img[o:o + sz] = rec + b'\xff' * (sz - len(rec))
|
||||
open(sys.argv[3], 'wb').write(img)
|
||||
|
||||
if __name__ == '__main__': main()
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Install the field switch into the Paradise ROM (Land/Water/Sky, firmware v1).
|
||||
feld_rom.py <rom_in> <rom_out> [--app app.bin] [--feld feld.bin]
|
||||
Holding B+C on the home screen starts the field switch, A+C (if --app)
|
||||
starts the program as before. The three save slots sit after the characters."""
|
||||
import sys, struct, os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from patch_rom import scene_table, xip, APP_OFF, STUB_OFF
|
||||
import subprocess, tempfile
|
||||
|
||||
FELD_OFF = 0xB1000
|
||||
SLOT_SIZE = 0x58000
|
||||
FELD_MAGIC = 0x444C4546
|
||||
|
||||
def build_stub(app_src, feld_src, origs, limit=90):
|
||||
here = os.path.dirname(os.path.abspath(__file__))
|
||||
S = os.path.join(here, '..', 'stub', 'stub.S')
|
||||
t = tempfile.mkdtemp()
|
||||
defs = ['-DAPP_SRC=%d' % app_src, '-DFELD=1', '-DFELD_SRC=%d' % feld_src, '-DHOOKS=1', '-DLIMIT=%d' % limit]
|
||||
defs += ['-DORIG%d=%d' % (i, v) for i, v in enumerate(origs)]
|
||||
if os.environ.get('HOOK_TEST'): defs.append('-DHOOK_TEST=1')
|
||||
subprocess.check_call(['clang', '--target=thumbv7em-none-eabi', '-mcpu=cortex-m4', '-x', 'assembler-with-cpp', '-c', S, '-o', t + '/s.o'] + defs)
|
||||
subprocess.check_call(['ld.lld', '--no-rosegment', '-Ttext=%#x' % xip(STUB_OFF), '-e', 'start', t + '/s.o', '-o', t + '/s.elf'])
|
||||
subprocess.check_call(['llvm-objcopy', '-O', 'binary', t + '/s.elf', t + '/s.bin'])
|
||||
return open(t + '/s.bin', 'rb').read()
|
||||
|
||||
def slot_off(d):
|
||||
asset_end = (0x111000 + 16 + struct.unpack_from('<I', d, 0x111000 + 8)[0] + 0xFFF) & ~0xFFF
|
||||
return max(0x830000, asset_end)
|
||||
|
||||
def main():
|
||||
rin, rout = sys.argv[1:3]
|
||||
d = bytearray(open(rin, 'rb').read())
|
||||
assert len(d) == 0x1000000
|
||||
fid = struct.unpack_from('<I', d, 0xD49000)[0]
|
||||
assert fid in (1, 2, 3), 'nur Land, Water oder Sky (Kennung %d)' % fid
|
||||
here = os.path.dirname(os.path.abspath(__file__))
|
||||
fb = sys.argv[sys.argv.index('--feld') + 1] if '--feld' in sys.argv else os.path.join(here, '..', 'feld', 'feld.bin')
|
||||
f = bytearray(open(fb, 'rb').read())
|
||||
i = f.find(struct.pack('<I', FELD_MAGIC))
|
||||
assert i >= 0
|
||||
so = slot_off(d)
|
||||
assert so + 3 * SLOT_SIZE <= 0x940000, 'no room for the slots'
|
||||
struct.pack_into('<II', f, i + 8, so, SLOT_SIZE)
|
||||
assert len(f) <= 0xC0000 - FELD_OFF
|
||||
tab = scene_table(bytes(d))
|
||||
o = tab['PSID_HOME']
|
||||
origs = list(struct.unpack_from('<4I', d, o + 12))
|
||||
if any(xip(STUB_OFF) <= v < xip(STUB_OFF) + 0x1000 for v in origs):
|
||||
raise SystemExit('ROM ist schon gepatcht')
|
||||
app_src = 0
|
||||
if '--app' in sys.argv:
|
||||
a = open(sys.argv[sys.argv.index('--app') + 1], 'rb').read()
|
||||
assert len(a) <= 0x10000
|
||||
d[APP_OFF:APP_OFF + 0x10000] = a + b'\x00' * (0x10000 - len(a))
|
||||
app_src = xip(APP_OFF)
|
||||
st = build_stub(app_src, xip(FELD_OFF), origs)
|
||||
d[STUB_OFF:STUB_OFF + 0x1000] = st + b'\xff' * (0x1000 - len(st))
|
||||
d[FELD_OFF:FELD_OFF + len(f)] = f
|
||||
for k in range(4):
|
||||
struct.pack_into('<I', d, o + 12 + 4 * k, (xip(STUB_OFF) + 0x40 + 6 * k) | 1)
|
||||
open(rout, 'wb').write(d)
|
||||
print('field switch added: slots from %#x, %#x each' % (so, SLOT_SIZE))
|
||||
|
||||
if __name__ == '__main__': main()
|
||||
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Item catalog from a Paradise image (ID, English name, fixed).
|
||||
Based on werkzeuge/gegenstaende-lesen.py and Bilder.kt from the TamaParadise app."""
|
||||
import struct, sys
|
||||
u16=lambda d,o: struct.unpack_from('<H',d,o)[0]
|
||||
u32=lambda d,o: struct.unpack_from('<I',d,o)[0]
|
||||
SATZ=46; A=318
|
||||
SONDER={305:' ',317:' ',375:'&',376:"'",381:'.'}
|
||||
def bildtab(d):
|
||||
o=0
|
||||
while o+4<len(d):
|
||||
e0=u32(d,o)
|
||||
if 0x1000<=e0<=0x8000 and e0%4==0:
|
||||
prev=e0; ok=True
|
||||
for i in range(1,60):
|
||||
w=u32(d,o+4*i)
|
||||
if w<=prev or w>0x1000000: ok=False;break
|
||||
prev=w
|
||||
if ok:
|
||||
k=o+e0
|
||||
if k+24<len(d):
|
||||
g=u32(d,k); p=u32(d,k+20)
|
||||
if 24<p<g and 1<=g<=0x20000 and p%8==0 and d[k+8]>0 and d[k+9]>0: return o
|
||||
o+=4
|
||||
return -1
|
||||
def itemtab(d,nb):
|
||||
best,bn=-1,0; o=0
|
||||
while o+SATZ<len(d):
|
||||
if 5001<=u16(d,o)<=9999:
|
||||
seen=set(); n=0
|
||||
while n<400 and o+n*SATZ+5<len(d):
|
||||
k=u16(d,o+n*SATZ); b=u16(d,o+n*SATZ+4)
|
||||
if not (5001<=k<=9999) or b>=nb or k in seen: break
|
||||
seen.add(k); n+=1
|
||||
if n>bn: bn,best=n,o
|
||||
if n>8: o+=n*SATZ-2
|
||||
o+=2
|
||||
return best,bn
|
||||
def s_at(d,t,n):
|
||||
ab=u16(d,t+2*n)
|
||||
if ab<=0 or ab>0x8000: return ''
|
||||
o=t+2*ab; s=''
|
||||
while len(s)<40:
|
||||
c=u16(d,o)
|
||||
if c==0: break
|
||||
if A<=c<A+26: s+=chr(65+c-A)
|
||||
elif c in SONDER: s+=SONDER[c]
|
||||
else: return ''
|
||||
o+=2
|
||||
return s.strip()
|
||||
def word3(d,t,n):
|
||||
ab=u16(d,t+2*n)
|
||||
if ab<=0 or ab>0x8000: return False
|
||||
return all(A<=u16(d,t+2*ab+2*k)<A+26 for k in range(3))
|
||||
WORDS=["FOOD","SNACKS","OUTSIDE TOY","LAB LOG","MILK"]
|
||||
def langtab(d):
|
||||
best=None;bp=0;o=0
|
||||
while o<len(d)-8:
|
||||
n=u16(d,o)
|
||||
if 100<=n<=4000 and u16(d,o+2)>n and u16(d,o+4)>u16(d,o+2) and u16(d,o+6)>u16(d,o+4) and word3(d,o,1):
|
||||
prev=n;ok=True
|
||||
for i in range(1,min(n,200)):
|
||||
w=u16(d,o+2*i)
|
||||
if w<prev or w>0x8000: ok=False;break
|
||||
prev=w
|
||||
if ok:
|
||||
p=sum(1 for k in range(1,120) if s_at(d,o,k) in WORDS)
|
||||
if p>bp: bp,best=p,o
|
||||
o+=2
|
||||
return best if bp>=2 else None
|
||||
def catalog(d):
|
||||
bt=bildtab(d); nb=u32(d,bt)//4
|
||||
it,n=itemtab(d,nb)
|
||||
lt=langtab(d)
|
||||
out=[]
|
||||
for i in range(n):
|
||||
o=it+i*SATZ; k=u16(d,o); nm=u16(d,o+2)+1; price=u16(d,o+24)
|
||||
if not (5001<=k<=8999): continue
|
||||
name=s_at(d,lt,nm) if lt is not None else ''
|
||||
out.append((k,price==0xFFFF,name))
|
||||
return sorted(out),hex(bt),hex(it),n,lt
|
||||
if __name__=='__main__':
|
||||
for f in sys.argv[1:]:
|
||||
d=open(f,'rb').read()
|
||||
c,bt,it,n,lt=catalog(d)
|
||||
print(f.split('/')[-1],bt,it,n,hex(lt) if lt else None,len(c),'fixed',[x[0] for x in c if x[1]],'without name',sum(1 for x in c if not x[2]))
|
||||
print(' ',c[:3],c[-3:], max(len(x[2]) for x in c))
|
||||
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Installs the stub into the Paradise ROM.
|
||||
patch_rom.py <rom_in> <app.bin> <rom_out> [--scene PSID_NAME] [--guest fw.bin --device pc|idl]
|
||||
Places the app image (max 64 KB) at APP_OFF and the stub at STUB_OFF and
|
||||
redirects all four handlers of the chosen scene to the stub."""
|
||||
import sys, struct, re, subprocess, os, tempfile
|
||||
XIP_DEC = 0x11000
|
||||
def xip(off): return 0x10000000 + off - XIP_DEC
|
||||
APP_OFF, STUB_OFF = 0xA0000, 0xB0000
|
||||
|
||||
def scene_table(d):
|
||||
names = {xip(m.start()): m.group()[:-1].decode() for m in re.finditer(rb'PSID_[A-Z0-9_]+\x00', d)}
|
||||
ptrs = [(o, w) for o in range(0, len(d) - 4, 4) for w in [struct.unpack_from('<I', d, o)[0]] if w in names]
|
||||
best, run = [], [ptrs[0]]
|
||||
for a, b in zip(ptrs, ptrs[1:]):
|
||||
if b[0] - a[0] == 28: run.append(b)
|
||||
else:
|
||||
if len(run) > len(best): best = run
|
||||
run = [b]
|
||||
if len(run) > len(best): best = run
|
||||
base = best[0][0] - 4
|
||||
return {names[w]: base + i * 28 for i, (o, w) in enumerate(best)}
|
||||
|
||||
def build_stub(app_src, origs=None, limit=90):
|
||||
here = os.path.dirname(os.path.abspath(__file__))
|
||||
S = os.path.join(here, '..', 'stub', 'stub.S')
|
||||
t = tempfile.mkdtemp()
|
||||
defs = ['-DAPP_SRC=%d' % app_src]
|
||||
if origs:
|
||||
defs += ['-DHOOKS=1', '-DLIMIT=%d' % limit] + ['-DORIG%d=%d' % (i, v) for i, v in enumerate(origs)]
|
||||
subprocess.check_call(['clang', '--target=thumbv7em-none-eabi', '-mcpu=cortex-m4', '-x', 'assembler-with-cpp', '-c', S, '-o', t + '/s.o'] + defs)
|
||||
subprocess.check_call(['ld.lld', '--no-rosegment', '-Ttext=%#x' % xip(STUB_OFF), '-e', 'start', t + '/s.o', '-o', t + '/s.elf'])
|
||||
subprocess.check_call(['llvm-objcopy', '-O', 'binary', t + '/s.elf', t + '/s.bin'])
|
||||
syms = {}
|
||||
for l in subprocess.run(['llvm-nm', t + '/s.elf'], capture_output=True, text=True).stdout.splitlines():
|
||||
p = l.split()
|
||||
if len(p) == 3: syms[p[2]] = int(p[0], 16)
|
||||
return open(t + '/s.bin', 'rb').read(), syms
|
||||
|
||||
GUEST_OFF, GUEST_END = 0x940000, 0xD40000
|
||||
DEVICES = {'pc': (0, 0x400000), 'idl': (1, 0x800000)}
|
||||
# Blocks with many data accesses first: they end up in the cached 1 MB window
|
||||
HOT = {'pc': [0x25, 0x21, 0x04, 0x02, 0x01, 0x03, 0x05, 0x20, 0x22, 0x23, 0x24, 0x26, 0x27, 0x28, 0x29, 0x2a],
|
||||
'idl': [0x0e, 0x0b, 0x19, 0x16, 0x0d, 0x18, 0x0c, 0x4f, 0x50, 0x15, 0x40, 0x4c, 0x4d, 0x0f, 0x17, 0x1a]}
|
||||
|
||||
def pack_guest(fw, dev):
|
||||
kind, size = DEVICES[dev]
|
||||
fw = fw[:size] + b'\xff' * (size - min(len(fw), size))
|
||||
nblk = size >> 16
|
||||
par = nblk - 1 if dev == 'pc' else 0 # always include the parameter block (save)
|
||||
used = [i for i in range(nblk) if i == par or fw[i << 16:(i + 1) << 16] != b'\xff' * 0x10000]
|
||||
order = [b for b in HOT[dev] if b in used] + [b for b in used if b not in HOT[dev]]
|
||||
m = bytearray(b'\xff' * 128)
|
||||
blob = bytearray()
|
||||
for idx, b in enumerate(order):
|
||||
m[b] = idx
|
||||
blob += fw[b << 16:(b + 1) << 16]
|
||||
assert GUEST_OFF + len(blob) <= GUEST_END, 'guest ROM too big: %d blocks' % len(order)
|
||||
return kind, bytes(m), bytes(blob)
|
||||
|
||||
def set_cfg(app, kind, guest_size, m):
|
||||
i = app.find(struct.pack('<I', 0x43584D50))
|
||||
assert i >= 0, 'PMXC not found'
|
||||
a = bytearray(app)
|
||||
struct.pack_into('<6I', a, i, 0x43584D50, 2, GUEST_OFF, guest_size, 0xC0000, kind)
|
||||
a[i + 24:i + 24 + 128] = m
|
||||
return bytes(a)
|
||||
|
||||
def main():
|
||||
rin, app, rout = sys.argv[1:4]
|
||||
scene = None # default: hold A+C on the home screen
|
||||
if '--scene' in sys.argv: scene = sys.argv[sys.argv.index('--scene') + 1]
|
||||
d = bytearray(open(rin, 'rb').read())
|
||||
a = open(app, 'rb').read()
|
||||
if '--guest' in sys.argv:
|
||||
dev = sys.argv[sys.argv.index('--device') + 1] if '--device' in sys.argv else 'pc'
|
||||
kind, m, blob = pack_guest(open(sys.argv[sys.argv.index('--guest') + 1], 'rb').read(), dev)
|
||||
d[GUEST_OFF:GUEST_OFF + len(blob)] = blob
|
||||
a = set_cfg(a, kind, len(blob), m)
|
||||
print('Gast', dev, len(blob) >> 16, 'Bloecke ab', hex(GUEST_OFF))
|
||||
assert len(a) <= 0x10000, 'App groesser als 64 KB'
|
||||
for off, n in ((APP_OFF, 0x10000), (STUB_OFF, 0x1000)):
|
||||
assert d[off:off + n] == b'\xff' * n or True
|
||||
d[APP_OFF:APP_OFF + 0x10000] = a + b'\x00' * (0x10000 - len(a))
|
||||
tab = scene_table(bytes(d))
|
||||
if scene:
|
||||
st, _ = build_stub(xip(APP_OFF))
|
||||
d[STUB_OFF:STUB_OFF + len(st)] = st
|
||||
o = tab[scene]
|
||||
for k in range(3, 7):
|
||||
struct.pack_into('<I', d, o + 4 * k, xip(STUB_OFF) | 1)
|
||||
print('Szene', scene, 'bei', hex(o), '-> Starter', hex(xip(STUB_OFF) | 1), 'Stub', len(st), 'Byte')
|
||||
else:
|
||||
o = tab['PSID_HOME']
|
||||
origs = list(struct.unpack_from('<4I', d, o + 12))
|
||||
if any(xip(STUB_OFF) <= v < xip(STUB_OFF) + 0x1000 for v in origs):
|
||||
raise SystemExit('ROM ist schon gepatcht (Heimbildschirm zeigt auf den Starter)')
|
||||
st, sy = build_stub(xip(APP_OFF), origs)
|
||||
d[STUB_OFF:STUB_OFF + len(st)] = st
|
||||
for k in range(4):
|
||||
struct.pack_into('<I', d, o + 12 + 4 * k, sy['hook%d' % k] | 1)
|
||||
print('Heimbildschirm: A+C halten startet den Emulator; Stub', len(st), 'Byte')
|
||||
open(rout, 'wb').write(d)
|
||||
|
||||
if __name__ == '__main__': main()
|
||||
Reference in new issue
Block a user