summaryrefslogtreecommitdiffstats
path: root/src/target_dsp/calypso/bin2cfile.py
blob: 9456a6ac46b4ea31f73260458e2d9fe1b3c57767 (plain)
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
#!/usr/bin/env python

import struct
import sys

def group_by_n(s, n, do_join=True):
	return ( ''.join(x) for x in zip(*[s[i::n] for i in range(n)]) )
	

def main(pn, filename):
	# Get all bytes
	f = open(filename, 'r')
	d = f.read()
	f.close()

	# Get the data
	ops = ''.join([
		'0x%04x,%s' % (
			struct.unpack('=H', x)[0],
			'\n\t\t\t' if (i&3==3) else ' '
		)
		for i, x
		in enumerate(group_by_n(d, 2))
	])[:-1]

	ops = '\t\t\t' + ops
	if ops[-1] == '\t':
		ops = ops[:-4]

	# Name
	name = filename.split('.',1)[0]

	# Header / footer
	print """
#define _SA_DECL (const uint16_t *)&(const uint16_t [])

static const struct dsp_section %s[] = {
	{
		.addr = 0x,
		.size = 0x%04x,
		.data = _SA_DECL {
%s
		},
	},
	{ /* Guard */
		.addr = 0,
		.size = 0,
		.data = NULL,
	},
};

#undef _SA_DECL
""" % (name, len(d)/2, ops)


if __name__ == "__main__":
	main(*sys.argv)