mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-13 06:39:07 +00:00
Added Random number generator
This commit is contained in:
3
Plugin Misc HW/plugin.properties
Normal file
3
Plugin Misc HW/plugin.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
classpath=net.simon987.mischwplugin.MiscHWPlugin
|
||||
name=Misc HW Plugin
|
||||
version=1.0
|
||||
@@ -0,0 +1,31 @@
|
||||
package net.simon987.mischwplugin;
|
||||
|
||||
import net.simon987.mischwplugin.event.CpuInitialisationListener;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.io.CpuHardwareDeserializer;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.plugin.ServerPlugin;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class MiscHWPlugin extends ServerPlugin implements CpuHardwareDeserializer {
|
||||
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
listeners.add(new CpuInitialisationListener());
|
||||
|
||||
LogManager.LOGGER.info("Initialised Misc Hardware Plugin");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CpuHardware deserializeHardware(JSONObject hwJson) {
|
||||
int hwid = (int) (long) hwJson.get("hwid");
|
||||
|
||||
switch (hwid) {
|
||||
case RandomNumberGenerator.HWID:
|
||||
return RandomNumberGenerator.deserialize(hwJson);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package net.simon987.mischwplugin;
|
||||
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomNumberGenerator extends CpuHardware {
|
||||
|
||||
public static final char HWID = 0x0007;
|
||||
|
||||
public static final char DEFAULT_ADDRESS = 0x0007;
|
||||
|
||||
private Random random;
|
||||
|
||||
public RandomNumberGenerator() {
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(random.nextInt(0xFFFF));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static RandomNumberGenerator deserialize(JSONObject hwJSON) {
|
||||
return new RandomNumberGenerator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package net.simon987.mischwplugin.event;
|
||||
|
||||
import net.simon987.mischwplugin.RandomNumberGenerator;
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.event.CpuInitialisationEvent;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
|
||||
public class CpuInitialisationListener implements GameEventListener {
|
||||
|
||||
@Override
|
||||
public Class getListenedEventType() {
|
||||
return CpuInitialisationEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
|
||||
CPU cpu = (CPU) event.getSource();
|
||||
|
||||
cpu.attachHardware(new RandomNumberGenerator(), RandomNumberGenerator.DEFAULT_ADDRESS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user