-
Notifications
You must be signed in to change notification settings - Fork 0
/
KOS.Kernel.Mod.txt
300 lines (268 loc) · 11.4 KB
/
KOS.Kernel.Mod.txt
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
MODULE Kernel; (*NW/PR 11.4.86 / 27.12.95 / 4.2.2014*)
IMPORT SYSTEM;
CONST SectorLength* = 1024; org = 64;
TYPE Sector* = ARRAY SectorLength OF BYTE;
VAR allocated*, NofSectors*: INTEGER;
heapOrg*, heapLim*: INTEGER;
stackOrg* , stackSize*, MemLim*: INTEGER;
clock, time, time0: INTEGER;
list0, list1, list2, list3: INTEGER; (*lists of free blocks of size n*256, 128, 64, 32 bytes*)
InitFPU: PROCEDURE;
(*Kolibri syscalls*)
Terminate: PROCEDURE; (*fn-1*)
PrintChar: PROCEDURE (ch: CHAR); (*fn63.1*)
SetMem: PROCEDURE (len: INTEGER): INTEGER; (*fn64.1*)
GetCounter: PROCEDURE (): INTEGER; (*fn26.9*)
GetDate: PROCEDURE (): INTEGER; (*fn29*)
GetTime: PROCEDURE (): INTEGER; (*fn3*)
SetRTC: PROCEDURE (fn, val: INTEGER): INTEGER; (*fn22*)
(* ---------- New: heap allocation ----------*)
PROCEDURE NewBlock(VAR p: LONGINT; len: LONGINT);
CONST N = 262144;
VAR adr, size, q: LONGINT;
BEGIN p := 0; adr := (heapLim + len + 256 + N - 1) DIV N * N;
IF adr > heapOrg THEN
IF SetMem(adr) = 0 THEN
p := heapLim; q := p + len; size := adr - heapLim;
SYSTEM.PUT(p, len); SYSTEM.PUT(p+4, -1);
SYSTEM.PUT(q, size-len); SYSTEM.PUT(q+4, -1); SYSTEM.PUT(q+8, list0); list0 := q;
heapLim := adr; MemLim := adr
END
END
END NewBlock;
PROCEDURE GetBlock(VAR p: LONGINT; len: LONGINT);
(*len is multiple of 256*)
VAR q0, q1, q2, size: LONGINT; done: BOOLEAN;
BEGIN q0 := 0; q1 := list0; done := FALSE;
WHILE ~done & (q1 # 0) DO
SYSTEM.GET(q1, size); SYSTEM.GET(q1+8, q2);
IF size < len THEN (*no fit*) q0 := q1; q1 := q2
ELSIF size = len THEN (*extract -> p*)
done := TRUE; p := q1;
IF q0 # 0 THEN SYSTEM.PUT(q0+8, q2) ELSE list0 := q2 END
ELSE (*reduce size*)
done := TRUE; p := q1; q1 := q1 + len;
SYSTEM.PUT(q1, size-len); SYSTEM.PUT(q1+4, -1); SYSTEM.PUT(q1+8, q2);
IF q0 # 0 THEN SYSTEM.PUT(q0+8, q1) ELSE list0 := q1 END
END
END ;
IF ~done THEN NewBlock(p, len) END
END GetBlock;
PROCEDURE GetBlock128(VAR p: LONGINT);
VAR q: LONGINT;
BEGIN
IF list1 # 0 THEN p := list1; SYSTEM.GET(list1+8, list1)
ELSE GetBlock(q, 256); SYSTEM.PUT(q+128, 128); SYSTEM.PUT(q+132, -1); SYSTEM.PUT(q+136, list1);
list1 := q + 128; p := q
END
END GetBlock128;
PROCEDURE GetBlock64(VAR p: LONGINT);
VAR q: LONGINT;
BEGIN
IF list2 # 0 THEN p := list2; SYSTEM.GET(list2+8, list2)
ELSE GetBlock128(q); SYSTEM.PUT(q+64, 64); SYSTEM.PUT(q+68, -1); SYSTEM.PUT(q+72, list2);
list2 := q + 64; p := q
END
END GetBlock64;
PROCEDURE GetBlock32(VAR p: LONGINT);
VAR q: LONGINT;
BEGIN
IF list3 # 0 THEN p := list3; SYSTEM.GET(list3+8, list3)
ELSE GetBlock64(q); SYSTEM.PUT(q+32, 32); SYSTEM.PUT(q+36, -1); SYSTEM.PUT(q+40, list3);
list3 := q + 32; p := q
END
END GetBlock32;
PROCEDURE New*(VAR ptr: LONGINT; tag: LONGINT);
(*called by NEW via MT[0]; ptr and tag are pointers*)
VAR p, size, lim: LONGINT;
BEGIN SYSTEM.GET(tag, size);
IF size = 32 THEN GetBlock32(p)
ELSIF size = 64 THEN GetBlock64(p)
ELSIF size = 128 THEN GetBlock128(p)
ELSE GetBlock(p, (size+255) DIV 256 * 256)
END ;
IF p = 0 THEN ptr := 0
ELSE ptr := p+8; SYSTEM.PUT(p, tag); lim := p + size; INC(p, 4); INC(allocated, size);
WHILE p < lim DO SYSTEM.PUT(p, 0); INC(p, 4) END
END
END New;
(* ---------- Garbage collector ----------*)
PROCEDURE Mark*(pref: LONGINT);
VAR pvadr, offadr, offset, tag, p, q, r: LONGINT;
BEGIN SYSTEM.GET(pref, pvadr); (*pointers < heapOrg considered NIL*)
WHILE pvadr # 0 DO
SYSTEM.GET(pvadr, p);
IF p >= heapOrg THEN SYSTEM.GET(p-4, offadr);
IF offadr = 0 THEN q := p; (*mark elements in data structure with root p*)
REPEAT SYSTEM.GET(p-4, offadr);
IF offadr = 0 THEN SYSTEM.GET(p-8, tag); offadr := tag + 16 ELSE INC(offadr, 4) END ;
SYSTEM.PUT(p-4, offadr); SYSTEM.GET(offadr, offset);
IF offset # -1 THEN (*down*)
SYSTEM.GET(p+offset, r);
IF r >= heapOrg THEN SYSTEM.GET(r-4, offadr);
IF offadr = 0 THEN SYSTEM.PUT(p+offset, q); q := p; p := r END
END
ELSE (*up*) SYSTEM.GET(q-4, offadr); SYSTEM.GET(offadr, offset);
IF p # q THEN SYSTEM.GET(q+offset, r); SYSTEM.PUT(q+offset, p); p := q; q := r END
END
UNTIL (p = q) & (offset = -1)
END
END ;
INC(pref, 4); SYSTEM.GET(pref, pvadr)
END
END Mark;
PROCEDURE Scan*;
VAR p, q, mark, tag, size: LONGINT;
BEGIN p := heapOrg;
REPEAT SYSTEM.GET(p+4, mark); q := p;
WHILE mark = 0 DO
SYSTEM.GET(p, tag); SYSTEM.GET(tag, size); INC(p, size); SYSTEM.GET(p+4, mark)
END ;
size := p - q; DEC(allocated, size); (*size of free block*)
IF size > 0 THEN
IF size MOD 64 # 0 THEN
SYSTEM.PUT(q, 32); SYSTEM.PUT(q+4, -1); SYSTEM.PUT(q+8, list3); list3 := q; INC(q, 32); DEC(size, 32)
END ;
IF size MOD 128 # 0 THEN
SYSTEM.PUT(q, 64); SYSTEM.PUT(q+4, -1); SYSTEM.PUT(q+8, list2); list2 := q; INC(q, 64); DEC(size, 64)
END ;
IF size MOD 256 # 0 THEN
SYSTEM.PUT(q, 128); SYSTEM.PUT(q+4, -1); SYSTEM.PUT(q+8, list1); list1 := q; INC(q, 128); DEC(size, 128)
END ;
IF size > 0 THEN
SYSTEM.PUT(q, size); SYSTEM.PUT(q+4, -1); SYSTEM.PUT(q+8, list0); list0 := q; INC(q, size)
END
END ;
IF mark > 0 THEN SYSTEM.GET(p, tag); SYSTEM.GET(tag, size); SYSTEM.PUT(p+4, 0); INC(p, size)
ELSE (*free*) SYSTEM.GET(p, size); INC(p, size)
END
UNTIL p >= heapLim
END Scan;
(* ---------- Disk storage management ----------*)
PROCEDURE InitSecMap*;
BEGIN NofSectors := 0
END InitSecMap;
PROCEDURE MarkSector*(sec: INTEGER);
BEGIN ASSERT(FALSE)
END MarkSector;
PROCEDURE FreeSector*(sec: INTEGER);
BEGIN ASSERT(FALSE)
END FreeSector;
PROCEDURE AllocSector*(hint: INTEGER; VAR sec: INTEGER);
BEGIN ASSERT(FALSE)
END AllocSector;
PROCEDURE GetSector*(src: INTEGER; VAR dst: Sector);
BEGIN ASSERT(FALSE)
END GetSector;
PROCEDURE PutSector*(dst: INTEGER; VAR src: Sector);
BEGIN ASSERT(FALSE)
END PutSector;
(*-------- Miscellaneous procedures----------*)
PROCEDURE DayOfWeek(year, month, day: INTEGER): INTEGER;
VAR tm: BYTE;
BEGIN
IF month < 3 THEN DEC(year) END;
SYSTEM.GET(SYSTEM.ADR($000302050003050104060204$) + month - 1, tm);
RETURN (year + year DIV 4 - year DIV 100 + year DIV 400 + tm + day) MOD 7 + 1
END DayOfWeek;
PROCEDURE Time*(): INTEGER;
BEGIN RETURN GetCounter() * 10 - time0
END Time;
PROCEDURE Clock*(): INTEGER;
VAR date, time, date1, second, minute, hour, day, month, year: INTEGER;
BEGIN date1 := GetDate(); (*avoid race condition at midnight*)
REPEAT date := date1; time := GetTime(); date1 := GetDate() UNTIL date = date1;
hour := time MOD 10H + time DIV 10H MOD 10H * 10;
minute := time DIV 100H MOD 10H + time DIV 1000H MOD 10H * 10;
second := time DIV 10000H MOD 10H + time DIV 100000H MOD 10H * 10;
year := date MOD 10H + date DIV 10H MOD 10H * 10;
month := date DIV 100H MOD 10H + date DIV 1000H MOD 10H * 10;
day := date DIV 10000H MOD 10H + date DIV 100000H MOD 10H * 10;
RETURN ((((year*10H + month)*20H + day)*20H + hour)*40H + minute)*40H + second
END Clock;
PROCEDURE SetClock*(dt: INTEGER);
VAR res, date, time, time0, date0, date1, second, minute, hour, day, month, year, dow: INTEGER;
BEGIN
year := dt DIV 4000000H MOD 40H;
month := dt DIV 400000H MOD 10H;
day := dt DIV 20000H MOD 20H;
hour := dt DIV 1000H MOD 20H;
minute := dt DIV 40H MOD 40H;
second := dt MOD 40H;
time := ((((second DIV 10 * 10H + second MOD 10)*10H + minute DIV 10)*10H + minute MOD 10)*10H + hour DIV 10)*10H + hour MOD 10;
date := ((((day DIV 10 * 10H + day MOD 10)*10H + month DIV 10)*10H + month MOD 10)*10H + year DIV 10)*10H + year MOD 10;
dow := DayOfWeek(year, month, day);
date1 := GetDate(); REPEAT date0 := date1; time0 := GetTime(); date1 := GetDate() UNTIL date0 = date1;
res := SetRTC(0, 0); (*set time 00:00:00 to avoid race condition on midnight*)
IF res = 0 THEN res := SetRTC(1, date);
IF res = 0 THEN res := SetRTC(0, time);
IF res = 0 THEN res := SetRTC(2, dow) END
END
END;
IF res # 0 THEN (*restore on fail*)
res := SetRTC(0, 0); res := SetRTC(1, date0); res := SetRTC(0, time0)
END
END SetClock;
PROCEDURE Install*(Padr, at: INTEGER);
BEGIN SYSTEM.PUT(org + at, Padr)
END Install;
PROCEDURE Id (): INTEGER;
BEGIN RETURN 0
END Id;
PROCEDURE PrintString(s: ARRAY OF CHAR);
VAR i: INTEGER;
BEGIN i := 0; WHILE s[i] # 0X DO PrintChar(s[i]); INC(i) END
END PrintString;
PROCEDURE PrintHex(x: INTEGER);
VAR i, y: INTEGER;
BEGIN
FOR i := 7 TO 0 BY -1 DO
y := ASR(x, i * 4) MOD 16;
IF y < 10 THEN PrintChar(CHR(y + 30H))
ELSE PrintChar(CHR(y + 37H))
END
END
END PrintHex;
PROCEDURE Trap(VAR a: INTEGER; b, c: INTEGER);
VAR u, v, w: INTEGER;
BEGIN w := c MOD 100H; (*trap number*)
IF w = 0 THEN New(a, b)
ELSIF w = 255 THEN (*led*)
ELSIF w = 254 THEN u := Id() (*id*)
ELSE (*stop*)
SYSTEM.GET(SYSTEM.REG(5) + 4, u); (*return address*)
PrintString("TRAP "); PrintHex(c); PrintString(" at "); PrintHex(u); PrintChar(0AX);
Terminate
END
END Trap;
PROCEDURE Init*;
VAR a, len: INTEGER; ch: CHAR;
BEGIN
SYSTEM.PUT(SYSTEM.ADR(InitFPU), SYSTEM.ADR($55 89E5 9BDBE3 68BF070000 D92C24 83C404 5D C3 90$));
SYSTEM.PUT(SYSTEM.ADR(Terminate), SYSTEM.ADR($B8FFFFFFFF CD40$));
SYSTEM.PUT(SYSTEM.ADR(PrintChar), SYSTEM.ADR($55 89E5 B83F000000 BB01000000 8B4D08 CD40 5D C20400 6690$));
SYSTEM.PUT(SYSTEM.ADR(SetMem), SYSTEM.ADR($55 89E5 B840000000 BB01000000 8B4D08 CD40 5D C20400 6690$));
SYSTEM.PUT(SYSTEM.ADR(GetCounter), SYSTEM.ADR($B81A000000 BB09000000 CD40 C3 6690 90$));
SYSTEM.PUT(SYSTEM.ADR(GetDate), SYSTEM.ADR($55 89E5 B81D000000 CD40 5D C3$));
SYSTEM.PUT(SYSTEM.ADR(GetTime), SYSTEM.ADR($55 89E5 B803000000 CD40 5D C3$));
SYSTEM.PUT(SYSTEM.ADR(SetRTC), SYSTEM.ADR($55 89E5 B816000000 8B5D0C 8B4D08 CD40 5D C20800$));
InitFPU; Install(SYSTEM.ADR(Trap), 20H); (*install temporary trap*)
(*get stack*)
SYSTEM.GET(24, stackOrg); stackSize := 10000H;
(*init heap*)
SYSTEM.GET(20, heapOrg); SYSTEM.GET(28, a);
IF (a # 0) & (a >= heapOrg) THEN (*command line string relocated?*)
len := 0;
REPEAT SYSTEM.GET(a, ch); INC(len); INC(a) UNTIL (ch = 0X) OR (len >= 65536);
heapOrg := a
END;
heapOrg := (heapOrg + 4095) DIV 4096 * 4096; heapLim := heapOrg + 262144; MemLim := heapLim;
IF SetMem(MemLim) # 0 THEN
PrintString("failed to allocate heap "); PrintHex(MemLim); PrintChar(0AX);
Terminate
END;
list1 := 0; list2 := 0; list3 := 0; list0 := heapOrg;
SYSTEM.PUT(list0, heapLim - heapOrg); SYSTEM.PUT(list0+4, -1); SYSTEM.PUT(list0+8, 0);
allocated := 0; clock := 0; time := 0; time0 := 0; time0 := Time(); InitSecMap
END Init;
END Kernel.