ProDOS-Utilities/prodos/bitmap.go

184 lines
4.5 KiB
Go
Raw Normal View History

// Copyright Terence J. Boldt (c)2021-2023
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
// This file provides access to volum bitmap on
// a ProDOS drive image
2021-06-07 00:15:41 +00:00
package prodos
2021-06-09 12:23:18 +00:00
import (
"io"
2021-06-09 12:23:18 +00:00
)
2021-06-07 00:15:41 +00:00
2022-01-23 22:30:18 +00:00
// ReadVolumeBitmap reads the volume bitmap from a ProDOS image
func ReadVolumeBitmap(reader io.ReaderAt) ([]byte, error) {
headerBlock, err := ReadBlock(reader, 2)
if err != nil {
return nil, err
}
2021-06-07 00:15:41 +00:00
volumeHeader := parseVolumeHeader(headerBlock)
totalBitmapBytes := volumeHeader.TotalBlocks / 8
if volumeHeader.TotalBlocks%8 > 0 {
totalBitmapBytes++
}
bitmap := make([]byte, totalBitmapBytes)
2021-06-07 00:15:41 +00:00
totalBitmapBlocks := totalBitmapBytes / 512
2021-06-07 00:15:41 +00:00
if totalBitmapBytes%512 > 0 {
totalBitmapBlocks++
}
2024-03-11 13:05:23 +00:00
for i := uint16(0); i < totalBitmapBlocks; i++ {
bitmapBlock, err := ReadBlock(reader, i+volumeHeader.BitmapStartBlock)
if err != nil {
return nil, err
}
2021-06-07 00:15:41 +00:00
2024-03-11 13:05:23 +00:00
for j := uint16(0); j < 512 && i*512+j < totalBitmapBytes; j++ {
2021-06-07 00:15:41 +00:00
bitmap[i*512+j] = bitmapBlock[j]
}
}
return bitmap, nil
2021-06-07 00:15:41 +00:00
}
2022-01-23 22:30:18 +00:00
// GetFreeBlockCount gets the number of free blocks on a ProDOS image
2024-03-11 13:05:23 +00:00
func GetFreeBlockCount(volumeBitmap []byte, totalBlocks uint16) uint16 {
freeBlockCount := uint16(0)
2022-01-23 22:30:18 +00:00
2024-03-11 13:05:23 +00:00
for i := uint16(0); i < totalBlocks; i++ {
2022-01-23 22:30:18 +00:00
if checkFreeBlockInVolumeBitmap(volumeBitmap, i) {
freeBlockCount++
}
}
return freeBlockCount
}
func writeVolumeBitmap(readerWriter ReaderWriterAt, bitmap []byte) error {
headerBlock, err := ReadBlock(readerWriter, 2)
if err != nil {
return err
}
2021-06-07 00:15:41 +00:00
volumeHeader := parseVolumeHeader(headerBlock)
totalBitmapBytes := volumeHeader.TotalBlocks / 8
if volumeHeader.TotalBlocks%8 > 0 {
totalBitmapBytes++
}
2021-06-07 00:15:41 +00:00
totalBitmapBlocks := totalBitmapBytes / 512
if totalBitmapBytes%512 > 0 {
totalBitmapBlocks++
2021-06-07 00:15:41 +00:00
}
2024-03-11 13:05:23 +00:00
for i := uint16(0); i < totalBitmapBlocks; i++ {
bitmapBlock, err := ReadBlock(readerWriter, i+volumeHeader.BitmapStartBlock)
if err != nil {
return err
}
2024-03-11 13:05:23 +00:00
for j := uint16(0); j < 512 && i*512+j < totalBitmapBytes; j++ {
bitmapBlock[j] = bitmap[i*512+j]
}
err = WriteBlock(readerWriter, volumeHeader.BitmapStartBlock+i, bitmapBlock)
if err != nil {
return err
}
}
return nil
2021-06-07 00:15:41 +00:00
}
2024-03-11 13:05:23 +00:00
func createVolumeBitmap(numberOfBlocks uint16) []byte {
// needs to be > uint16 because it's multiplying by a uint16
volumeBitmapBlocks := uint32(numberOfBlocks / 512 / 8)
if volumeBitmapBlocks*8*512 < uint32(numberOfBlocks) {
2021-06-09 12:23:18 +00:00
volumeBitmapBlocks++
}
// set all 1's to show blocks available...
volumeBitmap := make([]byte, volumeBitmapBlocks*512)
for i := 0; i < len(volumeBitmap); i++ {
volumeBitmap[i] = 0xFF
}
// boot blocks
2021-06-29 02:26:20 +00:00
markBlockInVolumeBitmap(volumeBitmap, 0)
markBlockInVolumeBitmap(volumeBitmap, 1)
2021-06-09 12:23:18 +00:00
// root directory
2021-06-29 02:26:20 +00:00
markBlockInVolumeBitmap(volumeBitmap, 2)
markBlockInVolumeBitmap(volumeBitmap, 3)
markBlockInVolumeBitmap(volumeBitmap, 4)
markBlockInVolumeBitmap(volumeBitmap, 5)
2021-06-09 12:23:18 +00:00
// volume bitmap blocks
2024-03-11 13:05:23 +00:00
for i := uint32(0); i < volumeBitmapBlocks; i++ {
markBlockInVolumeBitmap(volumeBitmap, uint16(6+i))
2021-06-09 12:23:18 +00:00
}
// blocks beyond the volume
totalBlocksInBitmap := volumeBitmapBlocks * 512 * 8
2024-03-11 13:05:23 +00:00
blocksBeyondEnd := totalBlocksInBitmap - uint32(numberOfBlocks)
2021-06-09 12:23:18 +00:00
if blocksBeyondEnd > 0 {
for i := totalBlocksInBitmap - blocksBeyondEnd; i < totalBlocksInBitmap; i++ {
2024-03-11 13:05:23 +00:00
markBlockInVolumeBitmap(volumeBitmap, uint16(i))
2021-06-09 12:23:18 +00:00
}
}
return volumeBitmap
}
2024-03-11 13:05:23 +00:00
func findFreeBlocks(volumeBitmap []byte, numberOfBlocks uint16) []uint16 {
blocks := make([]uint16, numberOfBlocks)
2021-06-26 01:15:20 +00:00
2024-03-11 13:05:23 +00:00
blocksFound := uint16(0)
2021-06-26 01:15:20 +00:00
2024-03-11 13:05:23 +00:00
// needs to be > uint16 because it's multiplying by a uint16
for i := uint32(0); i < uint32(len(volumeBitmap))*8; i++ {
if checkFreeBlockInVolumeBitmap(volumeBitmap, uint16(i)) {
blocks[blocksFound] = uint16(i)
2021-06-26 01:15:20 +00:00
blocksFound++
if blocksFound == numberOfBlocks {
return blocks
}
}
}
2021-06-07 00:15:41 +00:00
return nil
}
2021-06-09 12:23:18 +00:00
2024-03-11 13:05:23 +00:00
func markBlockInVolumeBitmap(volumeBitmap []byte, blockNumber uint16) {
2021-06-09 12:23:18 +00:00
bitToChange := blockNumber % 8
byteToChange := blockNumber / 8
2024-03-11 13:05:23 +00:00
byteToAnd := (uint8(0b10000000) >> uint8(bitToChange)) ^ 0b11111111
2021-06-09 12:23:18 +00:00
2021-06-12 02:43:35 +00:00
volumeBitmap[byteToChange] &= byte(byteToAnd)
}
2024-03-11 13:05:23 +00:00
func freeBlockInVolumeBitmap(volumeBitmap []byte, blockNumber uint16) {
2021-06-12 02:43:35 +00:00
bitToChange := blockNumber % 8
byteToChange := blockNumber / 8
2024-03-11 13:05:23 +00:00
byteToOr := uint8(0b10000000) >> uint8(bitToChange)
2021-06-12 02:43:35 +00:00
volumeBitmap[byteToChange] |= byte(byteToOr)
2021-06-09 12:23:18 +00:00
}
2021-06-26 01:15:20 +00:00
2024-03-11 13:05:23 +00:00
func checkFreeBlockInVolumeBitmap(volumeBitmap []byte, blockNumber uint16) bool {
2021-06-26 01:15:20 +00:00
bitToCheck := blockNumber % 8
byteToCheck := blockNumber / 8
2024-03-11 13:05:23 +00:00
byteToAnd := uint8(0b10000000) >> uint8(bitToCheck)
2021-06-26 01:15:20 +00:00
return (volumeBitmap[byteToCheck] & byte(byteToAnd)) > 0
}