[4164] | 1 | ; $Id: buffer.asm,v 1.6 2000-09-02 21:08:13 bird Exp $
|
---|
[2819] | 2 | ;
|
---|
[4164] | 3 | ; Simple resident buffer for use when overloading tkExecPgm - obsolete!
|
---|
[2819] | 4 | ;
|
---|
| 5 | ; Copyright (c) 2000 knut st. osmundsen
|
---|
| 6 | ;
|
---|
| 7 | ; Project Odin Software License can be found in LICENSE.TXT
|
---|
| 8 | ;
|
---|
| 9 | .486p
|
---|
| 10 |
|
---|
[2827] | 11 |
|
---|
[2819] | 12 | ;
|
---|
[2827] | 13 | ; Defined Constants And Macros
|
---|
| 14 | ;
|
---|
[4164] | 15 | NBR_BUFFERS EQU 2
|
---|
[2827] | 16 | BUFFER_SIZE EQU 1536
|
---|
| 17 |
|
---|
| 18 | ;
|
---|
[2819] | 19 | ; Include files
|
---|
| 20 | ;
|
---|
| 21 | include devsegdf.inc
|
---|
| 22 |
|
---|
| 23 | ;
|
---|
| 24 | ; Exported symbols
|
---|
| 25 | ;
|
---|
| 26 | public AcquireBuffer
|
---|
| 27 | public ReleaseBuffer
|
---|
[2823] | 28 | public QueryBufferSegmentOffset
|
---|
[2827] | 29 | public QueryBufferPointerFromFilename
|
---|
[2819] | 30 |
|
---|
| 31 |
|
---|
| 32 | ;
|
---|
[2827] | 33 | ; Imported Functions
|
---|
| 34 | ;
|
---|
| 35 | extrn stricmp:PROC
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | ;
|
---|
[2819] | 39 | ; Global Variables
|
---|
| 40 | ;
|
---|
[2823] | 41 | DATA16 SEGMENT
|
---|
[2827] | 42 | aachBuffer db BUFFER_SIZE*NBR_BUFFERS dup(?) ; The buffer
|
---|
[2823] | 43 | DATA16 ENDS
|
---|
[2819] | 44 |
|
---|
[2823] | 45 | DATA32 SEGMENT
|
---|
[2827] | 46 | afBuffer db NBR_BUFFERS dup(0) ; Access "semaphore"
|
---|
| 47 | ; 0 - not in use
|
---|
| 48 | ; 1 - in use
|
---|
[2823] | 49 | DATA32 ENDS
|
---|
[2819] | 50 |
|
---|
[2823] | 51 |
|
---|
[2819] | 52 | CODE32 segment
|
---|
| 53 | assume CS:CODE32, DS:NOTHING, SS:NOTHING
|
---|
| 54 |
|
---|
| 55 | ;;
|
---|
| 56 | ; Aquires a resident buffer. (intended use for tkExecPgm)
|
---|
| 57 | ; @cproto assembly only for time being.
|
---|
| 58 | ; @returns Pointer to buffer
|
---|
| 59 | ; @uses eax
|
---|
[2827] | 60 | ; @sketch if AfBuffer == 0 then
|
---|
[2819] | 61 | ; ok!
|
---|
| 62 | ; fBuffer <- 1
|
---|
| 63 | ; return pointer to buffer
|
---|
| 64 | ; else
|
---|
| 65 | ; fail
|
---|
| 66 | ; return NULL
|
---|
| 67 | ; endif
|
---|
| 68 | ; @status completely implemented.
|
---|
| 69 | ; @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
---|
| 70 | ; @remark cbBuffer holds the size of the buffer.
|
---|
| 71 | AcquireBuffer PROC NEAR
|
---|
| 72 | push ds
|
---|
[2827] | 73 | push ecx
|
---|
| 74 | push edx
|
---|
| 75 |
|
---|
| 76 | ; make ds flat
|
---|
| 77 | mov ax, seg FLAT:DATA32
|
---|
| 78 | mov ds, ax
|
---|
[2819] | 79 | ASSUME DS:FLAT
|
---|
[2827] | 80 |
|
---|
| 81 | ; loop thru all buffers and try reserve one
|
---|
| 82 | mov ah, 1 ; afBuffer[ecx] is set to this value (if equal to al)
|
---|
| 83 | mov ecx, NBR_BUFFERS ; interations.
|
---|
| 84 | mov edx, offset FLAT:afBuffer
|
---|
| 85 | add edx, ecx
|
---|
| 86 | AcquireBuffer_loop:
|
---|
| 87 | dec edx
|
---|
| 88 | mov al, 0 ; afBuffer[ecx] should have this value
|
---|
| 89 | lock cmpxchg [edx], ah
|
---|
| 90 | je AcquireBuffer_ok
|
---|
| 91 | loop AcquireBuffer_loop
|
---|
| 92 |
|
---|
| 93 | ; failure
|
---|
[2819] | 94 | AcquireBuffer_nok:
|
---|
| 95 | xor eax,eax
|
---|
[2827] | 96 | jmp AcquireBuffer_ret
|
---|
| 97 |
|
---|
| 98 | ;success - calc buffer pointer
|
---|
| 99 | AcquireBuffer_ok:
|
---|
| 100 | mov eax, BUFFER_SIZE
|
---|
| 101 | dec ecx
|
---|
| 102 | imul eax, ecx ; ecx has the buffer number
|
---|
| 103 | add eax, offset FLAT:aachBuffer
|
---|
| 104 | AcquireBuffer_ret:
|
---|
| 105 | pop edx
|
---|
| 106 | pop ecx
|
---|
[2819] | 107 | pop ds
|
---|
| 108 | ret
|
---|
| 109 | AcquireBuffer ENDP
|
---|
| 110 |
|
---|
| 111 | ;;
|
---|
| 112 | ; Release the resident buffer pointed to by eax from use.
|
---|
| 113 | ; @cproto assembly only for time being.
|
---|
| 114 | ; @returns 0 on success, 87 on error.
|
---|
| 115 | ; @param eax Pointer to buffer.
|
---|
| 116 | ; @uses eax
|
---|
| 117 | ; @equiv
|
---|
| 118 | ; @status
|
---|
| 119 | ; @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
---|
| 120 | ; @remark
|
---|
| 121 | ReleaseBuffer PROC NEAR
|
---|
| 122 | ASSUME DS:NOTHING
|
---|
| 123 | push ds
|
---|
[2827] | 124 | push ecx
|
---|
| 125 | push edx
|
---|
| 126 |
|
---|
| 127 | ; make ds flat
|
---|
| 128 | push eax
|
---|
| 129 | mov ax, seg FLAT:DATA32
|
---|
| 130 | mov ds, ax
|
---|
[2819] | 131 | ASSUME DS:FLAT
|
---|
[2827] | 132 | pop eax
|
---|
| 133 | push eax
|
---|
| 134 |
|
---|
| 135 | ; validate input and calc buffer number
|
---|
| 136 | cmp eax, offset FLAT:aachBuffer
|
---|
| 137 | jl ReleaseBuffer_nok ; if eax (buffer pointer) is less than the first buffer, then fail.
|
---|
| 138 | sub eax, offset FLAT:aachBuffer ; eax <- offset to buffer from aachBuffer
|
---|
| 139 | xor edx, edx
|
---|
| 140 | mov ecx, BUFFER_SIZE
|
---|
| 141 | div ecx ; eax <- buffer number, edx <- offset into buffer (should be NULL)
|
---|
| 142 | or edx, edx
|
---|
| 143 | jnz ReleaseBuffer_nok ; if offset into buffer not 0 the fail.
|
---|
| 144 | cmp eax, NBR_BUFFERS
|
---|
| 145 | jge ReleaseBuffer_nok ; if buffernumber >= number of buffers then fail.
|
---|
| 146 |
|
---|
| 147 | ; unlock buffer - if locked
|
---|
| 148 | mov edx, eax
|
---|
| 149 | add edx, offset FLAT:afBuffer ; ds:edx points at buffer "semaphore"
|
---|
[2819] | 150 | mov al, 1
|
---|
| 151 | mov ah, 0
|
---|
[2827] | 152 | lock cmpxchg [edx], ah
|
---|
| 153 | jne ReleaseBuffer_nok ; fail if buffer was not locked
|
---|
| 154 |
|
---|
[2819] | 155 | ReleaseBuffer_ok:
|
---|
[2827] | 156 | ;swipe out buffer
|
---|
| 157 | pop eax
|
---|
| 158 | push edi
|
---|
| 159 | push es
|
---|
| 160 | mov edi, eax
|
---|
| 161 | mov ax, ds
|
---|
| 162 | mov es, ax
|
---|
| 163 | ASSUME es:FLAT
|
---|
| 164 | xor eax, eax ; ecx is allready BUFFER_SIZE
|
---|
| 165 | rep stosb
|
---|
| 166 | pop es
|
---|
| 167 | pop edi
|
---|
| 168 |
|
---|
| 169 | ;return successfully
|
---|
| 170 | jmp ReleaseBuffer_ret
|
---|
| 171 |
|
---|
[2819] | 172 | ReleaseBuffer_nok:
|
---|
[2827] | 173 | ;failure
|
---|
| 174 | pop eax
|
---|
[2819] | 175 | mov eax, 87 ;some error
|
---|
[2827] | 176 |
|
---|
| 177 | ReleaseBuffer_ret:
|
---|
| 178 | pop edx
|
---|
| 179 | pop ecx
|
---|
[2819] | 180 | pop ds
|
---|
| 181 | ret
|
---|
| 182 | ReleaseBuffer ENDP
|
---|
| 183 |
|
---|
| 184 |
|
---|
| 185 | ;;
|
---|
| 186 | ; Gets the 16-bit segment and offset for this buffer.
|
---|
| 187 | ; @cproto assembly only for time being.
|
---|
| 188 | ; @returns segment in es and offset in eax
|
---|
| 189 | ; On error high word of eax is 87.
|
---|
| 190 | ; @param eax Buffer pointer.
|
---|
| 191 | ; @uses eax, es
|
---|
| 192 | ; @status completely implemented.
|
---|
| 193 | ; @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
---|
| 194 | ; @remark cbBuffer holds the size of the buffer.
|
---|
| 195 | QueryBufferSegmentOffset PROC NEAR
|
---|
| 196 | ASSUME DS:NOTHING
|
---|
| 197 | push ds
|
---|
[2827] | 198 |
|
---|
| 199 | ; make ds FLAT
|
---|
| 200 | push eax
|
---|
| 201 | mov ax, seg FLAT:DATA32
|
---|
| 202 | mov ds, ax
|
---|
[2819] | 203 | ASSUME DS:FLAT
|
---|
[2827] | 204 | pop eax
|
---|
[2819] | 205 |
|
---|
[2827] | 206 | ; validate parameter and calc offset relative to aachBuffer
|
---|
| 207 | cmp eax, offset FLAT:aachBuffer
|
---|
| 208 | jl QueryBufferSegmentOffset_nok
|
---|
| 209 | sub eax, offset FLAT:aachBuffer
|
---|
| 210 | cmp eax, NBR_BUFFERS * BUFFER_SIZE
|
---|
| 211 | jge QueryBufferSegmentOffset_nok
|
---|
| 212 |
|
---|
[2819] | 213 | QueryBufferSegmentOffset_ok:
|
---|
| 214 | jmp far ptr CODE16:GetBufferSegmentOffset16
|
---|
| 215 | QueryBufferSegmentOffset_Back::
|
---|
| 216 | pop ds
|
---|
| 217 | ret
|
---|
| 218 |
|
---|
| 219 | QueryBufferSegmentOffset_nok:
|
---|
| 220 | mov eax, 00570000h
|
---|
| 221 | pop ds
|
---|
| 222 | ret
|
---|
| 223 | QueryBufferSegmentOffset ENDP
|
---|
| 224 |
|
---|
| 225 |
|
---|
[2827] | 226 | ;;
|
---|
| 227 | ; Special function which checks all used buffers for the string passed in in eax.
|
---|
| 228 | ; @cproto PCHAR _Optlink QueryBufferPointerFromFilename(const char *pszFilename)
|
---|
| 229 | ; @returns Pointer to Buffer. NULL on error/notfound.
|
---|
| 230 | ; @param ds:eax Pointer to filename. The filename will later be compared to the string at buffer start.
|
---|
| 231 | ; Currently this parameter is not used, since there is only one buffer.
|
---|
| 232 | ; @uses eax.
|
---|
| 233 | ; @sketch
|
---|
| 234 | ; @status completely implemented
|
---|
| 235 | ; @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
---|
| 236 | ; @remark assumes ds flat on call.
|
---|
| 237 | QueryBufferPointerFromFilename PROC NEAR
|
---|
| 238 | ASSUME cs:CODE32, ds:FLAT
|
---|
| 239 | push ecx
|
---|
| 240 | push edx
|
---|
| 241 | push ebx
|
---|
| 242 |
|
---|
| 243 | ; loop thru all buffers until found or non left
|
---|
| 244 | mov ecx, NBR_BUFFERS
|
---|
| 245 | mov edx, BUFFER_SIZE
|
---|
| 246 | imul edx, ecx ; edx <- sizeof(aachBuffer)
|
---|
| 247 | add edx, offset FLAT:aachBuffer ; edx points at the first byte after the buffers.
|
---|
| 248 | mov ebx, offset FLAT:afBuffer
|
---|
| 249 | add ebx, ecx ; edx points at the first byte after the "semaphore" array (afBuffer).
|
---|
| 250 | QueryBufferPointerFromFilename_loop:
|
---|
| 251 | sub edx, BUFFER_SIZE ; edx points at the buffer being concidered.
|
---|
| 252 | dec ebx ; ebx points at the "semaphore" for the buffer being concidered.
|
---|
[2838] | 253 | cmp byte ptr [ebx], 1 ; Is buffer in use?
|
---|
[2827] | 254 | jne QueryBufferPointerFromFilename_next
|
---|
| 255 |
|
---|
| 256 | ; buffer is active - lets compare
|
---|
| 257 | push eax
|
---|
| 258 | push ecx
|
---|
| 259 | push edx
|
---|
[2838] | 260 | sub esp, 8
|
---|
[2827] | 261 | call stricmp
|
---|
[2838] | 262 | add esp, 8
|
---|
[2827] | 263 | pop edx
|
---|
| 264 | pop ecx
|
---|
| 265 | or eax, eax
|
---|
| 266 | jz QueryBufferPointerFromFilename_found
|
---|
| 267 | pop eax
|
---|
| 268 |
|
---|
| 269 | QueryBufferPointerFromFilename_next:
|
---|
| 270 | loop QueryBufferPointerFromFilename_loop
|
---|
| 271 | ; when we exit this loop we have failed!
|
---|
| 272 |
|
---|
| 273 | QueryBufferPointerFromFilename_nok:
|
---|
| 274 | ; The buffer was not found, return NULL pointer.
|
---|
| 275 | xor eax, eax
|
---|
[2837] | 276 | pop ebx
|
---|
[2827] | 277 | pop edx
|
---|
| 278 | pop ecx
|
---|
| 279 | ret
|
---|
| 280 |
|
---|
| 281 | QueryBufferPointerFromFilename_found:
|
---|
| 282 | ; The buffer was found, return the pointer to it!
|
---|
| 283 | pop eax
|
---|
[2837] | 284 | pop ebx
|
---|
[2827] | 285 | mov eax, edx
|
---|
| 286 | pop edx
|
---|
| 287 | pop ecx
|
---|
| 288 | ret
|
---|
| 289 |
|
---|
| 290 | QueryBufferPointerFromFilename ENDP
|
---|
| 291 |
|
---|
| 292 |
|
---|
| 293 |
|
---|
| 294 |
|
---|
[2819] | 295 | CODE32 ENDS
|
---|
| 296 |
|
---|
[2827] | 297 |
|
---|
[2819] | 298 | CODE16 SEGMENT
|
---|
| 299 | ;;
|
---|
[2827] | 300 | ; Gets the segment(->es) and offset(+ax -> ax) of the achBuffer.
|
---|
| 301 | ; @param ax offset to buffer relative to aachBuffer
|
---|
[2819] | 302 | ; Jumps back to GetBufferOffset32
|
---|
| 303 | GetBufferSegmentOffset16:
|
---|
[2823] | 304 | ASSUME CS:CODE16, DS:NOTHING
|
---|
[2827] | 305 | push ax
|
---|
| 306 | mov ax, seg aachBuffer
|
---|
[2819] | 307 | mov es, ax
|
---|
[2827] | 308 | pop ax
|
---|
| 309 | add ax, offset aachBuffer
|
---|
[2819] | 310 | jmp far ptr FLAT:QueryBufferSegmentOffset_Back
|
---|
| 311 | CODE16 ENDS
|
---|
| 312 |
|
---|
| 313 | END
|
---|
| 314 |
|
---|