major software code reorg
authoradam <adam@megacz.com>
Mon, 5 Feb 2007 19:09:02 +0000 (20:09 +0100)
committeradam <adam@megacz.com>
Mon, 5 Feb 2007 19:09:02 +0000 (20:09 +0100)
12 files changed:
src/edu/berkeley/fleet/api/Fleet.java
src/edu/berkeley/fleet/assembler/Main.java
src/edu/berkeley/fleet/assembler/Parser.java
src/edu/berkeley/fleet/interpreter/DataInbox.java
src/edu/berkeley/fleet/interpreter/DataOutbox.java
src/edu/berkeley/fleet/interpreter/Interpreter.java
src/edu/berkeley/fleet/interpreter/InterpreterBenkoBox.java
src/edu/berkeley/fleet/interpreter/Main.java
src/edu/berkeley/fleet/ships/Dcache.java [moved from src/edu/berkeley/fleet/ships/Mem.java with 52% similarity]
src/edu/berkeley/fleet/ships/Debug.java
src/edu/berkeley/fleet/ships/Execute.java
src/edu/berkeley/fleet/ships/Icache.java [new file with mode: 0644]

index 6546d17..c40b2db 100644 (file)
@@ -27,4 +27,12 @@ public abstract class Fleet implements Iterable<Ship> {
      */ 
     public abstract int computeTarget(int origin, int offset);
 
+    /**
+     *  This interface marks Fleets which can create ships on the fly, like the fleeterpreter;
+     *  if available, the parser will use this interface to create ships out of #ship definitions.
+     */
+    public static interface WithDynamicShips {
+        public Ship createShip(String shiptype, String shipname);
+    }
+
 }
\ No newline at end of file
index 6b81c8b..0b98b42 100644 (file)
@@ -12,10 +12,4 @@ public class Main {
         new Parser(fleet).parse(r, out);
     }
 
-    /** parse the assembly code on <tt>r</tt>, encode it for <tt>fleet</tt>, and insert
-     *  <tt>Instruction</tt>s into <tt>out</tt> */
-    public static void assemble(Fleet fleet, Reader r, ArrayList<Instruction> out) throws Exception {
-        new Parser(fleet).parse(r, out);
-    }
-
 }
\ No newline at end of file
index 2b2dc45..e8b7f37 100644 (file)
@@ -30,6 +30,12 @@ class Parser {
     // codebags in numerical order
     private ArrayList<CodeBag> codeBags = new ArrayList<CodeBag>();
     private HashMap<String,CodeBag> codeBagsByName = new HashMap<String,CodeBag>();
+    
+    private CodeBag getCodeBag(String name) {
+        CodeBag cb = codeBagsByName.get(name);
+        if (cb!=null) return cb;
+        return new CodeBag(name);
+    }
 
     Tree<String> parse(Reader r) throws Exception {
         InputStream grammarStream =
@@ -49,7 +55,9 @@ class Parser {
         CodeBag rootCodeBag = new CodeBag();
         baseCodeBag.add(new Instruction.Literal.CodeBagDescriptor(null, rootCodeBag.getFakeAddress(), 1));
         walk((Tree<String>)parse(r), rootCodeBag);
-        
+        if (fleet instanceof edu.berkeley.fleet.interpreter.Interpreter)
+            ((edu.berkeley.fleet.slipway.Slipway)fleet).dumpFabric(true);
+
         // map from arbitrary identifiers to actual addresses
         int[] codeBagMap = new int[codeBags.size()];
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -64,8 +72,8 @@ class Parser {
         }
 
         // now write for real
-        dos = new DataOutputStream(out);
-        cos = new CountingOutputStream(dos);
+        cos = new CountingOutputStream(out);
+        dos = new DataOutputStream(cos);
         for(int i=0; i<codeBags.size(); i++) {
             CodeBag c = codeBags.get(i);
             dos.flush();
@@ -83,39 +91,9 @@ class Parser {
             }
         }
         dos.flush();
-    }
-
-    public void parse(Reader r, ArrayList<Instruction> out) throws Exception {
-        // this needs to be "code bag zero"
-        CodeBag baseCodeBag = new CodeBag();
-        CodeBag rootCodeBag = new CodeBag();
-        Instruction inst0 = new Instruction.Literal.CodeBagDescriptor(null, rootCodeBag.getFakeAddress(), 1);
-        baseCodeBag.add(inst0);
-        walk((Tree<String>)parse(r), rootCodeBag);
-
-        // map from arbitrary identifiers to actual addresses
-        int[] codeBagMap = new int[codeBags.size()];
-        ArrayList<Instruction> temp = new ArrayList<Instruction>();
-        for(int i=0; i<codeBags.size(); i++) {
-            codeBagMap[i] = temp.size();
-            for(Instruction inst : codeBags.get(i))
-                temp.add(inst);
-        }
-
-        // now write for real
-        for(int i=0; i<codeBags.size(); i++) {
-            CodeBag c = codeBags.get(i);
-            for(Instruction inst : c) {
-                if (inst instanceof Instruction.Literal.CodeBagDescriptor) {
-                    Instruction.Literal.CodeBagDescriptor old = (Instruction.Literal.CodeBagDescriptor)inst;
-                    int offset = fleet.computeOffset(out.size(), codeBagMap[(int)old.offset]);
-                    inst = new Instruction.Literal.CodeBagDescriptor(old.dest,
-                                                                     offset,
-                                                                     codeBags.get((int)old.offset).size());
-                }
-                out.add(inst);
-            }
-        }
+        cos.flush();
+        out.flush();
+        out.close();
     }
 
     /** in the first pass, codebags are assigned "addresses" in arbitrary order */
@@ -131,22 +109,18 @@ class Parser {
                     fillCodeBag(statement, cb);
    
         } else if (head.equals("Import")) {
-            imports.add(string(t.child(0)));
+            // ignored
 
         } else if (head.equals("Ship")) {
             String name = name(t.child(0));
             String type = string(t.child(1));
             Ship ship = null;
-            if (fleet instanceof edu.berkeley.fleet.interpreter.Interpreter) {
-                edu.berkeley.fleet.interpreter.Interpreter interpreter =
-                    ((edu.berkeley.fleet.interpreter.Interpreter)fleet);
-                String classname = type;
-                boolean good = false;
-                for(String s : imports)
-                    if ((ship = interpreter.tryCreate(s+"."+classname, name)) != null)
-                        break;
+
+            if (fleet instanceof Fleet.WithDynamicShips) {
+                Fleet.WithDynamicShips dyn = ((Fleet.WithDynamicShips)fleet);
+                ship = dyn.createShip(type, name);
                 if (ship==null)
-                    throw new RuntimeException("couldn't find a ship called \""+classname+"\"");
+                    throw new RuntimeException("couldn't find a ship called \""+type+"\"");
             } else {
                 ship = allocateShip(type);
             }
@@ -195,6 +169,7 @@ class Parser {
     }
 
     private HashMap<String,Integer> numAllocated = new HashMap<String,Integer>();
+
     Ship allocateShip(String shipType) {
         int allocated = 0;
         if (numAllocated.get(shipType) != null)
@@ -215,7 +190,7 @@ class Parser {
     void fillCodeBag(Tree<String> t, CodeBag cb) {
         if (t.head()==null) return;
         else if (t.head().equals("NamedCodeBag")) {
-            CodeBag cb2 = new CodeBag(name(t.child(0)));
+            CodeBag cb2 = getCodeBag(name(t.child(0)));
             for(Tree<String> statement : t.child(1))
                 fillCodeBag(statement, cb2);
 
@@ -224,6 +199,12 @@ class Parser {
             BenkoBox benkobox = portReference(t.child(1));
             cb.add(new Instruction.Literal.Absolute(benkobox, literal));
 
+        } else if (t.head().equals("CodeBagDescriptor")) {
+            String refname = name(t.child(0).child(0));
+            CodeBag cb2 = getCodeBag(refname);
+            BenkoBox benkobox = portReference(t.child(1));
+            cb.add(new Instruction.Literal.CodeBagDescriptor(benkobox, cb2.getFakeAddress(), 0));
+
         } else if (t.head().equals("Fiber")) {
             BenkoBox benkobox = portReference(t.child(0));
             
index ed1f6c2..869a734 100644 (file)
@@ -8,15 +8,8 @@ public class DataInbox extends Inbox {
     public DataInbox(InterpreterShip ship, String name) {
         super(ship, name);
     }
-    public DataInbox(InterpreterShip ship, String name, boolean noInbox, boolean noChannelDef) {
+    public DataInbox(InterpreterShip ship, String name, boolean special) {
         super(ship, name);
-        this.noInbox = noInbox;
-        this.noChannelDef = noChannelDef;
-    }
-    public DataInbox(InterpreterShip ship, String name, boolean noInbox, boolean noChannelDef, boolean special) {
-        super(ship, name);
-        this.noInbox = noInbox;
-        this.noChannelDef = noChannelDef;
         this.special = special;
     }
 
index e524417..0292499 100644 (file)
@@ -6,6 +6,10 @@ public class DataOutbox extends Outbox {
     public DataOutbox(InterpreterShip ship, String name) { super(ship, name); }
     public DataOutbox(InterpreterShip ship, String name, boolean special) {
         super(ship, name); this.special = special; }
+    public DataOutbox(InterpreterShip ship, String name, boolean special, boolean ihorn) {
+        super(ship, name); this.special = special; this.ihorn = ihorn; }
+    public DataOutbox(InterpreterShip ship, String name, boolean special, boolean ihorn, boolean dhorn) {
+        super(ship, name); this.special = special; this.ihorn = ihorn; this.dhorn = dhorn; }
 
     private Interpreter getInterpreter() { return ((InterpreterShip)getShip()).getInterpreter(); }
     public void addDataFromShip(int data) {
index 789a314..2e934a3 100644 (file)
@@ -1,54 +1,84 @@
 package edu.berkeley.fleet.interpreter;
-import edu.berkeley.fleet.api.*;
-
-import edu.berkeley.fleet.api.*;
-import edu.berkeley.fleet.*;
-import java.lang.reflect.*;
-import edu.berkeley.sbp.chr.*;
-import edu.berkeley.sbp.misc.*;
-import edu.berkeley.sbp.meta.*;
-import edu.berkeley.sbp.bind.*;
-import edu.berkeley.sbp.util.*;
-import java.util.*;
 import java.io.*;
+import java.util.*;
+import java.lang.reflect.*;
+import edu.berkeley.fleet.*;
+import edu.berkeley.fleet.api.*;
+import edu.berkeley.fleet.ies44.*;
 import edu.berkeley.fleet.ships.*;
 
-public class Interpreter extends Fleet implements Iterable<Ship> {
+public class Interpreter extends Fleet /*, Fleet.WithDynamicShips*/ {
+
+    /** some "halt ship" can turn this on to stop the interpreter */
+    public boolean       halt         = false;
+
+    public ArrayList<InterpreterShip> shiplist   = new ArrayList<InterpreterShip>();
+    public HashMap<String,InterpreterShip> ships = new HashMap<String,InterpreterShip>();
+
+    public void go(byte[] instructions) {
 
-    public InterpreterBenkoBox resolve(edu.berkeley.fleet.api.BenkoBox bb) { return (InterpreterBenkoBox)bb; }
+        // find the first icache
+        Icache icache = null;
+        for(Ship ship : this)
+            if (ship instanceof Icache) {
+                icache = (Icache)ship;
+                break;
+            }
 
-    public void dumpCode(Instruction[] instructions) throws IOException {
-        DataOutputStream dos = new DataOutputStream(new FileOutputStream("build/fleet.bin"));
-        for(int i=1; i<instructions.length; i++) {
-            Instruction inst = instructions[i];
-            writeInstruction(dos, inst);
+        // load the icache and take note of the 0-address CBD
+        long launch = 0;
+        for(int i=0; i<instructions.length; i+=6) {
+            long word = 0;
+            for(int j=0; j<6; j++)
+                word = (word << 8) | (instructions[i+j] & 0xff);
+            icache.writeMem(i/6, word);
+            if (i==0) launch = word;
         }
-        dos.flush();
-        dos.close();
+
+        // dispatch the 0-address CBD
+        int base = (int)(launch >> 6);
+        base = base & ~(0xffffffff << 18);
+        int size = (int)launch;
+        size = size & ~(0xffffffff <<  6);
+        icache.dispatch(base, size);
+
+        while(!halt)
+            for(InterpreterShip ship : ships.values())
+                for(int j=0; j<10; j++)
+                    ship._service();
+
+        // run the ships a bit longer for good measure
+        for(int i=0; i<100; i++)
+            for(InterpreterShip ship : ships.values())
+                for(int j=0; j<10; j++)
+                    ship._service();
+
+        // check the state of the ships
+        for(InterpreterShip ship : ships.values())
+            ship.shutdown();
+
+        Log.println(Log.yellow("    DONE: ====== FLEET is halted.  Have a nice day.  ======"));
     }
 
     public void dispatch(Instruction i, long address) {
-
+        Log.dispatch(i);
         if (i instanceof Instruction.Executable) {
-            InterpreterBenkoBox sourceBenkoBox = resolve(((Instruction.Executable)i).benkoBox);
-            if (!(sourceBenkoBox instanceof InstructionPort))
-                throw new RuntimeException(sourceBenkoBox + " is not an InstructionPort!");
+            InterpreterBenkoBox sourceBenkoBox = (InterpreterBenkoBox)(((Instruction.Executable)i).benkoBox);
             ((InstructionPort)sourceBenkoBox).addInstruction(((Instruction.Executable)i));
 
         } else if (i instanceof Instruction.Literal.CodeBagDescriptor) {
-            Log.dispatch(i);
             Instruction.Literal.CodeBagDescriptor cbd = (Instruction.Literal.CodeBagDescriptor)i;
-            dispatchCodeBag(cbd.offset+address, cbd.size);
+            InterpreterBenkoBox destBenkoBox = (InterpreterBenkoBox)(cbd.dest);
+            long absolute_cbd = ((cbd.offset+address) << 6) | cbd.size;
+            destBenkoBox.addDataFromFabric((int)absolute_cbd);
             
         } else if (i instanceof Instruction.Literal.Absolute) {
-            InterpreterBenkoBox destBenkoBox = resolve(((Instruction.Literal.Absolute)i).dest);
+            InterpreterBenkoBox destBenkoBox = (InterpreterBenkoBox)(((Instruction.Literal.Absolute)i).dest);
             Log.data(((Instruction.Literal.Absolute)i).value+"", null, destBenkoBox);
             destBenkoBox.addDataFromFabric((int)((Instruction.Literal.Absolute)i).value);
 
         } else if (i instanceof Instruction.Kill) {
-            InterpreterBenkoBox benkoBox = resolve(((Instruction.Kill)i).benkoBox);
-            if (!(benkoBox instanceof InstructionPort))
-                throw new RuntimeException(benkoBox + " is not an InstructionPort!");
+            InterpreterBenkoBox benkoBox = (InterpreterBenkoBox)(((Instruction.Kill)i).benkoBox);
             ((InstructionPort)benkoBox).kill(((Instruction.Kill)i).count);
 
         } else {
@@ -56,104 +86,66 @@ public class Interpreter extends Fleet implements Iterable<Ship> {
         }
     }
 
-    /** some "halt ship" can turn this on to stop the interpreter */
-    public boolean halt = false;
+    public void sendToken(InterpreterBenkoBox source, InterpreterBenkoBox dest) {
+        Log.token(source, dest);
+        dest.addTokenFromFabric();
+    }
 
-    public int[] mem = new int[0];
+    public void sendData(InterpreterBenkoBox source, int data, InterpreterBenkoBox dest) {
+        Log.data(data+"", source, dest);
+        dest.addDataFromFabric(data);
+    }
 
-    public Instruction[] instructions = null;
-    public ArrayList<String> imports = new ArrayList<String>();
 
-    private static String getUniqueName(Ship ship) {
-        return ship.getType() + ship.getOrdinal();
-    }
+    // Implementation of the Fleet class abstract methods /////////////////////////////////////////////////////////
 
-    public ArrayList<InterpreterShip> shiplist = new ArrayList<InterpreterShip>();
-    public HashMap<String,InterpreterShip> ships = new HashMap<String,InterpreterShip>();
+    public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)shiplist.iterator(); }
 
-    /** read a machine-formatted instruction from a file (into a Java object) */
-    public Instruction readInstruction(DataInputStream is) throws IOException {
-        // FIXME
-        return null;
-    }
+    public int computeOffset(int origin, int target) { return (target - origin)/6; }
+    public int computeTarget(int origin, int offset) { return origin + (offset*6); }
 
-    public void writeInstruction(DataOutputStream os, Instruction d) throws IOException {
-        long instr = 0;
+    private InterpreterInstructionEncoder iie = new InterpreterInstructionEncoder();
+    public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
+    public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
+    public long writeInstruction(Instruction d) { return writeInstruction(d); }
+    public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
 
-        // Kill is encoded as Execute with the illegal combination (Latch & ~DataIn)
-        if (d instanceof Instruction.Kill) {
-            Instruction.Kill k = (Instruction.Kill)d;
-            d = new Instruction.Executable(k.benkoBox, null, k.count, false, false, true, false, false, false);
+    private class InterpreterInstructionEncoder extends InstructionEncoder {
+        public long getBoxAddr(BenkoBox box) { return ((InterpreterBenkoBox)box).addr; }
+        public long getBoxInstAddr(BenkoBox box) { return ((InterpreterBenkoBox)box).instr_addr; }
+        public BenkoBox getBoxByAddr(long dest) {
+            for(Ship ship : Interpreter.this)
+                for(BenkoBox bb : ship.getBenkoBoxes())
+                    if (((InterpreterBenkoBox)bb).addr == dest)
+                        return bb;
+            return null;
         }
-
-        if (d instanceof Instruction.Executable) {
-            Instruction.Executable inst = (Instruction.Executable)d;
-            InterpreterBenkoBox dest = resolve(inst.dest);
-            instr = dest==null ? 0 : (dest.addr << 1);
-            if (inst.count >= (1<<8))
-                throw new RuntimeException("count field must be less than 128");
-            instr |= (((long)inst.count) << (11+1));
-            if (inst.tokenIn)  instr |= (1L << (11+1+7+0));
-            if (inst.dataIn)   instr |= (1L << (11+1+7+1));
-            if (inst.latch)    instr |= (1L << (11+1+7+2));
-            if (inst.dataOut)  instr |= (1L << (11+1+7+3));
-            if (inst.tokenOut) instr |= (1L << (11+1+7+4));
-            if (inst.recycle)  instr |= (1L);
-            instr |= ((long)resolve(inst.benkoBox).instr_addr) << (11+5+7+1);
-
-        } else if (d instanceof Instruction.Literal.Absolute) {
-            Instruction.Literal.Absolute ld = (Instruction.Literal.Absolute)d;
-            instr = (2L << (11+24));
-            instr |= (resolve(ld.dest).addr) << 24;
-            if (ld.value >= (1<<25))
-                throw new RuntimeException("literals must be less than 2^24");
-            instr |= ((long)ld.value);
+        public BenkoBox getBoxByInstAddr(long dest) {
+            for(Ship ship : Interpreter.this)
+                for(BenkoBox bb : ship.getBenkoBoxes())
+                    if (((InterpreterBenkoBox)bb).instr_addr == dest)
+                        return bb;
+            return null;
         }
-
-        dump(os, (instr >> (5*8)) & 0xff);
-        dump(os, (instr >> (4*8)) & 0xff);
-        dump(os, (instr >> (3*8)) & 0xff);
-        dump(os, (instr >> (2*8)) & 0xff);
-        dump(os, (instr >> (1*8)) & 0xff);
-        dump(os, (instr >> (0*8)) & 0xff);
-    }
-    public void dump(OutputStream os, long data_) throws IOException {
-        int data = (int)data_;
-        os.write((byte)data);
-        //System.out.println(data);
-    }
-
-    public Iterator<Ship> iterator() {
-        return (Iterator<Ship>)(Object)shiplist.iterator();
     }
 
-    public void dispatchCodeBag(long base, long size) {
-        for(long i=base; i<base+size; i++)
-            dispatch(instructions[(int)i], i);
+    public Ship createShip(String shipType, String shipname) {
+        try {
+            Class c = Class.forName("edu.berkeley.fleet.ships."+shipType);
+            Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class });
+            InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname });
+            ships.put(shipname, ret);
+            shiplist.add(ret);
+            return ret;
+        } catch (Exception e) {
+            return null;
+        }
     }
 
-    public void go() {
-        Instruction.Literal.CodeBagDescriptor cbl =
-            (Instruction.Literal.CodeBagDescriptor)instructions[0];
-        dispatchCodeBag(cbl.offset+0, cbl.size);
-
-        while(!halt)
-            for(InterpreterShip ship : ships.values())
-                for(int j=0; j<10; j++)
-                    ship._service();
-
-        // run the ships a bit longer for good measure
-        for(int i=0; i<100; i++)
-            for(InterpreterShip ship : ships.values())
-                for(int j=0; j<10; j++)
-                    ship._service();
 
-        // check the state of the ships
-        for(InterpreterShip ship : ships.values())
-            ship.shutdown();
+    // Memory //////////////////////////////////////////////////////////////////////////////
 
-        Log.println(Log.yellow("    DONE: ====== FLEET is halted.  Have a nice day.  ======"));
-    }
+    public int[] mem = new int[0];
 
     public void dumpMem() {
         Log.print(Log.cyan("  MEMORY: "));
@@ -177,269 +169,6 @@ public class Interpreter extends Fleet implements Iterable<Ship> {
         mem[addr] = data;
     }
 
-    public InterpreterShip getShip(String name) {
-        InterpreterShip s = ships.get(name);
-        if (s == null) throw new RuntimeException("unknown ship \""+name+"\"");
-        return s;
-    }
-
-    public InterpreterShip tryCreate(String classname, String shipname) {
-        try {
-            Class c = Class.forName(classname);
-            Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class });
-            InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname });
-            ships.put(shipname, ret);
-            shiplist.add(ret);
-            return ret;
-        } catch (Exception e) {
-            return null;
-        }
-    }
 
-    public void sendToken(InterpreterBenkoBox source, InterpreterBenkoBox dest) {
-        Log.token(source, dest);
-        dest.addTokenFromFabric();
-    }
-
-    public void sendData(InterpreterBenkoBox source, int data, InterpreterBenkoBox dest) {
-        Log.data(data+"", source, dest);
-        dest.addDataFromFabric(data);
-    }
-
-    public void dumpFabric(boolean quiet) {
-        // FIXME: this is really ugly: the order of port declarations in
-        //        the XXXShip.java file must match the order in the .balsa file!
-
-        ArrayList instructionports = new ArrayList<InterpreterBenkoBox>();
-        for(InterpreterShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((InterpreterBenkoBox)port).special())
-                    instructionports.add(port);
-        FabricTree instructions =
-            new FabricTree((InterpreterBenkoBox[])instructionports.toArray(new InterpreterBenkoBox[0]),
-                           "ihorn",
-                           "instruction");
-
-        ArrayList inputports = new ArrayList<InterpreterBenkoBox>();
-        for(InterpreterShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((InterpreterBenkoBox)port).special())
-                    inputports.add(port);
-        FabricTree inputs =
-            new FabricTree((InterpreterBenkoBox[])inputports.toArray(new InterpreterBenkoBox[0]),
-                           "horn",
-                           "dest");
-
-        ArrayList outputports = new ArrayList<InterpreterBenkoBox>();
-        for(InterpreterShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes())
-                if (!((InterpreterBenkoBox)port).special())
-                    outputports.add(port);
-        FabricTree outputs =
-            new FabricTree((InterpreterBenkoBox[])outputports.toArray(new InterpreterBenkoBox[0]),
-                           "funnel",
-                           "source");
-
-        if (quiet) return;
-        System.out.println("`include \"macros.v\"");
-        /*
-          HashSet<Class> added = new HashSet<Class>();
-          for(Ship ship : shiplist)
-          if (!added.contains(ship.getClass())) {
-          added.add(ship.getClass());
-          System.out.println("import ["+ship.getBalsaName()+"]");
-          }
-        */
-        System.out.println("module fabric(clk, data_Execute0_in_r, data_Execute0_in_a, data_Execute0_in,");
-        System.out.println("                   data_Debug0_out_r, data_Debug0_out_a, data_Debug0_out);");
-        System.out.println("  input  clk;");
-        System.out.println("  input  data_Execute0_in_r;");
-        System.out.println("  output data_Execute0_in_a;");
-        System.out.println("  input  [(`PACKET_WIDTH-1):0] data_Execute0_in;");
-        System.out.println("  output data_Debug0_out_r;");
-        System.out.println("  input  data_Debug0_out_a;");
-        System.out.println("  output [(`PACKET_WIDTH-1):0] data_Debug0_out;");
-        System.out.println("  wire   [(`INSTRUCTION_WIDTH-1):0] data_Execute0_ihorn;");
-        System.out.println("  wire   [(`PACKET_WIDTH-1):0] data_Execute0_dhorn;");
-        System.out.println();
-        
-        System.out.println();
-
-        instructions.dumpChannels(true);
-        outputs.dumpChannels(true);
-        inputs.dumpChannels(true);
-        for(InterpreterShip ship : shiplist)
-            for(BenkoBox port : ship.getBenkoBoxes()) {
-                if (ship instanceof Execute && port instanceof Outbox) continue;
-                System.out.println("  wire [(`PACKET_WIDTH-1):0] data_"+getUniqueName(ship)+"_"+port.getName()+";");
-            }
-
-        System.out.println("");
-        instructions.dumpChannels(false);
-        System.out.println("");
-        outputs.dumpChannels(false);
-        System.out.println("");
-        inputs.dumpChannels(false);
-        System.out.println("");
-        for(InterpreterShip ship : shiplist) {
-            System.out.print(ship.getClass().getSimpleName().toLowerCase());
-            System.out.print(" ");
-            System.out.print("krunk"+(krunk++));
-            System.out.print("(clk, ");
-            boolean first = true;
-            for(BenkoBox port : ship.getBenkoBoxes()) {
-                if (!first) System.out.print(", ");
-                first = false;
-                System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
-                System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
-                System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
-                System.out.print(" ");
-            }
-            System.out.println(");");
-
-            for(BenkoBox port : ship.getBenkoBoxes()) {
-                if (((InterpreterBenkoBox)port).special()) continue;
-                if (port instanceof Inbox) {
-                    /*
-                    if (((InterpreterBenkoBox)port).noInbox())
-                        System.out.print("stupidinbox");
-                    else
-                    */
-                    System.out.print("inbox");
-                } else {
-                    System.out.print("outbox");
-                }
-                System.out.print(" krunk"+(krunk++)+"(clk, ");
-                System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
-                System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
-                System.out.print("instruction_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
-                System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
-                System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
-                System.out.print("dest_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
-                System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
-                System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
-                System.out.print("source_"+getUniqueName(port.getShip())+"_"+port.getName()+", ");
-                System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_r, ");
-                System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName()+"_a, ");
-                System.out.print("data_"+getUniqueName(port.getShip())+"_"+port.getName());
-                System.out.print(");");
-                System.out.println();
-            }
-
-        }
-        System.out.println("funnel topfun(clk,"+
-                           "              dest_r, dest_a, dest,"+
-                           "              source_r, source_a, source,"+
-                           "              data_Execute0_dhorn_r, data_Execute0_dhorn_a, data_Execute0_dhorn);");
-        System.out.println("assign instruction_r = data_Execute0_ihorn_r;");
-        System.out.println("assign data_Execute0_ihorn_a = instruction_a;");
-        System.out.println("assign instruction = data_Execute0_ihorn;");
-        System.out.println("endmodule");
-    }
-
-    private static class FabricTree {
-        int master_idx = 1;
-        String prefix;
-        Node root;
-        public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
-        public FabricTree(InterpreterBenkoBox[] ports, String component, String prefix) {
-            this.prefix = prefix;
-            root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
-        }
-        private Object mkNode(String name, String component, InterpreterBenkoBox[] ports,
-                              int start, int end, int addr, int bits) {
-            if (end-start == 0) return null;
-            if (end-start == 1) {
-                InterpreterBenkoBox p = ports[start];
-                if (prefix.equals("instruction")) {
-                    p.instr_addr = addr;
-                    p.instr_bits = bits;
-                } else {
-                    p.addr = addr;
-                    p.bits = bits;
-                }
-                return p;
-            }
-            int len = end-start;
-            return new Node(name,
-                            component,
-                            mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
-                            mkNode(name+"_1", component, ports, start+len/2, end,   addr | (1 << bits), bits+1),
-                            addr,
-                            bits);
-        }
-        private String describe(String prefix, Object o) {
-            if (o==null) return null;
-            if (o instanceof InterpreterBenkoBox) {
-                InterpreterBenkoBox p = (InterpreterBenkoBox)o;
-                return prefix+"_"+getUniqueName(p.getShip())+"_"+p.getName();
-            }
-            if (o instanceof Node) {
-                return ((Node)o).describe(prefix);
-            }
-            return null;
-        }
-        private class Node {
-            Object left;
-            Object right;
-            String name;
-            String component;
-            int addr;
-            int bits;
-            public Node(String name, String component, Object left, Object right, int addr, int bits) {
-                this.left = left;
-                this.right = right;
-                this.name = name;
-                this.component = component;
-                this.addr = addr;
-                this.bits = bits;
-            }
-            public void dumpChannels(int indentamount, boolean decl) {
-                String indent = "";
-                for(int i=0; i<indentamount; i++) indent += "  ";
-                if (decl) {
-                    String n = describe(prefix).startsWith("instruction")
-                        ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
-                    System.out.println("  wire "+n+" "+indent+describe(prefix)+";");
-                } else {
-                    System.out.println("     "+indent+
-                                       component+" "+
-                                       "krunk"+(krunk++)+"(clk, "+
-                                       describe(prefix)+"_r, "+
-                                       describe(prefix)+"_a, "+
-                                       describe(prefix)+", "+
-                                       FabricTree.this.describe(prefix, left)+"_r, "+
-                                       FabricTree.this.describe(prefix, left)+"_a, "+
-                                       FabricTree.this.describe(prefix, left)+", "+
-                                       FabricTree.this.describe(prefix, right)+"_r, "+
-                                       FabricTree.this.describe(prefix, right)+"_a, "+
-                                       FabricTree.this.describe(prefix, right)+
-                                       ");");
-                }
-                dumpChannels(left, indentamount+1, decl);
-                dumpChannels(right, indentamount+1, decl);
-            }
-            public void dumpChannels(Object o, int indentamount, boolean decl) {
-                if (o==null) return;
-                if (o instanceof Node) {
-                    ((Node)o).dumpChannels(indentamount, decl);
-                } else {
-                    String indent = "";
-                    for(int i=0; i<indentamount; i++) indent += "  ";
-                    if (decl) {
-                        String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
-                            ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
-                        System.out.println("  wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
-                    }
-                }
-            }
-            public String describe(String prefix) {
-                return prefix+name;
-            }
-        }
-    }
-    public static int krunk=0;
-
-    public int computeOffset(int origin, int target) { return target - origin; }
-    public int computeTarget(int origin, int offset) { return origin + offset; }
 }
+
index d9d3311..e458136 100644 (file)
@@ -14,17 +14,17 @@ public abstract class InterpreterBenkoBox extends BenkoBox {
         ship.addBenkoBox(name, this);
     }
 
-    int bits;
-    int addr;
-    int instr_bits;
-    int instr_addr;
+    public int bits;
+    public int addr;
+    public int instr_bits;
+    public int instr_addr;
     protected boolean special = false;
-    protected boolean noInbox = false;
-    protected boolean noChannelDef = false;
+    protected boolean ihorn = false;
+    protected boolean dhorn = false;
     void service() { }
     public boolean special() { return special; }
-    public boolean noInbox() { return noInbox; }
-    public boolean noChannelDef() { return noChannelDef; }
+    public boolean ihorn() { return ihorn; }
+    public boolean dhorn() { return dhorn; }
         
     /** adds one token to the port from the switch fabric side */
     void addTokenFromFabric() { addDataFromFabric(0); }
index ee7a87e..397253d 100644 (file)
@@ -7,6 +7,7 @@ import edu.berkeley.fleet.api.Instruction;
 import edu.berkeley.fleet.api.BenkoBox;
 import edu.berkeley.fleet.*;
 import edu.berkeley.fleet.interpreter.*;
+import edu.berkeley.fleet.slipway.*;
 
 public class Main {
     static boolean debugMemory = true;
@@ -50,27 +51,27 @@ public class Main {
             System.out.println("  --memory={hide|show}");
             System.exit(-1);
         }
-        new Main().go(new InputStreamReader(System.in));
+        new Main().go();
     }
 
-    public void go(Reader r) throws Exception {
-        Interpreter fleet = new Interpreter();
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        ArrayList<Instruction> arr = new ArrayList<Instruction>();
-        edu.berkeley.fleet.assembler.Main.assemble(fleet, r, arr);
-        fleet.instructions = (Instruction[])arr.toArray(new Instruction[0]);
-
+    public void go() throws Exception {
+        Slipway fleet = new Slipway();
+        
         if (dump_fabric) {
             fleet.dumpFabric(false);
-
-        } else if (dump_code) {
-            fleet.dumpFabric(true);
-            fleet.dumpCode(fleet.instructions);
-
         } else {
-            if (debugMemory) { fleet.dumpMem(); }
-            fleet.go();
-            if (debugMemory) { fleet.dumpMem(); }
+            fleet.dumpFabric(true);
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            edu.berkeley.fleet.assembler.Main.assemble(fleet, new InputStreamReader(System.in), baos);
+            if (dump_code) {
+                DataOutputStream dos = new DataOutputStream(new FileOutputStream("build/fleet.bin"));
+                dos.write(baos.toByteArray());
+                dos.close();
+            } else {
+                if (debugMemory) { fleet.dumpMem(); }
+                fleet.go(baos.toByteArray());
+                if (debugMemory) { fleet.dumpMem(); }
+            }
         }
     }
 
similarity index 52%
rename from src/edu/berkeley/fleet/ships/Mem.java
rename to src/edu/berkeley/fleet/ships/Dcache.java
index caa6ac3..c570f87 100644 (file)
@@ -5,7 +5,7 @@ import edu.berkeley.fleet.*;
 import java.util.*;
 import java.io.*;
 
-public class Mem extends InterpreterShip {
+public class Dcache extends InterpreterShip {
 
     DataInbox   read_addr  = new DataInbox(this,   "read_addr");
     DataOutbox  read_data  = new DataOutbox(this,  "read_data");
@@ -13,26 +13,33 @@ public class Mem extends InterpreterShip {
     DataInbox   write_data = new DataInbox(this,   "write_data");
     TokenOutbox write_done = new TokenOutbox(this, "write_done");
 
-    public Mem(Interpreter fleet, String name) { super(fleet, name); }
+    public Dcache(Interpreter fleet, String name) { super(fleet, name); }
 
-    public String getBalsaName() { return "mem"; }
+    public String getBalsaName() { return "dcache"; }
+
+    private long[] mem = new long[0];
+    public long readMem(int addr) { return mem[addr]; }
+    public void writeMem(int addr, long val) {
+        if (addr >= mem.length) {
+            long[] newmem = new long[addr * 2 + 1];
+            System.arraycopy(mem, 0, newmem, 0, mem.length);
+            mem = newmem;
+        }
+        mem[addr] = val;
+    }
 
     public void service() {
         if (read_addr.dataReadyForShip() &&
             read_data.readyForItemFromShip()) {
-            Interpreter f = (Interpreter)getFleet();
-            read_data.addDataFromShip(f.readMem(read_addr.removeDataForShip()));
+            read_data.addDataFromShip((int)readMem(read_addr.removeDataForShip()));
         }
 
         if (write_addr.dataReadyForShip() &&
             write_data.dataReadyForShip() &&
             write_done.readyForItemFromShip()) {
-            Interpreter f = (Interpreter)getFleet();
-            f.writeMem(write_addr.removeDataForShip(),
-                       write_data.removeDataForShip());
+            writeMem(write_addr.removeDataForShip(),
+                     write_data.removeDataForShip());
             write_done.addTokenFromShip();
         }
-
     }
-
 }
index 43862e5..c6273f0 100644 (file)
@@ -7,9 +7,8 @@ import java.io.*;
 
 public class Debug extends InterpreterShip {
 
-    //TokenInbox token = new TokenInbox(this, "token");
-    DataInbox   data  = new DataInbox(this, "data", true, false);
-    DataOutbox  out   = new DataOutbox(this, "out", true);
+    DataInbox   data  = new DataInbox(this,  "data");
+    DataOutbox  out   = new DataOutbox(this, "out",  true);
 
     public String getBalsaName() { return "debug"; }
 
@@ -18,12 +17,6 @@ public class Debug extends InterpreterShip {
     }
     
     public void service() {
-        /*
-        if (token.tokenReadyForShip()) {
-            Log.println(Log.invert("   DEBUG: got a token"+Log.clreol()));
-            token.removeTokenForShip();
-        }
-        */
         if (data.dataReadyForShip())
             Log.println(Log.invert("   DEBUG: got a datum: " +  data.removeDataForShip()+Log.clreol()));
     }
index 3db4f64..7880738 100644 (file)
@@ -7,9 +7,9 @@ import java.io.*;
 
 public class Execute extends InterpreterShip {
 
-    DataInbox   in  = new DataInbox(this,  "in", true, false, true);
-    DataOutbox  ihorn = new DataOutbox(this, "ihorn", true);
-    DataOutbox  dhorn = new DataOutbox(this, "dhorn", true);
+    DataInbox   in    = new DataInbox(this,  "in",    true);
+    DataOutbox  ihorn = new DataOutbox(this, "ihorn", true, true, false);
+    DataOutbox  dhorn = new DataOutbox(this, "dhorn", true, false, true);
 
     public String getBalsaName() { return "execute"; }
 
diff --git a/src/edu/berkeley/fleet/ships/Icache.java b/src/edu/berkeley/fleet/ships/Icache.java
new file mode 100644 (file)
index 0000000..11b8bbf
--- /dev/null
@@ -0,0 +1,62 @@
+package edu.berkeley.fleet.ships;
+import edu.berkeley.fleet.interpreter.*;
+import edu.berkeley.fleet.*;
+import edu.berkeley.fleet.api.*;
+
+import java.util.*;
+import java.io.*;
+
+public class Icache extends InterpreterShip {
+
+    DataInbox   write_addr = new DataInbox(this,   "write_addr");
+    DataInbox   write_data = new DataInbox(this,   "write_data");
+    TokenOutbox write_done = new TokenOutbox(this, "write_done");
+
+    DataInbox   cbd        = new DataInbox(this,   "cbd");
+
+    // only for hardware fleet
+    DataInbox   command    = new DataInbox(this,  "command", true);
+    DataOutbox  ihorn      = new DataOutbox(this, "ihorn",   true, true, false);
+    DataOutbox  dhorn      = new DataOutbox(this, "dhorn",   true, false, true);
+
+    public Icache(Interpreter fleet, String name) { super(fleet, name); }
+
+    public String getBalsaName() { return "icache"; }
+
+    private long[] mem = new long[0];
+    public long readMem(int addr) { return mem[addr]; }
+    public void writeMem(int addr, long val) {
+        if (addr >= mem.length) {
+            long[] newmem = new long[addr * 2 + 1];
+            System.arraycopy(mem, 0, newmem, 0, mem.length);
+            mem = newmem;
+        }
+        mem[addr] = val;
+    }
+
+    public void dispatch(int addr, int size) {
+        for(int i=addr; i<addr+size; i++) {
+            Instruction instr = ((Interpreter)getFleet()).readInstruction(readMem(i));
+            ((Interpreter)getFleet()).dispatch(instr, i);
+        }
+    }
+
+    public void service() {
+        if (cbd.dataReadyForShip()) {
+            int val = cbd.removeDataForShip();
+            int addr = val >> 6;
+            int size = val & 0x3f;
+            dispatch(addr, size);
+        }
+
+        if (write_addr.dataReadyForShip() &&
+            write_data.dataReadyForShip() &&
+            write_done.readyForItemFromShip()) {
+            Interpreter f = (Interpreter)getFleet();
+            f.writeMem(write_addr.removeDataForShip(),
+                       write_data.removeDataForShip());
+            write_done.addTokenFromShip();
+        }
+    }
+
+}