Subversion Repositories Kolibri OS

Rev

Rev 6647 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7597 akron1 1
(*
2
    Copyright 2016, 2018 Anton Krotov
6613 leency 3
 
4
    This program is free software: you can redistribute it and/or modify
5
    it under the terms of the GNU Lesser General Public License as published by
6
    the Free Software Foundation, either version 3 of the License, or
7
    (at your option) any later version.
8
 
9
    This program is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU Lesser General Public License for more details.
13
 
14
    You should have received a copy of the GNU Lesser General Public License
15
    along with this program.  If not, see .
16
*)
17
 
18
MODULE Debug;
19
 
20
IMPORT KOSAPI, sys := SYSTEM;
21
 
22
CONST
23
 
7597 akron1 24
  d = 1.0 - 5.0E-12;
6613 leency 25
 
26
VAR
27
 
7597 akron1 28
  Realp: PROCEDURE (x: REAL; width: INTEGER);
6613 leency 29
 
30
PROCEDURE Char*(c: CHAR);
31
VAR res: INTEGER;
32
BEGIN
33
  res := KOSAPI.sysfunc3(63, 1, ORD(c))
34
END Char;
35
 
36
PROCEDURE String*(s: ARRAY OF CHAR);
37
VAR n, i: INTEGER;
38
BEGIN
39
  n := LENGTH(s);
40
  FOR i := 0 TO n - 1 DO
41
    Char(s[i])
42
  END
43
END String;
44
 
45
PROCEDURE WriteInt(x, n: INTEGER);
46
VAR i: INTEGER; a: ARRAY 16 OF CHAR; neg: BOOLEAN;
47
BEGIN
48
  i := 0;
49
  IF n < 1 THEN
50
    n := 1
51
  END;
52
  IF x < 0 THEN
53
    x := -x;
54
    DEC(n);
55
    neg := TRUE
56
  END;
57
  REPEAT
58
    a[i] := CHR(x MOD 10 + ORD("0"));
59
    x := x DIV 10;
60
    INC(i)
61
  UNTIL x = 0;
62
  WHILE n > i DO
63
    Char(" ");
64
    DEC(n)
65
  END;
66
  IF neg THEN
67
    Char("-")
68
  END;
69
  REPEAT
70
    DEC(i);
71
    Char(a[i])
72
  UNTIL i = 0
73
END WriteInt;
74
 
7597 akron1 75
PROCEDURE IsNan(AValue: REAL): BOOLEAN;
6613 leency 76
VAR h, l: SET;
77
BEGIN
78
  sys.GET(sys.ADR(AValue), l);
79
  sys.GET(sys.ADR(AValue) + 4, h)
80
  RETURN (h * {20..30} = {20..30}) & ((h * {0..19} # {}) OR (l * {0..31} # {}))
81
END IsNan;
82
 
7597 akron1 83
PROCEDURE IsInf(x: REAL): BOOLEAN;
84
  RETURN ABS(x) = sys.INF()
6613 leency 85
END IsInf;
86
 
87
PROCEDURE Int*(x, width: INTEGER);
88
VAR i: INTEGER;
89
BEGIN
90
  IF x # 80000000H THEN
91
    WriteInt(x, width)
92
  ELSE
93
    FOR i := 12 TO width DO
94
      Char(20X)
95
    END;
96
    String("-2147483648")
97
  END
98
END Int;
99
 
7597 akron1 100
PROCEDURE OutInf(x: REAL; width: INTEGER);
101
VAR s: ARRAY 5 OF CHAR; i: INTEGER;
6613 leency 102
BEGIN
103
  IF IsNan(x) THEN
104
    s := "Nan";
105
    INC(width)
7597 akron1 106
  ELSIF IsInf(x) & (x > 0.0) THEN
6613 leency 107
    s := "+Inf"
7597 akron1 108
  ELSIF IsInf(x) & (x < 0.0) THEN
6613 leency 109
    s := "-Inf"
110
  END;
111
  FOR i := 1 TO width - 4 DO
112
    Char(" ")
113
  END;
114
  String(s)
115
END OutInf;
116
 
117
PROCEDURE Ln*;
118
BEGIN
119
  Char(0DX);
120
  Char(0AX)
121
END Ln;
122
 
7597 akron1 123
PROCEDURE _FixReal(x: REAL; width, p: INTEGER);
124
VAR e, len, i: INTEGER; y: REAL; minus: BOOLEAN;
6613 leency 125
BEGIN
126
  IF IsNan(x) OR IsInf(x) THEN
127
    OutInf(x, width)
128
  ELSIF p < 0 THEN
129
    Realp(x, width)
130
  ELSE
131
    len := 0;
132
    minus := FALSE;
7597 akron1 133
    IF x < 0.0 THEN
6613 leency 134
      minus := TRUE;
135
      INC(len);
136
      x := ABS(x)
137
    END;
138
    e := 0;
7597 akron1 139
    WHILE x >= 10.0 DO
140
      x := x / 10.0;
6613 leency 141
      INC(e)
142
    END;
143
    IF e >= 0 THEN
144
      len := len + e + p + 1;
7597 akron1 145
      IF x > 9.0 + d THEN
146
        INC(len)
6613 leency 147
      END;
148
      IF p > 0 THEN
7597 akron1 149
        INC(len)
6613 leency 150
      END
151
    ELSE
152
      len := len + p + 2
153
    END;
154
    FOR i := 1 TO width - len DO
155
      Char(" ")
156
    END;
157
    IF minus THEN
158
      Char("-")
159
    END;
160
    y := x;
7597 akron1 161
    WHILE (y < 1.0) & (y # 0.0) DO
162
      y := y * 10.0;
6613 leency 163
      DEC(e)
164
    END;
165
    IF e < 0 THEN
7597 akron1 166
      IF x - FLT(FLOOR(x)) > d THEN
167
        Char("1");
168
        x := 0.0
6613 leency 169
      ELSE
7597 akron1 170
        Char("0");
171
        x := x * 10.0
6613 leency 172
      END
173
    ELSE
174
      WHILE e >= 0 DO
7597 akron1 175
        IF x - FLT(FLOOR(x)) > d THEN
176
          IF x > 9.0 THEN
177
            String("10")
178
          ELSE
179
            Char(CHR(FLOOR(x) + ORD("0") + 1))
180
          END;
181
          x := 0.0
182
        ELSE
183
          Char(CHR(FLOOR(x) + ORD("0")));
184
          x := (x - FLT(FLOOR(x))) * 10.0
185
        END;
186
        DEC(e)
6613 leency 187
      END
188
    END;
189
    IF p > 0 THEN
190
      Char(".")
191
    END;
192
    WHILE p > 0 DO
7597 akron1 193
      IF x - FLT(FLOOR(x)) > d THEN
194
        Char(CHR(FLOOR(x) + ORD("0") + 1));
195
        x := 0.0
6613 leency 196
      ELSE
7597 akron1 197
        Char(CHR(FLOOR(x) + ORD("0")));
198
        x := (x - FLT(FLOOR(x))) * 10.0
6613 leency 199
      END;
200
      DEC(p)
201
    END
202
  END
6647 akron1 203
END _FixReal;
6613 leency 204
 
7597 akron1 205
PROCEDURE Real*(x: REAL; width: INTEGER);
6613 leency 206
VAR e, n, i: INTEGER; minus: BOOLEAN;
207
BEGIN
208
  IF IsNan(x) OR IsInf(x) THEN
209
    OutInf(x, width)
210
  ELSE
211
    e := 0;
212
    n := 0;
213
    IF width > 23 THEN
214
      n := width - 23;
215
      width := 23
216
    ELSIF width < 9 THEN
217
      width := 9
218
    END;
219
    width := width - 5;
7597 akron1 220
    IF x < 0.0 THEN
6613 leency 221
      x := -x;
222
      minus := TRUE
223
    ELSE
224
      minus := FALSE
225
    END;
7597 akron1 226
    WHILE x >= 10.0 DO
227
      x := x / 10.0;
6613 leency 228
      INC(e)
229
    END;
7597 akron1 230
    WHILE (x < 1.0) & (x # 0.0) DO
231
      x := x * 10.0;
6613 leency 232
      DEC(e)
233
    END;
7597 akron1 234
    IF x > 9.0 + d THEN
235
      x := 1.0;
6613 leency 236
      INC(e)
237
    END;
238
    FOR i := 1 TO n DO
239
      Char(" ")
240
    END;
241
    IF minus THEN
242
      x := -x
243
    END;
6647 akron1 244
    Realp := Real;
245
    _FixReal(x, width, width - 3);
6613 leency 246
    Char("E");
247
    IF e >= 0 THEN
248
      Char("+")
249
    ELSE
250
      Char("-");
251
      e := ABS(e)
252
    END;
253
    IF e < 100 THEN
254
      Char("0")
255
    END;
256
    IF e < 10 THEN
257
      Char("0")
258
    END;
259
    Int(e, 0)
260
  END
261
END Real;
262
 
7597 akron1 263
PROCEDURE FixReal*(x: REAL; width, p: INTEGER);
6647 akron1 264
BEGIN
265
  Realp := Real;
266
  _FixReal(x, width, p)
267
END FixReal;
268
 
6613 leency 269
PROCEDURE Open*;
270
TYPE
271
 
272
  info_struct = RECORD
273
    subfunc: INTEGER;
274
    flags:   INTEGER;
275
    param:   INTEGER;
276
    rsrvd1:  INTEGER;
277
    rsrvd2:  INTEGER;
278
    fname:   ARRAY 1024 OF CHAR
279
  END;
280
 
281
VAR info: info_struct; res: INTEGER;
282
BEGIN
283
  info.subfunc := 7;
284
  info.flags := 0;
7597 akron1 285
  info.param := sys.SADR(" ");
6613 leency 286
  info.rsrvd1 := 0;
287
  info.rsrvd2 := 0;
288
  info.fname := "/rd/1/develop/board";
289
  res := KOSAPI.sysfunc2(70, sys.ADR(info))
290
END Open;
291
 
6647 akron1 292
END Debug.