summaryrefslogtreecommitdiff
path: root/rtl/sinclairql/system.pp
blob: e168c2e61427a9302f258c81e86b8108a7cf2a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2020 by Karoly Balogh

    System unit for the Sinclair QL

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit System;

interface

{$define FPC_IS_SYSTEM}
{$define FPC_STDOUT_TRUE_ALIAS}
{$define FPC_ANSI_TEXTFILEREC}
{$define FPC_QL_USE_OSHEAP}

{$ifdef FPC_QL_USE_OSHEAP}
{$define HAS_MEMORYMANAGER}
{$endif FPC_QL_USE_OSHEAP}

{$i systemh.inc}

{Platform specific information}
const
    LineEnding = #10;
    LFNSupport = false;
    CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
    DirectorySeparator = '\';
    DriveSeparator = ':';
    ExtensionSeparator = '.';
    PathSeparator = ';';
    AllowDirectorySeparators : set of char = ['\','/'];
    AllowDriveSeparators : set of char = [':'];
    FileNameCaseSensitive = false;
    FileNameCasePreserving = false;
    maxExitCode = 255;
    MaxPathLen = 255;
    AllFilesMask = '*.*';

    sLineBreak = LineEnding;
    DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;

const
    UnusedHandle    = -1;
    StdInputHandle: longint = UnusedHandle;
    StdOutputHandle: longint = UnusedHandle;
    StdErrorHandle: longint = UnusedHandle;

var
    QL_ChannelIDNum : word;
    QL_ChannelIDs: pdword;
    QL_CommandLineLen : word;
    QL_CommandLine : pchar;

    argv: PPChar;
    argc: Longint;

    {$if defined(FPUSOFT)}

    {$define fpc_softfpu_interface}
    {$i softfpu.pp}
    {$undef fpc_softfpu_interface}

    {$endif defined(FPUSOFT)}

function SetQLJobName(const s: string): longint;
function GetQLJobName: string;
function GetQLJobNamePtr: pointer;


implementation

  {$define FPC_SYSTEM_HAS_STACKTOP}
  {$define FPC_SYSTEM_HAS_BACKTRACESTR}

  {$if defined(FPUSOFT)}

  {$define fpc_softfpu_implementation}
  {$define softfpu_compiler_mul32to64}
  {$define softfpu_inline}
  {$i softfpu.pp}
  {$undef fpc_softfpu_implementation}

  { we get these functions and types from the softfpu code }
  {$define FPC_SYSTEM_HAS_float64}
  {$define FPC_SYSTEM_HAS_float32}
  {$define FPC_SYSTEM_HAS_flag}
  {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  {$define FPC_SYSTEM_HAS_extractFloat32Sign}

  {$endif defined(FPUSOFT)}

  {$i system.inc}
  {$ifdef FPC_QL_USE_OSHEAP}
  {$i osheap.inc}
  {$endif FPC_QL_USE_OSHEAP}


function GetProcessID:SizeUInt;
begin
  GetProcessID := mt_inf(nil, nil);
end;

{*****************************************************************************
                             ParamStr
*****************************************************************************}

var
  args: PChar;

{ number of args }
function ParamCount: LongInt;
begin
  ParamCount:=argc;
end;

{ argument number l }
function ParamStr(l: LongInt): string;
begin
  if (l >= 0) and (l <= argc) then
    ParamStr:=argv[l]
  else
    ParamStr:='';
end;

procedure SysInitParamsAndEnv;
var
  i,j : longint;
  c : char;
  argv_size : longint;
const
  word_separators=[' ',#0];
begin
  argc:=0;
  argv:=nil;
  args:=GetMem(QL_CommandLineLen+1);
  if not assigned(args) then
    exit;

  Move(QL_CommandLine^,args^,QL_CommandLineLen);
  args[QL_CommandLineLen]:=#0;

  i:=0;
  c:=' ';
  while args[i]<>#0 do
    begin
      if (c in word_separators) and not (args[i] in word_separators) then
        inc(argc);
      c:=args[i];
      inc(i);
    end;

  { +2 is because argv[0] should be program name, 
    and argv[argc+1] is argv array terminator }
  argv:=GetMem((argc+2)*sizeof(pointer));
  if not assigned(argv) then
    begin
      argc:=0;
      exit;
    end;
  argv[argc+1]:=nil;
  { FIX ME: for now the 0th argument (program name) is just always empty }
  argv[0]:=#0;

  i:=0;
  j:=1;
  c:=' ';
  while args[i]<>#0 do
    begin
      if (c in word_separators) and not (args[i] in word_separators) then
        begin
          argv[j]:=@args[i];
          inc(j);
        end;
      c:=args[i];
      if (c in word_separators) then
        args[i]:=#0;
      inc(i);
    end;
end;

procedure randomize;
begin
  { Get the current date/time }
  randseed:=mt_rclck;
end;


{*****************************************************************************
                      Platform specific custom calls
*****************************************************************************}

procedure PrintStr(ch: longint; const s: shortstring);
begin
  io_sstrg(ch,-1,@s[1],ord(s[0]));
end;

procedure DebugStr(const s: shortstring); public name '_dbgstr';
var
  i: longint;
begin
  PrintStr(stdOutputHandle,s);
  for i:=0 to 10000 do begin end;
end;


var
  start_proc: byte; external name '_start'; 

  { WARNING! if you change this value, make sure there's enough
    buffer space for the job name in the startup code! }
const
  JOB_NAME_MAX_LEN = 48;

function SetQLJobName(const s: string): longint;
var
  len: longint;
begin
  SetQLJobName:=-1;
  if pword(@start_proc)[3] = $4afb then
    begin
      len:=length(s);
      if len > JOB_NAME_MAX_LEN then
        len:=JOB_NAME_MAX_LEN;
      Move(s[1],pword(@start_proc)[5],len);
      pword(@start_proc)[4]:=len;
      SetQLJobName:=len;
    end;
end;

function GetQLJobName: string;
var
  len: longint;
begin
  GetQLJobName:='';
  if pword(@start_proc)[3] = $4afb then
    begin
      len:=pword(@start_proc)[4];
      if len <= JOB_NAME_MAX_LEN then
        begin
          SetLength(GetQLJobName,len);
          Move(pword(@start_proc)[5],GetQLJobName[1],len);
        end;
    end;
end;

function GetQLJobNamePtr: pointer;
begin
  GetQLJobNamePtr:=nil;
  if pword(@start_proc)[3] = $4afb then
    begin
      GetQLJobNamePtr:=@pword(@start_proc)[4];
    end;
end;

{*****************************************************************************
                        System Dependent Entry code
*****************************************************************************}
var
  jobStackDataPtr: pointer; external name '__stackpointer_on_entry';
  program_name: shortstring; external name '__fpc_program_name';

{ QL/QDOS specific startup }
procedure SysInitQDOS;
var
  r: TQLRect;
begin
  QL_ChannelIDNum:=pword(jobStackDataPtr)[0];
  QL_ChannelIDs:=@pword(jobStackDataPtr)[1];
  QL_CommandLineLen:=pword(@QL_ChannelIDs[QL_ChannelIDNum])[0];
  QL_CommandLine:=@pword(@QL_ChannelIDs[QL_ChannelIDNum])[1];

  SetQLJobName(program_name);

  stdInputHandle:=io_open('con_',Q_OPEN);
  stdOutputHandle:=stdInputHandle;
  stdErrorHandle:=stdInputHandle;

  r.q_width:=512;
  r.q_height:=256;
  r.q_x:=0;
  r.q_y:=0;

  sd_wdef(stdInputHandle,-1,2,1,@r);
  sd_clear(stdInputHandle,-1);
end;

{*****************************************************************************
                         System Dependent Exit code
*****************************************************************************}

procedure haltproc(e:longint); external name '_haltproc';

procedure system_exit;
const
  anyKey: pchar = 'Press any key to exit';
begin
  if assigned(args) then
    FreeMem(args);
  if assigned(argv) then
    FreeMem(argv);

  io_sstrg(stdOutputHandle, -1, anyKey, length(anykey));
  io_fbyte(stdInputHandle, -1);

  stdInputHandle:=UnusedHandle;
  stdOutputHandle:=UnusedHandle;
  stdErrorHandle:=UnusedHandle;
  haltproc(exitcode);
end;

{*****************************************************************************
                         System Unit Initialization
*****************************************************************************}

procedure SysInitStdIO;
begin
  OpenStdIO(Input,fmInput,StdInputHandle);
  OpenStdIO(Output,fmOutput,StdOutputHandle);
  OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
{$ifndef FPC_STDOUT_TRUE_ALIAS}
  OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  OpenStdIO(StdErr,fmOutput,StdErrorHandle);
{$endif FPC_STDOUT_TRUE_ALIAS}
end;

function CheckInitialStkLen (StkLen: SizeUInt): SizeUInt;
begin
  CheckInitialStkLen := StkLen;
end;


begin
  StackLength := CheckInitialStkLen (InitialStkLen);
  StackBottom := StackTop - StackLength;
  StackMargin := min(align(StackLength div 20,2),STACK_MARGIN_MAX);
{ Initialize ExitProc }
  ExitProc:=Nil;
  SysInitQDOS;
{$ifndef FPC_QL_USE_OSHEAP}
{ Setup heap }
  InitHeap;
{$else FPC_QL_USE_OSHEAP}
//  InitOSHeap;
{$endif FPC_QL_USE_OSHEAP}
  SysInitExceptions;
{$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  InitUnicodeStringManager;
{$endif FPC_HAS_FEATURE_UNICODESTRINGS}
{ Setup stdin, stdout and stderr }
  SysInitStdIO;
{ Reset IO Error }
  InOutRes:=0;
{ Setup command line arguments }
  SysInitParamsAndEnv;
{$ifdef FPC_HAS_FEATURE_THREADING}
  InitSystemThreads;
{$endif FPC_HAS_FEATURE_THREADING}
end.