diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java b/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java index 51a7456..d1ba666 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java @@ -1,28 +1,63 @@ package net.brokenmoon.redcontrol.api; -import com.simon816.j65el02.Bus; -import com.simon816.j65el02.device.Device; import com.simon816.j65el02.device.RedBus; import net.brokenmoon.redcontrol.blockentities.Peripheral; +import net.brokenmoon.redcontrol.blocks.NetworkCarrier; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; -import java.util.Arrays; +public class RCBus { -public class RCBus extends Bus { + boolean isValid = false; - boolean isValid = true; + private RedBus redBus; Peripheral[] peripherals = new Peripheral[256]; public RCBus(RedBus redBus) { - super(redBus); + this.redBus = redBus; } - public void write(int address, int data) { - Peripheral peripheral = peripherals[address]; - peripheral.getBus().write(address, data); + public void write(int address, int data, World world, BlockPos pos) { + if(isValid) { + Peripheral peripheral = peripherals[address]; + peripheral.getBus().getRedBus().write(address, data); + } else { + generateBus(world, pos); + write(address, data, world, pos); + } } - public int read(int address, boolean cpuAccess) { - Peripheral peripheral = peripherals[address]; - return peripheral.getBus().getRedBus().read(address, cpuAccess) & 0xff; + public int read(int address, boolean cpuAccess, World world, BlockPos pos) { + if(isValid) { + Peripheral peripheral = peripherals[address]; + return peripheral.getBus().getRedBus().read(address, cpuAccess) & 0xff; + } else { + generateBus(world, pos); + return read(address, cpuAccess, world, pos); + } + } + + public void generateBus(World world, BlockPos pos){ + ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBus(world, pos); + } + + public RedBus getRedBus() { + return redBus; + } + + public void setRedBus(RedBus redBus) { + this.redBus = redBus; + } + + public void update() { + this.redBus.updatePeripheral(); + } + + public Peripheral[] getPeripherals(){ + return peripherals; + } + + public void setPeripherals(Peripheral[] peripherals){ + this.peripherals = peripherals; } } diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java b/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java new file mode 100644 index 0000000..1e9bb05 --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java @@ -0,0 +1,20 @@ +package net.brokenmoon.redcontrol.api; + +import com.simon816.j65el02.Cpu; + +public class RCCpu extends Cpu { + + private RCBus rcbus; + + public RCCpu() { + } + + public RCBus getRCBus() { + return rcbus; + } + + + public void setRCBus(RCBus rcbus) { + this.rcbus = rcbus; + } +} diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index 3e101f3..714ceee 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -5,6 +5,8 @@ import com.simon816.j65el02.Cpu; import com.simon816.j65el02.device.*; import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.api.RCBus; +import net.brokenmoon.redcontrol.api.RCCpu; +import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.util.math.BlockPos; @@ -32,7 +34,8 @@ public class CpuEntity extends Peripheral implements Runnable{ private boolean running = false; private Semaphore waiInterrupt = new Semaphore(2); - private final Cpu cpu; + private final RCCpu cpu; + private RCBus rcbus; private Path bootloader; { @@ -42,25 +45,31 @@ public class CpuEntity extends Peripheral implements Runnable{ private int defaultMonitorId = 1; private int defaultDriveId = 2; public CpuEntity(BlockPos pos, BlockState state) { - super(RedControl.CPU_BLOCK_ENTITY, pos, state); + super(RedControl.CPU_BLOCK_ENTITY, pos, state, 0); RedControl.LOGGER.info("Making CpuEntity"); - this.cpu = new Cpu(); - this.cpu.setBus(this.getBus()); - + this.cpu = new RCCpu(); + this.setRCBus(new RCBus(new RedBus())); + this.cpu.setRCBus(this.getRCBus()); + this.cpu.setBus(new Bus(this.getRCBus().getRedBus())); Memory ram = new Memory(0x0000, 0x2000); try { ram.loadFromFile(bootloader, 0x400, 0x100); } catch (IOException e) { throw new RuntimeException(e); } - this.getBus().addDevice(ram); + cpu.getBus().addDevice(ram); reset(); } - public RCBus getBus() { - return this.getBus(); + public RCBus getRCBus(){ + return this.rcbus; + } + + public void setRCBus(RCBus bus){ + this.rcbus = bus; } + public void setPeripheral(int id, RedBus.Peripheral peripheral) { this.getBus().getRedBus().setPeripheral(id, peripheral); } @@ -110,14 +119,14 @@ public class CpuEntity extends Peripheral implements Runnable{ public void reset() { stop(); this.cpu.reset(); - this.getBus().write(0, this.defaultDriveId); - this.getBus().write(1, this.defaultMonitorId); + this.getRCBus().write(0, this.defaultDriveId, this.getWorld(), this.getPos()); + this.getRCBus().write(1, this.defaultMonitorId, this.getWorld(), this.getPos()); } public void step() { this.waiInterrupt.acquireUninterruptibly(); this.cpu.step(); - this.getBus().update(); + this.getRCBus().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 7a44e97..27e9198 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java @@ -7,6 +7,6 @@ import net.minecraft.util.math.BlockPos; public class MonitorEntity extends Peripheral { public MonitorEntity(BlockPos pos, BlockState state) { - super(RedControl.MONITOR_BLOCK_ENTITY, pos, state); + super(RedControl.MONITOR_BLOCK_ENTITY, pos, state, 1); } } diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java index e16da0a..13b429f 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java @@ -11,8 +11,12 @@ import net.minecraft.util.math.BlockPos; public abstract class Peripheral extends BlockEntity { private RCBus bus; - public Peripheral(BlockEntityType type, BlockPos pos, BlockState state) { + + private int id; + + public Peripheral(BlockEntityType type, BlockPos pos, BlockState state, int id) { super(type, pos, state); + this.id = id; } public RCBus getBus() { @@ -22,4 +26,12 @@ public abstract class Peripheral extends BlockEntity { public void setBus(RCBus bus) { this.bus = bus; } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } } diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java index e17519d..8a5e2f0 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java @@ -1,10 +1,12 @@ package net.brokenmoon.redcontrol.blocks; +import com.simon816.j65el02.device.RedBus; 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.block.BlockState; +import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; @@ -39,4 +41,14 @@ public abstract class NetworkCarrier extends Block implements BlockEntityProvide public void invalidateBus(World world, BlockPos pos){ } + + public void generateBus(World world, BlockPos pos){ + Block worldBlock = world.getBlockState(pos).getBlock(); + if(world.getBlockEntity(pos) instanceof Peripheral){ + Peripheral entityBlock = (Peripheral) world.getBlockEntity(pos); + entityBlock.getBus().setPeripherals(new Peripheral[255]); + entityBlock.getBus().getPeripherals()[entityBlock.getId()] = entityBlock; + } + + } }