From fa056788b4a250ad1dff91b872f9ffd36f4af8f1 Mon Sep 17 00:00:00 2001 From: Nikolay Repin Date: Wed, 31 Jul 2024 19:25:01 +0700 Subject: [PATCH] [cocoemu] Rewrite priority encoders in ExceptionControlUnits to bit masks --- .../exceptions/ExceptionControlUnit.java | 42 ++++++++-------- .../ExtendedExceptionControlUnit.java | 48 +++++++++---------- 2 files changed, 43 insertions(+), 47 deletions(-) diff --git a/cocoemu/src/main/java/org/cdm/cocoemu/components/processors/cdm16/units/exceptions/ExceptionControlUnit.java b/cocoemu/src/main/java/org/cdm/cocoemu/components/processors/cdm16/units/exceptions/ExceptionControlUnit.java index 0a157782..e130589d 100644 --- a/cocoemu/src/main/java/org/cdm/cocoemu/components/processors/cdm16/units/exceptions/ExceptionControlUnit.java +++ b/cocoemu/src/main/java/org/cdm/cocoemu/components/processors/cdm16/units/exceptions/ExceptionControlUnit.java @@ -2,32 +2,11 @@ import org.cdm.cocoemu.components.processors.cdm16.Cdm16; -import java.util.ArrayList; -import java.util.List; - public class ExceptionControlUnit { public static ExceptionControlUnitOutputParameters compute(ExceptionControlUnitInputParameters parameters) { - List triggeredExceptions = new ArrayList<>(); - - if (parameters.exc_trig_sp()) { - triggeredExceptions.add(Cdm16.ExceptionNumbers.UNALIGNED_SP); - } - if (parameters.exc_trig_pc()) { - triggeredExceptions.add(Cdm16.ExceptionNumbers.UNALIGNED_PC); - } - if (parameters.exc_trig_invalid_inst()) { - triggeredExceptions.add(Cdm16.ExceptionNumbers.INVALID_INST); - } - if (parameters.exc_trig_ext()) { - triggeredExceptions.add(Cdm16.ExceptionNumbers.EXTERNAL_EXC); - } - boolean gotException = true; - Integer excNumber = triggeredExceptions - .stream() - .max(Integer::compareTo) - .orElse(-1); + int excNumber = getExceptionNumber(parameters); if (excNumber == -1) { gotException = false; @@ -85,4 +64,23 @@ public static ExceptionControlUnitOutputParameters compute(ExceptionControlUnitI latch_double_fault ); } + + private static int getExceptionNumber(ExceptionControlUnitInputParameters parameters) { + int triggeredExceptionsMask = 0; + + if (parameters.exc_trig_sp()) { + triggeredExceptionsMask |= 1 << Cdm16.ExceptionNumbers.UNALIGNED_SP; + } + if (parameters.exc_trig_pc()) { + triggeredExceptionsMask |= 1 << Cdm16.ExceptionNumbers.UNALIGNED_PC; + } + if (parameters.exc_trig_invalid_inst()) { + triggeredExceptionsMask |= 1 << Cdm16.ExceptionNumbers.INVALID_INST; + } + if (parameters.exc_trig_ext()) { + triggeredExceptionsMask |= 1 << Cdm16.ExceptionNumbers.EXTERNAL_EXC; + } + + return Integer.SIZE - Integer.numberOfLeadingZeros(triggeredExceptionsMask) - 1; + } } diff --git a/cocoemu/src/main/java/org/cdm/cocoemu/components/processors/cdm16e/exceptions/ExtendedExceptionControlUnit.java b/cocoemu/src/main/java/org/cdm/cocoemu/components/processors/cdm16e/exceptions/ExtendedExceptionControlUnit.java index 57a098dc..0e47546c 100644 --- a/cocoemu/src/main/java/org/cdm/cocoemu/components/processors/cdm16e/exceptions/ExtendedExceptionControlUnit.java +++ b/cocoemu/src/main/java/org/cdm/cocoemu/components/processors/cdm16e/exceptions/ExtendedExceptionControlUnit.java @@ -4,35 +4,11 @@ import org.cdm.cocoemu.components.processors.cdm16.units.exceptions.ExceptionControlUnitOutputParameters; import org.cdm.cocoemu.components.processors.cdm16e.Cdm16e.ExtendedExceptionNumbers; -import java.util.ArrayList; -import java.util.List; - public class ExtendedExceptionControlUnit { public static ExceptionControlUnitOutputParameters compute(ExtendedExceptionControlUnitInputParameters parameters) { - List triggeredExceptions = new ArrayList<>(); - - if (parameters.exc_trig_sp()) { - triggeredExceptions.add(Cdm16.ExceptionNumbers.UNALIGNED_SP); - } - if (parameters.exc_trig_pc()) { - triggeredExceptions.add(Cdm16.ExceptionNumbers.UNALIGNED_PC); - } - if (parameters.exc_trig_invalid_inst()) { - triggeredExceptions.add(Cdm16.ExceptionNumbers.INVALID_INST); - } - if (parameters.exc_trig_privilege_violation()) { - triggeredExceptions.add(ExtendedExceptionNumbers.PRIVILEGE_VIOLATION); - } - if (parameters.exc_trig_ext()) { - triggeredExceptions.add(Cdm16.ExceptionNumbers.EXTERNAL_EXC); - } - boolean gotException = true; - Integer excNumber = triggeredExceptions - .stream() - .max(Integer::compareTo) - .orElse(-1); + int excNumber = getExceptionNumber(parameters); if (excNumber == -1) { gotException = false; @@ -93,4 +69,26 @@ public static ExceptionControlUnitOutputParameters compute(ExtendedExceptionCont latch_double_fault ); } + + private static int getExceptionNumber(ExtendedExceptionControlUnitInputParameters parameters) { + int triggeredExceptionsMask = 0; + + if (parameters.exc_trig_sp()) { + triggeredExceptionsMask |= 1 << Cdm16.ExceptionNumbers.UNALIGNED_SP; + } + if (parameters.exc_trig_pc()) { + triggeredExceptionsMask |= 1 << Cdm16.ExceptionNumbers.UNALIGNED_PC; + } + if (parameters.exc_trig_invalid_inst()) { + triggeredExceptionsMask |= 1 << Cdm16.ExceptionNumbers.INVALID_INST; + } + if (parameters.exc_trig_privilege_violation()) { + triggeredExceptionsMask |= 1 << ExtendedExceptionNumbers.PRIVILEGE_VIOLATION; + } + if (parameters.exc_trig_ext()) { + triggeredExceptionsMask |= 1 << Cdm16.ExceptionNumbers.EXTERNAL_EXC; + } + + return Integer.SIZE - Integer.numberOfLeadingZeros(triggeredExceptionsMask) - 1; + } }