我建议您mov
通过位掩码选择功能并将检测延迟hlt
到逻辑的进一步下方。
public static final int MOVMatch = 0b01000000;
public static final int MOVMask = 0b00111111;
// I am guessing 3 bits per register - check this in the spec.
public static final int MOVFrom = 0b00111000;
public static final int MOVTo = 0b00000111;
public static final int MOVHLT = 0b00110110;
if ( (PC & MOVMatch) == MOVMatch) {
if ( (PC & MOVMatch) != MOVHLT ) {
int movFrom = PC & MOVFrom;
int movTo = PC & MOVTo;
// ...
} else {
// HLT!
}
}
另一种模式是将过程编码为一组命令。这样的事情可能是一个公平的起点:
public class Test {
enum Reg {
A, B, C, D, E, H, L, M, None;
}
static class Op {
final Act act;
final Reg f;
final Reg t;
// Default missing params to none.
public Op(Act act, Reg f) {
this(act, f, Reg.None);
}
public Op(Act act) {
this(act, Reg.None, Reg.None);
}
public Op(Act act, Reg f, Reg t) {
this.act = act;
this.f = f;
this.t = t;
}
// Perform the action.
public void act() {
act.act(f, t);
}
}
enum Act {
Mov {
@Override
void act(Reg f, Reg t) {
// Do something to the state of machine state that emulates a move from f to t.
// For demo, just print.
super.act(f, t);
}
},
Hlt {
@Override
void act(Reg f, Reg t) {
// Do something to the state of machine state that halts.
// For demo, just print.
super.act(f, t);
}
};
// All instructions should have an action.
void act(Reg f, Reg t) {
// Default action just prints.
System.out.println(this + " " + f + "," + t);
}
}
enum Inst {
b01000000(new Op(Act.Mov, Reg.B, Reg.B)),
b01000001(new Op(Act.Mov, Reg.B, Reg.C)),
b01110110(new Op(Act.Hlt));
final Op op;
Inst(Op op) {
this.op = op;
}
void act () {
op.act();
}
}
public void test() {
for ( Inst i : Inst.values() ) {
i.act();
}
}
public static void main(String args[]) {
try {
new Test().test();
} catch (Throwable t) {
t.printStackTrace(System.err);
}
}
}