Original author: Pierre Sarrazin. Preserve upstream GPLv3-or-later and file-specific notices. Source: http://sarrazip.com/dev/coco-letters-0.1.0.tar.gz Archive SHA-256: ad7c7771845e1a35014644fb562634a558abee632e1d7738683db6e9e9e46b94
389 lines
11 KiB
Perl
389 lines
11 KiB
Perl
#!/usr/bin/perl -w
|
|
#
|
|
# build-data.pl
|
|
# Copyright (C) 2026 Pierre Sarrazin <http://sarrazip.com/>
|
|
#
|
|
# 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
##########################################################################
|
|
#
|
|
# The game's data file contains font data followed by a list of 8k blocks,
|
|
# each one of which has this format:
|
|
# - Unsigned big-endian word giving the number of dictionary words in
|
|
# the file.
|
|
# - List of dictionary words, where each word is a string that ends
|
|
# in a character whose code is >= 0x80. The actual code of the last
|
|
# character of the word is this code & 0x7F. Example: "ABC" is
|
|
# encoded as "AB\xC3", because 0xC3 & 0x7F = 67 = ord('C').
|
|
# - Padding to have each block contain 8192 bytes.
|
|
|
|
require 5.14.0; # for /r modifier on s///
|
|
use strict;
|
|
|
|
|
|
$| = 1; # no buffering on STDOUT
|
|
|
|
my ($wordListFilename, $dataFilename, $wordLengthIndexFilename,
|
|
$minWordLen, $maxWordLen, $maxBlocks);
|
|
|
|
my $sectorSize = 256; # CoCo floppy sector size
|
|
my $mmuBlockLen = 8192; # CoCo 3 MMU block size
|
|
my $maxBlockLen = $mmuBlockLen - 2; # 1st word of block gives number of words in block
|
|
my $numWrittenBlocks = 0;
|
|
my $fhBlock; # data file whose name is in $dataFilename
|
|
my $dictStartOffset; # offset in bytes where the dictionary starts in $fhBlock
|
|
|
|
|
|
# Pads $fhBlock to a multiple of the argument.
|
|
# Uses $dictStartOffset.
|
|
#
|
|
sub
|
|
padDCTFile
|
|
{
|
|
my ($size) = @_;
|
|
die unless $size > 0;
|
|
|
|
my $fileLen = tell($fhBlock) - $dictStartOffset;
|
|
if ($fileLen < 0)
|
|
{
|
|
print "ERROR: failed to get writing position in file $dataFilename: $!\n";
|
|
exit 1;
|
|
}
|
|
my $fileLenAfterPadding = int(($fileLen + $size - 1) / $size) * $size;
|
|
my $paddingNeeded = $fileLenAfterPadding - $fileLen;
|
|
if ($paddingNeeded > 0)
|
|
{
|
|
print "Padding with $paddingNeeded bytes.\n";
|
|
print $fhBlock " " x $paddingNeeded;
|
|
}
|
|
}
|
|
|
|
|
|
# Receives a reference to an array of words.
|
|
# Writes those words to $fhBlock.
|
|
# Increments $numWrittenBlocks upon success.
|
|
#
|
|
sub writeBlock
|
|
{
|
|
my ($raBlock) = @_;
|
|
#print "writeBlock: ", $raBlock->[0], "\n";
|
|
|
|
my $numWordsInBlock = scalar(@$raBlock);
|
|
die unless $numWordsInBlock <= 65535;
|
|
|
|
print "Writing $numWordsInBlock words to $dataFilename\n";
|
|
|
|
print $fhBlock pack("n", $numWordsInBlock); # 16-bit big endian
|
|
|
|
my $wordIndexInBlock = 0;
|
|
for my $word (@$raBlock)
|
|
{
|
|
#print "Word at index $wordIndexInBlock: '$word'\n";
|
|
++$wordIndexInBlock;
|
|
my $prefix = substr($word, 0, length($word) - 1);
|
|
my $lastChar = substr($word, length($word) - 1);
|
|
my $encodedWord = $prefix . chr(ord($lastChar) + 0x80);
|
|
print $fhBlock $encodedWord;
|
|
}
|
|
|
|
++$numWrittenBlocks;
|
|
}
|
|
|
|
|
|
# Sort comparator.
|
|
#
|
|
sub byDecreasingLength
|
|
{
|
|
return length($b) <=> length($a);
|
|
}
|
|
|
|
|
|
# Parses the hex bytes in a C file and writes the binary data to $fh.
|
|
#
|
|
sub writeFont($$)
|
|
{
|
|
my ($fh, $cFilePath) = @_;
|
|
|
|
my $numBytesWritten = 0;
|
|
my $cFile;
|
|
if (!open($cFile, $cFilePath))
|
|
{
|
|
print "$0: ERROR: failed to open $cFilePath: $!\n";
|
|
exit 1;
|
|
}
|
|
while (<$cFile>)
|
|
{
|
|
# Line example:
|
|
# 0xd8, 0x88, 0x78, 0x98, 0xe8, 0x18, 0xd8, 0xf8, // 36 (0x24)
|
|
#
|
|
s!\s*//.*$!!; # remove any trailing comment
|
|
if (/^\s*(0x.*),/)
|
|
{
|
|
my @bytes = split /,\s*/, $1; # 0xNN, ..., 0xNN
|
|
@bytes = map { hex(s/^0x//r) } @bytes; # /r returns modified string instead of success count
|
|
my $binary = pack("C*", @bytes);
|
|
$numBytesWritten += length($binary);
|
|
print $fh $binary; # array of numbers converted to raw binary
|
|
}
|
|
}
|
|
close $cFile;
|
|
print "Wrote $numBytesWritten font bytes obtained from $cFilePath\n";
|
|
}
|
|
|
|
|
|
sub writeFonts($)
|
|
{
|
|
my ($fh) = @_;
|
|
|
|
writeFont($fh, "font4x8-ascii.c");
|
|
writeFont($fh, "font5x8-A-Z.c");
|
|
padDCTFile($sectorSize); # pad last sector to 256 bytes
|
|
}
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
die unless @ARGV == 6;
|
|
|
|
($wordListFilename, $dataFilename, $wordLengthIndexFilename,
|
|
$minWordLen, $maxWordLen, $maxBlocks) = @ARGV;
|
|
|
|
|
|
die unless defined $maxBlocks;
|
|
|
|
# Read the allowed words from $wordListFilename.
|
|
#
|
|
my $fhWords;
|
|
if (!open($fhWords, $wordListFilename))
|
|
{
|
|
print "$0: ERROR: $wordListFilename: $!\n";
|
|
exit 1;
|
|
}
|
|
|
|
my %wordTable; # key: upper-case word; value: 1
|
|
my $maxWordLenSeen = 0;
|
|
my $numRejectedWords = 0;
|
|
|
|
while (<$fhWords>)
|
|
{
|
|
s/\s+$//; # remove trailing white space
|
|
tr/a-z/A-Z/; # convert to uppercase
|
|
my $len = length($_);
|
|
if ($len >= $minWordLen && $len <= $maxWordLen && /^[A-Z]+$/)
|
|
{
|
|
$maxWordLenSeen = $len if $len > $maxWordLenSeen;
|
|
$wordTable{$_} = 1;
|
|
}
|
|
else
|
|
{
|
|
#print "Warning: word '$_' rejected.\n";
|
|
++$numRejectedWords;
|
|
}
|
|
}
|
|
|
|
close $fhWords;
|
|
|
|
my @selectedWords = sort keys %wordTable;
|
|
print "Number of words of lengths $minWordLen..$maxWordLen selected from $wordListFilename: ",
|
|
scalar(@selectedWords), "\n";
|
|
|
|
die unless @selectedWords > 0;
|
|
|
|
# Dump the selected words in a text file.
|
|
#
|
|
#open(SELECTED_WORDS, ">build-data-selected-words.txt") or die;
|
|
#print SELECTED_WORDS join("\n", @selectedWords), "\n";
|
|
#close(SELECTED_WORDS) or die;
|
|
|
|
print "Length of the longest selected word: $maxWordLenSeen\n";
|
|
print "Number of rejected words: $numRejectedWords\n";
|
|
|
|
die unless $maxWordLenSeen > 0;
|
|
|
|
# Sort the words from longest to shortest.
|
|
#
|
|
@selectedWords = sort byDecreasingLength @selectedWords;
|
|
|
|
|
|
# Create the output file.
|
|
#
|
|
if (!open($fhBlock, "> $dataFilename"))
|
|
{
|
|
print "$0: ERROR: failed to create $dataFilename: $!\n";
|
|
return undef;
|
|
}
|
|
binmode $fhBlock;
|
|
|
|
|
|
# Write font data (not related to the dictionary) at the start
|
|
# of the file.
|
|
#
|
|
writeFonts($fhBlock);
|
|
|
|
|
|
# Note where the font data ends and the dictionary data is about
|
|
# to be written.
|
|
#
|
|
$dictStartOffset = tell($fhBlock);
|
|
|
|
die unless $dictStartOffset % 256 == 0;
|
|
|
|
|
|
my @curBlock; # array of words
|
|
my $curBlockLen = 0; # length in bytes of current block, whose length must be <= $maxBlockLen;
|
|
# this is also the total length of the strings in @curBlock
|
|
my $curBlockWordCount = 0; # number of elements in @curBlock
|
|
|
|
my $totalLen = 0;
|
|
my $lenPrevWord; # length of the word processed by preceding iteration
|
|
|
|
|
|
# key: word length; value: {block, wordOffset, offset}.
|
|
# Describes the contents of a single block, not the whole dictionary.
|
|
# Each entry gets written to file $wordLengthIndexFilename.
|
|
#
|
|
my %wordLengthIndex;
|
|
|
|
for my $word (@selectedWords) # for each word in decreasing order of length
|
|
{
|
|
#print "Word: [$word], lenPrevWord=", $lenPrevWord || -1, "\n";
|
|
$totalLen += length($word);
|
|
|
|
if ($curBlockLen + length($word) > $maxBlockLen) # if no room in current block
|
|
{
|
|
# Any preceding block must be at an offset that is
|
|
# a multiple of 8192 bytes.
|
|
padDCTFile($mmuBlockLen);
|
|
|
|
writeBlock(\@curBlock);
|
|
|
|
# Reinit block.
|
|
@curBlock = ();
|
|
$curBlockLen = 0;
|
|
$curBlockWordCount = 0;
|
|
}
|
|
|
|
# If currentword has different length than previous one,
|
|
# declare some array entries.
|
|
#
|
|
if (!defined $lenPrevWord || length($word) != $lenPrevWord)
|
|
{
|
|
my $firstLenToDo = (defined $lenPrevWord ? $lenPrevWord - 1 : length($word));
|
|
my $lastLenToDo = length($word);
|
|
# Declare entries concerning word lengths $firstLenToDo to $lastLenToDo inclusively.
|
|
for (my $wlen = $firstLenToDo; $wlen >= $lastLenToDo; --$wlen)
|
|
{
|
|
print "Words of length $wlen start in block #",
|
|
$numWrittenBlocks, ", at word offset $curBlockWordCount, byte offset $curBlockLen\n";
|
|
$wordLengthIndex{$wlen} = {
|
|
block => $numWrittenBlocks,
|
|
wordOffset => $curBlockWordCount,
|
|
offset => $curBlockLen # byte offset
|
|
};
|
|
}
|
|
}
|
|
|
|
#print "Before: '", $curBlock[0], "', ", scalar(@curBlock), "\n";
|
|
push @curBlock, $word;
|
|
#print "First: '", $curBlock[0], "', ", scalar(@curBlock), "\n";
|
|
$curBlockLen += length($word);
|
|
++$curBlockWordCount;
|
|
|
|
$lenPrevWord = length($word);
|
|
}
|
|
|
|
# Complete %wordLengthIndex.
|
|
#
|
|
{
|
|
die unless defined $lenPrevWord;
|
|
die unless defined $minWordLen;
|
|
my $firstLenToDo = $lenPrevWord - 1;
|
|
my $lastLenToDo = $minWordLen - 1;
|
|
for (my $wlen = $firstLenToDo; $wlen >= $lastLenToDo; --$wlen)
|
|
{
|
|
print "Words of length ", $wlen + 1, " end at block #",
|
|
$numWrittenBlocks,
|
|
", at word offset $curBlockWordCount, byte offset $curBlockLen\n";
|
|
$wordLengthIndex{$wlen} = {
|
|
block => $numWrittenBlocks, # zero-based block index
|
|
wordOffset => $curBlockWordCount,
|
|
offset => $curBlockLen
|
|
};
|
|
}
|
|
}
|
|
|
|
# Write the last block, if any.
|
|
#
|
|
if ($curBlockLen > 0)
|
|
{
|
|
padDCTFile($mmuBlockLen);
|
|
|
|
writeBlock(\@curBlock);
|
|
}
|
|
|
|
# The last block only needs to be padded to a multiple of 256 bytes.
|
|
# This ends the last sector with defined bytes instead of garbage.
|
|
#
|
|
padDCTFile($sectorSize);
|
|
|
|
# Close the output file.
|
|
#
|
|
if (!close($fhBlock))
|
|
{
|
|
print "$0: ERROR: failed to close $dataFilename: $!\n";
|
|
exit 1;
|
|
}
|
|
|
|
|
|
print "Total length of the selected words: $totalLen characters\n";
|
|
print "Number of MMU blocks: $numWrittenBlocks\n";
|
|
print "Word count in last block: $curBlockWordCount\n";
|
|
print "Length of last block: $curBlockLen\n";
|
|
|
|
# Create a C file containing an array.
|
|
#
|
|
print "Creating $wordLengthIndexFilename\n";
|
|
open(W, "> $wordLengthIndexFilename") or die;
|
|
print W <<__EOF__;
|
|
/* Generated by build-data.pl. Do not edit. */
|
|
#include "defs.h"
|
|
const WordLengthRecord wordLengthRecords[] =
|
|
{
|
|
__EOF__
|
|
|
|
# An entry is written for length 1 because it indicates
|
|
# where the list of words of length 2 ends.
|
|
#
|
|
for my $len (1..$maxWordLenSeen)
|
|
{
|
|
my $rh = $wordLengthIndex{$len};
|
|
die "len=$len" unless defined $rh->{block};
|
|
die unless $rh->{offset} <= $maxBlockLen;
|
|
printf W " { %2u, %4u, %4u }, /* length %u */\n",
|
|
$rh->{block}, $rh->{wordOffset}, $rh->{offset}, $len;
|
|
}
|
|
|
|
print W <<__EOF__;
|
|
};
|
|
__EOF__
|
|
printf W "const byte numWordLengthRecords = %u;\n", $maxWordLenSeen;
|
|
close(W) or die;
|
|
|
|
if ($numWrittenBlocks > $maxBlocks)
|
|
{
|
|
print "$0: ERROR: Exceeded maximum number of blocks (wrote $numWrittenBlocks, max is $maxBlocks).\n";
|
|
exit 1;
|
|
}
|