1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
"""Build the authentic Japanese-styled English title screen (Screen 0).
Matches the original 1991 I'MAX Famicom title screen layout, palettes,
clouds, and animation 1-for-1:
- Left badge (rows 4-5, cols 2-9, pal 2): bold orange-red "FAMICOM(R)"
with crisp 1px white outline (matching the original ファミコン® badge).
- Main logo (rows 2-6, cols 11-29, pal 0): large calligraphic "GO BASICS"
in bright green -> horizontal green/black scanline stripe -> jet black
with crisp 1px white outline (matching 囲碁入門).
- Stamp (CHR bank 7 sprites 0-13, original $E57E OAM coords, pal 3):
crisp 56x16 white stamp "NIHON KIIN / APPROVED" (matching 日本棋院推薦).
- Menu items (rows 20-27, pal 3): bold pixel-serif "GO LESSONS",
"PLAY GO", and "RANK TEST" in the original white -> cream-gold ->
coral-orange sunset gradient, aligned beside the white Go-stone cursor.
- Strictly preserves all 53 runtime animation tiles (rows 11-17), the
Mount Fuji frame/sprites, all white clouds, and "(c)1991 I'MAX".
"""
from PIL import Image, ImageDraw, ImageFont
import make_book
# Every CHR bank 0 tile ever written by the title sunrise/light-ray animation
# across frames 31..700 (verified via nametable trace on the JP ROM).
ANIM_TILES = {
0x1C, 0x6D, 0x6E, 0x6F, 0x7C, 0x7D, 0x7F, 0x8D, 0x9D, 0xAD, 0xAE,
0xBD, 0xBE, 0xBF, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xDB, 0xDC, 0xDD,
0xDE, 0xDF, 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, 0xFE,
}
FONT_LOGO = '/usr/share/fonts/noto/NotoSerif-BlackItalic.ttf'
# Crisp hand-crafted 4x5 uppercase pixel font for the 56x16 white stamp.
FONT_4X5 = {
'A': [".##.", "#..#", "####", "#..#", "#..#"],
'B': ["###.", "#..#", "###.", "#..#", "###."],
'C': [".###", "#...", "#...", "#...", ".###"],
'D': ["###.", "#..#", "#..#", "#..#", "###."],
'E': ["####", "#...", "###.", "#...", "####"],
'G': [".###", "#...", "#.##", "#..#", ".###"],
'H': ["#..#", "#..#", "####", "#..#", "#..#"],
'I': ["###", ".#.", ".#.", ".#.", "###"],
'K': ["#..#", "#.#.", "##..", "#.#.", "#..#"],
'N': ["#..#", "##.#", "#.##", "#..#", "#..#"],
'O': [".##.", "#..#", "#..#", "#..#", ".##."],
'P': ["###.", "#..#", "###.", "#...", "#..."],
'R': ["###.", "#..#", "###.", "#.#.", "#..#"],
'S': [".###", "#...", ".##.", "...#", "###."],
'T': ["###", ".#.", ".#.", ".#.", ".#."],
'V': ["#..#", "#..#", "#..#", ".##.", ".##."],
'Y': ["#.#", "#.#", ".#.", ".#.", ".#."],
' ': ["..", "..", "..", "..", ".."],
}
# Hand-crafted 7x11 bold retro pixel glyphs for "FAMICOM" badge (italicized
# 1px to the right on the top half to evoke the classic Famicom wordmark).
FAMICOM_GLYPHS = {
'F': [
"######",
"######",
"##....",
"##....",
"#####.",
"#####.",
"##....",
"##....",
"##....",
"##....",
"##....",
],
'A': [
"..###..",
".#####.",
"##...##",
"##...##",
"##...##",
"#######",
"#######",
"##...##",
"##...##",
"##...##",
"##...##",
],
'M': [
"##...##",
"###.###",
"#######",
"##.#.##",
"##.#.##",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
],
'I': [
"####",
"####",
".##.",
".##.",
".##.",
".##.",
".##.",
".##.",
".##.",
"####",
"####",
],
'C': [
".#####.",
"######.",
"##...##",
"##.....",
"##.....",
"##.....",
"##.....",
"##.....",
"##...##",
"######.",
".#####.",
],
'O': [
".#####.",
"#######",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
"#######",
".#####.",
],
}
# Crisp hand-crafted 7x13 bold pixel-serif letters for the sunset title menu
# (GO LESSONS / PLAY GO / RANK TEST). 2px vertical stems and clean serifs
# keep every letter razor-sharp across the white -> cream -> coral gradient.
MENU_SERIF = {
'A': [
"..###..",
"..###..",
".##.##.",
".##.##.",
".##.##.",
"##...##",
"##...##",
"#######",
"#######",
"##...##",
"##...##",
"##...##",
"###.###",
],
'E': [
"#######",
"#######",
".##...#",
".##....",
".##..#.",
".#####.",
".#####.",
".##..#.",
".##....",
".##....",
".##...#",
"#######",
"#######",
],
'G': [
".#####.",
"#######",
"##...##",
"##.....",
"##.....",
"##.....",
"##..###",
"##..###",
"##...##",
"##...##",
"##...##",
"#######",
".#####.",
],
'K': [
"###..##",
".##.##.",
".##.##.",
".####..",
".###...",
".###...",
".####..",
".##.##.",
".##.##.",
".##..##",
".##..##",
".##..##",
"###..##",
],
'L': [
"####...",
".##....",
".##....",
".##....",
".##....",
".##....",
".##....",
".##....",
".##....",
".##...#",
".##...#",
"#######",
"#######",
],
'N': [
"###..##",
"###..##",
"####.##",
"####.##",
"##.####",
"##.####",
"##..###",
"##..###",
"##...##",
"##...##",
"##...##",
"##...##",
"###..##",
],
'O': [
".#####.",
"#######",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
"##...##",
"#######",
".#####.",
],
'P': [
"######.",
"#######",
".##..##",
".##..##",
".##..##",
".##..##",
".######",
".#####.",
".##....",
".##....",
".##....",
".##....",
"####...",
],
'R': [
"######.",
"#######",
".##..##",
".##..##",
".##..##",
".##..##",
".######",
".#####.",
".##.##.",
".##.##.",
".##..##",
".##..##",
"###..##",
],
'S': [
".#####.",
"#######",
"##...##",
"##.....",
"###....",
".#####.",
"..#####",
"....###",
".....##",
"##...##",
"##...##",
"#######",
".#####.",
],
'T': [
"#######",
"#######",
"#..#..#",
"..###..",
"..###..",
"..###..",
"..###..",
"..###..",
"..###..",
"..###..",
"..###..",
"..###..",
".#####.",
],
'Y': [
"###.###",
"##...##",
"##...##",
".##.##.",
".##.##.",
"..###..",
"..###..",
"..###..",
"..###..",
"..###..",
"..###..",
"..###..",
".#####.",
],
}
def _draw_famicom_badge():
"""64x16 orange-red FAMICOM(R) badge with crisp 1px white outline (pal 2)."""
W, H = 64, 16
mask = [[False] * W for _ in range(H)]
cx = 2
for ch in 'FAMICOM':
g = FAMICOM_GLYPHS[ch]
gw = len(g[0])
for y, row in enumerate(g):
# Subtle 1px forward slant on upper half for retro Famicom energy.
slant = 1 if y < 5 else 0
for x, pix in enumerate(row):
if pix == '#':
xx = cx + x + slant
yy = 3 + y
if 0 <= xx < W and 0 <= yy < H:
mask[yy][xx] = True
cx += gw + 1
out = [[0] * W for _ in range(H)]
for y in range(H):
for x in range(W):
if mask[y][x]:
out[y][x] = 2 # vibrant orange-red
else:
for dy in (-1, 0, 1):
for dx in (-1, 0, 1):
yy, xx = y + dy, x + dx
if 0 <= yy < H and 0 <= xx < W and mask[yy][xx]:
out[y][x] = 1 # crisp 1px white outline
break
if out[y][x]:
break
# Crisp registered-trademark (R) circle on the right (white, color 1)
reg = [
"..####..",
".#....#.",
"#.#..#.#",
"#.##.#.#",
"#.#.#..#",
"#.#..#.#",
".#....#.",
"..####..",
]
for ry, row in enumerate(reg):
for rx, ch in enumerate(row):
if ch == '#':
out[3 + ry][56 + rx] = 1
return out
def _draw_main_logo(text='GO BASICS'):
"""152x40 bold calligraphic logo in exact JP 囲碁入門 palette & striping."""
W, H = 152, 40
f = ImageFont.truetype(FONT_LOGO, 136)
dr0 = ImageDraw.Draw(Image.new('L', (10, 10)))
bb = dr0.textbbox((0, 0), text, font=f)
tw, th = bb[2] - bb[0], bb[3] - bb[1]
tmp = Image.new('L', (tw + 32, th + 32), 0)
ImageDraw.Draw(tmp).text((16 - bb[0], 16 - bb[1]), text, font=f, fill=255)
target_w, target_h = W - 6, H - 6
tmp = tmp.resize((target_w, target_h), Image.Resampling.LANCZOS)
px = tmp.load()
mask = [[False] * W for _ in range(H)]
for y in range(target_h):
for x in range(target_w):
if px[x, y] >= 118:
mask[y + 2][x + 3] = True
# Remove isolated 1px speckles so serifs stay crisp.
clean = [row[:] for row in mask]
for y in range(1, H - 1):
for x in range(1, W - 1):
if mask[y][x]:
nb = (mask[y - 1][x] + mask[y + 1][x] +
mask[y][x - 1] + mask[y][x + 1])
if nb == 0:
clean[y][x] = False
mask = clean
# Exact 囲碁入門 horizontal stripe gradient:
# bright green top -> alternating green/black stripes -> jet black bottom
stripe_map = {17: 3, 18: 2, 19: 3, 20: 3, 21: 2, 22: 2, 23: 3}
out = [[0] * W for _ in range(H)]
for y in range(H):
for x in range(W):
if mask[y][x]:
if y < 17:
out[y][x] = 2 # bright green
elif y in stripe_map:
out[y][x] = stripe_map[y]
else:
out[y][x] = 3 # jet black
# Crisp 1px white outline all around the logo.
for y in range(H):
for x in range(W):
if mask[y][x]:
continue
for dy in (-1, 0, 1):
for dx in (-1, 0, 1):
yy, xx = y + dy, x + dx
if 0 <= yy < H and 0 <= xx < W and mask[yy][xx]:
out[y][x] = 1 # white outline
break
if out[y][x]:
break
return out
def _draw_menu_letter(ch):
"""8x16 bold pixel-serif letter in JP sunset gradient (pal 3)."""
g = MENU_SERIF[ch]
# Exact JP vertical sunset bands (matching jp_menu_2x.png):
# top white(1) -> cream-gold(2) -> alternating coral/cream -> coral(3)
band = [1, 1, 1, 1, 1, 2, 2, 2, 3, 2, 3, 3, 3, 3, 3, 3]
out = [[0] * 8 for _ in range(16)]
for y, row in enumerate(g):
for x, pix in enumerate(row):
if pix == '#':
out[2 + y][1 + x] = band[2 + y]
return out
def _draw_subtitle_stamp(chr_):
"""Draw crisp 56x16 white stamp 'NIHON KIIN / APPROVED' into CHR bank 7.
Uses the exact 14 sprite tiles of the original 日本棋院推薦 stamp at
their original $E57E OAM positions (x=152..208, y=55..71, sprite pal 3:
color 1 = white box, color 3 = crisp black ink).
"""
W, H = 56, 16
# Solid white stamp (color 1) with clean 1px transparent padding on top/bot
# would shrink height; original JP stamp fills the full 56x16 white box
# (1px white border inside the 56x16 rectangle, black text in color 3).
canvas = [[1] * W for _ in range(H)]
def put_4x5(text, y0):
# Measure width (4px per char except I=3, space=2; 1px spacing)
total_w = sum(len(FONT_4X5[c][0]) for c in text) + len(text) - 1
x0 = (W - total_w) // 2
cx = x0
for ch in text:
g = FONT_4X5[ch]
for gy, row in enumerate(g):
for gx, p in enumerate(row):
if p == '#':
canvas[y0 + gy][cx + gx] = 3 # black ink
cx += len(g[0]) + 1
put_4x5('NIHON KIIN', 2)
put_4x5('APPROVED', 9)
top_tiles = [0x95, 0x96, 0x97, 0x98, 0x74, 0x75, 0x76]
bot_tiles = [0xA5, 0xA6, 0xA7, 0xA8, 0x84, 0x85, 0x86]
for row_idx, tile_row in enumerate((top_tiles, bot_tiles)):
for col_idx, t in enumerate(tile_row):
base = 7 * 4096 + t * 16
for y in range(8):
lo = hi = 0
for x in range(8):
v = canvas[row_idx * 8 + y][col_idx * 8 + x]
if v & 1:
lo |= 1 << (7 - x)
if v & 2:
hi |= 1 << (7 - x)
chr_[base + y] = lo
chr_[base + 8 + y] = hi
def build(b3, chr_):
"""Rewrite CHR bank 0, CHR bank 7 stamp, and return compressed Screen 0."""
s0 = bytearray(make_book.decompress(b3, 0)[0])
# Reserve every tile used by the mountain frame, runtime animation, clouds,
# and "(c)1991 I'MAX" so nothing animated or decorative is ever touched.
keep = set(ANIM_TILES) | {0x00}
for r in range(30):
for c in range(32):
in_fam = (4 <= r <= 5 and 2 <= c <= 9)
in_logo = (2 <= r <= 8 and 11 <= c <= 29)
in_menu = ((20 <= r <= 21 and 8 <= c <= 18) or
(23 <= r <= 24 and 6 <= c <= 17) or
(26 <= r <= 27 and 4 <= c <= 16))
if not (in_fam or in_logo or in_menu):
keep.add(s0[r * 32 + c])
else:
s0[r * 32 + c] = 0
free = sorted(set(range(256)) - keep)
pi = 0
tile_cache = {}
def get_tile(pix):
nonlocal pi
if all(v == 0 for row in pix for v in row):
return 0
key = tuple(tuple(row) for row in pix)
if key in tile_cache:
return tile_cache[key]
assert pi < len(free), f'ran out of free CHR0 title tiles ({pi}/{len(free)})'
code = free[pi]
pi += 1
tile_cache[key] = code
for y in range(8):
lo = hi = 0
for x in range(8):
v = pix[y][x]
if v & 1:
lo |= 1 << (7 - x)
if v & 2:
hi |= 1 << (7 - x)
chr_[code * 16 + y] = lo
chr_[code * 16 + 8 + y] = hi
return code
def blit(canvas, r0, c0):
H, W = len(canvas), len(canvas[0])
for ty in range(H // 8):
for tx in range(W // 8):
tile = [[canvas[ty * 8 + y][tx * 8 + x] for x in range(8)]
for y in range(8)]
s0[(r0 + ty) * 32 + (c0 + tx)] = get_tile(tile)
def set_attr(tr, tc, pal):
idx = 960 + (tr // 4) * 8 + (tc // 4)
sh = ((tr % 4) // 2) * 4 + ((tc % 4) // 2) * 2
s0[idx] = (s0[idx] & ~(3 << sh)) | ((pal & 3) << sh)
# 1. Left badge: FAMICOM(R) in rows 4-5, cols 2-9 (Palette 2).
blit(_draw_famicom_badge(), 4, 2)
for r in (4, 5):
for c in range(2, 10):
set_attr(r, c, 2)
# 2. Main calligraphic logo: GO BASICS in rows 2-6, cols 11-29 (Palette 0).
blit(_draw_main_logo('GO BASICS'), 2, 11)
for r in range(2, 7):
for c in range(11, 30):
set_attr(r, c, 0)
# 3. Sunset menu items aligned beside the white Go-stone cursor (Palette 3).
menu_items = [
(20, 8, 'GO LESSONS'),
(23, 6, 'PLAY GO'),
(26, 4, 'RANK TEST'),
]
letter_cache = {}
for r0, c0, word in menu_items:
for i, ch in enumerate(word):
if ch == ' ':
continue
if ch not in letter_cache:
g = _draw_menu_letter(ch)
letter_cache[ch] = (get_tile(g[:8]), get_tile(g[8:]))
t0, t1 = letter_cache[ch]
s0[r0 * 32 + c0 + i] = t0
s0[(r0 + 1) * 32 + c0 + i] = t1
for r in (r0, r0 + 1):
for c in range(c0, c0 + len(word)):
set_attr(r, c, 3)
# 4. Subtitle stamp in CHR bank 7 (original $E57E OAM positions untouched).
_draw_subtitle_stamp(chr_)
comp = make_book.compress(s0)
print(f'title screen: {pi}/{len(free)} CHR0 tiles, compressed {len(comp)}/{0x020A} bytes')
return comp
|