Skip to content

Commit

Permalink
[cocoemu] Rewrite priority encoders in ExceptionControlUnits to bit m…
Browse files Browse the repository at this point in the history
…asks
  • Loading branch information
Intelix8996 committed Jul 31, 2024
1 parent 7e211b0 commit fa05678
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit fa05678

Please sign in to comment.