utility.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004 Karoly Balogh for Genesi S.a.r.l. <www.genesi.lu>
  4. utility.library interface unit for MorphOS/PowerPC
  5. MorphOS port was done on a free Pegasos II/G4 machine
  6. provided by Genesi S.a.r.l. <www.genesi.lu>
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$inline on}
  14. unit utility;
  15. interface
  16. uses
  17. exec;
  18. var
  19. UtilityBase: Pointer;
  20. // utility.library date defines
  21. type
  22. PClockData = ^TClockData;
  23. TClockData = packed record
  24. Sec: Word;
  25. Min: Word;
  26. Hour: Word;
  27. MDay: Word;
  28. Month: Word;
  29. Year: Word;
  30. WDay: Word;
  31. end;
  32. // utility.library tagitem defines
  33. type
  34. Tag = LongWord;
  35. PTag = ^Tag;
  36. PPTagItem = ^PTagItem;
  37. PTagItem = ^TTagItem;
  38. TTagItem = packed record
  39. ti_Tag : Tag;
  40. ti_Data: LongWord;
  41. end;
  42. const
  43. TAG_DONE = 0;
  44. TAG_END = 0;
  45. TAG_IGNORE = 1;
  46. TAG_MORE = 2;
  47. TAG_SKIP = 3;
  48. TAG_USER = DWord(1 Shl 31);
  49. TAGFILTER_AND = 0;
  50. TAGFILTER_NOT = 1;
  51. MAP_REMOVE_NOT_FOUND = 0;
  52. MAP_KEEP_NOT_FOUND = 1;
  53. // utility.library namespace defines
  54. type
  55. PNamedObject = ^TNamedObject;
  56. TNamedObject = packed record
  57. no_Object: APTR;
  58. end;
  59. const
  60. ANO_NameSpace = 4000;
  61. ANO_UserSpace = 4001;
  62. ANO_Priority = 4002;
  63. ANO_Flags = 4003;
  64. NSB_NODUPS = 0;
  65. NSB_CASE = 1;
  66. NSF_NODUPS = 1 Shl NSB_NODUPS;
  67. NSF_CASE = 1 Shl NSB_CASE;
  68. // utility.library pack attributes and macros
  69. const
  70. PSTB_SIGNED = 31;
  71. PSTB_UNPACK = 30;
  72. PSTB_PACK = 29;
  73. PSTB_EXISTS = 26;
  74. PSTF_SIGNED = 1 Shl PSTB_SIGNED;
  75. PSTF_UNPACK = 1 Shl PSTB_UNPACK;
  76. PSTF_PACK = 1 Shl PSTB_PACK;
  77. PSTF_EXISTS = 1 Shl PSTB_EXISTS;
  78. const
  79. PKCTRL_PACKUNPACK = $00000000;
  80. PKCTRL_PACKONLY = $40000000;
  81. PKCTRL_UNPACKONLY = $20000000;
  82. PKCTRL_BYTE = $80000000;
  83. PKCTRL_WORD = $88000000;
  84. PKCTRL_LONG = $90000000;
  85. PKCTRL_UBYTE = $00000000;
  86. PKCTRL_UWORD = $08000000;
  87. PKCTRL_ULONG = $10000000;
  88. PKCTRL_BIT = $18000000;
  89. PKCTRL_FLIPBIT = $98000000;
  90. {$WARNING FIX ME!!! Some macros to convert}
  91. {
  92. PK_BITNUM1(flg) ((flg) == 0x01 ? 0 : (flg) == 0x02 ? 1 : (flg) == 0x04 ? 2 : (flg) == 0x08 ? 3 : (flg) == 0x10 ? 4 : (flg) == 0x20 ? 5 : (flg) == 0x40 ? 6 : 7)
  93. PK_BITNUM2(flg) ((flg < 0x100 ? PK_BITNUM1(flg) : 8 + PK_BITNUM1(flg >> 8)))
  94. PK_BITNUM(flg) ((flg < 0x10000 ? PK_BITNUM2(flg) : 16 + PK_BITNUM2(flg >> 16)))
  95. PK_WORDOFFSET(flg) ((flg) < 0x100 ? 1 : 0)
  96. PK_LONGOFFSET(flg) ((flg) < 0x100 ? 3 : (flg) < 0x10000 ? 2 : (flg) < 0x1000000 ? 1 : 0)
  97. PK_CALCOFFSET(type,field) ((ULONG)(&((struct type *)0)->field))
  98. PACK_STARTTABLE(tagbase) (tagbase)
  99. PACK_NEWOFFSET(tagbase) (-1L),(tagbase)
  100. PACK_ENDTABLE 0
  101. PACK_ENTRY(tagbase,tag,type,field,control) (control | ((tag-tagbase) << 16L) | PK_CALCOFFSET(type,field))
  102. PACK_BYTEBIT(tagbase,tag,type,field,control,flags) (control | ((tag-tagbase) << 16L) | PK_CALCOFFSET(type,field) | (PK_BITNUM(flags) << 13L))
  103. PACK_WORDBIT(tagbase,tag,type,field,control,flags) (control | ((tag-tagbase) << 16L) | (PK_CALCOFFSET(type,field) + PK_WORDOFFSET(flags)) | ((PK_BITNUM(flags) & 7) << 13L))
  104. PACK_LONGBIT(tagbase,tag,type,field,control,flags) (control | ((tag-tagbase) << 16L) | (PK_CALCOFFSET(type,field) + PK_LONGOFFSET(flags)) | ((PK_BITNUM(flags) & 7) << 13L))
  105. }
  106. // utility.library include
  107. const
  108. UtilityName = 'utility.library';
  109. type
  110. PUtilityBase = ^TUtilityBase;
  111. TUtilityBase = packed record
  112. ub_LibNode : TLibrary;
  113. ub_Language: Byte;
  114. ub_Reserved: Byte;
  115. end;
  116. // utility.library hook defines
  117. type
  118. PHook = ^THook;
  119. THook = packed record
  120. h_MinNode : TMinNode;
  121. h_Entry : Pointer;
  122. h_SubEntry: Pointer;
  123. h_Data : APTR;
  124. end;
  125. function FindTagItem(TagVal: Tag location 'd0'; TagList: PTagItem location 'a0'): PTagItem; SysCall MOS_UtilityBase 030;
  126. function GetTagData(TagValue: Tag location 'd0'; DefaultVal: LongWord location 'd1'; TagList: PTagItem location 'a0'): LongWord; SysCall MOS_UtilityBase 036;
  127. function PackBoolTags(InitialFlags: LongWord location 'd0'; TagList: PTagItem location 'a0'; BoolMap: PTagItem location 'a1'): LongWord; SysCall MOS_UtilityBase 042;
  128. function NextTagItem(TagListPtr: PPTagItem location 'a0'): PTagItem; overload; SysCall MOS_UtilityBase 048;
  129. function NextTagItem(var TagList: PTagItem location 'a0'): PTagItem; overload; SysCall MOS_UtilityBase 048;
  130. procedure FilterTagChanges(ChangeList: PTagItem location 'a0'; OriginalList: PTagItem location 'a1'; Apply: LongWord location 'd0'); SysCall MOS_UtilityBase 054;
  131. procedure MapTags(TagList: PTagItem location 'a0'; MapList: PTagItem location 'a1'; MapType: Cardinal location 'd0'); SysCall MOS_UtilityBase 060;
  132. function AllocateTagItems(NumTags: Cardinal location 'd0'): PTagItem; SysCall MOS_UtilityBase 066;
  133. function CloneTagItems(TagList: PTagItem location 'a0'): PTagItem; SysCall MOS_UtilityBase 072;
  134. procedure FreeTagItems(TagList: PTagItem location 'a0'); SysCall MOS_UtilityBase 078;
  135. procedure RefreshTagItemClones(Clone: PTagItem location 'a0'; Original: PTagItem location 'a1'); SysCall MOS_UtilityBase 084;
  136. function TagInArray(TagValue: Tag location 'd0'; TagArray: PTag location 'a0'): LongBool; SysCall MOS_UtilityBase 090;
  137. function FilterTagItems(TagList: PTagItem location 'a0'; FilterArray: PTag location 'a1'; Logic: LongWord location 'd0'): LongWord; SysCall MOS_UtilityBase 096;
  138. function CallHookPkt(Hook: PHook location 'a0'; HObject: APTR location 'a2'; ParamPacket: APTR location 'a1'): LongWord; SysCall MOS_UtilityBase 102;
  139. procedure Amiga2Date(Seconds: LongWord location 'd0'; Result: PClockData location 'a0'); SysCall MOS_UtilityBase 120;
  140. function Date2Amiga(Date: PClockData location 'a0'): LongWord; SysCall MOS_UtilityBase 126;
  141. function CheckDate(Date: PClockData location 'a0'): LongWord; SysCall MOS_UtilityBase 132;
  142. function SMult32(Arg1: LongInt location 'd0'; Arg2: LongInt location 'd1'): LongInt; SysCall MOS_UtilityBase 138;
  143. function UMult32(Arg1: LongWord location 'd0'; Arg2: LongWord location 'd1'): LongWord; SysCall MOS_UtilityBase 144;
  144. function SDivMod32(Dividend: LongInt location 'd0'; Divisor: LongInt location 'd1'): LongInt; SysCall MOS_UtilityBase 150;
  145. function UDivMod32(Dividend: LongWord location 'd0'; Divisor: LongWord location 'd1'): LongWord; SysCall MOS_UtilityBase 156;
  146. function Stricmp(String1: STRPTR location 'a0'; String2: STRPTR location 'a1'): LongInt; SysCall MOS_UtilityBase 162;
  147. function Strnicmp(String1: STRPTR location 'a0'; String2: STRPTR location 'a1'; Length: LongInt location 'd0'): LongInt; SysCall MOS_UtilityBase 168;
  148. function ToUpper(Character: LongWord location 'd0'): AnsiChar; SysCall MOS_UtilityBase 174;
  149. function ToLower(character: LongWord location 'd0'): AnsiChar; SysCall MOS_UtilityBase 180;
  150. procedure ApplyTagChanges(List: PTagItem location 'a0'; ChangeList: PTagItem location 'a1'); SysCall MOS_UtilityBase 186;
  151. function SMult64(Arg1: LongInt location 'd0'; Arg2: LongInt location 'd1'): LongInt; SysCall MOS_UtilityBase 198;
  152. function UMult64(Arg1: LongWord location 'd0'; Arg2: LongWord location 'd1'): LongWord; SysCall MOS_UtilityBase 204;
  153. function PackStructureTags(Pack: APTR location 'a0'; PackTable: PLongWord location 'a1'; TagList: PTagItem location 'a2'): LongWord; SysCall MOS_UtilityBase 210;
  154. function UnpackStructureTags(Pack: APTR location 'a0'; PackTable: PLongWord location 'a1'; TagList: PTagItem location 'a2'): LongWord; SysCall MOS_UtilityBase 216;
  155. function AddNamedObject(NameSpace: PNamedObject location 'a0'; NObject: PNamedObject location 'a1'): LongBool; SysCall MOS_UtilityBase 222;
  156. function AllocNamedObjectA(Name: STRPTR location 'a0'; TagList: PTagItem location 'a1'): PNamedObject; SysCall MOS_UtilityBase 228;
  157. function AttemptRemNamedObject(NObject: PNamedObject location 'a0'): LongInt; SysCall MOS_UtilityBase 234;
  158. function FindNamedObject(NameSpace: PNamedObject location 'a0'; Name: STRPTR location 'a1'; LastObject: PNamedObject location 'a2'): PNamedObject; SysCall MOS_UtilityBase 240;
  159. procedure FreeNamedObject(NObject: PNamedObject location 'a0'); SysCall MOS_UtilityBase 246;
  160. function NamedObjectName(NObject: PNamedObject location 'a0'): STRPTR; SysCall MOS_UtilityBase 252;
  161. procedure ReleaseNamedObject(NObject: PNamedObject location 'a0'); SysCall MOS_UtilityBase 258;
  162. procedure RemNamedObject(NObject: PNamedObject location 'a0'; Message: PMessage location 'a1'); SysCall MOS_UtilityBase 264;
  163. function GetUniqueID: LongWord; SysCall MOS_UtilityBase 270;
  164. // varargs version
  165. function AllocNamedObject(Name: STRPTR; const Tags: array of PtrUInt): PNamedObject; inline;
  166. function TAG_(Value: Pointer): PtrUInt; overload; inline;
  167. function TAG_(Value: PAnsiChar): PtrUInt; overload; inline;
  168. function TAG_(Value: Boolean): PtrUInt; overload; inline;
  169. function TAG_(Value: LongInt): PtrUInt; overload; inline;
  170. function TAG_(Value: LongWord): PtrUInt; overload; inline;
  171. function AsTag(Value: Pointer): PtrUInt; overload; inline;
  172. function AsTag(Value: PAnsiChar): PtrUInt; overload; inline;
  173. function AsTag(Value: Boolean): PtrUInt; overload; inline;
  174. function AsTag(Value: LongInt): PtrUInt; overload; inline;
  175. function AsTag(Value: LongWord): PtrUInt; overload; inline;
  176. // Hook and Dispatcher Helper
  177. { This procedure is used to pop Dispatcher arguments from the EmulHandle }
  178. procedure DISPATCHERARG(var cl; var obj; var msg);
  179. function HookEntry: PtrUInt;
  180. implementation
  181. function AllocNamedObject(Name: STRPTR; const Tags: array of PtrUInt): PNamedObject; inline;
  182. begin
  183. AllocNamedObject := AllocNamedObjectA(Name, @Tags);
  184. end;
  185. function TAG_(Value: Pointer): PtrUInt; inline;
  186. begin
  187. TAG_ := PtrUInt(Value);
  188. end;
  189. function TAG_(Value: PAnsiChar): PtrUInt; inline;
  190. begin
  191. TAG_ := PtrUInt(Value);
  192. end;
  193. function TAG_(Value: Boolean): PtrUInt; inline;
  194. begin
  195. if Value then
  196. TAG_ := LTrue
  197. else
  198. TAG_ := LFalse;
  199. end;
  200. function TAG_(Value: LongInt): PtrUInt; inline;
  201. begin
  202. TAG_ := PtrUInt(Value);
  203. end;
  204. function TAG_(Value: LongWord): PtrUInt; inline;
  205. begin
  206. TAG_ := PtrUInt(Value);
  207. end;
  208. function AsTag(Value: Pointer): LongWord; inline;
  209. begin
  210. AsTag := LongWord(Value);
  211. end;
  212. function AsTag(Value: PAnsiChar): PtrUInt; inline;
  213. begin
  214. AsTag := PtrUInt(Value);
  215. end;
  216. function AsTag(Value: Boolean): PtrUInt; inline;
  217. begin
  218. if Value then
  219. AsTag := LTrue
  220. else
  221. AsTag := LFalse;
  222. end;
  223. function AsTag(Value: LongInt): PtrUInt; inline;
  224. begin
  225. AsTag := PtrUInt(Value);
  226. end;
  227. function AsTag(Value: LongWord): PtrUInt; inline;
  228. begin
  229. AsTag := PtrUInt(Value);
  230. end;
  231. { This procedure is used to pop Dispatcher arguments from the EmulHandle }
  232. procedure DISPATCHERARG(var cl; var obj; var msg);
  233. begin
  234. with GetEmulHandle^ do
  235. begin
  236. PtrUInt(cl) := reg[regA0];
  237. PtrUInt(obj) := reg[regA2];
  238. PtrUInt(msg) := reg[regA1];
  239. end;
  240. end;
  241. {
  242. // assembler implementation, kept for reference
  243. asm
  244. lwz r6,32(r2) // REG_a0
  245. stw r6,(r3) // cl
  246. lwz r6,40(r2) // REG_a2
  247. stw r6,(r4) // obj
  248. lwz r6,36(r2) // REG_a1
  249. stw r6,(r5) // msg
  250. end;}
  251. type
  252. THookSubEntryFunc = function(a, b, c: Pointer): PtrUInt;
  253. function HookEntry: PtrUInt;
  254. var
  255. hook: PHook;
  256. begin
  257. hook := REG_A0;
  258. HookEntry := THookSubEntryFunc(hook^.h_SubEntry)(hook, REG_A2, REG_A1);
  259. end;
  260. begin
  261. UtilityBase := MOS_UtilityBase;
  262. end.