system.pp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. unit System;
  2. interface
  3. { The heap for MSDOS is implemented
  4. in tinyheap.inc include file,
  5. but it uses default SysGetMem names }
  6. {$define HAS_MEMORYMANAGER}
  7. { define TEST_FPU_INT10 to force keeping local int10,
  8. for testing purpose only }
  9. {$DEFINE FPC_INCLUDE_SOFTWARE_MUL}
  10. {$DEFINE FPC_INCLUDE_SOFTWARE_MOD_DIV}
  11. {$DEFINE FPC_USE_SMALL_DEFAULTSTACKSIZE}
  12. { To avoid warnings in thread.inc code,
  13. but value must be really given after
  14. systemh.inc is included otherwise the
  15. $mode switch is not effective }
  16. { Use Ansi Char for files }
  17. {$define FPC_ANSI_TEXTFILEREC}
  18. {$define FPC_STDOUT_TRUE_ALIAS}
  19. {$ifdef NO_WIDESTRINGS}
  20. { Do NOT use wide Char for files }
  21. {$undef FPC_HAS_FEATURE_WIDESTRINGS}
  22. {$endif NO_WIDESTRINGS}
  23. {$I systemh.inc}
  24. {$I tnyheaph.inc}
  25. {$I portsh.inc}
  26. const
  27. LineEnding = #13#10;
  28. { LFNSupport is a variable here, defined below!!! }
  29. DirectorySeparator = '\';
  30. DriveSeparator = ':';
  31. ExtensionSeparator = '.';
  32. PathSeparator = ';';
  33. AllowDirectorySeparators : set of char = ['\','/'];
  34. AllowDriveSeparators : set of char = [':'];
  35. { FileNameCaseSensitive and FileNameCasePreserving are defined separately below!!! }
  36. maxExitCode = 255;
  37. MaxPathLen = 256;
  38. const
  39. { Default filehandles }
  40. UnusedHandle = $ffff;{ instead of -1, as it is a word value}
  41. StdInputHandle = 0;
  42. StdOutputHandle = 1;
  43. StdErrorHandle = 2;
  44. FileNameCaseSensitive : boolean = false;
  45. FileNameCasePreserving: boolean = false;
  46. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  47. sLineBreak = LineEnding;
  48. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  49. { Default memory segments (Tp7 compatibility) }
  50. seg0040: Word = $0040;
  51. segA000: Word = $A000;
  52. segB000: Word = $B000;
  53. segB800: Word = $B800;
  54. { The value that needs to be added to the segment to move the pointer by
  55. 64K bytes (BP7 compatibility) }
  56. SelectorInc: Word = $1000;
  57. var
  58. { Mem[] support }
  59. mem : array[0..$7fff-1] of byte absolute $0:$0;
  60. memw : array[0..($7fff div sizeof(word))-1] of word absolute $0:$0;
  61. meml : array[0..($7fff div sizeof(longint))-1] of longint absolute $0:$0;
  62. { C-compatible arguments and environment }
  63. argc:smallint; //!! public name 'operatingsystem_parameter_argc';
  64. argv:PPchar; //!! public name 'operatingsystem_parameter_argv';
  65. { The DOS Program Segment Prefix segment (TP7 compatibility) }
  66. PrefixSeg:Word;public name '__fpc_PrefixSeg';
  67. SaveInt00: FarPointer;public name '__SaveInt00';
  68. SaveInt10: FarPointer;public name '__SaveInt10';
  69. SaveInt75: FarPointer;public name '__SaveInt75';
  70. fpu_status: word;public name '__fpu_status';
  71. AllFilesMask: string [3];
  72. {$ifndef RTLLITE}
  73. { System info }
  74. LFNSupport : boolean;
  75. {$ELSE RTLLITE}
  76. const
  77. LFNSupport = false;
  78. {$endif RTLLITE}
  79. implementation
  80. procedure DebugWrite(const S: string); forward;
  81. procedure DebugWriteLn(const S: string); forward;
  82. const
  83. fCarry = 1;
  84. { used for an offset fixup for accessing the proc parameters in asm routines
  85. that use nostackframe. We can't use the parameter name directly, because
  86. i8086 doesn't support sp relative addressing. }
  87. {$ifdef FPC_X86_CODE_FAR}
  88. extra_param_offset = 2;
  89. {$else FPC_X86_CODE_FAR}
  90. extra_param_offset = 0;
  91. {$endif FPC_X86_CODE_FAR}
  92. {$if defined(FPC_X86_DATA_FAR) or defined(FPC_X86_DATA_HUGE)}
  93. extra_data_offset = 2;
  94. {$else}
  95. extra_data_offset = 0;
  96. {$endif}
  97. type
  98. PFarByte = ^Byte;far;
  99. PFarChar = ^Char;far;
  100. PFarWord = ^Word;far;
  101. PPFarChar = ^PFarChar;
  102. var
  103. __stktop : pointer;public name '__stktop';
  104. __stkbottom : pointer;public name '__stkbottom';
  105. __nearheap_start: pointer;public name '__nearheap_start';
  106. __nearheap_end: pointer;public name '__nearheap_end';
  107. dos_version:Word;public name 'dos_version';
  108. dos_env_count:smallint;public name '__dos_env_count';
  109. dos_argv0 : PFarChar;public name '__fpc_dos_argv0';
  110. {$I registers.inc}
  111. procedure Intr(IntNo: Byte; var Regs: Registers); external name 'FPC_INTR';
  112. procedure MsDos(var Regs: Registers); external name 'FPC_MSDOS';
  113. { invokes int 21h with the carry flag set on entry; used for the LFN functions
  114. to ensure that the carry flag is set on exit on older DOS versions which don't
  115. support them }
  116. procedure MsDos_Carry(var Regs: Registers); external name 'FPC_MSDOS_CARRY';
  117. procedure InstallInterruptHandlers; external name 'FPC_INSTALL_INTERRUPT_HANDLERS';
  118. procedure RestoreInterruptHandlers; external name 'FPC_RESTORE_INTERRUPT_HANDLERS';
  119. function CheckNullArea: Boolean; external name 'FPC_CHECK_NULLAREA';
  120. var
  121. test_fpu_jmpbuf : jmp_buf;
  122. Procedure InterceptInvalidInstruction;
  123. begin
  124. longjmp(test_fpu_jmpbuf, 1);
  125. end;
  126. { Use msdos int21 set/get Interrupt address
  127. to check if coprocessor is present }
  128. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  129. Procedure SysInitFPU;
  130. const
  131. CR0_NE = $20;
  132. CR0_NOT_NE = $FFFF - CR0_NE;
  133. var
  134. prevInt06 : FarPointer;
  135. _newcr0_lw : word;
  136. restore_old_int10 : boolean;
  137. begin
  138. restore_old_int10:=false;
  139. asm
  140. fninit
  141. fldcw Default8087CW
  142. fwait
  143. end;
  144. if Test8087 < 3 then { i8087/i80287 do not have "native" exception mode (CR0:NE) }
  145. begin
  146. restore_old_int10:=true;
  147. end
  148. else
  149. begin
  150. asm
  151. push es
  152. push ds
  153. { Get previous interrupt 06 handler }
  154. mov ax, $3506
  155. int $21
  156. mov word [prevInt06],bx
  157. mov dx,es
  158. mov word [prevInt06+2],dx
  159. { Install local interrupt 06 handler }
  160. {$ifdef FPC_MM_TINY}
  161. { Do not use SEG here, as this introduces a relocation that
  162. is incompatible with COM executable generation }
  163. mov dx, cs
  164. {$else FPC_MM_TINY}
  165. mov dx, SEG InterceptInvalidInstruction
  166. {$endif FPC_MM_TINY}
  167. mov ds, dx
  168. mov dx, Offset InterceptInvalidInstruction
  169. mov ax, $2506
  170. int $21
  171. pop ds
  172. pop es
  173. end;
  174. if setjmp(test_fpu_jmpbuf)=0 then
  175. begin
  176. asm
  177. db $0f, $20, $c0 { mov eax,cr0 }
  178. { Reset CR0 Numeric Error bit,
  179. to trigger IRQ13 - interrupt $75,
  180. and thus avoid the generation of a $10 trap
  181. which iterferes with video interrupt handler }
  182. and ax,CR0_NOT_NE
  183. db $0f, $22, $c0 { mov cr0,eax }
  184. end;
  185. //writeln(stderr,'Change of cr0 succeeded');
  186. // Test that NE bit is indeed reset
  187. asm
  188. db $0f, $20, $c0 { mov eax,cr0 }
  189. mov _newcr0_lw, ax
  190. end;
  191. if (_newcr0_lw and CR0_NE) = 0 then
  192. restore_old_int10:=true;
  193. end
  194. else
  195. begin
  196. //writeln(stderr,'Change of cr0 failed');
  197. end;
  198. { Restore previous interrupt 06 handler }
  199. asm
  200. push ds
  201. mov ax, $2506
  202. lds dx,[prevInt06]
  203. int $21
  204. pop ds
  205. end;
  206. end;
  207. { Special handler of interrupt $10
  208. not needed anymore
  209. Restore previous interrupt $10 handler }
  210. {$ifndef TEST_FPU_INT10}
  211. if restore_old_int10 then
  212. asm
  213. push ds
  214. mov ax, $2510
  215. lds dx,[SaveInt10]
  216. int $21
  217. pop ds
  218. end;
  219. {$endif ndef TEST_FPU_INT10}
  220. end;
  221. {$I system.inc}
  222. {$I tinyheap.inc}
  223. {$I ports.inc}
  224. procedure DebugWrite(const S: string);
  225. begin
  226. asm
  227. {$if defined(FPC_X86_DATA_FAR) or defined(FPC_X86_DATA_HUGE)}
  228. push ds
  229. lds si, S
  230. {$else}
  231. mov si, S
  232. {$endif}
  233. {$ifdef FPC_ENABLED_CLD}
  234. cld
  235. {$endif FPC_ENABLED_CLD}
  236. lodsb
  237. mov cl, al
  238. xor ch, ch
  239. jcxz @@zero_length
  240. mov ah, 2
  241. @@1:
  242. lodsb
  243. mov dl, al
  244. int 21h
  245. loop @@1
  246. @@zero_length:
  247. {$if defined(FPC_X86_DATA_FAR) or defined(FPC_X86_DATA_HUGE)}
  248. pop ds
  249. {$endif}
  250. end ['ax','bx','cx','dx','si','di'];
  251. end;
  252. procedure DebugWriteLn(const S: string);
  253. begin
  254. DebugWrite(S);
  255. DebugWrite(#13#10);
  256. end;
  257. {*****************************************************************************
  258. ParamStr/Randomize
  259. *****************************************************************************}
  260. var
  261. internal_envp : PPFarChar = nil;
  262. procedure setup_environment;
  263. var
  264. env_count : smallint;
  265. cp, dos_env: PFarChar;
  266. begin
  267. env_count:=0;
  268. dos_env:=Ptr(MemW[PrefixSeg:$2C], 0);
  269. cp:=dos_env;
  270. while cp^<>#0 do
  271. begin
  272. inc(env_count);
  273. while (cp^ <> #0) do
  274. inc(cp); { skip to NUL }
  275. inc(cp); { skip to next character }
  276. end;
  277. internal_envp := getmem((env_count+1) * sizeof(PFarChar));
  278. cp:=dos_env;
  279. env_count:=0;
  280. while cp^<>#0 do
  281. begin
  282. internal_envp[env_count] := cp;
  283. inc(env_count);
  284. while (cp^ <> #0) do
  285. inc(cp); { skip to NUL }
  286. inc(cp); { skip to next character }
  287. end;
  288. internal_envp[env_count]:=nil;
  289. dos_env_count := env_count;
  290. if dos_version >= $300 then
  291. begin
  292. if cp=dos_env then
  293. inc(cp);
  294. inc(cp, 3);
  295. dos_argv0 := cp;
  296. end
  297. else
  298. dos_argv0 := nil;
  299. end;
  300. function envp:PPFarChar;public name '__fpc_envp';
  301. begin
  302. if not assigned(internal_envp) then
  303. setup_environment;
  304. envp:=internal_envp;
  305. end;
  306. procedure setup_arguments;
  307. var
  308. I: SmallInt;
  309. pc: PChar;
  310. pfc: PFarChar;
  311. quote: Char;
  312. count: SmallInt;
  313. arglen, argv0len: SmallInt;
  314. argblock: PChar;
  315. arg: PChar;
  316. doscmd : string[129]; { Dos commandline copied from PSP, max is 128 chars +1 for terminating zero }
  317. begin
  318. { force environment to be setup so dos_argv0 is loaded }
  319. envp;
  320. { load commandline from psp }
  321. SetLength(doscmd, Mem[PrefixSeg:$80]);
  322. for I := 1 to length(doscmd) do
  323. doscmd[I] := Chr(Mem[PrefixSeg:$80+I]);
  324. doscmd[length(doscmd)+1]:=#0;
  325. {$IfDef SYSTEM_DEBUG_STARTUP}
  326. Writeln(stderr,'Dos command line is #',doscmd,'# size = ',length(doscmd));
  327. {$EndIf }
  328. { find argv0len }
  329. argv0len:=0;
  330. if dos_argv0<>nil then
  331. begin
  332. pfc:=dos_argv0;
  333. while pfc^<>#0 do
  334. begin
  335. Inc(argv0len);
  336. Inc(pfc);
  337. end;
  338. end;
  339. { parse dos commandline }
  340. pc:=@doscmd[1];
  341. count:=1;
  342. { calc total arguments length and count }
  343. arglen:=argv0len+1;
  344. while pc^<>#0 do
  345. begin
  346. { skip leading spaces }
  347. while pc^ in [#1..#32] do
  348. inc(pc);
  349. if pc^=#0 then
  350. break;
  351. { calc argument length }
  352. quote:=' ';
  353. while (pc^<>#0) do
  354. begin
  355. case pc^ of
  356. #1..#32 :
  357. begin
  358. if quote<>' ' then
  359. inc(arglen)
  360. else
  361. break;
  362. end;
  363. '"' :
  364. begin
  365. if quote<>'''' then
  366. begin
  367. if pchar(pc+1)^<>'"' then
  368. begin
  369. if quote='"' then
  370. quote:=' '
  371. else
  372. quote:='"';
  373. end
  374. else
  375. inc(pc);
  376. end
  377. else
  378. inc(arglen);
  379. end;
  380. '''' :
  381. begin
  382. if quote<>'"' then
  383. begin
  384. if pchar(pc+1)^<>'''' then
  385. begin
  386. if quote='''' then
  387. quote:=' '
  388. else
  389. quote:='''';
  390. end
  391. else
  392. inc(pc);
  393. end
  394. else
  395. inc(arglen);
  396. end;
  397. else
  398. inc(arglen);
  399. end;
  400. inc(pc);
  401. end;
  402. inc(arglen); { for the null terminator }
  403. inc(count);
  404. end;
  405. { set argc and allocate argv }
  406. argc:=count;
  407. argv:=AllocMem((count+1)*SizeOf(PChar));
  408. { allocate a single memory block for all arguments }
  409. argblock:=GetMem(arglen);
  410. { create argv[0] }
  411. argv[0]:=argblock;
  412. arg:=argblock;
  413. if dos_argv0<>nil then
  414. begin
  415. pfc:=dos_argv0;
  416. while pfc^<>#0 do
  417. begin
  418. arg^:=pfc^;
  419. Inc(arg);
  420. Inc(pfc);
  421. end;
  422. end;
  423. arg^:=#0;
  424. Inc(arg);
  425. pc:=@doscmd[1];
  426. count:=1;
  427. while pc^<>#0 do
  428. begin
  429. { skip leading spaces }
  430. while pc^ in [#1..#32] do
  431. inc(pc);
  432. if pc^=#0 then
  433. break;
  434. { copy argument }
  435. argv[count]:=arg;
  436. quote:=' ';
  437. while (pc^<>#0) do
  438. begin
  439. case pc^ of
  440. #1..#32 :
  441. begin
  442. if quote<>' ' then
  443. begin
  444. arg^:=pc^;
  445. inc(arg);
  446. end
  447. else
  448. break;
  449. end;
  450. '"' :
  451. begin
  452. if quote<>'''' then
  453. begin
  454. if pchar(pc+1)^<>'"' then
  455. begin
  456. if quote='"' then
  457. quote:=' '
  458. else
  459. quote:='"';
  460. end
  461. else
  462. inc(pc);
  463. end
  464. else
  465. begin
  466. arg^:=pc^;
  467. inc(arg);
  468. end;
  469. end;
  470. '''' :
  471. begin
  472. if quote<>'"' then
  473. begin
  474. if pchar(pc+1)^<>'''' then
  475. begin
  476. if quote='''' then
  477. quote:=' '
  478. else
  479. quote:='''';
  480. end
  481. else
  482. inc(pc);
  483. end
  484. else
  485. begin
  486. arg^:=pc^;
  487. inc(arg);
  488. end;
  489. end;
  490. else
  491. begin
  492. arg^:=pc^;
  493. inc(arg);
  494. end;
  495. end;
  496. inc(pc);
  497. end;
  498. arg^:=#0;
  499. Inc(arg);
  500. {$IfDef SYSTEM_DEBUG_STARTUP}
  501. Writeln(stderr,'dos arg ',count,' #',strlen(argv[count]),'#',argv[count],'#');
  502. {$EndIf SYSTEM_DEBUG_STARTUP}
  503. inc(count);
  504. end;
  505. end;
  506. function paramcount : longint;
  507. begin
  508. if argv=nil then
  509. setup_arguments;
  510. paramcount := argc - 1;
  511. end;
  512. function paramstr(l : longint) : string;
  513. begin
  514. if argv=nil then
  515. setup_arguments;
  516. if (l>=0) and (l+1<=argc) then
  517. paramstr:=strpas(argv[l])
  518. else
  519. paramstr:='';
  520. end;
  521. procedure randomize;
  522. var
  523. hl : longint;
  524. regs : Registers;
  525. begin
  526. regs.AH:=$2C;
  527. MsDos(regs);
  528. hl:=regs.DX;
  529. randseed:=hl*$10000+ regs.CX;
  530. end;
  531. {*****************************************************************************
  532. System Dependent Exit code
  533. *****************************************************************************}
  534. procedure system_exit;
  535. var
  536. h : byte;
  537. begin
  538. RestoreInterruptHandlers;
  539. for h:=0 to max_files-1 do
  540. if openfiles[h] then
  541. begin
  542. {$ifdef SYSTEMDEBUG}
  543. writeln(stderr,'file ',h,' "',opennames[h],'" not closed at exit');
  544. {$endif SYSTEMDEBUG}
  545. if h>=5 then
  546. do_close(h);
  547. end;
  548. {$ifndef FPC_MM_TINY}
  549. if not CheckNullArea then
  550. writeln(stderr, 'Nil pointer assignment');
  551. {$endif FPC_MM_TINY}
  552. asm
  553. mov al, byte [exitcode]
  554. mov ah, 4Ch
  555. int 21h
  556. end;
  557. end;
  558. {*****************************************************************************
  559. SystemUnit Initialization
  560. *****************************************************************************}
  561. procedure InitDosHeap;
  562. type
  563. {$if defined(FPC_X86_DATA_FAR) or defined(FPC_X86_DATA_HUGE)}
  564. TPointerArithmeticType = HugePointer;
  565. {$else}
  566. TPointerArithmeticType = Pointer;
  567. {$endif}
  568. begin
  569. RegisterTinyHeapBlock_Simple_Prealigned(__nearheap_start, TPointerArithmeticType(__nearheap_end) - TPointerArithmeticType(__nearheap_start));
  570. end;
  571. function CheckLFN:boolean;
  572. var
  573. regs : Registers;
  574. RootName : pchar;
  575. buf : array [0..31] of char;
  576. begin
  577. { Check LFN API on drive c:\ }
  578. RootName:='C:\';
  579. { Call 'Get Volume Information' ($71A0) }
  580. regs.AX:=$71a0;
  581. regs.ES:=Seg(buf);
  582. regs.DI:=Ofs(buf);
  583. regs.CX:=32;
  584. regs.DS:=Seg(RootName^);
  585. regs.DX:=Ofs(RootName^);
  586. MsDos_Carry(regs);
  587. { If carryflag=0 and LFN API bit in ebx is set then use Long file names }
  588. CheckLFN:=(regs.Flags and fCarry=0) and (regs.BX and $4000=$4000);
  589. end;
  590. procedure SysInitStdIO;
  591. begin
  592. OpenStdIO(Input,fmInput,StdInputHandle);
  593. OpenStdIO(Output,fmOutput,StdOutputHandle);
  594. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  595. {$ifndef FPC_STDOUT_TRUE_ALIAS}
  596. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  597. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  598. {$endif FPC_STDOUT_TRUE_ALIAS}
  599. end;
  600. function GetProcessID: SizeUInt;
  601. begin
  602. GetProcessID := PrefixSeg;
  603. end;
  604. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  605. begin
  606. result := stklen;
  607. end;
  608. begin
  609. StackBottom := __stkbottom;
  610. StackLength := __stktop - __stkbottom;
  611. InstallInterruptHandlers;
  612. DetectFPU;
  613. if Test8087>0 then
  614. SysInitFPU;
  615. { To be set if this is a GUI or console application }
  616. IsConsole := TRUE;
  617. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  618. { If dynlibs feature is disabled,
  619. IsLibrary is a constant, which can thus not be set to a value }
  620. { To be set if this is a library and not a program }
  621. IsLibrary := FALSE;
  622. {$endif def FPC_HAS_FEATURE_DYNLIBS}
  623. { Setup heap }
  624. InitDosHeap;
  625. SysInitExceptions;
  626. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  627. initunicodestringmanager;
  628. {$endif def FPC_HAS_FEATURE_UNICODESTRINGS}
  629. { Setup stdin, stdout and stderr }
  630. SysInitStdIO;
  631. { Setup environment and arguments }
  632. { Done on request only Setup_Environment; }
  633. { Done on request only Setup_Arguments; }
  634. {$ifndef RTLLITE}
  635. { Use LFNSupport LFN }
  636. LFNSupport:=CheckLFN;
  637. if LFNSupport then
  638. begin
  639. FileNameCasePreserving:=true;
  640. AllFilesMask := '*';
  641. end
  642. else
  643. {$endif ndef RTLLITE}
  644. AllFilesMask := '*.*';
  645. { Reset IO Error }
  646. InOutRes:=0;
  647. {$ifdef FPC_HAS_FEATURE_THREADING}
  648. InitSystemThreads;
  649. {$endif}
  650. end.