| 1 | ; $Id: buffer.asm,v 1.5 2000-02-21 09:24:02 bird Exp $ | 
|---|
| 2 | ; | 
|---|
| 3 | ; Simple resident buffer for use when overloading tkExecPgm. | 
|---|
| 4 | ; | 
|---|
| 5 | ; Copyright (c) 2000 knut st. osmundsen | 
|---|
| 6 | ; | 
|---|
| 7 | ; Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 8 | ; | 
|---|
| 9 | .486p | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 | ; | 
|---|
| 13 | ;   Defined Constants And Macros | 
|---|
| 14 | ; | 
|---|
| 15 | NBR_BUFFERS     EQU 20 | 
|---|
| 16 | BUFFER_SIZE     EQU 1536 | 
|---|
| 17 |  | 
|---|
| 18 | ; | 
|---|
| 19 | ;   Include files | 
|---|
| 20 | ; | 
|---|
| 21 | include devsegdf.inc | 
|---|
| 22 |  | 
|---|
| 23 | ; | 
|---|
| 24 | ;   Exported symbols | 
|---|
| 25 | ; | 
|---|
| 26 | public AcquireBuffer | 
|---|
| 27 | public ReleaseBuffer | 
|---|
| 28 | public QueryBufferSegmentOffset | 
|---|
| 29 | public QueryBufferPointerFromFilename | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | ; | 
|---|
| 33 | ;   Imported Functions | 
|---|
| 34 | ; | 
|---|
| 35 | extrn stricmp:PROC | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | ; | 
|---|
| 39 | ;   Global Variables | 
|---|
| 40 | ; | 
|---|
| 41 | DATA16 SEGMENT | 
|---|
| 42 | aachBuffer  db BUFFER_SIZE*NBR_BUFFERS dup(?) ; The buffer | 
|---|
| 43 | DATA16 ENDS | 
|---|
| 44 |  | 
|---|
| 45 | DATA32 SEGMENT | 
|---|
| 46 | afBuffer    db NBR_BUFFERS dup(0)       ; Access "semaphore" | 
|---|
| 47 | ; 0 - not in use | 
|---|
| 48 | ; 1 - in use | 
|---|
| 49 | DATA32 ENDS | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 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 | 
|---|
| 60 | ; @sketch    if AfBuffer == 0 then | 
|---|
| 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 | 
|---|
| 73 | push    ecx | 
|---|
| 74 | push    edx | 
|---|
| 75 |  | 
|---|
| 76 | ; make ds flat | 
|---|
| 77 | mov     ax, seg FLAT:DATA32 | 
|---|
| 78 | mov     ds, ax | 
|---|
| 79 | ASSUME  DS:FLAT | 
|---|
| 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 | 
|---|
| 94 | AcquireBuffer_nok: | 
|---|
| 95 | xor     eax,eax | 
|---|
| 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 | 
|---|
| 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 | 
|---|
| 124 | push    ecx | 
|---|
| 125 | push    edx | 
|---|
| 126 |  | 
|---|
| 127 | ; make ds flat | 
|---|
| 128 | push    eax | 
|---|
| 129 | mov     ax, seg FLAT:DATA32 | 
|---|
| 130 | mov     ds, ax | 
|---|
| 131 | ASSUME  DS:FLAT | 
|---|
| 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" | 
|---|
| 150 | mov     al, 1 | 
|---|
| 151 | mov     ah, 0 | 
|---|
| 152 | lock cmpxchg [edx], ah | 
|---|
| 153 | jne     ReleaseBuffer_nok           ; fail if buffer was not locked | 
|---|
| 154 |  | 
|---|
| 155 | ReleaseBuffer_ok: | 
|---|
| 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 |  | 
|---|
| 172 | ReleaseBuffer_nok: | 
|---|
| 173 | ;failure | 
|---|
| 174 | pop     eax | 
|---|
| 175 | mov     eax, 87 ;some error | 
|---|
| 176 |  | 
|---|
| 177 | ReleaseBuffer_ret: | 
|---|
| 178 | pop     edx | 
|---|
| 179 | pop     ecx | 
|---|
| 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 | 
|---|
| 198 |  | 
|---|
| 199 | ; make ds FLAT | 
|---|
| 200 | push    eax | 
|---|
| 201 | mov     ax, seg FLAT:DATA32 | 
|---|
| 202 | mov     ds, ax | 
|---|
| 203 | ASSUME  DS:FLAT | 
|---|
| 204 | pop     eax | 
|---|
| 205 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 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. | 
|---|
| 253 | cmp     byte ptr [ebx], 1           ; Is buffer in use? | 
|---|
| 254 | jne     QueryBufferPointerFromFilename_next | 
|---|
| 255 |  | 
|---|
| 256 | ; buffer is active - lets compare | 
|---|
| 257 | push    eax | 
|---|
| 258 | push    ecx | 
|---|
| 259 | push    edx | 
|---|
| 260 | sub     esp, 8 | 
|---|
| 261 | call    stricmp | 
|---|
| 262 | add     esp, 8 | 
|---|
| 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 | 
|---|
| 276 | pop     ebx | 
|---|
| 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 | 
|---|
| 284 | pop     ebx | 
|---|
| 285 | mov     eax, edx | 
|---|
| 286 | pop     edx | 
|---|
| 287 | pop     ecx | 
|---|
| 288 | ret | 
|---|
| 289 |  | 
|---|
| 290 | QueryBufferPointerFromFilename ENDP | 
|---|
| 291 |  | 
|---|
| 292 |  | 
|---|
| 293 |  | 
|---|
| 294 |  | 
|---|
| 295 | CODE32 ENDS | 
|---|
| 296 |  | 
|---|
| 297 |  | 
|---|
| 298 | CODE16 SEGMENT | 
|---|
| 299 | ;; | 
|---|
| 300 | ; Gets the segment(->es) and offset(+ax -> ax) of the achBuffer. | 
|---|
| 301 | ; @param    ax  offset to buffer relative to aachBuffer | 
|---|
| 302 | ; Jumps back to GetBufferOffset32 | 
|---|
| 303 | GetBufferSegmentOffset16: | 
|---|
| 304 | ASSUME CS:CODE16, DS:NOTHING | 
|---|
| 305 | push    ax | 
|---|
| 306 | mov     ax, seg aachBuffer | 
|---|
| 307 | mov     es, ax | 
|---|
| 308 | pop     ax | 
|---|
| 309 | add     ax, offset aachBuffer | 
|---|
| 310 | jmp     far ptr FLAT:QueryBufferSegmentOffset_Back | 
|---|
| 311 | CODE16 ENDS | 
|---|
| 312 |  | 
|---|
| 313 | END | 
|---|
| 314 |  | 
|---|