From 4cef193da95f51b87222049fc102877b633f11e1 Mon Sep 17 00:00:00 2001 From: Cat's Eye Technologies Date: Tue, 1 Apr 2014 14:33:57 +0100 Subject: [PATCH] Parse more instructions. --- README.markdown | 29 ++++++++++++++++++++++++-- src/SixtyPical/Parser.hs | 45 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index 80f9767..2a4048a 100644 --- a/README.markdown +++ b/README.markdown @@ -70,11 +70,13 @@ An address knows if it is an address of a byte, of a word, or of a table. ### Blocks ### -Each routine is a block. It may be composed of inner blocks. +Each routine is a block. It may be composed of inner blocks, attached to +some instructions. SixtyPical does not have instructions that map literally to the 6502 branch instructions. Instead, each branch instruction has a corresponding -"if-then-else"-like construct with the same name as the branch. +"if-then-else"-like construct with the same name as the branch instruction. +These _test_ instructions each have two blocks, for the then and the else. The abstract states of the machine at each of the different block exits are merged during analysis. If any register or memory location is treated @@ -82,6 +84,23 @@ inconsistently (e.g. updated in one branch of the test, but not the other,) that register cannot subsequently be used without a declaration to the effect that we know what's going on. (This is all a bit fuzzy right now.) +There is also no `rts` instruction. It is included at the end of a routine, +but only when the routine is used as a subroutine. + +There are also _with_ instructions, which are associated with an opcode +that has a natural symmetrical opcode (e.g. `pha`, `sei`). These instructions +take a block. The natural symmetrical opcode is inserted at the end of the +block. + +TODO +---- + +* Parse HEX values like $40A3 +* Full machine model +* Check before analyzing +* Check, analyze, generate +* Addressing modes; rename instructions to match + Tests ----- @@ -113,7 +132,13 @@ A program may reserve and assign. | assign word screen 4000 | routine main { | lda screen + | tax + | tay | cmp score + | ldx score + | txa + | ldy score + | tya | } = True diff --git a/src/SixtyPical/Parser.hs b/src/SixtyPical/Parser.hs index 2efb2ed..cfa5327 100644 --- a/src/SixtyPical/Parser.hs +++ b/src/SixtyPical/Parser.hs @@ -15,8 +15,11 @@ Routine := "routine" RoutineName Block. Block := "{" {Command} "}". Command := "beq" Block "else" Block | "lda" (LocationName | Immediate) + | "ldx" (LocationName | Immediate) + | "ldy" (LocationName | Immediate) | "txa" | "tax" | "tya" | "tay" | "cmp" (LocationName | Immediate) + | "nop". -} @@ -70,7 +73,9 @@ block = do return cs command :: Parser Instruction -command = cmp <|> lda <|> beq <|> nop +command = cmp <|> (try lda) <|> (try ldx) <|> (try ldy) <|> + (try txa) <|> (try tax) <|> (try tya) <|> (try tay) <|> + beq <|> nop nop :: Parser Instruction nop = do @@ -92,6 +97,44 @@ lda = do l <- locationName return (LOAD A l) +ldx :: Parser Instruction +ldx = do + string "ldx" + spaces + l <- locationName + return (LOAD X l) + +ldy :: Parser Instruction +ldy = do + string "ldy" + spaces + l <- locationName + return (LOAD Y l) + +txa :: Parser Instruction +txa = do + string "txa" + spaces + return (COPY X A) + +tax :: Parser Instruction +tax = do + string "tax" + spaces + return (COPY A X) + +tya :: Parser Instruction +tya = do + string "tya" + spaces + return (COPY Y A) + +tay :: Parser Instruction +tay = do + string "tay" + spaces + return (COPY A Y) + beq :: Parser Instruction beq = do string "beq"