-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytesToNES.py
More file actions
executable file
·70 lines (62 loc) · 2.35 KB
/
bytesToNES.py
File metadata and controls
executable file
·70 lines (62 loc) · 2.35 KB
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
#!/usr/local/bin/python3
import sys
import argparse
parser = argparse.ArgumentParser(description="Converts bytes to NES controller strings, see '-d' for information on the format.")
parser.add_argument("-d", "--doc", help="display documentation on the NES controller string format and exit", action='store_true')
parser.add_argument('-f', "--format", help="change the format of output", choices=['bk2', 'fm2'])
parser.add_argument("-v", "--verbose", help="verbose human readable output, line is red if the controller combinaison is impossible a priori", action='store_true')
parser.add_argument("-b", "--byte", help="input one byte to convert")
args = parser.parse_args()
if (args.doc):
print("Mapping of the NES controller:")
print("7 - A\n6 - B\n5 - Select (s)\n4 - Start (S)\n3 - Up\n2 - Down\n1 - Left\n0 - Right\n")
indicators = "ABsSUDLR"
index = "76543210"
separator = "|"
print("---------------")
print(separator.join(index))
print(separator.join(indicators))
print("---------------")
exit(0)
def formatter(internal: dict[str, bool], fmt: str) -> str:
out = ""
if (fmt == "fm2"):
template = "|0|$|........||"
order = "RLDUTsBA"
sep = '.'
elif (fmt == "bk2"):
template = "|..|$|........|"
order = "UDLRSsBA"
sep = '.'
else:
template = "$"
order = "ABsSUDLR"
sep = '-'
for o in order:
out += o if internal[o if o != 'T' else 'S'] else sep
return template.replace('$', out)
def toIntern(byte: int) -> dict[str, bool]:
inputs_str = format(byte, '08b')
indicators = "ABsSUDLR"
dic = {}
for i, c in enumerate(inputs_str):
dic[indicators[i]] = True if c == "1" else False
return dic
def impossible(s: dict[str, bool]) -> bool:
return (s['U'] and s['D']) or (s['L'] and s['R'])
def display(data: bytes, fmt: str):
for b in data:
s = toIntern(b)
if (args.verbose):
print(('\033[31m' if impossible(s) else '\033[0m') + "0x{:02x}: ".format(b) + formatter(s, fmt))
else:
print(formatter(s, fmt))
if (args.byte != None):
byte = int(args.byte, 0)
if (byte < 0 or byte > 255):
print("Error: must be an uint8, between 0 and 255")
exit(2)
display(bytes([byte]), args.format)
else:
data = sys.stdin.buffer.read()
display(data, args.format)