From a7b751a9fd26f2ed6b1cb66af764ef32976f2386 Mon Sep 17 00:00:00 2001
From: jespergravgaard <jesper@balmangravgaard.dk>
Date: Mon, 14 May 2018 10:31:43 +0200
Subject: [PATCH] A bit more line anim - work in (early) progress.

---
 .../Pass0GenerateStatementSequence.java       |  4 +-
 .../dk/camelot64/kickc/test/kc/line-anim.kc   | 62 ++++++++++++++++++-
 2 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java b/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java
index 4bf162671..ef0972674 100644
--- a/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java
+++ b/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java
@@ -458,7 +458,7 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
          }
       }
 
-      // Add condition i<last+1 or i<last-1
+      // Add condition i!=last+1 or i!=last-1
       VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate();
       VariableRef tmpVarRef = tmpVar.getRef();
       Statement stmtTmpVar = new StatementAssignment(tmpVarRef, lValue.getRef(), Operators.NEQ, beyondLastVal, new StatementSource(ctx));
@@ -611,7 +611,7 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
 
    @Override
    public RValue visitExprArray(KickCParser.ExprArrayContext ctx) {
-      RValue array = (LValue) visit(ctx.expr(0));
+      RValue array = (RValue) visit(ctx.expr(0));
       RValue index = (RValue) visit(ctx.expr(1));
       return new PointerDereferenceIndexed(array, index);
    }
diff --git a/src/test/java/dk/camelot64/kickc/test/kc/line-anim.kc b/src/test/java/dk/camelot64/kickc/test/kc/line-anim.kc
index 5af807723..deba92d48 100644
--- a/src/test/java/dk/camelot64/kickc/test/kc/line-anim.kc
+++ b/src/test/java/dk/camelot64/kickc/test/kc/line-anim.kc
@@ -1,7 +1,13 @@
 // Animated lines drawn on a single color bitmap
+import "c64.kc"
+
+byte* BITMAP = $2000;
+byte* SCREEN = $400;
 
 // The number of points
 const byte SIZE = 4;
+// The delay between pixels
+const byte DELAY = 8;
 
 // The coordinates of the lines to animate
 word[4] x_start = { 10, 20, 30, 30 };
@@ -23,15 +29,65 @@ byte[4] delay;
 byte[4] frame;
 
 void main() {
+    *D011 = VIC_BMM|VIC_DEN|VIC_RSEL|3;
+    *VIC_MEMORY =  (byte)((((word)SCREEN&$3fff)/$40)|(((word)BITMAP&$3fff)/$400));
+    bitmap_init(BITMAP);
+    bitmap_clear();
     for( byte i=0; i!=8; i+=2) {
-        init(i);
+        point_init(i);
+        bitmap_plot(x_start[i], y_start[i>>1]);
+    }
+    while(true) {
+        (*BORDERCOL)++;
     }
-
 }
 
-void init(byte point_idx) {
+void point_init(byte point_idx) {
     x_cur[point_idx] = x_start[point_idx]<<4;
     y_cur[point_idx] = ((word)y_start[point_idx>>1])<<4;
+    delay[point_idx>>1] = DELAY;
+}
+
+// Tables for the plotter - initialized by calling bitmap_draw_init();
+const byte[256] bitmap_plot_ylo;
+const byte[256] bitmap_plot_yhi;
+const byte[256] bitmap_plot_bit;
+
+void bitmap_init(byte* bitmap) {
+    byte bits = $80;
+    for(byte x : 0..255) {
+        bitmap_plot_bit[x] = bits;
+        bits >>= 1;
+        if(bits==0) {
+          bits = $80;
+        }
+    }
+    byte* yoffs = bitmap;
+    for(byte y : 0..255) {
+        bitmap_plot_ylo[y] = y&$7 | <yoffs;
+        bitmap_plot_yhi[y] = >yoffs;
+        if((y&$7)==7) {
+            yoffs = yoffs + 40*8;
+        }
+    }
+}
+
+// Clear all graphics on the bitmap
+void bitmap_clear() {
+    byte* bitmap = (byte*) { bitmap_plot_yhi[0], bitmap_plot_ylo[0] };
+    for( byte y: 0..39 ) {
+        for( byte x: 0..199 ) {
+            *bitmap++ = 0;
+        }
+    }
+}
+
+// Plot a single dot in the bitmap
+void bitmap_plot(word x, byte y) {
+    byte* plotter = (byte*) { bitmap_plot_yhi[y], bitmap_plot_ylo[y] };
+    plotter += ( x & $fff7 );
+    *plotter |= bitmap_plot_bit[<x];
 }
 
 
+