Marina/MarinaTest.java: a few hacks to get the silicon working
[fleet.git] / ships / Counter.ship
index 0acc5b7..37aa452 100644 (file)
@@ -31,17 +31,50 @@ boolean op_c2 = false;
 boolean op_v1 = false;
 boolean op_v2 = false;
 long temp = 0;
+boolean out_draining;
 
 public void reset() {
   super.reset();
   full = false;
   temp = 0;
+  out_draining = false;
 }
 public void service() {
 
-  if (full) {
-    if (temp < 0) {
-      temp = 0;
+  if (!box_inOp.dataReadyForShip()) full = false;
+
+  if (out_draining && box_out.readyForDataFromShip()) {
+    if (op_count) temp = temp - box_in2.peekDataForShip();
+    else          temp--;
+    if (op_pass && op_v1) box_in1.removeDataForShip();
+    if (op_pass && op_v2) box_in2.removeDataForShip();
+    out_draining = false;
+
+  } else if (box_inOp.dataReadyForShip()) {
+    long op   = box_inOp.peekDataForShip();
+    op_count  = (op & 15)==12;
+    op_repeat = ((op>>2) & 3)==0;
+    op_pass   = ((op>>2) & 3)==1;
+    op_drop   = ((op>>2) & 3)==2;
+    op_c1     = (op_repeat || op_pass || op_drop) && !(((op>>1)&1)!=0);
+    op_c2     = (op_repeat || op_pass || op_drop) &&  (((op>>1)&1)!=0);
+    op_v1     = (op_repeat || op_pass || op_drop) && !(((op>>0)&1)!=0);
+    op_v2     = (op_repeat || op_pass || op_drop) &&  (((op>>0)&1)!=0);
+    if (!full) {
+      if (op_count && box_in1.dataReadyForShip() && box_in2.dataReadyForShip()) {
+        temp = box_in1.peekDataForShip() - box_in2.peekDataForShip();
+        box_in1.removeDataForShip();
+        full = true;
+      } else if (op_c1 && box_in1.dataReadyForShip()) {
+        temp = box_in1.peekDataForShip() - 1;
+        box_in1.removeDataForShip();
+        full = true;
+      } else if (op_c2 && box_in2.dataReadyForShip()) {
+        temp = box_in2.peekDataForShip() - 1;
+        box_in2.removeDataForShip();
+        full = true;
+      }
+    } else if (temp < 0) {
       full = false;
       box_inOp.removeDataForShip();
       if (op_count) box_in2.removeDataForShip();
@@ -50,38 +83,16 @@ public void service() {
 
     } else if (box_out.readyForDataFromShip()) {
       if (op_count) {
-        box_out.addDataFromShip(temp);
-        temp = temp - box_in2.peekDataForShip();
+        out_draining = true;
+        box_out.addDataFromShip(temp, (temp - box_in2.peekDataForShip()) < 0);
       } else if (op_v1 && box_in1.dataReadyForShip()) {
         if (op_drop) { box_in1.removeDataForShip(); temp--; }
-        else         { box_out.addDataFromShip(op_pass ? box_in1.removeDataForShip() : box_in1.peekDataForShip()); temp--; }
+        else         { box_out.addDataFromShip(box_in1.peekDataForShip(), temp<=0); out_draining = true; }
       } else if (op_v2 && box_in2.dataReadyForShip()) {
         if (op_drop) { box_in2.removeDataForShip(); temp--; }
-        else         { box_out.addDataFromShip(op_pass ? box_in2.removeDataForShip() : box_in2.peekDataForShip()); temp--; }
+        else         { box_out.addDataFromShip(box_in2.peekDataForShip(), temp<=0); out_draining = true; }
       }
-      
     }
-
-  } else if (box_inOp.dataReadyForShip()) {
-    long op   = box_inOp.peekDataForShip();
-    op_count  = (op & 15)==12;
-    op_repeat = ((op>>2) & 3)==0;
-    op_pass   = ((op>>2) & 3)==1;
-    op_drop   = ((op>>2) & 3)==2;
-    op_c1     = (op_repeat || op_pass || op_drop) && !(((op>>1)&1)!=0);
-    op_c2     = (op_repeat || op_pass || op_drop) &&  (((op>>1)&1)!=0);
-    op_v1     = (op_repeat || op_pass || op_drop) && !(((op>>0)&1)!=0);
-    op_v2     = (op_repeat || op_pass || op_drop) &&  (((op>>0)&1)!=0);
-
-    if (op_count && (!box_in1.dataReadyForShip() || !box_in2.dataReadyForShip())) return;
-    if (op_c1    &&  !box_in1.dataReadyForShip()) return;
-    if (op_c2    &&  !box_in2.dataReadyForShip()) return;
-    
-    full = true;
-
-    if (op_count) temp = box_in1.removeDataForShip() - box_in2.peekDataForShip();
-    if (op_c1)    temp = box_in1.removeDataForShip()-1;
-    if (op_c2)    temp = box_in2.removeDataForShip()-1;
   }
 }
 
@@ -97,6 +108,7 @@ public void service() {
   reg     out_draining;
   reg     full;
   initial full = 0;
+  reg c_flag;
   wire    op_count;  assign op_count  = inOp_d_trunc==12;
   wire    op_repeat; assign op_repeat = inOp_d[3:2]==0;
   wire    op_pass;   assign op_pass   = inOp_d[3:2]==1;
@@ -105,21 +117,27 @@ public void service() {
   wire    op_c2;     assign op_c2     = (op_repeat || op_pass || op_drop) &&  inOp_d[1];
   wire    op_v1;     assign op_v1     = (op_repeat || op_pass || op_drop) && !inOp_d[0];
   wire    op_v2;     assign op_v2     = (op_repeat || op_pass || op_drop) &&  inOp_d[0];
-  assign  out_d_ = op_v1 ? in1_d : op_v2 ? in2_d : temp;
+
+  wire [`WORDWIDTH-1:0] pre_out;
+  assign  pre_out = op_v1 ? in1_d : op_v2 ? in2_d : temp;
+  assign  out_d_  = { c_flag, pre_out };
 
   // FIXME: REPEAT with a count of zero will not work properly
 
+  wire [`WORDWIDTH-1:0] temp_minus_in2;
+  assign temp_minus_in2 = (temp - in2_d);
+
   always @(posedge clk) begin
-    if (!rst) begin
+    if (rst) begin
       `reset
       full <= 0;
       out_draining <= 0;
+      c_flag <= 0;
     end else begin
-      `flush
       `cleanup
       if (`inOp_empty)         full   <= 0;
       if (out_draining && `out_empty) begin
-        if (op_count) temp   <= temp - in2_d;
+        if (op_count) temp   <= temp_minus_in2;
         else          temp   <= temp - 1;
         if (op_pass && op_v1) `drain_in1
         if (op_pass && op_v2) `drain_in2
@@ -153,12 +171,15 @@ public void service() {
           if (op_count) begin
             `fill_out
             out_draining <= 1;
+            c_flag <= temp_minus_in2[`WORDWIDTH-1];
           end else if (op_v1 && `in1_full) begin
             if (op_drop)    begin `drain_in1 temp <= temp-1; end
             else            begin `fill_out out_draining <= 1; end
+            c_flag <= (temp==0);
           end else if (op_v2 && `in2_full) begin
             if (op_drop)    begin `drain_in2 temp <= temp-1; end
             else            begin `fill_out out_draining <= 1; end
+            c_flag <= (temp==0);
           end
         end
       end
@@ -173,16 +194,20 @@ public void service() {
 #expect 6
 #expect 3
 #expect 0
+#expect -1
 #expect 2
 #expect 1
 #expect 0
+#expect -1
 #expect 2
 #expect 2
 #expect 2
 #expect 2
+#expect -1
 #expect 9
 #expect 9
 #expect 9
+#expect -1
 
 debug.in:
   set ilc=*;
@@ -217,8 +242,12 @@ counter.inOp:
   deliver;
 
 counter.out:
-  set ilc=*;
+  head;
   collect, send to debug.in;
+  set flags a=c, b=b;
+  set word=-1;
+  [a] send to debug.in;
+  tail;
 
 
 == Contributors =========================================================