diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java b/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java index 2cb7080..26e67c3 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java @@ -14,4 +14,5 @@ public class RCCpu extends Cpu { this.world = world; this.pos = pos; } + } diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index 90f5b4c..fed9b1e 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -17,23 +17,11 @@ import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.Semaphore; -public class CpuEntity extends Peripheral implements Runnable{ +public class CpuEntity extends Peripheral{ + public int i = 0; - static long interruptTimer = 50L; - //Adjust as needed to match original clock speed - - TimerTask cpuTask = new TimerTask(){ - public void run(){ - for(int i = 0; i < 50; i++) - step(); - } - }; - - static Timer timer = new Timer(); boolean notTicked = true; - private boolean running = false; - private Semaphore waiInterrupt = new Semaphore(2); - private RCCpu cpu; + public RCCpu cpu; private RCBus rcbus; private Path bootloader; @@ -54,7 +42,7 @@ public class CpuEntity extends Peripheral implements Runnable{ be.cpu = new RCCpu(world, pos); be.setRCBus(new RCBus(new RedBus())); be.cpu.setBus(be.getRCBus()); - be.cpu.setBus(new Bus(be.getRCBus().getRedBus())); + be.cpu.setBus(be.getRCBus()); Memory ram = new Memory(0x0000, 0x2000); try { ram.loadFromFile(be.bootloader, 0x400, 0x100); @@ -64,12 +52,12 @@ public class CpuEntity extends Peripheral implements Runnable{ be.cpu.getBus().addDevice(ram); be.reset(); be.notTicked = false; - be.start(); } - } - - public void start(){ - timer.schedule(cpuTask, new Date(), 2L); + for(int i = 0; i < 50; i++) + be.step(); + if(be.cpu.isWaitingForInterrupt()){ + be.cpu.setInterrupt(false); + } } @@ -102,47 +90,19 @@ public class CpuEntity extends Peripheral implements Runnable{ this.defaultMonitorId = id; } - public boolean isRunning() { - return this.running; - } - public void run() { - this.running = true; - do { - step(); - } while (this.running); - } - - public void stop() { - this.running = false; - while (this.waiInterrupt.availablePermits() <= 0) { - this.waiInterrupt.release(); - } - this.waiInterrupt.drainPermits(); - this.waiInterrupt.release(2); - } public void reset() { - stop(); this.cpu.reset(); this.getRCBus().write(0, this.defaultDriveId, getWorld(), this.getPos()); this.getRCBus().write(1, this.defaultMonitorId, getWorld(), this.getPos()); } public void step() { - this.waiInterrupt.acquireUninterruptibly(); - this.cpu.step(); - this.getRCBus().update(); - if (this.cpu.isStopped()) { - this.stop(); - return; - } - if (this.cpu.isWaitingForInterrupt()) { - this.waiInterrupt.acquireUninterruptibly(); - this.cpu.assertIrq(); - } - if (this.waiInterrupt.availablePermits() < 2) { - this.waiInterrupt.release(); + i++; + if(!this.cpu.isWaitingForInterrupt()) { + this.cpu.step(); + this.getBus().update(); } } diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java index 9b54b55..a28ac49 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java @@ -4,10 +4,17 @@ import com.mojang.serialization.MapCodec; import com.simon816.j65el02.Cpu; import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.blockentities.CpuEntity; +import net.brokenmoon.redcontrol.blockentities.MonitorEntity; +import net.brokenmoon.redcontrol.blockentities.Peripheral; import net.minecraft.block.*; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityTicker; import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.text.Text; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; @@ -33,4 +40,17 @@ public class CpuBlock extends NetworkCarrier { return validateTicker(type, RedControl.CPU_BLOCK_ENTITY, (world1, pos, state1, be) -> CpuEntity.tick(world1, pos, state1, be)); } + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + if (!world.isClient) { + CpuEntity peripheral = (CpuEntity) world.getBlockEntity(pos); + player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().hashCode())), false); + player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().getRedBus().hashCode())), false); + player.sendMessage(Text.literal(String.valueOf(peripheral.i)), false); + player.sendMessage(Text.literal(String.valueOf(peripheral.cpu.isWaitingForInterrupt())), false); + } + + return ActionResult.SUCCESS; + } + } diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java index ee99809..56bef95 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java @@ -104,6 +104,7 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt if (!world.isClient) { Peripheral peripheral = (Peripheral) world.getBlockEntity(pos); player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().hashCode())), false); + player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().getRedBus().hashCode())), false); } return ActionResult.SUCCESS;