From 061a1661ddc54cd8722d7b02ae7be7b96097e990 Mon Sep 17 00:00:00 2001 From: Cat's Eye Technologies Date: Wed, 2 Apr 2014 17:48:44 +0100 Subject: [PATCH] Indexed indirect. For a relaxing demo. --- README.markdown | 13 ++++++++ eg/demo.60pical | 65 +++++++++++++++++++++++++++++++++++++-- src/SixtyPical/Emitter.hs | 2 ++ src/SixtyPical/Model.hs | 1 + src/SixtyPical/Parser.hs | 43 +++++++++++++++++++++++--- 5 files changed, 117 insertions(+), 7 deletions(-) diff --git a/README.markdown b/README.markdown index 7968c24..b32f952 100644 --- a/README.markdown +++ b/README.markdown @@ -313,6 +313,17 @@ A comment may appear after each command. | } = True +A comment may appear after each declaration. + + | reserve byte lives ; fnord + | assign byte gdcol 647 ; fnord + | external blastoff 4 ; fnnnnnnnnnnnnnnnnfffffffff + | + | routine main { + | nop + | } + = True + A program may `reserve` and `assign`. | reserve byte lives @@ -486,6 +497,7 @@ We cannot absolute-indexed a word. | sta screen | sta screen, x | sta screen, y + | sta (screen), y | dec screen | clc | cld @@ -530,6 +542,7 @@ We cannot absolute-indexed a word. = sta screen = sta screen, x = sta screen, y + = sta (screen), y = dec screen = clc = cld diff --git a/eg/demo.60pical b/eg/demo.60pical index 9f9c072..2c74896 100644 --- a/eg/demo.60pical +++ b/eg/demo.60pical @@ -1,4 +1,4 @@ -assign byte table screen 1024 +assign byte table screen $0400 assign byte table screen2 1274 assign byte table screen3 1524 assign byte table screen4 1774 @@ -14,11 +14,24 @@ assign byte table vic_bg 53281 assign vector cinv 788 reserve vector save_cinv +; TODO: this should be a word + +assign byte pos_lo $fb +assign byte pos_hi $fc + +reserve byte value + +reserve byte m_hi +reserve byte m_lo +reserve byte n_hi +reserve byte n_lo + routine main { lda #5 sta vic_border lda #0 sta vic_bg + jsr reset_pos jsr clear_screen sei { copy vector cinv to save_cinv @@ -28,11 +41,59 @@ routine main { repeat bcc { } } +routine reset_pos { + lda #$00 + sta pos_lo + lda #$04 + sta pos_hi +} + routine our_cinv { - inc screen + lda value + inc value + ldy #0 + sta (pos_lo), y + jsr increment_pos + jsr compare_pos + if beq { + jsr reset_pos + } else { + } jmp save_cinv } +routine increment_pos { + clc + lda pos_lo + adc #1 + sta pos_lo + lda pos_hi + adc #0 + sta pos_hi +} + +routine compare_pos { + lda pos_lo + sta m_lo + lda pos_hi + sta m_hi + lda #$07 + sta n_hi + lda #$e8 + sta n_lo + jsr compare_16_bit +} + +routine compare_16_bit { + lda m_hi + cmp n_hi + if beq { + lda m_lo + cmp n_lo + } else { + } +} + routine clear_screen { ldy #0 repeat bne { diff --git a/src/SixtyPical/Emitter.hs b/src/SixtyPical/Emitter.hs index bfaa00d..2f1290c 100644 --- a/src/SixtyPical/Emitter.hs +++ b/src/SixtyPical/Emitter.hs @@ -60,6 +60,8 @@ emitInstr p r (COPYINDEXED A (NamedLocation label) Y) = "sta " ++ label ++ ", y" emitInstr p r (COPYINDEXED (NamedLocation label) A X) = "lda " ++ label ++ ", x" emitInstr p r (COPYINDEXED (NamedLocation label) A Y) = "lda " ++ label ++ ", y" +emitInstr p r (COPYINDIRECTINDEXED A (NamedLocation label) Y) = "sta (" ++ label ++ "), y" + emitInstr p r (CMP A (NamedLocation label)) = "cmp " ++ label emitInstr p r (CMP X (NamedLocation label)) = "cpx " ++ label emitInstr p r (CMP Y (NamedLocation label)) = "cpy " ++ label diff --git a/src/SixtyPical/Model.hs b/src/SixtyPical/Model.hs index 09f6f0a..b23781b 100644 --- a/src/SixtyPical/Model.hs +++ b/src/SixtyPical/Model.hs @@ -57,6 +57,7 @@ data Branch = BCC | BCS | BEQ | BMI | BNE | BPL | BVC | BVS data Instruction = PUT StorageLocation DataValue | COPY StorageLocation StorageLocation | COPYINDEXED StorageLocation StorageLocation StorageLocation + | COPYINDIRECTINDEXED StorageLocation StorageLocation StorageLocation | CMPIMM StorageLocation DataValue | CMP StorageLocation StorageLocation | ADDIMM StorageLocation DataValue diff --git a/src/SixtyPical/Parser.hs b/src/SixtyPical/Parser.hs index 8c14949..680ce86 100644 --- a/src/SixtyPical/Parser.hs +++ b/src/SixtyPical/Parser.hs @@ -10,7 +10,7 @@ import SixtyPical.Model {- -Toplevel := {Decl} {Routine}. +Toplevel := {Decl [Comment]} {Routine}. Decl := "reserve" StorageType LocationName | "assign" StorageType LocationName Address | "external" RoutineName Address. @@ -37,10 +37,16 @@ Branch := "bcc" | "bcs" | "beq" | "bmi" | "bne" | "bpl" | "bvc" | "bvs". toplevel :: Parser Program toplevel = do - decls <- many (try assign <|> try reserve <|> try external) + decls <- many decl routines <- many routine return $ Program decls routines +decl :: Parser Decl +decl = do + d <- (try assign <|> try reserve <|> try external) + optional comment + return d + reserve :: Parser Decl reserve = do string "reserve" @@ -126,6 +132,32 @@ index = do "x" -> X "y" -> Y +data Directness = Direct LocationName + | Indirect LocationName + deriving (Ord, Show, Eq) + +indirect_location :: Parser Directness +indirect_location = do + string "(" + spaces + l <- locationName + string ")" + spaces + return $ Indirect l + +direct_location :: Parser Directness +direct_location = do + l <- locationName + return $ Direct l + +directness_location = (try indirect_location) <|> direct_location + +indirect_indexed :: (Directness -> [StorageLocation] -> Instruction) -> Parser Instruction +indirect_indexed f = do + d <- directness_location + indexes <- many index + return $ f d indexes + absolute_indexed :: (LocationName -> [StorageLocation] -> Instruction) -> Parser Instruction absolute_indexed f = do l <- locationName @@ -305,10 +337,11 @@ sta :: Parser Instruction sta = do string "sta" spaces - absolute_indexed gen + indirect_indexed gen where - gen l [] = COPY A (NamedLocation l) - gen l [reg] = COPYINDEXED A (NamedLocation l) reg + gen (Direct l) [] = COPY A (NamedLocation l) + gen (Direct l) [reg] = COPYINDEXED A (NamedLocation l) reg + gen (Indirect l) [reg] = COPYINDIRECTINDEXED A (NamedLocation l) reg stx :: Parser Instruction stx = do