summaryrefslogtreecommitdiff
path: root/fig-emulator-gb/src/Fig/Emulator/GB/Component/ROM.hs
blob: bfdc2fbcc0b5dc174d9c2279b92070105134fa7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Fig.Emulator.GB.Component.ROM
  ( compROM
  ) where

import Fig.Prelude
import Prelude (fromIntegral)

import qualified Data.Vector as V
import qualified Data.ByteString as BS

import Fig.Emulator.GB.Bus

newtype ROMError = ROMError Text
  deriving Show
instance Exception ROMError
instance Pretty ROMError where
  pretty (ROMError b) = mconcat
    [ "internal ROM error: "
    , b
    ]

-- | Initialize base ROM (no mapper) from a ByteString
compROM :: (MonadIO m, MonadThrow m) => ByteString -> Component m
compROM bs = Component
  { compState = V.fromList $ BS.unpack bs
  , compMatches = \a ->
      a >= start && a < end
  , compUpdate = \s _ -> pure s
  , compWrite = \s _ad _v ->
      pure s
      -- throwM . ROMError $ mconcat
      -- [ "tried to write to ROM at ", pretty ad
      -- ]
  , compRead = \s ad -> do
      let offset = fromIntegral . unAddr $ ad - start
      case s V.!? offset of
        Nothing -> throwM . ROMError $ mconcat
          [ "address ", pretty ad, " out of bounds"
          ]
        Just v -> pure v
  }
  where
    start = 0x0000
    -- end = 0x4000
    end = 0x8000