-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.py
executable file
·145 lines (118 loc) · 3.84 KB
/
decode.py
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
#!/usr/bin/python3
import os
import glob
import json
from struct import unpack_from
from typing import Dict, Any
PRINTED_YET = 0
M68K_JSON_PATH = os.getcwd() + '/v1'
REG_ORDER = ['d0', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7',
'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'usp',
'ssp', 'sr', 'pc'
]
def read_name(content, ptr):
numbytes, magic_num = unpack_from('<II', content, ptr)
ptr += 8
assert magic_num == 0x89ABCDEF
strlen = unpack_from('<I', content, ptr)[0]
ptr += 4
bs = unpack_from(str(strlen) + 's', content, ptr)[0]
ptr += strlen
nstr = bs.decode('utf-8')
return ptr, nstr
def read_transactions(content, ptr):
numbytes, magic_num = unpack_from("<II", content, ptr)
assert magic_num == 0x456789AB
ptr += 8
transactions = []
num_cycles, num_transactions = unpack_from("<II", content, ptr)
if num_cycles == 0:
global PRINTED_YET
PRINTED_YET += 1
ptr += 8
for i in range(0, num_transactions):
tw, cycles = unpack_from("<BI", content, ptr)
ptr += 5
if tw == 0:
transactions.append(['n', cycles])
continue
fc, addr_bus, data_bus, UDS, LDS = unpack_from("<IIIII", content, ptr)
bw = UDS + LDS
ptr += 20
if tw == 1: # write
tws = 'w'
elif tw == 2: # read
tws = 'r'
elif tw == 3: # TAS cycle
tws = 't'
elif tw == 4: # read address error (no AS assert)
tws = 're'
elif tw == 5: # write address error (no AS assert)
tws = 'we'
else:
raise Exception('BAD KIND')
transactions.append([tws, cycles, fc, addr_bus, '.w' if bw == 2 else '.b', data_bus, UDS, LDS])
return ptr, transactions, num_cycles
def read_state(content, ptr):
st = {}
numbytes, magic_num = unpack_from('<II', content, ptr)
ptr += 8
assert magic_num == 0x01234567
for a in REG_ORDER:
st[a] = unpack_from("<I", content, ptr)[0]
ptr += 4
pf0, pf1 = unpack_from("<II", content, ptr)
ptr += 8
st['prefetch'] = [pf0, pf1]
# RAM 6-byte values
num_rams = unpack_from("<I", content, ptr)[0]
ptr += 4
st['ram'] = []
for i in range(0, num_rams):
addr, data = unpack_from("<IH", content, ptr)
ptr += 6
assert addr < 0x1000000
st['ram'].append([addr, (data >> 8)])
st['ram'].append([addr | 1, data & 0xFF])
return ptr, st
def decode_test(content, ptr):
test = {}
numbytes, magic_num = unpack_from('<II', content, ptr)
assert magic_num == 0xABC12367
ptr += 8
ptr, test['name'] = read_name(content, ptr)
ptr, test['initial'] = read_state(content, ptr)
ptr, test['final'] = read_state(content, ptr)
ptr, test['transactions'], test['length'] = read_transactions(content, ptr)
return ptr, test
def decode_file(infilename, outfilename):
print('DECODE', infilename)
global PRINTED_YET
with open(infilename, 'rb') as infile:
content = infile.read()
ptr = 0
magic_num, num_tests = unpack_from('<II', content, ptr)
assert magic_num == 0x1A3F5D71
ptr += 8
tests = []
PRINTED_YET = 0
for i in range(0, num_tests):
ptr, test = decode_test(content, ptr)
tests.append(test)
if PRINTED_YET > 0:
print('NUM WITH NO CYCLES: ', PRINTED_YET)
if os.path.exists(outfilename):
os.unlink(outfilename)
with open(outfilename, 'w') as outfile:
outfile.write(json.dumps(tests, indent=2))
def do_path(where):
print("Doing path...", where + '/**.json.bin')
fs = glob.glob(where + '/**.json.bin')
for fname in fs:
decode_file(fname, fname[:-4])
#r = M68K_JSON_PATH + "/NEG.b.json.bin"
#decode_file(r, r[:-4])
def main():
do_path(M68K_JSON_PATH)
if __name__ == '__main__':
main()