#! /usr/bin/perl

use strict;
use utf8;

my $LETTER_WIDTH = 5;

my $font = {

	0x00 => [ # zero (spase)
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
	], 
	
	0x20 => [ # space
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
	], 
	
	0x21 => [ # !
		[0,0,0,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,1,0,0],
	], 
	
	0x22 => [ # "
		[0,0,0,0,0],
		[0,1,0,1,0],
		[0,1,0,1,0],
		[0,1,0,1,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
	], 
	
	0x23 => [ # #
		[0,0,0,0,0],
		[0,1,0,1,0],
		[0,1,0,1,0],
		[1,1,1,1,1],
		[0,1,0,0,0],
		[1,1,1,1,1],
		[0,1,0,1,0],
		[0,1,0,1,0],
	],
	
	0x24 => [ # $
		[0,0,0,0,0],
		[0,0,1,0,0],
		[0,1,1,1,1],
		[1,0,1,0,0],
		[0,1,1,1,0],
		[0,0,1,0,1],
		[1,1,1,1,0],
		[0,0,1,0,0],
	],
	0x25 => [ # %
		[0,0,0,0,0],
		[1,1,0,0,0],
		[1,1,0,0,1],
		[0,0,0,1,0],
		[0,0,1,0,0],
		[0,1,0,0,0],
		[1,0,0,1,1],
		[0,0,0,1,1],
	],
	
	0x26 => [ # &
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
	],
	
	0x61 => [ # a
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,1,1,1,0],
		[0,0,0,0,1],
		[0,1,1,1,1],
		[1,0,0,0,1],
		[0,1,1,1,1],
	],
	
	0x62 => [ # b
		[0,0,0,0,0],
		[1,0,0,0,0],
		[1,0,0,0,0],
		[1,0,0,0,0],
		[1,1,1,1,0],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[1,1,1,1,0],
	],
	
	0x63 => [ # c
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,1,1,1,1],
		[1,0,0,0,0],
		[1,0,0,0,0],
		[1,0,0,0,0],
		[0,1,1,1,1],
	],
	
	0x64 => [ # d
		[0,0,0,0,0],
		[0,0,0,0,1],
		[0,0,0,0,1],
		[0,0,0,0,1],
		[0,1,1,1,1],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[0,1,1,1,1],
	],
	
	0x65 => [ # e
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,1,1,1,0],
		[1,0,0,0,1],
		[1,1,1,1,1],
		[1,0,0,0,0],
		[0,1,1,1,1],
	],
	
	0x66 => [ # f
		[0,0,0,0,0],
		[0,0,0,1,0],
		[0,0,1,0,1],
		[0,0,1,0,0],
		[0,1,1,1,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
	],
	
	0x67 => [ # g
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,1,1,1,1],
		[1,0,0,0,1],
		[0,1,1,1,1],
		[0,0,0,0,1],
		[1,1,1,1,0],
	],
	
	0x68 => [ # h
		[0,0,0,0,0],
		[1,0,0,0,0],
		[1,0,0,0,0],
		[1,0,0,0,0],
		[1,1,1,1,0],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[1,0,0,0,1],
	],
	
	0x69 => [ # i
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,1,0,0],
		[0,0,0,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
	],
	
	0x6a => [ # j
		[0,0,0,0,0],
		[0,0,0,1,0],
		[0,0,0,0,0],
		[0,0,0,1,0],
		[0,0,0,1,0],
		[0,0,0,1,0],
		[1,0,0,1,0],
		[0,1,1,0,0],
	],
	
	0x6b => [ # k
		[0,0,0,0,0],
		[1,0,0,0,0],
		[1,0,0,0,0],
		[1,0,0,1,0],
		[1,0,1,0,0],
		[1,1,0,0,0],
		[1,0,1,0,0],
		[1,0,0,1,0],
	],
	
	0x6c => [ # l
		[0,0,0,0,0],
		[0,1,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,1,1,1,0],
	],
	
	0x6d => [ # m
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,1,0,1,1],
		[1,0,1,0,1],
		[1,0,1,0,1],
		[1,0,1,0,1],
		[1,0,0,0,1],
	],
	
	0x6e => [ # n
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,0,1,1,0],
		[1,1,0,0,1],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[1,0,0,0,1],
	],
	
	0x6f => [ # o
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,1,1,1,0],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[0,1,1,1,0],
	],
	
	0x70 => [ # p
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,1,1,1,0],
		[1,0,0,0,1],
		[1,1,1,1,0],
		[1,0,0,0,0],
		[1,0,0,0,0],
	],
	
	0x71 => [ # q
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,1,1,1,1],
		[1,0,0,0,1],
		[0,1,1,1,1],
		[0,0,0,0,1],
		[0,0,0,0,1],
	],
	
	0x72 => [ # r
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,0,1,1,0],
		[1,1,0,0,1],
		[1,0,0,0,0],
		[1,0,0,0,0],
		[1,0,0,0,0],
	],
	
	0x73 => [ # s
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,1,1,1,1],
		[1,0,0,0,0],
		[0,1,1,1,0],
		[0,0,0,0,1],
		[1,1,1,1,0],
	],
	
	0x74 => [ # t
		[0,0,0,0,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[1,1,1,1,1],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,0,1,0,1],
		[0,0,0,1,0],
	],
	
	0x75 => [ # u
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[0,1,1,1,0],
	],
	
	0x76 => [ # v
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[0,1,0,1,0],
		[0,0,1,0,0],
	],
	
	0x77 => [ # w
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,0,0,0,1],
		[1,0,0,0,1],
		[1,0,1,0,1],
		[1,0,1,0,1],
		[0,1,0,1,0],
	],
	
	0x78 => [ # x
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,0,0,0,1],
		[0,1,0,1,0],
		[0,0,1,0,0],
		[0,1,0,1,0],
		[1,0,0,0,1],
	],
	
	0x79 => [ # y
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,0,0,0,1],
		[0,1,0,1,0],
		[0,0,1,0,0],
		[0,0,1,0,0],
		[0,1,0,0,0],
	],
	
	0x7a => [ # z
		[0,0,0,0,0],
		[0,0,0,0,0],
		[0,0,0,0,0],
		[1,1,1,1,1],
		[0,0,0,1,0],
		[0,0,1,0,0],
		[0,1,0,0,0],
		[1,1,1,1,1],
	],
	
	0xff => [ # unknown char
		[0,0,0,0,0],
		[0,1,1,1,0],
		[1,0,0,0,1],
		[0,0,0,0,1],
		[0,0,0,1,0],
		[0,0,1,0,0],
		[0,0,0,0,0],
		[0,0,1,0,0],
	],
};

open( my $fh, '>', './chargen.asm') or die "unable to open output file! $! \n";

print $fh ";************** CHARACTER GENERATOR\n";
print $fh "getletter\n"; # label
print $fh "\tcall    getadd\n";# reading letter's code from EEPROM

foreach my $chr (sort{$a <=> $b}keys %$font){
	my $hex_chr = sprintf("%x",$chr);
	print $fh "\tmovlw\t0x$hex_chr\n";
	print $fh "\txorwf\tscratch,0\n";
	print $fh "\tbtfsc\tstatus,z\n";
	print $fh "\tgoto\tltr_0x$hex_chr\n\n";
       
}

print $fh "\tgoto\tltr_0xff\n\n"; # letter not recognized - draw empty space	


print $fh ";************** LETTER IMAGES\n";

foreach my $chr (sort{$a <=> $b}keys %$font){
	
	my $hex_chr = sprintf("%x",$chr);
	print $fh "ltr_0x$hex_chr\n";
	
	foreach my $col (1..$LETTER_WIDTH){
		my @tmp = ();
		foreach my $row ( reverse @{$$font{$chr}} ){
			push @tmp, $$row[$col-1];
		}
		print $fh "\tmovlw\tb'" . join('', @tmp) . "'\n";
		print $fh "\tmovwf\tbuff_$col\n"
	}	
	
	print $fh "\tgoto\tbuff\n\n";
	
}














