From 61042af39813a8a535e268a2b5ae9c7a40c9030f Mon Sep 17 00:00:00 2001 From: Astoria Date: Mon, 8 Apr 2024 22:25:51 -0500 Subject: [PATCH] Atleast its trying to read from the bus --- .../net/brokenmoon/redcontrol/api/RCBus.java | 56 ++++++------------- .../net/brokenmoon/redcontrol/api/RCCpu.java | 13 +++++ .../redcontrol/blockentities/CpuEntity.java | 26 +++------ .../redcontrol/blockentities/Peripheral.java | 20 +++---- .../redcontrol/blocks/NetworkCarrier.java | 1 - 5 files changed, 45 insertions(+), 71 deletions(-) diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java b/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java index 8335ca2..b2197bd 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java @@ -20,29 +20,20 @@ public class RCBus extends Bus { 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) { - getRedBus().write(address, data); + Device device = findDevice(address); + device.write(address - device.startAddress(), data); } else { generateBusWithWrite(address, data, world, pos); } } + @Override 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; + write(address, data, backupWorld, backupPos); } private void generateBusWithWrite(int address, int data, World world, BlockPos pos) { @@ -51,8 +42,8 @@ public class RCBus extends Bus { public int read(int address, boolean cpuAccess, World world, BlockPos pos) { if(isValid) { - RedControl.LOGGER.info("Reading! " + address + " " + cpuAccess); - return getRedBus().read(address, cpuAccess) & 0xff; + Device device = findDevice(address); + return device.read(address - device.startAddress(), cpuAccess) & 0xff; } else { return generateBusWithRead(address, cpuAccess, world, pos); } @@ -61,29 +52,20 @@ 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 RCBus(RedBus redBus, World world, BlockPos pos) { + super(redBus); + this.redBus = redBus; + backupWorld = world; + backupPos = pos; + } + + public void generateBus(World world, BlockPos pos){ ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBus(world, pos); } @@ -101,11 +83,7 @@ public class RCBus extends Bus { this.getRedBus().updatePeripheral(); } - public boolean getValid(){ - return this.isValid; - } - - public void setValid(boolean validity){ - this.isValid = validity; + public void setValid(boolean val){ + this.isValid = val; } } diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java b/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java index 8cd55f2..f27540b 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 com.simon816.j65el02.device.Memory; import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.blockentities.CpuEntity; import net.minecraft.util.math.BlockPos; @@ -11,6 +12,7 @@ import net.minecraft.world.World; public class RCCpu extends Cpu { private CpuEntity cpuEntity; + private Memory ram = new Memory(0x0000, 0x2000);; public RCCpu(CpuEntity cpuEntity) { this.cpuEntity = cpuEntity; @@ -34,4 +36,15 @@ public class RCCpu extends Cpu { this.cpuEntity.getBus().getRedBus().write(address + 1, value >>> 8); } } + + @Override + public void setBus(Bus bus) { + super.setBus(bus); + if(!cpuEntity.notTicked) + getBus().addDevice(this.ram); + } + + public Memory getRam() { + return ram; + } } diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index 507427a..edb37cc 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -15,11 +15,12 @@ import java.nio.file.Paths; public class CpuEntity extends Peripheral{ public int i = 0; - boolean notTicked = true; + public boolean notTicked = true; public RCCpu cpu; private RCBus rcbus; private Path bootloader; + { bootloader = Paths.get("/home/astoria/code/java/mods/RedControl/src/main/resources/assets/redcontrol/image/rpcboot.bin"); } @@ -35,16 +36,14 @@ 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(), world, pos)); - be.cpu.setBus(be.getRCBus()); - be.cpu.setBus(be.getRCBus()); - Memory ram = new Memory(0x0000, 0x2000); + be.setBus(new RCBus(new RedBus(), world, pos)); + be.cpu.setBus(be.getBus()); + be.cpu.getBus().addDevice(be.cpu.getRam()); try { - ram.loadFromFile(be.bootloader, 0x400, 0x100); + be.cpu.getRam().loadFromFile(be.bootloader, 0x400, 0x100); } catch (IOException e) { throw new RuntimeException(e); } - be.cpu.getBus().addDevice(ram); be.reset(); be.notTicked = false; } @@ -56,15 +55,6 @@ public class CpuEntity extends Peripheral{ } - 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); } @@ -89,8 +79,8 @@ public class CpuEntity extends Peripheral{ public void reset() { this.cpu.reset(); - this.getRCBus().write(0, this.defaultDriveId, getWorld(), this.getPos()); - this.getRCBus().write(1, this.defaultMonitorId, getWorld(), this.getPos()); + this.getBus().write(0, this.defaultDriveId, getWorld(), this.getPos()); + this.getBus().write(1, this.defaultMonitorId, getWorld(), this.getPos()); } public void step() { diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java index e6ef6d6..b1d462a 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java @@ -1,6 +1,7 @@ package net.brokenmoon.redcontrol.blockentities; import com.simon816.j65el02.Bus; +import com.simon816.j65el02.device.Device; import com.simon816.j65el02.device.RedBus; import net.brokenmoon.redcontrol.api.RCBus; import net.minecraft.block.BlockState; @@ -16,32 +17,25 @@ import net.minecraft.world.World; public abstract class Peripheral extends BlockEntity implements RedBus.Peripheral { - private RCBus bus; + protected RCBus bus; + + protected int id; - private int id; public Peripheral(BlockEntityType type, BlockPos pos, BlockState state, int id) { super(type, pos, state); this.id = id; } - public RCBus getBus() { - return bus; + public RCBus getBus(){ + return this.bus; } - public void setBus(RCBus bus) { + public void setBus(RCBus bus){ this.bus = bus; } public int getId() { return id; } - - public void setId(int id) { - this.id = id; - } - - public void onJoinBus() { - bus.getRedBus().setPeripheral(this.id, this); - } } diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java index c98f3b4..5e9c3b3 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java @@ -75,7 +75,6 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt private void replaceBus(RCBus bus, World world, BlockPos pos) { if(world.getBlockEntity(pos) instanceof Peripheral && ((Peripheral) world.getBlockEntity(pos)).getBus() != bus){ ((Peripheral) world.getBlockEntity(pos)).setBus(bus); - ((Peripheral) world.getBlockEntity(pos)).onJoinBus(); floodBus(bus, world, pos); } }