Skip to content

Commit

Permalink
Improved RFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
Ledmington committed Nov 4, 2024
1 parent f465b60 commit 98418c8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 23 deletions.
51 changes: 28 additions & 23 deletions emu/src/main/java/com/ledmington/emu/RFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,62 @@
public enum RFlags {

/** ID Flag (ID). System flag. */
ID(21, '?'),
ID(21, "ID"),

/** Virtual Interrupt Pending (VIP). System flag. */
VirtualInterruptPending(20, '?'),
VirtualInterruptPending(20, "VIP"),

/** Virtual Interrupt Flag (VIF). System flag. */
VirtualInterrupt(19, '?'),
VirtualInterrupt(19, "VIF"),

/** Alignment Check / Access Control (AC). System flag. */
AlignmentCheck(18, '?'),
AlignmentCheck(18, "AC"),

/** Virtual-8068 Mode (VM). System flag. */
Virtual8086Mode(17, 'V'),
Virtual8086Mode(17, "VM"),

/** Resume Flag (RF). System flag. */
Resume(16, 'R'),
Resume(16, "RF"),

/** Nested Task (NT). System flag. */
NestedTask(14, 'N'),
NestedTask(14, "NT"),

/** I/O Privilege Level. System flag. */
IOPrivilegeLevel(12, '?'),
/** I/O Privilege Level (IOPL). System flag. */
IOPrivilegeLevel(12, "IOPL"),

/** Overflow Flag (OF). Status flag. */
Overflow(11, 'O'),
Overflow(11, "OF"),

/** Direction Flag (DF). Control flag. */
Direction(10, 'D'),
Direction(10, "DF"),

/** Interrupt Enable Flag (IF). System flag. */
InterruptEnable(9, 'I'),
InterruptEnable(9, "IF"),

/** Trap Flag (TF). System flag. */
Trap(8, 'T'),
Trap(8, "TF"),

/** Sign Flag (SF). Status flag. */
Sign(7, 'S'),
Sign(7, "SF"),

/** Zero Flag (ZF). Status flag. */
Zero(6, 'Z'),
Zero(6, "ZF"),

/** Auxiliary Carry Flag (AF). Status flag. */
AuxiliaryCarry(4, 'A'),
AuxiliaryCarry(4, "AF"),

/** Parity Flag (PF). Status flag. */
Parity(2, 'P'),
Parity(2, "PF"),

/** Carry Flag (CF). Status flag. */
Carry(0, 'C');
Carry(0, "CF");

private final int bitIndex;
private final char initial;
private final String symbol;

RFlags(final int bit, final char initial) {
RFlags(final int bit, final String symbol) {
this.bitIndex = bit;
this.initial = initial;
this.symbol = symbol;
}

/**
Expand All @@ -88,7 +88,12 @@ public int bit() {
return bitIndex;
}

public char getInitial() {
return initial;
/**
* Returns a String representing the flag.
*
* @return The unique String representing the flag.
*/
public String getSymbol() {
return symbol;
}
}
68 changes: 68 additions & 0 deletions emu/src/test/java/com/ledmington/emu/TestRFlags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* emu - Processor Emulator
* Copyright (C) 2023-2024 Filippo Barbari <filippo.barbari@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ledmington.emu;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

final class TestRFlags {

@ParameterizedTest
@EnumSource(RFlags.class)
void validBits(final RFlags f) {
assertTrue(
f.bit() >= 0 && f.bit() < 32,
() -> String.format("RFlag %s has an invalid bit index of %,d.", f, f.bit()));
}

@Test
void uniqueBits() {
for (final RFlags a : RFlags.values()) {
for (final RFlags b : RFlags.values()) {
if (a == b) {
continue;
}
assertNotEquals(
a.bit(),
b.bit(),
() -> String.format(
"RFlag %s has the same bit index of RFlag %s, which is %,d.", a, b, a.bit()));
}
}
}

@Test
void uniqueSymbol() {
for (final RFlags a : RFlags.values()) {
for (final RFlags b : RFlags.values()) {
if (a == b) {
continue;
}
assertNotEquals(
a.getSymbol(),
b.getSymbol(),
() -> String.format(
"RFlag %s has the same symbol of RFlag %s, which is '%s'.", a, b, a.getSymbol()));
}
}
}
}

0 comments on commit 98418c8

Please sign in to comment.