libstd.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. {
  650. define:
  651. lcache lwl
  652. scache swl
  653. flush lwr $0,
  654. inval swr $0,
  655. }
  656. // Interrupt enable bits
  657. // (NOTE: bits set to 1 enable the corresponding level interrupt)
  658. SR_IMASK = $0000ff00; // Interrupt mask
  659. SR_IMASK8 = $00000000; // mask level 8
  660. SR_IMASK7 = $00008000; // mask level 7
  661. SR_IMASK6 = $0000c000; // mask level 6
  662. SR_IMASK5 = $0000e000; // mask level 5
  663. SR_IMASK4 = $0000f000; // mask level 4
  664. SR_IMASK3 = $0000f800; // mask level 3
  665. SR_IMASK2 = $0000fc00; // mask level 2
  666. SR_IMASK1 = $0000fe00; // mask level 1
  667. SR_IMASK0 = $0000ff00; // mask level 0
  668. SR_IBIT8 = $00008000; // bit level 8
  669. SR_IBIT7 = $00004000; // bit level 7
  670. SR_IBIT6 = $00002000; // bit level 6
  671. SR_IBIT5 = $00001000; // bit level 5
  672. SR_IBIT4 = $00000800; // bit level 4
  673. SR_IBIT3 = $00000400; // bit level 3
  674. SR_IBIT2 = $00000200; // bit level 2
  675. SR_IBIT1 = $00000100; // bit level 1
  676. SR_KUO = $00000020; // old kernel/user, 0 => k, 1 => u
  677. SR_IEO = $00000010; // old interrupt enable, 1 => enable
  678. SR_KUP = $00000008; // prev kernel/user, 0 => k, 1 => u
  679. SR_IEP = $00000004; // prev interrupt enable, 1 => enable
  680. SR_KUC = $00000002; // cur kernel/user, 0 => k, 1 => u
  681. SR_IEC = $00000001; // cur interrupt enable, 1 => enable
  682. SR_IMASKSHIFT = 8;
  683. SR_FMT = '\20\40BD\26TS\25PE\24CM\23PZ\22SwC\21IsC\20IM7\17IM6\16IM5\15IM4\14IM3\13IM2\12IM1\11IM0\6KUo\5IEo\4KUp\3IEp\2KUc\1IEc';
  684. // Cause Register
  685. CAUSE_BD = $80000000; // Branch delay slot */
  686. CAUSE_CEMASK = $30000000; // coprocessor error */
  687. CAUSE_CESHIFT = 28;
  688. /// Interrupt pending bits
  689. CAUSE_IP8 = $00008000; // External level 8 pending
  690. CAUSE_IP7 = $00004000; // External level 7 pending
  691. CAUSE_IP6 = $00002000; // External level 6 pending
  692. CAUSE_IP5 = $00001000; // External level 5 pending
  693. CAUSE_IP4 = $00000800; // External level 4 pending
  694. CAUSE_IP3 = $00000400; // External level 3 pending
  695. CAUSE_SW2 = $00000200; // Software level 2 pending
  696. CAUSE_SW1 = $00000100; // Software level 1 pending
  697. CAUSE_IPMASK = $0000FF00; // Pending interrupt mask
  698. CAUSE_IPSHIFT = 8;
  699. CAUSE_EXCMASK = $0000003C; // Cause code bits
  700. CAUSE_EXCSHIFT = 2;
  701. CAUSE_FMT = '\20\40BD\36CE1\35CE0\20IP8\17IP7\16IP6\15IP5\14IP4\13IP3\12SW2\11SW1\1INT';
  702. // Cause register exception codes
  703. function EXC_CODE(x: dword): dword;
  704. const
  705. // Hardware exception codes
  706. EXC_INT = 0 shl 2; // interrupt
  707. EXC_MOD = 1 shl 2; // TLB mod
  708. EXC_RMISS = 2 shl 2; // Read TLB Miss
  709. EXC_WMISS = 3 shl 2; // Write TLB Miss
  710. EXC_RADE = 4 shl 2; // Read Address Error
  711. EXC_WADE = 5 shl 2; // Write Address Error
  712. EXC_IBE = 6 shl 2; // Instruction Bus Error
  713. EXC_DBE = 7 shl 2; // Data Bus Error
  714. EXC_SYSCALL = 8 shl 2; // SYSCALL
  715. EXC_BREAK = 9 shl 2; // BREAKpoint
  716. EXC_II = 10 shl 2; // Illegal Instruction
  717. EXC_CPU = 11 shl 2; // CoProcessor Unusable
  718. EXC_OV = 12 shl 2; // OVerflow
  719. // software exception codes
  720. SEXC_SEGV = 16 shl 2; // Software detected seg viol
  721. SEXC_RESCHED= 17 shl 2; // resched request
  722. SEXC_PAGEIN = 18 shl 2; // page-in request
  723. SEXC_CPU = 19 shl 2; // coprocessor unusable
  724. // Coprocessor 0 registers
  725. C0_INX = 0; // tlb index
  726. C0_RAND = 1; // tlb random
  727. C0_TLBLO = 2; // tlb entry low
  728. C0_CTXT = 4; // tlb context
  729. C0_PIDMASK = 6; // Mips2
  730. C0_BADVADDR = 8; // bad virtual address
  731. C0_TLBHI = 10; // tlb entry hi
  732. C0_PID = 10; // Mips2
  733. C0_SR = 12; // status register
  734. C0_CAUSE = 13; // exception cause
  735. C0_EPC = 14; // exception pc
  736. C0_PRID = 15; // revision identifier
  737. C0_ERREG = 16; // Mips2
  738. // Coprocessor 0 operations
  739. C0_READI = $1; // read ITLB entry addressed by C0_INDEX
  740. C0_WRITEI = $2; // write ITLB entry addressed by C0_INDEX
  741. C0_WRITER = $6; // write ITLB entry addressed by C0_RAND
  742. C0_PROBE = $8; // probe for ITLB entry addressed by TLBHI
  743. C0_RFE = $10; // restore for exception
  744. // Flags for the nofault handler. 0 means no fault is expected.
  745. NF_BADADDR = 1; // badaddr, wbadaddr
  746. NF_COPYIO = 2; // copyin, copyout
  747. NF_ADDUPC = 3; // addupc
  748. NF_FSUMEM = 4; // fubyte, subyte, fuword, suword
  749. NF_USERACC = 5; // useracc
  750. NF_SOFTFP = 6; // softfp
  751. NF_REVID = 7; // revision ids
  752. NF_NENTRIES = 8;
  753. // TLB size constants
  754. TLBWIREDBASE = 0; // WAG for now
  755. NWIREDENTRIES = 8; // WAG for now
  756. TLBRANDOMBASE = NWIREDENTRIES;
  757. NTLBENTRIES = 64;
  758. NRANDOMENTRIES = (NTLBENTRIES - NWIREDENTRIES);
  759. TLBRAND_RANDMASK = $00003f00;
  760. TLBRAND_RANDSHIFT = 8;
  761. // Chip interrupt vector
  762. NC0VECS = 8;
  763. // extern int (*c0vec_tbl[])();
  764. var
  765. c0vec_tbl : array of function: longint; external;
  766. const
  767. BRK_KERNEL = $f1;
  768. EXCEPT_NORM = 1;
  769. EXCEPT_UTLB = 2;
  770. EXCEPT_BRKPT = 3;
  771. EXCEPT_DB = 4;
  772. EXCEPT_GDB = 4;
  773. EXCEPT_INT = 9;
  774. EXCEPT_ELSE = $ff;
  775. // setjmp.h
  776. // simple non-local-jump for single task environment
  777. // jmp_buf indices
  778. const
  779. JB_PC = 0;
  780. JB_SP = 1;
  781. JB_FP = 2;
  782. JB_S0 = 3;
  783. JB_S1 = 4;
  784. JB_S2 = 5;
  785. JB_S3 = 6;
  786. JB_S4 = 7;
  787. JB_S5 = 8;
  788. JB_S6 = 9;
  789. JB_S7 = 10;
  790. JB_GP = 11;
  791. JB_SIZE = 12;
  792. type
  793. jmp_buf = array [0..JB_SIZE - 1] of longint;
  794. function setjmp(buf: jmp_buf): longint; external;
  795. procedure longjmp(buf: jmp_buf; i: longint); external;
  796. // libsio.h
  797. // status bits
  798. const
  799. SR_IRQ = $200;
  800. SR_CTS = $100;
  801. SR_DSR = $80;
  802. SR_FE = $20;
  803. SR_OE = $10;
  804. SR_PERROR = $8;
  805. SR_TXU = $4;
  806. SR_RXRDY = $2;
  807. SR_TXRDY = $1;
  808. SIO_CTS = $100;
  809. SIO_DSR = $80;
  810. SIO_FE = $20;
  811. SIO_OE = $10;
  812. SIO_PERROR = $8;
  813. SIO_TXU = $4;
  814. SIO_RXRDY = $2;
  815. SIO_TXRDY = $1;
  816. // control bits
  817. CR_DSRIEN = $1000;
  818. CR_RXIEN = $800;
  819. CR_TXIEN = $400;
  820. CR_BUFSZ_1 = $0;
  821. CR_BUFSZ_2 = $100;
  822. CR_BUFSZ_4 = $200;
  823. CR_BUFSZ_8 = $300;
  824. CR_INTRST = $40;
  825. CR_RTS = $20;
  826. CR_ERRRST = $10;
  827. CR_BRK = $8;
  828. CR_RXEN = $4;
  829. CR_DTR = $2;
  830. CR_TXEN = $1;
  831. SIO_BIT_DTR = CR_DTR;
  832. SIO_BIT_RTS = CR_RTS;
  833. // mode bits
  834. MR_SB_00 = $0;
  835. MR_SB_01 = $40;
  836. MR_SB_10 = $80;
  837. MR_SB_11 = $C0;
  838. MR_P_EVEN = $20;
  839. MR_PEN = $10;
  840. MR_CHLEN_5 = $0;
  841. MR_CHLEN_6 = $4;
  842. MR_CHLEN_7 = $8;
  843. MR_CHLEN_8 = $C;
  844. MR_BR_1 = $1;
  845. MR_BR_16 = $2;
  846. MR_BR_64 = $3;
  847. type
  848. Sio1CallbackFunction = function: pointer;
  849. function AddSIO(baud: longint): longint; external;
  850. function DelSIO: longint; external;
  851. function _sio_control(cmd, arg, param: dword): longint; external;
  852. function Sio1Callback(func: Sio1CallbackFunction): longint; external;
  853. generic function __va_rounded_size<T>: SizeInt;
  854. generic procedure va_start<T>(out AP: Pointer; var LastArg: T);
  855. procedure va_end(out AP: Pointer);
  856. generic function va_arg<T>(var AP: Pointer):T;
  857. implementation
  858. generic function __va_rounded_size<T>: SizeInt;inline;
  859. begin
  860. Result := ((sizeof(T) + sizeof (longint) - 1) div sizeof (longint)) * sizeof (longint);
  861. end;
  862. generic procedure va_start<T>(out AP: Pointer; var LastArg: T);inline;
  863. begin
  864. AP := Pointer(@LastArg) + specialize __va_rounded_size<T>;
  865. end;
  866. procedure va_end(out AP: Pointer);inline;
  867. begin
  868. AP := nil;
  869. end;
  870. generic function va_arg<T>(var AP: Pointer):T;inline;
  871. type
  872. PT=^T;
  873. begin
  874. Result := PT(AP)^;
  875. Inc(AP, specialize __va_rounded_size<T>);
  876. end;
  877. function strdup(p: pchar): pchar;
  878. begin
  879. strdup:= malloc2(strlen(p) + 1);
  880. strcpy(strdup, p);
  881. end;
  882. function isalpha(ch: char): boolean;
  883. begin
  884. isalpha:= boolean(byte(ch) and (_U or _L));
  885. end;
  886. function isupper(ch: char): boolean;
  887. begin
  888. isupper:= boolean(byte(ch) and _U);
  889. end;
  890. function islower(ch: char): boolean;
  891. begin
  892. islower:= boolean(byte(ch) and _L);
  893. end;
  894. function isdigit(ch: char): boolean;
  895. begin
  896. isdigit:= boolean(byte(ch) and _N);
  897. end;
  898. function isxdigit(ch: char): boolean;
  899. begin
  900. isxdigit:= boolean(byte(ch) and (_X or _N));
  901. end;
  902. function isspace(ch: char): boolean;
  903. begin
  904. isspace:= boolean(byte(ch) and _S);
  905. end;
  906. function ispunct(ch: char): boolean;
  907. begin
  908. ispunct:= boolean(byte(ch) and _P);
  909. end;
  910. function isalnum(ch: char): boolean;
  911. begin
  912. isalnum:= boolean(byte(ch) and (_U or _L or _N));
  913. end;
  914. function isprint(ch: char): boolean;
  915. begin
  916. isprint:= boolean(byte(ch) and (_P or _U or _L or _N or _B));
  917. end;
  918. function isgraph(ch: char): boolean;
  919. begin
  920. isgraph:= boolean(byte(ch) and (_P or _U or _L or _N));
  921. end;
  922. function iscntrl(ch: char): boolean;
  923. begin
  924. iscntrl:= boolean(byte(ch) and _C);
  925. end;
  926. function isascii(ch: char): boolean;
  927. begin
  928. isascii:= boolean(byte(ch) <= $7f);
  929. end;
  930. function toascii(ch: char): char;
  931. begin
  932. toascii:= char(byte(ch) and $7f);
  933. end;
  934. function major(x: longint): dword;
  935. begin
  936. major:= dword((x shr 8) and 0377);
  937. end;
  938. function minor(x: longint): longint;
  939. begin
  940. minor:= x and 0377;
  941. end;
  942. function makedev(x, y: longint): longint;
  943. begin
  944. makedev:= ((x shl 8) or y);
  945. end;
  946. function FIOCNBLOCK: longint;
  947. begin
  948. FIOCNBLOCK:= (dword('f') shl 8) or 1;
  949. end;
  950. function FIOCSCAN: longint;
  951. begin
  952. FIOCSCAN:= (dword('f') shl 8) or 2;
  953. end;
  954. function TIOCRAW: longint;
  955. begin
  956. TIOCRAW:= (dword('t') shl 8) or 1;
  957. end;
  958. function TIOCFLUSH: longint;
  959. begin
  960. TIOCFLUSH:= (dword('t') shl 8) or 2;
  961. end;
  962. function TIOCREOPEN: longint;
  963. begin
  964. TIOCREOPEN:= (dword('t') shl 8) or 3;
  965. end;
  966. function TIOCBAUD: longint;
  967. begin
  968. TIOCBAUD:= (dword('t') shl 8) or 4;
  969. end;
  970. function TIOCEXIT: longint;
  971. begin
  972. TIOCEXIT:= (dword('t') shl 8) or 5;
  973. end;
  974. function TIOCDTR: longint;
  975. begin
  976. TIOCDTR:= (dword('t') shl 8) or 6;
  977. end;
  978. function TIOCRTS: longint;
  979. begin
  980. TIOCRTS:= (dword('t') shl 8) or 7;
  981. end;
  982. function TIOCLEN: longint;
  983. begin
  984. TIOCLEN:= (dword('t') shl 8) or 8;
  985. end;
  986. function TIOCPARITY: longint;
  987. begin
  988. TIOCPARITY:= (dword('t') shl 8) or 9;
  989. end;
  990. function TIOSTATUS: longint;
  991. begin
  992. TIOSTATUS:= (dword('t') shl 8) or 10;
  993. end;
  994. function TIOERRRST: longint;
  995. begin
  996. TIOERRRST:= (dword('t') shl 8) or 11;
  997. end;
  998. function TIOEXIST: longint;
  999. begin
  1000. TIOEXIST:= (dword('t') shl 8) or 12;
  1001. end;
  1002. function TIORLEN: longint;
  1003. begin
  1004. TIORLEN:= (dword('t') shl 8) or 13;
  1005. end;
  1006. function DIOFORMAT: longint;
  1007. begin
  1008. DIOFORMAT:= (dword('d') shl 8) or 1;
  1009. end;
  1010. // Address conversion macros
  1011. function K0_TO_K1(x: dword): dword;
  1012. begin
  1013. result:= x or $A0000000;
  1014. end;
  1015. function K1_TO_K0(x: dword): dword;
  1016. begin
  1017. result:= x and $9FFFFFFF;
  1018. end;
  1019. function K0_TO_PHYS(x: dword): dword;
  1020. begin
  1021. result:= x and $1FFFFFFF;
  1022. end;
  1023. function K1_TO_PHYS(x: dword): dword;
  1024. begin
  1025. result:= x and $1FFFFFFF;
  1026. end;
  1027. function PHYS_TO_K0(x: dword): dword;
  1028. begin
  1029. result:= x or $80000000;
  1030. end;
  1031. function PHYS_TO_K1(x: dword): dword;
  1032. begin
  1033. result:= x or $A0000000;
  1034. end;
  1035. // Address predicates
  1036. function IS_KSEG0(x: dword): boolean;
  1037. begin
  1038. result:= ((x >= K0BASE) and (x < K1BASE));
  1039. end;
  1040. function IS_KSEG1(x: dword): boolean;
  1041. begin
  1042. result:= ((x >= K1BASE) and (x < K2BASE));
  1043. end;
  1044. {
  1045. function IS_KSEG2(x: dword): boolean;
  1046. begin
  1047. result:= ((x >= K2BASE) and (x < KPTEBASE));
  1048. end;
  1049. function IS_KPTESEG(x: dword): boolean;
  1050. begin
  1051. result:= (x >= KPTEBASE);
  1052. end;
  1053. }
  1054. function IS_KUSEG(x: dword): boolean;
  1055. begin
  1056. result:= (x < K0BASE);
  1057. end;
  1058. function EXC_CODE(x: dword): dword;
  1059. begin
  1060. result:= x shl 2;
  1061. end;
  1062. begin
  1063. end.