From 2ec68d6fd5d71af6c5b5be7fb21beb91f6ea901e Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Sun, 19 May 2019 09:31:12 +0200 Subject: [PATCH] added hex2dec test --- .../dk/camelot64/kickc/test/TestPrograms.java | 5 + src/test/kc/hex2dec.kc | 56 ++++ src/test/ref/hex2dec.asm | 282 ++++++++++++++++++ 3 files changed, 343 insertions(+) create mode 100644 src/test/kc/hex2dec.kc create mode 100644 src/test/ref/hex2dec.asm diff --git a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java index 0fc59583b..c4a10ab7a 100644 --- a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java +++ b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java @@ -64,6 +64,11 @@ public class TestPrograms { */ + @Test + public void testHex2Dec() throws IOException, URISyntaxException { + compileAndCompare("hex2dec"); + } + @Test public void testTernaryInference() throws IOException, URISyntaxException { compileAndCompare("ternary-inference"); diff --git a/src/test/kc/hex2dec.kc b/src/test/kc/hex2dec.kc new file mode 100644 index 000000000..d1d838356 --- /dev/null +++ b/src/test/kc/hex2dec.kc @@ -0,0 +1,56 @@ +// Testing hex to decimal conversion + +const unsigned char *control = 0xd011; +const unsigned char *raster = 0xd012; +const unsigned char *bordercol = 0xd020; + +void main() { + asm { sei } + cls(); + while(true) { + do { + unsigned char rst = (*control&0x80)|(*raster>>1); + } while (rst!=0x30); + unsigned char *screen = 0x0400; + *bordercol = 1; + utoa(00000, screen); (*bordercol)++; screen += 40; + utoa(01234, screen); (*bordercol)++; screen += 40; + utoa(05678, screen); (*bordercol)++; screen += 40; + utoa(09999, screen); (*bordercol)++; screen += 40; + utoa(59999, screen); + *bordercol = 0; + } + +} + +void cls() { + unsigned char *screen = 0x0400; + for( unsigned char *sc: screen..screen+999) *sc=' '; +} + +unsigned char[] DIGITS = "0123456789abcdef"; +const unsigned char RADIX_BINARY = 2; +const unsigned char RADIX_OCTAL = 8; +const unsigned char RADIX_DECIMAL = 10; +const unsigned char RADIX_HEX = 16; + + + +// simple 'utoa' without using multiply or divide +unsigned int append(unsigned char *dst, unsigned int value, unsigned int sub){ + *dst = '0'; + unsigned int sub3 = sub*2+sub; + while (value >= sub3){ *dst += 3; value -= sub3; } + while (value >= sub){ ++*dst; value -= sub; } + return value; +} + +void utoa(unsigned int value, unsigned char *dst){ + unsigned char bStarted = 0; + if (bStarted == 1 || value >= 10000){ value = append(dst++, value, 10000); bStarted = 1; } + if (bStarted == 1 || value >= 1000){ value = append(dst++, value, 1000); bStarted = 1; } + if (bStarted == 1 || value >= 100){ value = append(dst++, value, 100); bStarted = 1; } + if (bStarted == 1 || value >= 10){ value = append(dst++, value, 10); bStarted = 1; } + *dst++ = '0' + (unsigned char) value; + *dst = 0; +} diff --git a/src/test/ref/hex2dec.asm b/src/test/ref/hex2dec.asm new file mode 100644 index 000000000..b080e5c44 --- /dev/null +++ b/src/test/ref/hex2dec.asm @@ -0,0 +1,282 @@ +// Testing hex to decimal conversion +.pc = $801 "Basic" +:BasicUpstart(main) +.pc = $80d "Program" + .label control = $d011 + .label raster = $d012 + .label bordercol = $d020 +main: { + .label _1 = 8 + sei + jsr cls + b1: + lda #$80 + and control + sta _1 + lda raster + lsr + ora _1 + cmp #$30 + bne b1 + lda #1 + sta bordercol + lda #<$400 + sta utoa.dst + lda #>$400 + sta utoa.dst+1 + lda #0 + sta utoa.value + sta utoa.value+1 + jsr utoa + inc bordercol + lda #<$400+$28 + sta utoa.dst + lda #>$400+$28 + sta utoa.dst+1 + lda #<$4d2 + sta utoa.value + lda #>$4d2 + sta utoa.value+1 + jsr utoa + inc bordercol + lda #<$400+$28+$28 + sta utoa.dst + lda #>$400+$28+$28 + sta utoa.dst+1 + lda #<$162e + sta utoa.value + lda #>$162e + sta utoa.value+1 + jsr utoa + inc bordercol + lda #<$400+$28+$28+$28 + sta utoa.dst + lda #>$400+$28+$28+$28 + sta utoa.dst+1 + lda #<$270f + sta utoa.value + lda #>$270f + sta utoa.value+1 + jsr utoa + inc bordercol + lda #<$400+$28+$28+$28+$28 + sta utoa.dst + lda #>$400+$28+$28+$28+$28 + sta utoa.dst+1 + lda #<$ea5f + sta utoa.value + lda #>$ea5f + sta utoa.value+1 + jsr utoa + lda #0 + sta bordercol + jmp b1 +} +// utoa(word zeropage(2) value, byte* zeropage(4) dst) +utoa: { + .label value = 2 + .label dst = 4 + lda value+1 + cmp #>$2710 + bcc !+ + beq !b5+ + jmp b5 + !b5: + lda value + cmp #<$2710 + bcc !b5+ + jmp b5 + !b5: + !: + ldx #0 + b1: + cpx #1 + beq b6 + lda value+1 + cmp #>$3e8 + bcc !+ + bne b6 + lda value + cmp #<$3e8 + bcs b6 + !: + b2: + cpx #1 + beq b7 + lda value+1 + cmp #>$64 + bcc !+ + bne b7 + lda value + cmp #<$64 + bcs b7 + !: + b3: + cpx #1 + beq b8 + lda value+1 + cmp #>$a + bcc !+ + bne b8 + lda value + cmp #<$a + bcs b8 + !: + b4: + lda value + clc + adc #'0' + ldy #0 + sta (dst),y + inc dst + bne !+ + inc dst+1 + !: + lda #0 + tay + sta (dst),y + rts + b8: + lda #$a + sta append.sub + lda #0 + sta append.sub+1 + jsr append + inc dst + bne !+ + inc dst+1 + !: + jmp b4 + b7: + lda #$64 + sta append.sub + lda #0 + sta append.sub+1 + jsr append + inc dst + bne !+ + inc dst+1 + !: + ldx #1 + jmp b3 + b6: + lda #<$3e8 + sta append.sub + lda #>$3e8 + sta append.sub+1 + jsr append + inc dst + bne !+ + inc dst+1 + !: + ldx #1 + jmp b2 + b5: + lda #<$2710 + sta append.sub + lda #>$2710 + sta append.sub+1 + jsr append + inc dst + bne !+ + inc dst+1 + !: + ldx #1 + jmp b1 +} +// simple 'utoa' without using multiply or divide +// append(byte* zeropage(4) dst, word zeropage(2) value, word zeropage(6) sub) +append: { + .label _0 = 9 + .label sub3 = 9 + .label value = 2 + .label dst = 4 + .label return = 2 + .label sub = 6 + lda #'0' + ldy #0 + sta (dst),y + lda sub + asl + sta _0 + lda sub+1 + rol + sta _0+1 + lda sub3 + clc + adc sub + sta sub3 + lda sub3+1 + adc sub+1 + sta sub3+1 + b1: + lda sub3+1 + cmp value+1 + bne !+ + lda sub3 + cmp value + !: + bcc b2 + beq b2 + b3: + lda sub+1 + cmp value+1 + bne !+ + lda sub + cmp value + !: + bcc b4 + beq b4 + rts + b4: + ldy #0 + lda (dst),y + clc + adc #1 + sta (dst),y + lda value + sec + sbc sub + sta value + lda value+1 + sbc sub+1 + sta value+1 + jmp b3 + b2: + lda #3 + clc + ldy #0 + adc (dst),y + sta (dst),y + lda value + sec + sbc sub3 + sta value + lda value+1 + sbc sub3+1 + sta value+1 + jmp b1 +} +cls: { + .label screen = $400 + .label sc = 2 + lda #screen + sta sc+1 + b1: + lda #' ' + ldy #0 + sta (sc),y + inc sc + bne !+ + inc sc+1 + !: + lda sc+1 + cmp #>screen+$3e7+1 + bne b1 + lda sc + cmp #