Skip to content

Instantly share code, notes, and snippets.

@lynn
Created February 22, 2018 20:26
Show Gist options
  • Save lynn/2bf48ca4fe4f34acc1bd7b767c34049e to your computer and use it in GitHub Desktop.
Save lynn/2bf48ca4fe4f34acc1bd7b767c34049e to your computer and use it in GitHub Desktop.
Describe Game Genie codes
#!/usr/bin/env python3
import sys
def describe(code):
"""
Return a written description of what a Game Genie code does.
Args:
code (str): A 6- or 8-letter Game Genie code like 'SXIOPO' or 'PEVXIYGA'.
Returns:
str: A description of the code.
Raises:
ValueError: If the given code is invalid.
"""
# Translate the code into nibbles.
try:
nibbles = []
for c in code:
nibbles.append('APZLGITYEOXUKSVN'.index(c))
except ValueError:
raise ValueError('%s contains %r, which is invalid in a Game Genie code.' % (code, c))
if len(nibbles) not in (6, 8):
raise ValueError('%s is invalid: Game Genie codes are 6 or 8 letters long.' % code)
# Unscramble the code.
n0, n1, n2, n3, n4, n5 = nibbles[:6]
address = ((n3 & 7) << 12) | ((n5 & 7) << 8) | ((n4 & 8) << 8) \
| ((n2 & 7) << 4) | ((n1 & 8) << 4) | (n4 & 7) | (n3 & 8)
if len(nibbles) == 6:
data = ((n1 & 7) << 4) | ((n0 & 8) << 4) | (n0 & 7) | (n5 & 8)
return '{0} will make reads to 0x{1:04x} always yield {2} (0x{2:02x}).'.format(code, address, data)
elif len(nibbles) == 8:
n6, n7 = nibbles[6:8]
data = ((n1 & 7) << 4) | ((n0 & 8) << 4) | (n0 & 7) | (n7 & 8)
compare = ((n7 & 7) << 4) | ((n6 & 8) << 4) | (n6 & 7) | (n5 & 8)
return '{0} will make reads to 0x{1:04x} always yield {2} (0x{2:02x}) ' \
'when the actual value is {3} (0x{3:02x}).'.format(code, address, data, compare)
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit('Usage: %s [Game Genie code]' % sys.argv[0])
for code in sys.argv[1:]:
try:
print(describe(code.upper()))
except ValueError as e:
sys.stderr.write('{}\n'.format(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment