-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Large amount of refactoring to make CPU use State monad and lenses.
- Loading branch information
Showing
29 changed files
with
870 additions
and
979 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,41 @@ | ||
module Chip8 | ||
( emulateCpuCycle, | ||
initializeChip8, | ||
loadFontset, | ||
loadGameByFilePath, | ||
loadFontsetIntoMemory, | ||
loadGameIntoMemory | ||
) where | ||
|
||
import System.Random | ||
import Data.Word | ||
import Data.ByteString as BS | ||
import qualified Data.Vector as V | ||
import Control.Monad.State | ||
import Control.Lens | ||
|
||
import Constants | ||
import Cpu | ||
import Types | ||
|
||
emulateCpuCycle :: Chip8 -> Chip8 | ||
emulateCpuCycle = decrementSoundTimer . decrementDelayTimer . executeOpcode | ||
|
||
initializeChip8 :: IO Chip8 | ||
initializeChip8 = do | ||
newSeed <- newStdGen | ||
let emptyMemory = memory chip8InitialState | ||
let updatedMemory = loadFontset emptyMemory | ||
return chip8InitialState { memory = updatedMemory, randomNumberSeed = newSeed } | ||
|
||
loadFontset :: V.Vector Word8 -> V.Vector Word8 | ||
loadFontset givenMemory = V.update givenMemory fontsetAddresses | ||
where | ||
fontsetVector = V.fromList chip8Fontset | ||
fontsetAddresses = V.imap (\currentIndex fontsetByte -> (currentIndex, fontsetByte)) fontsetVector | ||
|
||
loadGameByFilePath :: String -> Chip8 -> IO Chip8 | ||
loadGameByFilePath filePath chip8State = do | ||
contents <- BS.readFile filePath | ||
let game = unpack contents | ||
let initialMemory = memory chip8State | ||
let updatedMemory = loadGameIntoMemory initialMemory game | ||
return chip8State { memory = updatedMemory } | ||
|
||
loadGameIntoMemory :: V.Vector Word8 -> [Word8] -> V.Vector Word8 | ||
loadGameIntoMemory givenMemory game = V.update givenMemory gameAddresses | ||
where | ||
gameVector = V.fromList game | ||
gameAddresses = V.imap (\currentIndex gameByte -> (currentIndex + 0x200, gameByte)) gameVector | ||
|
||
|
||
initializeChip8 :: StdGen -> Chip8 () | ||
initializeChip8 newSeed = do | ||
modify (\givenState -> givenState & randomNumberSeed .~ newSeed) | ||
loadFontsetIntoMemory | ||
|
||
loadFontsetIntoMemory :: Chip8 () | ||
loadFontsetIntoMemory = do | ||
let fontsetVector = V.fromList chip8Fontset | ||
fontsetAddresses = V.imap (\currentIndex fontsetByte -> (currentIndex, fontsetByte)) fontsetVector | ||
loadFontset = flip V.update fontsetAddresses | ||
modify (\givenState -> givenState & memory %~ loadFontset) | ||
|
||
loadGameIntoMemory :: [Word8] -> Chip8 () | ||
loadGameIntoMemory game = do | ||
let gameVector = V.fromList $ game | ||
gameAddresses = V.imap (\currentIndex gameByte -> (currentIndex + 0x200, gameByte)) gameVector | ||
loadGame = flip V.update gameAddresses | ||
modify (\givenState -> givenState & memory %~ loadGame) | ||
|
||
emulateCpuCycle :: Chip8 () | ||
emulateCpuCycle = do | ||
executeOpcode | ||
decrementDelayTimer | ||
decrementSoundTimer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.