| 1 | unit Instructions;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Generics.Collections, Cpu;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TParamType = (ptNone, ptNumber, ptReg, ptRegIndirect, ptRegIndirectIndex,
|
|---|
| 10 | ptRegIndirectGroup);
|
|---|
| 11 | TParamTypeArray = array of TParamType;
|
|---|
| 12 |
|
|---|
| 13 | TInstructionInfo = class
|
|---|
| 14 | Instruction: TInstruction;
|
|---|
| 15 | Name: string;
|
|---|
| 16 | Params: TParamTypeArray;
|
|---|
| 17 | Description: string;
|
|---|
| 18 | end;
|
|---|
| 19 |
|
|---|
| 20 | { TInstructionSet }
|
|---|
| 21 |
|
|---|
| 22 | TInstructionSet = class
|
|---|
| 23 | Items: TObjectList<TInstructionInfo>;
|
|---|
| 24 | function SearchName(Name: string): TInstructionInfo;
|
|---|
| 25 | function SearchInstruction(Instruction: TInstruction): TInstructionInfo;
|
|---|
| 26 | function AddNew(Instruction: TInstruction; Name: string;
|
|---|
| 27 | Params: TParamTypeArray; Description: string): TInstructionInfo;
|
|---|
| 28 | constructor Create;
|
|---|
| 29 | destructor Destroy; override;
|
|---|
| 30 | end;
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | implementation
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | { TInstructionSet }
|
|---|
| 37 |
|
|---|
| 38 | function TInstructionSet.SearchName(Name: string): TInstructionInfo;
|
|---|
| 39 | var
|
|---|
| 40 | I: Integer;
|
|---|
| 41 | begin
|
|---|
| 42 | I := 0;
|
|---|
| 43 | while (I < Items.Count) and (Items[I].Name <> Name) do Inc(I);
|
|---|
| 44 | if I < Items.Count then Result := Items[I]
|
|---|
| 45 | else Result := nil;
|
|---|
| 46 | end;
|
|---|
| 47 |
|
|---|
| 48 | function TInstructionSet.SearchInstruction(Instruction: TInstruction
|
|---|
| 49 | ): TInstructionInfo;
|
|---|
| 50 | var
|
|---|
| 51 | I: Integer;
|
|---|
| 52 | begin
|
|---|
| 53 | I := 0;
|
|---|
| 54 | while (I < Items.Count) and (Items[I].Instruction <> Instruction) do Inc(I);
|
|---|
| 55 | if I < Items.Count then Result := Items[I]
|
|---|
| 56 | else Result := nil;
|
|---|
| 57 | end;
|
|---|
| 58 |
|
|---|
| 59 | function TInstructionSet.AddNew(Instruction: TInstruction; Name: string;
|
|---|
| 60 | Params: TParamTypeArray; Description: string): TInstructionInfo;
|
|---|
| 61 | begin
|
|---|
| 62 | Result := TInstructionInfo.Create;
|
|---|
| 63 | Result.Instruction := Instruction;
|
|---|
| 64 | Result.Name := Name;
|
|---|
| 65 | Result.Params := Params;
|
|---|
| 66 | Result.Description := Description;
|
|---|
| 67 | Items.Add(Result);
|
|---|
| 68 | end;
|
|---|
| 69 |
|
|---|
| 70 | constructor TInstructionSet.Create;
|
|---|
| 71 | begin
|
|---|
| 72 | Items := TObjectList<TInstructionInfo>.Create;
|
|---|
| 73 | AddNew(inNop, 'NOP', [], 'No operation - The instruction doesn''t do anything.');
|
|---|
| 74 | AddNew(inHalt, 'HALT', [], 'It terminates program execution and halts processor. Processor can be waked up by interrupt.');
|
|---|
| 75 | AddNew(inSet, 'SET', [ptReg, ptNumber], 'Sets register to immediate constant value.');
|
|---|
| 76 | AddNew(inCopy, 'CP', [ptReg, ptReg], 'Copies value from one register to another.');
|
|---|
| 77 | AddNew(inInc, 'INC', [ptReg], 'Increments value in specified register.');
|
|---|
| 78 | AddNew(inDec, 'DEC', [ptReg], 'Decrements value in specified register.');
|
|---|
| 79 | AddNew(inLoad, 'LD', [ptReg, ptRegIndirect], 'Loads value from memory to register.');
|
|---|
| 80 | AddNew(inStore, 'ST', [ptRegIndirect, ptReg], 'Stores value from register to memory.');
|
|---|
| 81 | AddNew(inAdd, 'ADD', [ptReg, ptReg], 'Adds second register to first register.');
|
|---|
| 82 | AddNew(inSub, 'SUB', [ptReg, ptReg], 'Subtracts second register from first register.');
|
|---|
| 83 | AddNew(inIn, 'IN', [ptReg, ptRegIndirectGroup], 'Reads value from input port to register.');
|
|---|
| 84 | AddNew(inOut, 'OUT', [ptRegIndirectGroup, ptReg], 'Writes value from register to output port.');
|
|---|
| 85 | AddNew(inJumpZero, 'JZ', [ptReg, ptNumber], 'Jumps to given address if value of register is zero');
|
|---|
| 86 | AddNew(inJumpNotZero, 'JNZ', [ptReg, ptNumber], 'Jumps to given address if value of register is not zero');
|
|---|
| 87 | AddNew(inPush, 'PUSH', [ptReg], 'Pushes value of register to top of the stack.');
|
|---|
| 88 | AddNew(inPop, 'POP', [ptReg], 'Pops value of register from top of the stack.');
|
|---|
| 89 | AddNew(inCall, 'CALL', [ptNumber], 'Calls subroutine at given address. Stores return address to the stack.');
|
|---|
| 90 | AddNew(inRet, 'RET', [], 'Return from subroutine. Reads return address from top of the stack to IP register.');
|
|---|
| 91 | AddNew(inAnd, 'AND', [ptReg, ptReg], 'Bitwise AND operation. Result is stored in first parameter.');
|
|---|
| 92 | AddNew(inOr, 'OR', [ptReg, ptReg], 'Bitwise OR operation. Result is stored in first parameter.');
|
|---|
| 93 | AddNew(inXor, 'XOR', [ptReg, ptReg], 'Bitwise XOR operation. Result is stored in first parameter.');
|
|---|
| 94 | AddNew(inXor, 'SHL', [ptReg, ptReg], 'Shifts all bits to the left in first register by n bits according value of second register.');
|
|---|
| 95 | AddNew(inXor, 'SHR', [ptReg, ptReg], 'Shifts all bits to the right in first register by n bits according value of second register.');
|
|---|
| 96 | AddNew(inMul, 'MUL', [ptReg, ptReg], 'Multiplies first register with second register value.');
|
|---|
| 97 | AddNew(inDiv, 'DIV', [ptReg, ptReg], 'Diviedes first register with second register value.');
|
|---|
| 98 | AddNew(inMod, 'MOD', [ptReg, ptReg], 'Returns modulo in first registr after division of values.');
|
|---|
| 99 | AddNew(inJump, 'JP', [ptNumber], 'Unconditional absolute jump to defined address.');
|
|---|
| 100 | AddNew(inJumpRel, 'JR', [ptNumber], 'Unconditional relative jump to defined address.');
|
|---|
| 101 | AddNew(inLoadIndex, 'LDI', [ptReg, ptRegIndirectIndex], 'Loads value from memory with numeric index to register.');
|
|---|
| 102 | AddNew(inStoreIndex, 'STI', [ptRegIndirectIndex, ptReg], 'Stores value from register to memory with numeric index.');
|
|---|
| 103 | AddNew(inLoadCpu, 'LDC', [], 'Loads value from system register.');
|
|---|
| 104 | AddNew(inStoreCpu, 'STC', [], 'Stores value to system register.');
|
|---|
| 105 | AddNew(inEi, 'EI', [], 'Enables interrupts.');
|
|---|
| 106 | AddNew(inDi, 'DI', [], 'Disables interrupts.');
|
|---|
| 107 | end;
|
|---|
| 108 |
|
|---|
| 109 | destructor TInstructionSet.Destroy;
|
|---|
| 110 | begin
|
|---|
| 111 | FreeAndNil(Items);
|
|---|
| 112 | inherited;
|
|---|
| 113 | end;
|
|---|
| 114 |
|
|---|
| 115 | end.
|
|---|
| 116 |
|
|---|