java/ch/wlkl/processor/Test.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.wlkl.processor;

import static ch.wlkl.env.Env.*;
import ch.wlkl.env.IOCompare;
import ch.wlkl.env.IOTest;
import static ch.wlkl.processor.Proc1.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 *
 * @author walter
 */
public class Test {
    IOTest tst = new IOTest();
    IOCompare cmp = new IOCompare();
    
    public void begin(String s) {
        tst.begin(s);
        push(tst);
      }
    
   public void compare(String s, String c) {
       cmp.compare(s, c);
       push(cmp);
    }


    public void end() {
        ((IOTest)pop()).end();
    }
    

    public void memory(int aSz, int wSz) {
        begin("memory");
       
        Memory m = new Memory(aSz, wSz);
        for (int a = 0; a < (1 << aSz); a++) 
            debug(1, "" + a + " = " + m.getWord(a) + ", adr = " + Memory.a2i(m.adr));
        m.iVal[0].set(true);
        m.iVal[2].set(true);
        m.adr[0].set(false);
        m.adr[1].set(true);
        int aSet = Memory.a2i(m.adr);
        out("adr = " + aSet);
        if (aSet != (1 << aSz) - 2)
            err("bad aSet " + aSet);
        out("set iVal adr");
        m.iSet.set(true);
        m.iSet.set(false);
        out("set iSet");
        m.iSet.set(false);
        for (int a = 0; a < (1<<aSz); a++) {
            if ((a == aSet ? 5 : 0) == m.getWord(a))
                debug(1, a + " = " + m.getWord(a));
            else
                err(a + " = " + m.getWord(a));
            m.setWord(a, a*3);
        }
        out("after * 3");
        for (int a = 0; a < (1<<aSz); a++) {
            if (a * 3 == m.getWord(a))
                debug(1, a + " = " + m.getWord(a));
            else
                err(a + " = " + m.getWord(a));
            m.setWord(a, a+77);
        }
     end();   
    }
    
    public void add(int sz) {
        tst.debugLevel = 0;
        begin("add " + sz);
        for (int l = 1; l <= sz; l++) 
            for (int r = 1; r <= sz; r++) 
                add(l, r);
        end();
    }

        public void add(int lSz, int rSz) {
        int cnt = 1<<lSz * 1<<rSz;     
        out("adding lSz=" + lSz + " and rSz=" + rSz + " ==> " + cnt + " adds");
        GateOr []  l = new GateOr[lSz];
        for (int i = 0; i < lSz; i++) 
            l[i] = new GateOr();
        GateOr []  r = new GateOr[rSz];
        for (int i = 0; i < rSz; i++) 
            r[i] = new GateOr();
        GateAdd a = new GateAdd(l, r);
        for (int i = 0; i < (1 << lSz); i++) {
            Memory.i2a(i, l);
            for (int j = 0; j < (1 << rSz); j++) {
                Memory.i2a(j, r);
                debug(1, i + " + " + j + " = " + Memory.a2i(a.out));  
                cnt--;
                if (Memory.a2i(a.out) != i+j)
                    err(i + " + " + j + " = " + Memory.a2i(a.out) + " != " + (i+j));
            }
        }
        if (cnt != 0)
            err("adding lSz=" + lSz + " and rSz=" + rSz + " but cnt not 0 but " + cnt + " adds");
    }

    public void proc0(String test, String comp, int start, int... cd) {
        compare(test, comp);
        Proc1 p = new Proc1();
        p.loadM(start, cd);
        p.run(start);
        end();
    }

    public void proc1() {   // trace go stop
        proc0("proc1: trace go stop", "--- begin   proc1: trace go stop------------\n"
                + "debug run start pc=7\n"
                + "    debug pc=7 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=7\n"
                + "    debug pc=8 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=8\n"
                + "    debug pc=9 code=109 r0=0 r1=0 r2=0 r3=0\n"
                + "  debug badOp=109 at pc=9\n"
                + "    debug pc=10 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=10\n"
                + "    debug pc=11 code=3 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=15 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=15\n"
                + "    debug pc=16 code=1 r0=0 r1=0 r2=0 r3=0\n"
                + "debug run stop pc=16\n"
                + "--- end ok  proc1: trace go stop: 0 errors, 15 says and 10 debugs ------------"
                , 7
                , trace, trace, 109, trace, go, 15, 113, 114, trace, stop);
    }

    public void proc2() {   // lit out
        proc0("proc2: lit out", "--- begin   proc2: lit out------------\n" 
                + "debug run start pc=58\n"
                + "    debug pc=58 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=58\n"
                + "    debug pc=59 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=61 code=2 r0=160 r1=0 r2=0 r3=0\n"
                + "  trace pc=61\n"
                + "    debug pc=62 code=4 r0=160 r1=0 r2=0 r3=0\n"
                + "    debug pc=64 code=4 r0=163 r1=160 r2=0 r3=0\n"
                + "    debug pc=66 code=4 r0=165 r1=163 r2=160 r3=0\n"
                + "    debug pc=68 code=2 r0=167 r1=165 r2=163 r3=160\n"
                + "  trace pc=68\n"
                + "    debug pc=69 code=3 r0=167 r1=165 r2=163 r3=160\n"
                + "    debug pc=73 code=2 r0=167 r1=165 r2=163 r3=160\n"
                + "  trace pc=73\n"
                + "    debug pc=74 code=5 r0=167 r1=165 r2=163 r3=160\n"
                + "out = 167\n"
                + "    debug pc=75 code=5 r0=165 r1=163 r2=160 r3=0\n"
                + "out = 165\n"
                + "    debug pc=76 code=5 r0=163 r1=160 r2=0 r3=0\n"
                + "out = 163\n"
                + "    debug pc=77 code=5 r0=160 r1=0 r2=0 r3=0\n"
                + "out = 160\n"
                + "    debug pc=78 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=78\n"
                + "    debug pc=79 code=5 r0=0 r1=0 r2=0 r3=0\n"
                + "out = 0\n"
                + "    debug pc=80 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=82 code=6 r0=87 r1=0 r2=0 r3=0\n"
                + "    debug pc=83 code=2 r0=187 r1=0 r2=0 r3=0\n"
                + "  trace pc=83\n"
                + "    debug pc=84 code=5 r0=187 r1=0 r2=0 r3=0\n"
                + "out = 187\n"
                + "    debug pc=85 code=1 r0=0 r1=0 r2=0 r3=0\n"
                + "debug run stop pc=85\n"
                + "--- end ok  proc2: lit out: 0 errors, 35 says and 22 debugs ------------"
                , 58, trace, lit, 160, trace, lit, 163, lit, 165, lit, 167, trace, go, 73, 171, 172, trace, out, out, out, out, trace, out, lit, 87, load, trace, out, stop, 186, 187, 188);
    }
    public void proc3() {   // store load
        proc0("proc3: store load", "--- begin   proc3: store load------------\n"
                + "debug run start pc=100\n"
                + "    debug pc=100 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=102 code=4 r0=101 r1=0 r2=0 r3=0\n"
                + "    debug pc=104 code=4 r0=201 r1=101 r2=0 r3=0\n"
                + "    debug pc=106 code=4 r0=107 r1=201 r2=101 r3=0\n"
                + "    debug pc=108 code=7 r0=207 r1=107 r2=201 r3=101\n"
                + "    debug pc=109 code=7 r0=201 r1=101 r2=0 r3=0\n"
                + "    debug pc=110 code=5 r0=0 r1=0 r2=0 r3=0\n"
                + "out = 0\n"
                + "    debug pc=111 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=113 code=6 r0=201 r1=0 r2=0 r3=0\n"
                + "    debug pc=114 code=5 r0=101 r1=0 r2=0 r3=0\n"
                + "out = 101\n"
                + "    debug pc=115 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=117 code=6 r0=207 r1=0 r2=0 r3=0\n"
                + "    debug pc=118 code=5 r0=107 r1=0 r2=0 r3=0\n"
                + "out = 107\n"
                + "    debug pc=119 code=1 r0=0 r1=0 r2=0 r3=0\n"
                + "debug run stop pc=119\n"
                + "--- end ok  proc3: store load: 0 errors, 20 says and 16 debugs ------------"
                , 100, lit, 101, lit, 201, lit, 107, lit, 207, store, store, out, lit, 201, load, out, lit, 207, load, out, stop);
    }
 
    public void proc4() {  // add eq
        proc0("proc4: add eq", "--- begin   proc4: add eq------------\n"
                + "debug run start pc=11\n"
                + "    debug pc=11 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=13 code=4 r0=7 r1=0 r2=0 r3=0\n"
                + "    debug pc=15 code=8 r0=77 r1=7 r2=0 r3=0\n"
                + "    debug pc=16 code=5 r0=84 r1=0 r2=0 r3=0\n"
                + "out = 84\n"
                + "    debug pc=17 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=19 code=4 r0=128 r1=0 r2=0 r3=0\n"
                + "    debug pc=21 code=8 r0=128 r1=128 r2=0 r3=0\n"
                + "    debug pc=22 code=5 r0=0 r1=0 r2=0 r3=0\n"
                + "out = 0\n"
                + "    debug pc=23 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=25 code=4 r0=200 r1=0 r2=0 r3=0\n"
                + "    debug pc=27 code=8 r0=57 r1=200 r2=0 r3=0\n"
                + "    debug pc=28 code=5 r0=1 r1=0 r2=0 r3=0\n"
                + "out = 1\n"
                + "    debug pc=29 code=9 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=30 code=5 r0=1 r1=0 r2=0 r3=0\n"
                + "out = 1\n"
                + "    debug pc=31 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=33 code=9 r0=128 r1=0 r2=0 r3=0\n"
                + "    debug pc=34 code=5 r0=0 r1=0 r2=0 r3=0\n"
                + "out = 0\n"
                + "    debug pc=35 code=1 r0=0 r1=0 r2=0 r3=0\n"
                + "debug run stop pc=35\n" 
                + "--- end ok  proc4: add eq: 0 errors, 26 says and 20 debugs ------------" //
                , 11, lit, 7, lit, 77, add, out // 7 + 77 ==> out,
                , lit, 128, lit, 128, add, out // 128+28 ==> out
                , lit, 200, lit, 57, add, out // 200+57 ==> out
                , eq, out // 0=0 ==> out
                , lit, 128, eq, out // 128=0 ==> out
                , stop);
    }

     
    public void proc5() {  // goItT dup
        proc0("proc5: goItT dup", "--- begin   proc5: goItT dup------------\n"
                + "debug run start pc=48\n"
                + "    debug pc=48 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=50 code=11 r0=52 r1=0 r2=0 r3=0\n"
                + "    debug pc=51 code=5 r0=52 r1=52 r2=0 r3=0\n"
                + "out = 52\n"
                + "    debug pc=52 code=10 r0=52 r1=0 r2=0 r3=0\n"
                + "    debug pc=54 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=54\n"
                + "    debug pc=55 code=3 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=58 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=58\n"
                + "    debug pc=59 code=4 r0=0 r1=0 r2=0 r3=0\n"
                + "    debug pc=61 code=11 r0=63 r1=0 r2=0 r3=0\n"
                + "    debug pc=62 code=5 r0=63 r1=63 r2=0 r3=0\n"
                + "out = 63\n"
                + "    debug pc=63 code=10 r0=63 r1=0 r2=0 r3=0\n"
                + "    debug pc=68 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=68\n"
                + "    debug pc=69 code=2 r0=0 r1=0 r2=0 r3=0\n"
                + "  trace pc=69\n"
                + "    debug pc=70 code=1 r0=0 r1=0 r2=0 r3=0\n"
                + "debug run stop pc=70\n"
                + "--- end ok  proc5: goItT dup: 0 errors, 23 says and 16 debugs ------------" //
                , 48, lit, 52, dup, out // 52 ==> out, r0
                , goIfT, 57, trace, go, 58, trace, trace // if then trace 57 else trace 54 endif; trace 58
                , lit, 63, dup, out // 63 ==> out, r0
                , goIfT, 68, trace, go, 69, trace, trace // if then trace 68 else trace 65 endif; trace 69
                , stop);
    }

    public void proc6() { 
        cmp.debugLevel = 1;
        int[] cd = {lit, 4, lit, 30, store // 60: von ==> m[30]
            , lit, 10, lit, 31, store // 65: bis ==> m[31]
            , lit, 30, load, out // von ==> out
            , lit, 31, load, out // bis ==> out
            , lit, 0, lit, 32, store // summe = 0 ==> m[32]
            , lit, 30, load // von ==> r0 = act
            , dup, goIfT, 97 // 86: goIf ungerade
            , dup, lit, 32, load, add, lit, 32, store // summe+=r0
            , dup, lit, 31, load, eq, goIfT, 109 // 97: if act == bis go end 
            , lit, 1, add // 97: r0=act++
            , go, 86 // :107 go 86
            , lit, 32, load, out // :109 sum ==> out
            , stop};
        proc0("proc6: addiere gerade Zahlen 4..10 > 28" //, "-- begin   proc6: addiere gerade Zahlen 4..10 > 28------------\n"
                , "--- begin   proc6: addiere gerade Zahlen 4..10 > 28------------\n" + "debug run start pc=60\n"
                + "out = 4\n"
                + "out = 10\n"
                + "out = 28\n"
                + "debug run stop pc=113\n"
                + "--- end ok  proc6: addiere gerade Zahlen 4..10 > 28: 0 errors, 6 says and 114 debugs ------------" //
                , 60, cd);
        cd[6] = 23;
        cmp.debugLevel = 1;
        proc0("proc6: addiere gerade Zahlen 4..23 > 130" //, "-- begin   proc6: addiere gerade Zahlen 4..10 > 28------------\n"
                , "--- begin   proc6: addiere gerade Zahlen 4..23 > 130------------\n"
                + "debug run start pc=60\n"
                + "out = 4\n"
                + "out = 23\n"
                + "out = 130\n"
                + "debug run stop pc=113\n"
                + "--- end ok  proc6: addiere gerade Zahlen 4..23 > 130: 0 errors, 6 says and 280 debugs ------------" //
                , 60, cd);
        cd[1] = 3;
        cd[6] = 31;
        cmp.debugLevel = 1;
        proc0("proc6: addiere gerade Zahlen 1..31 > 238" //, "-- begin   proc6: addiere gerade Zahlen 4..10 > 28------------\n"
                , "--- begin   proc6: addiere gerade Zahlen 1..31 > 238------------\n"
                + "debug run start pc=60\n"
                + "out = 3\n"
                + "out = 31\n"
                + "out = 238\n"
                + "debug run stop pc=113\n"
                + "--- end ok  proc6: addiere gerade Zahlen 1..31 > 238: 0 errors, 6 says and 394 debugs ------------" //
                , 60, cd);
    }

    public void all() {
        memory(7, 13);
        add(9);
        proc1();
        proc2();
        proc3();
        proc4();
        proc5();
        proc6();
        tst.total(cmp);
    }

    void t1() {
        out("-77 ==> " + Proc1.opName(-775));
        out("5 ==> " + Proc1.opName(5));
        Field[] flds = Proc1.class.getFields();
        for (Field f : flds) {
            out(f.getName() + " mod " + f.getModifiers() + " class=" + f.getType());
            out("static=" + Modifier.STATIC + " public="+Modifier.PUBLIC + " puSta=" 
            + (((f.getModifiers()&Modifier.STATIC)!=0) & ((f.getModifiers()&Modifier.PUBLIC) != 0)));
            out("int=" + (f.getType() == Integer.TYPE));
        if (f.getType() == Integer.TYPE & (f.getModifiers()&Modifier.STATIC) != 0 & (f.getModifiers()&Modifier.PUBLIC) != 0)
            try {
                out(f.getName() + " = " + f.getInt(null));
            } catch (Exception ex) {
                out("exception " + ex);
            }
        }
        
    }

    public static void main(String[] args) {
        for (int i = -2; i < bad1+3; i++) {
         out("" + i + " ==> " + opName(i)); 
        }
        Test t = new Test();
        //    t.memory(7, 13);   
        t.all(); 
        // t.proc1();
//        t.t1();
    }
   
}