1 | unit UMachine;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Math;
|
---|
9 |
|
---|
10 | type
|
---|
11 | //TAddrInt = Int8;
|
---|
12 | TAddrInt = Int16;
|
---|
13 | //TAddrInt = Int32;
|
---|
14 | //TAddrInt = Int64;
|
---|
15 | TDataInt = Int16;
|
---|
16 | //TDataInt = Int16;
|
---|
17 | //TDataInt = Int32;
|
---|
18 | //TDataInt = Int64;
|
---|
19 |
|
---|
20 | TOpcode = (opNop, opHalt, opCopy, opAdd, opSub, opMul, opDiv, opSet,
|
---|
21 | opParamClearAll, opParamSelect, opParamSet, opParamIndirect, opParamNext,
|
---|
22 | opSkip, opSign, opParamSize);
|
---|
23 |
|
---|
24 | TAreaType = (atAddress, atMemory, atIOPort, atRegister, atSysRegister);
|
---|
25 |
|
---|
26 | TSysAddr = (saInstructionPointer, saAddrWidth, saDataWidth, saRegisterCount);
|
---|
27 |
|
---|
28 | TParameter = record
|
---|
29 | Number: TAddrInt;
|
---|
30 | Area: Integer;
|
---|
31 | Size: Integer;
|
---|
32 | Defined: Boolean;
|
---|
33 | end;
|
---|
34 |
|
---|
35 | TOpcodeHandler = procedure of object;
|
---|
36 |
|
---|
37 | { TMachine }
|
---|
38 |
|
---|
39 | TMachine = class
|
---|
40 | private
|
---|
41 | OpcodeTable: array[TOpcode] of TOpcodeHandler;
|
---|
42 | SkipNextInstruction: Boolean;
|
---|
43 | CurrentParam: Integer;
|
---|
44 | procedure OpcodeNop;
|
---|
45 | procedure OpcodeHalt;
|
---|
46 | procedure OpcodeCopy;
|
---|
47 | procedure OpcodeAdd;
|
---|
48 | procedure OpcodeSub;
|
---|
49 | procedure OpcodeMul;
|
---|
50 | procedure OpcodeDiv;
|
---|
51 | procedure OpcodeParamNext;
|
---|
52 | procedure OpcodeParamSelect;
|
---|
53 | procedure OpcodeParamSet;
|
---|
54 | procedure OpcodeParamIndirect;
|
---|
55 | procedure OpcodeParamClearAll;
|
---|
56 | procedure OpcodeParamSize;
|
---|
57 | procedure OpcodeSkip;
|
---|
58 | procedure OpcodeSign;
|
---|
59 | function ReadNext: Integer;
|
---|
60 | public
|
---|
61 | Memory: array of Byte;
|
---|
62 | Registers: array of TAddrInt;
|
---|
63 | IP: TAddrInt;
|
---|
64 | Params: array of TParameter;
|
---|
65 | Terminated: Boolean;
|
---|
66 | function ReadPort(Address: Integer): TDataInt;
|
---|
67 | procedure WritePort(Address: TAddrInt; Value: TDataInt);
|
---|
68 | function ReadArea(AreaIndex: Integer; Address: TAddrInt): TDataInt;
|
---|
69 | procedure WriteArea(AreaIndex: Integer; Address: TAddrInt; Value: TDataInt);
|
---|
70 | function ReadSys(Address: TAddrInt): TDataInt;
|
---|
71 | procedure Run;
|
---|
72 | constructor Create;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | implementation
|
---|
76 |
|
---|
77 | { TMachine }
|
---|
78 |
|
---|
79 | procedure TMachine.OpcodeNop;
|
---|
80 | begin
|
---|
81 | end;
|
---|
82 |
|
---|
83 | procedure TMachine.OpcodeHalt;
|
---|
84 | begin
|
---|
85 | Terminated := True;
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TMachine.OpcodeCopy;
|
---|
89 | begin
|
---|
90 | // for I := 0 to Params[0].Size - 1 do begin
|
---|
91 | WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[1].Area, Params[1].Number));
|
---|
92 | end;
|
---|
93 |
|
---|
94 | procedure TMachine.OpcodeAdd;
|
---|
95 | begin
|
---|
96 | if Params[2].Defined then
|
---|
97 | WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[2].Area, Params[2].Number) + ReadArea(Params[1].Area, Params[1].Number))
|
---|
98 | else if Params[1].Defined then WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[0].Area, Params[0].Number) + ReadArea(Params[1].Area, Params[1].Number))
|
---|
99 | else WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[0].Area, Params[0].Number) + 1);
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TMachine.OpcodeSub;
|
---|
103 | begin
|
---|
104 | if Params[2].Defined then
|
---|
105 | WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[2].Area, Params[2].Number) - ReadArea(Params[1].Area, Params[1].Number))
|
---|
106 | else if Params[1].Defined then WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[0].Area, Params[0].Number) - ReadArea(Params[1].Area, Params[1].Number))
|
---|
107 | else WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[0].Area, Params[0].Number) - 1);
|
---|
108 | end;
|
---|
109 |
|
---|
110 | procedure TMachine.OpcodeMul;
|
---|
111 | begin
|
---|
112 | if Params[2].Defined then
|
---|
113 | WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[2].Area, Params[2].Number) * ReadArea(Params[1].Area, Params[1].Number))
|
---|
114 | else WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[0].Area, Params[0].Number) * ReadArea(Params[1].Area, Params[1].Number));
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TMachine.OpcodeDiv;
|
---|
118 | begin
|
---|
119 | if Params[2].Defined then
|
---|
120 | WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[2].Area, Params[2].Number) div ReadArea(Params[1].Area, Params[1].Number))
|
---|
121 | else WriteArea(Params[0].Area, Params[0].Number, ReadArea(Params[0].Area, Params[0].Number) div ReadArea(Params[1].Area, Params[1].Number));
|
---|
122 | end;
|
---|
123 |
|
---|
124 | procedure TMachine.OpcodeParamNext;
|
---|
125 | begin
|
---|
126 | Inc(CurrentParam);
|
---|
127 | end;
|
---|
128 |
|
---|
129 | procedure TMachine.OpcodeParamSelect;
|
---|
130 | begin
|
---|
131 | CurrentParam := ReadNext;
|
---|
132 | end;
|
---|
133 |
|
---|
134 | procedure TMachine.OpcodeParamSet;
|
---|
135 | begin
|
---|
136 | Params[CurrentParam].Number := ReadNext;
|
---|
137 | Params[CurrentParam].Area := Integer(atAddress);
|
---|
138 | Params[CurrentParam].Defined := True;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TMachine.OpcodeParamIndirect;
|
---|
142 | begin
|
---|
143 | if not Params[CurrentParam].Defined then
|
---|
144 | raise Exception.Create('Undefined parameter ' + IntToStr(CurrentParam));
|
---|
145 | if Params[CurrentParam].Area = Integer(atAddress) then
|
---|
146 | Params[CurrentParam].Area := ReadNext
|
---|
147 | else begin
|
---|
148 | Params[CurrentParam].Number := ReadArea(Params[CurrentParam].Area, Params[CurrentParam].Number);
|
---|
149 | Params[CurrentParam].Area := ReadNext;
|
---|
150 | end;
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TMachine.OpcodeParamClearAll;
|
---|
154 | var
|
---|
155 | I: Integer;
|
---|
156 | begin
|
---|
157 | for I := 0 to Length(Params) - 1 do begin
|
---|
158 | Params[I].Number := 0;
|
---|
159 | Params[I].Area := Integer(atAddress);
|
---|
160 | Params[I].Defined := False;
|
---|
161 | Params[I].Size := 1;
|
---|
162 | end;
|
---|
163 | CurrentParam := 0;
|
---|
164 | end;
|
---|
165 |
|
---|
166 | procedure TMachine.OpcodeParamSize;
|
---|
167 | begin
|
---|
168 | Params[CurrentParam].Size := ReadNext;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | procedure TMachine.OpcodeSkip;
|
---|
172 | begin
|
---|
173 | if ReadArea(Params[0].Area, Params[0].Number) < 0 then begin
|
---|
174 | SkipNextInstruction := True;
|
---|
175 | end;
|
---|
176 | end;
|
---|
177 |
|
---|
178 | procedure TMachine.OpcodeSign;
|
---|
179 | begin
|
---|
180 | if not Params[1].Defined then
|
---|
181 | WriteArea(Params[0].Area, Params[0].Number, Sign(ReadArea(Params[1].Area, Params[1].Number)))
|
---|
182 | else WriteArea(Params[0].Area, Params[0].Number, Sign(ReadArea(Params[0].Area, Params[0].Number)));
|
---|
183 | end;
|
---|
184 |
|
---|
185 | function TMachine.ReadNext: Integer;
|
---|
186 | begin
|
---|
187 | if (IP >= 0) and (IP < Length(Memory)) then Result := Memory[IP]
|
---|
188 | else raise Exception.Create('Memory access out of range ' + IntToHex(IP, 8));
|
---|
189 | Inc(IP);
|
---|
190 | end;
|
---|
191 |
|
---|
192 | function TMachine.ReadPort(Address: Integer): TDataInt;
|
---|
193 | begin
|
---|
194 | Result := 0;
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TMachine.WritePort(Address: TAddrInt; Value: TDataInt);
|
---|
198 | begin
|
---|
199 |
|
---|
200 | end;
|
---|
201 |
|
---|
202 | function TMachine.ReadArea(AreaIndex: Integer; Address: TAddrInt): TDataInt;
|
---|
203 | begin
|
---|
204 | if AreaIndex = Integer(atAddress) then Result := Address
|
---|
205 | else if AreaIndex = Integer(atMemory) then Result := Memory[Address]
|
---|
206 | else if AreaIndex = Integer(atIOPort) then Result := ReadPort(Address)
|
---|
207 | else if AreaIndex = Integer(atRegister) then Result := Registers[Address]
|
---|
208 | else if AreaIndex = Integer(atSysRegister) then Result := ReadSys(Address)
|
---|
209 | else raise Exception.Create('Unsupported area number ' + IntToStr(AreaIndex));
|
---|
210 | end;
|
---|
211 |
|
---|
212 | procedure TMachine.WriteArea(AreaIndex: Integer; Address: TAddrInt;
|
---|
213 | Value: TDataInt);
|
---|
214 | begin
|
---|
215 | if AreaIndex = Integer(atAddress) then
|
---|
216 | else if AreaIndex = Integer(atMemory) then Memory[Address] := Value
|
---|
217 | else if AreaIndex = Integer(atIOPort) then WritePort(Address, Value)
|
---|
218 | else if AreaIndex = Integer(atRegister) then Registers[Address] := Value
|
---|
219 | else if AreaIndex = Integer(atSysRegister) then begin
|
---|
220 | if Address = 0 then IP := Value
|
---|
221 | end else raise Exception.Create('Unsupported area number ' + IntToStr(AreaIndex));
|
---|
222 | end;
|
---|
223 |
|
---|
224 | function TMachine.ReadSys(Address: TAddrInt): TDataInt;
|
---|
225 | begin
|
---|
226 | if Address = Integer(saInstructionPointer) then Result := IP
|
---|
227 | else if Address = Integer(saAddrWidth) then Result := SizeOf(TAddrInt)
|
---|
228 | else if Address = Integer(saDataWidth) then Result := SizeOf(TDataInt)
|
---|
229 | else if Address = Integer(saRegisterCount) then Result := Length(Registers)
|
---|
230 | else Result := 0;
|
---|
231 | end;
|
---|
232 |
|
---|
233 | procedure TMachine.Run;
|
---|
234 | var
|
---|
235 | Opcode: TOpcode;
|
---|
236 | I: Integer;
|
---|
237 | begin
|
---|
238 | IP := 0;
|
---|
239 | Terminated := False;
|
---|
240 | CurrentParam := 0;
|
---|
241 | while not Terminated do begin
|
---|
242 | Opcode := TOpcode(ReadNext);
|
---|
243 | if SkipNextInstruction and ((Opcode <> opParamIndirect) and (Opcode <> opParamSelect) and
|
---|
244 | (Opcode <> opParamClearAll) and (Opcode <> opParamSet) and (Opcode <> opParamNext)) then begin
|
---|
245 | SkipNextInstruction := False;
|
---|
246 | end else begin
|
---|
247 | if Assigned(OpcodeTable[Opcode]) then
|
---|
248 | OpcodeTable[Opcode]
|
---|
249 | else raise Exception.Create('Unknown opcode ' + IntToStr(Integer(Opcode)));
|
---|
250 | end;
|
---|
251 | end;
|
---|
252 | end;
|
---|
253 |
|
---|
254 | constructor TMachine.Create;
|
---|
255 | begin
|
---|
256 | SetLength(Registers, 16);
|
---|
257 | SetLength(Params, 4);
|
---|
258 |
|
---|
259 | // Opcode table initialization
|
---|
260 | OpcodeTable[opNop] := OpcodeNop;
|
---|
261 | OpcodeTable[opHalt] := OpcodeHalt;
|
---|
262 | OpcodeTable[opCopy] := OpcodeCopy;
|
---|
263 | OpcodeTable[opAdd] := OpcodeAdd;
|
---|
264 | OpcodeTable[opSub] := OpcodeSub;
|
---|
265 | OpcodeTable[opMul] := OpcodeMul;
|
---|
266 | OpcodeTable[opDiv] := OpcodeDiv;
|
---|
267 | OpcodeTable[opParamSelect] := OpcodeParamSelect;
|
---|
268 | OpcodeTable[opParamSet] := OpcodeParamSet;
|
---|
269 | OpcodeTable[opParamIndirect] := OpcodeParamIndirect;
|
---|
270 | OpcodeTable[opParamClearAll] := OpcodeParamClearAll;
|
---|
271 | OpcodeTable[opSkip] := OpcodeSkip;
|
---|
272 | OpcodeTable[opSign] := OpcodeSign;
|
---|
273 | OpcodeTable[opParamNext] := OpcodeParamNext;
|
---|
274 | OpcodeTable[opParamSize] := OpcodeParamSize;
|
---|
275 | end;
|
---|
276 |
|
---|
277 | end.
|
---|
278 |
|
---|