From 50390b0787e484520507baf3eb5302c36f3621ea Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Tue, 12 Dec 2017 16:04:59 +0000 Subject: [PATCH] Can `copy` a literal word to a word table. --- HISTORY.md | 1 + src/sixtypical/analyzer.py | 5 ++++- src/sixtypical/compiler.py | 16 ++++++++++++++++ tests/SixtyPical Analysis.md | 14 ++++++++++++++ tests/SixtyPical Compilation.md | 19 +++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index d542246..5b87b64 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -8,6 +8,7 @@ History of SixtyPical * The `forward` modifier can also be used to indicate that the symbol being copied in a `copy` to a vector is a routine that is defined further down in the source. * Initialized `word` memory locations. +* Can `copy` a literal word to a word table. * Fixed bug which was preventing `if` branches to diverge in what they initialized, if it was already initialized when going into the `if`. * Fixed a bug which was making it crash when trying to analyze `repeat forever` loops. diff --git a/src/sixtypical/analyzer.py b/src/sixtypical/analyzer.py index b678342..496a8f9 100644 --- a/src/sixtypical/analyzer.py +++ b/src/sixtypical/analyzer.py @@ -352,7 +352,7 @@ class Analyzer(object): else: raise TypeMismatchError((src, dest)) - elif isinstance(src, LocationRef) and isinstance(dest, IndexedRef): + elif isinstance(src, (LocationRef, ConstantRef)) and isinstance(dest, IndexedRef): if src.type == TYPE_WORD and dest.ref.type == TYPE_WORD_TABLE: pass else: @@ -391,6 +391,9 @@ class Analyzer(object): context.assert_meaningful(src, dest.ref, dest.index) context.set_touched(src) # TODO and dest.index? context.set_written(dest.ref) + elif isinstance(src, ConstantRef) and isinstance(dest, IndexedRef): + context.assert_meaningful(src, dest.ref, dest.index) + context.set_written(dest.ref) elif isinstance(src, IndexedRef) and isinstance(dest, LocationRef): context.assert_meaningful(src.ref, src.index, dest) context.set_touched(dest) # TODO and src.index? diff --git a/src/sixtypical/compiler.py b/src/sixtypical/compiler.py index 5a0952d..8a6feed 100644 --- a/src/sixtypical/compiler.py +++ b/src/sixtypical/compiler.py @@ -383,6 +383,22 @@ class Compiler(object): self.emitter.emit(STA(addressing_mode(Offset(dest_label, 256)))) else: raise NotImplementedError + elif isinstance(src, ConstantRef) and isinstance(dest, IndexedRef): + if src.type == TYPE_WORD and dest.ref.type == TYPE_WORD_TABLE: + dest_label = self.labels[dest.ref.name] + addressing_mode = None + if dest.index == REG_X: + addressing_mode = AbsoluteX + elif dest.index == REG_Y: + addressing_mode = AbsoluteY + else: + raise NotImplementedError(dest) + self.emitter.emit(LDA(Immediate(Byte(src.low_byte())))) + self.emitter.emit(STA(addressing_mode(dest_label))) + self.emitter.emit(LDA(Immediate(Byte(src.high_byte())))) + self.emitter.emit(STA(addressing_mode(Offset(dest_label, 256)))) + else: + raise NotImplementedError elif isinstance(src, IndexedRef) and isinstance(dest, LocationRef): if src.ref.type == TYPE_WORD_TABLE and dest.type == TYPE_WORD: src_label = self.labels[src.ref.name] diff --git a/tests/SixtyPical Analysis.md b/tests/SixtyPical Analysis.md index 5c8262b..5a9e1cb 100644 --- a/tests/SixtyPical Analysis.md +++ b/tests/SixtyPical Analysis.md @@ -388,6 +388,20 @@ Copying to and from a word table. | } ? TypeMismatchError +You can also copy a literal word to a word table. + + | word table many + | + | routine main + | inputs many + | outputs many + | trashes a, x, n, z + | { + | ld x, 0 + | copy 9999, many + x + | } + = ok + ### add ### Can't `add` from or to a memory location that isn't initialized. diff --git a/tests/SixtyPical Compilation.md b/tests/SixtyPical Compilation.md index ae21a7e..cc80189 100644 --- a/tests/SixtyPical Compilation.md +++ b/tests/SixtyPical Compilation.md @@ -434,6 +434,25 @@ Copy literal word to word. = $0814 STA $0819 = $0817 RTS +You can also copy a literal word to a word table. + + | word table many + | + | routine main + | inputs many + | outputs many + | trashes a, x, n, z + | { + | ld x, 0 + | copy 9999, many + x + | } + = $080D LDX #$00 + = $080F LDA #$0F + = $0811 STA $081A,X + = $0814 LDA #$27 + = $0816 STA $091A,X + = $0819 RTS + Copy vector to vector. | vector bar