ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/DOSGame/trunk/HEAPUTIL.ASM
Revision: 4
Committed: Wed Feb 11 15:26:06 2026 UTC (8 weeks, 4 days ago) by figdor32
File size: 3944 byte(s)
Log Message:
Initial check-in

File Contents

# Content
1 ; ES = Object table segment
2 ; CX = Object count, must be divisible by 8
3 TABLE_InitializeBitmap:
4 MOV WORD [ES:0], CX ; Store the object count
5 SHR CX, 3 ; Divide CX by 8
6 MOV AL, 0 ; Fill with zeroes
7 XOR DI, DI ; The bitmap is the first section of the table save for the obj count
8 ADD DI, 2 ; First WORD is the object count
9 REP STOSB ; TIMES CX Fill ES:DI with AL.
10 ; We can't assume the bitmap will be divisible by a WORD or DWORD,
11 ; but thankfully this only really gets called once during the entire lifetime of the program.
12 RET
13
14 ; ES = Object table segment
15 ; AX = Object handle
16 ; RET: ES:DI = Object addr
17 TABLE_GetObjectAddr:
18 MOV WORD BX, [ES:0] ; Get object count
19 MOV DI, BX ; Set to object count
20 SHR DI, 3 ; Divide by 8
21 ADD DI, 2 ; And skip the object count
22 ADD DI, BX ; Then add object lengths
23 SHL AX, 4 ; Multiply the object handle by 16
24 ADD DI, AX ; And point to the proper paragraph
25 ADD DI, 15 ; Make sure we round up if not aligned
26 AND DI, 0xFFF0 ; Clear the lower 4 bits to align to 16 bytes
27 RET
28 ; AX = Object handle
29 ; ES = Object table segment
30 TABLE_ObjFree:
31 PUSH DI ; Store DI
32 MOV DI, [ES:0] ; Store object count
33 SHR DI, 3 ; Divide by 8 to skip the bitmap
34 ADD DI, 2 ; Skip the object count WORD
35 ADD DI, AX ; Ptr to the object length
36 MOV BYTE CL, [ES:DI] ; Get the object length
37 CALL TABLE_INTR_ObjFree ; And free the specified bits
38 POP DI ; Restore DI
39 RET
40
41 ; ES = Object table segment
42 ; AX = Object handle
43 ; CX = Number of paragraphs to free
44 TABLE_INTR_ObjFree:
45 PUSH AX ; Store the object handle
46 PUSH CX ; And store the object length
47 .loop:
48 BTR [ES:2], AX ; Object handle is simply the object position, we can use it verbatim
49 INC AX ; Increase the bit position
50 DEC CX ; And decrease the counter
51 JNZ .loop ; If counter is nonzero, loop
52 POP CX ; Restore object length
53 POP AX ; Restore object handle
54 ; Technically those two are now invalid, but I'm not sure how the final code will look like.
55 RET
56
57 ; ES = Object segment
58 ; DX = Object offset
59 ; CX = Object length
60 TABLE_INTR_SetObjLength:
61 PUSH DI ; Store DI
62 MOV WORD DI, [ES:0] ; Store the object count
63 SHR DI, 3 ; Divide by 8 to skip the bitmap
64 ADD DI, 2 ; Skip the object count
65 ADD DI, DX ; Ptr to the object size
66 MOV BYTE [ES:DI], CL ; Store the object size
67 POP DI ; And restore DI
68 RET
69
70 ; ES = Object table segment
71 ; CX = Number of paragraphs to allocate
72 ; RET AX = Object handle (bit index)
73 TABLE_ObjAlloc:
74 PUSH CX
75 MOV BX, [ES:0] ; Object count
76 SUB BX, CX ; Max starting bit index = total - needed
77 XOR DX, DX ; DX = current bit index (our cursor)
78
79 .next_candidate:
80 PUSH DX ; Save starting bit index
81 PUSH CX ; Save number of bits to test
82 MOV SI, DX ; SI = test index
83 MOV DI, CX ; DI = countdown
84
85 .test_bits:
86 BT [ES:2], SI
87 JC .bit_used ; If bit set, abort this sequence
88 INC SI
89 DEC DI
90 JNZ .test_bits
91
92 ; If we got here, we found CX contiguous 0s starting at DX
93 POP CX ; Restore CX for writing
94 POP DX ; DX = starting index
95 MOV SI, DX
96
97 .set_bits:
98 BTS [ES:2], SI
99 INC SI
100 LOOP .set_bits
101
102 MOV AX, DX ; Return object handle = starting bit
103 POP CX ; Clean up
104 CALL TABLE_INTR_SetObjLength
105 RET
106
107 .bit_used:
108 POP CX
109 POP DX
110 INC DX
111 CMP DX, BX
112 JBE .next_candidate
113
114 .fail:
115 MOV AX, 0xFFFF ; Error: no space
116 STC
117 RET