Ship memory,
long offset,
BitVector[] vals) throws RuntimeException {
- doMem(true, fp, pool, memory, offset, vals);
- }
-
- public static void writeMem(FleetProcess fp,
- ShipPool pool,
- Ship memory,
- long offset,
- long[] vals) throws RuntimeException {
- doMem(false, fp, pool, memory, offset, long2bv(fp.getFleet(), vals));
+ try {
+ Ship alu = pool.allocateShip("Alu");
+ pool.assertAllocated(memory);
+ MemoryInputStream mos = new MemoryInputStream(fp, pool, memory, alu, offset, 1);
+ fp.flush();
+ for(int i=0; i<vals.length; i++) {
+ int pct = (int)Math.ceil(100.0*((double)(i)/((double)(vals.length-1))));
+ String status = i + "/" + (vals.length-1) + "= " + pct + "%";
+ System.out.print("\rreading from address: " + status + "...");
+ vals[i] = mos.readWord();
+ System.out.print("\rread from address: " + status + ", got " + vals[i] + " = " + vals[i].toLong()+" ");
+ }
+ mos.close();
+ pool.releaseShip(alu);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
public static void writeMem(FleetProcess fp,
- ShipPool pool,
+ ShipPool pool,
Ship memory,
long offset,
BitVector[] vals) throws RuntimeException {
- doMem(false, fp, pool, memory, offset, vals);
- }
-
- public static void doMem(final boolean read,
- final FleetProcess fp,
- final ShipPool pool,
- final Ship memory,
- final long offset,
- final BitVector[] vals) {
-
try {
- MemoryOutputStream mos = new MemoryOutputStream(fp, pool, memory, offset, read);
+ Ship alu = pool.allocateShip("Alu");
+ pool.assertAllocated(memory);
+ MemoryOutputStream mos = new MemoryOutputStream(fp, pool, memory, alu, offset);
for(int i=0; i<vals.length; i++) {
- if (read) vals[i] = mos.readWord();
- else mos.writeWord(vals[i]);
int pct = (int)Math.ceil(100.0*((double)(i)/((double)(vals.length-1))));
String status = i + "/" + (vals.length-1) + "= " + pct + "%";
- if (read) System.out.print("\rread from address: " + status + ", got " + vals[i] + " = " + vals[i].toLong()+" ");
- else System.out.print("\rwrote to address: " + status +" ");
+ System.out.print("\rwrote to address: " + status +" ");
+ mos.writeWord(vals[i]);
}
mos.close();
+ pool.releaseShip(alu);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
+ public static class MemoryOutputStream extends DockOutputStream {
- public static class MemoryOutputStream {
private FleetProcess fp;
- private ShipPool pool;
private Ship memory;
private Ship alu;
- private long offset;
- private int inflight;
- private boolean read;
public MemoryOutputStream(FleetProcess fp,
ShipPool pool,
Ship memory,
- long offset,
- boolean read) throws IOException {
- this(fp, pool, memory, offset, read, 1);
- }
+ Ship alu,
+ long offset) throws IOException {
- public MemoryOutputStream(FleetProcess fp,
- ShipPool pool,
- Ship memory,
- long offset,
- boolean read,
- int inflight) throws IOException {
+ super(fp, pool, memory.getDock("inDataWrite"), alu.getDock("out").getDataDestination());
+ pool.assertAllocated(memory);
+ pool.assertAllocated(alu);
- if (fp.getFleet() != memory.getFleet())
- throw new RuntimeException("Fleet mismatch");
this.fp = fp;
- this.pool = pool;
this.memory = memory;
- this.offset = offset;
- this.inflight = inflight;
- this.read = read;
- this.alu = pool.allocateShip("Alu");
- pool.allocateShip(fp.getDebugInputDock().getShip());
+ this.alu = alu;
- CodeBag ctx = new CodeBag(fp.getFleet());
+ buildIt(fp, memory, alu, memory.getDock("inAddrWrite"), offset);
+ CodeBag cb = new CodeBag(fp.getFleet());
LoopFactory lf;
-
- // alu.in1: receive and deliver
- lf = ctx.loopFactory(alu.getDock("in1"), 0);
- lf.abortLoopIfTorpedoPresent();
- lf.recvWord();
- lf.deliver();
-
- // alu.in2: receive tokens, deliver 1's
- lf = ctx.loopFactory(alu.getDock("in2"), 1);
- lf.literal(1);
- lf = lf.makeNext(0);
- lf.abortLoopIfTorpedoPresent();
- lf.recvToken();
- lf.deliver();
-
- // alu.inOp: receive tokens, deliver ADD's
- lf = ctx.loopFactory(alu.getDock("inOp"), 1);
- lf.literal("ADD");
- lf = lf.makeNext(0);
+ lf = cb.loopFactory(memory.getDock("out"), 0);
lf.abortLoopIfTorpedoPresent();
- lf.recvToken();
- lf.deliver();
-
- // alu.out: for each token, provide a word of count-data
- lf = ctx.loopFactory(alu.getDock("out"), 1);
- lf.literal(offset);
- lf = lf.makeNext(0);
- lf.abortLoopIfTorpedoPresent();
- lf.recvToken();
- lf.sendWord(read ? memory.getDock("inAddrRead") : memory.getDock("inAddrWrite"));
- lf.sendWord(alu.getDock("in1"));
- lf.sendToken(alu.getDock("in2"));
- lf.sendToken(alu.getDock("inOp"));
lf.collectWord();
+ cb.dispatch(fp);
+ }
- if (read) {
- // memory.inAddrRead: just recv and deliver
- lf = ctx.loopFactory(memory.getDock("inAddrRead"), 0);
- lf.abortLoopIfTorpedoPresent();
- lf.recvWord();
- lf.deliver();
+ public void close() {
+ super.close();
+ fp.sendTorpedo(memory.getDock("inAddrWrite"));
+ fp.sendTorpedo(memory.getDock("out"));
+ fp.sendTorpedo(alu.getDock("in1"));
+ fp.sendTorpedo(alu.getDock("in2"));
+ fp.sendTorpedo(alu.getDock("inOp"));
+ fp.sendTorpedo(alu.getDock("out"));
+ }
- } else {
- // memory.inDataWrite: recv a word, send a token to alu.out, deliver the word
- lf = ctx.loopFactory(memory.getDock("inDataWrite"), 0);
- lf.abortLoopIfTorpedoPresent();
- lf.recvWord();
- lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
- lf.setPredicate(Predicate.FlagA);
- lf.sendToken(fp.getDebugInputDock());
- lf.setPredicate(Predicate.NotFlagA);
- lf.sendToken(alu.getDock("out"));
- lf.deliver();
- lf.setPredicate(null);
-
- // memory.inAddrWrite: just recv and deliver
- lf = ctx.loopFactory(memory.getDock("inAddrWrite"), 0);
- lf.abortLoopIfTorpedoPresent();
- lf.recvWord();
- lf.deliver();
- }
+ }
- // memory.out: send a token to debug.in, recv a word, deliver it
- lf = ctx.loopFactory(memory.getDock("out"), 0);
- lf.abortLoopIfTorpedoPresent();
- lf.collectWord();
- if (read) lf.sendWord(fp.getDebugInputDock());
- // FIXME: perhaps feed-through here if we get fancy
- if (read) {
- lf = ctx.loopFactory(fp.getDebugInputDock(), inflight);
- lf.sendToken(alu.getDock("out"));
- lf = lf.makeNext(0);
- lf.abortLoopIfTorpedoPresent();
- lf.recvWord();
- lf.deliver();
- lf.sendToken(alu.getDock("out"));
- }
- ctx.dispatch(fp);
- fp.flush();
- }
+ public static class MemoryInputStream extends DockInputStream {
- public BitVector readWord() {
- return fp.recvWord();
- }
+ private FleetProcess fp;
+ private Ship memory;
+ private Ship alu;
- public void writeWord(BitVector bv) {
- fp.sendWord(memory.getDock("inDataWrite").getDataDestination(), bv, new BitVector(1).set(0));
+ public MemoryInputStream(FleetProcess fp,
+ ShipPool pool,
+ Ship memory,
+ Ship alu,
+ long offset,
+ int inflight) throws IOException {
+ super(fp, pool, memory.getDock("out"), alu.getDock("out").getDataDestination(), inflight);
+ pool.assertAllocated(memory);
+ pool.assertAllocated(alu);
+ this.fp = fp;
+ this.memory = memory;
+ this.alu = alu;
+ buildIt(fp, memory, alu, memory.getDock("inAddrRead"), offset);
}
public void close() {
- CodeBag ctx = new CodeBag(fp.getFleet());
- LoopFactory lf;
- lf = ctx.loopFactory(fp.getDebugInputDock(), 1);
- if (read) {
- fp.sendTorpedo(fp.getDebugInputDock());
- fp.flush();
- for(int i=0; i<inflight; i++) // FIXME: use a loop counter here
- lf.recvWord();
- lf.deliver();
- } else {
- lf.recvWord();
- lf.deliver();
- BitVector bv = new BitVector(fp.getFleet().getWordWidth());
- fp.sendWord(memory.getDock("inDataWrite").getDataDestination(), bv, new BitVector(1).set(1));
- }
- ctx.dispatch(fp);
- fp.flush();
- fp.recvWord();
-
- if (read) {
- fp.sendTorpedo(memory.getDock("inAddrRead"));
- } else {
- fp.sendTorpedo(memory.getDock("inAddrWrite"));
- fp.sendTorpedo(memory.getDock("inDataWrite"));
- }
-
+ super.close();
+ fp.sendTorpedo(memory.getDock("inAddrRead"));
fp.sendTorpedo(memory.getDock("out"));
fp.sendTorpedo(alu.getDock("in1"));
fp.sendTorpedo(alu.getDock("in2"));
fp.sendTorpedo(alu.getDock("inOp"));
fp.sendTorpedo(alu.getDock("out"));
- fp.flush();
-
- pool.releaseShip(alu);
- pool.releaseShip(fp.getDebugInputDock().getShip());
}
}
+ private static void buildIt(FleetProcess fp, Ship memory, Ship alu, Dock dest, long offset) {
+
+ CodeBag ctx = new CodeBag(fp.getFleet());
+
+ LoopFactory lf;
+ // alu.in1: receive and deliver
+ lf = ctx.loopFactory(alu.getDock("in1"), 0);
+ lf.abortLoopIfTorpedoPresent();
+ lf.recvWord();
+ lf.deliver();
+
+ // alu.in2: receive tokens, deliver 1's
+ lf = ctx.loopFactory(alu.getDock("in2"), 1);
+ lf.literal(1);
+ lf = lf.makeNext(0);
+ lf.abortLoopIfTorpedoPresent();
+ lf.recvToken();
+ lf.deliver();
+
+ // alu.inOp: receive tokens, deliver ADD's
+ lf = ctx.loopFactory(alu.getDock("inOp"), 1);
+ lf.literal("ADD");
+ lf = lf.makeNext(0);
+ lf.abortLoopIfTorpedoPresent();
+ lf.recvToken();
+ lf.deliver();
+
+ // alu.out: for each token, provide a word of count-data
+ lf = ctx.loopFactory(alu.getDock("out"), 1);
+ lf.literal(offset);
+ lf = lf.makeNext(0);
+ lf.abortLoopIfTorpedoPresent();
+ lf.recvToken();
+ lf.sendWord(dest);
+ lf.sendWord(alu.getDock("in1"));
+ lf.sendToken(alu.getDock("in2"));
+ lf.sendToken(alu.getDock("inOp"));
+ lf.collectWord();
+
+ lf = ctx.loopFactory(dest, 0);
+ lf.abortLoopIfTorpedoPresent();
+ lf.recvWord();
+ lf.deliver();
- private static BitVector[] long2bv(Fleet fleet, long[] initialValues) {
- BitVector[] bv = new BitVector[initialValues.length];
- for(int i=0; i<initialValues.length; i++)
- bv[i] = new BitVector(fleet.getWordWidth()).set(initialValues[i]);
- return bv;
+ ctx.dispatch(fp);
}
public static void putMemoryShipInDispatchMode(FleetProcess fp, Ship memoryShip) {
}
ShipPool pool = new ShipPool(fleet);
+ pool.allocateShip(memory);
writeMem(fp, pool, memory, 0, vals);
readMem(fp, pool, memory, 0, vals2);
System.out.println();