blob: 0b3f2e6add6f4ddacfa6ea321a847eedd8f14364 (
plain) (
blame)
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
|
# Part of the Fifteen-Thieves Project
# Chris Xiong 2020
# License: Expat (MIT)
def roland_checksum(payload):
return (0x80 - sum(payload) % 0x80) % 0x80
def check_roland_checksum(msg):
return (0x80 - sum(msg[6:-2]) % 0x80) % 0x80 == msg[-2]
def int2belist(x):
ret = []
while x > 0:
ret = [x & 0xff] + ret
x >>= 8
return ret
def belist2int(x):
ret = 0
for i in x:
ret <<= 8
ret |= i
return ret
def int2b7belist(x):
ret = []
while x > 0:
ret = [x & 0x7f] + ret
x >>= 7
return ret
def strb7list(x):
return ' '.join([format(a, "02x") for a in x])
def b7belist2int(x):
ret = 0
for i in x:
ret <<= 7
ret |= i
return ret
def hexrep2b7val(x):
ret = 0
b = 0
while x > 0:
if x & 0xff > 0x7f:
raise ValueError("invalid value")
ret |= (x & 0xff) << (7 * b)
b += 1
x >>= 8
return ret
def padbelist(x, l=4):
return ([0]*(l-len(x))) + x
def int2packetint(x):
ret = []
while x > 0:
ret = [x & 0x0f] + ret
x >>= 4
return ret
def packetint2int(x):
ret = 0
for i in x:
ret <<= 4
ret |= i
return ret
def load_memoryregion(fn):
ret = []
with open(fn, "rb") as f:
while True:
addr = list(f.read(4))
if len(addr) < 4:
break
l = belist2int(list(f.read(4)))
data = list(f.read(l))
ret.append( (addr, data) )
return ret
def write_memoryregion(fn, mr):
with open(fn, "wb") as f:
for addr, data in mr:
l = padbelist(int2belist(len(data)))
f.write(bytearray(addr))
f.write(bytearray(l))
f.write(bytearray(data))
# vim: expandtab shiftwidth=4 tabstop=4
|