/* util.h 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 . */ #ifndef _H_util #define _H_util #include "constants.h" #include #include #if _CMOC_VERSION_ < 1100 #error "This project requires version 0.1.100 or later of CMOC." #endif // Gives a number of elements, not a number of bytes. // #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) #define ARRAY_END(array) ((array) + ARRAY_SIZE(array)) // Counter that is incremented 60 times per second. // extern word timer; // Counts down to zero from VSYNCS_PER_FLIP. // extern byte numVSyncsBeforeScreenFlip; // Sets up the IRQ service that updates 'numVSyncsBeforeScreenFlip' and 'timer' // on the V-Sync interrupt. // void setupIRQService(void); void coldBoot(void); // Equivalent to SCREEN 1,colorSet. // __gcccall void setCoCo12ColorSet(byte colorSet); // Emulates setCoCo12ColorSet() with palette changes. // __gcccall void setCoCo3ColorSet(byte colorSet); // Returns a random integer between the two limits, inclusively. // word rnd(word lowerLimit, word upperLimit); #if 0 // Makes the absolute value of 'n' even. // sword makeEven(sword n); // Returns ⌈num / den⌉. // __gcccall byte ceilDiv(byte num, byte den); #endif // screenRow: Considered negative if in -64..-1, non-negative if 0..191. // Returns this value as a signed word. // __gcccall asm sword signExtendYPos(byte screenRow); // angle: 0..NUM_BIPLANE_BITMAPS-1. // Returns (256 * cos(a * 22.5 deg), 256 * sin(a * 22.5 deg)). // void cosSin256ths(byte angle, sword *cosine, sword *sine); sword cos256ths(byte angle); // Takes two values in 256ths of a pixel. // Returns a256 * b256 * 2**16, i.e., in whole pixels. // sword mul256thsToByte(sword a256, sword b256); #if 0 // valueToAdd: 0..9. // digitIndex: Index in decimalScore[]. // void addToDecimalDigit(byte valueToAdd, sbyte digitIndex, byte digitArray[SCORE_WIDTH]); #endif // Sets parameters used by printWrapped(). // MUST be called at least once before calling printWrapped(). // leftMargin: Column where logical lines start. // logicalLineLength: leftMargin + logicalLineLength must be less than screen width. // void setWrappedPrinting(byte leftMargin, byte logicalLineLength); // setWrappedPrinting() MUST have been called at least once before // calling this function. // Newlines must be specified with '\n'. // Character INVERTED_COLOR_TOGGLE_CHAR toggles color inversion. // void printWrapped(const char *text); // Copies 'numRows' of bitmapRows[] twice each to screenPtr. // void copyDoubleRowBitmap(byte *screenPtr, const uint32_t bitmapRows[][8], byte numRows); // XORs every byte of the region at screenPtr with xorArg. // The region size is numRows * BYTES_PER_ROW. // void xorRegion(byte *screenPtr, byte numRows, byte xorArg); // Must be called before calling playSoundSample(). // void enableSound(void); // enableSound() must be called before calling this function. // The array bytes must contain sample values in { 0, 4, 8, ... , 252 }, // bits 0 and 1 must be zero. // delayLength: Longer values put more time between writing each // sample byte to the sound port. A unit represents 6 cycles // on a CoCo 6809 CPU. // void playSoundSample(const void *sampleArray, const void *sampleArrayEnd, word delayLength); #if 0 __gcccall void memzero(char *dest, byte numBytes); #endif // Returns a random value between 0..3. // Optimizes the number of calls to rand(). // byte generateRandomTayste(void); // x: -127..+127. Positive axis points right. // y: -127..+127. Positive axis points down. // Returns an angle from -128 to 127, representing -180 to +179 degrees, // where 0 is right, 64 is up, -128 is left, -64 is down. // int8_t approxAtan2Signed8(int8_t y, int8_t x); // shooterOrientation: 0..15 (0 is right, 4 is up, 8 is left, etc.). // maxDistance: Distance beyond which the objects are considered too far. // maxDistanceSquared: Must be maxDistance * maxDistance. // // If the distance between 'shooter' and 'target' is not too long, // stores the angle between the shooter's nose and the shooter-to-target // line in *angleInBrads (-128 to 127), and returns TRUE. // (See approxAtan2Signed8().) // // Otherwise, returns FALSE. // // Example: S----N // // T // // Point S is the back of 'shooter', N is its nose, and T is the target. // The returned angle is the N-S-T angle. // BOOL computeAngleIfNotTooFar(byte shooterX, byte shooterY, byte shooterOrientation, byte targetX, byte targetY, word maxDistance, word maxDistanceSquared, int8_t *angleInBrads); #if DEBUG /* Useful for debugging. */ #define beckputchar(ch) (* (byte *) 0xFF42 = (byte) (ch)) void beckPrintStr(const char *s); void beckPrintInt(int n); void beckPrintLineNo(int lineNo); void beckPrintInts(int a, int b); void beckerPortConsoleOutHook(void); void beckPrintf(const char *fmt, ...); void printDecimalArray(const byte array[], byte numDigits); void breakpoint(void); #endif /* DEBUG */ #endif /* _H_util */