diff --git a/J65el02 b/J65el02 index e82ee86..d4975fa 160000 --- a/J65el02 +++ b/J65el02 @@ -1 +1 @@ -Subproject commit e82ee867d35793695a3d5013f654c1732c06b74c +Subproject commit d4975fa8a08d2026086a08b7eae6543696b61cf7 diff --git a/src/main/java/net/brokenmoon/redcontrol/api/FakeBus.java b/src/main/java/net/brokenmoon/redcontrol/api/FakeBus.java deleted file mode 100644 index 4a89d01..0000000 --- a/src/main/java/net/brokenmoon/redcontrol/api/FakeBus.java +++ /dev/null @@ -1,9 +0,0 @@ -package net.brokenmoon.redcontrol.api; - -import com.simon816.j65el02.device.RedBus; - -public class FakeBus extends RCBus{ - public FakeBus(RedBus redBus) { - super(redBus); - } -} diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java b/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java index 6f05e12..8335ca2 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java @@ -1,6 +1,7 @@ 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.RedControl; import net.brokenmoon.redcontrol.blockentities.Peripheral; @@ -8,26 +9,42 @@ import net.brokenmoon.redcontrol.blocks.NetworkCarrier; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import java.util.Arrays; + public class RCBus extends Bus { private boolean isValid = false; private RedBus redBus; - public RCBus(RedBus redBus) { + private World backupWorld; + private BlockPos backupPos; + + public RCBus(RedBus redBus, World world, BlockPos pos) { super(redBus); this.redBus = redBus; + backupWorld = world; + backupPos = pos; } public void write(int address, int data, World world, BlockPos pos) { if(isValid) { - RedControl.LOGGER.info("Writing! " + address + " " + data); getRedBus().write(address, data); } else { generateBusWithWrite(address, data, world, pos); } } + public void write(int address, int data) { + Device device = findDevice(address); + device.write(address - device.startAddress(), data); + } + + public int read(int address, boolean cpuAccess) { + Device device = findDevice(address); + return device.read(address - device.startAddress(), cpuAccess) & 0xff; + } + private void generateBusWithWrite(int address, int data, World world, BlockPos pos) { ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBusWithWrite(world, pos, address, data); } @@ -44,6 +61,28 @@ public class RCBus extends Bus { private int generateBusWithRead(int address, boolean cpuAccess, World world, BlockPos pos) { return ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBusWithRead(world, pos, address, cpuAccess); } + @Override + protected Device findDevice(int address) { + // RedBus takes priority + if (this.redBus.inRange(address)) { + return this.redBus; + } + int idx = Arrays.binarySearch(this.boundaries, address); + if (idx < 0) { + idx = -idx - 2; + } + return this.devices.get(idx); + } + + @Override + public void write(int address, int data) { + write(address, data, backupWorld, backupPos); + } + + @Override + public int read(int address, boolean cpuAccess) { + return read(address, cpuAccess, backupWorld, backupPos); + } public void generateBus(World world, BlockPos pos){ ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBus(world, pos); @@ -57,8 +96,9 @@ public class RCBus extends Bus { this.redBus = redBus; } + @Override public void update() { - this.redBus.updatePeripheral(); + this.getRedBus().updatePeripheral(); } public boolean getValid(){ diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java b/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java index 2713a61..8cd55f2 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java @@ -3,6 +3,7 @@ package net.brokenmoon.redcontrol.api; import com.simon816.j65el02.Bus; import com.simon816.j65el02.Cpu; import com.simon816.j65el02.CpuState; +import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.blockentities.CpuEntity; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -13,6 +14,7 @@ public class RCCpu extends Cpu { public RCCpu(CpuEntity cpuEntity) { this.cpuEntity = cpuEntity; + this.setBus(this.cpuEntity.getBus()); } public void setInterrupt(boolean val){ @@ -20,18 +22,16 @@ public class RCCpu extends Cpu { } @Override - private int readByte(int address) { - return this.cpuEntity.getBus().read(address, true); + protected int readByte(int address) { + return this.cpuEntity.getBus().getRedBus().read(address, true); } @Override - private void writeMemory(int address, int value, boolean x) { + protected void writeMemory(int address, int value, boolean x) { this.getBus().write(address, value); boolean flag = x ? this.state.indexWidthFlag : this.state.mWidthFlag; if (!this.state.emulationFlag && !flag) { - this.cpuEntity.getBus().write(address + 1, value >>> 8); + this.cpuEntity.getBus().getRedBus().write(address + 1, value >>> 8); } } - - @Override } diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index b3db297..507427a 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -1,9 +1,7 @@ package net.brokenmoon.redcontrol.blockentities; -import com.simon816.j65el02.Bus; import com.simon816.j65el02.device.*; import net.brokenmoon.redcontrol.RedControl; -import net.brokenmoon.redcontrol.api.FakeBus; import net.brokenmoon.redcontrol.api.RCBus; import net.brokenmoon.redcontrol.api.RCCpu; import net.minecraft.block.BlockState; @@ -13,10 +11,6 @@ import net.minecraft.world.World; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Date; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.Semaphore; public class CpuEntity extends Peripheral{ public int i = 0; @@ -41,8 +35,8 @@ public class CpuEntity extends Peripheral{ public static void tick(World world, BlockPos pos, BlockState state, CpuEntity be) { if(be.notTicked) { be.cpu = new RCCpu(be); - be.setRCBus(new RCBus(new RedBus())); - be.cpu.setBus((FakeBus)be.getRCBus()); + be.setRCBus(new RCBus(new RedBus(), world, pos)); + be.cpu.setBus(be.getRCBus()); be.cpu.setBus(be.getRCBus()); Memory ram = new Memory(0x0000, 0x2000); try { @@ -54,7 +48,7 @@ public class CpuEntity extends Peripheral{ be.reset(); be.notTicked = false; } - for(int i = 0; i < 50; i++) + for(int i = 0; i < 500; i++) be.step(); if(be.cpu.isWaitingForInterrupt()){ be.cpu.setInterrupt(false); diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java index 56bef95..c98f3b4 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java @@ -30,12 +30,12 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt @Override public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) { - + generateBus(world, pos); } @Override public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { - if(world.getBlockEntity(pos) instanceof Peripheral){ + if(state.hasBlockEntity() && world.getBlockEntity(pos) instanceof Peripheral){ ((Peripheral) world.getBlockEntity(pos)).getBus().setValid(false); } if (state.hasBlockEntity() && !state.isOf(newState.getBlock())) { @@ -55,7 +55,7 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt if(world.getBlockEntity(pos) instanceof Peripheral){ Peripheral entityBlock = (Peripheral) world.getBlockEntity(pos); if(entityBlock.getBus() == null) - entityBlock.setBus(new RCBus(new RedBus())); + entityBlock.setBus(new RCBus(new RedBus(), world, pos)); entityBlock.getBus().getRedBus().setPeripheral(entityBlock.getId(), entityBlock); entityBlock.getBus().setValid(true); floodBus(entityBlock.getBus(), world, pos);