1 | unit Instructions;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections, Cpu;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TParamType = (ptNone, ptRegIndirectIndex, ptRegIndirect, ptReg,
|
---|
10 | ptDataWidth, ptAddressWidth, ptData, ptAddress);
|
---|
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 | { TInstructionInfos }
|
---|
21 |
|
---|
22 | TInstructionInfos = class(TObjectList<TInstructionInfo>)
|
---|
23 | function SearchByName(Name: string): TInstructionInfo;
|
---|
24 | function SearchByNameMultiple(Name: string): TInstructionInfos;
|
---|
25 | function SearchInstruction(Instruction: TInstruction): TInstructionInfo;
|
---|
26 | function AddNew(Instruction: TInstruction; Name: string;
|
---|
27 | Params: TParamTypeArray; Description: string): TInstructionInfo;
|
---|
28 | procedure Init;
|
---|
29 | end;
|
---|
30 |
|
---|
31 | const
|
---|
32 | ParamTypeString: array[TParamType] of string = ('None', 'Data', 'Address',
|
---|
33 | 'Register', 'Indirect register', 'Indirect indexed register',
|
---|
34 | 'Data width', 'Address width');
|
---|
35 |
|
---|
36 |
|
---|
37 | implementation
|
---|
38 |
|
---|
39 | { TInstructionInfos }
|
---|
40 |
|
---|
41 | function TInstructionInfos.SearchByName(Name: string): TInstructionInfo;
|
---|
42 | var
|
---|
43 | I: Integer;
|
---|
44 | begin
|
---|
45 | I := 0;
|
---|
46 | while (I < Count) and (Items[I].Name <> Name) do Inc(I);
|
---|
47 | if I < Count then Result := Items[I]
|
---|
48 | else Result := nil;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | function TInstructionInfos.SearchByNameMultiple(Name: string
|
---|
52 | ): TInstructionInfos;
|
---|
53 | var
|
---|
54 | I: Integer;
|
---|
55 | begin
|
---|
56 | Result := TInstructionInfos.Create(False);
|
---|
57 | for I := 0 to Count - 1 do
|
---|
58 | if Items[I].Name = Name then Result.Add(Items[I]);
|
---|
59 | end;
|
---|
60 |
|
---|
61 | function TInstructionInfos.SearchInstruction(Instruction: TInstruction
|
---|
62 | ): TInstructionInfo;
|
---|
63 | var
|
---|
64 | I: Integer;
|
---|
65 | begin
|
---|
66 | I := 0;
|
---|
67 | while (I < Count) and (Items[I].Instruction <> Instruction) do Inc(I);
|
---|
68 | if I < Count then Result := Items[I]
|
---|
69 | else Result := nil;
|
---|
70 | end;
|
---|
71 |
|
---|
72 | function TInstructionInfos.AddNew(Instruction: TInstruction; Name: string;
|
---|
73 | Params: TParamTypeArray; Description: string): TInstructionInfo;
|
---|
74 | begin
|
---|
75 | Result := TInstructionInfo.Create;
|
---|
76 | Result.Instruction := Instruction;
|
---|
77 | Result.Name := Name;
|
---|
78 | Result.Params := Params;
|
---|
79 | Result.Description := Description;
|
---|
80 | Add(Result);
|
---|
81 | end;
|
---|
82 |
|
---|
83 | procedure TInstructionInfos.Init;
|
---|
84 | begin
|
---|
85 | Clear;
|
---|
86 | AddNew(inNop, 'NOP', [], 'No operation - The instruction doesn''t do anything.');
|
---|
87 | AddNew(inHalt, 'HALT', [], 'It terminates program execution and halts processor. Processor can be waked up by interrupt.');
|
---|
88 | AddNew(inLoadConst, 'LD', [ptReg, ptData], 'Sets register to immediate constant value.');
|
---|
89 | AddNew(inLoadConstSize, 'LD', [ptDataWidth, ptReg, ptData], 'Sets register to immediate constant value.');
|
---|
90 | AddNew(inLoad, 'LD', [ptReg, ptReg], 'Copies value from one register to another.');
|
---|
91 | AddNew(inLoadSize, 'LD', [ptDataWidth, ptReg, ptReg], 'Copies value from one register to another.');
|
---|
92 | AddNew(inLoadMem, 'LD', [ptReg, ptRegIndirect], 'Loads value from memory to register.');
|
---|
93 | AddNew(inLoadMemSize, 'LD', [ptDataWidth, ptReg, ptRegIndirect], 'Loads value from memory to register.');
|
---|
94 | AddNew(inStoreMem, 'LD', [ptRegIndirect, ptReg], 'Stores value from register to memory.');
|
---|
95 | AddNew(inStoreMemSize, 'LD', [ptDataWidth, ptRegIndirect, ptReg], 'Stores value from register to memory.');
|
---|
96 | AddNew(inLoadMemIndex, 'LD', [ptReg, ptRegIndirectIndex], 'Loads value from memory with numeric index to register.');
|
---|
97 | AddNew(inLoadMemIndexSize, 'LD', [ptDataWidth, ptReg, ptRegIndirectIndex], 'Loads value from memory with numeric index to register.');
|
---|
98 | AddNew(inStoreMemIndex, 'LD', [ptRegIndirectIndex, ptReg], 'Stores value from register to memory with numeric index.');
|
---|
99 | AddNew(inStoreMemIndexSize, 'LD', [ptDataWidth, ptRegIndirectIndex, ptReg], 'Stores value from register to memory with numeric index.');
|
---|
100 | AddNew(inInc, 'INC', [ptReg], 'Increments value in specified register.');
|
---|
101 | AddNew(inIncSize, 'INC', [ptDataWidth, ptReg], 'Increments value in specified register.');
|
---|
102 | AddNew(inDec, 'DEC', [ptReg], 'Decrements value in specified register.');
|
---|
103 | AddNew(inDecSize, 'DEC', [ptDataWidth, ptReg], 'Decrements value in specified register.');
|
---|
104 | AddNew(inAdd, 'ADD', [ptReg, ptReg], 'Adds second register to first register.');
|
---|
105 | AddNew(inAddSize, 'ADD', [ptDataWidth, ptReg, ptReg], 'Adds second register to first register.');
|
---|
106 | AddNew(inSub, 'SUB', [ptReg, ptReg], 'Subtracts second register from first register.');
|
---|
107 | AddNew(inSubSize, 'SUB', [ptDataWidth, ptReg, ptReg], 'Subtracts second register from first register.');
|
---|
108 | AddNew(inInput, 'IN', [ptReg, ptRegIndirect], 'Reads value from input port to register.');
|
---|
109 | AddNew(inInputSize, 'IN', [ptDataWidth, ptReg, ptRegIndirect], 'Reads value from input port to register.');
|
---|
110 | AddNew(inOutput, 'OUT', [ptRegIndirect, ptReg], 'Writes value from register to output port.');
|
---|
111 | AddNew(inOutputSize, 'OUT', [ptDataWidth, ptRegIndirect, ptReg], 'Writes value from register to output port.');
|
---|
112 | AddNew(inJumpZero, 'JZ', [ptReg, ptAddress], 'Jumps to given address if value of register is zero');
|
---|
113 | AddNew(inJumpNotZero, 'JNZ', [ptReg, ptAddress], 'Jumps to given address if value of register is not zero');
|
---|
114 | AddNew(inPush, 'PUSH', [ptReg], 'Pushes value of register to top of the stack.');
|
---|
115 | AddNew(inPushSize, 'PUSH', [ptDataWidth, ptReg], 'Pushes value of register to top of the stack.');
|
---|
116 | AddNew(inPop, 'POP', [ptReg], 'Pops value of register from top of the stack.');
|
---|
117 | AddNew(inPopSize, 'POP', [ptDataWidth, ptReg], 'Pops value of register from top of the stack.');
|
---|
118 | AddNew(inCall, 'CALL', [ptAddress], 'Calls subroutine at given address. Stores return address to the stack.');
|
---|
119 | AddNew(inCallSize, 'CALL', [ptAddressWidth, ptAddress], 'Calls subroutine at given address. Stores return address to the stack.');
|
---|
120 | AddNew(inRet, 'RET', [], 'Return from subroutine. Reads return address from top of the stack to IP register.');
|
---|
121 | AddNew(inRetSize, 'RET', [ptAddressWidth], 'Return from subroutine. Reads return address from top of the stack to IP register.');
|
---|
122 | AddNew(inAnd, 'AND', [ptReg, ptReg], 'Bitwise AND operation. Result is stored in first parameter.');
|
---|
123 | AddNew(inAndSize, 'AND', [ptDataWidth, ptReg, ptReg], 'Bitwise AND operation. Result is stored in first parameter.');
|
---|
124 | AddNew(inOr, 'OR', [ptReg, ptReg], 'Bitwise OR operation. Result is stored in first parameter.');
|
---|
125 | AddNew(inOrSize, 'OR', [ptDataWidth, ptReg, ptReg], 'Bitwise OR operation. Result is stored in first parameter.');
|
---|
126 | AddNew(inXor, 'XOR', [ptReg, ptReg], 'Bitwise XOR operation. Result is stored in first parameter.');
|
---|
127 | AddNew(inXorSize, 'XOR', [ptDataWidth, ptReg, ptReg], 'Bitwise XOR operation. Result is stored in first parameter.');
|
---|
128 | AddNew(inShl, 'SHL', [ptReg, ptReg], 'Shifts all bits to the left in first register by n bits according value of second register.');
|
---|
129 | AddNew(inShlSize, 'SHL', [ptDataWidth, ptReg, ptReg], 'Shifts all bits to the left in first register by n bits according value of second register.');
|
---|
130 | AddNew(inShr, 'SHR', [ptReg, ptReg], 'Shifts all bits to the right in first register by n bits according value of second register.');
|
---|
131 | AddNew(inShrSize, 'SHR', [ptDataWidth, ptReg, ptReg], 'Shifts all bits to the right in first register by n bits according value of second register.');
|
---|
132 | AddNew(inMul, 'MUL', [ptReg, ptReg], 'Multiplies first register with second register value.');
|
---|
133 | AddNew(inMulSize, 'MUL', [ptDataWidth, ptReg, ptReg], 'Multiplies first register with second register value.');
|
---|
134 | AddNew(inDiv, 'DIV', [ptReg, ptReg], 'Diviedes first register with second register value.');
|
---|
135 | AddNew(inDivSize, 'DIV', [ptDataWidth, ptReg, ptReg], 'Diviedes first register with second register value.');
|
---|
136 | //AddNew(inMod, 'MOD', [ptReg, ptReg], 'Returns modulo in first registr after division of values.');
|
---|
137 | AddNew(inJump, 'JP', [ptAddress], 'Unconditional absolute jump to defined address.');
|
---|
138 | AddNew(inJumpSize, 'JP', [ptAddressWidth, ptAddress], 'Unconditional absolute jump to defined address.');
|
---|
139 | AddNew(inJumpRel, 'JR', [ptAddress], 'Unconditional relative jump to defined address.');
|
---|
140 | AddNew(inJumpRelSize, 'JR', [ptAddressWidth, ptAddress], 'Unconditional relative jump to defined address.');
|
---|
141 | //AddNew(inLoadCpu, 'LDC', [], 'Loads value from system register.');
|
---|
142 | //AddNew(inStoreCpu, 'STC', [], 'Stores value to system register.');
|
---|
143 | AddNew(inEnableInterrupts, 'EI', [], 'Enables interrupts.');
|
---|
144 | AddNew(inDisableInterrupts, 'DI', [], 'Disables interrupts.');
|
---|
145 | end;
|
---|
146 |
|
---|
147 | end.
|
---|
148 |
|
---|