Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for embedding byte sequences and binary files (images) #5

Merged
merged 1 commit into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Lazyboy/Control.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
module Lazyboy.Control where

import Control.Monad.Trans.RWS
import Data.Word
import Lazyboy.Types

-- | Execute an action within a global label and pass the action the label.
Expand All @@ -31,6 +32,23 @@ withLocalLabel block = do
tell [LABEL label]
block label

-- | Embed a file and return a (global) label for it.
embedFile :: FilePath -> Lazyboy Label
embedFile file = do
label <- Global <$> get
tell [LABEL label, INCLUDE file]
return label

-- | Embed an image and return a (global) label for it.
embedImage = embedFile

-- | Embed a sequence of bytes into the file and return a (global) label for it.
embedBytes :: [Word8] -> Lazyboy Label
embedBytes bytes = do
label <- Global <$> get
tell [LABEL label, BYTES bytes]
return label

-- | Suspend execution indefinitely by jumping infinitely.
freeze :: Lazyboy ()
freeze = loop $ return ()
Expand Down
3 changes: 1 addition & 2 deletions src/Lazyboy/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,4 @@ memcpy src dest len = do
tell [LDrrnn HL src, LDrrnn DE dest, LDrn B len]
withLocalLabel $ \label -> do
tell [LDAHLI] -- load a byte from [HL] into A and increment
tell [LDrrA DE, INCrr DE, DECr B, JUMPif NonZero label]

tell [LDrrA DE, INCrr DE, DECr B, JUMPif NonZero label]
3 changes: 3 additions & 0 deletions src/Lazyboy/Target/ASM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Lazyboy.Target.ASM where
import Control.Monad.Trans.RWS.Lazy
import Data.Aeson
import Data.Char (toLower)
import Data.List (intercalate)
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy.IO as T
import Lazyboy.Types
Expand Down Expand Up @@ -191,6 +192,8 @@ instance Show Instruction where
show (LABEL l) = printf "%s:" l
show (JUMP l) = printf "jp %s" l
show (JUMPif c l) = printf "jp %s, %s" c l
show (INCLUDE file) = printf "INCBIN \"%s\"" file
show (BYTES bytes) = printf "db " ++ intercalate "," (map (printf "$%X") bytes)

show _ = error "Use of unimplemented instruction"

Expand Down
2 changes: 2 additions & 0 deletions src/Lazyboy/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,7 @@ data Instruction =
| LABEL Label -- create a numbered label
| JUMP Label -- jump to a label
| JUMPif Condition Label -- conditional jumping to a label
| INCLUDE FilePath -- include a file
| BYTES [Word8] -- define some bytes with a global label

deriving (Read, Eq)
2 changes: 1 addition & 1 deletion templates/bare.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ SECTION "main", ROM0[$0150]

main:
{{#body}}
{{.}}
{{{.}}}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this stops html entities being escaped in the output, which we don't want because we use quotation marks in file inclusion that would be rendered as "

{{/body}}
94 changes: 53 additions & 41 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,45 +79,57 @@ main = hspec $ do
, "ld HL, $C000"
, "ld [HL], 151"
]
describe "embedImage" $ do
it "leverages RGBASM to include a binary" $ do
let program = execLazyboy $ embedImage "test.bin"
program `shouldBe` [LABEL $ Global 1, INCLUDE "test.bin"]
describe "embedBytes" $ do
it "defines a raw sequence of bytes" $ do
let program = execLazyboy $ embedBytes [0x00, 0x01, 0x02]
program `shouldBe` [LABEL $ Global 1, BYTES [0x00, 0x01, 0x02]]

describe "Prelude.show" $ do
it "disallows loading [AF] into A" $ do
disallow (show $ LDArr AF)
it "disallows loading [SP] into A" $ do
disallow (show $ LDArr SP)
it "disallows loading [PC] into A" $ do
disallow (show $ LDArr PC)
it "disallows loading A into [AF]" $ do
disallow (show $ LDrrA AF)
it "disallows loading A into [SP]" $ do
disallow (show $ LDrrA SP)
it "disallows loading A into [PC]" $ do
disallow (show $ LDrrA PC)
it "disallows loading a 16 bit value into AF" $ do
disallow (show $ LDrrnn AF 0x00)
it "disallows loading a 16 bit value into PC" $ do
disallow (show $ LDrrnn PC 0x00)
it "disallows pushing stack pointer" $ do
disallow (show $ PUSH SP)
it "disallows pushing program counter" $ do
disallow (show $ PUSH PC)
it "disallows popping stack pointer" $ do
disallow (show $ POP SP)
it "disallows popping program counter" $ do
disallow (show $ POP PC)
it "disallows an invalid RST vector value" $ do
disallow (show $ RST 0x02)
it "disallows adding AF to HL" $ do
disallow (show $ ADDHLrr AF)
it "disallows adding PC to HL" $ do
disallow (show $ ADDHLrr PC)
it "disallows incrementing AF" $ do
disallow (show $ INCrr AF)
it "disallows incrementing PC" $ do
disallow (show $ INCrr PC)
it "disallows decrementing AF" $ do
disallow (show $ DECrr AF)
it "disallows decrementing PC" $ do
disallow (show $ DECrr PC)
it "enforces only 3-bit values can be passed to BIT instructions" $ do
disallow (show $ BITnr 0x80 A)
describe "Lazyboy.Target.ASM" $ do
describe "show" $ do
it "disallows loading [AF] into A" $ do
disallow (show $ LDArr AF)
it "disallows loading [SP] into A" $ do
disallow (show $ LDArr SP)
it "disallows loading [PC] into A" $ do
disallow (show $ LDArr PC)
it "disallows loading A into [AF]" $ do
disallow (show $ LDrrA AF)
it "disallows loading A into [SP]" $ do
disallow (show $ LDrrA SP)
it "disallows loading A into [PC]" $ do
disallow (show $ LDrrA PC)
it "disallows loading a 16 bit value into AF" $ do
disallow (show $ LDrrnn AF 0x00)
it "disallows loading a 16 bit value into PC" $ do
disallow (show $ LDrrnn PC 0x00)
it "disallows pushing stack pointer" $ do
disallow (show $ PUSH SP)
it "disallows pushing program counter" $ do
disallow (show $ PUSH PC)
it "disallows popping stack pointer" $ do
disallow (show $ POP SP)
it "disallows popping program counter" $ do
disallow (show $ POP PC)
it "disallows an invalid RST vector value" $ do
disallow (show $ RST 0x02)
it "disallows adding AF to HL" $ do
disallow (show $ ADDHLrr AF)
it "disallows adding PC to HL" $ do
disallow (show $ ADDHLrr PC)
it "disallows incrementing AF" $ do
disallow (show $ INCrr AF)
it "disallows incrementing PC" $ do
disallow (show $ INCrr PC)
it "disallows decrementing AF" $ do
disallow (show $ DECrr AF)
it "disallows decrementing PC" $ do
disallow (show $ DECrr PC)
it "enforces only 3-bit values can be passed to BIT instructions" $ do
disallow (show $ BITnr 0x80 A)
it "formats embedded byte sequences correctly" $ do
let program = map show $ execLazyboy $ tell [BYTES [97, 98]]
program `shouldBe` ["db $61,$62" ]