mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-13 14:49:03 +00:00
Added basic floppy drives #3.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.Memory;
|
||||
import net.simon987.server.game.ControllableUnit;
|
||||
import net.simon987.server.game.Direction;
|
||||
import net.simon987.server.game.GameObject;
|
||||
@@ -29,6 +30,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
|
||||
|
||||
private ArrayList<Integer> keyboardBuffer = new ArrayList<>();
|
||||
|
||||
private FloppyDisk floppyDisk;
|
||||
|
||||
private User parent;
|
||||
|
||||
private int energy;
|
||||
@@ -181,4 +184,9 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
|
||||
public int getMaxEnergy() {
|
||||
return maxEnergy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Memory getFloppyData() {
|
||||
return ((CubotFloppyDrive) getParent().getCpu().getHardware(CubotFloppyDrive.DEFAULT_ADDRESS)).getFloppy().getMemory();
|
||||
}
|
||||
}
|
||||
|
||||
105
Plugin Cubot/src/net/simon987/cubotplugin/CubotFloppyDrive.java
Normal file
105
Plugin Cubot/src/net/simon987/cubotplugin/CubotFloppyDrive.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class CubotFloppyDrive extends CpuHardware {
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
static final char HWID = 0x000B;
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 0x000B;
|
||||
|
||||
private static final int POLL = 1;
|
||||
private static final int READ_SECTOR = 2;
|
||||
private static final int WRITE_SECTOR = 3;
|
||||
|
||||
private Cubot cubot;
|
||||
private FloppyDisk floppyDisk;
|
||||
|
||||
public CubotFloppyDrive(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
|
||||
this.floppyDisk = new FloppyDisk();//todo remove
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
|
||||
if (a == POLL) {
|
||||
|
||||
if (floppyDisk != null) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(0);
|
||||
} else {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(1);
|
||||
}
|
||||
|
||||
} else if (a == READ_SECTOR) {
|
||||
|
||||
if (floppyDisk == null) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(0);
|
||||
} else {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(1);
|
||||
|
||||
int x = getCpu().getRegisterSet().getRegister("X").getValue();
|
||||
int y = getCpu().getRegisterSet().getRegister("Y").getValue();
|
||||
|
||||
floppyDisk.readSector(x, cubot.getParent().getCpu().getMemory(), y);
|
||||
}
|
||||
|
||||
|
||||
} else if (a == WRITE_SECTOR) {
|
||||
if (floppyDisk == null) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(0);
|
||||
} else {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(1);
|
||||
|
||||
int x = getCpu().getRegisterSet().getRegister("X").getValue();
|
||||
int y = getCpu().getRegisterSet().getRegister("Y").getValue();
|
||||
|
||||
floppyDisk.writeSector(x, cubot.getParent().getCpu().getMemory(), y);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
if (floppyDisk != null) {
|
||||
json.put("floppy", floppyDisk.serialise());
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static CubotFloppyDrive deserialize(JSONObject hwJSON) {
|
||||
|
||||
CubotFloppyDrive drive = new CubotFloppyDrive((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
|
||||
if (hwJSON.containsKey("floppy")) {
|
||||
drive.floppyDisk = FloppyDisk.deserialise((JSONObject) hwJSON.get("floppy"));
|
||||
} else {
|
||||
drive.floppyDisk = new FloppyDisk();
|
||||
}
|
||||
|
||||
return drive;
|
||||
}
|
||||
|
||||
public FloppyDisk getFloppy() {
|
||||
return floppyDisk;
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,8 @@ public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer,
|
||||
return CubotHologram.deserialize(hwJson);
|
||||
case CubotBattery.HWID:
|
||||
return CubotBattery.deserialize(hwJson);
|
||||
case CubotFloppyDrive.HWID:
|
||||
return CubotFloppyDrive.deserialize(hwJson);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
106
Plugin Cubot/src/net/simon987/cubotplugin/FloppyDisk.java
Normal file
106
Plugin Cubot/src/net/simon987/cubotplugin/FloppyDisk.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
|
||||
import net.simon987.server.assembly.Memory;
|
||||
import net.simon987.server.io.JSONSerialisable;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
/**
|
||||
* Represents a floppy disk that is inside a floppy drive.
|
||||
* Floppies contains 80 tracks with 18 sectors per track.
|
||||
* That's 1440 sectors of 512 words. (total 1,474,560 bytes / 737,280 words / 1.44Mb)
|
||||
*/
|
||||
public class FloppyDisk implements JSONSerialisable {
|
||||
|
||||
/**
|
||||
* Contents of the disk
|
||||
*/
|
||||
private Memory memory;
|
||||
|
||||
/**
|
||||
* Current location of the read/write head.
|
||||
* Used to calculate seek time
|
||||
*/
|
||||
private int rwHeadTrack = 0;
|
||||
|
||||
|
||||
public FloppyDisk() {
|
||||
this.memory = new Memory(1024 * 1440);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read 512 words from the specified sector to cpu memory at specified address
|
||||
*
|
||||
* @param sector sector to read (0-1440)
|
||||
* @param cpuMemory Cpu memory to write to
|
||||
* @param ramAddress address of the data to write in CPU memory
|
||||
* @return Whether or not the read operation was in the same track as the last r/w
|
||||
*/
|
||||
public boolean readSector(int sector, Memory cpuMemory, int ramAddress) {
|
||||
|
||||
cpuMemory.write(ramAddress, memory.getBytes(), sector * 512, 1024);
|
||||
|
||||
LogManager.LOGGER.fine("Read 512 words from floppy sector:" + sector + " to memory addr:" + ramAddress);
|
||||
|
||||
//Calculate seek time
|
||||
int deltaTrack = (sector / 80) - rwHeadTrack;
|
||||
|
||||
if (deltaTrack != 0) {
|
||||
rwHeadTrack = (sector / 80);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write 512 words to the specified sector from cpu memory at the specified address
|
||||
*
|
||||
* @param sector sector to write (0-1440)
|
||||
* @param cpuMemory Cpu memory to read from
|
||||
* @param ramAddress address of the data to read in CPU memory
|
||||
* @return Whether or not the read operation was in the same track as the last r/w
|
||||
*/
|
||||
public boolean writeSector(int sector, Memory cpuMemory, int ramAddress) {
|
||||
|
||||
memory.write(sector * 512, cpuMemory.getBytes(), ramAddress * 2, 1024);
|
||||
|
||||
LogManager.LOGGER.fine("Wrote 512 words to floppy sector:" + sector + " from memory addr:" + ramAddress);
|
||||
|
||||
//Calculate seek time
|
||||
int deltaTrack = (sector / 80) - rwHeadTrack;
|
||||
|
||||
if (deltaTrack != 0) {
|
||||
rwHeadTrack = (sector / 80);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("rwHeadTrack", rwHeadTrack);
|
||||
json.put("memory", memory.serialise());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static FloppyDisk deserialise(JSONObject json) {
|
||||
|
||||
FloppyDisk floppyDisk = new FloppyDisk();
|
||||
|
||||
floppyDisk.rwHeadTrack = (int) (long) json.get("rwHeadTrack");
|
||||
floppyDisk.memory = Memory.deserialize((JSONObject) json.get("memory"));
|
||||
|
||||
return floppyDisk;
|
||||
}
|
||||
|
||||
public Memory getMemory() {
|
||||
return memory;
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,8 @@ public class CpuInitialisationListener implements GameEventListener {
|
||||
emoteHw.setCpu(cpu);
|
||||
CubotBattery batteryHw = new CubotBattery((Cubot) user.getControlledUnit());
|
||||
batteryHw.setCpu(cpu);
|
||||
CubotFloppyDrive floppyHw = new CubotFloppyDrive((Cubot) user.getControlledUnit());
|
||||
floppyHw.setCpu(cpu);
|
||||
|
||||
cpu.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(laserHw, CubotLaser.DEFAULT_ADDRESS);
|
||||
@@ -47,5 +49,6 @@ public class CpuInitialisationListener implements GameEventListener {
|
||||
cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(emoteHw, CubotHologram.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(batteryHw, CubotBattery.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(floppyHw, CubotFloppyDrive.DEFAULT_ADDRESS);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user