implement crappy multiplier
[fleet.git] / ships / BitFifo.ship
index 1373b36..feb2c72 100644 (file)
@@ -1,26 +1,95 @@
 ship: BitFifo
 
 == Ports ===========================================================
-data  in:   in
-
-data  in:   cmd.drop
-data  in:   cmd.dropInv
-data  in:   cmd.takeFillZeros
-data  in:   cmd.takeInvFillZeros
-data  in:   cmd.takeFillOnes
-data  in:   cmd.takeInvFillOnes
-data  in:   cmd.takeSignExtend
-data  in:   cmd.takeInvSignExtend
-
-data  out:  out
+in:   inEnqueue
+in:   inEnqueueOp
+  constant rev:      .......................1.............
+  constant inv:      ........................1............
+  constant count:    .........................nnnnnn......
+  constant offset:   ...............................nnnnnn
 
+out:  outDequeue
+in:   inDequeueOp
+  
 == Constants ========================================================
 
 == TeX ==============================================================
 
+\begin{verbatim}
++-------------+   +---------------+   +------------+
+| inEnqueue   |-->|               |-->| outDequeue |
++-------------+   |               |   +------------+
+                  |    BitFifo    |
++-------------+   |               |   +-------------+
+| inEnqueueOp |-->|               |<--| inDequeueOp |
++-------------+   +---------------+   +-------------+
+\end{verbatim}
+
+\section*{inEnqueueOp}
+\setlength{\bitwidth}{6mm}
+{\tt\footnotesize
+\begin{bytefield}{14}
+  \bitheader[b]{0,5,6,11,12,13}\\
+  \bitbox{1}{Rev} 
+  \bitbox{1}{Inv} 
+  \bitbox{6}{Count} 
+  \bitbox{6}{Offset} 
+\end{bytefield}
+}
+
+\begin{itemize}
+   \item [\tt Rev]    ({\bf Reverse Before Enqueue})
+   \item [\tt Inv]    ({\bf Invert Count}) -- treat {\tt Count} as {\tt 37-Offset-Count}
+   \item [\tt Count]  ({\bf Count of Bits To Enqueue})
+   \item [\tt Offset] ({\bf Offset of Bits To Enqueue})
+\end{itemize}
+
+By default, bits are enqueued {\it most significant bit first} (bits
+in Sun byte order).  If {\tt Rev=1}, the bits are reversed before
+performing the directions below.
+
+If {\tt Inv=1}, then the {\tt Count} field is treated as if its value
+was actually {\tt 37-Offset-Count} for all directions below.
+
+
+
+\pagebreak
+
+\section*{inDequeueOp}
+\setlength{\bitwidth}{6mm}
+{\tt\scriptsize
+\begin{bytefield}{23}
+  \bitheader[b]{0,5,6,11-21,22}\\
+  \bitbox{2}{Until} 
+  \bitbox{1}{Sort} 
+  \bitbox{1}{Get} 
+  \bitbox{1}{Rev} 
+  \bitbox{1}{Inv} 
+  \bitbox{1}{Rst} 
+  \bitbox{2}{Left\\ Fill} 
+  \bitbox{2}{Right\\ Fill} 
+  \bitbox{6}{Count} 
+  \bitbox{6}{Offset} 
+\end{bytefield}
+}
+
+\begin{itemize}
+   \item [\tt Until]  ({\bf Shift until you see a (0, 1)})
+   \item [\tt Sort]   ({\bf Sort the Bits})
+   \item [\tt Get]    ({\bf Get the value of the counter})
+   \item [\tt Rev]    ({\bf Reverse Before Enqueue})
+   \item [\tt Inv]    ({\bf Invert Count}) -- treat {\tt Count} as {\tt 37-Offset-Count}
+   \item [\tt Rst]    ({\bf Reset Counter Before Operation})
+   \item [\tt Left Fill]  ({\bf Left Fill (0, 1, sign)})
+   \item [\tt Right Fill]  ({\bf Right Fill (0, 1, sign)})
+   \item [\tt Count]  ({\bf Count of Bits To Enqueue})
+   \item [\tt Offset] ({\bf Offset of Bits To Enqueue})
+\end{itemize}
+
+
 == Fleeterpreter ====================================================
 // Internal storage
-private static class BitStorage {
+static class BitStorage {
   long[] bits;
   int numbits = 0;
   int start = 0;
@@ -38,7 +107,8 @@ private static class BitStorage {
     int entry = ((numbits + start) / 64) % bits.length;
     int off = (numbits + start) % 64;
     int maxadd = 64 - off;
-    bits[entry] = (bits[entry] & (-1L >>> maxadd)) | (data << off);
+    long mask = maxadd < 64 ? (-1L >>> maxadd) : 0L;
+    bits[entry] = (bits[entry] & mask) | (data << off);
     if (num > maxadd) {
       numbits += maxadd;
       return add(data >>> maxadd, num - maxadd);
@@ -53,11 +123,13 @@ private static class BitStorage {
     int off = start % 64;
     int max = 64 - off;
     int n = num > max ? max : num;
-    long res = (bits[entry] >>> off) & (-1L >>> (64 - n));
+    long mask = n > 0 ? (-1L >>> (64 - n)) : 0L;
+    long res = (bits[entry] >>> off) & mask;
     int oldstart = start;
     if (n < num) {
       int n2 = num - n;
-      res |= (bits[(entry + 1) % bits.length] & (-1L >>> (64 - n2))) << n;
+      long mask2 = n2 > 0 ? (-1L >>> (64 - n2)) : 0L;
+      res |= (bits[(entry + 1) % bits.length] & mask2) << n;
     }
     start = (start + num) % (64 * bits.length);
     numbits -= num;
@@ -67,6 +139,43 @@ private static class BitStorage {
     return 64 * bits.length;
   }
   // Test code for BitStorage
+  static void test3(String[] args) {
+      BitStorage bs = new BitStorage(37);
+      Random rand = new Random();
+      Vector ins = new Vector();
+      Vector ret = new Vector();
+      StringBuffer sbi = new StringBuffer();
+      StringBuffer sbr = new StringBuffer();
+      System.out.println("==== Test #3 ====");
+      System.out.println("inserting...");
+      long data = rand.nextLong();
+      bs.add(data, 0);
+      ins.add(new Integer(0));
+      data = rand.nextLong();
+      int num = rand.nextInt(37);
+      int s = bs.size();
+      bs.add(data, num);
+      assert bs.size() - s == num : "bad size: " + s + " + " + num + " != " + bs.size();
+      ins.add(new Integer(num));
+      add(sbi, data, num);
+      print(data, num);
+      System.out.println("\nretrieving...");
+      num = bs.size();
+      data = bs.get(num);
+      ret.add(new Integer(num));
+      add(sbr, data, num);
+      print(data, num);
+      System.out.println("\ninsertion sequence:");
+      for (int i = 0; i < ins.size(); i++) {
+         System.out.print(" " + ins.get(i));
+      }
+      System.out.println("\nretrieval sequence:");
+      for (int i = 0; i < ret.size(); i++) {
+         System.out.print(" " + ret.get(i));
+      }
+      System.out.println();
+      check(sbi, sbr);
+  }
   static void test2(String[] args) {
       int iters = (args.length > 0 ? Integer.parseInt(args[0]) : 10);
       BitStorage bs = new BitStorage(37 * iters);
@@ -187,6 +296,7 @@ private static class BitStorage {
 public static void main(String[] args) {
     BitStorage.test1(args);
     BitStorage.test2(args);
+    BitStorage.test3(args);
 }
 
 // Ship implementation
@@ -196,13 +306,13 @@ private static final int WORDSIZE = 37;
 private BitStorage bits = new BitStorage(MAXBITS);
 
 public void service() {
-  if (!box_out.readyForDataFromShip() && !box_in.dataReadyForShip()) return;
-  if (box_in.dataReadyForShip() && bits.hasSpace(WORDSIZE)) {
-    long data = box_in.removeDataForShip();
+  if (!box_outDequeue.readyForDataFromShip() && !box_inEnqueue.dataReadyForShip()) return;
+  if (box_inEnqueue.dataReadyForShip() && bits.hasSpace(WORDSIZE)) {
+    long data = box_inEnqueue.removeDataForShip();
     bits.add(data, WORDSIZE);
   }
-  if (selector == null && !box_cmd.dataReadyForShip()) return;
-  if (selector == null) selector = box_cmd.removePacketForShip();
+  if (selector == null && !box_inEnqueueOp.dataReadyForShip()) return;
+  if (selector == null) selector = box_inEnqueueOp.removePacketForShip();
 
   String port = selector.destination.getDestinationName();
   long val = selector.value; // check if <= 37?
@@ -215,14 +325,14 @@ public void service() {
   }
 
   if (port.startsWith("take")) {
-    if (!box_out.readyForDataFromShip()) return;
+    if (!box_outDequeue.readyForDataFromShip()) return;
     long data = bits.get((int) val);
     if (port.endsWith("FillOnes")) {
       data |= (-1L << val);
     } else if (port.endsWith("SignExtend")) {
       data = (data << (64 - val)) >> (64 - val);
     }
-    box_out.addDataFromShip(data);
+    box_outDequeue.addDataFromShip(data);
     selector = null;
     return;
   } else {