| 1 | unit Instructions;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Generics.Collections, Cpu;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TParamType = (ptNone, ptNumber, ptIndirect, ptIndirect2);
|
|---|
| 10 | TParamTypeArray = array of TParamType;
|
|---|
| 11 |
|
|---|
| 12 | TInstructionInfo = class
|
|---|
| 13 | Instruction: TInstruction;
|
|---|
| 14 | Name: string;
|
|---|
| 15 | Params: TParamTypeArray;
|
|---|
| 16 | Description: string;
|
|---|
| 17 | end;
|
|---|
| 18 |
|
|---|
| 19 | TInstructionInfos = TObjectList<TInstructionInfo>;
|
|---|
| 20 |
|
|---|
| 21 | { TInstructionSet }
|
|---|
| 22 |
|
|---|
| 23 | TInstructionSet = class
|
|---|
| 24 | Items: TInstructionInfos;
|
|---|
| 25 | function SearchName(Name: string): TInstructionInfo;
|
|---|
| 26 | function SearchInstruction(Instruction: TInstruction): TInstructionInfo;
|
|---|
| 27 | function AddNew(Instruction: TInstruction; Name: string;
|
|---|
| 28 | Params: TParamTypeArray; Description: string): TInstructionInfo;
|
|---|
| 29 | constructor Create;
|
|---|
| 30 | destructor Destroy; override;
|
|---|
| 31 | end;
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | implementation
|
|---|
| 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 := TInstructionInfos.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(inLoadConst, 'LDC', [ptIndirect, ptNumber], 'Loads constant into address location.');
|
|---|
| 76 | AddNew(inLoadIndex, 'LDI', [ptIndirect, ptIndirect, ptNumber], 'Loads value from one memory location with added relative index to another.');
|
|---|
| 77 | AddNew(inLoad, 'LD', [ptIndirect, ptIndirect], 'Loads value from one memory location to another.');
|
|---|
| 78 | AddNew(inLoadMem, 'LDM', [ptIndirect, ptIndirect2], 'Loads value from one indirect memory location to another.');
|
|---|
| 79 | AddNew(inStoreMem, 'STM', [ptIndirect2, ptIndirect], 'Stotes value to indirect memory location from source location.');
|
|---|
| 80 | AddNew(inIncrement, 'INC', [ptIndirect], 'Increments value in specified register.');
|
|---|
| 81 | AddNew(inDecrement, 'DEC', [ptIndirect], 'Decrements value in specified register.');
|
|---|
| 82 | AddNew(inInput, 'IN', [ptIndirect, ptIndirect], 'Reads value from input port to register.');
|
|---|
| 83 | AddNew(inOutput, 'OUT', [ptIndirect, ptIndirect], 'Writes value from register to output port.');
|
|---|
| 84 | AddNew(inJump, 'JP', [ptNumber], 'Unconditional absolute jump to defined address.');
|
|---|
| 85 | AddNew(inJumpZero, 'JPZ', [ptIndirect, ptNumber], 'Jumps to absolute address if value of register is zero');
|
|---|
| 86 | AddNew(inJumpNotZero, 'JPNZ', [ptIndirect, ptNumber], 'Jumps to absolute address if value of register is not zero');
|
|---|
| 87 | AddNew(inJumpRel, 'JR', [ptNumber], 'Unconditional relative jump to defined address.');
|
|---|
| 88 | AddNew(inJumpRelZero, 'JRZ', [ptIndirect, ptNumber], 'Jumps to relative address if value of register is zero');
|
|---|
| 89 | AddNew(inJumpRelNotZero, 'JRNZ', [ptIndirect, ptNumber], 'Jumps to relative address if value of register is not zero');
|
|---|
| 90 | AddNew(inPush, 'PUSH', [ptIndirect], 'Push memory location onto stack.');
|
|---|
| 91 | AddNew(inPop, 'POP', [ptIndirect], 'Pop item from stack and store it into memory location.');
|
|---|
| 92 | AddNew(inCall, 'CALL', [ptNumber], 'Call subroutine.');
|
|---|
| 93 | AddNew(inReturn, 'RET', [], 'Return from subrotine.');
|
|---|
| 94 | AddNew(inAdd, 'ADD', [ptIndirect, ptIndirect, ptIndirect], 'Addition of two numbers.');
|
|---|
| 95 | AddNew(inSubtract, 'Sub', [ptIndirect, ptIndirect, ptIndirect], 'Subtraction of two numbers.');
|
|---|
| 96 | AddNew(inAnd, 'AND', [ptIndirect, ptIndirect, ptIndirect], 'Logical AND operation.');
|
|---|
| 97 | AddNew(inOr, 'OR', [ptIndirect, ptIndirect, ptIndirect], 'Logical OR operation.');
|
|---|
| 98 | AddNew(inXor, 'XOR', [ptIndirect, ptIndirect, ptIndirect], 'Logical XOR operation.');
|
|---|
| 99 | AddNew(inShiftLeft, 'SHL', [ptIndirect, ptIndirect, ptIndirect], 'Bitwise shift to the left.');
|
|---|
| 100 | AddNew(inShiftRight, 'SHR', [ptIndirect, ptIndirect, ptIndirect], 'Bitwise shift to the right.');
|
|---|
| 101 | AddNew(inBitSet, 'SET', [ptIndirect, ptIndirect, ptIndirect], 'Set specific bit.');
|
|---|
| 102 | AddNew(inBitReset, 'RES', [ptIndirect, ptIndirect, ptIndirect], 'Reset specific bit.');
|
|---|
| 103 | AddNew(inBitTest, 'BIT', [ptIndirect, ptIndirect, ptIndirect], 'Test of specific bit.');
|
|---|
| 104 | end;
|
|---|
| 105 |
|
|---|
| 106 | destructor TInstructionSet.Destroy;
|
|---|
| 107 | begin
|
|---|
| 108 | FreeAndNil(Items);
|
|---|
| 109 | inherited;
|
|---|
| 110 | end;
|
|---|
| 111 |
|
|---|
| 112 | end.
|
|---|
| 113 |
|
|---|