Atleast its trying to read from the bus

main
Astoria 7 months ago
parent 70fca3120e
commit 61042af398

@ -20,29 +20,20 @@ public class RCBus extends Bus {
private World backupWorld; private World backupWorld;
private BlockPos backupPos; 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) { public void write(int address, int data, World world, BlockPos pos) {
if(isValid) { if(isValid) {
getRedBus().write(address, data); Device device = findDevice(address);
device.write(address - device.startAddress(), data);
} else { } else {
generateBusWithWrite(address, data, world, pos); generateBusWithWrite(address, data, world, pos);
} }
} }
@Override
public void write(int address, int data) { public void write(int address, int data) {
Device device = findDevice(address); write(address, data, backupWorld, backupPos);
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) { 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) { public int read(int address, boolean cpuAccess, World world, BlockPos pos) {
if(isValid) { if(isValid) {
RedControl.LOGGER.info("Reading! " + address + " " + cpuAccess); Device device = findDevice(address);
return getRedBus().read(address, cpuAccess) & 0xff; return device.read(address - device.startAddress(), cpuAccess) & 0xff;
} else { } else {
return generateBusWithRead(address, cpuAccess, world, pos); 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) { private int generateBusWithRead(int address, boolean cpuAccess, World world, BlockPos pos) {
return ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBusWithRead(world, pos, address, cpuAccess); 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 @Override
public int read(int address, boolean cpuAccess) { public int read(int address, boolean cpuAccess) {
return read(address, cpuAccess, backupWorld, backupPos); 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){ public void generateBus(World world, BlockPos pos){
((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBus(world, pos); ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBus(world, pos);
} }
@ -101,11 +83,7 @@ public class RCBus extends Bus {
this.getRedBus().updatePeripheral(); this.getRedBus().updatePeripheral();
} }
public boolean getValid(){ public void setValid(boolean val){
return this.isValid; this.isValid = val;
}
public void setValid(boolean validity){
this.isValid = validity;
} }
} }

@ -3,6 +3,7 @@ package net.brokenmoon.redcontrol.api;
import com.simon816.j65el02.Bus; import com.simon816.j65el02.Bus;
import com.simon816.j65el02.Cpu; import com.simon816.j65el02.Cpu;
import com.simon816.j65el02.CpuState; import com.simon816.j65el02.CpuState;
import com.simon816.j65el02.device.Memory;
import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.RedControl;
import net.brokenmoon.redcontrol.blockentities.CpuEntity; import net.brokenmoon.redcontrol.blockentities.CpuEntity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -11,6 +12,7 @@ import net.minecraft.world.World;
public class RCCpu extends Cpu { public class RCCpu extends Cpu {
private CpuEntity cpuEntity; private CpuEntity cpuEntity;
private Memory ram = new Memory(0x0000, 0x2000);;
public RCCpu(CpuEntity cpuEntity) { public RCCpu(CpuEntity cpuEntity) {
this.cpuEntity = cpuEntity; this.cpuEntity = cpuEntity;
@ -34,4 +36,15 @@ public class RCCpu extends Cpu {
this.cpuEntity.getBus().getRedBus().write(address + 1, value >>> 8); 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;
}
} }

@ -15,11 +15,12 @@ import java.nio.file.Paths;
public class CpuEntity extends Peripheral{ public class CpuEntity extends Peripheral{
public int i = 0; public int i = 0;
boolean notTicked = true; public boolean notTicked = true;
public RCCpu cpu; public RCCpu cpu;
private RCBus rcbus; private RCBus rcbus;
private Path bootloader; private Path bootloader;
{ {
bootloader = Paths.get("/home/astoria/code/java/mods/RedControl/src/main/resources/assets/redcontrol/image/rpcboot.bin"); 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) { public static void tick(World world, BlockPos pos, BlockState state, CpuEntity be) {
if(be.notTicked) { if(be.notTicked) {
be.cpu = new RCCpu(be); be.cpu = new RCCpu(be);
be.setRCBus(new RCBus(new RedBus(), world, pos)); be.setBus(new RCBus(new RedBus(), world, pos));
be.cpu.setBus(be.getRCBus()); be.cpu.setBus(be.getBus());
be.cpu.setBus(be.getRCBus()); be.cpu.getBus().addDevice(be.cpu.getRam());
Memory ram = new Memory(0x0000, 0x2000);
try { try {
ram.loadFromFile(be.bootloader, 0x400, 0x100); be.cpu.getRam().loadFromFile(be.bootloader, 0x400, 0x100);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
be.cpu.getBus().addDevice(ram);
be.reset(); be.reset();
be.notTicked = false; 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) { public void setPeripheral(int id, RedBus.Peripheral peripheral) {
this.getBus().getRedBus().setPeripheral(id, peripheral); this.getBus().getRedBus().setPeripheral(id, peripheral);
} }
@ -89,8 +79,8 @@ public class CpuEntity extends Peripheral{
public void reset() { public void reset() {
this.cpu.reset(); this.cpu.reset();
this.getRCBus().write(0, this.defaultDriveId, getWorld(), this.getPos()); this.getBus().write(0, this.defaultDriveId, getWorld(), this.getPos());
this.getRCBus().write(1, this.defaultMonitorId, getWorld(), this.getPos()); this.getBus().write(1, this.defaultMonitorId, getWorld(), this.getPos());
} }
public void step() { public void step() {

@ -1,6 +1,7 @@
package net.brokenmoon.redcontrol.blockentities; package net.brokenmoon.redcontrol.blockentities;
import com.simon816.j65el02.Bus; import com.simon816.j65el02.Bus;
import com.simon816.j65el02.device.Device;
import com.simon816.j65el02.device.RedBus; import com.simon816.j65el02.device.RedBus;
import net.brokenmoon.redcontrol.api.RCBus; import net.brokenmoon.redcontrol.api.RCBus;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
@ -16,9 +17,10 @@ import net.minecraft.world.World;
public abstract class Peripheral extends BlockEntity implements RedBus.Peripheral { 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) { public Peripheral(BlockEntityType<?> type, BlockPos pos, BlockState state, int id) {
super(type, pos, state); super(type, pos, state);
@ -26,7 +28,7 @@ public abstract class Peripheral extends BlockEntity implements RedBus.Periphera
} }
public RCBus getBus(){ public RCBus getBus(){
return bus; return this.bus;
} }
public void setBus(RCBus bus){ public void setBus(RCBus bus){
@ -36,12 +38,4 @@ public abstract class Peripheral extends BlockEntity implements RedBus.Periphera
public int getId() { public int getId() {
return id; return id;
} }
public void setId(int id) {
this.id = id;
}
public void onJoinBus() {
bus.getRedBus().setPeripheral(this.id, this);
}
} }

@ -75,7 +75,6 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt
private void replaceBus(RCBus bus, World world, BlockPos pos) { private void replaceBus(RCBus bus, World world, BlockPos pos) {
if(world.getBlockEntity(pos) instanceof Peripheral && ((Peripheral) world.getBlockEntity(pos)).getBus() != bus){ if(world.getBlockEntity(pos) instanceof Peripheral && ((Peripheral) world.getBlockEntity(pos)).getBus() != bus){
((Peripheral) world.getBlockEntity(pos)).setBus(bus); ((Peripheral) world.getBlockEntity(pos)).setBus(bus);
((Peripheral) world.getBlockEntity(pos)).onJoinBus();
floodBus(bus, world, pos); floodBus(bus, world, pos);
} }
} }

Loading…
Cancel
Save