require 'hsslib.pl';

@kanatab = (
# a
"\xa7", "\xb1", "\xa8", "\xb2", "\xa9", "\xb3", "\xaa", "\xb4", "\xab", "\xb5", 
# ka
"\xb6", "\xb6\xde", "\xb7", "\xb7\xde", "\xb8", "\xb8\xde", 
"\xb9", "\xb9\xde", "\xba", "\xba\xde", 
# sa
"\xbb", "\xbb\xde", "\xbc", "\xbc\xde", "\xbd", "\xbd\xde", 
"\xbe", "\xbe\xde", "\xbf", "\xbf\xde", 
# ta
"\xc0", "\xc0\xde", "\xc1", "\xc1\xde", "\xaf", "\xc2", "\xc2\xde", 
"\xc3", "\xc3\xde", "\xc4", "\xc4\xde", 
# na
"\xc5", "\xc6", "\xc7", "\xc8", "\xc9", 
# ha
"\xca", "\xca\xde", "\xca\xdf", "\xcb", "\xcb\xde", "\xcb\xdf", 
"\xcc", "\xcc\xde", "\xcc\xdf", "\xcd", "\xcd\xde", "\xcd\xdf", 
"\xce", "\xce\xde", "\xce\xdf", 
# ma
"\xcf", "\xd0", "\xd1", "\xd2", "\xd3",
# ya
"\xac", "\xd4", "\xad", "\xd5", "\xae", "\xd6",
# ra
"\xd7", "\xd8", "\xd9", "\xda", "\xdb",
# wa
"\xdc", "\xdc", "\xb2", "\xb4", "\xa6", "\xdd"
);

# customization

&hss'init('/dev/hss0');
if (&hss'testStart) {
	die "card not ready";
}
&hss'startSH;

# reset
&hss'kisoku("S4P4U0V32X0\n");

while (<>) {
	$line = $_;
	chop($line);
	$text = '';
	while (length($line)) {
		$ch = unpack('C', substr($line, 0, 1));
		if (0xa1 <= $ch && $ch <= 0xfe) {
			$text .= &convert(unpack('n', substr($line, 0, 2)));
			$line = substr($line, 2);
		} else {
			$text .= '/00 ';
			$line = substr($line, 1);
		}
	}

	$text =~ s/ +$//;
	if ($text !~ /\/00$/) {
		$text .= '/00';
	}
	$text .= ".\n";
#	print STDERR $text;
	&hss'kisoku($text);
}

exit 0;

sub convert {
	local($ch) = @_;
	if (0xa4a1 <= $ch && $ch <= 0xa4f3) {
		return $kanatab[$ch - 0xa4a1];
	} else {
		return "";
	}
}
