From 213a6bff5ef01e39f52c439b0fb29f1614da4d71 Mon Sep 17 00:00:00 2001 From: pingu Date: Mon, 30 Mar 2026 14:18:12 +0200 Subject: [PATCH] hl and bc as lenses --- src/GB/CPU.hs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/GB/CPU.hs b/src/GB/CPU.hs index 1205a8c..b68bc24 100644 --- a/src/GB/CPU.hs +++ b/src/GB/CPU.hs @@ -61,17 +61,13 @@ data Registers = Registers { _a :: Word8 makeLenses ''Registers -getBC :: Registers -> Word16 -getBC r = (( .<<. 8) . fromIntegral $ r ^. b) + (fromIntegral $ r ^. c) +bc :: Lens' Registers Word16 +bc = lens (\r -> (( .<<. 8) . fromIntegral $ r ^. b) + (fromIntegral $ r ^. c)) + (\r w -> r & b %~ (const $ fromIntegral $ w .>>. 8) & c %~ (const $ fromIntegral $ w .&. 255)) -setBC :: Registers -> Word16 -> Registers -setBC r w = r & b %~ (const $ fromIntegral $ w .>>. 8) & c %~ (const $ fromIntegral $ w .&. 255) - -getHL :: Registers -> Word16 -getHL r = ((.<<. 8) . fromIntegral $ r ^. h) + (fromIntegral $ r ^. l) - -setHL :: Registers -> Word16 -> Registers -setHL r w = r & h %~ (const $ fromIntegral $ w .>>. 8) & l %~ (const $ fromIntegral $ w .&. 255) +hl :: Lens' Registers Word16 +hl = lens (\r -> ((.<<. 8) . fromIntegral $ r ^. h) + (fromIntegral $ r ^. l)) + (\r w -> r & h %~ (const $ fromIntegral $ w .>>. 8) & l %~ (const $ fromIntegral $ w .&. 255)) data CPU = CPU { _registers :: Registers , _pc :: Word16