Skip to content

Commit

Permalink
better sytle.
Browse files Browse the repository at this point in the history
  • Loading branch information
lialan committed Jul 21, 2014
1 parent 1ccd18d commit 243d35f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
17 changes: 13 additions & 4 deletions src/Chip8/Opcodes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ import Debug.Trace

import Chip8.State (VMState(..))

xSize :: VMState -> Word8
xSize s@VMState { extended = e} =
if e then 128 else 64

ySize :: VMState -> Word8
ySize s@VMState { extended = e} =
if e then 64 else 32


runInstruction :: VMState -- ^ Initial CPU state
-> Word16 -- ^ Full CPU instruction
-> VMState -- ^ Resulting CPU state
Expand Down Expand Up @@ -117,7 +126,7 @@ op00EE s@VMState { stack = stack } _ =
op00FB :: VMState
-> Word16
-> VMState
op00FB s _ = shiftScreen s 4 0
op00FB s i = shiftScreen s 4 0

-- | Scroll display 4 pixels left
op00FC :: VMState
Expand Down Expand Up @@ -519,7 +528,7 @@ drawSprite s@VMState { display = display, extended = e } x y addr n =
-- Adds the x,y offset to the relative pixel co-ordinate
addOffset (sx, sy) = (sx + x, sy + y)
-- Checks if the pixel co-ordinate is within the display bounds
inBounds (x, y) = if e then (x >= 0 && x < 128) && (y >= 0 && y < 64) else (x >= 0 && x < 64) && (y >= 0 && y < 32)
inBounds (x, y) = (x >= 0 && x < xSize s) && (y >= 0 && y < ySize s)
-- Gets the sprite's relative pixels, adds offsets and filters out of bounds
sprite = filter inBounds $ map addOffset $ getSprite s addr n
-- Folding func to turn sprite in to updated pixels and flag on collision
Expand All @@ -537,14 +546,14 @@ screenCoordinates s@VMState { extended = e } offx offy =
| x <- [0..127],
y <- [0..63]]
where
inBounds ((x, y), z) = if e then (x >= 0 && x < 128) && (y >= 0 && y < 64) else (x >= 0 && x < 64) && (y >= 0 && y < 32)
inBounds ((x, y), z) = (x >= 0 && x < xSize s) && (y >= 0 && y < ySize s)

shiftScreen :: VMState
-> Word8 -- ^ x coordinate offset
-> Word8 -- ^ y coordinate offset
-> VMState
shiftScreen s@VMState {display = display } offx offy =
s { display = listArray ((0,0),(127,63)) (repeat False) // display' }
s { display = listArray ((0,0),(xSize s - 1, ySize s - 1)) (repeat False) // display' }
where
display' = screenCoordinates s offx offy

Expand Down
12 changes: 1 addition & 11 deletions src/Chip8/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Chip8.State
VMState(..)
, create
, nextInstruction
, showDisplay
) where

import Data.Word
Expand Down Expand Up @@ -40,7 +39,7 @@ create p g = VMState { memory = listArray (0x0, 0xFFF) memContents
, i = 0x0
, v = listArray (0x0, 0xF) []
, stack = []
, display = listArray ((0,0),(128,64)) (repeat False)
, display = listArray ((0,0),(127,63)) (repeat False)
, pressed = S.empty
, delayTimer = 0
, waitForKeypress = Nothing
Expand Down Expand Up @@ -92,12 +91,3 @@ nextInstruction VMState { pc = pc, memory = memory } =
where
b1 = fromIntegral $ memory ! pc -- First byte in instruction
b2 = fromIntegral $ memory ! (pc + 1) -- Second byte in instruction

-- | Returns a string representation of a VM state's display
showDisplay :: VMState -- ^ The VM state
-> String -- ^ A string showing the display contents using ascii
showDisplay s =
unlines [unwords [toPixel (display s ! (x, y)) | x <- [0..63]] | y <- [0..31]]
where
toPixel True = ""
toPixel False = " "
4 changes: 2 additions & 2 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import Graphics.Gloss.Data.Color (black, white)
import Graphics.Gloss.Data.Display
import Graphics.Gloss.Interface.Pure.Game

import Chip8.State (VMState(..), create, nextInstruction, showDisplay)
import Chip8.State (VMState(..), create, nextInstruction)
import Chip8.Opcodes (runInstruction)

cpuFrequency :: Int
cpuFrequency = 10
cpuFrequency = 100

screenFrequency :: Int
screenFrequency = 1
Expand Down

0 comments on commit 243d35f

Please sign in to comment.