我正在构建一个模拟器,我有一个名为Emulator的类,它有与字体相关的数据和一组稍后放入CPU的指令。但是,我希望有一个“容器”类,它具有所有这些信息,因此Emulator更短,更易读。这个面向对象吗?什么是好方法?
final int[] fontSet = {
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
private List<Instruction> initInstructions() {
List<Instruction> instructions = new ArrayList<>(INSTRUCTIONS_NUM);
add0Instructions(instructions);
add1Instructions(instructions);
add2Instructions(instructions);
add3Instructions(instructions);
add4Instructions(instructions);
add5Instructions(instructions);
add6Instructions(instructions);
add7Instructions(instructions);
add8Instructions(instructions);
add9Instructions(instructions);
addAInstructions(instructions);
addBInstructions(instructions);
addCInstructions(instructions);
addDInstructions(instructions);
addEInstructions(instructions);
addFInstructions(instructions);
return instructions;
}
private void add0Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Clears Display
public void execute(final CPU cpu) {
cpu.getMemoryMap().getMemory(MemoryType.DISPLAY).clear();
Platform.runLater(new Runnable() { // TODO: NO SE SI HACE FALTA
@Override
public void run() {
display.paint(cpu.getMemoryMap().getMemory(MemoryType.DISPLAY));
}
});
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.get() == 0x00E0;
}
});
instructions.add(new Instruction() { //Return from a subroutine
public void execute(CPU cpu) {
cpu.getInstPointer().set(cpu.getStack().pop() + 2);
}
public boolean validate(OpCode opCode) {
return opCode.get() == 0x00EE;
}
});
}
private void add1Instructions(List<Instruction> instructions) { // Set InstPointer to KKK // CHEQUEADA
instructions.add(new Instruction() {
public void execute(CPU cpu) {
cpu.getInstPointer().set(cpu.getOpCode().get() & 0x0FFF);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x01;
}
});
}
private void add2Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Call subroutine at nnn
public void execute(CPU cpu) {
cpu.getStack().push(cpu.getInstPointer().get() & 0x0000FFFF);
cpu.getInstPointer().set(cpu.getOpCode().get() & 0x0FFF);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x02;
}
});
}
private void add3Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if Vx = kk
public void execute(CPU cpu) {
int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);
if(cpu.getRegistry(position).get() == data)
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x03;
}
});
}
private void add4Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if Vx != kk
public void execute(CPU cpu) {
int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int data = Bitwise.getByte(cpu.getOpCode().get(), 1);
if (cpu.getRegistry(position).get() != data)
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x04;
}
});
}
private void add5Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if Vx = Vy
public void execute(CPU cpu) {
int position1 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int position2 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 2);
if(cpu.getRegistry(position1).equals(cpu.getRegistry(position2)))
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x05 && opCode.getNibble(3) == 0x00;
}
});
}
private void add6Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set Vx = kk
public void execute(CPU cpu) {
int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);
cpu.getRegistry(position).set(data);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x6;
}
});
}
private void add7Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set Vx = Vx + kk
public void execute(CPU cpu) {
int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);
cpu.getRegistry(position).set(Bitwise.getByteAsInt(cpu.getRegistry(position).get() + data, 1));
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x07;
}
});
}
private void add8Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set Vx = Vy
public void execute(CPU cpu) {
int position1 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int position2 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 2);
cpu.getRegistry(position1).set(cpu.getRegistry(position2).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x00;
}
});
instructions.add(new Instruction() { //MUCHAS OPERACIONES LOGICAS
public void execute(CPU cpu) { //Set Vx = Vx OR Vy
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).or(cpu.getRegistry(position2));
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x01;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx AND Vy
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).and(cpu.getRegistry(position2));
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x02;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx XOR Vy
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).xor(cpu.getRegistry(position2));
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x03;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx + Vy, set VF = carry
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).add(cpu.getRegistry(position2));
cpu.getRegistry(0xF).set(cpu.getRegistry(position1).get() > 255 ? 0x1 : 0x0);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x04;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx - Vy, set VF = NOT borrow
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).sub(cpu.getRegistry(position2));
cpu.getRegistry(0xF).set(cpu.getRegistry(position1).get() > 0 ? 0x1 : 0x0);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x05;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx SHR 1
int position = cpu.getOpCode().getNibble(1);
cpu.getRegistry(0xF).set(cpu.getRegistry(position & 0x0001).get());
cpu.getRegistry(position).set(cpu.getRegistry(position).get() / 2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x06;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vy - Vx, set VF = NOT borrow
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
int sub = cpu.getRegistry(position2).get() - cpu.getRegistry(position1).get();
cpu.getRegistry(position1).set(sub);
cpu.getRegistry(0xF).set(sub > 0 ? 0x1 : 0x0);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x07;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx SHL 1
int position = cpu.getOpCode().getNibble(1);
cpu.getRegistry(0x0F).set((cpu.getRegistry((position & 0x8000) == 0x8000 ? 1 : 0).get()));
cpu.getRegistry(position).set(cpu.getRegistry(position).get() * 2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x0E;
}
});
}
private void add9Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if Vx != Vy
public void execute(CPU cpu) {
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
if(!cpu.getRegistry(position1).equals(cpu.getRegistry(position2)))
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x09;
}
});
}
private void addAInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set I = nnn
public void execute(CPU cpu) {
cpu.getRegisterI().set(cpu.getOpCode().get() & 0x0FFF);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0A;
}
});
}
private void addBInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Jump to location nnn + V0
public void execute(CPU cpu) {
int sum = Bitwise.getByteAsInt(cpu.getRegistry(0x0).get(), 1) + (cpu.getOpCode().get() & 0x0FFF);
cpu.getInstPointer().set(sum);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0B;
}
});
}
private void addCInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set Vx = random byte AND kk
public void execute(CPU cpu) {
int position = cpu.getOpCode().getNibble(1);
int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);
data = Bitwise.and(data, rand.nextInt(255 + 1)); // TODO: TAMBIEN NECESITA UN RAND
cpu.getRegistry(position).set(data);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0C;
}
});
}
private void addDInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision
public void execute(final CPU cpu) {
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
int height = cpu.getOpCode().getNibble(3);
int x = Bitwise.getByteAsInt(cpu.getRegistry(position1).get(), 1);
int y = Bitwise.getByteAsInt(cpu.getRegistry(position2).get(), 1);
cpu.getRegistry(0x0F).set(0);
for(int offsetY = 0; offsetY < height; offsetY++) {
int line = cpu.getMemoryMap().getMemory(MemoryType.RAM).get(cpu.getRegisterI().get() + offsetY);
for(int offsetX = 0; offsetX < 8; offsetX++) {
int pixel = line & (0x80 >> offsetX);
if(pixel != 0) {
int totalX = x + offsetX;
int totalY = y + offsetY;
int index;
totalX = totalX % 64;
totalY = totalY % 32;
index = (totalY * 64) + totalX;
if(cpu.getMemoryMap().getMemory(MemoryType.RAM).get(index) == 1)
cpu.getRegistry(0x0F).set(1);
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(index, cpu.getMemoryMap().getMemory(MemoryType.RAM).get(index) ^ 1);
}
}
}
cpu.getInstPointer().add(2);
//screen.setNeedRedraw(true); //ESTE BOOLEAN TIENE QUE IR EN ALGUN LADO
Platform.runLater(new Runnable() {
@Override
public void run() {
display.paint(cpu.getMemoryMap().getMemory(MemoryType.DISPLAY));
}
});
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0D;
}
});
}
private void addEInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if key with the value of Vx is pressed
public void execute(CPU cpu) {
int position = cpu.getOpCode().getNibble(1);
int data = cpu.getRegistry(position).get();
if(cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(data) == 1)
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0E && (opCode.get() & 0x00FF) == 0x9E;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Skip next instruction if key with the value of Vx is not pressed
int position = cpu.getOpCode().getNibble(1);
int data = cpu.getRegistry(position).get();
if(cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(data) == 0)
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0E && (opCode.get() & 0x00FF) == 0xA1;
}
});
}
private void addFInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = delay timer value
int position = cpu.getOpCode().getNibble(1);
cpu.getRegistry(position).set(cpu.getDelayTimer().get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x07;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Wait for a key press, store the value of the key in Vx
int position = cpu.getOpCode().getNibble(1);
int keyboardSize = cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).size();
for (int i = 0; i < keyboardSize; i++) {
if (cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(i) == 1) {
cpu.getRegistry(position).set(cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(i));
cpu.getInstPointer().add(2);
cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).clear();
return;
}
}
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x0A;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set delay timer = Vx
int position = cpu.getOpCode().getNibble(1);
cpu.getDelayTimer().set(cpu.getRegistry(position).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && ((opCode.get() & 0x00FF) == 0x15);
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set sound timer = Vx
int position = cpu.getOpCode().getNibble(1);
cpu.getSoundTimer().set(cpu.getRegistry(position).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x18;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set I = I + Vx
int position = cpu.getOpCode().getNibble(1);
cpu.getRegisterI().set(cpu.getRegisterI().get() + cpu.getRegistry(position).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x1E;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set I = location of sprite for digit Vx
int position = cpu.getOpCode().getNibble(1);
int character = cpu.getRegistry(position).get();
cpu.getRegisterI().set(0x0050 + character*5);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x29;
}
});
instructions.add(new Instruction() { //Store BCD representation of Vx in memory locations I, I+1, and I+2
public void execute(CPU cpu) {
int position = cpu.getOpCode().getNibble(1);
int data = cpu.getRegistry(position).get();
int hundreds = (data - (data % 100)) / 100;
int tens;
data -= hundreds * 100;
tens = (data - (data % 10)) / 10;
data -= tens * 10;
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get(), (byte)hundreds);
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get() + 1, (byte)tens);
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get() + 2, (byte)data);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x33;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Store registers V0 through Vx in memory starting at location I
int index = cpu.getOpCode().getNibble(1);
for(int i=0; i <= index; i++)
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get() + i, (byte)cpu.getRegistry(i).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x55;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Read registers V0 through Vx from memory starting at location I
int index = cpu.getOpCode().getNibble(1);
for(int i=0; i <= index; i++)
cpu.getRegistry(i).set(cpu.getMemoryMap().getMemory(MemoryType.RAM).get(cpu.getRegisterI().get() + i));
cpu.getRegisterI().set(cpu.getRegisterI().get() + index + 1);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x65;
}
});
}
答案 0 :(得分:4)
如果要将数据存储在变量中:
方法1:
让他们final
示例:public static final FieldType MYFIELD;
方法2:
您可以创建所有变量private
并为其编写getter。
示例2:
private static FieldType myField;
public static FieldType getMyField() {
return myField;
}
答案 1 :(得分:2)
不变性是您正在寻找的关键词。使您的数据容器(Model)类不可变是一个很好的设计
有效的Java项目项目15深入探讨了这一点。
答案 2 :(得分:1)
是的,这是Java中的常见实践。像这样创建一个POJO类:
public class Data {
private String property1;
public Data(String property1) {
this.property1 = property1;
}
public String getProperty1() {
return property1;
}
}
答案 3 :(得分:0)
说实话,我不完全确定你要从你发布的代码中完成什么。但是,为了回应您的实际标题“如何使用Java创建只读数据类”,您应该查看Enumerations。
基本上Enums将使您能够在编译时创建一次数据集,并提供一堆帮助方法来读取您想要的数据。
我不完全确定它是否会对您的用例有所帮助,但它肯定会允许您将数据分成只读样式格式。
答案 4 :(得分:-2)
我不确定我是否正确地读你,但听起来像你在谈论在源中定义一堆常量。这是一个错误 - 数据不应该存在于您的源代码中。它应该存在于数据文件中。