| 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 | Param4: TOpcodeParam;
|
|---|
| 19 | end;
|
|---|
| 20 |
|
|---|
| 21 | { TOpcodeDefs }
|
|---|
| 22 |
|
|---|
| 23 | TOpcodeDefs = class(TFPGObjectList<TOpcodeDef>)
|
|---|
| 24 | procedure CheckMissingOpcodes;
|
|---|
| 25 | function SearchByOpcode(Opcode: TOpcode): TOpcodeDef;
|
|---|
| 26 | function SearchByName(Name: string): TOpcodeDef;
|
|---|
| 27 | function AddNew(Opcode: TOpcode; Name: string;
|
|---|
| 28 | Param1: TOpcodeParam = prNone;
|
|---|
| 29 | Param2: TOpcodeParam = prNone;
|
|---|
| 30 | Param3: TOpcodeParam = prNone;
|
|---|
| 31 | Param4: TOpcodeParam = prNone): TOpcodeDef;
|
|---|
| 32 | constructor Create(FreeObjects: Boolean = True);
|
|---|
| 33 | end;
|
|---|
| 34 |
|
|---|
| 35 | function IntToHexEx(Value: Int64; Digits: ShortInt = -1; Prefix: string = ''): string; overload;
|
|---|
| 36 | function IntToHexEx(Value: QWord; Digits: ShortInt = -1; Prefix: string = ''): string; overload;
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | implementation
|
|---|
| 40 |
|
|---|
| 41 | const
|
|---|
| 42 | HexChars: array[0..15] of Char = '0123456789ABCDEF';
|
|---|
| 43 |
|
|---|
| 44 | function IntToHexEx(Value: Int64; Digits: ShortInt = -1; Prefix: string = ''): string;
|
|---|
| 45 | var
|
|---|
| 46 | I: Integer;
|
|---|
| 47 | Negative: Boolean;
|
|---|
| 48 | begin
|
|---|
| 49 | Negative := Value < 0;
|
|---|
| 50 | if Negative then Value := -Value;
|
|---|
| 51 | Result := '';
|
|---|
| 52 | if Digits >= 0 then begin
|
|---|
| 53 | for I := 0 to Digits - 1 do begin
|
|---|
| 54 | Result := HexChars[Value and $f] + Result;
|
|---|
| 55 | Value := Value shr 4;
|
|---|
| 56 | end;
|
|---|
| 57 | end else begin
|
|---|
| 58 | if Value <> 0 then begin
|
|---|
| 59 | while QWord(Value) > 0 do begin
|
|---|
| 60 | Result := HexChars[Value and $f] + Result;
|
|---|
| 61 | Value := Value shr 4;
|
|---|
| 62 | end;
|
|---|
| 63 | end else Result := '0';
|
|---|
| 64 | end;
|
|---|
| 65 | Result := Prefix + Result;
|
|---|
| 66 | if Negative then Result := '-' + Result;
|
|---|
| 67 | end;
|
|---|
| 68 |
|
|---|
| 69 | function IntToHexEx(Value: QWord; Digits: ShortInt = -1; Prefix: string = ''): string;
|
|---|
| 70 | var
|
|---|
| 71 | I: Integer;
|
|---|
| 72 | begin
|
|---|
| 73 | Result := '';
|
|---|
| 74 | if Digits >= 0 then begin
|
|---|
| 75 | for I := 0 to Digits - 1 do begin
|
|---|
| 76 | Result := HexChars[Value and $f] + Result;
|
|---|
| 77 | Value := Value shr 4;
|
|---|
| 78 | end;
|
|---|
| 79 | end else begin
|
|---|
| 80 | if Value <> 0 then begin
|
|---|
| 81 | while Value > 0 do begin
|
|---|
| 82 | Result := HexChars[Value and $f] + Result;
|
|---|
| 83 | Value := Value shr 4;
|
|---|
| 84 | end;
|
|---|
| 85 | end else Result := '0';
|
|---|
| 86 | end;
|
|---|
| 87 | Result := Prefix + Result;
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | { TOpcodeDefs }
|
|---|
| 91 |
|
|---|
| 92 | procedure TOpcodeDefs.CheckMissingOpcodes;
|
|---|
| 93 | var
|
|---|
| 94 | Opcode: TOpcode;
|
|---|
| 95 | OpcodeDef: TOpcodeDef;
|
|---|
| 96 | begin
|
|---|
| 97 | for Opcode := Low(TOpcode) to High(TOpcode) do begin
|
|---|
| 98 | OpcodeDef := SearchByOpcode(Opcode);
|
|---|
| 99 | if not Assigned(OpcodeDef) then
|
|---|
| 100 | raise Exception.Create('Opcode ' + IntToStr(Integer(Opcode)) + ' is not defined.');
|
|---|
| 101 | end;
|
|---|
| 102 | end;
|
|---|
| 103 |
|
|---|
| 104 | function TOpcodeDefs.SearchByOpcode(Opcode: TOpcode): TOpcodeDef;
|
|---|
| 105 | var
|
|---|
| 106 | I: Integer;
|
|---|
| 107 | begin
|
|---|
| 108 | I := 0;
|
|---|
| 109 | while (I < Count) and (Items[I].Opcode <> Opcode) do Inc(I);
|
|---|
| 110 | if I < Count then Result := Items[I]
|
|---|
| 111 | else Result := nil;
|
|---|
| 112 | end;
|
|---|
| 113 |
|
|---|
| 114 | function TOpcodeDefs.SearchByName(Name: string): TOpcodeDef;
|
|---|
| 115 | var
|
|---|
| 116 | I: Integer;
|
|---|
| 117 | begin
|
|---|
| 118 | I := 0;
|
|---|
| 119 | while (I < Count) and (Items[I].Name <> Name) do Inc(I);
|
|---|
| 120 | if I < Count then Result := Items[I]
|
|---|
| 121 | else Result := nil;
|
|---|
| 122 | end;
|
|---|
| 123 |
|
|---|
| 124 | function TOpcodeDefs.AddNew(Opcode: TOpcode; Name: string;
|
|---|
| 125 | Param1: TOpcodeParam = prNone;
|
|---|
| 126 | Param2: TOpcodeParam = prNone;
|
|---|
| 127 | Param3: TOpcodeParam = prNone;
|
|---|
| 128 | Param4: TOpcodeParam = prNone): TOpcodeDef;
|
|---|
| 129 | begin
|
|---|
| 130 | Result := TOpcodeDef.Create;
|
|---|
| 131 | Result.Opcode := Opcode;
|
|---|
| 132 | Result.Name := Name;
|
|---|
| 133 | Result.Param1 := Param1;
|
|---|
| 134 | Result.Param2 := Param2;
|
|---|
| 135 | Result.Param3 := Param3;
|
|---|
| 136 | Result.Param4 := Param4;
|
|---|
| 137 | Add(Result);
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | constructor TOpcodeDefs.Create(FreeObjects: Boolean = True);
|
|---|
| 141 | begin
|
|---|
| 142 | inherited;
|
|---|
| 143 | { TOpcode = (opNop, opLoad, opLoadConst, opNeg,
|
|---|
| 144 | opJump, opJumpRel,
|
|---|
| 145 | opInc, opDec,
|
|---|
| 146 | opLoadMem, opStoreMem,
|
|---|
| 147 | opAdd, opSub,
|
|---|
| 148 | opInput, opOutput,
|
|---|
| 149 | opCall, opCallRel, opRet,
|
|---|
| 150 | opExchg,
|
|---|
| 151 | opAnd, opOr, opXor,
|
|---|
| 152 | opShl, opShr,
|
|---|
| 153 | opRor, opRol,
|
|---|
| 154 | opPush, opPop,
|
|---|
| 155 | opJumpRelCond,
|
|---|
| 156 | opLdir, opLddr,
|
|---|
| 157 | opJumpCond, opTestEqual, opTestNotEqual, opTestLess,
|
|---|
| 158 | opTestLessEqual, opTestGreater, opTestGreaterEqual,
|
|---|
| 159 | opMul, opDiv, opHalt
|
|---|
| 160 | );
|
|---|
| 161 | }
|
|---|
| 162 | AddNew(opNop, 'NOP');
|
|---|
| 163 | AddNew(opHalt, 'HALT');
|
|---|
| 164 | AddNew(opLoad, 'LD', prReg, prReg);
|
|---|
| 165 | AddNew(opLoadConst, 'LDI', prReg, prData);
|
|---|
| 166 | AddNew(opJump, 'JP', prAddr);
|
|---|
| 167 | AddNew(opJumpCond, 'JPC', prAddr);
|
|---|
| 168 | AddNew(opJumpRel, 'JR', prAddrRel);
|
|---|
| 169 | AddNew(opJumpRelCond, 'JRC', prAddrRel);
|
|---|
| 170 | AddNew(opTestEqual, 'TE', prReg, prReg);
|
|---|
| 171 | AddNew(opTestNotEqual, 'TNE', prReg, prReg);
|
|---|
| 172 | AddNew(opTestGreater, 'TG', prReg, prReg);
|
|---|
| 173 | AddNew(opTestGreaterEqual, 'TGE', prReg, prReg);
|
|---|
| 174 | AddNew(opTestLess, 'TL', prReg, prReg);
|
|---|
| 175 | AddNew(opTestLessEqual, 'TLE', prReg, prReg);
|
|---|
| 176 | AddNew(opTestZero, 'TZ', prReg);
|
|---|
| 177 | AddNew(opTestNotZero, 'TNZ', prReg);
|
|---|
| 178 | AddNew(opNeg, 'NEG', prReg);
|
|---|
| 179 | AddNew(opClear, 'CLR', prReg);
|
|---|
| 180 | AddNew(opLoadMem, 'LDM', prReg, prReg);
|
|---|
| 181 | AddNew(opStoreMem, 'STM', prReg, prReg);
|
|---|
| 182 | AddNew(opExchg, 'EX', prReg, prReg);
|
|---|
| 183 | AddNew(opPush, 'PUSH', prReg);
|
|---|
| 184 | AddNew(opPop, 'POP', prReg);
|
|---|
| 185 | AddNew(opCall, 'CALL', prAddr);
|
|---|
| 186 | AddNew(opCallRel, 'CALR', prAddr);
|
|---|
| 187 | AddNew(opRet, 'RET');
|
|---|
| 188 | AddNew(opAdd, 'ADD', prReg, prReg);
|
|---|
| 189 | AddNew(opSub, 'SUB', prReg, prReg);
|
|---|
| 190 | AddNew(opInc, 'INC', prReg);
|
|---|
| 191 | AddNew(opDec, 'DEC', prReg);
|
|---|
| 192 | AddNew(opInput, 'IN', prReg, prReg);
|
|---|
| 193 | AddNew(opOutput, 'OUT', prReg, prReg);
|
|---|
| 194 | AddNew(opShl, 'SHL', prReg, prReg);
|
|---|
| 195 | AddNew(opShr, 'SHR', prReg, prReg);
|
|---|
| 196 | AddNew(opRol, 'ROL', prReg, prReg);
|
|---|
| 197 | AddNew(opRor, 'ROR', prReg, prReg);
|
|---|
| 198 | AddNew(opTestEqual, 'TESTEQ', prReg, prReg);
|
|---|
| 199 | AddNew(opAnd, 'AND', prReg, prReg);
|
|---|
| 200 | AddNew(opOr, 'OR', prReg, prReg);
|
|---|
| 201 | AddNew(opXor, 'XOR', prReg, prReg);
|
|---|
| 202 | AddNew(opLdir, 'LDIR', prReg, prReg, prReg, prReg);
|
|---|
| 203 | AddNew(opLddr, 'LDDR', prReg, prReg, prReg, prReg);
|
|---|
| 204 | AddNew(opMul, 'MUL', prReg, prReg);
|
|---|
| 205 | AddNew(opDiv, 'DIV', prReg, prReg);
|
|---|
| 206 | AddNew(opMod, 'MOD', prReg, prReg);
|
|---|
| 207 | CheckMissingOpcodes;
|
|---|
| 208 | end;
|
|---|
| 209 |
|
|---|
| 210 | end.
|
|---|
| 211 |
|
|---|