From 9721848c4e56ede5933e794473497eedaa2b1566 Mon Sep 17 00:00:00 2001 From: Astoria Date: Mon, 8 Apr 2024 13:13:31 -0500 Subject: [PATCH] Beginnings of BUS --- .../redcontrol/api/IRedBusConnectable.java | 4 --- .../net/brokenmoon/redcontrol/api/RCBus.java | 10 ++++++++ .../redcontrol/blockentities/CpuEntity.java | 24 ++++++++---------- .../blockentities/MonitorEntity.java | 3 +-- .../redcontrol/blockentities/Peripheral.java | 25 +++++++++++++++++++ .../redcontrol/blocks/CpuBlock.java | 2 +- .../redcontrol/blocks/MonitorBlock.java | 2 +- .../redcontrol/blocks/NetworkCarrier.java | 22 ++++++++++++++++ 8 files changed, 70 insertions(+), 22 deletions(-) delete mode 100644 src/main/java/net/brokenmoon/redcontrol/api/IRedBusConnectable.java create mode 100644 src/main/java/net/brokenmoon/redcontrol/api/RCBus.java create mode 100644 src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java create mode 100644 src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java diff --git a/src/main/java/net/brokenmoon/redcontrol/api/IRedBusConnectable.java b/src/main/java/net/brokenmoon/redcontrol/api/IRedBusConnectable.java deleted file mode 100644 index 6c08e1f..0000000 --- a/src/main/java/net/brokenmoon/redcontrol/api/IRedBusConnectable.java +++ /dev/null @@ -1,4 +0,0 @@ -package net.brokenmoon.redcontrol.api; - -public interface IRedBusConnectable { -} diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java b/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java new file mode 100644 index 0000000..de1d093 --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java @@ -0,0 +1,10 @@ +package net.brokenmoon.redcontrol.api; + +import com.simon816.j65el02.Bus; +import com.simon816.j65el02.device.RedBus; + +public class RCBus extends Bus { + public RCBus(RedBus redBus) { + super(redBus); + } +} diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index 84c4b25..3e101f3 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -4,7 +4,7 @@ import com.simon816.j65el02.Bus; import com.simon816.j65el02.Cpu; import com.simon816.j65el02.device.*; import net.brokenmoon.redcontrol.RedControl; -import net.brokenmoon.redcontrol.api.IRedBusConnectable; +import net.brokenmoon.redcontrol.api.RCBus; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.util.math.BlockPos; @@ -16,7 +16,7 @@ import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.Semaphore; -public class CpuEntity extends BlockEntity implements IRedBusConnectable, Runnable{ +public class CpuEntity extends Peripheral implements Runnable{ static long interruptTimer = 50L; //Adjust as needed to match original clock speed @@ -32,9 +32,7 @@ public class CpuEntity extends BlockEntity implements IRedBusConnectable, Runnab private boolean running = false; private Semaphore waiInterrupt = new Semaphore(2); - private final Bus bus; private final Cpu cpu; - private final RedBus redBus; private Path bootloader; { @@ -47,9 +45,7 @@ public class CpuEntity extends BlockEntity implements IRedBusConnectable, Runnab super(RedControl.CPU_BLOCK_ENTITY, pos, state); RedControl.LOGGER.info("Making CpuEntity"); this.cpu = new Cpu(); - this.redBus = new RedBus(); - this.bus = new Bus(this.redBus); - this.cpu.setBus(this.bus); + this.cpu.setBus(this.getBus()); Memory ram = new Memory(0x0000, 0x2000); try { @@ -57,16 +53,16 @@ public class CpuEntity extends BlockEntity implements IRedBusConnectable, Runnab } catch (IOException e) { throw new RuntimeException(e); } - this.bus.addDevice(ram); + this.getBus().addDevice(ram); reset(); } - public Bus getBus() { - return this.bus; + public RCBus getBus() { + return this.getBus(); } public void setPeripheral(int id, RedBus.Peripheral peripheral) { - this.redBus.setPeripheral(id, peripheral); + this.getBus().getRedBus().setPeripheral(id, peripheral); } public int getDefaultDriveId() { @@ -114,14 +110,14 @@ public class CpuEntity extends BlockEntity implements IRedBusConnectable, Runnab public void reset() { stop(); this.cpu.reset(); - this.bus.write(0, this.defaultDriveId); - this.bus.write(1, this.defaultMonitorId); + this.getBus().write(0, this.defaultDriveId); + this.getBus().write(1, this.defaultMonitorId); } public void step() { this.waiInterrupt.acquireUninterruptibly(); this.cpu.step(); - this.bus.update(); + this.getBus().update(); if (this.cpu.isStopped()) { this.stop(); return; diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java index ac44374..7a44e97 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java @@ -1,12 +1,11 @@ package net.brokenmoon.redcontrol.blockentities; import net.brokenmoon.redcontrol.RedControl; -import net.brokenmoon.redcontrol.api.IRedBusConnectable; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.util.math.BlockPos; -public class MonitorEntity extends BlockEntity implements IRedBusConnectable { +public class MonitorEntity extends Peripheral { public MonitorEntity(BlockPos pos, BlockState state) { super(RedControl.MONITOR_BLOCK_ENTITY, pos, state); } diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java new file mode 100644 index 0000000..e16da0a --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java @@ -0,0 +1,25 @@ +package net.brokenmoon.redcontrol.blockentities; + +import com.simon816.j65el02.Bus; +import com.simon816.j65el02.device.RedBus; +import net.brokenmoon.redcontrol.api.RCBus; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.util.math.BlockPos; + +public abstract class Peripheral extends BlockEntity { + + private RCBus bus; + public Peripheral(BlockEntityType type, BlockPos pos, BlockState state) { + super(type, pos, state); + } + + public RCBus getBus() { + return bus; + } + + public void setBus(RCBus bus) { + this.bus = bus; + } +} diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java index b19175e..6936cff 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java @@ -8,7 +8,7 @@ import net.minecraft.block.entity.BlockEntity; import net.minecraft.util.math.BlockPos; import org.jetbrains.annotations.Nullable; -public class CpuBlock extends Block implements BlockEntityProvider { +public class CpuBlock extends NetworkCarrier { public CpuBlock(Settings settings) { super(settings); } diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java b/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java index 23661ef..cde3ec0 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java @@ -8,7 +8,7 @@ import net.minecraft.block.entity.BlockEntity; import net.minecraft.util.math.BlockPos; import org.jetbrains.annotations.Nullable; -public class MonitorBlock extends Block implements BlockEntityProvider { +public class MonitorBlock extends NetworkCarrier { public MonitorBlock(Settings settings) { super(settings); } diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java new file mode 100644 index 0000000..9e88006 --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java @@ -0,0 +1,22 @@ +package net.brokenmoon.redcontrol.blocks; + +import net.brokenmoon.redcontrol.api.RCBus; +import net.brokenmoon.redcontrol.blockentities.Peripheral; +import net.minecraft.block.Block; +import net.minecraft.block.BlockEntityProvider; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +public abstract class NetworkCarrier extends Block implements BlockEntityProvider { + public NetworkCarrier(Settings settings) { + super(settings); + } + + public RCBus getBus(World world, BlockPos pos){ + return ((Peripheral)world.getBlockEntity(pos)).getBus(); + } + + public void setBus(World world, BlockPos pos, RCBus bus){ + ((Peripheral)world.getBlockEntity(pos)).setBus(bus); + } +}