/***************************************************************
 * Copyright (C) 2003 01.10.2003 - 01.12.2003
 *    Computer Graphics Support Group of 30 Phys-Math Lyceum
 ***************************************************************/

/* FILE NAME   : COMMONDF.H
 * PURPOSE     : Using CGSG_STYLE of writing programs and some utils.
 * PROGRAMMER  : SK1.
 * LAST UPDATE : 01.12.2003
 * NOTE        : 'TCL' - 'Traditional CGSG Library'.
 *               Upgrade 01.12.2003:
 *                 Correcting errors in 'CGSG types' and 'Converting types'
 *                 Adding 10 new types
 *                 Correcting errors in operations with bits
 *                 Adding new feach with TYPEDEF
 *                 Adding new function CUBE and renaming AVERAGE to AVR
 *                 Modifing comments in the head (new feach with history)
 *                 Adding some constants from Bios Data Area (0x44A + 0x450)
 *                 Adding separating between absolute adress and segment.
 *               Version 6.3
 *
 * No part of this file may be changed without agreement of
 * Computer Graphics Support Group of 30 Phys-Math Lyceum
 */

#ifndef __COMMONDF_H__
#define __COMMONDF_H__

#define TYPEDEF 1

/* Main define */
#define CGSG "Forever"
#define SK1 "Kopeliovitch Sergey"

/* CGSG types */
#ifndef __NO_STANDART_CGSG_TYPES__
  #if TYPEDEF
    typedef int BOOL;
    typedef int SHORT;
    typedef void VOID;
    typedef char CHAR;
    typedef unsigned char BYTE;
    typedef int INT;
    typedef unsigned int WORD;
    typedef long LONG;
    typedef unsigned long DWORD;
    typedef float FLOAT, FLT;
    typedef double DOUBLE, DBL;
    typedef long double REAL, LDBL;
  #else
    #define VOID void
    #define CHAR char
    #define BYTE unsigned char
    #define INT int
    #define WORD unsigned int
    #define LONG long
    #define DWORD unsigned long
    #define FLOAT float
    #define DOUBLE double
    #define REAL long double
    #define BOOL int
  #endif /* TYPEDEF */
#endif /* __NO_STANDART_CGSG_TYPES */

/* Other CGSG types */
#ifndef __NO_OTHER_CGSG_TYPES__
  #if TYPEDEF
    typedef signed char SCHAR;
    typedef unsigned char UCHAR;
    typedef signed short SINT;
    typedef unsigned short UINT;
    typedef signed long SLONG;
    typedef unsigned long ULONG;
    typedef signed char INT8;
    typedef unsigned char UINT8;
    typedef signed short INT16;
    typedef unsigned short UINT16;
    typedef signed long INT32;
    typedef unsigned long UINT32;
  #else
    #define SHORT int
    #define USHORT unsigned int
    #define SCHAR signed char
    #define UCHAR undigned char
    #define SINT signed int
    #define UINT unsigned int
    #define SLONG signed long
    #define ULONG unsigned long
    #define INT8 signed char
    #define UINT8 unsigned char
    #define INT16 signed int
    #define UINT16 unsigned int
    #define INT32 signed long
    #define UINT32 unsigned long
  #endif /* TYPEDEF */
#endif /* __NO_OTHER_CGSG_TYPES__ */

/* States */
#define ON 1
#define OFF 0
#define TRUE 1
#define FALSE 0
#define YES 1
#define NO 0
#define BONUS 1
#define PONOS 0

/* Converting of types */
#define WORD2BYTE(A, B) (((WORD)(A) << 8) + (BYTE)(B))
#define DWORD2WORD(A, B) (((DWORD)(A) << 16) + (WORD)(B))
#define DWORD2BYTE(A, B, C, D) \
  (((DWORD)(A) << 24) + ((DWORD)(B) << 16) + ((DWORD)(C) << 8) + (DWORD)(D))

/* And bit number Bit from the number A */
#define COM_ANDBIT(A, Bit) ((A) &= (1 << (Bit)))
/* Or bit number Bit of the number A */
#define COM_ORBIT(A, Bit) ((A) |= (1 << (Bit)))
/* Xor bit number Bit of the number A */
#define COM_XORBIT(A, Bit) ((A) ^= (1 << (Bit)))
/* Sets bit number Bit of the number A to zero */
#define COM_ZEROBIT(A, Bit) ((A) &= ~(1 << (Bit)))

/* Swaps two variables */
#define COM_SWAP(A, B, C) ((C) = (A), (A) = (B), (B) = (C))
/* Builds RGB-color from R, G abd B */
#define COM_RGB(R, G, B) (((DWORD)(R) << 16) + ((DWORD)(G) << 8) + (DWORD)(B))
/* Finds absolute value */
#define COM_ABS(A) ((A) > 0 ? (A) : -(A))
/* Finds maximum from two variables */
#define COM_MAX(A, B) ((A) > (B) ? (A) : (B))
/* Finds minimum from two variables */
#define COM_MIN(A, B) ((A) < (B) ? (A) : (B))
/* Finds average of two variables */
#define COM_AVR(A, B) (((A) + (B)) / 2.0)
/* Finds second power */
#define COM_SQR(A) ((A) * (A))
/* Finds third pow */
#define COM_CUBE(A) ((A) * (A) * (A))
/* Finds forth power */
#define COM_QUAD(A) (COM_SQR(A) * COM_SQR(A))

/* Video segments */
#define COM_SCREEN 0xB8000000UL
#define COM_VIDEO 0xA0000000UL
#define COM_MONO 0xB0000000UL
#define COM_BIOS 0x400UL
#define COM_SCREENSEG 0xB800UL
#define COM_VIDEOSEG 0xA000UL
#define COM_MONOSEG 0xB000UL
#define COM_BIOSSEG 0x400L

/* BIOS variables */
#define COM_SHIFTSTATUS *(WORD *)(0x417)
#define COM_VIDEOMODE *(BYTE *)(0x449)
#define COM_TIMER *(DWORD *)(0x46C)
#define COM_STRLEN *(BYTE *)(0x44A)
#define COM_CURSORX *(BYTE *)(0x450)
#define COM_CURSORY *(BYTE *)(0x451)

/* Shift's statuses */
#define COM_SYSREQ (COM_SHIFTSTATUS & 0x8000U)
#define COM_CAPS (COM_SHIFTSTATUS & 0x4000U)
#define COM_NUM (COM_SHIFTSTATUS & 0x2000U)
#define COM_SCROLL (COM_SHIFTSTATUS & 0x1000U)
#define COM_RIGHTCTRL (COM_SHIFTSTATUS & 0x800U)
#define COM_RIGHTALT (COM_SHIFTSTATUS & 0x400U)
#define COM_LEFTCTRL (COM_SHIFTSTATUS & 0x200U)
#define COM_LEFTALT (COM_SHIFTSTATUS & 0x100U)
#define COM_INSERT (COM_SHIFTSTATUS & 0x80U)
#define COM_CAPS_STATE (COM_SHIFTSTATUS & 0x40U)
#define COM_NUM_STATE (COM_SHIFTSTATUS & 0x20U)
#define COM_SCROLL_STATE (COM_SHIFTSTATUS & 0x10U)
#define COM_ALT (COM_SHIFTSTATUS & 0x8U)
#define COM_CTRL (COM_SHIFTSTATUS & 0x4U)
#define COM_LEFTSHIFT (COM_SHIFTSTATUS & 0x2U)
#define COM_RIGHTSHIFT (COM_SHIFTSTATUS & 0x1U)

#endif /* __COMMONDF_H__ */

/* END OF 'COMMONDF.H' FILE */
