get rid of ChainControls
[fleet.git] / ships / Memory.ship
index c8cd957..2d1d6f1 100644 (file)
@@ -2,19 +2,101 @@ ship: Memory
 
 == Ports ===========================================================
 data  in:    inCBD
-data  in:    inAddr.read
-data  in:    inAddr.write
-data  in:    inAddr.readMany
-data  in:    inAddr.writeMany
-data  in:    inData
-data  in:    inStride
-data  in:    inCount
+data  in:    inAddrRead
+data  in:    inAddrWrite
+data  in:    inDataWrite
 
 data  out:   out
 
+== TeX ==============================================================
+
+The {\tt Memory} ship represents an interface to a storage space,
+which can be used to read from it or write to it.  This storage space
+might be a fast on-chip cache, off chip DRAM, or perhaps even a disk
+drive.
+
+Generally, distinct {\tt Memory} ships do not access the same backing
+storage, although this is not strictly prohibited.
+
+Each {\tt Memory} ship may have multiple {\it interfaces}, numbered
+starting with {\tt 0}.  Each interface may have any subset of the
+following docks: {\tt inCBD}, {\tt inAddrRead}, {\tt inAddrWrite},
+{\tt inDataWrite}, and {\tt out}.  If {\tt inCBD} or {\tt inAddrRead}
+is present on an interface, then {\tt out} must be present as well.
+If {\tt inAddrWrite} is present then {\tt inDataWrite} must be present
+as well.
+
+Each interface serializes the operations presented to it; this means
+that an interface with both read and write capabilities will not be
+able to read and write concurrently.  Instead, a {\tt Memory} ship
+with the ability to read and write concurrently should have two
+interfaces, one which is read-only and one which is write-only.
+
+There may be multiple {\tt Memory} ships which interface to the same
+physical storage space.  An implementation of Fleet must provide
+additional documentation to the programmer indicating which {\tt
+Memory} ships correspond to which storage spaces.  A single {\tt
+Memory} ship may also access a ``virtual storage space'' formed by
+concatenating multiple physical storage spaces.
+
+\subsection*{Code Bag Fetch}
+
+When a word appears at the {\tt inCBD} port, it is treated as a {\it
+code bag descriptor}, as shown below:
+
+\begin{center}
+\setlength{\bitwidth}{3mm}
+{\tt
+\begin{bytefield}{37}
+  \bitheader[b]{36,6,5,0}\\
+  \bitbox{31}{Address} 
+  \bitbox{6}{size} 
+\end{bytefield}
+}
+\end{center}
+
+When a word arrives at the {\tt inCBD} port, it is treated as a memory
+read with {\tt inAddrRead=Address}, {\tt inStride=1}, and {\tt
+inCount=size}.
+
+\subsection*{Reading}
+
+When a word is delivered to {\tt inAddrRead}, the word residing in
+memory at that address is provided at {\tt out}.  The {\tt c-flag} at
+the {\tt out} port is set to zero.
+
+\subsection*{Writing}
+
+When a word is delivered to {\tt inAddrWrite} and {\tt inDataWrite},
+the word at {\tt inDataWrite} is written to the address specified by
+{\tt inAddrWrite}.  Once the word is successfully committed to memory,
+the value {\tt inAddr+inStride} is provided at {\tt out} (that is, the
+address of the next word to be written).  The {\tt c-flag} at
+the {\tt out} port is set to one.
+
+\subsection*{To Do}
+
+Stride and count are not implemented.
+
+We need a way to do an ``unordered fetch'' -- a way to tell the memory
+unit to retrieve some block of words in any order it likes.  This can
+considerably accelerate fetches when the first word of the region is
+not cached, but other parts are cached.  This can also be used for
+dispatching codebags efficiently -- but how will we make sure that
+instructions destined for a given pump are dispatched in the correct
+order (source sequence guarantee)?
+
+A more advanced form would be ``unordered fetch of ordered records''
+-- the ability to specify a record size (in words), the offset of the
+first record, and the number of records to be fetched.  The memory
+unit would then fetch the records in any order it likes, but would be
+sure to return the words comprising a record in the order in which
+they appear in memory.  This feature could be used to solve the source
+sequence guarantee problem mentioned in the previous paragraph.
+
 == Fleeterpreter ====================================================
     private long[] mem = new long[0];
-    public long readMem(int addr) { return mem[addr]; }
+    public long readMem(int addr) { return addr >= mem.length ? 0 : mem[addr]; }
     public void writeMem(int addr, long val) {
         if (addr >= mem.length) {
             long[] newmem = new long[addr * 2 + 1];
@@ -23,144 +105,144 @@ data  out:   out
         }
         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 boot(byte[] instructions) {
-        Interpreter fleet = (Interpreter)getFleet();
-        // load the iscratch and take note of the 0-address INCBD
-        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);
-            writeMem(i/6, word);
-            if (i==0) launch = word;
-        }
-
-        // dispatch the 0-address INCBD
-        int base = (int)(launch >> 6);
-        base = base & ~(0xffffffff << 18);
-        int size = (int)launch;
-        size = size & ~(0xffffffff <<  6);
-        dispatch(base, size);
+    private Queue<Long> toDispatch = new LinkedList<Long>();
+    public void reset() {
+      super.reset();
+      mem = new long[0];
+      toDispatch.clear();
     }
-
     public void service() {
+        if (toDispatch.size() > 0) {
+            if (!box_out.readyForDataFromShip()) return;
+            box_out.addDataFromShip(toDispatch.remove());
+        }
         if (box_inCBD.dataReadyForShip()) {
             long val = box_inCBD.removeDataForShip();
-            long addr = val >> 6;
-            long size = val & 0x3f;
-            dispatch((int)addr, (int)size);
-        }
-        if (box_inAddr.dataReadyForShip() && box_out.readyForItemFromShip()) {
-            Packet packet = box_inAddr.peekPacketForShip();
-            if (packet.destination.getDestinationName().equals("read")) {
-                box_out.addDataFromShip(readMem((int)box_inAddr.removeDataForShip()));
-            } else if (packet.destination.getDestinationName().equals("write") && box_inData.dataReadyForShip()) {
-                writeMem((int)box_inAddr.removeDataForShip(),
-                         box_inData.removeDataForShip());
-                box_out.addDataFromShip(0);
-            }
+            long addr = ((Interpreter)getFleet()).CBD_OFFSET.getval(val);
+            long size = ((Interpreter)getFleet()).CBD_SIZE.getval(val);
+            for(int i=0; i<size; i++)
+              toDispatch.add(readMem((int)(addr+i)));
+        } else if (box_inAddrWrite.dataReadyForShip() && box_inDataWrite.dataReadyForShip() && box_out.readyForDataFromShip()) {
+            writeMem((int)box_inAddrWrite.removeDataForShip(), box_inDataWrite.removeDataForShip());
+            box_out.addDataFromShip(0,true);
+        } else if (box_inAddrRead.dataReadyForShip() && box_out.readyForDataFromShip()) {
+            box_out.addDataFromShip(readMem((int)box_inAddrRead.removeDataForShip()),false);
         }
     }
 
 == FleetSim ==============================================================
 
 == FPGA ==============================================================
-`include "macros.v"
-`define BRAM_ADDR_WIDTH 14
-`define BRAM_DATA_WIDTH `DATAWIDTH
-`define BRAM_NAME dscratch_bram
-`include "bram.inc"
-
-module dscratch (clk, 
-               read_addr_r,    read_addr_a_,   read_addr_d,
-               read_data_r_,   read_data_a,    read_data_d_,
-               write_addr_r,   write_addr_a_,  write_addr_d,
-               write_data_r,   write_data_a_,  write_data_d,
-               write_done_r_,  write_done_a,   write_done_d_
-              );
-
-  input  clk;
-  `input(read_addr_r,    read_addr_a,   read_addr_a_,   [(`DATAWIDTH-1):0],  read_addr_d)
-  `output(read_data_r,   read_data_r_,  read_data_a,    [(`DATAWIDTH-1):0],  read_data_d_)
-  `defreg(read_data_d_,                                 [(`DATAWIDTH-1):0],  read_data_d)
-
-  `input(write_addr_r,   write_addr_a,  write_addr_a_,  [(`DATAWIDTH-1):0],  write_addr_d)
-  `input(write_data_r,   write_data_a,  write_data_a_,  [(`DATAWIDTH-1):0],  write_data_d)
-  `output(write_done_r,  write_done_r_, write_done_a,   [(`DATAWIDTH-1):0],  write_done_d_)
-  `defreg(write_done_d_,                                [(`DATAWIDTH-1):0],  write_done_d)
-
-  reg                           bram_we;
-  wire                          bram_we_;
-  assign bram_we_ = bram_we;
-  wire [(`BRAM_DATA_WIDTH-1):0] bram_read_data;
-  reg  [(`BRAM_ADDR_WIDTH-1):0] bram_write_address;
-  wire [(`BRAM_ADDR_WIDTH-1):0] bram_read_address;
-  reg  [(`BRAM_DATA_WIDTH-1):0] bram_write_data;
-  wire [(`BRAM_DATA_WIDTH-1):0] bram_write_data_;
-  assign bram_write_data_ = bram_write_data;
-  `BRAM_NAME mybram(clk,
-                    bram_we_,          bram_write_address,
-                    bram_read_address, bram_write_data_,
-                    not_connected,     bram_read_data);
-
-  reg send_done;
-
-  reg have_read;    initial have_read = 0;
-  reg read_pending; initial read_pending = 0;
-  assign bram_read_address = read_addr_d;
 
+  `define BRAM_ADDR_WIDTH 14
+  `define BRAM_SIZE (1<<(`BRAM_ADDR_WIDTH))
+
+  reg    [(`WORDWIDTH-1):0]       ram [((`BRAM_SIZE)-1):0];
+  reg    [(`BRAM_ADDR_WIDTH-1):0] addr1;
+  reg    [(`BRAM_ADDR_WIDTH-1):0] addr2;
+  reg    [(`WORDWIDTH-1):0]       out1;
+  reg    [(`WORDWIDTH-1):0]       out2;
+
+  reg                             out_w;
+  reg                             write_flag;
+  reg [(`BRAM_ADDR_WIDTH-1):0]    cursor;
+  reg [(`CODEBAG_SIZE_BITS-1):0]  counter;
+
+  assign out_d_ = { out_w, out1 };
+
+  // I use "blocking assignment" here in order to facilitate BRAM inference
   always @(posedge clk) begin
-    bram_we = 0;
-    if (send_done) begin
-      `onwrite(write_done_r, write_done_a)
-        send_done = 0;
-      end
-    end else begin
-      if (!write_addr_r && write_addr_a) write_addr_a = 0;
-      if (!write_data_r && write_data_a) write_data_a = 0;
-      if (write_addr_r && write_data_r) begin
-        write_addr_a = 1;
-        write_data_a = 1;
-        bram_we = 1;
-        send_done = 1;
-        bram_write_address = write_addr_d;
-        bram_write_data = write_data_d;
-      end
-    end
+    write_flag = 0;
 
-    if (read_pending) begin
-        read_pending <= 0;
-        have_read    <= 1;
-        read_data_d  <= bram_read_data;
-    end else if (have_read) begin
-      `onwrite(read_data_r, read_data_a)
-        have_read <= 0;
-      end
+    if (rst) begin
+      `reset
+      cursor      = 0;
+      counter     = 0;
     end else begin
-      `onread(read_addr_r, read_addr_a)
-        // ======= Careful with the timing here! =====================
-        // We MUST capture bram_read_data on the very next clock since
-        // read_addr_d is free to change after the next clock
-        // ===========================================================
-        read_pending <= 1;
+      `cleanup
+
+      if (counter!=0) begin
+        if (`out_empty) begin
+          `fill_out
+          out_w    = 0;
+          addr1    = cursor;
+          cursor   = cursor  + 1;
+          counter  = counter - 1;
+        end
+
+      end else if (`inCBD_full) begin
+        cursor    = inCBD_d[(`WORDWIDTH-1):(`CODEBAG_SIZE_BITS)];
+        counter   = inCBD_d[(`CODEBAG_SIZE_BITS-1):0];
+        addr1     = cursor;
+        `drain_inCBD
+
+      end else if (`out_empty && `inAddrRead_full) begin
+        addr1     = inAddrRead_d[(`WORDWIDTH-1):0];
+        `drain_inAddrRead
+        `fill_out
+        out_w     = 0;
+
+      end else if (`out_empty && `inAddrWrite_full && `inDataWrite_full) begin
+        write_flag = 1;
+        `drain_inAddrWrite
+        `drain_inDataWrite
+        `fill_out
+        addr2     = inAddrWrite_d[(`WORDWIDTH-1):0];
+        out_w     = 1;
+
       end
     end
 
+    // this must appear at the end of the block, outside of any if..then's
+    if (write_flag) 
+      ram[addr2] <= inDataWrite_d; 
+    out1 <= ram[addr1];
+    out2 <= ram[addr2]; 
   end
+    
+
+
 
-endmodule
+== Test ==============================================================
+// Note: this only tests the read/write interfaces, not the inCBD interface
+// FIXME: test c-flag at out dock
+
+// expected output
+#expect 10
+
+// ships required in order to run this code
+#ship debug          : Debug
+#ship memory         : Memory
+
+memory.inAddrWrite:
+  set word=3;
+  deliver;
+  deliver;
+
+memory.inDataWrite:
+  set word=4;
+  deliver;
+  set word=10;
+  deliver;
+
+memory.inAddrRead:
+  recv token;
+  set word=3;
+  deliver;
+
+memory.out:
+  collect;
+  collect;
+  send token to memory.inAddrRead;
+  collect;
+  send to debug.in;
+
+debug.in:
+  set ilc=*;
+  recv, deliver;
 
 
 == Constants ========================================================
-== TeX ==============================================================
 
 == Contributors =========================================================
 Adam Megacz <megacz@cs.berkeley.edu>