From 30779f58b602f508ae3b8048bdb576191f7d9980 Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Mon, 7 Oct 2019 01:27:47 +0200 Subject: [PATCH] Restructured register system --- .../AbstractAm2900MachineDefinition.java | 40 ++++----- .../model/am2900/machine/Am2900Machine.java | 84 +++++++------------ .../machine/registers/Am2900Register.java | 8 ++ .../net.mograsim.machine/META-INF/MANIFEST.MF | 1 + .../src/net/mograsim/machine/Machine.java | 1 + .../mograsim/machine/MachineDefinition.java | 10 ++- .../src/net/mograsim/machine/Register.java | 54 ------------ .../net/mograsim/machine/RegisterGroup.java | 16 ---- .../mograsim/machine/SimpleRegisterGroup.java | 38 --------- .../mograsim/machine/StandardRegister.java | 47 ----------- .../mograsim/machine/UnionRegisterGroup.java | 41 --------- .../HighLevelStateBasedRegister.java | 82 ++++++++++++++++++ .../ModelComponentBasedRegister.java | 17 ++++ .../mograsim/machine/registers/Register.java | 24 ++++++ .../machine/registers/RegisterGroup.java | 18 ++++ .../machine/registers/SimpleRegister.java | 25 ++++++ .../registers/SimpleRegisterGroup.java | 45 ++++++++++ .../plugin/launch/MachineRegister.java | 2 +- .../plugin/launch/MachineRegisterGroup.java | 22 ++--- .../plugin/launch/MachineStackFrame.java | 27 +++++- 20 files changed, 312 insertions(+), 290 deletions(-) create mode 100644 plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/Am2900Register.java delete mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/Register.java delete mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/RegisterGroup.java delete mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/SimpleRegisterGroup.java delete mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/StandardRegister.java delete mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/UnionRegisterGroup.java create mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/registers/HighLevelStateBasedRegister.java create mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/registers/ModelComponentBasedRegister.java create mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/registers/Register.java create mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/registers/RegisterGroup.java create mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/registers/SimpleRegister.java create mode 100644 plugins/net.mograsim.machine/src/net/mograsim/machine/registers/SimpleRegisterGroup.java diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/AbstractAm2900MachineDefinition.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/AbstractAm2900MachineDefinition.java index 20f7c58f..aed53563 100644 --- a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/AbstractAm2900MachineDefinition.java +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/AbstractAm2900MachineDefinition.java @@ -1,16 +1,14 @@ package net.mograsim.logic.model.am2900.machine; +import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; -import java.util.Set; +import java.util.List; -import net.mograsim.logic.model.am2900.machine.registers.NumberedRegister; -import net.mograsim.logic.model.am2900.machine.registers.QRegister; import net.mograsim.logic.model.model.LogicModelModifiable; import net.mograsim.machine.ISASchema; import net.mograsim.machine.MachineDefinition; -import net.mograsim.machine.Register; -import net.mograsim.machine.RegisterGroup; +import net.mograsim.machine.registers.Register; +import net.mograsim.machine.registers.RegisterGroup; //we can't use the Singleton pattern here because a MachineDefinition needs a public parameterless constructor //(used for detecting installed machines in plugin.core) @@ -19,15 +17,17 @@ public class AbstractAm2900MachineDefinition implements MachineDefinition public static final String SIMPLE_AM2900_MACHINE_ID = "Am2900Simple"; public static final String STRICT_AM2900_MACHINE_ID = "Am2900Strict"; - public static final Set allRegisters; + public static final List unsortedRegisters; + public static final List registerGroups; static { - Set allRegistersModifiable = new HashSet<>(); - allRegistersModifiable.add(QRegister.instance); - allRegistersModifiable.addAll(NumberedRegister.instancesCorrectOrder); - // TODO MSR, muSR, MIR, IR, PC/BZ... - allRegisters = Collections.unmodifiableSet(allRegistersModifiable); + List unsortedRegistersModifiable = new ArrayList<>(); + // TODO MIR, IR, PC/BZ... + unsortedRegisters = Collections.unmodifiableList(unsortedRegistersModifiable); + List registerGroupsModifiable = new ArrayList<>(); + // TODO Am2901, Am2904, Am2910 + registerGroups = Collections.unmodifiableList(registerGroupsModifiable); } public final boolean strict; @@ -62,9 +62,15 @@ public class AbstractAm2900MachineDefinition implements MachineDefinition } @Override - public Set getRegisters() + public List getUnsortedRegisters() { - return allRegisters; + return unsortedRegisters; + } + + @Override + public List getRegisterGroups() + { + return registerGroups; } @Override @@ -98,10 +104,4 @@ public class AbstractAm2900MachineDefinition implements MachineDefinition return Am2900MicroInstructionMemoryDefinition.instance; } - @Override - public Set getRegisterGroups() - { - return null; // TODO create groups for am2904, am2901-asmUsable, am2901-internal, ... - } - } diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java index 352ce123..fa281db4 100644 --- a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java @@ -1,8 +1,7 @@ package net.mograsim.logic.model.am2900.machine; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; +import java.util.List; import java.util.Set; import java.util.function.Consumer; @@ -10,8 +9,7 @@ import net.mograsim.logic.core.components.CoreClock; import net.mograsim.logic.core.timeline.Timeline; import net.mograsim.logic.core.types.Bit; import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.model.am2900.machine.registers.NumberedRegister; -import net.mograsim.logic.model.am2900.machine.registers.QRegister; +import net.mograsim.logic.model.am2900.machine.registers.Am2900Register; import net.mograsim.logic.model.model.LogicModel; import net.mograsim.logic.model.model.LogicModelModifiable; import net.mograsim.logic.model.model.components.ModelComponent; @@ -21,13 +19,14 @@ import net.mograsim.logic.model.modeladapter.LogicCoreAdapter; import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; import net.mograsim.machine.Machine; import net.mograsim.machine.MachineDefinition; -import net.mograsim.machine.Register; import net.mograsim.machine.mi.AssignableMicroInstructionMemory; import net.mograsim.machine.mi.MicroInstruction; import net.mograsim.machine.mi.MicroInstructionDefinition; import net.mograsim.machine.mi.StandardMicroInstructionMemory; import net.mograsim.machine.mi.parameters.MicroInstructionParameter; import net.mograsim.machine.mi.parameters.ParameterClassification; +import net.mograsim.machine.registers.Register; +import net.mograsim.machine.registers.RegisterGroup; import net.mograsim.machine.standard.memory.AssignableMainMemory; import net.mograsim.machine.standard.memory.WordAddressableMemory; @@ -43,7 +42,6 @@ public class Am2900Machine implements Machine private long activeInstructionAddress; private final Set amicListeners; - private final Map, Consumer>> modelListenersPerRegisterListenerPerRegister; public Am2900Machine(LogicModelModifiable model, AbstractAm2900MachineDefinition am2900MachineDefinition) { @@ -52,7 +50,6 @@ public class Am2900Machine implements Machine this.am2900 = IndirectModelComponentCreator.createComponent(logicModel, "resloader:Am2900Loader:jsonres:net/mograsim/logic/model/am2900/components/Am2900.json", "Am2900"); this.amicListeners = new HashSet<>(); - this.modelListenersPerRegisterListenerPerRegister = new HashMap<>(); CoreModelParameters params = new CoreModelParameters(); params.gateProcessTime = 50; @@ -92,12 +89,27 @@ public class Am2900Machine implements Machine am2900.setHighLevelState("muir_2.q", jzMI.toBitVector()); if (!machineDefinition.strict) { - for (Register r : machineDefinition.getRegisters()) - setRegister(r, BitVector.of(Bit.ZERO, r.getWidth())); + setRegistersToZero(machineDefinition.getUnsortedRegisters()); + setRegisterGroupToZero(machineDefinition.getRegisterGroups()); // TODO reset latches? } } + private void setRegistersToZero(List registers) + { + for (Register r : registers) + setRegister(r, BitVector.of(Bit.ZERO, r.getWidth())); + } + + private void setRegisterGroupToZero(List registerGroups) + { + for (RegisterGroup rg : registerGroups) + { + setRegistersToZero(rg.getRegisters()); + setRegisterGroupToZero(rg.getSubGroups()); + } + } + @Override public LogicModel getModel() { @@ -121,74 +133,34 @@ public class Am2900Machine implements Machine return clock; } - // TODO too much code duplication - @Override public BitVector getRegister(Register r) { - String am2901CellSuffix = getAm2901CellSuffix(r); - BitVector result = BitVector.of(); - for (int i = 0; i < 16; i += 4) - { - String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix); - result = result.concat((BitVector) am2900.getHighLevelState(hlsID)); - } - return result; + return castAm2900Register(r).read(am2900); } @Override public void setRegister(Register r, BitVector value) { - String am2901CellSuffix = getAm2901CellSuffix(r); - for (int i = 0; i < 16; i += 4) - { - String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix); - am2900.setHighLevelState(hlsID, value.subVector(i, i + 4)); - } + castAm2900Register(r).write(am2900, value); } @Override public void addRegisterListener(Register r, Consumer listener) { - Map, Consumer> modelListenersPerRegisterListener = modelListenersPerRegisterListenerPerRegister - .computeIfAbsent(r, k -> new HashMap<>()); - if (modelListenersPerRegisterListener.containsKey(listener)) - return; - - Consumer modelListener = v -> listener.accept(getRegister(r)); - String am2901CellSuffix = getAm2901CellSuffix(r); - for (int i = 0; i < 16; i += 4) - { - String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix); - am2900.addHighLevelStateListener(hlsID, modelListener); - } + castAm2900Register(r).addListener(am2900, listener); } @Override public void removeRegisterListener(Register r, Consumer listener) { - Map, Consumer> modelListenersPerRegisterListener = modelListenersPerRegisterListenerPerRegister.get(r); - if (modelListenersPerRegisterListener == null) - return; - - Consumer modelListener = modelListenersPerRegisterListener.get(listener); - if (modelListener == null) - return; - - String am2901CellSuffix = getAm2901CellSuffix(r); - for (int i = 0; i < 16; i += 4) - { - String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix); - am2900.removeHighLevelStateListener(hlsID, modelListener); - } + castAm2900Register(r).removeListener(am2900, listener); } - private static String getAm2901CellSuffix(Register r) + private static Am2900Register castAm2900Register(Register r) { - if (r instanceof QRegister) - return "qreg.q"; - if (r instanceof NumberedRegister) - return "regs.c" + ((NumberedRegister) r).getIndexAsBitstring() + ".q"; + if (r instanceof Am2900Register) + return (Am2900Register) r; throw new IllegalArgumentException("Not a register of an Am2900Machine: " + r); } diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/Am2900Register.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/Am2900Register.java new file mode 100644 index 00000000..a0f1eb82 --- /dev/null +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/Am2900Register.java @@ -0,0 +1,8 @@ +package net.mograsim.logic.model.am2900.machine.registers; + +import net.mograsim.machine.registers.ModelComponentBasedRegister; + +public interface Am2900Register extends ModelComponentBasedRegister +{ + // marker interface +} \ No newline at end of file diff --git a/plugins/net.mograsim.machine/META-INF/MANIFEST.MF b/plugins/net.mograsim.machine/META-INF/MANIFEST.MF index afbfc4c4..20cd1a96 100644 --- a/plugins/net.mograsim.machine/META-INF/MANIFEST.MF +++ b/plugins/net.mograsim.machine/META-INF/MANIFEST.MF @@ -17,6 +17,7 @@ Export-Package: net.mograsim.machine, net.mograsim.machine.mi, net.mograsim.machine.mi.components, net.mograsim.machine.mi.parameters, + net.mograsim.machine.registers, net.mograsim.machine.standard.memory Bundle-Activator: net.mograsim.machine.MachineLoader Bundle-ActivationPolicy: lazy diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/Machine.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/Machine.java index ed219573..66408ced 100644 --- a/plugins/net.mograsim.machine/src/net/mograsim/machine/Machine.java +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/Machine.java @@ -7,6 +7,7 @@ import net.mograsim.logic.core.timeline.Timeline; import net.mograsim.logic.core.types.BitVector; import net.mograsim.logic.model.model.LogicModel; import net.mograsim.machine.mi.AssignableMicroInstructionMemory; +import net.mograsim.machine.registers.Register; import net.mograsim.machine.standard.memory.AssignableMainMemory; public interface Machine diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineDefinition.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineDefinition.java index 1c4aa868..18e628f5 100644 --- a/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineDefinition.java +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineDefinition.java @@ -1,8 +1,10 @@ package net.mograsim.machine; -import java.util.Set; +import java.util.List; import net.mograsim.machine.mi.MicroInstructionMemoryDefinition; +import net.mograsim.machine.registers.Register; +import net.mograsim.machine.registers.RegisterGroup; public interface MachineDefinition { @@ -32,12 +34,12 @@ public interface MachineDefinition ISASchema getISASchema(); /** - * Returns a set of all {@link Register}s present in the machine. + * Returns a set of all {@link Register}s present in the machine that are not part of a register group. * * @return all registers present in the machine. * @author Christian Femers */ - Set getRegisters(); + List getUnsortedRegisters(); /** * Returns a set of all RegisterGroups that the machine contains @@ -45,7 +47,7 @@ public interface MachineDefinition * @return all register groups present in the machine. * @author Christian Femers */ - Set getRegisterGroups(); + List getRegisterGroups(); /** * The number of bits used by the machine to address main memory. Note that this should be the number of bits used in the CPU, not a diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/Register.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/Register.java deleted file mode 100644 index 979f1be3..00000000 --- a/plugins/net.mograsim.machine/src/net/mograsim/machine/Register.java +++ /dev/null @@ -1,54 +0,0 @@ -package net.mograsim.machine; - -import java.util.Set; - -/** - * A register in a machine is defined by this interface. A hardware register may have {@link Register#names() named sub-registers}. - * - * - * @author Christian Femers - * - */ -public interface Register extends Identifiable -{ - /** - * The unique identifier of the register. This does not have to be the display name or name in the assembly language. - * - * @return the registers id as case sensitive String - * @author Christian Femers - */ - @Override - String id(); - - /** - * The name(s) of this register. This is the displayed name and the name used in the assembly language. All names of all registers must - * be case-insensitive unique. A register can have multiple names if these names address different regions (but still have a common - * hardware structure), e.g. EAX, AX, AL, AH. - * - * @return all the names of regions addressing this register, must be case-insensitive. - * @author Christian Femers - */ - Set names(); - - /** - * Returns the complete width in bits of the underlying hardware structure the register and possible sub-registers are part of. - * - * @param name the name of the register - * @return the width of the (sub-)register in bits. - * - * @see #names() - * @author Christian Femers - */ - int getWidth(); - - /** - * Returns the width in bits of the register or a named part of it. - * - * @param name the name of the register - * @return the width of the (sub-)register in bits. - * - * @see #names() - * @author Christian Femers - */ - int getWidth(String name); -} diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/RegisterGroup.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/RegisterGroup.java deleted file mode 100644 index 4838590f..00000000 --- a/plugins/net.mograsim.machine/src/net/mograsim/machine/RegisterGroup.java +++ /dev/null @@ -1,16 +0,0 @@ -package net.mograsim.machine; - -import java.util.Set; - -public interface RegisterGroup extends Identifiable -{ - /** - * Returns all Registers contained in this group and subgroups - */ - Set getRegisters(); - - /** - * Returns the sub groups of this group. May very well be an empty set. - */ - Set getSubGroups(); -} diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/SimpleRegisterGroup.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/SimpleRegisterGroup.java deleted file mode 100644 index 0c1d2ae5..00000000 --- a/plugins/net.mograsim.machine/src/net/mograsim/machine/SimpleRegisterGroup.java +++ /dev/null @@ -1,38 +0,0 @@ -package net.mograsim.machine; - -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -public class SimpleRegisterGroup implements RegisterGroup -{ - - private final String id; - private final Set registers; - - protected SimpleRegisterGroup(String id, Register... registers) - { - this.id = id; - this.registers = Collections.unmodifiableSet(new HashSet<>(List.of(registers))); - } - - @Override - public String id() - { - return id; - } - - @Override - public Set getRegisters() - { - return registers; - } - - @Override - public Set getSubGroups() - { - return Set.of(); - } - -} diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/StandardRegister.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/StandardRegister.java deleted file mode 100644 index 6fea551f..00000000 --- a/plugins/net.mograsim.machine/src/net/mograsim/machine/StandardRegister.java +++ /dev/null @@ -1,47 +0,0 @@ -package net.mograsim.machine; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public abstract class StandardRegister implements Register -{ - private final String id; - private final Set names; - private final int width; - private final Map widthForAliases; - - public StandardRegister(String id, Set names, int width, Map widthForAliases) - { - this.id = id; - this.names = Collections.unmodifiableSet(new HashSet<>(names)); - this.width = width; - this.widthForAliases = Collections.unmodifiableMap(new HashMap<>(widthForAliases)); - } - - @Override - public String id() - { - return id; - } - - @Override - public Set names() - { - return names; - } - - @Override - public int getWidth() - { - return width; - } - - @Override - public int getWidth(String name) - { - return widthForAliases.getOrDefault(name, width); - } -} \ No newline at end of file diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/UnionRegisterGroup.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/UnionRegisterGroup.java deleted file mode 100644 index bfe4248f..00000000 --- a/plugins/net.mograsim.machine/src/net/mograsim/machine/UnionRegisterGroup.java +++ /dev/null @@ -1,41 +0,0 @@ -package net.mograsim.machine; - -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -public class UnionRegisterGroup implements RegisterGroup -{ - private final String id; - private final Set subGroups; - private final Set subGroupRegisters; - - public UnionRegisterGroup(String id, RegisterGroup... subGroups) - { - this.id = id; - this.subGroups = Collections.unmodifiableSet(new HashSet<>(List.of(subGroups))); - this.subGroupRegisters = this.subGroups.stream().flatMap(sg -> sg.getRegisters().stream()).distinct() - .collect(Collectors.toUnmodifiableSet()); - } - - @Override - public String id() - { - return id; - } - - @Override - public Set getRegisters() - { - return subGroupRegisters; - } - - @Override - public Set getSubGroups() - { - return subGroups; - } - -} diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/HighLevelStateBasedRegister.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/HighLevelStateBasedRegister.java new file mode 100644 index 00000000..e774f703 --- /dev/null +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/HighLevelStateBasedRegister.java @@ -0,0 +1,82 @@ +package net.mograsim.machine.registers; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.logic.model.model.components.ModelComponent; + +public class HighLevelStateBasedRegister extends SimpleRegister implements ModelComponentBasedRegister +{ + private final String[] hlsIDsToConcat; + private final int[] hlsWidthes; + + private final Map, Consumer> modelListenersPerRegisterListener; + + public HighLevelStateBasedRegister(String id, String hlsID, int logicWidth) + { + this(id, new int[] { logicWidth }, hlsID); + } + + public HighLevelStateBasedRegister(String id, int[] hlsWidthes, String... hlsIDsToConcat) + { + super(id, Arrays.stream(hlsWidthes).sum()); + if (hlsIDsToConcat.length != hlsWidthes.length) + throw new IllegalArgumentException(); + this.hlsIDsToConcat = Arrays.copyOf(hlsIDsToConcat, hlsIDsToConcat.length); + this.hlsWidthes = Arrays.copyOf(hlsWidthes, hlsIDsToConcat.length); + + this.modelListenersPerRegisterListener = new HashMap<>(); + } + + @Override + public BitVector read(ModelComponent component) + { + BitVector result = BitVector.of(); + for (int i = 0; i < hlsIDsToConcat.length; i++) + { + BitVector hls = (BitVector) component.getHighLevelState(hlsIDsToConcat[i]); + if (hls.length() != hlsWidthes[i]) + throw new IllegalArgumentException(); + result = result.concat(hls); + } + return result; + } + + @Override + public void write(ModelComponent component, BitVector value) + { + if (value.length() != getWidth()) + throw new IllegalArgumentException(); + for (int i = 0, off = 0; i < hlsIDsToConcat.length; i++) + { + int oldOff = off; + off += hlsWidthes[i]; + component.setHighLevelState(hlsIDsToConcat[i], value.subVector(oldOff, off)); + } + } + + @Override + public void addListener(ModelComponent component, Consumer listener) + { + if (modelListenersPerRegisterListener.containsKey(listener)) + return; + + Consumer modelListener = v -> listener.accept(read(component)); + for (String hlsID : hlsIDsToConcat) + component.addHighLevelStateListener(hlsID, modelListener); + } + + @Override + public void removeListener(ModelComponent component, Consumer listener) + { + Consumer modelListener = modelListenersPerRegisterListener.get(listener); + if (modelListener == null) + return; + + for (String hlsID : hlsIDsToConcat) + component.removeHighLevelStateListener(hlsID, modelListener); + } +} \ No newline at end of file diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/ModelComponentBasedRegister.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/ModelComponentBasedRegister.java new file mode 100644 index 00000000..96e34a7a --- /dev/null +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/ModelComponentBasedRegister.java @@ -0,0 +1,17 @@ +package net.mograsim.machine.registers; + +import java.util.function.Consumer; + +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.logic.model.model.components.ModelComponent; + +public interface ModelComponentBasedRegister +{ + public BitVector read(ModelComponent component); + + public void write(ModelComponent component, BitVector value); + + public void addListener(ModelComponent component, Consumer listener); + + public void removeListener(ModelComponent component, Consumer listener); +} \ No newline at end of file diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/Register.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/Register.java new file mode 100644 index 00000000..8c3f90bf --- /dev/null +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/Register.java @@ -0,0 +1,24 @@ +package net.mograsim.machine.registers; + +import net.mograsim.machine.Identifiable; + +/** + * A register in a machine is defined by this interface. A hardware register may have {@link Register#names() named sub-registers}. + * + * + * @author Christian Femers + * + */ +public interface Register extends Identifiable +{ + /** + * Returns the complete width in bits of the underlying hardware structure the register and possible sub-registers are part of. + * + * @param name the name of the register + * @return the width of the (sub-)register in bits. + * + * @see #names() + * @author Christian Femers + */ + int getWidth(); +} \ No newline at end of file diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/RegisterGroup.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/RegisterGroup.java new file mode 100644 index 00000000..8fe60f37 --- /dev/null +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/RegisterGroup.java @@ -0,0 +1,18 @@ +package net.mograsim.machine.registers; + +import java.util.List; + +import net.mograsim.machine.Identifiable; + +public interface RegisterGroup extends Identifiable +{ + /** + * Returns all Registers contained in this group that are not part of a subgroup. + */ + List getRegisters(); + + /** + * Returns the sub groups of this group. May very well be an empty set. + */ + List getSubGroups(); +} diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/SimpleRegister.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/SimpleRegister.java new file mode 100644 index 00000000..57c2150e --- /dev/null +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/SimpleRegister.java @@ -0,0 +1,25 @@ +package net.mograsim.machine.registers; + +public class SimpleRegister implements Register +{ + private final String id; + private final int width; + + public SimpleRegister(String id, int width) + { + this.id = id; + this.width = width; + } + + @Override + public String id() + { + return id; + } + + @Override + public int getWidth() + { + return width; + } +} \ No newline at end of file diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/SimpleRegisterGroup.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/SimpleRegisterGroup.java new file mode 100644 index 00000000..09e80292 --- /dev/null +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/registers/SimpleRegisterGroup.java @@ -0,0 +1,45 @@ +package net.mograsim.machine.registers; + +import java.util.List; + +public class SimpleRegisterGroup implements RegisterGroup +{ + private final String id; + private final List registers; + private final List subgroups; + + protected SimpleRegisterGroup(String id, Register... registers) + { + this(id, new RegisterGroup[0], registers); + } + + protected SimpleRegisterGroup(String id, RegisterGroup... subgroups) + { + this(id, subgroups, new Register[0]); + } + + protected SimpleRegisterGroup(String id, RegisterGroup[] subgroups, Register... registers) + { + this.id = id; + this.registers = List.of(registers); + this.subgroups = List.of(subgroups); + } + + @Override + public String id() + { + return id; + } + + @Override + public List getRegisters() + { + return registers; + } + + @Override + public List getSubGroups() + { + return subgroups; + } +} \ No newline at end of file diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegister.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegister.java index 8c0685dc..f8d343f4 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegister.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegister.java @@ -18,7 +18,7 @@ import org.eclipse.swt.SWT; import net.mograsim.logic.core.types.BitVector; import net.mograsim.machine.Machine; -import net.mograsim.machine.Register; +import net.mograsim.machine.registers.Register; import net.mograsim.plugin.MograsimActivator; public class MachineRegister extends PlatformObject implements IRegister diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegisterGroup.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegisterGroup.java index bb7ed264..fdf90ad8 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegisterGroup.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegisterGroup.java @@ -1,7 +1,6 @@ package net.mograsim.plugin.launch; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; import org.eclipse.core.runtime.PlatformObject; @@ -12,22 +11,20 @@ import org.eclipse.debug.core.model.IRegister; import org.eclipse.debug.core.model.IRegisterGroup; import net.mograsim.machine.Machine; -import net.mograsim.machine.Register; +import net.mograsim.machine.registers.Register; import net.mograsim.plugin.MograsimActivator; -import net.mograsim.plugin.util.NumberRespectingStringComparator; public class MachineRegisterGroup extends PlatformObject implements IRegisterGroup { private final MachineStackFrame stackFrame; + private final String name; private final List registers; - public MachineRegisterGroup(MachineStackFrame stackFrame) + public MachineRegisterGroup(MachineStackFrame stackFrame, String name, List registers) { this.stackFrame = stackFrame; - Set machineRegisters = getMachine().getDefinition().getRegisters(); - this.registers = machineRegisters.stream() - .sorted((r1, r2) -> NumberRespectingStringComparator.CASE_SENSITIVE.compare(r1.id(), r2.id())) - .map(r -> new MachineRegister(this, r)).collect(Collectors.toUnmodifiableList()); + this.name = name; + this.registers = registers.stream().map(r -> new MachineRegister(this, r)).collect(Collectors.toUnmodifiableList()); } @Override @@ -56,7 +53,7 @@ public class MachineRegisterGroup extends PlatformObject implements IRegisterGro @Override public String getName() throws DebugException { - return "pseudo register group"; + return name; } @Override @@ -68,6 +65,11 @@ public class MachineRegisterGroup extends PlatformObject implements IRegisterGro @Override public boolean hasRegisters() throws DebugException { - return true; + return hasRegistersSafe(); + } + + public boolean hasRegistersSafe() + { + return registers.size() > 0; } } \ No newline at end of file diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineStackFrame.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineStackFrame.java index 14f64206..2744a3c3 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineStackFrame.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineStackFrame.java @@ -1,5 +1,9 @@ package net.mograsim.plugin.launch; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import org.eclipse.core.runtime.PlatformObject; import org.eclipse.debug.core.DebugException; import org.eclipse.debug.core.ILaunch; @@ -10,17 +14,34 @@ import org.eclipse.debug.core.model.IThread; import org.eclipse.debug.core.model.IVariable; import net.mograsim.machine.Machine; +import net.mograsim.machine.MachineDefinition; +import net.mograsim.machine.registers.RegisterGroup; import net.mograsim.plugin.MograsimActivator; public class MachineStackFrame extends PlatformObject implements IStackFrame { private final MachineThread thread; - private final MachineRegisterGroup registerGroup; + private final List registerGroups; public MachineStackFrame(MachineThread thread) { this.thread = thread; - this.registerGroup = new MachineRegisterGroup(this); + List registerGroupsModifiable = new ArrayList<>(); + MachineDefinition machDef = getMachine().getDefinition(); + if (!machDef.getUnsortedRegisters().isEmpty()) + registerGroupsModifiable.add(new MachineRegisterGroup(this, "", machDef.getUnsortedRegisters())); + addRegisterGroups(null, registerGroupsModifiable, machDef.getRegisterGroups()); + this.registerGroups = Collections.unmodifiableList(registerGroupsModifiable); + } + + private void addRegisterGroups(String base, List target, List registerGroups) + { + for (RegisterGroup rg : registerGroups) + { + String name = (base == null ? "" : base + '.') + rg.id(); + target.add(new MachineRegisterGroup(this, name, rg.getRegisters())); + addRegisterGroups(name, target, rg.getSubGroups()); + } } @Override @@ -182,7 +203,7 @@ public class MachineStackFrame extends PlatformObject implements IStackFrame @Override public IRegisterGroup[] getRegisterGroups() throws DebugException { - return new IRegisterGroup[] { registerGroup }; + return registerGroups.toArray(IRegisterGroup[]::new); } @Override -- 2.17.1