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(inInc, 'INC', [ptIndirect], 'Increments value in specified register.');
|
---|
81 | AddNew(inDec, '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 given address if value of register is zero');
|
---|
86 | AddNew(inJumpNotZero, 'JPNZ', [ptIndirect, ptNumber], 'Jumps to given address if value of register is not zero');
|
---|
87 | AddNew(inPush, 'PUSH', [ptIndirect], 'Push memory location onto stack.');
|
---|
88 | AddNew(inPop, 'POP', [ptIndirect], 'Pop item from stack and store it into memory location.');
|
---|
89 | AddNew(inCall, 'CALL', [ptNumber], 'Call subroutine.');
|
---|
90 | AddNew(inRet, 'RET', [], 'Return from subrotine.');
|
---|
91 | end;
|
---|
92 |
|
---|
93 | destructor TInstructionSet.Destroy;
|
---|
94 | begin
|
---|
95 | FreeAndNil(Items);
|
---|
96 | inherited;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | end.
|
---|
100 |
|
---|