summaryrefslogtreecommitdiff
path: root/xpp/cmd2inc
blob: f1aa9c96f9ccfde121b08fcdf39ed8e4d4677a32 (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
#! /usr/bin/perl -w

use strict;

my $input;
my $header;
my $comment;
my $len;

@ARGV == 1 or @ARGV == 2 or die "Usage: $0 <infile> <outfile>\n";
$input = $ARGV[0];
$header = $ARGV[1];
open(IF, "$input") or die "Failed to open '$input': $!\n";

if($header) {
	open(HF, ">$header") or die "Failed to write '$header': $!\n";
	select HF;
}

while(<IF>) {
	chomp;
	undef $comment;
	s/\r//;				# CRLF -> LF
	if(s/\s*[;#]\s*(.*?)$//) {	# Comments
		$comment = $1;
	}
	if(/^\s*$/) {		# Empty lines
		next;
	}
	my ($slic0, $slic1, $slic2, $slic3, $op, @data) = split;
	my $slic = "$slic3$slic2$slic1$slic0";
	die "Bad slic address '$slic'" if length($slic) != 8;
	die "Bad op (op=%op)\n" if $op !~ /^[WR][DI]$/;
	if($op =~ /D$/) {	# Direct
		die "Bad input at line $.: @data=(@data)\n" if @data != 2;
		#                   len      reg    data
		printf "S_(0x$slic,\t0x02,\t0x%02s, 0x%02s),\t", $data[0], $data[1];
	} else {		# Indirect
		die "Bad input at line $.: @data=(@data)\n" if @data != 3;
		#                   len          data1         data2          reg
		printf "S_(0x$slic,\t0x06,\t0x1C, 0x%02s, 0x1D, 0x%02s, 0x1E, 0x%2s),\t", $data[2], $data[1], $data[0];
	}
} continue {
	if(defined($comment)) {
		print "// $comment";
	}
	print "\n";
}