source: branches/ByteArray/Instructions.pas

Last change on this file was 59, checked in by chronos, 6 months ago
  • Fixed: Assembler and disassembler to work correctly with supported instructions.
File size: 7.9 KB
Line 
1unit Instructions;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, Cpu;
7
8type
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
31const
32 ParamTypeString: array[TParamType] of string = ('None', 'Data', 'Address',
33 'Register', 'Indirect register', 'Indirect indexed register',
34 'Data width', 'Address width');
35
36
37implementation
38
39{ TInstructionInfos }
40
41function TInstructionInfos.SearchByName(Name: string): TInstructionInfo;
42var
43 I: Integer;
44begin
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;
49end;
50
51function TInstructionInfos.SearchByNameMultiple(Name: string
52 ): TInstructionInfos;
53var
54 I: Integer;
55begin
56 Result := TInstructionInfos.Create(False);
57 for I := 0 to Count - 1 do
58 if Items[I].Name = Name then Result.Add(Items[I]);
59end;
60
61function TInstructionInfos.SearchInstruction(Instruction: TInstruction
62 ): TInstructionInfo;
63var
64 I: Integer;
65begin
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;
70end;
71
72function TInstructionInfos.AddNew(Instruction: TInstruction; Name: string;
73 Params: TParamTypeArray; Description: string): TInstructionInfo;
74begin
75 Result := TInstructionInfo.Create;
76 Result.Instruction := Instruction;
77 Result.Name := Name;
78 Result.Params := Params;
79 Result.Description := Description;
80 Add(Result);
81end;
82
83procedure TInstructionInfos.Init;
84begin
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(inOutput, 'OUT', [ptRegIndirect, ptReg], 'Writes value from register to output port.');
110 AddNew(inJumpZero, 'JZ', [ptReg, ptAddress], 'Jumps to given address if value of register is zero');
111 AddNew(inJumpNotZero, 'JNZ', [ptReg, ptAddress], 'Jumps to given address if value of register is not zero');
112 AddNew(inPush, 'PUSH', [ptReg], 'Pushes value of register to top of the stack.');
113 AddNew(inPushSize, 'PUSH', [ptDataWidth, ptReg], 'Pushes value of register to top of the stack.');
114 AddNew(inPop, 'POP', [ptReg], 'Pops value of register from top of the stack.');
115 AddNew(inPopSize, 'POP', [ptDataWidth, ptReg], 'Pops value of register from top of the stack.');
116 AddNew(inCall, 'CALL', [ptAddress], 'Calls subroutine at given address. Stores return address to the stack.');
117 AddNew(inCallSize, 'CALL', [ptAddressWidth, ptAddress], 'Calls subroutine at given address. Stores return address to the stack.');
118 AddNew(inRet, 'RET', [], 'Return from subroutine. Reads return address from top of the stack to IP register.');
119 AddNew(inRetSize, 'RET', [ptAddressWidth], 'Return from subroutine. Reads return address from top of the stack to IP register.');
120 AddNew(inAnd, 'AND', [ptReg, ptReg], 'Bitwise AND operation. Result is stored in first parameter.');
121 AddNew(inAndSize, 'AND', [ptDataWidth, ptReg, ptReg], 'Bitwise AND operation. Result is stored in first parameter.');
122 AddNew(inOr, 'OR', [ptReg, ptReg], 'Bitwise OR operation. Result is stored in first parameter.');
123 AddNew(inOrSize, 'OR', [ptDataWidth, ptReg, ptReg], 'Bitwise OR operation. Result is stored in first parameter.');
124 AddNew(inXor, 'XOR', [ptReg, ptReg], 'Bitwise XOR operation. Result is stored in first parameter.');
125 AddNew(inXorSize, 'XOR', [ptDataWidth, ptReg, ptReg], 'Bitwise XOR operation. Result is stored in first parameter.');
126 AddNew(inShl, 'SHL', [ptReg, ptReg], 'Shifts all bits to the left in first register by n bits according value of second register.');
127 AddNew(inShlSize, 'SHL', [ptDataWidth, ptReg, ptReg], 'Shifts all bits to the left in first register by n bits according value of second register.');
128 AddNew(inShr, 'SHR', [ptReg, ptReg], 'Shifts all bits to the right in first register by n bits according value of second register.');
129 AddNew(inShrSize, 'SHR', [ptDataWidth, ptReg, ptReg], 'Shifts all bits to the right in first register by n bits according value of second register.');
130 AddNew(inMul, 'MUL', [ptReg, ptReg], 'Multiplies first register with second register value.');
131 AddNew(inMulSize, 'MUL', [ptDataWidth, ptReg, ptReg], 'Multiplies first register with second register value.');
132 AddNew(inDiv, 'DIV', [ptReg, ptReg], 'Diviedes first register with second register value.');
133 AddNew(inDivSize, 'DIV', [ptDataWidth, ptReg, ptReg], 'Diviedes first register with second register value.');
134 //AddNew(inMod, 'MOD', [ptReg, ptReg], 'Returns modulo in first registr after division of values.');
135 AddNew(inJump, 'JP', [ptAddress], 'Unconditional absolute jump to defined address.');
136 AddNew(inJumpSize, 'JP', [ptAddressWidth, ptAddress], 'Unconditional absolute jump to defined address.');
137 AddNew(inJumpRel, 'JR', [ptAddress], 'Unconditional relative jump to defined address.');
138 AddNew(inJumpRelSize, 'JR', [ptAddressWidth, ptAddress], 'Unconditional relative jump to defined address.');
139 //AddNew(inLoadCpu, 'LDC', [], 'Loads value from system register.');
140 //AddNew(inStoreCpu, 'STC', [], 'Stores value to system register.');
141 AddNew(inEnableInterrupts, 'EI', [], 'Enables interrupts.');
142 AddNew(inDisableInterrupts, 'DI', [], 'Disables interrupts.');
143end;
144
145end.
146
Note: See TracBrowser for help on using the repository browser.