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