diff --git a/J65el02 b/J65el02 index 9e2e514..3690b00 160000 --- a/J65el02 +++ b/J65el02 @@ -1 +1 @@ -Subproject commit 9e2e5145e04787fc2cd36f53e67c572887ee1b15 +Subproject commit 3690b0031fd685abecc2f7d21678c61dca69f40f diff --git a/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java b/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java new file mode 100644 index 0000000..327f9e9 --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java @@ -0,0 +1,67 @@ +package net.brokenmoon.redcontrol.api; + +import com.simon816.j65el02.Bus; +import com.simon816.j65el02.Cpu; +import com.simon816.j65el02.device.Memory; +import com.simon816.j65el02.device.RedBus; +import com.simon816.j65el02.device.RedBusState; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Paths; + +public class Emulator { + + private RedBus bus; + public Cpu cpu; + private Memory ram; + public boolean isRunning = false; + + public Emulator(RedBus bus){ + this.bus = bus; + cpu = new Cpu(); + cpu.setBus(new Bus(bus)); + ram = new Memory(0x0000, 0x2000, new RedBusState()); + try { + ram.loadFromFile(Paths.get("/home/astoria/code/java/mods/RedControl/src/main/resources/assets/redcontrol/image/redforth.img"), 0x400, 0x100); + + } catch (IOException e) { + throw new RuntimeException(e); + } + cpu.getBus().addDevice(ram, cpu.redBusState); + cpu.reset(); + ram.write(0, 2, cpu.redBusState); + ram.write(1, 1, cpu.redBusState); + System.out.println("Emulator made"); + } + + public RedBus getBus() { + return bus; + } + + public void setBus(RedBus bus) { + this.bus = bus; + } + + public boolean isWaitingOnInterrupt(){ + return cpu.isWaitingForInterrupt(); + } + + public void setWaitingOnInterrupt(){ + cpu.getCpuState().intWait = false; + } + + public void step() { + if(!isWaitingOnInterrupt() && isRunning) { + this.cpu.step(); + this.cpu.getBus().update(cpu.redBusState); + } + } + + public void reset(){ + isRunning = false; + cpu.reset(); + ram.write(0, 2, cpu.redBusState); + ram.write(1, 1, cpu.redBusState); + } +} diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java b/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java deleted file mode 100644 index a95e2bf..0000000 --- a/src/main/java/net/brokenmoon/redcontrol/api/RCBus.java +++ /dev/null @@ -1,101 +0,0 @@ -package net.brokenmoon.redcontrol.api; - -import com.simon816.j65el02.Bus; -import com.simon816.j65el02.device.Device; -import com.simon816.j65el02.device.Memory; -import com.simon816.j65el02.device.RedBus; -import net.brokenmoon.redcontrol.RedControl; -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; - - private World backupWorld; - private BlockPos backupPos; - - - - public void write(int address, int data, int redbusTarget, int redbusStartAddress, World world, BlockPos pos) { - if(isValid) { - Device device = findDevice(address, redbusStartAddress); - if(!(device instanceof Memory)) - RedControl.LOGGER.info(device.toString()); - device.write(address, data, redbusTarget, redbusStartAddress); - } else { - generateBusWithWrite(address, data, redbusTarget, redbusStartAddress, world, pos); - } - } - - @Override - public void write(int address, int data, int redbusTarget, int redbusStartAddress) { - write(address, data, redbusTarget, redbusStartAddress, backupWorld, backupPos); - } - - private void generateBusWithWrite(int address, int data, int redbusTarget, int redbusStartAddress, World world, BlockPos pos) { - ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBusWithWrite(world, pos, address, data, redbusTarget, redbusStartAddress); - } - - public int read(int address, boolean cpuAccess, int redbusTarget, int redbusStartAddress, World world, BlockPos pos) { - if(isValid) { - Device device = findDevice(address, redbusStartAddress); - if(!(device instanceof Memory)) - RedControl.LOGGER.info(device.toString()); - return device.read(address, cpuAccess, redbusTarget, redbusStartAddress) & 0xff; - } else { - return generateBusWithRead(address, cpuAccess, redbusTarget, redbusStartAddress, world, pos); - } - } - - private int generateBusWithRead(int address, boolean cpuAccess, int redbusTarget, int redbusStartAddress, World world, BlockPos pos) { - return ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBusWithRead(world, pos, address, cpuAccess, redbusTarget, redbusStartAddress); - } - - @Override - public int read(int address, boolean cpuAccess, int redbusTarget, int redbusStartAddress) { - return read(address, cpuAccess, redbusTarget, redbusStartAddress, 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); - } - - public RedBus getRedBus() { - return redBus; - } - - public void setRedBus(RedBus redBus) { - this.redBus = redBus; - } - - public void setValid(boolean val){ - this.isValid = val; - } - - @Override - protected Device findDevice(int address, int redbusStartAddress) { - // RedBus takes priority - if (address >= redbusStartAddress && address <= (redbusStartAddress + 0xFF)) { - return this.redBus; - } - int idx = Arrays.binarySearch(this.boundaries, address); - if (idx < 0) { - idx = -idx - 2; - } - return this.devices.get(idx); - } -} diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java b/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java deleted file mode 100644 index e493f1e..0000000 --- a/src/main/java/net/brokenmoon/redcontrol/api/RCCpu.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.brokenmoon.redcontrol.api; - -import com.simon816.j65el02.Bus; -import com.simon816.j65el02.Cpu; -import com.simon816.j65el02.device.Memory; -import net.brokenmoon.redcontrol.blockentities.CpuEntity; - -public class RCCpu extends Cpu { - - private CpuEntity cpuEntity; - private Memory ram = new Memory(0x0000, 0x2000); - - public RCCpu(CpuEntity cpuEntity, RCBus bus) { - this.cpuEntity = cpuEntity; - this.setBus(bus); - } - - public void setInterrupt(boolean val){ - state.intWait = val; - } - - @Override - protected int readByte(int address) { - return this.cpuEntity.getBus().getRedBus().read(address, true, redbusTarget, redbusStartAddress); - } - - @Override - protected void writeMemory(int address, int value, boolean x) { - this.getBus().write(address, value, this.redbusTarget, this.redbusStartAddress); - boolean flag = x ? this.state.indexWidthFlag : this.state.mWidthFlag; - if (!this.state.emulationFlag && !flag) { - this.cpuEntity.getBus().getRedBus().write(address + 1, value >>> 8, redbusTarget, redbusStartAddress); - } - } - - @Override - public void setBus(Bus bus) { - super.setBus(bus); - this.getBus().addDevice(this.ram); - } - - public Memory getRam() { - return ram; - } -} diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCRedbus.java b/src/main/java/net/brokenmoon/redcontrol/api/RCRedbus.java deleted file mode 100644 index 81201b6..0000000 --- a/src/main/java/net/brokenmoon/redcontrol/api/RCRedbus.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.brokenmoon.redcontrol.api; - -import com.simon816.j65el02.device.RedBus; -import net.brokenmoon.redcontrol.RedControl; - -public class RCRedbus extends RedBus { - @Override - public void write(int address, int data, int redbusTarget, int redbusStartAddress) { - if (!this.enabled) { - return; - } - Peripheral peripheral = this.peripherals[redbusTarget]; - RedControl.LOGGER.info("Peripheral write at " + address + " for " + redbusTarget); - if (peripheral != null) { - peripheral.write(address, data & 0xff); - } - } - - @Override - public int read(int address, boolean cpuAccess, int redbusTarget, int redbusStartAddress) { - if (!this.enabled) { - return 0; - } - Peripheral peripheral = this.peripherals[redbusTarget]; - RedControl.LOGGER.info("Peripheral read at " + address + " for " + redbusTarget); - if (peripheral != null) { - return peripheral.read(address); - } - return 0; - } -} diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCWorldBus.java b/src/main/java/net/brokenmoon/redcontrol/api/RCWorldBus.java new file mode 100644 index 0000000..9629110 --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/api/RCWorldBus.java @@ -0,0 +1,39 @@ +package net.brokenmoon.redcontrol.api; + +import com.simon816.j65el02.device.RedBus; +import net.brokenmoon.redcontrol.blocks.NetworkCarrier; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +public class RCWorldBus { + + private boolean isValid = false; + + private RedBus redBus; + + private World backupWorld; + private BlockPos backupPos; + + public RCWorldBus(RedBus redBus, World world, BlockPos pos) { + this.redBus = redBus; + backupWorld = world; + backupPos = 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 setValid(boolean val){ + this.isValid = val; + } +} diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index 50d857e..0e1230e 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -2,9 +2,7 @@ package net.brokenmoon.redcontrol.blockentities; import com.simon816.j65el02.device.RedBus; import net.brokenmoon.redcontrol.RedControl; -import net.brokenmoon.redcontrol.api.RCBus; -import net.brokenmoon.redcontrol.api.RCCpu; -import net.brokenmoon.redcontrol.api.RCRedbus; +import net.brokenmoon.redcontrol.api.Emulator; import net.minecraft.block.BlockState; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -17,8 +15,7 @@ public class CpuEntity extends Peripheral{ public int i = 0; public boolean notTicked = true; - public RCCpu cpu; - private RCBus rcbus; + public Emulator core; private Path bootloader; @@ -34,41 +31,18 @@ public class CpuEntity extends Peripheral{ public static void tick(World world, BlockPos pos, BlockState state, CpuEntity be) { if(be.notTicked) { - RCBus bus; - if(be.getBus() == null) { - bus = new RCBus(new RCRedbus(), world, pos); - be.bus = bus; - } else { - bus = be.getBus(); - } - if (be.cpu == null){ + if (be.core == null){ RedControl.LOGGER.info("Making CpuEntity"); - be.cpu = new RCCpu(be, bus); - } - - try { - be.cpu.getRam().loadFromFile(be.bootloader, 0x400, 0x100); - } catch (IOException e) { - throw new RuntimeException(e); + be.core = new Emulator(new RedBus()); } be.reset(); be.notTicked = false; } for(int i = 0; i < 500; i++) be.step(); - if(be.cpu.isWaitingForInterrupt()){ - be.cpu.setInterrupt(false); - } - } - - @Override - public void setBus(RCBus bus){ - this.bus = bus; - if(cpu == null) { - RedControl.LOGGER.info("Making CpuEntity setbus edition"); - this.cpu = new RCCpu(this, bus); + if(be.core.isWaitingOnInterrupt()){ + be.core.setWaitingOnInterrupt(); } - this.cpu.setBus(bus); } public int getDefaultDriveId() { @@ -90,17 +64,12 @@ public class CpuEntity extends Peripheral{ public void reset() { - this.cpu.reset(); - this.getBus().write(0, this.defaultDriveId, cpu.redbusTarget, cpu.redbusStartAddress, getWorld(), this.getPos()); - this.getBus().write(1, this.defaultMonitorId, cpu.redbusTarget, cpu.redbusStartAddress, getWorld(), this.getPos()); + this.core.reset(); } public void step() { i++; - if(!this.cpu.isWaitingForInterrupt()) { - this.cpu.step(); - this.getBus().update(this.cpu.redbusTarget); - } + core.step(); } @Override diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java index 4ef3da4..b53c86c 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java @@ -1,23 +1,16 @@ 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.brokenmoon.redcontrol.api.RCWorldBus; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; 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; public abstract class Peripheral extends BlockEntity implements RedBus.Peripheral { - protected RCBus bus; + protected RedBus bus; + protected RCWorldBus worldBus; protected int id; @@ -27,13 +20,14 @@ public abstract class Peripheral extends BlockEntity implements RedBus.Periphera this.id = id; } - public RCBus getBus(){ - return this.bus; + public RCWorldBus getBus(){ + return this.worldBus; } - public void setBus(RCBus bus){ - this.bus = bus; - this.bus.getRedBus().setPeripheral(id, this); + public void setBus(RCWorldBus bus){ + this.bus = bus.getRedBus(); + this.bus.setPeripheral(id, this); + this.worldBus = bus; } public int getId() { diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java index a28ac49..e44c90b 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java @@ -47,7 +47,7 @@ public class CpuBlock extends NetworkCarrier { 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); + player.sendMessage(Text.literal(String.valueOf(peripheral.core.isWaitingOnInterrupt())), 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 b4d0610..d0e989f 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java @@ -1,8 +1,7 @@ package net.brokenmoon.redcontrol.blocks; import com.simon816.j65el02.device.RedBus; -import net.brokenmoon.redcontrol.api.RCBus; -import net.brokenmoon.redcontrol.api.RCRedbus; +import net.brokenmoon.redcontrol.api.RCWorldBus; import net.brokenmoon.redcontrol.blockentities.Peripheral; import net.minecraft.block.*; import net.minecraft.entity.LivingEntity; @@ -21,11 +20,11 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt super(settings); } - public RCBus getBus(World world, BlockPos pos){ + public RCWorldBus getBus(World world, BlockPos pos){ return ((Peripheral)world.getBlockEntity(pos)).getBus(); } - public void setBus(World world, BlockPos pos, RCBus bus){ + public void setBus(World world, BlockPos pos, RCWorldBus bus){ ((Peripheral)world.getBlockEntity(pos)).setBus(bus); } @@ -55,7 +54,7 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt Block worldBlock = world.getBlockState(pos).getBlock(); if(world.getBlockEntity(pos) instanceof Peripheral){ Peripheral entityBlock = (Peripheral) world.getBlockEntity(pos); - RCBus bus = new RCBus(new RCRedbus(), world, pos); + RCWorldBus bus = new RCWorldBus(new RedBus(), world, pos); entityBlock.setBus(bus); entityBlock.getBus().setValid(true); floodBus(entityBlock.getBus(), world, pos); @@ -63,7 +62,7 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt } - private void floodBus(RCBus bus, World world, BlockPos pos) { + private void floodBus(RCWorldBus bus, World world, BlockPos pos) { replaceBus(bus, world, pos.north()); replaceBus(bus, world, pos.south()); replaceBus(bus, world, pos.east()); @@ -72,32 +71,13 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt replaceBus(bus, world, pos.down()); } - private void replaceBus(RCBus bus, World world, BlockPos pos) { + private void replaceBus(RCWorldBus bus, World world, BlockPos pos) { if(world.getBlockEntity(pos) instanceof Peripheral && ((Peripheral) world.getBlockEntity(pos)).getBus() != bus){ ((Peripheral) world.getBlockEntity(pos)).setBus(bus); floodBus(bus, world, pos); } } - public void generateBusWithWrite(World world, BlockPos pos, int address, int data, int redbusTarget, int redbusStartAddress) { - if(world.getBlockEntity(pos) instanceof Peripheral){ - Peripheral entityBlock = (Peripheral) world.getBlockEntity(pos); - generateBus(world,pos); - floodBus(entityBlock.getBus(), world, pos); - entityBlock.getBus().write(address, data, redbusTarget, redbusStartAddress, world, pos); - } - } - - public int generateBusWithRead(World world, BlockPos pos, int address, boolean cpuAccess, int redbusTarget, int redbusStartAddress) { - if(world.getBlockEntity(pos) instanceof Peripheral){ - Peripheral entityBlock = (Peripheral) world.getBlockEntity(pos); - generateBus(world,pos); - floodBus(entityBlock.getBus(), world, pos); - return entityBlock.getBus().read(address, cpuAccess, redbusTarget, redbusStartAddress, world, pos); - } - return 0; - } - @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { if (!world.isClient) {