libstd.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. {$MODE OBJFPC}
  2. unit libstd;
  3. interface
  4. type
  5. Pdword = ^dword;
  6. Pshort = ^smallint;
  7. Pbyte = ^byte;
  8. Pinteger = ^integer;
  9. // stddef.h
  10. type
  11. size_t = dword;
  12. wchar_t = dword;
  13. u_char = byte;
  14. u_short = smallint;
  15. u_int = longint;
  16. u_long = longint;
  17. const
  18. WEOF = $ffffffff;
  19. NULL = 0;
  20. // errno.h
  21. const
  22. EPERM = 1; // Not owner
  23. ENOENT = 2; // No such file or directory
  24. ESRCH = 3; // No such process
  25. EINTR = 4; // Interrupted system call
  26. EIO = 5; // I/O error
  27. ENXIO = 6; // No such device or address
  28. E2BIG = 7; // Arg list too long
  29. ENOEXEC = 8; // Exec format error
  30. EBADF = 9; // Bad file number
  31. ECHILD = 10; // No children
  32. EAGAIN = 11; // No more processes
  33. ENOMEM = 12; // Not enough core
  34. EACCES = 13; // Permission denied
  35. EFAULT = 14; // Bad address
  36. ENOTBLK = 15; // Block device required
  37. EBUSY = 16; // Mount device busy
  38. EEXIST = 17; // File exists
  39. EXDEV = 18; // Cross-device link
  40. ENODEV = 19; // No such device
  41. ENOTDIR = 20; // Not a directory
  42. EISDIR = 21; // Is a directory
  43. EINVAL = 22; // Invalid argument
  44. ENFILE = 23; // File table overflow
  45. EMFILE = 24; // Too many open files
  46. ENOTTY = 25; // Not a typewriter
  47. ETXTBSY = 26; // Text file busy
  48. EFBIG = 27; // File too large
  49. ENOSPC = 28; // No space left on device
  50. ESPIPE = 29; // Illegal seek
  51. EROFS = 30; // Read-only file system
  52. EFORMAT = 31; // Bad file format
  53. EPIPE = 32; // Broken pipe
  54. // math software
  55. EDOM = 33; // Argument too large
  56. ERANGE = 34; // Result too large
  57. // non-blocking and interrupt i/o
  58. EWOULDBLOCK = 35; // Operation would block
  59. EINPROGRESS = 36; // Operation now in progress
  60. EALREADY = 37; // Operation already in progress
  61. var
  62. errno : longint; external;
  63. // asm.h
  64. const
  65. R0 = 0;
  66. R1 = 1;
  67. R2 = 2;
  68. R3 = 3;
  69. R4 = 4;
  70. R5 = 5;
  71. R6 = 6;
  72. R7 = 7;
  73. R8 = 8;
  74. R9 = 9;
  75. R10 = 10;
  76. R11 = 11;
  77. R12 = 12;
  78. R13 = 13;
  79. R14 = 14;
  80. R15 = 15;
  81. R16 = 16;
  82. R17 = 17;
  83. R18 = 18;
  84. R19 = 19;
  85. R20 = 20;
  86. R21 = 21;
  87. R22 = 22;
  88. R23 = 23;
  89. R24 = 24;
  90. R25 = 25;
  91. R26 = 26;
  92. R27 = 27;
  93. R28 = 28;
  94. R29 = 29;
  95. R30 = 30;
  96. R31 = 31;
  97. zero = 0;
  98. AT = 1; // assembler temp
  99. v0 = 2; // return value
  100. v1 = 3;
  101. a0 = 4; // argument registers
  102. a1 = 5;
  103. a2 = 6;
  104. a3 = 7;
  105. t0 = 8; // caller saved
  106. t1 = 9;
  107. t2 = 10;
  108. t3 = 11;
  109. t4 = 12;
  110. t5 = 13;
  111. t6 = 14;
  112. t7 = 15;
  113. s0 = 16; // callee saved
  114. s1 = 17;
  115. s2 = 18;
  116. s3 = 19;
  117. s4 = 20;
  118. s5 = 21;
  119. s6 = 22;
  120. s7 = 23;
  121. t8 = 24; // code generator
  122. t9 = 25;
  123. k0 = 26; // kernel temporary
  124. k1 = 27;
  125. gp = 28; // global pointer
  126. sp = 29; // stack pointer
  127. fp = 30; // frame pointer
  128. ra = 31; // return address
  129. // register offset
  130. R_R0 = 0;
  131. R_R1 = 1;
  132. R_R2 = 2;
  133. R_R3 = 3;
  134. R_R4 = 4;
  135. R_R5 = 5;
  136. R_R6 = 6;
  137. R_R7 = 7;
  138. R_R8 = 8;
  139. R_R9 = 9;
  140. R_R10 = 10;
  141. R_R11 = 11;
  142. R_R12 = 12;
  143. R_R13 = 13;
  144. R_R14 = 14;
  145. R_R15 = 15;
  146. R_R16 = 16;
  147. R_R17 = 17;
  148. R_R18 = 18;
  149. R_R19 = 19;
  150. R_R20 = 20;
  151. R_R21 = 21;
  152. R_R22 = 22;
  153. R_R23 = 23;
  154. R_R24 = 24;
  155. R_R25 = 25;
  156. R_R26 = 26;
  157. R_R27 = 27;
  158. R_R28 = 28;
  159. R_R29 = 29;
  160. R_R30 = 30;
  161. R_R31 = 31;
  162. R_EPC = 32;
  163. R_MDHI= 33;
  164. R_MDLO= 34;
  165. R_SR = 35;
  166. R_CAUSE= 36;
  167. NREGS = 40;
  168. // compiler defined bindings
  169. R_ZERO = R_R0;
  170. R_AT = R_R1;
  171. R_V0 = R_R2;
  172. R_V1 = R_R3;
  173. R_A0 = R_R4;
  174. R_A1 = R_R5;
  175. R_A2 = R_R6;
  176. R_A3 = R_R7;
  177. R_T0 = R_R8;
  178. R_T1 = R_R9;
  179. R_T2 = R_R10;
  180. R_T3 = R_R11;
  181. R_T4 = R_R12;
  182. R_T5 = R_R13;
  183. R_T6 = R_R14;
  184. R_T7 = R_R15;
  185. R_S0 = R_R16;
  186. R_S1 = R_R17;
  187. R_S2 = R_R18;
  188. R_S3 = R_R19;
  189. R_S4 = R_R20;
  190. R_S5 = R_R21;
  191. R_S6 = R_R22;
  192. R_S7 = R_R23;
  193. R_T8 = R_R24;
  194. R_T9 = R_R25;
  195. R_K0 = R_R26;
  196. R_K1 = R_R27;
  197. R_GP = R_R28;
  198. R_SP = R_R29;
  199. R_FP = R_R30;
  200. R_RA = R_R31;
  201. // types.h
  202. type
  203. ushort = smallint; // sys III compat */
  204. uint = dword; // sys V compat */
  205. ulong = dword; // sys V compat */
  206. _quad = packed record
  207. val : array [0..1] of longint;
  208. end;
  209. quad = _quad;
  210. daddr_t = longint;
  211. caddr_t = pchar;
  212. qaddr_t = ^longint; // should be typedef quad * qaddr_t; */
  213. ino_t = dword;
  214. swblk_t = longint;
  215. time_t = longint;
  216. dev_t = smallint;
  217. off_t = longint;
  218. uid_t = u_short;
  219. gid_t = u_short;
  220. const
  221. NBBY = 8; // number of bits in a byte */
  222. type
  223. _physadr = packed record
  224. r : array [0..0] of longint;
  225. end;
  226. physadr = ^_physadr;
  227. label_t = packed record
  228. val : array [0..11] of longint;
  229. end;
  230. // kernel.h Rev. 3
  231. const
  232. DescMask = $ff000000;
  233. DescTH = DescMask;
  234. DescHW = $f0000000;
  235. DescEV = $f1000000;
  236. DescRC = $f2000000;
  237. DescUEV = $f3000000; // User event
  238. DescSW = $f4000000; // BIOS
  239. HwVBLANK = DescHW or $01; // VBLANK
  240. HwGPU = DescHW or $02; // GPU
  241. HwCdRom = DescHW or $03; // CDROM Decorder
  242. HwDMAC = DescHW or $04; // DMA controller
  243. HwRTC0 = DescHW or $05; // RTC0
  244. HwRTC1 = DescHW or $06; // RTC1
  245. HwRTC2 = DescHW or $07; // RTC2
  246. HwCNTL = DescHW or $08; // Controller
  247. HwSPU = DescHW or $09; // SPU
  248. HwPIO = DescHW or $0a; // PIO
  249. HwSIO = DescHW or $0b; // SIO
  250. HwCPU = DescHW or $10; // Exception
  251. HwCARD = DescHW or $11; // memory card
  252. HwCARD_0 = DescHW or $12; // memory card
  253. HwCARD_1 = DescHW or $13; // memory card
  254. SwCARD = DescSW or $01; // memory card
  255. SwMATH = DescSW or $02; // libmath
  256. RCntCNT0 = DescRC or $00; // display pixel
  257. RCntCNT1 = DescRC or $01; // horizontal sync
  258. RCntCNT2 = DescRC or $02; // one-eighth of system clock
  259. RCntCNT3 = DescRC or $03; // vertical sync target value fixed to 1
  260. RCntMdINTR = $1000;
  261. RCntMdNOINTR= $2000;
  262. RCntMdSC = $0001;
  263. RCntMdSP = $0000;
  264. RCntMdFR = $0000;
  265. RCntMdGATE = $0010;
  266. EvSpCZ = $0001; // counter becomes zero
  267. EvSpINT = $0002; // interrupted
  268. EvSpIOE = $0004; // end of i/o
  269. EvSpCLOSE = $0008; // file was closed
  270. EvSpACK = $0010; // command acknowledged
  271. EvSpCOMP = $0020; // command completed
  272. EvSpDR = $0040; // data ready
  273. EvSpDE = $0080; // data end
  274. EvSpTIMOUT = $0100; // time out
  275. EvSpUNKNOWN = $0200; // unknown command
  276. EvSpIOER = $0400; // end of read buffer
  277. EvSpIOEW = $0800; // end of write buffer
  278. EvSpTRAP = $1000; // general interrupt
  279. EvSpNEW = $2000; // new device
  280. EvSpSYSCALL = $4000; // system call instruction
  281. EvSpERROR = $8000; // error happned
  282. EvSpPERROR = $8001; // previous write error happned
  283. EvSpEDOM = $0301; // domain error in libmath
  284. EvSpERANGE = $0302; // range error in libmath
  285. EvMdINTR = $1000;
  286. EvMdNOINTR = $2000;
  287. EvStUNUSED = $0000;
  288. EvStWAIT = $1000;
  289. EvStACTIVE = $2000;
  290. EvStALREADY = $4000;
  291. TcbMdRT = $1000; // reserved by system
  292. TcbMdPRI = $2000; // reserved by system
  293. TcbStUNUSED = $1000;
  294. TcbStACTIVE = $4000;
  295. type
  296. EXEC = packed record
  297. pc0 : dword; // Execution start address
  298. gp0 : dword; // gp register initial value
  299. t_addr : dword; // Starting address of initialized text section
  300. t_size : dword; // Size of text section
  301. d_addr : dword; // Starting address of initialized data section
  302. d_size : dword; // Size of initialized data section
  303. b_addr : dword; // Uninitialized data section start address
  304. b_size : dword; // Uninitialized data section size
  305. s_addr : dword; // Stack start address (specified by the user)
  306. s_size : dword; // Stack size (specified by the user)
  307. sp : dword; // Register shunt variable
  308. fp : dword; // Register shunt variable
  309. gp : dword; // Register shunt variable
  310. ret : dword; // Register shunt variable
  311. base : dword; // Register shunt variable
  312. end;
  313. PEXEC = ^EXEC;
  314. ToT = packed record
  315. head : pdword;
  316. size : longint;
  317. end;
  318. TCB = packed record
  319. status : longint;
  320. mode : longint;
  321. reg : array [0..NREGS - 1] of dword; // never change the offset of this
  322. system : array [0..5] of longint; // reserved by system
  323. end;
  324. PTCB = ^TCB;
  325. TCBH = packed record
  326. entry : PTCB; // NULL
  327. flag : longint;
  328. end;
  329. EvCB = packed record
  330. desc : dword;
  331. status : longint;
  332. spec : longint;
  333. mode : longint;
  334. FHandler : pointer;
  335. system : array [0..1] of longint; // reserved by system
  336. end;
  337. XF_HDR = packed record
  338. key : array [0..7] of char;
  339. text : dword;
  340. data : dword;
  341. _exec : EXEC;
  342. title : array [0..59] of char; // "PlayStation(tm) Executable A1"
  343. end;
  344. var
  345. SysToT : array [0..31] of ToT; external;
  346. SysClearRCnt : array [0..0] of longint; external;
  347. type
  348. PDIRENTRY = ^DIRENTRY;
  349. DIRENTRY = packed record
  350. name : array [0..29] of char; // Filename
  351. attr : longint; // Attributes (dependent on file system)
  352. size : longint; // File size (in bytes)
  353. next : PDIRENTRY; // Pointer to next file entry (for user)
  354. system : array [0..7] of char; // Reserved by system
  355. end;
  356. // abs.h
  357. function abs(num: longint): longint; stdcall; external;
  358. // Basic system types and major/minor device constructing/busting macros.
  359. function major(x: longint): dword; // major part of a device
  360. function minor(x: longint): longint; // minor part of a device
  361. function makedev(x, y: longint): longint; // make a device number
  362. // memory.h
  363. function memcpy(dest: pointer; src: pointer; count: dword): pointer; stdcall external;
  364. function memmove(dest: pointer; const src: pointer; count: dword): pointer; stdcall external;
  365. function memcmp(const lhs: pointer; const rhs: pointer; count: dword): longint; stdcall external;
  366. function memchr(const ptr: pointer; val: char; num: dword): pointer; stdcall external;
  367. function memset(const dest: pointer; ch: byte; count: dword): pointer; stdcall external;
  368. function bcopy(const src: pointer; dest: pointer; len: dword): pointer; stdcall external;
  369. function bzero(s: pointer; n: dword): pointer; stdcall external;
  370. function bcmp(const s1: pointer; const s2: pointer; n: dword): longint; stdcall external;
  371. // malloc.h
  372. procedure InitHeap(p: pdword; l: dword); stdcall external; // this doesn't work - use InitHeap2
  373. procedure free(p: pointer); stdcall external; // this doesn't work - use free2
  374. function malloc(l: size_t): pointer; stdcall external; // this doesn't work - use malloc2
  375. function calloc(nitems: size_t; l: size_t): pointer; stdcall external; // this doesn't work - use calloc2
  376. function realloc(p: pointer; l: size_t): pointer; stdcall external; // this doesn't work - use realloc2
  377. procedure InitHeap2(p: pdword; l: dword); stdcall external;
  378. procedure free2(p: pointer); stdcall external;
  379. function malloc2(l: size_t): pointer; stdcall external;
  380. function calloc2(l1: size_t; l2: size_t): pointer; stdcall external;
  381. function realloc2(p: pointer; l: size_t): pointer; stdcall external;
  382. procedure InitHeap3(p: pdword; l: dword); stdcall external;
  383. procedure free3(p: pointer); stdcall external;
  384. function malloc3(l: size_t): pointer; stdcall external;
  385. function calloc3(l1: size_t; l2: size_t): pointer; stdcall external;
  386. function realloc3(p: pointer; l: size_t): pointer; stdcall external;
  387. // strings.h
  388. const
  389. LMAX = 256;
  390. function strcat(dest: pchar; const src: pchar): pchar; stdcall external;
  391. function strncat(dest: pchar; const src: pchar; num: size_t): pchar; stdcall; external;
  392. function strcmp(str1: pchar; str2: pchar): longint; stdcall; external;
  393. function strncmp(const str1: pchar; const str2: pchar; num: size_t): longint; stdcall; external;
  394. function strcpy(dest: pchar; src: pchar): pchar; stdcall; external;
  395. function strncpy(dest: pchar; const src: pchar; num: size_t): pchar; stdcall; external;
  396. function strlen(s: pchar): longint; stdcall; external;
  397. function index(const s: pchar; pos: byte): pchar; stdcall; external;
  398. function rindex(const s: pchar; pos: byte): pchar; stdcall; external;
  399. function strchr(const src: pchar; ch: char): pchar; stdcall; external;
  400. function strrchr(const src: pchar; ch: char): pchar; stdcall; external;
  401. function strpbrk(const str1: pchar; const str2: pchar): pchar; stdcall; external;
  402. function strspn(const str1: pchar; const str2: pchar): longint; stdcall; external;
  403. function strcspn(const str1: pchar; const str2: pchar): longint; stdcall; external;
  404. function strtok(str1: pchar; const str2: pchar): pchar; stdcall; external;
  405. function strstr(const str1: pchar; const str2: pchar): pchar; stdcall; external;
  406. function strdup(p: pchar): pchar;
  407. // rand.h
  408. const
  409. RAND_MAX = 32767;
  410. function rand: longint; stdcall; external;
  411. procedure srand(x: longint); stdcall; external;
  412. // stdlib.h
  413. type
  414. Tcmp = function (const a: pointer; const b: pointer): longint;
  415. Pcmp = ^Tcmp;
  416. function bsearch(const key: pointer; const ptr: pointer; count: size_t; size: size_t; comp: Pcmp ) : pointer; stdcall; external;
  417. procedure exit; stdcall; external;
  418. // stdio.h
  419. const
  420. BUFSIZ = 1024;
  421. EOF = -1;
  422. SEEK_SET = 0;
  423. SEEK_CUR = 1;
  424. SEEK_END = 2;
  425. function printf(const fmt: pchar): longint; varargs; stdcall; external;
  426. function sprintf(buffer: pchar; const fmt: pchar): longint; varargs; stdcall; external;
  427. function getc(s: longint): char; stdcall; external;
  428. function getchar: char; stdcall; external;
  429. function gets(s: pchar): pchar; stdcall; external;
  430. procedure putc(ch: char; s: longint); stdcall; external;
  431. procedure putchar(ch: char); stdcall; external;
  432. procedure puts(const s: pchar); stdcall; external;
  433. // convert.h
  434. function atoi(const str: pchar): longint; stdcall; external;
  435. function atol(const str: pchar): longint; stdcall; external;
  436. function strtol(const str: pchar; endptr: pointer; base: longint): longint; stdcall; external;
  437. function strtoul(const str: pchar; endptr: pointer; base: longint): dword; stdcall; external;
  438. function labs(x: longint): longint; stdcall; external;
  439. // limits.h
  440. const
  441. CHAR_BIT = 8;
  442. SCHAR_MIN = -128;
  443. SCHAR_MAX = 127;
  444. UCHAR_MAX = 255;
  445. CHAR_MIN = SCHAR_MIN;
  446. CHAR_MAX = SCHAR_MAX;
  447. SHRT_MIN = -32768;
  448. SHRT_MAX = 32767;
  449. USHRT_MAX = 65535;
  450. INT_MIN = -2147483648;
  451. INT_MAX = 2147483647;
  452. UINT_MAX = 4294967295;
  453. LONG_MIN = -2147483648;
  454. LONG_MAX = 2147483647;
  455. ULONG_MAX = 4294967295;
  456. USI_MAX = 4294967295; // max decimal value of an "unsigned"
  457. WORD_BIT = 32; // # of bits in a "word" or "int"
  458. MB_LEN_MAX = 4;
  459. // qsort.h
  460. procedure qsort(base: pointer; num: size_t; size: size_t; compar: pointer); stdcall; external;
  461. // ctype.h
  462. const
  463. _U = $01; // upper case letter
  464. _L = $02; // lower case letter
  465. _N = $04; // digit
  466. _S = $08; // space, tab, newline, vertical tab, formfeed, or carriage return
  467. _P = $10; // punctuation character
  468. _C = $20; // control character or delete
  469. _X = $40; // hexadecimal digit [0-9a-fA-F]
  470. _B = $80; // blank (space)
  471. //extern char _ctype_[];
  472. function toupper(ch: char): char; stdcall; external;
  473. function tolower(ch: char): char; stdcall; external;
  474. function isalpha(ch: char): boolean;
  475. function isupper(ch: char): boolean;
  476. function islower(ch: char): boolean;
  477. function isdigit(ch: char): boolean;
  478. function isxdigit(ch: char): boolean;
  479. function isspace(ch: char): boolean;
  480. function ispunct(ch: char): boolean;
  481. function isalnum(ch: char): boolean;
  482. function isprint(ch: char): boolean;
  483. function isgraph(ch: char): boolean;
  484. function iscntrl(ch: char): boolean;
  485. function isascii(ch: char): boolean;
  486. function toascii(ch: char): char;
  487. //function _toupper(ch: char) ((unsigned char)(c)-'a'+'A')
  488. //function _tolower(ch: char) ((unsigned char)(c)-'A'+'a')
  489. // ioctl.h
  490. function FIOCNBLOCK: longint;
  491. function FIOCSCAN: longint; // scan for input
  492. // tty and sio
  493. function TIOCRAW: longint; // disable xon/xoff control
  494. function TIOCFLUSH: longint; // flush input buffer
  495. function TIOCREOPEN: longint; // reopen
  496. function TIOCBAUD: longint; // set baud rate
  497. function TIOCEXIT: longint; // console interrup
  498. function TIOCDTR: longint; // control DTR line
  499. function TIOCRTS: longint; // control RTS line
  500. function TIOCLEN: longint; // stop<<16 | char
  501. // stop 0:none 1:1 2:1.5 3:2bit
  502. // char 0:5 1:6 2:7 3:8bit
  503. function TIOCPARITY: longint; // parity 0:none 1:e 3:o
  504. function TIOSTATUS: longint; // return status
  505. function TIOERRRST: longint; // error reset
  506. function TIOEXIST: longint; // exist test with DTR/CTS
  507. function TIORLEN: longint; // receive buffer length
  508. // disk
  509. function DIOFORMAT: longint; // format
  510. // fcntl.h
  511. const
  512. FREAD = $0001; // readable
  513. FWRITE = $0002; // writable
  514. FNBLOCK = $0004; // non-blocking reads
  515. FRLOCK = $0010; // read locked (non-shared)
  516. FWLOCK = $0020; // write locked (non-shared)
  517. FAPPEND = $0100; // append on each write
  518. FCREAT = $0200; // create if nonexistant
  519. FTRUNC = $0400; // truncate to zero length
  520. FSCAN = $1000; // scan type
  521. FRCOM = $2000; // remote command entry
  522. FNBUF = $4000; // no ring buf. and console interrupt
  523. FASYNC = $8000; // asyncronous i/o
  524. // file.h
  525. const
  526. // Flag for open()
  527. O_RDONLY = FREAD;
  528. O_WRONLY = FWRITE;
  529. O_RDWR = FREAD or FWRITE;
  530. O_CREAT = FCREAT; // open with file create
  531. O_NOBUF = FNBUF; // no device buffer and console interrupt
  532. O_NBLOCK = FNBLOCK; // non blocking mode
  533. O_NOWAIT = FASYNC; // asyncronous i/o
  534. // fs.h
  535. // device table
  536. type
  537. Tdevice_table = packed record
  538. dt_string : pchar; // device name
  539. dt_type : longint; // device "type"
  540. dt_bsize : longint; // file system type
  541. dt_desc : pchar; // device description
  542. dt_init : function(): plongint; // device init routine
  543. dt_open : function(): plongint; // device open routine
  544. dt_strategy : function(): plongint; // device strategy routine, returns cnt
  545. dt_close : function(): plongint; // device close routine
  546. dt_ioctl : function(): plongint; // device ioctl routine
  547. dt_read : function(): plongint; // fs read routine, returns count
  548. dt_write : function(): plongint; // fs write routine, return count
  549. dt_delete : function(): plongint; // file delete routine
  550. dt_undelete : function(): plongint; // file delete routine
  551. dt_firstfile : function(): plongint;// directory serach routine
  552. dt_nextfile : function(): plongint; // directory serach routine
  553. dt_format : function(): plongint;
  554. dt_cd : function(): plongint;
  555. dt_rename : function(): plongint;
  556. dt_remove : function(): plongint;
  557. dt_else : function(): plongint;
  558. end;
  559. Pdevice_table = ^Tdevice_table;
  560. // device types
  561. const
  562. DTTYPE_CHAR = $1; // character device
  563. DTTYPE_CONS = $2; // can be console
  564. DTTYPE_BLOCK = $4; // block device
  565. DTTYPE_RAW = $8; // raw device that uses fs switch
  566. DTTYPE_FS = $10;
  567. // character device flags
  568. DB_RAW = $1; // don't interpret special chars
  569. DB_STOPPED = $2; // stop output
  570. DB_BREAK = $4; // cntl-c raise console interrpt
  571. // character device buffer
  572. CBUFSIZE = 256;
  573. type
  574. device_buf = packed record
  575. db_flags : longint; // character device flags
  576. db_in : pchar; // pts at next free char
  577. db_out : pchar; // pts at next filled char
  578. db_buf : array [0..CBUFSIZE - 1] of char; // circular buffer for input
  579. end;
  580. // circular buffer functions */
  581. {
  582. CIRC_EMPTY(x) ((x)->db_in == (x)->db_out)
  583. CIRC_FLUSH(x) ((x)->db_in = (x)->db_out = (x)->db_buf)
  584. CIRC_STOPPED(x) ((x)->db_flags & DB_STOPPED)
  585. }
  586. // io block
  587. iob = packed record
  588. i_flgs : longint;
  589. i_unit : longint; // pseudo device unit
  590. i_ma : pchar; // memory address of i/o buffer
  591. i_cc : dword; // character count of transfer
  592. i_offset : dword; // seek offset in file
  593. i_fstype : longint; // file system type
  594. i_errno : longint; // error # return
  595. i_dp : Pdevice_table; // pointer into device_table
  596. i_size : dword;
  597. i_head : longint;
  598. i_fd : longint; // file descriptor
  599. end;
  600. // Request codes
  601. const
  602. READ = 1;
  603. WRITE = 2;
  604. NIOB = 16; // max number of open files
  605. // r3000.h
  606. // Segment base addresses and sizes
  607. K0BASE = $80000000;
  608. K0SIZE = $20000000;
  609. K1BASE = $A0000000;
  610. K1SIZE = $20000000;
  611. K2BASE = $C0000000;
  612. K2SIZE = $20000000;
  613. // Exception vectors
  614. UT_VEC = K0BASE; // utlbmiss vector
  615. E_VEC = K0BASE + $80; // exception vector
  616. R_VEC = K1BASE + $1fc00000; // reset vector
  617. // Address conversion macros
  618. function K0_TO_K1(x: dword): dword; // kseg0 to kseg1
  619. function K1_TO_K0(x: dword): dword; // kseg1 to kseg0
  620. function K0_TO_PHYS(x: dword): dword; // kseg0 to physical
  621. function K1_TO_PHYS(x: dword): dword; // kseg1 to physical
  622. function PHYS_TO_K0(x: dword): dword; // physical to kseg0
  623. function PHYS_TO_K1(x: dword): dword; // physical to kseg1
  624. // Address predicates
  625. function IS_KSEG0(x: dword): boolean;
  626. function IS_KSEG1(x: dword): boolean;
  627. //function IS_KSEG2(x: dword): boolean;
  628. //function IS_KPTESEG(x: dword): boolean;
  629. function IS_KUSEG(x: dword): boolean;
  630. const
  631. // Cache size constants
  632. MINCACHE = 4 * 1024;
  633. MAXCACHE = 64 * 1024;
  634. // Status register
  635. SR_CUMASK = $f0000000; // coproc usable bits
  636. SR_CU3 = $80000000; // Coprocessor 3 usable
  637. SR_CU2 = $40000000; // Coprocessor 2 usable
  638. SR_CU1 = $20000000; // Coprocessor 1 usable
  639. SR_CU0 = $10000000; // Coprocessor 0 usable
  640. SR_BEV = $00400000; // use boot exception vectors
  641. // Cache control bits
  642. SR_TS = $00200000; // TLB shutdown
  643. SR_PE = $00100000; // cache parity error
  644. SR_CM = $00080000; // cache miss
  645. SR_PZ = $00040000; // cache parity zero
  646. SR_SWC = $00020000; // swap cache
  647. SR_ISC = $00010000; // Isolate data cache
  648. SR_MM_MODE = $00010000; // lwl/swl/etc become scache/etc
  649. // Interrupt enable bits
  650. // (NOTE: bits set to 1 enable the corresponding level interrupt)
  651. SR_IMASK = $0000ff00; // Interrupt mask
  652. SR_IMASK8 = $00000000; // mask level 8
  653. SR_IMASK7 = $00008000; // mask level 7
  654. SR_IMASK6 = $0000c000; // mask level 6
  655. SR_IMASK5 = $0000e000; // mask level 5
  656. SR_IMASK4 = $0000f000; // mask level 4
  657. SR_IMASK3 = $0000f800; // mask level 3
  658. SR_IMASK2 = $0000fc00; // mask level 2
  659. SR_IMASK1 = $0000fe00; // mask level 1
  660. SR_IMASK0 = $0000ff00; // mask level 0
  661. SR_IBIT8 = $00008000; // bit level 8
  662. SR_IBIT7 = $00004000; // bit level 7
  663. SR_IBIT6 = $00002000; // bit level 6
  664. SR_IBIT5 = $00001000; // bit level 5
  665. SR_IBIT4 = $00000800; // bit level 4
  666. SR_IBIT3 = $00000400; // bit level 3
  667. SR_IBIT2 = $00000200; // bit level 2
  668. SR_IBIT1 = $00000100; // bit level 1
  669. SR_KUO = $00000020; // old kernel/user, 0 => k, 1 => u
  670. SR_IEO = $00000010; // old interrupt enable, 1 => enable
  671. SR_KUP = $00000008; // prev kernel/user, 0 => k, 1 => u
  672. SR_IEP = $00000004; // prev interrupt enable, 1 => enable
  673. SR_KUC = $00000002; // cur kernel/user, 0 => k, 1 => u
  674. SR_IEC = $00000001; // cur interrupt enable, 1 => enable
  675. SR_IMASKSHIFT = 8;
  676. SR_FMT = '\20\40BD\26TS\25PE\24CM\23PZ\22SwC\21IsC\20IM7\17IM6\16IM5\15IM4\14IM3\13IM2\12IM1\11IM0\6KUo\5IEo\4KUp\3IEp\2KUc\1IEc';
  677. // Cause Register
  678. CAUSE_BD = $80000000; // Branch delay slot */
  679. CAUSE_CEMASK = $30000000; // coprocessor error */
  680. CAUSE_CESHIFT = 28;
  681. /// Interrupt pending bits
  682. CAUSE_IP8 = $00008000; // External level 8 pending
  683. CAUSE_IP7 = $00004000; // External level 7 pending
  684. CAUSE_IP6 = $00002000; // External level 6 pending
  685. CAUSE_IP5 = $00001000; // External level 5 pending
  686. CAUSE_IP4 = $00000800; // External level 4 pending
  687. CAUSE_IP3 = $00000400; // External level 3 pending
  688. CAUSE_SW2 = $00000200; // Software level 2 pending
  689. CAUSE_SW1 = $00000100; // Software level 1 pending
  690. CAUSE_IPMASK = $0000FF00; // Pending interrupt mask
  691. CAUSE_IPSHIFT = 8;
  692. CAUSE_EXCMASK = $0000003C; // Cause code bits
  693. CAUSE_EXCSHIFT = 2;
  694. CAUSE_FMT = '\20\40BD\36CE1\35CE0\20IP8\17IP7\16IP6\15IP5\14IP4\13IP3\12SW2\11SW1\1INT';
  695. // Cause register exception codes
  696. function EXC_CODE(x: dword): dword;
  697. const
  698. // Hardware exception codes
  699. EXC_INT = 0 shl 2; // interrupt
  700. EXC_MOD = 1 shl 2; // TLB mod
  701. EXC_RMISS = 2 shl 2; // Read TLB Miss
  702. EXC_WMISS = 3 shl 2; // Write TLB Miss
  703. EXC_RADE = 4 shl 2; // Read Address Error
  704. EXC_WADE = 5 shl 2; // Write Address Error
  705. EXC_IBE = 6 shl 2; // Instruction Bus Error
  706. EXC_DBE = 7 shl 2; // Data Bus Error
  707. EXC_SYSCALL = 8 shl 2; // SYSCALL
  708. EXC_BREAK = 9 shl 2; // BREAKpoint
  709. EXC_II = 10 shl 2; // Illegal Instruction
  710. EXC_CPU = 11 shl 2; // CoProcessor Unusable
  711. EXC_OV = 12 shl 2; // OVerflow
  712. // software exception codes
  713. SEXC_SEGV = 16 shl 2; // Software detected seg viol
  714. SEXC_RESCHED= 17 shl 2; // resched request
  715. SEXC_PAGEIN = 18 shl 2; // page-in request
  716. SEXC_CPU = 19 shl 2; // coprocessor unusable
  717. // Coprocessor 0 registers
  718. C0_INX = 0; // tlb index
  719. C0_RAND = 1; // tlb random
  720. C0_TLBLO = 2; // tlb entry low
  721. C0_CTXT = 4; // tlb context
  722. C0_PIDMASK = 6; // Mips2
  723. C0_BADVADDR = 8; // bad virtual address
  724. C0_TLBHI = 10; // tlb entry hi
  725. C0_PID = 10; // Mips2
  726. C0_SR = 12; // status register
  727. C0_CAUSE = 13; // exception cause
  728. C0_EPC = 14; // exception pc
  729. C0_PRID = 15; // revision identifier
  730. C0_ERREG = 16; // Mips2
  731. // Coprocessor 0 operations
  732. C0_READI = $1; // read ITLB entry addressed by C0_INDEX
  733. C0_WRITEI = $2; // write ITLB entry addressed by C0_INDEX
  734. C0_WRITER = $6; // write ITLB entry addressed by C0_RAND
  735. C0_PROBE = $8; // probe for ITLB entry addressed by TLBHI
  736. C0_RFE = $10; // restore for exception
  737. // Flags for the nofault handler. 0 means no fault is expected.
  738. NF_BADADDR = 1; // badaddr, wbadaddr
  739. NF_COPYIO = 2; // copyin, copyout
  740. NF_ADDUPC = 3; // addupc
  741. NF_FSUMEM = 4; // fubyte, subyte, fuword, suword
  742. NF_USERACC = 5; // useracc
  743. NF_SOFTFP = 6; // softfp
  744. NF_REVID = 7; // revision ids
  745. NF_NENTRIES = 8;
  746. // TLB size constants
  747. TLBWIREDBASE = 0; // WAG for now
  748. NWIREDENTRIES = 8; // WAG for now
  749. TLBRANDOMBASE = NWIREDENTRIES;
  750. NTLBENTRIES = 64;
  751. NRANDOMENTRIES = (NTLBENTRIES - NWIREDENTRIES);
  752. TLBRAND_RANDMASK = $00003f00;
  753. TLBRAND_RANDSHIFT = 8;
  754. // Chip interrupt vector
  755. NC0VECS = 8;
  756. // extern int (*c0vec_tbl[])();
  757. var
  758. c0vec_tbl : array of function: longint; external;
  759. const
  760. BRK_KERNEL = $f1;
  761. EXCEPT_NORM = 1;
  762. EXCEPT_UTLB = 2;
  763. EXCEPT_BRKPT = 3;
  764. EXCEPT_DB = 4;
  765. EXCEPT_GDB = 4;
  766. EXCEPT_INT = 9;
  767. EXCEPT_ELSE = $ff;
  768. // setjmp.h
  769. // simple non-local-jump for single task environment
  770. // jmp_buf indices
  771. const
  772. JB_PC = 0;
  773. JB_SP = 1;
  774. JB_FP = 2;
  775. JB_S0 = 3;
  776. JB_S1 = 4;
  777. JB_S2 = 5;
  778. JB_S3 = 6;
  779. JB_S4 = 7;
  780. JB_S5 = 8;
  781. JB_S6 = 9;
  782. JB_S7 = 10;
  783. JB_GP = 11;
  784. JB_SIZE = 12;
  785. type
  786. jmp_buf = array [0..JB_SIZE - 1] of longint;
  787. function setjmp(buf: jmp_buf): longint; external;
  788. procedure longjmp(buf: jmp_buf; i: longint); external;
  789. // libsio.h
  790. // status bits
  791. const
  792. SR_IRQ = $200;
  793. SR_CTS = $100;
  794. SR_DSR = $80;
  795. SR_FE = $20;
  796. SR_OE = $10;
  797. SR_PERROR = $8;
  798. SR_TXU = $4;
  799. SR_RXRDY = $2;
  800. SR_TXRDY = $1;
  801. SIO_CTS = $100;
  802. SIO_DSR = $80;
  803. SIO_FE = $20;
  804. SIO_OE = $10;
  805. SIO_PERROR = $8;
  806. SIO_TXU = $4;
  807. SIO_RXRDY = $2;
  808. SIO_TXRDY = $1;
  809. // control bits
  810. CR_DSRIEN = $1000;
  811. CR_RXIEN = $800;
  812. CR_TXIEN = $400;
  813. CR_BUFSZ_1 = $0;
  814. CR_BUFSZ_2 = $100;
  815. CR_BUFSZ_4 = $200;
  816. CR_BUFSZ_8 = $300;
  817. CR_INTRST = $40;
  818. CR_RTS = $20;
  819. CR_ERRRST = $10;
  820. CR_BRK = $8;
  821. CR_RXEN = $4;
  822. CR_DTR = $2;
  823. CR_TXEN = $1;
  824. SIO_BIT_DTR = CR_DTR;
  825. SIO_BIT_RTS = CR_RTS;
  826. // mode bits
  827. MR_SB_00 = $0;
  828. MR_SB_01 = $40;
  829. MR_SB_10 = $80;
  830. MR_SB_11 = $C0;
  831. MR_P_EVEN = $20;
  832. MR_PEN = $10;
  833. MR_CHLEN_5 = $0;
  834. MR_CHLEN_6 = $4;
  835. MR_CHLEN_7 = $8;
  836. MR_CHLEN_8 = $C;
  837. MR_BR_1 = $1;
  838. MR_BR_16 = $2;
  839. MR_BR_64 = $3;
  840. type
  841. Sio1CallbackFunction = function: pointer;
  842. function AddSIO(baud: longint): longint; external;
  843. function DelSIO: longint; external;
  844. function _sio_control(cmd, arg, param: dword): longint; external;
  845. function Sio1Callback(func: Sio1CallbackFunction): longint; external;
  846. generic function __va_rounded_size<T>: SizeInt;
  847. generic procedure va_start<T>(out AP: Pointer; var LastArg: T);
  848. procedure va_end(out AP: Pointer);
  849. generic function va_arg<T>(var AP: Pointer):T;
  850. implementation
  851. generic function __va_rounded_size<T>: SizeInt;inline;
  852. begin
  853. Result := ((sizeof(T) + sizeof (longint) - 1) div sizeof (longint)) * sizeof (longint);
  854. end;
  855. generic procedure va_start<T>(out AP: Pointer; var LastArg: T);inline;
  856. begin
  857. AP := Pointer(@LastArg) + specialize __va_rounded_size<T>;
  858. end;
  859. procedure va_end(out AP: Pointer);inline;
  860. begin
  861. AP := nil;
  862. end;
  863. generic function va_arg<T>(var AP: Pointer):T;inline;
  864. type
  865. PT=^T;
  866. begin
  867. Result := PT(AP)^;
  868. Inc(AP, specialize __va_rounded_size<T>);
  869. end;
  870. function strdup(p: pchar): pchar;
  871. begin
  872. strdup:= malloc2(strlen(p) + 1);
  873. strcpy(strdup, p);
  874. end;
  875. function isalpha(ch: char): boolean;
  876. begin
  877. isalpha:= boolean(byte(ch) and (_U or _L));
  878. end;
  879. function isupper(ch: char): boolean;
  880. begin
  881. isupper:= boolean(byte(ch) and _U);
  882. end;
  883. function islower(ch: char): boolean;
  884. begin
  885. islower:= boolean(byte(ch) and _L);
  886. end;
  887. function isdigit(ch: char): boolean;
  888. begin
  889. isdigit:= boolean(byte(ch) and _N);
  890. end;
  891. function isxdigit(ch: char): boolean;
  892. begin
  893. isxdigit:= boolean(byte(ch) and (_X or _N));
  894. end;
  895. function isspace(ch: char): boolean;
  896. begin
  897. isspace:= boolean(byte(ch) and _S);
  898. end;
  899. function ispunct(ch: char): boolean;
  900. begin
  901. ispunct:= boolean(byte(ch) and _P);
  902. end;
  903. function isalnum(ch: char): boolean;
  904. begin
  905. isalnum:= boolean(byte(ch) and (_U or _L or _N));
  906. end;
  907. function isprint(ch: char): boolean;
  908. begin
  909. isprint:= boolean(byte(ch) and (_P or _U or _L or _N or _B));
  910. end;
  911. function isgraph(ch: char): boolean;
  912. begin
  913. isgraph:= boolean(byte(ch) and (_P or _U or _L or _N));
  914. end;
  915. function iscntrl(ch: char): boolean;
  916. begin
  917. iscntrl:= boolean(byte(ch) and _C);
  918. end;
  919. function isascii(ch: char): boolean;
  920. begin
  921. isascii:= boolean(byte(ch) <= $7f);
  922. end;
  923. function toascii(ch: char): char;
  924. begin
  925. toascii:= char(byte(ch) and $7f);
  926. end;
  927. function major(x: longint): dword;
  928. begin
  929. major:= dword((x shr 8) and 0377);
  930. end;
  931. function minor(x: longint): longint;
  932. begin
  933. minor:= x and 0377;
  934. end;
  935. function makedev(x, y: longint): longint;
  936. begin
  937. makedev:= ((x shl 8) or y);
  938. end;
  939. function FIOCNBLOCK: longint;
  940. begin
  941. FIOCNBLOCK:= (dword('f') shl 8) or 1;
  942. end;
  943. function FIOCSCAN: longint;
  944. begin
  945. FIOCSCAN:= (dword('f') shl 8) or 2;
  946. end;
  947. function TIOCRAW: longint;
  948. begin
  949. TIOCRAW:= (dword('t') shl 8) or 1;
  950. end;
  951. function TIOCFLUSH: longint;
  952. begin
  953. TIOCFLUSH:= (dword('t') shl 8) or 2;
  954. end;
  955. function TIOCREOPEN: longint;
  956. begin
  957. TIOCREOPEN:= (dword('t') shl 8) or 3;
  958. end;
  959. function TIOCBAUD: longint;
  960. begin
  961. TIOCBAUD:= (dword('t') shl 8) or 4;
  962. end;
  963. function TIOCEXIT: longint;
  964. begin
  965. TIOCEXIT:= (dword('t') shl 8) or 5;
  966. end;
  967. function TIOCDTR: longint;
  968. begin
  969. TIOCDTR:= (dword('t') shl 8) or 6;
  970. end;
  971. function TIOCRTS: longint;
  972. begin
  973. TIOCRTS:= (dword('t') shl 8) or 7;
  974. end;
  975. function TIOCLEN: longint;
  976. begin
  977. TIOCLEN:= (dword('t') shl 8) or 8;
  978. end;
  979. function TIOCPARITY: longint;
  980. begin
  981. TIOCPARITY:= (dword('t') shl 8) or 9;
  982. end;
  983. function TIOSTATUS: longint;
  984. begin
  985. TIOSTATUS:= (dword('t') shl 8) or 10;
  986. end;
  987. function TIOERRRST: longint;
  988. begin
  989. TIOERRRST:= (dword('t') shl 8) or 11;
  990. end;
  991. function TIOEXIST: longint;
  992. begin
  993. TIOEXIST:= (dword('t') shl 8) or 12;
  994. end;
  995. function TIORLEN: longint;
  996. begin
  997. TIORLEN:= (dword('t') shl 8) or 13;
  998. end;
  999. function DIOFORMAT: longint;
  1000. begin
  1001. DIOFORMAT:= (dword('d') shl 8) or 1;
  1002. end;
  1003. // Address conversion macros
  1004. function K0_TO_K1(x: dword): dword;
  1005. begin
  1006. result:= x or $A0000000;
  1007. end;
  1008. function K1_TO_K0(x: dword): dword;
  1009. begin
  1010. result:= x and $9FFFFFFF;
  1011. end;
  1012. function K0_TO_PHYS(x: dword): dword;
  1013. begin
  1014. result:= x and $1FFFFFFF;
  1015. end;
  1016. function K1_TO_PHYS(x: dword): dword;
  1017. begin
  1018. result:= x and $1FFFFFFF;
  1019. end;
  1020. function PHYS_TO_K0(x: dword): dword;
  1021. begin
  1022. result:= x or $80000000;
  1023. end;
  1024. function PHYS_TO_K1(x: dword): dword;
  1025. begin
  1026. result:= x or $A0000000;
  1027. end;
  1028. // Address predicates
  1029. function IS_KSEG0(x: dword): boolean;
  1030. begin
  1031. result:= ((x >= K0BASE) and (x < K1BASE));
  1032. end;
  1033. function IS_KSEG1(x: dword): boolean;
  1034. begin
  1035. result:= ((x >= K1BASE) and (x < K2BASE));
  1036. end;
  1037. {
  1038. function IS_KSEG2(x: dword): boolean;
  1039. begin
  1040. result:= ((x >= K2BASE) and (x < KPTEBASE));
  1041. end;
  1042. function IS_KPTESEG(x: dword): boolean;
  1043. begin
  1044. result:= (x >= KPTEBASE);
  1045. end;
  1046. }
  1047. function IS_KUSEG(x: dword): boolean;
  1048. begin
  1049. result:= (x < K0BASE);
  1050. end;
  1051. function EXC_CODE(x: dword): dword;
  1052. begin
  1053. result:= x shl 2;
  1054. end;
  1055. begin
  1056. end.