From 054619d2fb309c662b3babb5df699619cc6e9dd8 Mon Sep 17 00:00:00 2001 From: Lucian Mogosanu Date: Sat, 13 Dec 2014 22:45:22 +0200 Subject: [PATCH] ZXS: Add fetcher --- src/ZXS/Fetch.hs | 22 ++++++++++++++++++++++ src/ZXS/Machine.hs | 11 ++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/ZXS/Fetch.hs diff --git a/src/ZXS/Fetch.hs b/src/ZXS/Fetch.hs new file mode 100644 index 0000000..d1889e0 --- /dev/null +++ b/src/ZXS/Fetch.hs @@ -0,0 +1,22 @@ +module ZXS.Fetch where + +-- opcode fetcher, with some side effects in the CPU + +import Data.Word +import Control.Monad.IO.Class (liftIO) + +import Z80.CPU +import Z80.Microcode +import ZXS.Machine +import ZXS.RAM + +fetch :: ZXS Word8 +fetch = do + Spectrum cpu mainMem <- getZXState + -- get pc + let pc = getPC cpu + -- getch byte + b <- liftIO $ ramGetByte mainMem pc + -- update pc + modifyCPU $ flip modifyPC (+ 1) + return b diff --git a/src/ZXS/Machine.hs b/src/ZXS/Machine.hs index f43cd26..1130470 100644 --- a/src/ZXS/Machine.hs +++ b/src/ZXS/Machine.hs @@ -16,7 +16,16 @@ data Spectrum = Spectrum -- the ZXS transformer newtype ZXST m a = ZXST { unZXST :: StateT Spectrum m a } - deriving (Monad, Functor, Applicative, MonadIO) + deriving (MonadState Spectrum, Monad, Functor, Applicative, MonadIO) -- for now we "fall back" to IO as the default inner monad type ZXS a = ZXST IO a + +modifyCPU :: (CPU -> CPU) -> ZXS () +modifyCPU f = modify $ \ spc -> spc { zxCPU = f $ zxCPU spc } + +modifyMainMem :: (RAM -> RAM) -> ZXS () +modifyMainMem f = modify $ \ spc -> spc { zxMainMem = f $ zxMainMem spc } + +getZXState :: ZXS Spectrum +getZXState = get -- 1.7.10.4