home *** CD-ROM | disk | FTP | other *** search
- {**************************************************************************
-
- MISC
-
- Copyright (C) 1990,91 Anthony Rumble
-
- Author: Anthony Rumble
- Date: 15/01/91
-
- *************************************************************************}
- unit misc;
-
- interface
-
- uses dos, crt, mtask;
-
- function DecToHex (Number: longint; HexChars: byte): string;
- function julian:longint;
- function mkstring(t:word):string;
- procedure wait(t:longint);
- function spc(o:integer):string;
- function rpt(r:char; tms:word):string;
- function pwr(I:integer; power:integer):integer;
- procedure display_bits(i:byte);
- procedure pause;
- function Exist(fs :string) :boolean;
- Function Upper(Str:string):string;
-
- Function Malloc(Var Ptr; Size : Word) : Word;
- { Allocate free memory and return a pointer to it. The amount of memory }
- { requested from DOS is calculated as (Size/4)+1 paragraphs. If the }
- { allocation is successful, the untyped VAR parameter Ptr will be populated }
- { with the address of the allocated memory block, and the function will return}
- { a zero result. Should the request to DOS fail, Ptr will be populated with }
- { the value NIL, and the function will return the appropriate DOS error code. }
-
- Function Dalloc(Var Ptr) : Word;
- { Deallocate the memory pointed to by the untyped VAR parameter Ptr }
-
- Function DosMemAvail : LongInt;
- { Return the size of the largest contiguous chuck of memory available for use }
-
- implementation
- {***************************************************************************
- DECTOHEX
- ***************************************************************************}
- { -- Converts any number into a Hex character string -- }
- function DecToHex (Number: longint; HexChars: byte): string;
- const
- D2H: array[0..$F] of char = '0123456789ABCDEF';
- var
- HexStr: string;
- HexChar,Bits: byte;
- begin
- HexStr:='';
- for HexChar:=0 to pred(HexChars) do
- begin
- Bits:=HexChar shl 2;
- HexStr:=D2H[(Number shr Bits) and $F] + HexStr;
- end;
- DecToHex:='$' + HexStr;
- end;
- {***************************************************************************
- JULIAN
- ***************************************************************************}
- function julian:longint;
- var
- year, month, day, dayofweek:word;
- hour, minute, second, sec100:word;
- temp:longint;
- begin
- temp:=0;
- getdate(year, month, day, dayofweek);
- gettime(hour, minute, second, sec100);
- temp:=month * 2419200;
- temp:=temp + day * 86400;
- temp:=temp + hour * 3600;
- temp:=temp + minute * 60;
- temp:=temp + second;
- julian:=temp;
- end;
- {***************************************************************************
- RPT
- ***************************************************************************}
- function rpt(r:char; tms:word):string;
- var
- tmp:string;
- x:integer;
- begin
- tmp:='';
- for x:=1 to tms do
- tmp:=tmp+r;
- rpt:=tmp;
- end;
- {***************************************************************************
- MKSTRING
- ***************************************************************************}
- function mkstring(t:word):string;
- var
- temp:string;
- begin
- str(t, temp);
- mkstring:=temp;
- end;
-
- {***************************************************************************
- WAIT
- ***************************************************************************}
- procedure wait(t:longint);
- var
- tmp:longint;
- begin
- tmp:=julian+t;
- repeat;
- taskswitch;
- until julian > tmp;
- end;
- {***************************************************************************
- SPC
- ***************************************************************************}
- function spc(o:integer):string;
- var g:integer;
- tmp:string;
- begin
- tmp:='';
- for g:=1 to o do
- begin
- tmp:=tmp+' ';
- end;
- spc:=tmp;
- end;
- {***************************************************************************
- PWR
- ***************************************************************************}
- function pwr(i:integer; power:integer):integer;
- var
- a:integer;
- tmp:longint;
- begin
- tmp:=1;
- for a:=1 to power do
- begin
- tmp:=tmp*i;
- end;
- pwr:=tmp;
- end;
- {***************************************************************************
- DISPLAY_BITS
- ***************************************************************************}
- procedure display_bits(i:byte);
- var
- tmp:byte;
- begin
- tmp:=i;
- {Check bit 7}
- if tmp>127 then
- begin
- write('1');
- tmp:=tmp-128;
- end else write('0');
- {Check bit 6}
- if tmp>63 then
- begin
- write('1');
- tmp:=tmp-64;
- end else write('0');
- {Check bit 5}
- if tmp>31 then
- begin
- write('1');
- tmp:=tmp-32;
- end else write('0');
- {Check bit 4}
- if tmp>15 then
- begin
- write('1');
- tmp:=tmp-16;
- end else write('0');
- {Check bit 3}
- if tmp>7 then
- begin
- write('1');
- tmp:=tmp-8;
- end else write('0');
- {Check bit 2}
- if tmp>3 then
- begin
- write('1');
- tmp:=tmp-4;
- end else write('0');
- {Check bit 1}
- if tmp>1 then
- begin
- write('1');
- tmp:=tmp-2;
- end else write('0');
- {Check bit 0}
- if tmp>0 then
- begin
- write('1');
- end else write('0');
- writeln;
- end;
- {***************************************************************************
- PAUSE
- ***************************************************************************}
- procedure pause;
- var
- ch:char;
- begin
- write('Press any key to continue:');
- repeat;
- taskswitch;
- until keypressed;
- ch:=readkey;
- writeln;
- end;
- {***************************************************************************
- EXIST
- ---------------------------------------------------------------------------
- Returns True if file exists; otherwise, it returns False.
- Closes the file if it exists.
- ***************************************************************************}
- function Exist(fs :string) :boolean;
- var
- f: file;
- tmp:boolean;
- begin
- {$I-}
- Assign(f,fs);
- Reset(f);
- Close(f);
- {$I+}
- tmp:=(IOResult=0) and (fs<>'');
- exist:=tmp;
- end;
- {***************************************************************************
- MALLOC
- ***************************************************************************}
- Function Malloc(Var Ptr; Size : Word) : Word;
- Begin
- Inline(
- $8B/$46/<SIZE/ { mov ax,[bp+<Size]}
- $B9/$04/$00/ { mov cx,4}
- $D3/$E8/ { shr ax,cl}
- $40/ { inc ax}
- $89/$C3/ { mov bx,ax}
- $B4/$48/ { mov ah,$48}
- $CD/$21/ { int $21 ;Allocate memory}
- $72/$07/ { jc AllocErr ;If any errors ....}
- $C7/$46/$FE/$00/$00/ {NoErrors: mov word [bp-2],0 ;Return 0 for successful allocation}
- $EB/$05/ { jmp short Exit}
- $89/$46/$FE/ {AllocErr: mov [bp-2],ax ;Return error code}
- $31/$C0/ { xor ax,ax ;Store a NIL value into the ptr}
- $C4/$7E/<PTR/ {Exit: les di,[bp+<Ptr] ;Address of pointer into es:di}
- $50/ { push ax ;Save the Segment part}
- $31/$C0/ { xor ax,ax ;Offset is always 0}
- $FC/ { cld ;Make sure direction is upward}
- $AB/ { stosw ;Store offset of memory block}
- $58/ { pop ax ;Get back segment part}
- $AB); { stosw ;Store segment of memory block}
-
- End {Malloc};
- {***************************************************************************
- DALLOC
- ***************************************************************************}
- Function Dalloc(Var Ptr) : Word;
- Begin
- If Pointer(Ptr) <> NIL then begin
- Inline(
- $B4/$49/ { mov ah,$49}
- $C4/$7E/<PTR/ { les di,[bp+<Ptr]}
- $26/$C4/$3D/ { es: les di,[di]}
- $CD/$21/ { int $21}
- $72/$02/ { jc Exit}
- $31/$C0/ {NoError: xor ax,ax}
- $89/$46/$FE); {Exit: mov [bp-2],ax}
- Pointer(Ptr) := NIL;
- end {if}
- else
- Dalloc := 0;
- End {Dealloc};
- {***************************************************************************
- DOSMEMAVAIL
- ***************************************************************************}
- Function DosMemAvail : LongInt;
- Begin
- Inline(
- $BB/$FF/$FF/ { mov bx,$FFFF}
- $B4/$48/ { mov ah,$48}
- $CD/$21/ { int $21}
- $89/$D8/ { mov ax,bx}
- $B9/$10/$00/ { mov cx,16}
- $F7/$E1/ { mul cx}
- $89/$46/$FC/ { mov [bp-4],ax}
- $89/$56/$FE); { mov [bp-2],dx}
- end {DosMemAvail};
- {***************************************************************************
- UPPER
- ***************************************************************************}
- Function Upper(Str:string):string;
- var
- I : integer;
- begin
- For I := 1 to length(Str) do
- Str[I] := Upcase(Str[I]);
- Upper := Str;
- end; {Func Upper}
-
-
- end.