1 | unit UOpcode;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, fgl, UCpu;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TOpcodeParam = (prNone, prReg, prData, prAddr, prAddrRel);
|
---|
12 | TOpcodeDef = class
|
---|
13 | Opcode: TOpcode;
|
---|
14 | Name: string;
|
---|
15 | Param1: TOpcodeParam;
|
---|
16 | Param2: TOpcodeParam;
|
---|
17 | Param3: TOpcodeParam;
|
---|
18 | Prefix: Boolean;
|
---|
19 | end;
|
---|
20 |
|
---|
21 | { TOpcodeDefs }
|
---|
22 |
|
---|
23 | TOpcodeDefs = class(TFPGObjectList<TOpcodeDef>)
|
---|
24 | function SearchByOpcode(Opcode: TOpcode): TOpcodeDef;
|
---|
25 | function SearchByName(Name: string): TOpcodeDef;
|
---|
26 | function AddNew(Opcode: TOpcode; Name: string; Param1, Param2, Param3:
|
---|
27 | TOpcodeParam; Prefix: Boolean): TOpcodeDef;
|
---|
28 | constructor Create(FreeObjects: Boolean = True);
|
---|
29 | end;
|
---|
30 |
|
---|
31 | function IntToHexEx(Value: Int64; Digits: ShortInt = -1; Prefix: string = ''): string; overload;
|
---|
32 | function IntToHexEx(Value: QWord; Digits: ShortInt = -1; Prefix: string = ''): string; overload;
|
---|
33 |
|
---|
34 |
|
---|
35 | implementation
|
---|
36 |
|
---|
37 | const
|
---|
38 | HexChars: array[0..15] of Char = '0123456789ABCDEF';
|
---|
39 |
|
---|
40 | function IntToHexEx(Value: Int64; Digits: ShortInt = -1; Prefix: string = ''): string;
|
---|
41 | var
|
---|
42 | I: Integer;
|
---|
43 | Negative: Boolean;
|
---|
44 | begin
|
---|
45 | Negative := Value < 0;
|
---|
46 | if Negative then Value := -Value;
|
---|
47 | Result := '';
|
---|
48 | if Digits >= 0 then begin
|
---|
49 | for I := 0 to Digits - 1 do begin
|
---|
50 | Result := HexChars[Value and $f] + Result;
|
---|
51 | Value := Value shr 4;
|
---|
52 | end;
|
---|
53 | end else begin
|
---|
54 | if Value <> 0 then begin
|
---|
55 | while QWord(Value) > 0 do begin
|
---|
56 | Result := HexChars[Value and $f] + Result;
|
---|
57 | Value := Value shr 4;
|
---|
58 | end;
|
---|
59 | end else Result := '0';
|
---|
60 | end;
|
---|
61 | Result := Prefix + Result;
|
---|
62 | if Negative then Result := '-' + Result;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | function IntToHexEx(Value: QWord; Digits: ShortInt = -1; Prefix: string = ''): string;
|
---|
66 | var
|
---|
67 | I: Integer;
|
---|
68 | begin
|
---|
69 | Result := '';
|
---|
70 | if Digits >= 0 then begin
|
---|
71 | for I := 0 to Digits - 1 do begin
|
---|
72 | Result := HexChars[Value and $f] + Result;
|
---|
73 | Value := Value shr 4;
|
---|
74 | end;
|
---|
75 | end else begin
|
---|
76 | if Value <> 0 then begin
|
---|
77 | while Value > 0 do begin
|
---|
78 | Result := HexChars[Value and $f] + Result;
|
---|
79 | Value := Value shr 4;
|
---|
80 | end;
|
---|
81 | end else Result := '0';
|
---|
82 | end;
|
---|
83 | Result := Prefix + Result;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | { TOpcodeDefs }
|
---|
87 |
|
---|
88 | function TOpcodeDefs.SearchByOpcode(Opcode: TOpcode): TOpcodeDef;
|
---|
89 | var
|
---|
90 | I: Integer;
|
---|
91 | begin
|
---|
92 | I := 0;
|
---|
93 | while (I < Count) and (Items[I].Opcode <> Opcode) do Inc(I);
|
---|
94 | if I < Count then Result := Items[I]
|
---|
95 | else Result := nil;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | function TOpcodeDefs.SearchByName(Name: string): TOpcodeDef;
|
---|
99 | var
|
---|
100 | I: Integer;
|
---|
101 | begin
|
---|
102 | I := 0;
|
---|
103 | while (I < Count) and (Items[I].Name <> Name) do Inc(I);
|
---|
104 | if I < Count then Result := Items[I]
|
---|
105 | else Result := nil;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | function TOpcodeDefs.AddNew(Opcode: TOpcode; Name: string; Param1, Param2,
|
---|
109 | Param3: TOpcodeParam; Prefix: Boolean): TOpcodeDef;
|
---|
110 | begin
|
---|
111 | Result := TOpcodeDef.Create;
|
---|
112 | Result.Opcode := Opcode;
|
---|
113 | Result.Name := Name;
|
---|
114 | Result.Param1 := Param1;
|
---|
115 | Result.Param2 := Param2;
|
---|
116 | Result.Param3 := Param3;
|
---|
117 | Result.Prefix := Prefix;
|
---|
118 | Add(Result);
|
---|
119 | end;
|
---|
120 |
|
---|
121 | constructor TOpcodeDefs.Create(FreeObjects: Boolean = True);
|
---|
122 | begin
|
---|
123 | inherited;
|
---|
124 | AddNew(opNop, 'NOP', prNone, prNone, prNone, False);
|
---|
125 | AddNew(opHalt, 'HALT', prNone, prNone, prNone, False);
|
---|
126 | AddNew(opLoad, 'LD', prReg, prReg, prNone, False);
|
---|
127 | AddNew(opLoadi, 'LDI', prReg, prData, prNone, False);
|
---|
128 | AddNew(opJump, 'JP', prAddr, prNone, prNone, False);
|
---|
129 | AddNew(opJumpZero, 'JPZ', prAddr, prNone, prNone, False);
|
---|
130 | AddNew(opJumpNotZero ,'JPNZ', prAddr, prNone, prNone, False);
|
---|
131 | AddNew(opJumpRel, 'JR', prAddrRel, prNone, prNone, False);
|
---|
132 | AddNew(opJumpRelZero, 'JRZ', prAddrRel, prNone, prNone, False);
|
---|
133 | AddNew(opJumpRelNotZero, 'JRNZ', prAddrRel, prNone, prNone, False);
|
---|
134 | AddNew(opNeg, 'NEG', prReg, prNone, prNone, False);
|
---|
135 | AddNew(opClear, 'CLR', prReg, prNone, prNone, False);
|
---|
136 | AddNew(opLoadMem, 'LDM', prReg, prReg, prNone, False);
|
---|
137 | AddNew(opStoreMem, 'STM', prReg, prReg, prNone, False);
|
---|
138 | AddNew(opExchg, 'EX', prReg, prReg, prNone, False);
|
---|
139 | AddNew(opPush, 'PUSH', prReg, prNone, prNone, False);
|
---|
140 | AddNew(opPop, 'POP', prReg, prNone, prNone, False);
|
---|
141 | AddNew(opCall, 'CALL', prAddr, prNone, prNone, False);
|
---|
142 | AddNew(opRet, 'RET', prNone, prNone, prNone, False);
|
---|
143 | AddNew(opAdd, 'ADD', prReg, prReg, prNone, False);
|
---|
144 | AddNew(opAddi, 'ADDI', prReg, prData, prNone, False);
|
---|
145 | AddNew(opSub, 'SUB', prReg, prReg, prNone, False);
|
---|
146 | AddNew(opSubi, 'SUBI', prReg, prData, prNone, False);
|
---|
147 | AddNew(opInc, 'INC', prReg, prNone, prNone, False);
|
---|
148 | AddNew(opDec, 'DEC', prReg, prNone, prNone, False);
|
---|
149 | AddNew(opIn, 'IN', prReg, prAddr, prNone, False);
|
---|
150 | AddNew(opOut, 'OUT', prAddr, prReg, prNone, False);
|
---|
151 | AddNew(opShl, 'SHL', prReg, prReg, prNone, False);
|
---|
152 | AddNew(opShr, 'SHR', prReg, prReg, prNone, False);
|
---|
153 | AddNew(opDataPrefix8, 'DP8', prNone, prNone, prNone, True);
|
---|
154 | AddNew(opDataPrefix16, 'DP16', prNone, prNone, prNone, True);
|
---|
155 | AddNew(opDataPrefix32, 'DP32', prNone, prNone, prNone, True);
|
---|
156 | AddNew(opDataPrefix64, 'DP64', prNone, prNone, prNone, True);
|
---|
157 | AddNew(opDataSize, 'DS', prNone, prNone, prNone, False);
|
---|
158 | AddNew(opAddrSize, 'AS', prNone, prNone, prNone, False);
|
---|
159 | AddNew(opTest, 'TEST', prReg, prNone, prNone, False);
|
---|
160 | AddNew(opAnd, 'AND', prReg, prReg, prNone, False);
|
---|
161 | AddNew(opOr, 'OR', prReg, prReg, prNone, False);
|
---|
162 | AddNew(opXor, 'XOR', prReg, prReg, prNone, False);
|
---|
163 | AddNew(opLdir, 'LDIR', prReg, prReg, prReg, False);
|
---|
164 | AddNew(opLddr, 'LDDR', prReg, prReg, prReg, False);
|
---|
165 | AddNew(opMul, 'MUL', prReg, prReg, prNone, False);
|
---|
166 | AddNew(opDiv, 'DIV', prReg, prReg, prNone, False);
|
---|
167 | AddNew(opMod, 'MOD', prReg, prReg, prNone, False);
|
---|
168 | AddNew(opAddrPrefix8, 'AP8', prNone, prNone, prNone, True);
|
---|
169 | AddNew(opAddrPrefix16, 'AP16', prNone, prNone, prNone, True);
|
---|
170 | AddNew(opAddrPrefix32, 'AP32', prNone, prNone, prNone, True);
|
---|
171 | AddNew(opAddrPrefix64, 'AP64', prNone, prNone, prNone, True);
|
---|
172 | AddNew(opConvert, 'CON', prReg, prNone, prNone, True);
|
---|
173 | end;
|
---|
174 |
|
---|
175 | end.
|
---|
176 |
|
---|