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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
; common startup code for all the memory models
%ifdef __TINY__
%define __NEAR_CODE__
%define __NEAR_DATA__
%elifdef __SMALL__
%define __NEAR_CODE__
%define __NEAR_DATA__
%elifdef __MEDIUM__
%define __FAR_CODE__
%define __NEAR_DATA__
%elifdef __COMPACT__
%define __NEAR_CODE__
%define __FAR_DATA__
%elifdef __LARGE__
%define __FAR_CODE__
%define __FAR_DATA__
%elifdef __HUGE__
%define __FAR_CODE__
%define __FAR_DATA__
%else
%fatal "Memory model not defined."
%endif
%ifdef __FAR_CODE__
extra_param_offset equ 2
%else
extra_param_offset equ 0
%endif
%ifdef __FAR_DATA__
extra_data_offset equ 2
%else
extra_data_offset equ 0
%endif
cpu 8086
segment text use16 class=code
extern PASCALMAIN
extern __fpc_PrefixSeg
extern dos_version
extern __Test8086
extern _edata ; defined by WLINK, indicates start of BSS
extern _end ; defined by WLINK, indicates end of BSS
extern __stklen
extern __stktop
extern __stkbottom
extern __nearheap_start
extern __nearheap_end
extern ___heap
%ifdef __NEAR_DATA__
extern __fpc_stackplusmaxheap_in_para
%endif
%ifndef __TINY__
%ifdef __FAR_DATA__
extern ___stack
%endif
%ifdef __NEAR_DATA__
extern ___stacktop
%endif
%endif
extern __SaveInt00
extern FPC_HANDLEERROR
%ifdef __TINY__
resb 0100h
%endif
..start:
%ifdef __TINY__
mov bx, cs
%else
mov bx, dgroup
%ifdef __NEAR_DATA__
; init the stack
mov ss, bx
mov sp, ___stacktop wrt dgroup
%endif
%endif
; zero fill the BSS section
mov es, bx
mov di, _edata wrt dgroup
mov cx, _end wrt dgroup
sub cx, di
xor al, al
cld
rep stosb
; save the Program Segment Prefix
push ds
; init DS
mov ds, bx
; pop the PSP from stack and store it in the pascal variable PrefixSeg
pop ax
mov word [__fpc_PrefixSeg], ax
; get DOS version and save it in the pascal variable dos_version
mov ax, 3000h
int 21h
xchg al, ah
mov word [dos_version], ax
; detect CPU
xor bx, bx ; 0=8086/8088/80186/80188/NEC V20/NEC V30
; on pre-286 processors, bits 12..15 of the FLAGS registers are always set
pushf
pop ax
and ah, 0fh
push ax
popf
pushf
pop ax
and ah, 0f0h
cmp ah, 0f0h
je cpu_detect_done
; at this point we have a 286 or higher
inc bx
; on a true 286 in real mode, bits 12..15 are always clear
pushf
pop ax
or ah, 0f0h
push ax
popf
pushf
pop ax
and ah, 0f0h
jz cpu_detect_done
; we have a 386+
inc bx
cpu_detect_done:
mov [__Test8086], bl
%ifdef __NEAR_DATA__
; ****************************************************************************
; ** near data memory layout setup **
; ****************************************************************************
; allocate max heap
; first we determine in paragraphs ax:=min(64kb, data+bss+stack+maxheap)
mov ax, _end wrt dgroup
add ax, 15
mov cl, 4
shr ax, cl
add ax, word [__fpc_stackplusmaxheap_in_para]
cmp ax, 1000h ; 1000h = 64k in paragraphs
jbe data_with_maxheap_less_than_64k
mov ax, 1000h
data_with_maxheap_less_than_64k:
; try to resize our main DOS memory block until the end of the data segment (or even smaller, if maxheap is small)
mov cx, word [__fpc_PrefixSeg]
%ifdef __TINY__
mov dx, cs
%else
mov dx, dgroup
%endif
sub dx, cx ; dx = (ds - psp) in paragraphs
push dx ; save (ds - psp)
add dx, 1000h ; 64kb in paragraphs
; get our MCB size in paragraphs
dec cx
mov es, cx
mov bx, word [es:3]
; is it smaller than the maximum data segment size?
cmp bx, dx
jbe skip_mem_realloc
mov bx, dx
inc cx
mov es, cx
mov ah, 4Ah
int 21h
jc mem_realloc_err
skip_mem_realloc:
; bx = the new size in paragraphs
pop cx ; cx = (ds - psp)
sub bx, cx
mov cl, 4
shl bx, cl
sub bx, 2
mov sp, bx
mov word [__stktop], sp
add bx, 2
sub bx, word [__stklen]
and bl, 0FEh
mov word [__stkbottom], bx
cmp bx, _end wrt dgroup
jb not_enough_mem
; heap is between [ds:_end wrt dgroup] and [ds:__stkbottom - 1]
mov word [__nearheap_start], _end wrt dgroup
mov bx, word [__stkbottom]
dec bx
mov word [__nearheap_end], bx
; ****************************************************************************
; ** near data setup done **
; ****************************************************************************
%endif
%ifdef __FAR_DATA__
mov word [__stktop], sp
mov word [__stkbottom], 0
mov ax, ss
mov word [__stkbottom + 2], ax
mov word [__stktop + 2], ax
mov dx, sp
add dx, 15
mov cl, 4
shr dx, cl
add ax, dx
mov word [__nearheap_start], 0
mov word [__nearheap_end], 0FFF0h
mov word [__nearheap_start + 2], ax
mov word [__nearheap_end + 2], ax
%endif
%ifdef __FAR_CODE__
jmp far PASCALMAIN
%else
jmp PASCALMAIN
%endif
%ifdef __NEAR_DATA__
not_enough_mem:
mov dx, not_enough_mem_msg
jmp error_msg
mem_realloc_err:
; at this point there's still (ds-psp) pushed on the stack, but we won't
; bother popping it, because we exit to DOS with an error message here
mov dx, mem_realloc_err_msg
error_msg:
mov ah, 9
int 21h
mov ax, 4CFFh
int 21h
%endif
FPC_INT00_HANDLER:
sub sp, 4 ; reserve space on the stack for the retf
push cx
push ds
push bp
; init ds
%ifdef __TINY__
mov bp, cs
%else
mov bp, dgroup
%endif
mov ds, bp
%ifdef __NEAR_DATA__
; in memory models, where SS=DS, also
; check whether we're running on the same stack
mov cx, ss
cmp bp, cx
jne .call_previous_handler
%endif
%ifndef __FAR_CODE__
; check whether we're coming from the same code segment
mov bp, sp
mov cx, [bp + 3*2 + 6] ; get caller segment
mov bp, cs
cmp bp, cx
jne .call_previous_handler
%endif
; runerror 200
mov bp, sp
mov cx, [bp + 3*2 + 4] ; get caller offset
%ifdef __FAR_CODE__
mov dx, [bp + 3*2 + 6] ; get caller segment
%endif
pop bp
add sp, 2*2 + 4 + 6
xor ax, ax
push ax
mov ax, 200
push ax
%ifdef __FAR_CODE__
push dx
%endif
push cx
cld
%ifdef __FAR_CODE__
jmp far FPC_HANDLEERROR
%else
jmp FPC_HANDLEERROR
%endif
.call_previous_handler:
mov bp, sp
mov cx, [__SaveInt00]
mov [bp + 3*2], cx
mov cx, [__SaveInt00+2]
mov [bp + 3*2 + 2], cx
pop bp
pop ds
pop cx
retf ; jumps to the previous handler with all registers and stack intact
global FPC_INSTALL_INTERRUPT_HANDLERS
FPC_INSTALL_INTERRUPT_HANDLERS:
push ds
; save old int 00 handler
mov ax, 3500h
int 21h
mov [__SaveInt00], bx
mov bx, es
mov [__SaveInt00+2], bx
; install the new int 00 handler
%ifndef __TINY__
push cs
pop ds
%endif
mov dx, FPC_INT00_HANDLER
mov ax, 2500h
int 21h
pop ds
%ifdef __FAR_CODE__
retf
%else
ret
%endif
global FPC_RESTORE_INTERRUPT_HANDLERS
FPC_RESTORE_INTERRUPT_HANDLERS:
push ds
mov ax, 2500h
lds dx, [__SaveInt00]
int 21h
pop ds
%ifdef __FAR_CODE__
retf
%else
ret
%endif
global FPC_MSDOS_CARRY
FPC_MSDOS_CARRY:
stc
global FPC_MSDOS
FPC_MSDOS:
mov al, 21h ; not ax, because only the low byte is used
pop dx
%ifdef __FAR_CODE__
pop bx
%endif
pop cx
%ifdef __FAR_DATA__
pop si
%endif
push ax
%ifdef __FAR_DATA__
push si
%endif
push cx
%ifdef __FAR_CODE__
push bx
%endif
push dx
global FPC_INTR
FPC_INTR:
push bp
mov bp, sp
mov al, byte [bp + 6 + extra_param_offset + extra_data_offset]
mov byte [cs:int_number], al
mov si, [bp + 4 + extra_param_offset]
push ds
%ifdef __FAR_DATA__
mov ax, [bp + 6 + extra_param_offset]
mov ds, ax
%endif
mov ax, word [si + 16]
mov es, ax
mov ax, word [si + 14] ; ds
push ax
mov ax, word [si]
mov bx, word [si + 2]
mov cx, word [si + 4]
mov dx, word [si + 6]
mov bp, word [si + 8]
mov di, word [si + 12]
mov si, word [si + 10]
pop ds
db 0CDh ; opcode of INT xx
int_number:
db 255
pushf
push ds
push si
push bp
mov bp, sp
%ifdef __FAR_DATA__
mov si, [bp + 16 + extra_param_offset]
%else
mov si, word [bp + 8]
%endif
mov ds, si
mov si, word [bp + 14 + extra_param_offset]
mov word [si], ax
mov word [si + 2], bx
mov word [si + 4], cx
mov word [si + 6], dx
mov word [si + 12], di
mov ax, es
mov word [si + 16], ax
pop ax
mov word [si + 8], ax
pop ax
mov word [si + 10], ax
pop ax
mov word [si + 14], ax
pop ax
mov word [si + 18], ax
pop ds
pop bp
%ifdef __FAR_CODE__
retf 4 + extra_data_offset
%else
ret 4 + extra_data_offset
%endif
%ifndef __TINY__
global FPC_CHECK_NULLAREA
FPC_CHECK_NULLAREA:
push ds
pop es
xor di, di
mov cx, 32
mov al, 1
cld
repe scasb
je .skip
dec ax ; 1 byte shorter than dec al
.skip:
%ifdef __FAR_CODE__
retf
%else
ret
%endif
%endif
segment data class=data
%ifdef __NEAR_DATA__
mem_realloc_err_msg:
db 'Memory allocation error', 13, 10, '$'
not_enough_mem_msg:
db 'Not enough memory', 13, 10, '$'
%endif
; add reference to the beginning of the minimal heap, so the object
; module, containing the heap segment doesn't get smartlinked away
dd ___heap
segment bss class=bss
%ifndef __TINY__
segment _NULL align=16 class=BEGDATA
global __nullarea
__nullarea:
dd 01010101h, 01010101h, 01010101h, 01010101h
dd 01010101h, 01010101h, 01010101h, 01010101h
segment _AFTERNULL align=2 class=BEGDATA
dw 0
%ifdef __NEAR_DATA__
segment stack stack class=stack
%else
segment data
; add reference to the beginning of stack, so the object module,
; containing the stack segment doesn't get smartlinked away
dd ___stack
%endif
%endif
%ifdef __TINY__
group dgroup text data bss
%else
%ifdef __NEAR_DATA__
group dgroup _NULL _AFTERNULL data bss stack
%else
group dgroup _NULL _AFTERNULL data bss
%endif
%endif
|