aboutsummaryrefslogtreecommitdiff
path: root/tools/rlink_make_speed_table/rlink_make_speed_table.pl
blob: 910fbf822cea578ff6af87b8ffe97ce25e9650cc (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/perl
#***************************************************************************
#*   Copyright (C) 2008 Lou Deluxe                                         *
#*   lou.openocd012@fixit.nospammail.net                                   *
#*                                                                         *
#*   This program is free software; you can redistribute it and/or modify  *
#*   it under the terms of the GNU General Public License as published by  *
#*   the Free Software Foundation; either version 2 of the License, or     *
#*   (at your option) any later version.                                   *
#*                                                                         *
#*   This program is distributed in the hope that it will be useful,       *
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
#*   GNU General Public License for more details.                          *
#*                                                                         *
#*   You should have received a copy of the GNU General Public License     *
#*   along with this program; if not, write to the                         *
#*   Free Software Foundation, Inc.,                                       *
#*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
#***************************************************************************

# A simple utility to read a list of files (names composed by numeric prescaler arguments) and compose a C source file defining data structures which hold the binary data read from those files.

my @speed_table = ();

print <<HEADER;
/* This file was created automatically by the following script:
 *   $0
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "rlink.h"
#include "st7.h"

HEADER

for $prescaler (sort {$b <=> $a} @ARGV) {
	my(@ary) = (
		byte_array_from_file(${prescaler} . "_init.dtc"),
		byte_array_from_file(${prescaler} . "_call.dtc")
	);

	for $i (@ary) {
		$i = sprintf("%d", $i);
	}
	$bytes = join(', ', @ary);
	$bytes =~ s/(^|\s)(.{70}?\S*)/\2\n/go;	# break up long lines
	$bytes =~ s/\n +/\n/go;
	$bytes =~ s/(^|\n)/\1\t/go;		# format nicely
	printf("static const u8 dtc_%d[] = {\n%s\n};\n\n", $prescaler, $bytes);
	push(@speed_table, sprintf("\tdtc_%d, sizeof(dtc_%d), (ST7_FOSC * 2) / (1000 * %d), %d\n", $prescaler, $prescaler, $prescaler, $prescaler));
}

printf("const rlink_speed_table_t rlink_speed_table[] = {{\n%s}};\n\n", join("}, {\n", @speed_table));
printf("const size_t rlink_speed_table_size = sizeof(rlink_speed_table) / sizeof(*rlink_speed_table);\n\n");


sub byte_array_from_file {
	my($filename) = @_;

	my(@array, $text, $i) = ();

	open(IN, '<', $filename) || die "$filename: $!";
	undef($/);
	$text = <IN>;
	close(IN);

	for($i = 0; $i < length($text); $i++) {
		push(@array, ord(substr($text, $i, 1)));
	}

	@array;
}