module Sound.MED.Raw.BlockInfo where

import qualified Sound.MED.Raw.BlockCmdPageTable as BlockCmdPageTable
import Sound.MED.Raw.BlockCmdPageTable(BlockCmdPageTable)

import Sound.MED.Basic.Amiga

data BlockInfo = BlockInfo
  { hlmask       :: Maybe [ ULONG ]
  , blockname    :: Maybe [ UBYTE ]
  , blocknamelen :: ULONG
  , pagetable    :: Maybe BlockCmdPageTable
  , reserved     :: [ ULONG ]
  }
  deriving (Show)

peek :: UWORD -> UWORD -> MEM -> PTR -> IO BlockInfo
peek tracks numlines m p = do
  let hlmasklen = (numlines + 31) `div` 32
  hlmask'''     <- peekPTR m p
  hlmask'       <-
    skipIf (hlmask''' == 0) $
    mapM (peekULONG m) $ pointerRangeGen hlmask''' 4 hlmasklen
  blocknamelen' <- peekULONG m (p+8)
  blockname'''  <- peekPTR m (p+4)
  blockname'    <-
    skipIf (blockname''' == 0) $
    mapM (peekUBYTE m) $ pointerRangeGen blockname''' 1 blocknamelen'
  pagetable'''  <- peekPTR m (p+12)
  pagetable'    <- BlockCmdPageTable.peek tracks numlines m $? pagetable'''
  reserved'     <- mapM (peekULONG m) $ pointerRange (p+16) 4 5
  return $ BlockInfo
    hlmask' blockname' blocknamelen' pagetable' reserved'