Skip to content

Instantly share code, notes, and snippets.

@mcgrew
Created April 16, 2020 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcgrew/0d740fbffe6921813008a40d459143e1 to your computer and use it in GitHub Desktop.
Save mcgrew/0d740fbffe6921813008a40d459143e1 to your computer and use it in GitHub Desktop.
NES Game Genie code generator
#!/usr/bin/python
# This python script will generate game genie codes for NES
# Pass in the address and value, with an optional compare value
# in hexadecimal format and it will output a game genie code.
# This is quick and dirty and largely untested, so YMMV.
from sys import argv
if len(argv) < 3:
print(f'Usage: {argv[0]} <address> <value> [<compare>]')
print("All values should be in hexadecimal")
exit(-1)
letters = 'APZLGITYEOXUKSVN'
bit_order_6 = '1678H234:IJKLABCDMNO5EFG'
bit_order_8 = '1678H234:IJKLABCDMNOwEFGsxyz5tuv'
address = int(argv[1], 16) # between 0x8000 and 0xffff
value = int(argv[2], 16) # between 0x0 and 0xff
bit_order = bit_order_6
addr_int = ((value << 16) | (address)) & 0x7fff
if len(argv) > 3:
compare = int(argv[3], 16) # between 0x0 and 0xff
bit_order = bit_order_8
addr_int |= 0x1000
addr_int = (addr_int << 8) | (compare)
sorted_bit_order = sorted(bit_order)
code_int = 0
for i,v in enumerate(bit_order):
code_int <<= 1
bit_index = sorted_bit_order.index(v)
code_int |= (addr_int >> (len(bit_order) - 1 - bit_index) & 1)
gg_code = ''
for i in range(len(bit_order) - 4, -1, -4):
gg_code += letters[(code_int >> i) & 0xf]
print(gg_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment