si_prc.pp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2020 by Karoly Balogh
  4. System Entry point for the Sinclair QL
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit si_prc;
  12. interface
  13. implementation
  14. {$i qdosfuncs.inc}
  15. var
  16. binstart: byte; external name '_stext';
  17. binend: byte; external name '_etext';
  18. bssstart: byte; external name '_sbss';
  19. bssend: byte; external name '_ebss';
  20. procedure PascalMain; external name 'PASCALMAIN';
  21. procedure PascalStart; forward;
  22. { this function must be the first in this unit which contains code }
  23. {$OPTIMIZATION OFF}
  24. function _FPC_proc_start: longint; cdecl; assembler; nostackframe; public name '_start';
  25. asm
  26. bra @start
  27. dc.l $0
  28. dc.w $4afb
  29. dc.w 3
  30. dc.l $46504300 { Job name, just FPC for now }
  31. @start:
  32. { relocation code }
  33. { get our actual position in RAM }
  34. lea.l binstart(pc),a0
  35. move.l a0,d0
  36. { get an offset to the end of the binary. this works both
  37. relocated and not. The decision if to relocate is done
  38. later then }
  39. lea.l binend,a1
  40. lea.l binstart,a0
  41. sub.l a0,a1
  42. add.l d0,a1
  43. move.l d0,a0
  44. { read the relocation marker, this is always two padding bytes
  45. ad the end of .text, so we're free to poke there }
  46. move.w -2(a1),d7
  47. beq @noreloc
  48. { zero out the relocation marker, so if our code is called again
  49. without reload, it won't relocate itself twice }
  50. move.w #0,-2(a1)
  51. { first item in the relocation table is the number of relocs }
  52. move.l (a1)+,d7
  53. beq @noreloc
  54. @relocloop:
  55. { we read the offsets and relocate them }
  56. move.l (a1)+,d1
  57. add.l d0,(a0,d1)
  58. subq.l #1,d7
  59. bne @relocloop
  60. @noreloc:
  61. jsr PascalStart
  62. end;
  63. procedure _FPC_proc_halt(_ExitCode: longint); public name '_haltproc';
  64. begin
  65. mt_frjob(-1, _ExitCode);
  66. end;
  67. procedure PascalStart;
  68. begin
  69. { initialize .bss }
  70. FillChar(bssstart,PtrUInt(@bssend)-PtrUInt(@bssstart),#0);
  71. PascalMain;
  72. Halt; { this should never be reached }
  73. end;
  74. end.