Skip to content

Commit

Permalink
Added test rom/rom loading
Browse files Browse the repository at this point in the history
  • Loading branch information
rkoeninger committed Aug 15, 2017
1 parent 42acc64 commit d41e27a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion GameCom.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ library

executable GameCom
main-is: Main.hs
build-depends: base, data-default, GameCom, sdl2
build-depends: base, data-default, bytestring, directory, GameCom, sdl2
hs-source-dirs: src/main
default-language: Haskell2010

Expand Down
Binary file added roms/full_palette.nes
Binary file not shown.
20 changes: 17 additions & 3 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ module Main where

import Control.Concurrent (threadDelay)
import Control.Monad (forM_)
import qualified Data.ByteString as BS
import Data.Default (Default(..))
import Foreign.C.Types
import GameCom (step)
import Memory (MachineState(..), Color)
import PPU (getPixel)
import ROM (parseROM)
import SDL.Vect
import qualified SDL
import System.Directory (getCurrentDirectory)

width = 256
height = 240
scale = 4
frameDelayMs = 100
frameDelayMs = 0
endDelayMs = 5000000
romName = "full_palette"

draw :: MachineState -> SDL.Window -> IO ()
draw state window =
Expand All @@ -38,14 +42,24 @@ loop state window newFrame n = do
if newFrame
then draw state window
else return ()
if n `mod` 60 == 0
then putStrLn $ "Frames remaining: " ++ show n
else return ()
SDL.updateWindowSurface window
threadDelay frameDelayMs
let (newFrame', state') = step state
loop state' window newFrame' (n - 1)

fromRight (Right x) = x
fromRight (Left x) = error $ "ROM load failure: " ++ x

main :: IO ()
main = do
let state = def
pwd <- getCurrentDirectory
putStrLn $ "Working Directory: " ++ pwd
romBytes <- BS.readFile $ "roms/" ++ romName ++ ".nes"

let state = def { rom = fromRight $ parseROM romBytes }
SDL.initialize [SDL.InitVideo]

window <- SDL.createWindow
Expand All @@ -54,7 +68,7 @@ main = do
{ SDL.windowInitialSize = V2 (256 * scale) (240 * scale) }
SDL.showWindow window

loop state window True 1
loop state window True 10000

putStrLn "Clock stopped"
threadDelay endDelayMs
Expand Down

0 comments on commit d41e27a

Please sign in to comment.