/* dict.c Copyright (C) 2026 Pierre Sarrazin 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 . */ #include "dict.h" #include "file.h" // See dict_setSearchProgressCallback(). // static void (*dict_searchProgressCallback)(void); void dict_setSearchProgressCallback(void (*functor)(void)) { dict_searchProgressCallback = functor; } static byte numDictBlocks; // index into dictMMUBlockIndexes[] // See memory map in HACKING.md. // static const byte dictMMUBlockIndexes[MAX_NUM_DICT_BLOCKS] = { 48, 49, 50, 54, 55, 56, 60, 61, 62 }; typedef struct DataFileReader { DECBFile *file; word fileSectorIndex; // zero-based sector number in 'file' const word numFileSectors; // length of 'file' in sectors byte **textScrnWritePos; // used to reposition 32x16 cursor byte *progressionPos; // position where progression % gets written } DataFileReader; // Returns a DECB_* error code. // static byte readSector(DataFileReader *fileReader, byte *sectorDest) { byte err = decb_readSector(fileReader->file, sectorDest, fileReader->fileSectorIndex); if (err != DECB_OK) { printf("\nFAILED TO READ SECTOR TO %p: ERROR %u\n", sectorDest, err); return err; } // Reposition cursor at the screen position of the // progression percentage, and update it. // *fileReader->textScrnWritePos = fileReader->progressionPos; printf("%3u%%", (fileReader->fileSectorIndex + 1) * 100 / fileReader->numFileSectors); return DECB_OK; } // Reads 'numSectors' from 'fileReader' into 'dest'. // static byte loadFontData(DataFileReader *fileReader, byte *dest, byte numSectors) { for (byte i = 0; i < numSectors; ++i, ++fileReader->fileSectorIndex, dest += 256) { byte err = readSector(fileReader, dest); if (err != DECB_OK) return err; } return DECB_OK; } // Initializes numDictBlocks. // static byte loadDictionarySectors(DataFileReader *fileReader) { // For each MMU block to fill. // for (numDictBlocks = 0; numDictBlocks < MAX_NUM_DICT_BLOCKS && fileReader->fileSectorIndex < fileReader->numFileSectors; ++numDictBlocks) { // Tune in the next MMU block to fill, at address // DICT_BANK * BLOCK_SIZE. // const byte mmuBlockIndex = dictMMUBlockIndexes[numDictBlocks]; mmuSet(DICT_BANK, mmuBlockIndex); // Start a writing pointer at the start of the MMU block to fill. // byte *sectorDest = (byte *) (DICT_BANK * BLOCK_SIZE); // For each 256-byte page in the current MMU block, // but without going past the file's last sector. // for (byte i = 0; i < BLOCK_SIZE / 256 && fileReader->fileSectorIndex < fileReader->numFileSectors; ++i, ++fileReader->fileSectorIndex, sectorDest += 256) { byte err = readSector(fileReader, sectorDest); if (err != DECB_OK) return err; // Check the first 16 bits of an MMU block, which must contain // the number of words in that block. A block being 8192 bytes, // there cannot be more than 8192 words in a block. // assert(i != 0 || * (word *) sectorDest <= BLOCK_SIZE); } } // for return DECB_OK; } // Initializes numDictBlocks. // BOOL loadDataFile(byte driveNo, byte **textScrnWritePos) { printf("LOADING DICTIONARY... "); // Position where the progression percentage will be displayed. // byte *progressionPos = *textScrnWritePos; const char *filename = DATA_FILENAME; // defined with -D in Makefile DECBFile file; byte err = decb_openSectorFile(&file, driveNo, filename); if (err != DECB_OK) { printf("\nFAILED TO OPEN %s: ERROR %u\n", filename, err); return FALSE; } // Get the number of sectors in the file. // byte lastFileGranule; word numFileSectors = 0; byte numGranules = decb_getNumGranulesInOpenFile(&file, &lastFileGranule, &numFileSectors); if (numGranules == 0xFF || numFileSectors == 0) { printf("\nFAILED TO GET FILE SIZE\n"); err = DECB_ERR_CORRUPT_DIR; } else { DataFileReader fileReader = { &file, 0, numFileSectors, textScrnWritePos, progressionPos }; // Load font data (not related to dictionary). // err = loadFontData(&fileReader, (byte *) FONT_4x8_BUFFER, FONT_4x8_SECTORS); if (err == DECB_OK) { assert(fileReader.fileSectorIndex == FONT_4x8_SECTORS); err = loadFontData(&fileReader, (byte *) FONT_5x8_BUFFER, FONT_5x8_SECTORS); if (err == DECB_OK) { assert(fileReader.fileSectorIndex == FONT_4x8_SECTORS + FONT_5x8_SECTORS); // Load the dictionary into MMU blocks. // err = loadDictionarySectors(&fileReader); } } } putchar('\n'); byte closeErr = decb_closeSectorFile(&file); if (closeErr != DECB_OK) { printf("\nFAILED TO CLOSE %s: ERROR %u\n", filename, err); return FALSE; } return err == DECB_OK; } // src: Must point to a high-bit terminated string. // Writes a NUL-terminated string in dest[]. // static byte decodeWord(const byte *src, char dest[MAX_WORD_LEN + 1]) { for (const byte *reader = src; ; ) { byte ch = *reader++; if (ch & 0x80) // if last char { *dest++ = (char) (ch & 0x7F); *dest = '\0'; return (byte) (reader - src); } *dest++ = (char) ch; } } BOOL iterateDictionary(ProcessDictBlockFuncPtr processNextBlock, ProcessDictWordFuncPtr processDictWord, byte wordLength, void *userData) { //printf("itD(wL=%u): nWLR=%u; ", wordLength, numWordLengthRecords); byte b; // starting block no (0 .. MAX_NUM_DICT_BLOCKS - 1) byte lastBlock; // last block (inclusively) to process word startWordOffset; // in words word startOffset; // in bytes (0 .. BLOCK_SIZE - 2) word endWordOffset; // in words // If the word length is not zero and within the possible word lengths found in the dictionary, // then restrict the search to words of that length. // if (wordLength >= MIN_WORD_LEN && wordLength < numWordLengthRecords + (MIN_WORD_LEN - 1)) { const WordLengthRecord *startRecord = &wordLengthRecords[wordLength - (MIN_WORD_LEN - 1)]; b = startRecord->blockNo; //printf("b=%u; ", b); assert(b < MAX_NUM_DICT_BLOCKS); startWordOffset = startRecord->wordOffset; startOffset = startRecord->offset; //printf("startOffset=%u; ", startOffset); assert(startOffset <= BLOCK_SIZE); const WordLengthRecord *endRecord = &wordLengthRecords[wordLength - MIN_WORD_LEN]; lastBlock = endRecord->blockNo; endWordOffset = endRecord->wordOffset; } else // search the entire dictionary: { b = 0; lastBlock = numDictBlocks - 1; startWordOffset = 0; startOffset = 0; endWordOffset = 0xFFFF; } //printf("iterateDictionary: lastBlock=%u, startWordOffset=%u, endWordOffset=%u\n", lastBlock, startWordOffset, endWordOffset); assert(lastBlock < MAX_NUM_DICT_BLOCKS); for ( ; b <= lastBlock; ++b) { const byte mmuBlockIndex = dictMMUBlockIndexes[b]; mmuSet(DICT_BANK, mmuBlockIndex); const byte *block = (unsigned) (DICT_BANK * BLOCK_SIZE); word numWordsInBlock = * (word *) block; //printf("iterateDictionary: b=%u, numWordsInBlock=%u\n", b, numWordsInBlock); if (processNextBlock) { byte action = (*processNextBlock)(numWordsInBlock, userData); if (action == DICT_ITER_STOP) return FALSE; if (action == DICT_ITER_NEXT_BLOCK) continue; assert(action == DICT_ITER_LIST_WORDS); mmuSet(DICT_BANK, mmuBlockIndex); // callback invocation may have changed DICT_BANK slot (e.g., recursive search) } if (processDictWord) { const byte *wordPtr = block + 2 + startOffset; word endWordOffsetThisBlock = (endWordOffset == 0xFFFF || b < lastBlock ? * (word *) block : endWordOffset); //printf("iterateDictionary: b=%u, endWordOffsetThisBlock=%u\n", b, endWordOffsetThisBlock); for (word w = startWordOffset; w < endWordOffsetThisBlock; ++w) { char currentWord[MAX_WORD_LEN + 1]; byte wordLen = decodeWord(wordPtr, currentWord); //printf("[%s]\n", currentWord); // Invoke the callback on the decoded word. // Stop the iteration if the callback asks for that. // This callback is allowed to change the block tuned in to DICT_BANK, // namely if it does a recursive iteration. // if (!(*processDictWord)(currentWord, wordLen, userData)) return FALSE; mmuSet(DICT_BANK, mmuBlockIndex); // callback invocation may have changed DICT_BANK slot (e.g., recursive search) wordPtr += wordLen; } } // Start next block at its beginning. startWordOffset = 0; startOffset = 0; } return TRUE; } static __gcccall asm BOOL equalStrings(const char *a, const char *b) { asm { ; N.B.: This code is not appropriate under OS-9, where Y must ; be preserved, as it points to the data segment of the ; current process. ; ; X contains 'a', as per __gcccall. ; ldy 2,s ; 'b' @loop ldb ,x+ ; read char from 'a' cmpb ,y+ ; compare to char from 'b' bne @returnFalse ; tstb bne @loop ; ldb #1 rts @returnFalse clrb } } typedef struct Lookup { const char *candidateWord; byte length; byte blockNo; BOOL found; // must be initialized to FALSE } Lookup; static BOOL compareWords(const char *currentWord, byte currentWordLength, void *userData) { Lookup *lookup = (Lookup *) userData; //printf("compareWords: currentWord='%s' (%u)\n", currentWord, currentWordLength); // Show a progress indicator, if the callback for it has been registered. // static byte timeToShowProgress = 0; if (dict_searchProgressCallback && ++timeToShowProgress == 0) (*dict_searchProgressCallback)(); // If the current word is shorter than the candidate word, then fail, // because all of the following words are also shorter than the candidate word. if (currentWordLength < lookup->length) { //printf("compareWords: FALSE b/c %u < %u\n", currentWordLength, lookup->length); return FALSE; // stop iteration and leave lookup->found at FALSE } if (equalStrings(currentWord, lookup->candidateWord)) // if match { //printf("compareWords: TRUE (found)\n"); lookup->found = TRUE; return FALSE; // stop iteration } return TRUE; // continue iteration } BOOL isWordInDictionary(const char *candidateWord) { byte candidateWordLen = (byte) strlen(candidateWord); if (candidateWordLen < MIN_WORD_LEN || candidateWordLen > MAX_WORD_LEN) return FALSE; Lookup lookup = { candidateWord, candidateWordLen, 0, FALSE }; (void) iterateDictionary(NULL, compareWords, lookup.length, &lookup); return lookup.found; } static byte getStatsFunctor(word numWordsInBlock, void *userData) { //printf("getStatsFunctor(%u, %p)\n", numWordsInBlock, userData, ); ** (word **) userData += numWordsInBlock; return DICT_ITER_NEXT_BLOCK; } void getDictionaryStats(word *totalNumWords) { *totalNumWords = 0; if (!iterateDictionary(getStatsFunctor, NULL, 0, (void *) &totalNumWords)) assert(FALSE); }