Skip to content

Instantly share code, notes, and snippets.

@agrif
Created April 5, 2013 00:25
Show Gist options
  • Select an option

  • Save agrif/5315638 to your computer and use it in GitHub Desktop.

Select an option

Save agrif/5315638 to your computer and use it in GitHub Desktop.
assemble :: Line -> AsmAct Word8 Word16 ()
assemble (Line labels Nothing) = sequence_ $ do
l <- labels
return $ (getPtr >>= setLabel l)
assemble (Line labels (Just (Instruction inst rawargs))) = do
assemble (Line labels Nothing)
args <- runListT $ do
arg <- ListT $ return rawargs
case arg of
Label l -> do
v <- lift $ getLabel l
return $ Constant $ fromIntegral v
_ -> return arg
case (inst, args) of
(Builtin "db", _) -> defineBytes args
(Builtin "defb", _) -> defineBytes args
(Builtin "dm", _) -> defineBytes args
(Builtin "defm", _) -> defineBytes args
(Builtin "adc", [Register "a", Constant x]) ->
if fitsByte x
then write [0xCE, fromIntegral x]
else throwError "adc cannot handle constants larger than a byte"
(Builtin "adc", [Register "a", Indirect (Register "hl")]) -> write [0x8E]
(Builtin "adc", [Register "a", Register "a"]) -> write [0x8F]
(Builtin "adc", [Register "a", Register "b"]) -> write [0x88]
(Builtin "adc", [Register "a", Register "c"]) -> write [0x89]
(Builtin "adc", [Register "a", Register "d"]) -> write [0x8A]
(Builtin "adc", [Register "a", Register "e"]) -> write [0x8B]
(Builtin "adc", [Register "a", Register "h"]) -> write [0x8C]
(Builtin "adc", [Register "a", Register "l"]) -> write [0x8D]
(Builtin "add", [Register "a", Constant x]) ->
if fitsByte x
then write [0xC6, fromIntegral x]
else throwError "adc cannot handle constants larger than a byte"
(Builtin "add", [Register "a", Indirect (Register "hl")]) -> write [0x86]
(Builtin "add", [Register "a", Register "a"]) -> write [0x87]
(Builtin "add", [Register "a", Register "b"]) -> write [0x80]
(Builtin "add", [Register "a", Register "c"]) -> write [0x81]
(Builtin "add", [Register "a", Register "d"]) -> write [0x82]
(Builtin "add", [Register "a", Register "e"]) -> write [0x83]
(Builtin "add", [Register "a", Register "h"]) -> write [0x84]
(Builtin "add", [Register "a", Register "l"]) -> write [0x85]
(Builtin "add", [Register "hl", Register "bc"]) -> write [0x09]
(Builtin "add", [Register "hl", Register "de"]) -> write [0x19]
(Builtin "add", [Register "hl", Register "hl"]) -> write [0x29]
(Builtin "add", [Register "hl", Register "sp"]) -> write [0x39]
(Builtin "add", [Register "sp", Constant x]) ->
if fitsByte x
then write [0xE8, fromIntegral x]
else throwError "add cannot handle constants larger than a byte"
(Builtin "and", [Constant x]) ->
if fitsByte x
then write [0xE6, fromIntegral x]
else throwError "and cannot handle constants larger than a byte"
(Builtin "and", [Indirect (Register "hl")]) -> write [0xA6]
(Builtin "and", [Register "a"]) -> write [0xA7]
(Builtin "and", [Register "b"]) -> write [0xA0]
(Builtin "and", [Register "c"]) -> write [0xA1]
(Builtin "and", [Register "d"]) -> write [0xA2]
(Builtin "and", [Register "e"]) -> write [0xA3]
(Builtin "and", [Register "h"]) -> write [0xA4]
(Builtin "and", [Register "l"]) -> write [0xA5]
(Macro name, _) -> throwError $ "invalid instruction " ++ name ++ " with args " ++ (show args)
(Builtin name, _) -> throwError $ "invalid instruction " ++ name ++ " with args " ++ (show args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment