replace submodule with walksanatora/J65el02, BlockModificationEvent, and switch to using accessors over making everything public

main
Walker Fowlkes 1 month ago
parent 83fdc7b864
commit 157b53e1c3

2
.gitattributes vendored

@ -1,4 +1,4 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf

3
.gitmodules vendored

@ -1,3 +1,4 @@
[submodule "J65el02"]
path = J65el02
url = git@git.broken-moon.net:astoriaFloyd/J65el02MC.git
url = git@github.com:walksanatora/J65el02.git

@ -1,14 +1,14 @@
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 com.simon816.j65el02.device.RedBus;
import com.simon816.j65el02.device.RedBusState;
import net.brokenmoon.redcontrol.RedControl;
import net.brokenmoon.redcontrol.mixin.BusAccessor;
import net.brokenmoon.redcontrol.mixin.CpuAccessor;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;
public class Emulator {
@ -21,17 +21,17 @@ public class Emulator {
this.bus = bus;
cpu = new Cpu();
cpu.setBus(new TempBus(bus));
ram = new Memory(0x0000, 0x2000 - 1, cpu.redBusState);
ram = new Memory(0x0000, 0x2000 - 1, ((CpuAccessor)cpu).getRedBusState());
try {
ram.loadFromFile(Paths.get("/home/astoria/code/java/mods/RedControl/src/main/resources/assets/redcontrol/image/rpcboot.bin"),0x00, 0x400);
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.getBus().addDevice(ram, ((CpuAccessor)cpu).getRedBusState());
cpu.reset();
ram.write(0, 2, cpu.redBusState);
ram.write(1, 1, cpu.redBusState);
ram.write(0, 2, ((CpuAccessor)cpu).getRedBusState());
ram.write(1, 1, ((CpuAccessor)cpu).getRedBusState());
System.out.println("Emulator made");
}
@ -42,7 +42,7 @@ public class Emulator {
public void setBus(RedBus bus) {
RedControl.LOGGER.info("Setting Emulator bus");
this.bus = bus;
this.cpu.getBus().setRedBus(bus);
((BusAccessor)this.cpu.getBus()).setRedBus(bus);
}
public boolean isWaitingOnInterrupt(){
@ -55,14 +55,16 @@ public class Emulator {
public void step() {
if(!isWaitingOnInterrupt()) {
CpuState state = ((CpuAccessor)this.cpu).getState();
System.out.println(state.toTraceEvent());
this.cpu.step();
this.cpu.getBus().update(cpu.redBusState);
this.cpu.getBus().update(((CpuAccessor)cpu).getRedBusState());
}
}
public void reset(){
cpu.reset();
ram.write(0, 2, cpu.redBusState);
ram.write(1, 1, cpu.redBusState);
ram.write(0, 2, ((CpuAccessor)cpu).getRedBusState());
ram.write(1, 1, ((CpuAccessor)cpu).getRedBusState());
}
}

@ -14,14 +14,14 @@ public class TempBus extends Bus {
@Override
public void write(int address, int data, RedBusState state) {
Device device = findDevice(address,state);
RedControl.LOGGER.info("Writing to bus with device " + device + "at address " + (address - device.startAddress(state)) + " with data " + data + " with target " + state.activeDeviceId + " and redbus offset of " + state.offset);
//RedControl.LOGGER.info("Writing to bus with device " + device + "at address " + (address - device.startAddress(state)) + " with data " + data + " with target " + state.activeDeviceId + " and redbus offset of " + state.offset);
device.write(address - device.startAddress(state) % 0x2000, data, state);
}
@Override
public int read(int address, boolean cpuAccess, RedBusState state) {
Device device = findDevice(address,state);
RedControl.LOGGER.info("Reading from bus with device " + device + "at address " + (address - device.startAddress(state)) + " with target " + state.activeDeviceId + " and redbus offset of " + state.offset);
//RedControl.LOGGER.info("Reading from bus with device " + device + "at address " + (address - device.startAddress(state)) + " with target " + state.activeDeviceId + " and redbus offset of " + state.offset);
return device.read(address - device.startAddress(state) % 0x2000, cpuAccess, state) & 0xff;
}
}

@ -0,0 +1,22 @@
package net.brokenmoon.redcontrol.api.events;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
public class BlockModificationEvents {
/**
* this event is triggered when a blockstate is changed.
*/
public static Event<OnStateChange> ON_CHANGE = EventFactory.createArrayBacked(OnStateChange.class, callbacks -> (world,blockPos,oldState,newState) -> {
for (OnStateChange callback : callbacks) {
callback.onChange(world, blockPos,oldState,newState);
}
});
public @FunctionalInterface interface OnStateChange {void onChange(@NotNull World world,@NotNull BlockPos blockPos,@NotNull BlockState oldState, @NotNull BlockState newState);}
}

@ -18,12 +18,6 @@ public class CpuEntity extends Peripheral{
public boolean notTicked = true;
public Emulator core = new Emulator(new TempRedBus());
private Path bootloader;
{
bootloader = Paths.get("/home/astoria/code/java/mods/RedControl/src/main/resources/assets/redcontrol/image/rpcboot.bin");
}
private int defaultMonitorId = 1;
private int defaultDriveId = 2;
@ -81,9 +75,7 @@ public class CpuEntity extends Peripheral{
}
@Override
public void update() {
}
public void update() {}
@Override
public void setBus(RCWorldBus bus){

@ -42,12 +42,15 @@ public class CpuBlock extends NetworkCarrier {
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
super.onUse(state,world,pos,player,hand,hit);
if (!world.isClient) {
CpuEntity peripheral = (CpuEntity) world.getBlockEntity(pos);
player.sendMessage(Text.literal("Cpu Debug: "), false);
player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().hashCode())), false);
player.sendMessage(Text.literal(String.valueOf(peripheral.core.cpu.getBus().getRedBus().hashCode())), false);
player.sendMessage(Text.literal(String.valueOf(peripheral.i)), false);
player.sendMessage(Text.literal(String.valueOf(peripheral.core.isWaitingOnInterrupt())), false);
player.sendMessage(Text.literal(String.valueOf(peripheral.core.isWaitingOnInterrupt())), false);
}
return ActionResult.SUCCESS;

@ -34,6 +34,7 @@ public class MonitorBlock extends NetworkCarrier {
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
super.onUse(state,world,pos,player,hand,hit);
if (!world.isClient) {
MonitorEntity monitor = (MonitorEntity) world.getBlockEntity(pos);
player.sendMessage(Text.literal(String.valueOf(monitor.getBus().hashCode())), false);

@ -4,6 +4,7 @@ import com.simon816.j65el02.device.RedBus;
import net.brokenmoon.redcontrol.api.RCWorldBus;
import net.brokenmoon.redcontrol.api.TempRedBus;
import net.brokenmoon.redcontrol.blockentities.Peripheral;
import net.brokenmoon.redcontrol.mixin.RedBusAccessor;
import net.minecraft.block.*;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
@ -83,11 +84,13 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!world.isClient) {
Peripheral peripheral = (Peripheral) world.getBlockEntity(pos);
player.sendMessage(Text.literal("Network Carrier Debug: "), false);
player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().hashCode())), false);
player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().getRedBus().hashCode())), false);
for(int i = 0; i < peripheral.getBus().getRedBus().peripherals.length; i++){
if (peripheral.getBus().getRedBus().peripherals[i] != null){
player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().getRedBus().peripherals[i])), false);
RedBus.Peripheral[] peripherals = ((RedBusAccessor)peripheral.getBus().getRedBus()).getPeripherals();
for (RedBus.Peripheral value : peripherals) {
if (value != null) {
player.sendMessage(Text.literal(String.valueOf(value)), false);
}
}
}

@ -0,0 +1,12 @@
package net.brokenmoon.redcontrol.mixin;
import com.simon816.j65el02.Bus;
import com.simon816.j65el02.device.RedBus;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(Bus.class)
public interface BusAccessor {
@Accessor(value = "redBus",remap = false)
void setRedBus(RedBus redBus);
}

@ -0,0 +1,15 @@
package net.brokenmoon.redcontrol.mixin;
import com.simon816.j65el02.Cpu;
import com.simon816.j65el02.CpuState;
import com.simon816.j65el02.device.RedBusState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(Cpu.class)
public interface CpuAccessor {
@Accessor(value = "redBusState",remap = false)
RedBusState getRedBusState();
@Accessor(value = "state",remap = false)
CpuState getState();
}

@ -0,0 +1,12 @@
package net.brokenmoon.redcontrol.mixin;
import com.simon816.j65el02.device.RedBus;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(RedBus.class)
public interface RedBusAccessor {
@Accessor(value = "peripherals",remap = false)
RedBus.Peripheral[] getPeripherals();
}

@ -0,0 +1,37 @@
package net.brokenmoon.redcontrol.mixin;
import net.brokenmoon.redcontrol.api.events.BlockModificationEvents;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Debug(export = true, print = true)
@Mixin(World.class)
public abstract class WorldMixin {
@Unique
private BlockState old;
@Shadow
public BlockState getBlockState(BlockPos pos) {return null;}
@Inject(method = "setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z", at = @At("RETURN"))
private void hookBlockPlace(BlockPos pos, BlockState state, int flags, int maxUpdateDepth, CallbackInfoReturnable<Boolean> cir) {
World world = (World)(Object)this;
//IDEA says that this is unreachable but it is reachable.
if (cir.getReturnValue() && !world.isClient()) {
BlockModificationEvents.ON_CHANGE.invoker().onChange(world,pos,this.old,state);
}
}
@Inject(method = "setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z", at = @At("HEAD"))
private void hookBlockRemove(BlockPos pos, BlockState state, int flags, int maxUpdateDepth, CallbackInfoReturnable<Boolean> cir) {
this.old = getBlockState(pos);
}
}

@ -1,11 +1,14 @@
{
"required": true,
"package": "net.brokenmoon.redcontrol.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ExampleMixin"
],
"injectors": {
"defaultRequire": 1
"required": true,
"package": "net.brokenmoon.redcontrol.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"BusAccessor",
"CpuAccessor",
"ExampleMixin",
"RedBusAccessor"
],
"injectors": {
"defaultRequire": 1
}
}
Loading…
Cancel
Save