6 | * Defines what a CPU can do 7 | */ 8 | public interface InstructionSet { 9 | 10 | /** 11 | * Get an instruction by its opcode 12 | * 13 | * @param opcode opcode of the instruction 14 | * @return the instruction, null is not found 15 | */ 16 | Instruction get(int opcode); 17 | 18 | /** 19 | * Get an instruction by its mnemonic 20 | * 21 | * @param mnemonic mnemonic of the instruction, not case sensitive 22 | * @return the instruction, if not found, the default instruction is returned 23 | */ 24 | Instruction get(String mnemonic); 25 | 26 | /** 27 | * Add an instruction to the set 28 | * 29 | * @param instruction instruction to add 30 | */ 31 | void add(Instruction instruction); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/OperandType.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly; 2 | 3 | /** 4 | * Types of an operand 5 | */ 6 | public enum OperandType { 7 | 8 | REGISTER16("16-bit Register"), 9 | MEMORY_IMM16("16-bit Memory referred by immediate"), 10 | MEMORY_REG16("16-bit Memory referred by register"), 11 | MEMORY_REG_DISP16("16-bit Memory referred by register with displacement"), 12 | IMMEDIATE16("16-bit Immediate"); 13 | 14 | /** 15 | * Description of the Operand type 16 | */ 17 | private String description; 18 | 19 | public String getDescription() { 20 | return description; 21 | } 22 | 23 | OperandType(String desc) { 24 | this.description = desc; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/Register.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly; 2 | 3 | /** 4 | * Represents a register in a cpu 5 | */ 6 | public class Register { 7 | 8 | /** 9 | * Name of the register 10 | */ 11 | private String name; 12 | 13 | /** 14 | * 16-bit value of the register 15 | */ 16 | private char value = 0; 17 | 18 | /** 19 | * Create a new Register 20 | * 21 | * @param name Name of the register, always set in Upper case 22 | */ 23 | public Register(String name) { 24 | this.name = name.toUpperCase(); 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public char getValue() { 32 | return value; 33 | } 34 | 35 | /** 36 | * Set value of the register. 37 | * 38 | * @param value value to set. It is casted to char 39 | */ 40 | public void setValue(int value) { 41 | this.value = (char) value; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | 47 | return name + "=" + value; 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/Section.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly; 2 | 3 | /** 4 | * Section of a user-created program. 5 | * The execution will start at the beginning of the code 6 | * segment. 7 | */ 8 | public enum Section { 9 | 10 | /** 11 | * Code section of the program. Contains executable code 12 | */ 13 | TEXT, 14 | 15 | /** 16 | * Data section of the program. Contains initialised data 17 | */ 18 | DATA 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/Target.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly; 2 | 3 | /** 4 | * A Target is a location that can be read and written to during 5 | * the execution of an instruction. 6 | *
7 | * For example: MOV dstTARGET, srcTARGET 8 | *
9 | * A target is usually Memory or Register
10 | */
11 | public interface Target {
12 |
13 | /**
14 | * Get a value from a Target at an address.
15 | *
16 | * @param address Address of the value. Can refer to a memory address or the index
17 | * of a register
18 | * @return value at specified address
19 | */
20 | int get(int address);
21 |
22 | /**
23 | * Set a value at an address
24 | *
25 | * @param address address of the value to change
26 | * @param value value to set
27 | */
28 | void set(int address, int value);
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/AssemblyException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 | /**
4 | * Threw when a problem is encountered while parsing a line
5 | * of a user's code, making it impossible to translate it to
6 | * binary code.
7 | */
8 | public class AssemblyException extends Exception {
9 |
10 | /**
11 | * Line offset in the user's code.
12 | */
13 | private int line;
14 |
15 | /**
16 | * Create a new Assembly Exception
17 | *
18 | * @param msg Message of the exception
19 | * @param line Line offset in the user's code.
20 | */
21 | public AssemblyException(String msg, int line) {
22 | super(msg);
23 | this.line = line;
24 | }
25 |
26 | public int getLine() {
27 | return line;
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/CancelledException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 | public class CancelledException extends Exception {
4 | public CancelledException() {
5 | super("CPU Initialisation was cancelled");
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/DuplicateSectionException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 | /**
4 | * Threw when a user attempts to define the same section twice
5 | */
6 | public class DuplicateSectionException extends AssemblyException {
7 |
8 | /**
9 | * Message of the exception
10 | */
11 | private static final String message = "Sections can only be defined once";
12 |
13 | /**
14 | * Create a new Duplicate Section Exception
15 | */
16 | public DuplicateSectionException(int line) {
17 | super(message, line);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/EmptyLineException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 | /**
4 | * Threw when the parser encounters an empty line
5 | */
6 | public class EmptyLineException extends AssemblyException {
7 | public EmptyLineException(int line) {
8 | super("Encountered empty line", line);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/FatalAssemblyException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 | /**
4 | * Class of exceptions that should stop assembly immediately
5 | */
6 | public class FatalAssemblyException extends AssemblyException {
7 |
8 | /**
9 | * Message of the exception
10 | */
11 | private static final String message = "A fatal assembly error has occurred";
12 |
13 | /**
14 | * Create a new Duplicate Section Exception
15 | */
16 | public FatalAssemblyException(String msg, int line) {
17 | super(msg, line);
18 | }
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/IllegalOperandException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 |
4 | public class IllegalOperandException extends AssemblyException {
5 | public IllegalOperandException(String msg, int line) {
6 | super(msg, line);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/InvalidMnemonicException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 | /**
4 | * Threw when the parse encounters an invalid mnemonic
5 | */
6 | public class InvalidMnemonicException extends AssemblyException {
7 | public InvalidMnemonicException(String mnemonic, int line) {
8 | super("Unknown mnemonic \"" + mnemonic + "\"", line);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/InvalidOperandException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 | /**
4 | * Threw when the Assembler attempts to parse a malformed or invalid operand
5 | */
6 | public class InvalidOperandException extends AssemblyException {
7 |
8 | /**
9 | * Creates a new Invalid operand Exception
10 | *
11 | * @param msg Message
12 | * @param line Line offset in the user's code.Used to display an error icon
13 | * in the editor.
14 | */
15 | public InvalidOperandException(String msg, int line) {
16 | super(msg, line);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/OffsetOverflowException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 | /**
4 | * Threw when offset for stored instruction/data overflows the size of memory
5 | */
6 | public class OffsetOverflowException extends FatalAssemblyException {
7 |
8 | /**
9 | * Message of the exception
10 | */
11 | private static final String message = "Program data exceeds memory size ";
12 |
13 | /**
14 | * Create a new Offset Overflow Exception
15 | */
16 | public OffsetOverflowException(int offset, int memSize, int line) {
17 | super(message + offset + " > " + memSize, line);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/exception/PseudoInstructionException.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.exception;
2 |
3 | /**
4 | * Threw when the parser encounters a pseudo instruction
5 | * (Instruction that doesn't produce any binary output
6 | */
7 | public class PseudoInstructionException extends AssemblyException {
8 | public PseudoInstructionException(int line) {
9 | super("Pseudo instruction encountered", line);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/BrkInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.Instruction;
4 | import net.simon987.server.assembly.Status;
5 |
6 | /**
7 | * BRK (Break) Instruction. Will set the break flag and stop the CPU
8 | * execution
9 | */
10 | public class BrkInstruction extends Instruction {
11 |
12 | public static final int OPCODE = 0;
13 |
14 | public BrkInstruction() {
15 | super("brk", OPCODE);
16 | }
17 |
18 | @Override
19 | public Status execute(Status status) {
20 |
21 | status.setBreakFlag(true);
22 |
23 | return status;
24 | }
25 |
26 | public boolean noOperandsValid() {
27 | return true;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/CallInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | /**
9 | * Move the execution (Jump) to an address, and save the current IP value, the execution will return to this value
10 | * after the RET instruction is executed
11 | *
12 | * FLAGS are not altered
13 | */
14 | public class CallInstruction extends Instruction {
15 |
16 | public static final int OPCODE = 21;
17 |
18 | private CPU cpu;
19 |
20 | public CallInstruction(CPU cpu) {
21 | super("call", OPCODE);
22 |
23 | this.cpu = cpu;
24 | }
25 |
26 | @Override
27 | public Status execute(Target src, int srcIndex, Status status) {
28 | //Push ip
29 | cpu.getRegisterSet().set(7, cpu.getRegisterSet().get(7) - 1); //Decrement SP (stack grows towards smaller addresses)
30 | cpu.getMemory().set(cpu.getRegisterSet().get(7), cpu.getIp());
31 |
32 | //Jmp
33 | cpu.setIp((char) src.get(srcIndex));
34 |
35 | return status;
36 | }
37 |
38 | @Override
39 | public Status execute(int src, Status status) {
40 | //Push ip
41 | cpu.getRegisterSet().set(7, cpu.getRegisterSet().get(7) - 1); //Decrement SP (stack grows towards smaller addresses)
42 | cpu.getMemory().set(cpu.getRegisterSet().get(7), cpu.getIp());
43 |
44 | //Jmp
45 | cpu.setIp((char) src);
46 |
47 | return status;
48 | }
49 | }
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/DecInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.Instruction;
4 | import net.simon987.server.assembly.Status;
5 | import net.simon987.server.assembly.Target;
6 | import net.simon987.server.assembly.Util;
7 |
8 | public class DecInstruction extends Instruction {
9 |
10 | public static final int OPCODE = 43;
11 |
12 | public DecInstruction() {
13 | super("dec", OPCODE);
14 | }
15 |
16 | @Override
17 | public Status execute(Target dst, int dstIndex, Status status) {
18 | char a = (char) dst.get(dstIndex);
19 | int result = a - 1;
20 |
21 | // Like x86 Carry flag is preserved during INC/DEC
22 | // (Use ADD x, 1 to have carry flag change)
23 | // Other flags set according to result
24 | status.setSignFlag(Util.checkSign16(result));
25 | status.setZeroFlag((char) result == 0);
26 | status.setOverflowFlag(Util.checkOverFlowSub16(a, 1));
27 |
28 | dst.set(dstIndex, result);
29 | return status;
30 | }
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/HwiInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | /**
9 | * Send hardware interrupt
10 | *
Used to interact with the World using hardware
11 | */
12 | public class HwiInstruction extends Instruction {
13 |
14 | public static final int OPCODE = 9;
15 |
16 | private CPU cpu;
17 |
18 | public HwiInstruction(CPU cpu) {
19 | super("hwi", OPCODE);
20 | this.cpu = cpu;
21 | }
22 |
23 | @Override
24 | public Status execute(Target src, int srcIndex, Status status) {
25 | status.setErrorFlag(cpu.getHardwareHost().hardwareInterrupt(src.get(srcIndex), cpu.getStatus()));
26 |
27 | return status;
28 | }
29 |
30 | @Override
31 | public Status execute(int src, Status status) {
32 |
33 | status.setErrorFlag(cpu.getHardwareHost().hardwareInterrupt(src, cpu.getStatus()));
34 |
35 | return status;
36 | }
37 |
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/HwqInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.*;
4 |
5 | public class HwqInstruction extends Instruction {
6 |
7 | private static final int OPCODE = 28;
8 |
9 | private CPU cpu;
10 | private Register b;
11 |
12 | public HwqInstruction(CPU cpu) {
13 | super("hwq", OPCODE);
14 | this.cpu = cpu;
15 | this.b = cpu.getRegisterSet().getRegister("B");
16 | }
17 |
18 | @Override
19 | public Status execute(Target src, int srcIndex, Status status) {
20 | b.setValue(cpu.getHardwareHost().hardwareQuery(src.get(srcIndex)));
21 |
22 | return status;
23 | }
24 |
25 | @Override
26 | public Status execute(int src, Status status) {
27 | b.setValue(cpu.getHardwareHost().hardwareQuery(src));
28 |
29 | return status;
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/IncInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.Instruction;
4 | import net.simon987.server.assembly.Status;
5 | import net.simon987.server.assembly.Target;
6 | import net.simon987.server.assembly.Util;
7 |
8 | public class IncInstruction extends Instruction {
9 |
10 | public static final int OPCODE = 42;
11 |
12 | public IncInstruction() {
13 | super("inc", OPCODE);
14 | }
15 |
16 | @Override
17 | public Status execute(Target dst, int dstIndex, Status status) {
18 | char a = (char) dst.get(dstIndex);
19 | int result = a + 1;
20 |
21 | // Like x86 Carry flag is preserved during INC/DEC
22 | // (Use ADD x, 1 to have carry flag change)
23 | // Other flags set according to result
24 | status.setSignFlag(Util.checkSign16(result));
25 | status.setZeroFlag((char) result == 0);
26 | status.setOverflowFlag(Util.checkOverFlowAdd16(a, 1));
27 |
28 | dst.set(dstIndex, result);
29 | return status;
30 | }
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JaInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | /**
9 | * Jump if above
10 | */
11 | public class JaInstruction extends Instruction {
12 |
13 | public static final int OPCODE = 46;
14 |
15 | private CPU cpu;
16 |
17 | public JaInstruction(CPU cpu) {
18 | super("ja", OPCODE);
19 |
20 | this.cpu = cpu;
21 | }
22 |
23 | @Override
24 | public Status execute(Target src, int srcIndex, Status status) {
25 | if (!status.isCarryFlag() && !status.isZeroFlag()) {
26 | cpu.setIp((char) src.get(srcIndex));
27 | }
28 | return status;
29 | }
30 |
31 | @Override
32 | public Status execute(int src, Status status) {
33 | if (!status.isCarryFlag() && !status.isZeroFlag()) {
34 | cpu.setIp((char) src);
35 | }
36 | return status;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JcInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JcInstruction extends Instruction {
9 |
10 | private static final int OPCODE = 33;
11 |
12 | private CPU cpu;
13 |
14 | public JcInstruction(CPU cpu) {
15 | super("jc", OPCODE);
16 | this.cpu = cpu;
17 | }
18 |
19 | @Override
20 | public Status execute(Target src, int srcIndex, Status status) {
21 | if (status.isCarryFlag()) {
22 | cpu.setIp((char) src.get(srcIndex));
23 | }
24 | return status;
25 | }
26 |
27 | @Override
28 | public Status execute(int src, Status status) {
29 | if (status.isCarryFlag()) {
30 | cpu.setIp((char) src);
31 | }
32 | return status;
33 | }
34 |
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JgInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JgInstruction extends Instruction {
9 |
10 | public static final int OPCODE = 15;
11 |
12 | private CPU cpu;
13 |
14 | public JgInstruction(CPU cpu) {
15 | super("jg", OPCODE);
16 |
17 | this.cpu = cpu;
18 | }
19 |
20 | @Override
21 | public Status execute(Target src, int srcIndex, Status status) {
22 | if (status.isSignFlag() == status.isOverflowFlag() && !status.isZeroFlag()) {
23 | cpu.setIp((char) src.get(srcIndex));
24 | }
25 | return status;
26 | }
27 |
28 | @Override
29 | public Status execute(int src, Status status) {
30 | if (status.isSignFlag() == status.isOverflowFlag() && !status.isZeroFlag()) {
31 | cpu.setIp((char) src);
32 | }
33 | return status;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JgeInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | /**
9 | * Conditional jump: jump if greater or equal
10 | */
11 | public class JgeInstruction extends Instruction {
12 |
13 | public static final int OPCODE = 16;
14 |
15 | private CPU cpu;
16 |
17 | public JgeInstruction(CPU cpu) {
18 | super("jge", OPCODE);
19 |
20 | this.cpu = cpu;
21 | }
22 |
23 | @Override
24 | public Status execute(Target src, int srcIndex, Status status) {
25 | if (status.isSignFlag() == status.isOverflowFlag()) {
26 | cpu.setIp((char) src.get(srcIndex));
27 | }
28 | return status;
29 | }
30 |
31 | @Override
32 | public Status execute(int src, Status status) {
33 | if (status.isSignFlag() == status.isOverflowFlag()) {
34 | cpu.setIp((char) src);
35 | }
36 | return status;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JlInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JlInstruction extends Instruction {
9 |
10 | public static final int OPCODE = 17;
11 |
12 | private CPU cpu;
13 |
14 | public JlInstruction(CPU cpu) {
15 | super("jl", OPCODE);
16 |
17 | this.cpu = cpu;
18 | }
19 |
20 | @Override
21 | public Status execute(Target src, int srcIndex, Status status) {
22 | if (status.isSignFlag() != status.isOverflowFlag()) {
23 | cpu.setIp((char) src.get(srcIndex));
24 | }
25 | return status;
26 | }
27 |
28 | @Override
29 | public Status execute(int src, Status status) {
30 | if (status.isSignFlag() != status.isOverflowFlag()) {
31 | cpu.setIp((char) src);
32 | }
33 | return status;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JleInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JleInstruction extends Instruction {
9 |
10 | public static final int OPCODE = 18;
11 |
12 | private CPU cpu;
13 |
14 | public JleInstruction(CPU cpu) {
15 | super("jle", OPCODE);
16 |
17 | this.cpu = cpu;
18 | }
19 |
20 | @Override
21 | public Status execute(Target src, int srcIndex, Status status) {
22 | if (status.isSignFlag() != status.isOverflowFlag() || status.isZeroFlag()) {
23 | cpu.setIp((char) src.get(srcIndex));
24 | }
25 | return status;
26 | }
27 |
28 | @Override
29 | public Status execute(int src, Status status) {
30 | if (status.isSignFlag() != status.isOverflowFlag() || status.isZeroFlag()) {
31 | cpu.setIp((char) src);
32 | }
33 | return status;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JmpInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JmpInstruction extends Instruction {
9 |
10 | public static final int OPCODE = 10;
11 |
12 | private CPU cpu;
13 |
14 | public JmpInstruction(CPU cpu) {
15 | super("jmp", OPCODE);
16 |
17 | this.cpu = cpu;
18 | }
19 |
20 | @Override
21 | public Status execute(Target src, int srcIndex, Status status) {
22 |
23 | cpu.setIp((char) src.get(srcIndex));
24 | return status;
25 | }
26 |
27 | @Override
28 | public Status execute(int src, Status status) {
29 |
30 | cpu.setIp((char) src);
31 | return status;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JnaInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | /**
9 | * Jump if not above
10 | */
11 | public class JnaInstruction extends Instruction {
12 |
13 | public static final int OPCODE = 47;
14 |
15 | private CPU cpu;
16 |
17 | public JnaInstruction(CPU cpu) {
18 | super("jna", OPCODE);
19 |
20 | this.cpu = cpu;
21 | }
22 |
23 | @Override
24 | public Status execute(Target src, int srcIndex, Status status) {
25 | if (status.isCarryFlag() || status.isZeroFlag()) {
26 | cpu.setIp((char) src.get(srcIndex));
27 | }
28 | return status;
29 | }
30 |
31 | @Override
32 | public Status execute(int src, Status status) {
33 | if (status.isCarryFlag() || status.isZeroFlag()) {
34 | cpu.setIp((char) src);
35 | }
36 | return status;
37 | }
38 | }
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JncInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JncInstruction extends Instruction {
9 |
10 | private static final int OPCODE = 34;
11 |
12 | private CPU cpu;
13 |
14 | public JncInstruction(CPU cpu) {
15 | super("jnc", OPCODE);
16 | this.cpu = cpu;
17 | }
18 |
19 | @Override
20 | public Status execute(Target src, int srcIndex, Status status) {
21 | if (!status.isCarryFlag()) {
22 | cpu.setIp((char) src.get(srcIndex));
23 | }
24 | return status;
25 | }
26 |
27 | @Override
28 | public Status execute(int src, Status status) {
29 | if (!status.isCarryFlag()) {
30 | cpu.setIp((char) src);
31 | }
32 | return status;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JnoInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 |
9 | public class JnoInstruction extends Instruction {
10 |
11 | private static final int OPCODE = 37;
12 |
13 | private CPU cpu;
14 |
15 | public JnoInstruction(CPU cpu) {
16 | super("jno", OPCODE);
17 | this.cpu = cpu;
18 | }
19 |
20 | @Override
21 | public Status execute(Target src, int srcIndex, Status status) {
22 | if (!status.isOverflowFlag()) {
23 | cpu.setIp((char) src.get(srcIndex));
24 | }
25 | return status;
26 | }
27 |
28 | @Override
29 | public Status execute(int src, Status status) {
30 | if (!status.isOverflowFlag()) {
31 | cpu.setIp((char) src);
32 | }
33 | return status;
34 | }
35 | }
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JnsInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JnsInstruction extends Instruction {
9 |
10 | public static final int OPCODE = 27;
11 |
12 | private CPU cpu;
13 |
14 | public JnsInstruction(CPU cpu) {
15 | super("jns", OPCODE);
16 |
17 | this.cpu = cpu;
18 | }
19 |
20 | @Override
21 | public Status execute(Target src, int srcIndex, Status status) {
22 | if (!status.isSignFlag()) {
23 | cpu.setIp((char) src.get(srcIndex));
24 | }
25 | return status;
26 | }
27 |
28 | @Override
29 | public Status execute(int src, Status status) {
30 | if (!status.isSignFlag()) {
31 | cpu.setIp((char) src);
32 | }
33 | return status;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JnzInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JnzInstruction extends Instruction {
9 |
10 | public static final int OPCODE = 13;
11 |
12 | private CPU cpu;
13 |
14 | public JnzInstruction(CPU cpu) {
15 | super("jnz", OPCODE);
16 |
17 | this.cpu = cpu;
18 | }
19 |
20 | @Override
21 | public Status execute(Target src, int srcIndex, Status status) {
22 | if (!status.isZeroFlag()) {
23 | cpu.setIp((char) src.get(srcIndex));
24 | }
25 | return status;
26 | }
27 |
28 | @Override
29 | public Status execute(int src, Status status) {
30 | if (!status.isZeroFlag()) {
31 | cpu.setIp((char) src);
32 | }
33 | return status;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JoInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JoInstruction extends Instruction {
9 |
10 | private static final int OPCODE = 36;
11 |
12 | private CPU cpu;
13 |
14 | public JoInstruction(CPU cpu) {
15 | super("jo", OPCODE);
16 | this.cpu = cpu;
17 | }
18 |
19 | @Override
20 | public Status execute(Target src, int srcIndex, Status status) {
21 | if (status.isOverflowFlag()) {
22 | cpu.setIp((char) src.get(srcIndex));
23 | }
24 | return status;
25 | }
26 |
27 | @Override
28 | public Status execute(int src, Status status) {
29 | if (status.isOverflowFlag()) {
30 | cpu.setIp((char) src);
31 | }
32 | return status;
33 | }
34 | }
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JsInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JsInstruction extends Instruction {
9 |
10 | public static final int OPCODE = 26;
11 |
12 | private CPU cpu;
13 |
14 | public JsInstruction(CPU cpu) {
15 | super("js", OPCODE);
16 |
17 | this.cpu = cpu;
18 | }
19 |
20 | @Override
21 | public Status execute(Target src, int srcIndex, Status status) {
22 | if (status.isSignFlag()) {
23 | cpu.setIp((char) src.get(srcIndex));
24 | }
25 | return status;
26 | }
27 |
28 | @Override
29 | public Status execute(int src, Status status) {
30 | if (status.isSignFlag()) {
31 | cpu.setIp((char) src);
32 | }
33 | return status;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/JzInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.CPU;
4 | import net.simon987.server.assembly.Instruction;
5 | import net.simon987.server.assembly.Status;
6 | import net.simon987.server.assembly.Target;
7 |
8 | public class JzInstruction extends Instruction {
9 |
10 | /**
11 | * Opcode of the instruction
12 | */
13 | public static final int OPCODE = 14;
14 |
15 | private CPU cpu;
16 |
17 | public JzInstruction(CPU cpu) {
18 | super("jz", OPCODE);
19 |
20 | this.cpu = cpu;
21 | }
22 |
23 | @Override
24 | public Status execute(Target src, int srcIndex, Status status) {
25 | if (status.isZeroFlag()) {
26 | cpu.setIp((char) src.get(srcIndex));
27 | }
28 | return status;
29 | }
30 |
31 | @Override
32 | public Status execute(int src, Status status) {
33 | if (status.isZeroFlag()) {
34 | cpu.setIp((char) src);
35 | }
36 | return status;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/LeaInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.Instruction;
4 |
5 | public class LeaInstruction extends Instruction {
6 |
7 | public static final int OPCODE = 30;
8 |
9 | public LeaInstruction() {
10 | super("lea", OPCODE);
11 | }
12 |
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/Server/src/main/java/net/simon987/server/assembly/instruction/MovInstruction.java:
--------------------------------------------------------------------------------
1 | package net.simon987.server.assembly.instruction;
2 |
3 | import net.simon987.server.assembly.Instruction;
4 | import net.simon987.server.assembly.Status;
5 | import net.simon987.server.assembly.Target;
6 |
7 | /**
8 | * The MOV instruction copies data from a source to a destination.
9 | * it overwrites the value in the destination. The destination can be
10 | * memory or register, while the source can be an immediate value, memory
11 | * or register.
12 | *
13 | * The instruction might have unexpected behavior if the an 14 | * operand of type [register + displacement] is used and its sum 15 | * is greater than 0xFFFF (It will overflow) 16 | *
17 | *18 | * The MOV instruction doesn't change any flags 19 | *
20 | */ 21 | public class MovInstruction extends Instruction { 22 | 23 | public static final int OPCODE = 1; 24 | 25 | public MovInstruction() { 26 | super("mov", OPCODE); 27 | } 28 | 29 | @Override 30 | public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) { 31 | 32 | dst.set(dstIndex, src.get(srcIndex)); 33 | 34 | return status; 35 | } 36 | 37 | @Override 38 | public Status execute(Target dst, int dstIndex, int src, Status status) { 39 | 40 | dst.set(dstIndex, src); 41 | 42 | 43 | return status; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/NegInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.Instruction; 4 | import net.simon987.server.assembly.Status; 5 | import net.simon987.server.assembly.Target; 6 | 7 | public class NegInstruction extends Instruction { 8 | 9 | public static final int OPCODE = 25; 10 | 11 | public NegInstruction() { 12 | super("neg", OPCODE); 13 | } 14 | 15 | @Override 16 | public Status execute(Target dst, int dstIndex, Status status) { 17 | //If the operand is zero, the carry flag is cleared; in all other cases, the carry flag is set. 18 | 19 | char destination = (char) dst.get(dstIndex); 20 | 21 | if (destination == 0) { 22 | status.setCarryFlag(false); 23 | status.setZeroFlag(true); 24 | } else { 25 | status.setCarryFlag(true); 26 | } 27 | 28 | //Attempting to negate a word containing -32,768 causes no change to the operand and sets the Overflow Flag. 29 | if (destination == 0x8000) { 30 | status.setOverflowFlag(true); 31 | } else { 32 | dst.set(dstIndex, -destination); 33 | } 34 | 35 | return status; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/NopInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.Instruction; 4 | 5 | /** 6 | * NOP (No operation instruction). 7 | * Does nothing 8 | */ 9 | public class NopInstruction extends Instruction { 10 | 11 | public static final int OPCODE = 63; 12 | 13 | public NopInstruction() { 14 | super("nop", OPCODE); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/NotInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.Instruction; 4 | import net.simon987.server.assembly.Status; 5 | import net.simon987.server.assembly.Target; 6 | 7 | public class NotInstruction extends Instruction { 8 | 9 | public static final int OPCODE = 29; 10 | 11 | public NotInstruction() { 12 | super("not", OPCODE); 13 | } 14 | 15 | @Override 16 | public Status execute(Target dst, int dstIndex, Status status) { 17 | dst.set(dstIndex, ~dst.get(dstIndex)); 18 | 19 | return status; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/OrInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.Instruction; 4 | import net.simon987.server.assembly.Status; 5 | import net.simon987.server.assembly.Target; 6 | import net.simon987.server.assembly.Util; 7 | 8 | public class OrInstruction extends Instruction { 9 | 10 | public static final int OPCODE = 5; 11 | 12 | public OrInstruction() { 13 | super("or", OPCODE); 14 | } 15 | 16 | @Override 17 | public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) { 18 | 19 | int a = (char) dst.get(dstIndex); 20 | int b = (char) src.get(srcIndex); 21 | 22 | 23 | int result = (a | b); 24 | 25 | status.setSignFlag(Util.checkSign16(result)); 26 | status.setZeroFlag((char) result == 0); 27 | status.setOverflowFlag(false); 28 | status.setCarryFlag(false); 29 | 30 | dst.set(dstIndex, result); 31 | 32 | return status; 33 | } 34 | 35 | @Override 36 | public Status execute(Target dst, int dstIndex, int src, Status status) { 37 | int a = (char) dst.get(dstIndex); 38 | int b = (char) src; 39 | 40 | 41 | int result = (a | b); 42 | 43 | status.setSignFlag(Util.checkSign16(result)); 44 | status.setZeroFlag((char) result == 0); 45 | status.setOverflowFlag(false); 46 | status.setCarryFlag(false); 47 | 48 | dst.set(dstIndex, result); 49 | 50 | return status; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/PopInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.CPU; 4 | import net.simon987.server.assembly.Instruction; 5 | import net.simon987.server.assembly.Status; 6 | import net.simon987.server.assembly.Target; 7 | 8 | /** 9 | * Created by simon on 02/06/17. 10 | */ 11 | public class PopInstruction extends Instruction { 12 | 13 | public static final int OPCODE = 20; 14 | 15 | private CPU cpu; 16 | 17 | public PopInstruction(CPU cpu) { 18 | super("pop", OPCODE); 19 | 20 | this.cpu = cpu; 21 | } 22 | 23 | @Override 24 | public Status execute(Target dst, int dstIndex, Status status) { 25 | 26 | dst.set(dstIndex, cpu.getMemory().get(cpu.getRegisterSet().getRegister("SP").getValue())); 27 | cpu.getRegisterSet().getRegister("SP").setValue(cpu.getRegisterSet().getRegister("SP").getValue() + 1); //Increment SP (stack grows towards smaller) 28 | 29 | return status; 30 | } 31 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/PopfInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.CPU; 4 | import net.simon987.server.assembly.Instruction; 5 | import net.simon987.server.assembly.Register; 6 | import net.simon987.server.assembly.Status; 7 | 8 | /** 9 | * Pops a single word off the top of the stack and sets the CPU flags to it. 10 | */ 11 | public class PopfInstruction extends Instruction { 12 | 13 | public static final int OPCODE = 44; 14 | 15 | private CPU cpu; 16 | 17 | public PopfInstruction(CPU cpu) { 18 | super("popf", OPCODE); 19 | 20 | this.cpu = cpu; 21 | } 22 | 23 | @Override 24 | public Status execute(Status status) { 25 | 26 | Register sp = cpu.getRegisterSet().getRegister("SP"); 27 | 28 | // Get the word on the top of the stack 29 | char flags = (char) cpu.getMemory().get(sp.getValue()); 30 | 31 | // Overwrite the CPU flags 32 | status.fromByte(flags); 33 | 34 | // Increment SP 35 | sp.setValue(sp.getValue() + 1); 36 | 37 | return status; 38 | } 39 | 40 | public boolean noOperandsValid() { 41 | return true; 42 | } 43 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/PushInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.*; 4 | 5 | public class PushInstruction extends Instruction { 6 | 7 | public static final int OPCODE = 19; 8 | 9 | private CPU cpu; 10 | 11 | public PushInstruction(CPU cpu) { 12 | super("push", OPCODE); 13 | 14 | this.cpu = cpu; 15 | } 16 | 17 | @Override 18 | public Status execute(Target src, int srcIndex, Status status) { 19 | 20 | Register sp = cpu.getRegisterSet().getRegister("SP"); 21 | 22 | sp.setValue(sp.getValue() - 1); //Decrement SP (stack grows towards smaller) 23 | cpu.getMemory().set(sp.getValue(), src.get(srcIndex)); 24 | 25 | return status; 26 | } 27 | 28 | @Override 29 | public Status execute(int src, Status status) { 30 | Register sp = cpu.getRegisterSet().getRegister("SP"); 31 | 32 | sp.setValue(sp.getValue() - 1); //Decrement SP (stack grows towards smaller) 33 | cpu.getMemory().set(sp.getValue(), src); 34 | 35 | return status; 36 | } 37 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/PushfInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.CPU; 4 | import net.simon987.server.assembly.Instruction; 5 | import net.simon987.server.assembly.Register; 6 | import net.simon987.server.assembly.Status; 7 | 8 | /** 9 | * Pushes the current CPU flags onto the stack. 10 | */ 11 | public class PushfInstruction extends Instruction { 12 | 13 | public static final int OPCODE = 45; 14 | 15 | private CPU cpu; 16 | 17 | public PushfInstruction(CPU cpu) { 18 | super("pushf", OPCODE); 19 | 20 | this.cpu = cpu; 21 | } 22 | 23 | @Override 24 | public Status execute(Status status) { 25 | 26 | // Decrement SP 27 | Register sp = cpu.getRegisterSet().getRegister("SP"); 28 | sp.setValue(sp.getValue() - 1); 29 | 30 | // Push the current flags 31 | cpu.getMemory().set(sp.getValue(), status.toByte()); 32 | 33 | return status; 34 | } 35 | 36 | public boolean noOperandsValid() { 37 | return true; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/RetInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.CPU; 4 | import net.simon987.server.assembly.Instruction; 5 | import net.simon987.server.assembly.Status; 6 | 7 | public class RetInstruction extends Instruction { 8 | 9 | /** 10 | * Opcode of the instruction 11 | */ 12 | public static final int OPCODE = 22; 13 | 14 | private CPU cpu; 15 | 16 | public RetInstruction(CPU cpu) { 17 | super("ret", OPCODE); 18 | 19 | this.cpu = cpu; 20 | } 21 | 22 | @Override 23 | public Status execute(Status status) { 24 | cpu.setIp((char) cpu.getMemory().get(cpu.getRegisterSet().get(7))); //Jmp 25 | cpu.getRegisterSet().set(7, cpu.getRegisterSet().get(7) + 1); //Inc SP 26 | 27 | return status; 28 | } 29 | 30 | @Override 31 | public Status execute(int src, Status status) { 32 | cpu.setIp((char) cpu.getMemory().get(cpu.getRegisterSet().get(7))); //Jmp 33 | cpu.getRegisterSet().set(7, cpu.getRegisterSet().get(7) + src + 1); //Inc SP 34 | 35 | return status; 36 | } 37 | 38 | @Override 39 | public boolean noOperandsValid() { 40 | return true; 41 | } 42 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SalInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * Alias of SHL instruction 5 | */ 6 | public class SalInstruction extends ShlInstruction { 7 | 8 | public SalInstruction() { 9 | super("sal"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetaInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetaInstruction extends SetccInstruction { 7 | 8 | public SetaInstruction() { 9 | super("seta"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetaeInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetaeInstruction extends SetccInstruction { 7 | 8 | public SetaeInstruction() { 9 | super("setae"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetbInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetbInstruction extends SetccInstruction { 7 | 8 | public SetbInstruction() { 9 | super("setb"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetbeInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetbeInstruction extends SetccInstruction { 7 | 8 | public SetbeInstruction() { 9 | super("setbe"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetcInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetcInstruction extends SetccInstruction { 7 | 8 | public SetcInstruction() { 9 | super("setc"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SeteInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SeteInstruction extends SetccInstruction { 7 | 8 | public SeteInstruction() { 9 | super("sete"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetgInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetgInstruction extends SetccInstruction { 7 | 8 | public SetgInstruction() { 9 | super("setg"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetgeInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetgeInstruction extends SetccInstruction { 7 | 8 | public SetgeInstruction() { 9 | super("setge"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetlInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetlInstruction extends SetccInstruction { 7 | 8 | public SetlInstruction() { 9 | super("setl"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetleInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetleInstruction extends SetccInstruction { 7 | 8 | public SetleInstruction() { 9 | super("setle"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetnaInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetnaInstruction extends SetccInstruction { 7 | 8 | public SetnaInstruction() { 9 | super("setna"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetnaeInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetnaeInstruction extends SetccInstruction { 7 | 8 | public SetnaeInstruction() { 9 | super("setnae"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetnbInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetnbInstruction extends SetccInstruction { 7 | 8 | public SetnbInstruction() { 9 | super("setnb"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetnbeInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetnbeInstruction extends SetccInstruction { 7 | 8 | public SetnbeInstruction() { 9 | super("setnbe"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetncInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetncInstruction extends SetccInstruction { 7 | 8 | public SetncInstruction() { 9 | super("setnc"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetneInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetneInstruction extends SetccInstruction { 7 | 8 | public SetneInstruction() { 9 | super("setne"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetngInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetngInstruction extends SetccInstruction { 7 | 8 | public SetngInstruction() { 9 | super("setng"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetngeInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetngeInstruction extends SetccInstruction { 7 | 8 | public SetngeInstruction() { 9 | super("setnge"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetnlInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetnlInstruction extends SetccInstruction { 7 | 8 | public SetnlInstruction() { 9 | super("setnl"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetnleInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetnleInstruction extends SetccInstruction { 7 | 8 | public SetnleInstruction() { 9 | super("setnle"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetnoInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetnoInstruction extends SetccInstruction { 7 | 8 | public SetnoInstruction() { 9 | super("setno"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetnsInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetnsInstruction extends SetccInstruction { 7 | 8 | public SetnsInstruction() { 9 | super("setns"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetnzInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetnzInstruction extends SetccInstruction { 7 | 8 | public SetnzInstruction() { 9 | super("setnz"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetoInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetoInstruction extends SetccInstruction { 7 | 8 | public SetoInstruction() { 9 | super("seto"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetsInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetsInstruction extends SetccInstruction { 7 | 8 | public SetsInstruction() { 9 | super("sets"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SetzInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | /** 4 | * alias of SetccInstruction 5 | */ 6 | public class SetzInstruction extends SetccInstruction { 7 | 8 | public SetzInstruction() { 9 | super("setz"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/SubInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.Instruction; 4 | import net.simon987.server.assembly.Status; 5 | import net.simon987.server.assembly.Target; 6 | import net.simon987.server.assembly.Util; 7 | 8 | public class SubInstruction extends Instruction { 9 | 10 | public static final int OPCODE = 3; 11 | 12 | public SubInstruction() { 13 | super("sub", OPCODE); 14 | } 15 | 16 | @Override 17 | public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) { 18 | 19 | int a = (char) dst.get(dstIndex); 20 | int b = (char) src.get(srcIndex); 21 | 22 | int result = a - b; 23 | 24 | status.setSignFlag(Util.checkSign16(result)); 25 | status.setZeroFlag((char) result == 0); 26 | status.setOverflowFlag(Util.checkOverFlowSub16(a, b)); 27 | status.setCarryFlag(Util.checkCarry16(result)); 28 | 29 | dst.set(dstIndex, result); 30 | 31 | return status; 32 | } 33 | 34 | @Override 35 | public Status execute(Target dst, int dstIndex, int src, Status status) { 36 | 37 | int a = (char) dst.get(dstIndex); 38 | int b = (char) src; 39 | 40 | int result = a - b; 41 | 42 | status.setSignFlag(Util.checkSign16(result)); 43 | status.setZeroFlag((char) result == 0); 44 | status.setOverflowFlag(Util.checkOverFlowSub16(a, b)); 45 | status.setCarryFlag(Util.checkCarry16(result)); 46 | 47 | dst.set(dstIndex, result); 48 | 49 | return status; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/TestInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.Instruction; 4 | import net.simon987.server.assembly.Status; 5 | import net.simon987.server.assembly.Target; 6 | import net.simon987.server.assembly.Util; 7 | 8 | public class TestInstruction extends Instruction { 9 | 10 | public static final int OPCODE = 11; 11 | 12 | public TestInstruction() { 13 | super("test", OPCODE); 14 | } 15 | 16 | @Override 17 | public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) { 18 | 19 | int a = (char) dst.get(dstIndex); 20 | int b = (char) src.get(srcIndex); 21 | 22 | int result = (a & b); 23 | 24 | status.setSignFlag(Util.checkSign16(result)); 25 | status.setZeroFlag((char) result == 0); 26 | status.setOverflowFlag(false); 27 | status.setCarryFlag(false); 28 | 29 | return status; 30 | } 31 | 32 | @Override 33 | public Status execute(Target dst, int dstIndex, int src, Status status) { 34 | int a = (char) dst.get(dstIndex); 35 | int b = (char) src; 36 | 37 | 38 | int result = (a & b); 39 | 40 | status.setSignFlag(Util.checkSign16(result)); 41 | status.setZeroFlag((char) result == 0); 42 | status.setOverflowFlag(false); 43 | status.setCarryFlag(false); 44 | 45 | return status; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/XchgInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.CPU; 4 | import net.simon987.server.assembly.Instruction; 5 | import net.simon987.server.assembly.Status; 6 | import net.simon987.server.assembly.Target; 7 | 8 | 9 | /** 10 | * Swap operands. Does not alter the flags 11 | */ 12 | public class XchgInstruction extends Instruction { 13 | 14 | public static final int OPCODE = 31; 15 | 16 | private CPU cpu; 17 | 18 | public XchgInstruction(CPU cpu) { 19 | super("xchg", OPCODE); 20 | 21 | this.cpu = cpu; 22 | } 23 | 24 | @Override 25 | public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) { 26 | 27 | int tmp = dst.get(dstIndex); 28 | dst.set(dstIndex, src.get(srcIndex)); 29 | src.set(srcIndex, tmp); 30 | 31 | return status; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/assembly/instruction/XorInstruction.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.assembly.instruction; 2 | 3 | import net.simon987.server.assembly.Instruction; 4 | import net.simon987.server.assembly.Status; 5 | import net.simon987.server.assembly.Target; 6 | import net.simon987.server.assembly.Util; 7 | 8 | 9 | public class XorInstruction extends Instruction { 10 | 11 | /** 12 | * Opcode of the instruction 13 | */ 14 | public static final int OPCODE = 38; 15 | 16 | public XorInstruction() { 17 | super("xor", OPCODE); 18 | } 19 | 20 | @Override 21 | public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) { 22 | 23 | int a = (char) dst.get(dstIndex); 24 | int b = (char) src.get(srcIndex); 25 | 26 | 27 | int result = (a ^ b); 28 | 29 | status.setSignFlag(Util.checkSign16(result)); 30 | status.setZeroFlag((char) result == 0); 31 | status.setOverflowFlag(false); 32 | status.setCarryFlag(false); 33 | 34 | dst.set(dstIndex, result); 35 | 36 | return status; 37 | } 38 | 39 | @Override 40 | public Status execute(Target dst, int dstIndex, int src, Status status) { 41 | int a = (char) dst.get(dstIndex); 42 | int b = (char) src; 43 | 44 | 45 | int result = (a ^ b); 46 | 47 | status.setSignFlag(Util.checkSign16(result)); 48 | status.setZeroFlag((char) result == 0); 49 | status.setOverflowFlag(false); 50 | status.setCarryFlag(false); 51 | 52 | dst.set(dstIndex, result); 53 | 54 | return status; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/AutokeyCypher.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | public class AutokeyCypher extends ShiftSubstitutionCypher { 4 | 5 | public AutokeyCypher(String charset){ 6 | super(charset); 7 | } 8 | 9 | public AutokeyCypher(){ 10 | super(); 11 | } 12 | 13 | @Override 14 | protected char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partialCypherText) { 15 | // if (i < key.length){ 16 | // return key[i]; 17 | // } else { 18 | // return plaintext[i - key.length]; 19 | // } 20 | return 0; 21 | } 22 | 23 | @Override 24 | protected char decryptionShiftAt(int position, char[] cypherText, char[] key, char[] partialPlainText) { 25 | // if (i < key.length){ 26 | // return key[i]; 27 | // } else { 28 | // return partialPlainText[i - key.length]; 29 | // } 30 | return 0; 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/CaesarCypher.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | public class CaesarCypher extends ShiftSubstitutionCypher { 4 | 5 | public CaesarCypher(String charset){ 6 | super(charset); 7 | } 8 | 9 | public CaesarCypher(){ 10 | super(); 11 | } 12 | 13 | /** 14 | * Uses the first character of the key as the shift, and ignores the rest. 15 | */ 16 | @Override 17 | protected char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partialCypherText) { 18 | return key[0]; 19 | } 20 | 21 | /** 22 | * Uses the first character of the key as the shift, and ignores the rest. 23 | */ 24 | @Override 25 | protected char decryptionShiftAt(int position, char[] cypherText, char[] key, char[] partialPlainText) { 26 | return key[0]; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/CryptoException.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | 4 | public class CryptoException extends Exception { 5 | 6 | public CryptoException () { 7 | 8 | } 9 | 10 | public CryptoException (String message) { 11 | super (message); 12 | } 13 | 14 | public CryptoException (Throwable cause) { 15 | super (cause); 16 | } 17 | 18 | public CryptoException (String message, Throwable cause) { 19 | super (message, cause); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/CryptoProvider.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | public class CryptoProvider{ 4 | 5 | public static final int NO_CYPHER = 0x0000; 6 | public static final int CAESAR_CYPHER = 0x0001; 7 | public static final int VIGENERE_CYPHER = 0x0002; 8 | public static final int AUTOKEY_CYPHER = 0x0003; 9 | 10 | public static final int PASSWORD_LENGTH = 8; //Same as CubotComPort.MESSAGE_LENGTH 11 | 12 | private String charset; 13 | private RandomStringGenerator passwordGenerator; 14 | 15 | public CryptoProvider(String charset){ 16 | this.charset = charset; 17 | this.passwordGenerator = new RandomStringGenerator(PASSWORD_LENGTH, charset); 18 | } 19 | 20 | public CryptoProvider(){ 21 | this(RandomStringGenerator.ALPHANUMERIC_CHARSET); 22 | } 23 | 24 | public Cypher getCypher(int cypherId){ 25 | switch (cypherId){ 26 | case NO_CYPHER: 27 | return new NoCypher(charset); 28 | case CAESAR_CYPHER: 29 | return new CaesarCypher(charset); 30 | case VIGENERE_CYPHER: 31 | return new VigenereCypher(charset); 32 | case AUTOKEY_CYPHER: 33 | return new AutokeyCypher(charset); 34 | default: 35 | return null; 36 | } 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/Cypher.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | public interface Cypher { 4 | 5 | char[] encrypt(char[] plainText, char[] key) throws CryptoException; 6 | 7 | char[] decrypt(char[] cypherText, char[] key) throws CryptoException; 8 | 9 | String textCharset(); 10 | 11 | String keyCharset(); 12 | 13 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/InvalidCharsetException.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | 4 | public class InvalidCharsetException extends CryptoException { 5 | 6 | public InvalidCharsetException () { 7 | 8 | } 9 | 10 | public InvalidCharsetException (String message) { 11 | super (message); 12 | } 13 | 14 | public InvalidCharsetException (Throwable cause) { 15 | super (cause); 16 | } 17 | 18 | public InvalidCharsetException (String message, Throwable cause) { 19 | super (message, cause); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/InvalidKeyException.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | 4 | public class InvalidKeyException extends CryptoException { 5 | 6 | public InvalidKeyException () { 7 | 8 | } 9 | 10 | public InvalidKeyException (String message) { 11 | super (message); 12 | } 13 | 14 | public InvalidKeyException (Throwable cause) { 15 | super (cause); 16 | } 17 | 18 | public InvalidKeyException (String message, Throwable cause) { 19 | super (message, cause); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/NoCypher.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | public class NoCypher implements Cypher { 4 | 5 | private String charset; 6 | 7 | public NoCypher(String charset){ 8 | this.charset = charset; 9 | } 10 | 11 | public NoCypher() { 12 | this(RandomStringGenerator.ALPHANUMERIC_CHARSET); 13 | } 14 | 15 | public char[] encrypt(char[] plainText, char[] key) throws InvalidCharsetException { 16 | 17 | char[] cypherText = new char[plainText.length]; 18 | for (int i = 0; i < plainText.length; i++) { 19 | char p = plainText[i]; 20 | int p_ind = charset.indexOf(p); 21 | if (p_ind == -1) { 22 | throw new InvalidCharsetException("Plaintext contains invalid character: " + p); 23 | } 24 | cypherText[i] = p; 25 | } 26 | return cypherText; 27 | } 28 | 29 | 30 | public char[] decrypt(char[] cypherText, char[] key) throws InvalidCharsetException { 31 | 32 | char[] plaintext = new char[cypherText.length]; 33 | 34 | for (int i = 0; i < cypherText.length; i++) { 35 | 36 | char c = cypherText[i]; 37 | int c_ind = charset.indexOf(c); 38 | if (c_ind == -1){ 39 | throw new InvalidCharsetException("CypherText contains invalid character: " + c); 40 | } 41 | plaintext[i] = c; 42 | } 43 | return plaintext; 44 | } 45 | 46 | public String textCharset(){ 47 | return charset; 48 | } 49 | 50 | public String keyCharset(){ 51 | return charset; 52 | } 53 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/SecretKeyGenerator.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | import javax.crypto.KeyGenerator; 4 | import javax.crypto.SecretKey; 5 | import java.security.NoSuchAlgorithmException; 6 | import java.security.SecureRandom; 7 | import java.util.Base64; 8 | 9 | public class SecretKeyGenerator { 10 | 11 | private static final String KEY_GENERATION_ALGORITHM = "HmacSHA1"; 12 | private KeyGenerator keyGen; 13 | 14 | public SecretKeyGenerator() { 15 | try { 16 | keyGen = KeyGenerator.getInstance(KEY_GENERATION_ALGORITHM); 17 | } catch (NoSuchAlgorithmException e) { 18 | throw new RuntimeException("Error creating Key generator", e); 19 | } 20 | keyGen.init(new SecureRandom(SecureRandom.getSeed(32))); 21 | } 22 | 23 | public String generate() { 24 | SecretKey secretKey = keyGen.generateKey(); 25 | return Base64.getEncoder().encodeToString(secretKey.getEncoded()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/crypto/VigenereCypher.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.crypto; 2 | 3 | public class VigenereCypher extends ShiftSubstitutionCypher { 4 | 5 | public VigenereCypher(String charset){ 6 | super(charset); 7 | } 8 | 9 | public VigenereCypher(){ 10 | super(); 11 | } 12 | 13 | @Override 14 | protected char encryptionShiftAt(int position, char[] plaintext, char[] key, char[] partialCypherText) { 15 | // int j = i % key.length; 16 | // return key[j]; 17 | 18 | return 0; 19 | } 20 | 21 | @Override 22 | protected char decryptionShiftAt(int position, char[] cypherText, char[] key, char[] partialPlainText) { 23 | // int j = i % key.length; 24 | // return key[j]; 25 | 26 | return 0; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/event/CpuInitialisationEvent.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.event; 2 | 3 | import net.simon987.server.assembly.CPU; 4 | import net.simon987.server.game.objects.ControllableUnit; 5 | 6 | public class CpuInitialisationEvent extends GameEvent { 7 | 8 | private ControllableUnit unit; 9 | 10 | public CpuInitialisationEvent(CPU cpu, ControllableUnit unit) { 11 | setSource(cpu); 12 | this.unit = unit; 13 | } 14 | 15 | public ControllableUnit getUnit() { 16 | return unit; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/event/DebugCommandEvent.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.event; 2 | 3 | import net.simon987.server.websocket.OnlineUser; 4 | import org.bson.types.ObjectId; 5 | import org.json.simple.JSONObject; 6 | 7 | import java.io.IOException; 8 | 9 | public class DebugCommandEvent extends GameEvent { 10 | 11 | private JSONObject command; 12 | 13 | public DebugCommandEvent(JSONObject json, OnlineUser user) { 14 | this.command = json; 15 | this.setSource(user); 16 | } 17 | 18 | public String getName() { 19 | return (String) command.get("command"); 20 | } 21 | 22 | public String getString(String key) { 23 | return (String) command.get(key); 24 | } 25 | 26 | public int getInt(String key) { 27 | return (int) (long) command.get(key); 28 | } 29 | 30 | public long getLong(String key) { 31 | return (long) command.get(key); 32 | } 33 | 34 | public ObjectId getObjectId(String key) { 35 | return new ObjectId((String) command.get(key)); 36 | } 37 | 38 | /** 39 | * Send back a response to the command issuer 40 | */ 41 | public void reply(String message) { 42 | 43 | JSONObject response = new JSONObject(); 44 | response.put("t", "debug"); 45 | response.put("message", message); 46 | 47 | try { 48 | ((OnlineUser) getSource()).getWebSocket().getRemote().sendString(response.toJSONString()); 49 | } catch (IOException e) { 50 | e.printStackTrace(); 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/event/GameEvent.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.event; 2 | 3 | 4 | public class GameEvent { 5 | 6 | /** 7 | * If the event is cancelled the action won't be performed 8 | */ 9 | private boolean cancelled = false; 10 | 11 | /** 12 | * The game object that triggered the event 13 | */ 14 | private Object source; 15 | 16 | 17 | //---------------------------- 18 | public boolean isCancelled() { 19 | return cancelled; 20 | } 21 | 22 | public void setCancelled(boolean cancelled) { 23 | this.cancelled = cancelled; 24 | } 25 | 26 | public Object getSource() { 27 | return source; 28 | } 29 | 30 | public void setSource(Object source) { 31 | this.source = source; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/event/GameEventDispatcher.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.event; 2 | 3 | import net.simon987.server.plugin.PluginManager; 4 | import net.simon987.server.plugin.ServerPlugin; 5 | 6 | import java.util.ArrayList; 7 | 8 | 9 | public class GameEventDispatcher { 10 | 11 | private PluginManager pluginManager; 12 | 13 | private ArrayList6 | * Inspired by http://www.cokeandcode.com/main/tutorials/path-finding/ 7 | */ 8 | public class Node implements Comparable { 9 | 10 | /** 11 | * x coordinate of the node 12 | */ 13 | public int x; 14 | 15 | /** 16 | * y coordinate of the node 17 | */ 18 | public int y; 19 | 20 | /** 21 | * Cost of getting from the start node to this node 22 | */ 23 | public int gScore; 24 | 25 | /** 26 | * Total cost of getting from the start node to the goal 27 | */ 28 | public int fScore; 29 | 30 | /** 31 | * Parent of the node 32 | */ 33 | public Node parent; 34 | 35 | 36 | /** 37 | * Create a new Node 38 | * 39 | * @param x X coordinate of the node 40 | * @param y Y coordinate of the node 41 | */ 42 | public Node(int x, int y) { 43 | this.x = x; 44 | this.y = y; 45 | 46 | gScore = Integer.MAX_VALUE; 47 | fScore = Integer.MAX_VALUE; 48 | } 49 | 50 | /** 51 | * Compare two Nodes using their fScore 52 | */ 53 | @Override 54 | public int compareTo(Object o) { 55 | Node other = (Node) o; 56 | 57 | return Integer.compare(fScore, other.fScore); 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "(" + this.x + ", " + this.y + ")"; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Server/src/main/java/net/simon987/server/game/pathfinding/SortedArrayList.java: -------------------------------------------------------------------------------- 1 | package net.simon987.server.game.pathfinding; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | /** 7 | * Wrapper of ArrayList to make it sorted 8 | *
9 | * inspired by http://www.cokeandcode.com/main/tutorials/path-finding/
10 | */
11 | public class SortedArrayList extends ArrayListLeaderboard
15 |
16 |
36 |
17 |
23 |
24 |
25 | #foreach($row in $stats)
26 | Player
18 | Completed vaults
19 | Death count
20 | Total execution time (ms)
21 | Walk distance
22 |
27 |
33 | #end
34 |
35 | $row.getKey().getUsername()
28 | $row.getValue().get("completedVaults")
29 | $row.getValue().get("death")
30 | $row.getValue().get("executionTime")
31 | $row.getValue().get("walkDistance")
32 |