Sector Bitmap





These functions are used for indicating disk usage. Space is indicated by a bitmap where one bit represents one block (512 bytes) on the media. When a block is in use, the appropiate bit in the BITMAP table will be set to one. For groups of 32 or more blocks, these groups will always start on a multiple of 32 blocks.

The bitmap is set up so that block 0 is indicated by the top bit of the first byte.

variable p-blocks       \ -- addr
Holds groups of less than 32 blocks required.

variable no-32          \ -- addr
Holds number of 32 block groups required.

variable b-ptr          \ -- addr
Holds disk buffer search pointer.

variable grab/free      \ -- addr
Holds true for grab, false for release

BLOCK_SIZE 8 * equ BLOCKS/BIT_TABLE     \ -- n
Number of bits (blocks) per block.

BLOCKS/BIT_TABLE equ BLOCKS/GROUP       \ -- n
Synonym.

$FFFFFFFF equ 32BLOCKS  \ -- mask
Indicator for 32 blocks.

: bitmask       \ n -- mask
Creates an n-bit mask in the low bits.

: topBitmask    \ n -- pattern
Creates an n-bit mask starting at the highest bit.

  • 1 bit is $80000000
  • 4 bits is $f0000000
  • 16 bits is $ffff0000
  • : (find-partial-block) { l_data l_size | l_block-no -- start-block-no }
    Finds partial block of SIZE anywhere in 32 blocks. Returns -1 if it won't fit.

    : find-<32blocks        \ -- start|-1
    Search the current bitmap block for the number of blocks held in variable P-BLOCK, returning -1 if NOT FOUND, otherwise returning the start block.

  • no find of continue => -1 resets "start block" pointer, continue search
  • no find of small block => -1 continue search
  • find of continue => 0 all found and done
  • find of small block => ptr all found done, set "start block" pointer
  • : find-free-blocks      { l_req-blocks | l_start-block l_32-found -- start-block }
    Find the required number of blocks, returning the start block, or -1 if space cannot be found.

    : block-order   { l_start l_amount | l_buff -- buffer }
    Set up the data structure required to grab or release a number of blocks starting at a given block.

    : adjust-blk-addr       \ addr1 -- addr2
    COMPILER: Convert a block number to address a cell containing its indicator bit. Equivalent to:

      #32 / cells

    : 32mod         \ n -- mod(n,32)
    COMPILER: Given a block number, return the bit number of its indicator bit in a cell. Equivalent to:

      #32 mod

    : take/free     \ data mask -- data'
    Apply the bit mask to grab or free the required blocks in data.

    : take/free32   \ -- mask
    Return the data value to grab/free 32 blocks.

    : grab-first-block      \ start-block start-address first-amount --
    Using the starting bitmap block start-block, grab/free first-amount blocks from start-address onwards.

    : grab-32complete-blocks        \ block --
    Grab/free the complete block of the usage bit map.

    : grab-last-block       \ last-amount last-block --
    Grab/free the given amount in the last required block of the usage bitmap.

    : allocate-blocks       { l_buff -- }
    Using the data structure created by BLOCK-ORDER, grab/free the blocks and FREE the buffer.

    : need-blocks   \ amount -- start
    This attempts to find a contigous area of required space and allocates it. Amount is the mumber of blocks required. Start is the starting block number on the media, or -1 if space cannot be found.

    : free-blocks   \ start amount --
    This will release the required number of blocks from the start point.




    Diagnostics

    : d-fbm         { | l_*buff -- }
    Display the disk bitmap.