home *** CD-ROM | disk | FTP | other *** search
- #ifndef __STANDARD_H
- #define __STANDARD_H
-
- #define hibytes(value, hsize) (value >> hsize)
- #define lobytes(value, hsize) ((value << hsize) >> hsize)
- #define segment(thing) ((ubit16)((void _seg *)((void far *)thing)))
- #define offset(thing) ((ubit16)((void near *)((void far *)thing)))
-
- typedef signed char bit8;
- typedef signed short int bit16;
- typedef signed long int bit32;
-
- typedef unsigned char ubit8;
- typedef unsigned short int ubit16;
- typedef unsigned long int ubit32;
-
- typedef float fbit32;
- typedef double fbit64;
- typedef long double fbit80;
-
- bit8 getbit(bit8 value, bit8 bitnum);
- bit8 getbit(bit16 value, bit8 bitnum);
- bit8 getbit(bit32 value, bit8 bitnum);
- bit8 setbit(bit8 value, bit8 bitnum, bit8 bitval);
- bit16 setbit(bit16 value, bit8 bitnum, bit8 bitval);
- bit32 setbit(bit32 value, bit8 bitnum, bit8 bitval);
- void swap(bit8 *val1, bit8 *val2);
- void swap(bit16 *val1, bit16 *val2);
- void swap(bit32 *val1, bit32 *val2);
- fbit32 ttp(fbit64 x, bit8 ex);
- void hout(bit16, bit8); //Send val to hardware port
- bit8 hin(bit16); //Get val from hardware port
- void setint(bit8, void interrupt far (*isr)()); //Set interrupt vector
- void interrupt far (*getint(bit8))(); //Get interrupt vector
- void reboot();
-
- //Get bits from a --
- //bit8
- bit8 getbit(bit8 value, bit8 bitnum){
- if(bitnum > 7) return -1;
- return ((ubit8)(value << (7 - bitnum)) >> 7);}
- //bit16
- bit8 getbit(bit16 value, bit8 bitnum){
- if(bitnum > 15) return -1;
- return ((ubit16)(value << (15 - bitnum)) >> 15);}
- //bit32
- bit8 getbit(bit32 value, bit8 bitnum){
- if(bitnum > 31) return -1;
- return ((ubit32)(value << (31 - bitnum)) >> 31);}
-
- //Set bit of a --
- //bit8
- bit8 setbit(bit8 value, bit8 bitnum, bit8 bitval)
- {
- bit8 bitadd = 1, i;
- if(bitnum > 7) return 0;
-
- if(bitval){
- if(getbit(value, bitnum) == 0) {
- for(i=0;i<bitnum;i++) bitadd *= 2;
- value += bitadd; }
- }
- if(!bitval){
- if(getbit(value, bitnum) == 1) {
- for(i=0;i<bitnum;i++) bitadd *= 2;
- value -= bitadd; }
- }
-
- return value;
- }
-
- //bit16
- bit16 setbit(bit16 value, bit8 bitnum, bit8 bitval)
- {
- bit16 bitadd = 1, i;
- if(bitnum > 15) return 0;
-
- if(bitval){
- if(getbit(value, bitnum) == 0) {
- for(i=0;i<bitnum;i++) bitadd *= 2;
- value += bitadd; }
- }
- if(!bitval){
- if(getbit(value, bitnum) == 1) {
- for(i=0;i<bitnum;i++) bitadd *= 2;
- value -= bitadd; }
- }
-
- return value;
- }
-
- //bit32
- bit32 setbit(bit32 value, bit8 bitnum, bit8 bitval)
- {
- bit32 bitadd = 1, i;
- if(bitnum > 31) return 0;
-
- if(bitval){
- if(getbit(value, bitnum) == 0) {
- for(i=0;i<bitnum;i++) bitadd *= 2;
- value += bitadd; }
- }
- if(!bitval){
- if(getbit(value, bitnum) == 1) {
- for(i=0;i<bitnum;i++) bitadd *= 2;
- value -= bitadd; }
- }
-
- return value;
- }
-
- //Swap two values of type -
- //bit8
- void swap(bit8 *val1, bit8 *val2)
- { bit8 temp = *val1; *val1 = *val2; *val2 = temp; }
-
- //bit16
- void swap(bit16 *val1, bit16 *val2)
- { bit16 temp = *val1; *val1 = *val2; *val2 = temp; }
-
- //bit32
- void swap(bit32 *val1, bit32 *val2)
- { bit32 temp = *val1; *val1 = *val2; *val2 = temp; }
-
- //Calculate exponent
- fbit32 ttp(fbit64 x, bit8 ex)
- {
- bit8 i;
- fbit64 tx = x;
-
- if(ex == 0) return 1;
- if(ex == 1) return x;
-
- for(i = 1; i < ex; i++) x *= tx;
- return x;
- }
-
- void hout(bit16 portid, bit8 value)
- {
- asm mov DX,portid
- asm mov AL,value
- asm out DX,AL
-
- return;
- }
-
- bit8 hin(bit16 portid)
- {
- asm mov DX,portid
- asm in AL,DX
-
- return _AL;
- }
-
- void setint(bit8 intnum, void interrupt far (*isr)())
- {
- bit16 isrseg = hibytes((bit32)isr, 16);
- bit16 isroff = lobytes((bit32)isr, 16);
- asm mov AH,0x25
- asm mov AL,intnum
- asm mov DX,isroff
- asm mov CX,isrseg
- asm push DS
- asm mov DS,CX
- asm int 0x21
- asm pop DS
- }
-
- void interrupt far (*getint(bit8 intnum))()
- {
- bit16 hseg, hoff;
- asm mov AH,0x35
- asm mov AL,intnum
- asm int 0x21
- asm mov hseg,ES
- asm mov hoff,BX
- return (void interrupt far (*)())((void _seg *)(hseg) + (void near *)(hoff));
- }
-
- void reboot(bit8 wc)
- {
- void ((far *rb)()) = (void (far *)())0xFFFF0000;
- rb();
- return;
- }
-
- #endif