From a5b4849058157d166ec4b5463ccaec6b82d493d4 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 29 Dec 2017 14:17:51 +0100 Subject: [PATCH] fixes --- il65/codegen.py | 77 +++++++++++++++++++++++++++++++++++-------------- lib/c64lib.ill | 18 +++++++----- 2 files changed, 65 insertions(+), 30 deletions(-) diff --git a/il65/codegen.py b/il65/codegen.py index 5442264b4..127623700 100644 --- a/il65/codegen.py +++ b/il65/codegen.py @@ -406,15 +406,18 @@ class CodeGenerator: is_incr = isinstance(stmt, ParseResult.InplaceIncrStmt) if isinstance(stmt.what, ParseResult.RegisterValue): reg = stmt.what.register + # note: these operations below are all checked to be ok if is_incr: if reg == 'A': + # a += 1..255 self.p("\t\tclc") self.p("\t\tadc #{:d}".format(stmt.howmuch)) elif reg in REGISTER_BYTES: - # 8 bit incr if stmt.howmuch == 1: + # x/y += 1 self.p("\t\tin{:s}".format(reg.lower())) else: + # x/y += 2..255 self.p("\t\tpha") self.p("\t\tt{:s}a".format(reg.lower())) self.p("\t\tclc") @@ -422,33 +425,49 @@ class CodeGenerator: self.p("\t\tta{:s}".format(reg.lower())) self.p("\t\tpla") elif reg == "AX": + # AX += 1..255 self.p("\t\tclc") - self.p("\t\tadc #1") - self.p("\t\tbne +") + self.p("\t\tadc #{:d}".format(stmt.howmuch)) + self.p("\t\tbcc +") self.p("\t\tinx") self.p("+") elif reg == "AY": + # AY += 1..255 self.p("\t\tclc") - self.p("\t\tadc #1") - self.p("\t\tbne +") + self.p("\t\tadc #{:d}".format(stmt.howmuch)) + self.p("\t\tbcc +") self.p("\t\tiny") self.p("+") elif reg == "XY": - self.p("\t\tinx") - self.p("\t\tbne +") - self.p("\t\tiny") - self.p("+") + if stmt.howmuch == 1: + # XY += 1 + self.p("\t\tinx") + self.p("\t\tbne +") + self.p("\t\tiny") + self.p("+") + else: + # XY += 2..255 + self.p("\t\tpha") + self.p("\t\ttxa") + self.p("\t\tclc") + self.p("\t\tadc #{:d}".format(stmt.howmuch)) + self.p("\t\ttax") + self.p("\t\tbcc +") + self.p("\t\tiny") + self.p("+\t\tpla") else: raise CodeError("invalid incr register: " + reg) else: if reg == 'A': + # a -= 1..255 self.p("\t\tsec") self.p("\t\tsbc #{:d}".format(stmt.howmuch)) elif reg in REGISTER_BYTES: - # 8 bit decr if stmt.howmuch == 1: + # x/y -= 1 self.p("\t\tde{:s}".format(reg.lower())) else: + # x/y -= 2..255 self.p("\t\tpha") self.p("\t\tt{:s}a".format(reg.lower())) self.p("\t\tsec") @@ -456,22 +475,36 @@ class CodeGenerator: self.p("\t\tta{:s}".format(reg.lower())) self.p("\t\tpla") elif reg == "AX": - self.p("\t\tcmp #0") - self.p("\t\tbne +") + # AX -= 1..255 + self.p("\t\tsec") + self.p("\t\tsbc #{:d}".format(stmt.howmuch)) + self.p("\t\tbcs +") self.p("\t\tdex") - self.p("+\t\tsec") - self.p("\t\tsbc #1") + self.p("+") elif reg == "AY": - self.p("\t\tcmp #0") - self.p("\t\tbne +") + # AY -= 1..255 + self.p("\t\tsec") + self.p("\t\tsbc #{:d}".format(stmt.howmuch)) + self.p("\t\tbcs +") self.p("\t\tdey") - self.p("+\t\tsec") - self.p("\t\tsbc #1") + self.p("+") elif reg == "XY": - self.p("\t\tcpx #0") - self.p("\t\tbne +") - self.p("\t\tdey") - self.p("+\t\tdex") + if stmt.howmuch == 1: + # XY -= 1 + self.p("\t\tcpx #0") + self.p("\t\tbne +") + self.p("\t\tdey") + self.p("+\t\tdex") + else: + # XY -= 2..255 + self.p("\t\tpha") + self.p("\t\ttxa") + self.p("\t\tsec") + self.p("\t\tsbc #{:d}".format(stmt.howmuch)) + self.p("\t\ttax") + self.p("\t\tbcs +") + self.p("\t\tdey") + self.p("+\t\tpla") else: raise CodeError("invalid decr register: " + reg) elif isinstance(stmt.what, (ParseResult.MemMappedValue, ParseResult.IndirectValue)): diff --git a/lib/c64lib.ill b/lib/c64lib.ill index e26d9e554..63724ce63 100644 --- a/lib/c64lib.ill +++ b/lib/c64lib.ill @@ -540,15 +540,17 @@ sub print_byte_decimal (ubyte: A) -> (A?, X?, Y?) { asm { jsr byte2decimal pha - tya - cmp #'0' - beq + + cpy #'0' + bne _print_hundreds + cpx #'0' + bne _print_tens + pla + jmp c64.CHROUT +_print_hundreds tya jsr c64.CHROUT -+ txa - cmp #'0' - beq + +_print_tens txa jsr c64.CHROUT -+ pla + pla jmp c64.CHROUT } } @@ -561,7 +563,7 @@ sub print_byte_hex (prefix: SC, ubyte: A) -> (A?, X?, Y?) { lda #'$' jsr c64.CHROUT pla - + jsr byte2hex ++ jsr byte2hex txa jsr c64.CHROUT tya