Last active
November 18, 2020 01:25
-
-
Save xspager/3f48b844fb9408359d51 to your computer and use it in GitHub Desktop.
Dump some stuff from a GameBoy Rom. Saves the Nintendo logo from it if PIL (or Pillow) is installed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import array | |
if len(sys.argv) < 2: | |
filename = "pokeyellow.gbc" | |
else: | |
filename = sys.argv[1] | |
rom = array.array('B') | |
rom.fromstring(open(filename, "rb").read()) | |
logo = rom[0x104:0x134] | |
title = rom[0x134:0x143] | |
def byte_to_1bpp(n): | |
return [ | |
(n & 0b10000000) >> 7, | |
(n & 0b01000000) >> 6, | |
(n & 0b00100000) >> 5, | |
(n & 0b00010000) >> 4, | |
(n & 0b00001000) >> 3, | |
(n & 0b00000100) >> 2, | |
(n & 0b00000010) >> 1, | |
n & 0b00000001 | |
] | |
#print byte_to_1bpp(0xFF) | |
#print byte_to_1bpp(0x0F) | |
#print byte_to_1bpp(0xF0) | |
vmem = array.array('B', [ | |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF | |
]) | |
# Interesting Python GB emulator: https://bitbucket.org/pypy/lang-gameboy/src/410e34ee2cffa503a4107439b6d4ab257f377095/pygirl/gameboy.py?at=default#cl-203 | |
# GBC boot rom dissasemble: http://www.its.caltech.edu/~costis/cgb_hack/gbc_bios.txt | |
LINE_IN_BYTES = 6 | |
sprite = 0 | |
for n in xrange(0, len(logo), 24): | |
for byte_i in xrange(n, n+24, 4): | |
for i in xrange(0, 4): | |
high_nibble = logo[byte_i+i] & 0b11110000 | |
low_nible = logo[byte_i+i] & 0b00001111 | |
if i < 2: | |
offset = (i * LINE_IN_BYTES * 2) + sprite | |
vmem[offset] &= high_nibble | |
vmem[offset + LINE_IN_BYTES] &= low_nible << 4 # skip to the next line | |
else: | |
offset = ((i-2) * LINE_IN_BYTES * 2) + sprite | |
vmem[offset] |= high_nibble >> 4 | |
vmem[offset + LINE_IN_BYTES] |= low_nible | |
sprite += 1 | |
sprite = 24 | |
try: | |
from PIL import Image | |
image = Image.frombytes('1', (48,8), vmem) | |
image.save('logo.png') | |
except: | |
pass | |
for line_start in xrange(0, len(logo), LINE_IN_BYTES): | |
line_slice = vmem[line_start:line_start+LINE_IN_BYTES] | |
line = sum([byte_to_1bpp(n) for n in line_slice], []) | |
print ''.join([str(n) for n in line]).replace('0', ' ').replace('1', '#') | |
print "Title: %s" % title.tostring() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment