From 3a0a7b0a89fd841edd5f25f79cdb877051d0e948 Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Tue, 9 Apr 2024 22:35:42 -0400 Subject: End-of-stream emulator WIP --- .../src/Fig/Emulator/GB/Component/ROM.hs | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 fig-emulator-gb/src/Fig/Emulator/GB/Component/ROM.hs (limited to 'fig-emulator-gb/src/Fig/Emulator/GB/Component/ROM.hs') diff --git a/fig-emulator-gb/src/Fig/Emulator/GB/Component/ROM.hs b/fig-emulator-gb/src/Fig/Emulator/GB/Component/ROM.hs new file mode 100644 index 0000000..b5ea24f --- /dev/null +++ b/fig-emulator-gb/src/Fig/Emulator/GB/Component/ROM.hs @@ -0,0 +1,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 = pure + , 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 -- cgit v1.2.3