File: simlib.pas

package info (click to toggle)
fpc 0.99.13-19991013-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 23,104 kB
  • ctags: 9,760
  • sloc: pascal: 253,711; ansic: 5,236; makefile: 3,855; yacc: 2,016; lex: 707; asm: 526; xml: 443; sh: 200; perl: 87; sed: 21; csh: 12; cpp: 1
file content (237 lines) | stat: -rw-r--r-- 6,656 bytes parent folder | download
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
{
    $Id: simlib.pas,v 1.1 1999/06/14 11:49:48 florian Exp $
    This file is part of the Free Pascal simulator environment
    Copyright (c) 1999 by Florian Klaempfl

    This unit implemements routines for data types which aren't
    support by commonly used compilers

    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.

 **********************************************************************}
{$N+}
{ we do some strange things here }
{$O-}
{$R-}
unit simlib;

  interface

    uses
       simbase;

    procedure byte_zap(q : qword;b : byte;var result : qword);

    { shifts q b bytes left }
    procedure shift_left_q(q : qword;b : byte;var result : qword);

    { shifts q b bytes right }
    procedure shift_right_q(q : qword;b : byte;var result : qword);

    { returns true if i1<i2 assuming that c1 and c2 are unsigned !}
    function ltu(c1,c2 : comp) : boolean;

    { returns true if i1=<i2 assuming that c1 and c2 are unsigned !}
    function leu(c1,c2 : comp) : boolean;

    { adds to owords, returns true if an overflow occurs }
    function addoword(o1,o2 : oword;var result : oword) : boolean;

    { adds two words, returns true if an overflow occurs }
    function addword(w1,w2 : word;var result : word) : boolean;

    { sets an oword to zero }
    procedure zerooword(var o : oword);

    { multiplies two qwords into a full oword }
    procedure mulqword(q1,q2 : qword;var result : oword);

  implementation

    procedure byte_zap(q : qword;b : byte;var result : qword);

      var
         i : tindex;

      begin
         for i:=0 to 7 do
           if ((1 shl i) and b)=0 then
             tqwordrec(result).bytes[i]:=tqwordrec(q).bytes[i]
           else
             tqwordrec(result).bytes[i]:=0;
      end;

    { shifts q b bytes left }
    procedure shift_left_q(q : qword;b : byte;var result : qword);

      var
         i : tindex;

      begin
         result:=0;
         if b>63 then
         else if b>31 then
           tqwordrec(result).high32:=tqwordrec(q).low32 shl (b-32)
         else
           begin
              { bad solution! A qword shift would be nice! }
              result:=q;
              for i:=1 to b do
                begin
                   tqwordrec(result).high32:=tqwordrec(result).high32 shl 1;
                   if (tqwordrec(result).low32 and $80000000)<>0 then
                     tqwordrec(result).high32:=tqwordrec(result).high32 or 1;
                   tqwordrec(result).low32:=tqwordrec(result).low32 shl 1;
                end;
           end;
      end;

    { shifts q b bytes right }
    procedure shift_right_q(q : qword;b : byte;var result : qword);

      var
         i : tindex;

      begin
         result:=0;
         if b>63 then
         else if b>31 then
           tqwordrec(result).low32:=tqwordrec(q).high32 shr (b-32)
         else
           begin
              { bad solution! A qword shift would be nice! }
              result:=q;
              for i:=1 to b do
                begin
                   tqwordrec(result).low32:=tqwordrec(result).low32 shr 1;
                   if (tqwordrec(result).high32 and 1)<>0 then
                     tqwordrec(result).low32:=tqwordrec(result).low32 or
                       $80000000;
                   tqwordrec(result).high32:=tqwordrec(result).high32 shr 1;
                end;
           end;
      end;

    { returns true if i1<i2 assuming that c1 and c2 are unsigned !}
    function ltu(c1,c2 : comp) : boolean;

      begin
         if (c1>=0) and (c2>=0) then
           ltu:=c1<c2
         else if (c1<0) and (c2>=0) then
           ltu:=false
         else if (c1>=0) and (c2<0) then
           ltu:=true
         else
           ltu:=c1<c2
      end;

    { returns true if i1=<i2 assuming that c1 and c2 are unsigned !}
    function leu(c1,c2 : comp) : boolean;

      begin
         if (c1>=0) and (c2>=0) then
           leu:=c1<=c2
         else if (c1<0) and (c2>=0) then
           leu:=false
         else if (c1>=0) and (c2<0) then
           leu:=true
         else
           leu:=c1<=c2
      end;

    { "ands" two qwords }
    procedure andqword(w1,w2 : qword;var result : qword);

      begin
         tqwordrec(result).low32:=tqwordrec(w1).low32 and tqwordrec(w2).low32;
         tqwordrec(result).high32:=tqwordrec(w1).high32 and tqwordrec(w2).high32;
      end;

    { adds two words, returns true if an overflow occurs }
    function addword(w1,w2 : word;var result : word) : boolean;

      var
         l : longint;

      begin
         l:=w1+w2;
         addword:=(l and $10000)<>0;
         result:=l and $ffff;
      end;

    { adds two owords, returns true if an overflow occurs }
    function addoword(o1,o2 : oword;var result : oword) : boolean;

      var
         i : tindex;
         carry : word;

      begin
         carry:=0;
         for i:=0 to 7 do
           begin
              result[i]:=o1[i]+o2[i]+carry;
              { an overflow has occured, if the result is less
                than one of the summands
              }
              if (result[i]<o1[i]) or (result[i]<o2[i]) then
                carry:=1
              else
                carry:=0;
           end;
         addoword:=carry=1;
      end;

    { sets an oword to zero }
    procedure zerooword(var o : oword);

      begin
         fillchar(o,sizeof(o),0);
      end;

    { multiplies two qwords into a full oword }
    procedure mulqword(q1,q2 : qword;var result : oword);

      var
         i : tindex;
         h,bitpos : qword;
         ho1 : oword;

      begin
         { result is zero }
         zerooword(ho1);
         result:=ho1;
         towordrec(ho1).low64:=q1;

         bitpos:=1;

         for i:=0 to 63 do
           begin
              andqword(q2,bitpos,h);
              if h<>0 then
                addoword(result,ho1,result);

              { ho1:=2*ho1 }
              addoword(ho1,ho1,ho1);
              shift_left_q(bitpos,1,bitpos);
           end;
      end;

end.
{
  $Log: simlib.pas,v $
  Revision 1.1  1999/06/14 11:49:48  florian
    + initial revision, it runs simple Alpha Linux ELF executables
       - integer operations are nearly completed (non with overflow checking)
       - floating point operations aren't implemented (except loading and
         storing)
       - only the really necessary system calls are implemented by dummys
         write syscalls are redirected to the console

}