1 | unit UMachine;
|
---|
2 |
|
---|
3 |
|
---|
4 | {$DEFINE EXT_IO}
|
---|
5 | {$DEFINE EXT_ADDRESSING_MODES}
|
---|
6 | {$DEFINE EXT_ARITHMETIC}
|
---|
7 | {$DEFINE EXT_LOGICAL}
|
---|
8 | {$DEFINE EXT_STACK}
|
---|
9 | {$DEFINE EXT_SUBROUTINE}
|
---|
10 | {$DEFINE EXT_SHIFT}
|
---|
11 | {$DEFINE EXT_BLOCK}
|
---|
12 | {$DEFINE EXT_GENERAL}
|
---|
13 | {$DEFINE EXT_BIT}
|
---|
14 | //{$DEFINE EXT_LOWER_WIDTH}
|
---|
15 |
|
---|
16 | // Extension dependencies
|
---|
17 | {$IFDEF EXT_SUBROUTINE}
|
---|
18 | {$DEFINE EXT_STACK}
|
---|
19 | {$ENDIF}
|
---|
20 |
|
---|
21 | {$mode objfpc}{$H+}
|
---|
22 |
|
---|
23 | interface
|
---|
24 |
|
---|
25 | uses
|
---|
26 | Classes, SysUtils;
|
---|
27 |
|
---|
28 | type
|
---|
29 | TOpcode = (opNop, opHalt, opLD, opLDC, opLDM, opSTM, opInc, opDec, opJP, opJr,
|
---|
30 | opPush, opPop, opCall, opRet, opAdd, opSub, opMul, opDiv, opShr, opShl,
|
---|
31 | opIn, opOut, opXchg, opXor, opOr, opAnd, opJpc, opJrc,
|
---|
32 | opTstZ, opTstNZ, opTstC, opTstNC, opLDMD, opSTMD, opLdir, opLddr,
|
---|
33 | opBitSet, opBitRes, opBitGet, opBitPut,
|
---|
34 | opPrefix8, opPrefix16, opPrefix32, opPrefix64, opPrefix128);
|
---|
35 |
|
---|
36 | TOpcodeHandler = procedure of object;
|
---|
37 |
|
---|
38 | TDataWidth = (dwNative, dw8, dw16, dw32, dw64, dw128);
|
---|
39 |
|
---|
40 | // Goals: Simple to implement, fast execution
|
---|
41 | // Address and data have same bit width
|
---|
42 | // Memory access with lower bit width?
|
---|
43 | // Related instructions grouped as extensions
|
---|
44 |
|
---|
45 | { TCPU }
|
---|
46 |
|
---|
47 | generic TCPU<T> = class
|
---|
48 | type
|
---|
49 | TOutputEvent = procedure (Port: T; Value: T) of object;
|
---|
50 | TInputEvent = function (Port: T): T of object;
|
---|
51 | PT = ^T;
|
---|
52 | TOpcodeIndex = Byte;
|
---|
53 | POpcodeIndex = ^TOpcodeIndex;
|
---|
54 | TRegIndex = Byte;
|
---|
55 | PRegIndex = ^TRegIndex;
|
---|
56 | var
|
---|
57 | Terminated: Boolean;
|
---|
58 | OpcodeHandlers: array[TOpcode] of TOpcodeHandler;
|
---|
59 | function ReadNext: T;
|
---|
60 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
61 | function ReadNext8: ShortInt;
|
---|
62 | function ReadNext16: SmallInt;
|
---|
63 | function ReadNext32: Integer;
|
---|
64 | function ReadNext64: Int64;
|
---|
65 | {$ENDIF}
|
---|
66 | function ReadOpcode: TOpcodeIndex;
|
---|
67 | function ReadReg: TRegIndex;
|
---|
68 | procedure Step;
|
---|
69 | procedure AddOpcode(Opcode: TOpcode; Handler: TOpcodeHandler);
|
---|
70 | procedure InitOpcodes;
|
---|
71 | private
|
---|
72 | FOnInput: TInputEvent;
|
---|
73 | FOnOutput: TOutputEvent;
|
---|
74 | Condition: Boolean;
|
---|
75 | procedure OpcodeNoOperation;
|
---|
76 | procedure OpcodeHalt;
|
---|
77 | procedure OpcodeIncrement;
|
---|
78 | procedure OpcodeDecrement;
|
---|
79 | procedure OpcodeJump;
|
---|
80 | procedure OpcodeJumpConditional;
|
---|
81 | procedure OpcodeJumpRelative;
|
---|
82 | procedure OpcodeJumpRelativeConditional;
|
---|
83 | procedure OpcodeLoad;
|
---|
84 | procedure OpcodeLoadConst;
|
---|
85 | procedure OpcodeLoadMemory;
|
---|
86 | procedure OpcodeStoreMemory;
|
---|
87 | procedure OpcodeTestNotZero;
|
---|
88 | procedure OpcodeTestZero;
|
---|
89 | procedure OpcodeTestNotCarry;
|
---|
90 | procedure OpcodeTestCarry;
|
---|
91 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
92 | procedure OpcodePrefix8;
|
---|
93 | procedure OpcodePrefix16;
|
---|
94 | procedure OpcodePrefix32;
|
---|
95 | procedure OpcodePrefix64;
|
---|
96 | {$ENDIF}
|
---|
97 | {$IFDEF EXT_GENERAL}
|
---|
98 | procedure OpcodeExchange;
|
---|
99 | {$ENDIF}
|
---|
100 | {$IFDEF EXT_IO}
|
---|
101 | procedure OpcodeInput;
|
---|
102 | procedure OpcodeOutput;
|
---|
103 | {$ENDIF}
|
---|
104 | {$IFDEF EXT_ADDRESSING_MODES}
|
---|
105 | procedure OpcodeLoadMemoryDisplacement;
|
---|
106 | procedure OpcodeStoreMemoryDisplacement;
|
---|
107 | {$ENDIF}
|
---|
108 | {$IFDEF EXT_ARITHMETIC}
|
---|
109 | procedure OpcodeAddition;
|
---|
110 | procedure OpcodeSubtraction;
|
---|
111 | procedure OpcodeDivision;
|
---|
112 | procedure OpcodeMultiplication;
|
---|
113 | {$ENDIF}
|
---|
114 | {$IFDEF EXT_LOGICAL}
|
---|
115 | procedure OpcodeAnd;
|
---|
116 | procedure OpcodeOr;
|
---|
117 | procedure OpcodeXor;
|
---|
118 | {$ENDIF}
|
---|
119 | {$IFDEF EXT_STACK}
|
---|
120 | procedure OpcodePop;
|
---|
121 | procedure OpcodePush;
|
---|
122 | {$ENDIF}
|
---|
123 | {$IFDEF EXT_SUBROUTINE}
|
---|
124 | procedure OpcodeCall;
|
---|
125 | procedure OpcodeReturn;
|
---|
126 | {$ENDIF}
|
---|
127 | {$IFDEF EXT_SHIFT}
|
---|
128 | procedure OpcodeShiftLeft;
|
---|
129 | procedure OpcodeShiftRight;
|
---|
130 | {$ENDIF}
|
---|
131 | {$IFDEF EXT_BLOCK}
|
---|
132 | procedure OpcodeLdir;
|
---|
133 | procedure OpcodeLddr;
|
---|
134 | {$ENDIF}
|
---|
135 | {$IFDEF EXT_BIT}
|
---|
136 | procedure OpcodeBitSet;
|
---|
137 | procedure OpcodeBitReset;
|
---|
138 | procedure OpcodeBitGet;
|
---|
139 | procedure OpcodeBitPut;
|
---|
140 | {$ENDIF}
|
---|
141 | public
|
---|
142 | Registers: array of T;
|
---|
143 | Memory: array of Byte;
|
---|
144 | IP: T;
|
---|
145 | DataWidth: TDataWidth;
|
---|
146 | {$IFDEF EXT_STACK}
|
---|
147 | SP: T;
|
---|
148 | {$ENDIF}
|
---|
149 | procedure Run;
|
---|
150 | constructor Create;
|
---|
151 | property OnInput: TInputEvent read FOnInput write FOnInput;
|
---|
152 | property OnOutput: TOutputEvent read FOnOutput write FOnOutput;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | { TInstructionWriter }
|
---|
156 |
|
---|
157 | generic TInstructionWriter<T> = class
|
---|
158 | type
|
---|
159 | PT = ^T;
|
---|
160 | TOpcodeIndex = Byte;
|
---|
161 | POpcodeIndex = ^TOpcodeIndex;
|
---|
162 | TRegIndex = Byte;
|
---|
163 | PRegIndex = ^TRegIndex;
|
---|
164 | var
|
---|
165 | Addr: Integer;
|
---|
166 | Machine: specialize TCPU<T>;
|
---|
167 | procedure AddLoadConst(Reg: TRegIndex; Value: T);
|
---|
168 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
169 | procedure AddLoadConst8(Reg: TRegIndex; Value: ShortInt);
|
---|
170 | procedure AddLoadConst16(Reg: TRegIndex; Value: SmallInt);
|
---|
171 | procedure AddLoadConst32(Reg: TRegIndex; Value: Integer);
|
---|
172 | procedure AddLoadConst64(Reg: TRegIndex; Value: Int64);
|
---|
173 | {$ENDIF}
|
---|
174 | procedure AddNop;
|
---|
175 | procedure AddHalt;
|
---|
176 | procedure AddReg(Reg: TRegIndex);
|
---|
177 | procedure AddOpcode(Opcode: TOpcode);
|
---|
178 | procedure AddData(Value: T);
|
---|
179 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
180 | procedure AddData8(Value: ShortInt);
|
---|
181 | procedure AddData16(Value: SmallInt);
|
---|
182 | procedure AddData32(Value: Integer);
|
---|
183 | procedure AddData64(Value: Int64);
|
---|
184 | {$ENDIF}
|
---|
185 | end;
|
---|
186 |
|
---|
187 |
|
---|
188 | implementation
|
---|
189 |
|
---|
190 | { TInstructionWriter }
|
---|
191 |
|
---|
192 | procedure TInstructionWriter.AddLoadConst(Reg: TRegIndex; Value: T);
|
---|
193 | begin
|
---|
194 | AddOpcode(opLDC);
|
---|
195 | AddReg(Reg);
|
---|
196 | AddData(Value);
|
---|
197 | end;
|
---|
198 |
|
---|
199 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
200 | procedure TInstructionWriter.AddLoadConst8(Reg: TRegIndex; Value: ShortInt);
|
---|
201 | begin
|
---|
202 | AddOpcode(opPrefix8);
|
---|
203 | AddOpcode(opLDC);
|
---|
204 | AddData8(Reg);
|
---|
205 | AddData8(Value);
|
---|
206 | end;
|
---|
207 |
|
---|
208 | procedure TInstructionWriter.AddLoadConst16(Reg: TRegIndex; Value: SmallInt);
|
---|
209 | begin
|
---|
210 | AddOpcode(opPrefix16);
|
---|
211 | AddOpcode(opLDC);
|
---|
212 | AddReg(Reg);
|
---|
213 | AddData16(Value);
|
---|
214 | end;
|
---|
215 |
|
---|
216 | procedure TInstructionWriter.AddLoadConst32(Reg: TRegIndex; Value: Integer);
|
---|
217 | begin
|
---|
218 | AddOpcode(opPrefix32);
|
---|
219 | AddOpcode(opLDC);
|
---|
220 | AddReg(Reg);
|
---|
221 | AddData32(Value);
|
---|
222 | end;
|
---|
223 |
|
---|
224 | procedure TInstructionWriter.AddLoadConst64(Reg: TRegIndex; Value: Int64);
|
---|
225 | begin
|
---|
226 | AddOpcode(opPrefix64);
|
---|
227 | AddOpcode(opLDC);
|
---|
228 | AddReg(Reg);
|
---|
229 | AddData64(Value);
|
---|
230 | end;
|
---|
231 | {$ENDIF}
|
---|
232 |
|
---|
233 | procedure TInstructionWriter.AddNop;
|
---|
234 | begin
|
---|
235 | AddOpcode(opNop);
|
---|
236 | end;
|
---|
237 |
|
---|
238 | procedure TInstructionWriter.AddHalt;
|
---|
239 | begin
|
---|
240 | AddOpcode(opHalt);
|
---|
241 | end;
|
---|
242 |
|
---|
243 | procedure TInstructionWriter.AddReg(Reg: TRegIndex);
|
---|
244 | begin
|
---|
245 | PRegIndex(@(Machine.Memory[Addr]))^ := Reg;
|
---|
246 | Inc(Addr, SizeOf(TRegIndex));
|
---|
247 | end;
|
---|
248 |
|
---|
249 | procedure TInstructionWriter.AddOpcode(Opcode: TOpcode);
|
---|
250 | begin
|
---|
251 | POpcodeIndex(@(Machine.Memory[Addr]))^ := TOpcodeIndex(Opcode);
|
---|
252 | Inc(Addr, SizeOf(TOpcodeIndex));
|
---|
253 | end;
|
---|
254 |
|
---|
255 | procedure TInstructionWriter.AddData(Value: T);
|
---|
256 | begin
|
---|
257 | PT(@(Machine.Memory[Addr]))^ := Value;
|
---|
258 | Inc(Addr, SizeOf(T));
|
---|
259 | end;
|
---|
260 |
|
---|
261 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
262 | procedure TInstructionWriter.AddData8(Value: ShortInt);
|
---|
263 | begin
|
---|
264 | PShortInt(@(Machine.Memory[Addr]))^ := Value;
|
---|
265 | Inc(Addr, SizeOf(ShortInt));
|
---|
266 | end;
|
---|
267 |
|
---|
268 | procedure TInstructionWriter.AddData16(Value: SmallInt);
|
---|
269 | begin
|
---|
270 | PSmallInt(@(Machine.Memory[Addr]))^ := Value;
|
---|
271 | Inc(Addr, SizeOf(SmallInt));
|
---|
272 | end;
|
---|
273 |
|
---|
274 | procedure TInstructionWriter.AddData32(Value: Integer);
|
---|
275 | begin
|
---|
276 | PInteger(@(Machine.Memory[Addr]))^ := Value;
|
---|
277 | Inc(Addr, SizeOf(Integer));
|
---|
278 | end;
|
---|
279 |
|
---|
280 | procedure TInstructionWriter.AddData64(Value: Int64);
|
---|
281 | begin
|
---|
282 | PInt64(@(Machine.Memory[Addr]))^ := Value;
|
---|
283 | Inc(Addr, SizeOf(Int64));
|
---|
284 | end;
|
---|
285 | {$ENDIF}
|
---|
286 |
|
---|
287 | { TCPU }
|
---|
288 |
|
---|
289 | function TCPU.ReadNext: T;
|
---|
290 | begin
|
---|
291 | Result := PT(@Memory[IP])^;
|
---|
292 | Inc(IP, SizeOf(T));
|
---|
293 | end;
|
---|
294 |
|
---|
295 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
296 | function TCPU.ReadNext8: ShortInt;
|
---|
297 | begin
|
---|
298 | Result := PShortInt(@Memory[IP])^;
|
---|
299 | Inc(IP, SizeOf(ShortInt));
|
---|
300 | end;
|
---|
301 |
|
---|
302 | function TCPU.ReadNext16: SmallInt;
|
---|
303 | begin
|
---|
304 | Result := PSmallInt(@Memory[IP])^;
|
---|
305 | Inc(IP, SizeOf(SmallInt));
|
---|
306 | end;
|
---|
307 |
|
---|
308 | function TCPU.ReadNext32: Integer;
|
---|
309 | begin
|
---|
310 | Result := PInteger(@Memory[IP])^;
|
---|
311 | Inc(IP, SizeOf(Integer));
|
---|
312 | end;
|
---|
313 |
|
---|
314 | function TCPU.ReadNext64: Int64;
|
---|
315 | begin
|
---|
316 | Result := PInt64(@Memory[IP])^;
|
---|
317 | Inc(IP, SizeOf(Int64));
|
---|
318 | end;
|
---|
319 | {$ENDIF}
|
---|
320 |
|
---|
321 | function TCPU.ReadOpcode: TOpcodeIndex;
|
---|
322 | begin
|
---|
323 | Result := POpcodeIndex(@Memory[IP])^;
|
---|
324 | Inc(IP, SizeOf(TOpcodeIndex));
|
---|
325 | end;
|
---|
326 |
|
---|
327 | function TCPU.ReadReg: TRegIndex;
|
---|
328 | begin
|
---|
329 | Result := PRegIndex(@Memory[IP])^;
|
---|
330 | Inc(IP, SizeOf(TRegIndex));
|
---|
331 | end;
|
---|
332 |
|
---|
333 | procedure TCPU.Run;
|
---|
334 | begin
|
---|
335 | IP := 0;
|
---|
336 | {$IFDEF EXT_STACK}
|
---|
337 | SP := Length(Memory);
|
---|
338 | {$ENDIF}
|
---|
339 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
340 | DataWidth := dwNative;
|
---|
341 | {$ENDIF}
|
---|
342 | Terminated := False;
|
---|
343 | while not Terminated do
|
---|
344 | Step;
|
---|
345 | end;
|
---|
346 |
|
---|
347 | constructor TCPU.Create;
|
---|
348 | begin
|
---|
349 | SetLength(Registers, 32);
|
---|
350 | SetLength(Memory, 100);
|
---|
351 | InitOpcodes;
|
---|
352 | end;
|
---|
353 |
|
---|
354 | procedure TCPU.OpcodeNoOperation;
|
---|
355 | begin
|
---|
356 | end;
|
---|
357 |
|
---|
358 | procedure TCPU.OpcodeHalt;
|
---|
359 | begin
|
---|
360 | Terminated := True;
|
---|
361 | end;
|
---|
362 |
|
---|
363 | procedure TCPU.OpcodeLoad;
|
---|
364 | var
|
---|
365 | Src, Dest: TRegIndex;
|
---|
366 | begin
|
---|
367 | Dest := ReadReg;
|
---|
368 | Src := ReadReg;
|
---|
369 | Registers[Dest] := Registers[Src];
|
---|
370 | end;
|
---|
371 |
|
---|
372 | procedure TCPU.OpcodeLoadConst;
|
---|
373 | var
|
---|
374 | Reg: TRegIndex;
|
---|
375 | begin
|
---|
376 | Reg := ReadReg;
|
---|
377 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
378 | case DataWidth of
|
---|
379 | dwNative: Registers[Reg] := ReadNext;
|
---|
380 | dw8: Registers[Reg] := ReadNext8;
|
---|
381 | dw16: Registers[Reg] := ReadNext16;
|
---|
382 | dw32: Registers[Reg] := ReadNext32;
|
---|
383 | dw64: Registers[Reg] := ReadNext64;
|
---|
384 | end;
|
---|
385 | {$ELSE}
|
---|
386 | Registers[Reg] := ReadNext;
|
---|
387 | {$ENDIF}
|
---|
388 | end;
|
---|
389 |
|
---|
390 | procedure TCPU.OpcodeLoadMemory;
|
---|
391 | var
|
---|
392 | Src, Dest: TRegIndex;
|
---|
393 | begin
|
---|
394 | Dest := ReadReg;
|
---|
395 | Src := ReadReg;
|
---|
396 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
397 | case DataWidth of
|
---|
398 | dwNative: Registers[Dest] := PT(@Memory[Registers[Src]])^;
|
---|
399 | dw8: Registers[Dest] := PShortInt(@Memory[Registers[Src]])^;
|
---|
400 | dw16: Registers[Dest] := PSmallInt(@Memory[Registers[Src]])^;
|
---|
401 | dw32: Registers[Dest] := PInteger(@Memory[Registers[Src]])^;
|
---|
402 | dw64: Registers[Dest] := PInt64(@Memory[Registers[Src]])^;
|
---|
403 | end;
|
---|
404 | {$ELSE}
|
---|
405 | Registers[Dest] := PT(@Memory[Registers[Src]])^;
|
---|
406 | {$ENDIF}
|
---|
407 | end;
|
---|
408 |
|
---|
409 | procedure TCPU.OpcodeStoreMemory;
|
---|
410 | var
|
---|
411 | Src, Dest: TRegIndex;
|
---|
412 | begin
|
---|
413 | Dest := ReadReg;
|
---|
414 | Src := ReadReg;
|
---|
415 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
416 | case DataWidth of
|
---|
417 | dwNative: PT(@Memory[Registers[Dest]])^ := Registers[Src];
|
---|
418 | dw8: PShortInt(@Memory[Registers[Dest]])^ := Registers[Src];
|
---|
419 | dw16: PSmallInt(@Memory[Registers[Dest]])^ := Registers[Src];
|
---|
420 | dw32: PInteger(@Memory[Registers[Dest]])^ := Registers[Src];
|
---|
421 | dw64: PInt64(@Memory[Registers[Dest]])^ := Registers[Src];
|
---|
422 | end;
|
---|
423 | {$ELSE}
|
---|
424 | PT(@Memory[Registers[Dest]])^ := Registers[Src];
|
---|
425 | {$ENDIF}
|
---|
426 | end;
|
---|
427 |
|
---|
428 | {$IFDEF EXT_ADDRESSING_MODES}
|
---|
429 | procedure TCPU.OpcodeLoadMemoryDisplacement;
|
---|
430 | var
|
---|
431 | Disp, Src, Dest: TRegIndex;
|
---|
432 | begin
|
---|
433 | Dest := ReadReg;
|
---|
434 | Src := ReadReg;
|
---|
435 | Disp := ReadReg;
|
---|
436 | Registers[Dest] := PT(@Memory[Registers[Src] + Registers[Disp]])^;
|
---|
437 | end;
|
---|
438 |
|
---|
439 | procedure TCPU.OpcodeStoreMemoryDisplacement;
|
---|
440 | var
|
---|
441 | Disp, Src, Dest: TRegIndex;
|
---|
442 | begin
|
---|
443 | Dest := ReadReg;
|
---|
444 | Disp := ReadReg;
|
---|
445 | Src := ReadReg;
|
---|
446 | PT(@Memory[Registers[Dest] + Registers[Disp]])^ := Registers[Src];
|
---|
447 | end;
|
---|
448 | {$ENDIF}
|
---|
449 |
|
---|
450 | procedure TCPU.OpcodeIncrement;
|
---|
451 | var
|
---|
452 | Reg: TRegIndex;
|
---|
453 | begin
|
---|
454 | Reg := ReadReg;
|
---|
455 | Registers[Reg] := Registers[Reg] + 1;
|
---|
456 | end;
|
---|
457 |
|
---|
458 | procedure TCPU.OpcodeDecrement;
|
---|
459 | var
|
---|
460 | Reg: TRegIndex;
|
---|
461 | begin
|
---|
462 | Reg := ReadReg;
|
---|
463 | Registers[Reg] := Registers[Reg] - 1;
|
---|
464 | end;
|
---|
465 |
|
---|
466 | {$IFDEF EXT_ARITHMETIC}
|
---|
467 | procedure TCPU.OpcodeAddition;
|
---|
468 | var
|
---|
469 | Dest, Src: TRegIndex;
|
---|
470 | begin
|
---|
471 | Dest := ReadReg;
|
---|
472 | Src := ReadReg;
|
---|
473 | Registers[Dest] := Registers[Dest] + Registers[Src];
|
---|
474 | end;
|
---|
475 |
|
---|
476 | procedure TCPU.OpcodeSubtraction;
|
---|
477 | var
|
---|
478 | Dest, Src: TRegIndex;
|
---|
479 | begin
|
---|
480 | Dest := ReadReg;
|
---|
481 | Src := ReadReg;
|
---|
482 | Registers[Dest] := Registers[Dest] - Registers[Src];
|
---|
483 | end;
|
---|
484 |
|
---|
485 | procedure TCPU.OpcodeMultiplication;
|
---|
486 | var
|
---|
487 | Dest, Src: TRegIndex;
|
---|
488 | begin
|
---|
489 | Dest := ReadReg;
|
---|
490 | Src := ReadReg;
|
---|
491 | Registers[Dest] := Registers[Dest] * Registers[Src];
|
---|
492 | end;
|
---|
493 |
|
---|
494 | procedure TCPU.OpcodeDivision;
|
---|
495 | var
|
---|
496 | Dest, Src: TRegIndex;
|
---|
497 | begin
|
---|
498 | Dest := ReadReg;
|
---|
499 | Src := ReadReg;
|
---|
500 | Registers[Dest] := Registers[Dest] div Registers[Src];
|
---|
501 | end;
|
---|
502 | {$ENDIF}
|
---|
503 |
|
---|
504 | {$IFDEF EXT_SHIFT}
|
---|
505 | procedure TCPU.OpcodeShiftLeft;
|
---|
506 | var
|
---|
507 | Dest, Src: TRegIndex;
|
---|
508 | begin
|
---|
509 | Dest := ReadReg;
|
---|
510 | Src := ReadReg;
|
---|
511 | Registers[Dest] := Registers[Dest] shl Registers[Src];
|
---|
512 | end;
|
---|
513 |
|
---|
514 | procedure TCPU.OpcodeShiftRight;
|
---|
515 | var
|
---|
516 | Src, Dest: TRegIndex;
|
---|
517 | begin
|
---|
518 | Dest := ReadReg;
|
---|
519 | Src := ReadReg;
|
---|
520 | Registers[Dest] := Registers[Dest] shr Registers[Src];
|
---|
521 | end;
|
---|
522 | {$ENDIF}
|
---|
523 |
|
---|
524 | {$IFDEF EXT_LOGICAL}
|
---|
525 | procedure TCPU.OpcodeAnd;
|
---|
526 | var
|
---|
527 | Src, Dest: TRegIndex;
|
---|
528 | begin
|
---|
529 | Dest := ReadReg;
|
---|
530 | Src := ReadReg;
|
---|
531 | Registers[Dest] := Registers[Dest] and Registers[Src];
|
---|
532 | end;
|
---|
533 |
|
---|
534 | procedure TCPU.OpcodeOr;
|
---|
535 | var
|
---|
536 | Src, Dest: TRegIndex;
|
---|
537 | begin
|
---|
538 | Dest := ReadReg;
|
---|
539 | Src := ReadReg;
|
---|
540 | Registers[Dest] := Registers[Dest] or Registers[Src];
|
---|
541 | end;
|
---|
542 |
|
---|
543 | procedure TCPU.OpcodeXor;
|
---|
544 | var
|
---|
545 | Src, Dest: TRegIndex;
|
---|
546 | begin
|
---|
547 | Dest := ReadReg;
|
---|
548 | Src := ReadReg;
|
---|
549 | Registers[Dest] := Registers[Dest] xor Registers[Src];
|
---|
550 | end;
|
---|
551 | {$ENDIF}
|
---|
552 |
|
---|
553 | procedure TCPU.OpcodeJump;
|
---|
554 | var
|
---|
555 | Reg: TRegIndex;
|
---|
556 | begin
|
---|
557 | Reg := ReadReg;
|
---|
558 | IP := Registers[Reg];
|
---|
559 | end;
|
---|
560 |
|
---|
561 | procedure TCPU.OpcodeJumpConditional;
|
---|
562 | var
|
---|
563 | Reg: TRegIndex;
|
---|
564 | begin
|
---|
565 | Reg := ReadReg;
|
---|
566 | if Condition then
|
---|
567 | IP := Registers[Reg];
|
---|
568 | end;
|
---|
569 |
|
---|
570 | procedure TCPU.OpcodeJumpRelative;
|
---|
571 | var
|
---|
572 | Reg: TRegIndex;
|
---|
573 | begin
|
---|
574 | Reg := ReadReg;
|
---|
575 | IP := IP + Registers[Reg];
|
---|
576 | end;
|
---|
577 |
|
---|
578 | procedure TCPU.OpcodeJumpRelativeConditional;
|
---|
579 | var
|
---|
580 | Reg: TRegIndex;
|
---|
581 | begin
|
---|
582 | Reg := ReadReg;
|
---|
583 | if Condition then
|
---|
584 | IP := IP + Registers[Reg];
|
---|
585 | end;
|
---|
586 |
|
---|
587 | {$IFDEF EXT_STACK}
|
---|
588 | procedure TCPU.OpcodePush;
|
---|
589 | var
|
---|
590 | Reg: TRegIndex;
|
---|
591 | begin
|
---|
592 | SP := SP - SizeOf(T);
|
---|
593 | Reg := ReadReg;
|
---|
594 | PT(@Memory[SP])^ := Registers[Reg];
|
---|
595 | end;
|
---|
596 |
|
---|
597 | procedure TCPU.OpcodePop;
|
---|
598 | var
|
---|
599 | Reg: TRegIndex;
|
---|
600 | begin
|
---|
601 | Reg := ReadReg;
|
---|
602 | Registers[Reg] := PT(@Memory[SP])^;
|
---|
603 | SP := SP + SizeOf(T);
|
---|
604 | end;
|
---|
605 | {$ENDIF}
|
---|
606 |
|
---|
607 | {$IFDEF EXT_SUBROUTINE}
|
---|
608 | procedure TCPU.OpcodeCall;
|
---|
609 | var
|
---|
610 | Reg: TRegIndex;
|
---|
611 | begin
|
---|
612 | SP := SP - SizeOf(T);
|
---|
613 | Reg := ReadReg;
|
---|
614 | PT(@Memory[SP])^ := IP;
|
---|
615 | IP := Registers[Reg];
|
---|
616 | end;
|
---|
617 |
|
---|
618 | procedure TCPU.OpcodeReturn;
|
---|
619 | begin
|
---|
620 | IP := PT(@Memory[SP])^;
|
---|
621 | SP := SP + SizeOf(T);
|
---|
622 | end;
|
---|
623 | {$ENDIF}
|
---|
624 |
|
---|
625 | {$IFDEF EXT_IO}
|
---|
626 | procedure TCPU.OpcodeInput;
|
---|
627 | var
|
---|
628 | Src, Dest: TRegIndex;
|
---|
629 | begin
|
---|
630 | Dest := ReadReg;
|
---|
631 | Src := ReadReg;
|
---|
632 | if Assigned(FOnInput) then
|
---|
633 | Registers[Dest] := FOnInput(Registers[Src]);
|
---|
634 | end;
|
---|
635 |
|
---|
636 | procedure TCPU.OpcodeOutput;
|
---|
637 | var
|
---|
638 | Dest, Src: TRegIndex;
|
---|
639 | begin
|
---|
640 | Dest := ReadReg;
|
---|
641 | Src := ReadReg;
|
---|
642 | if Assigned(FOnOutput) then
|
---|
643 | FOnOutput(Registers[Dest], Registers[Src]);
|
---|
644 | end;
|
---|
645 | {$ENDIF}
|
---|
646 |
|
---|
647 | procedure TCPU.OpcodeTestZero;
|
---|
648 | var
|
---|
649 | Reg: TRegIndex;
|
---|
650 | begin
|
---|
651 | Reg := ReadReg;
|
---|
652 | Condition := Registers[Reg] = 0;
|
---|
653 | end;
|
---|
654 |
|
---|
655 | procedure TCPU.OpcodeTestNotCarry;
|
---|
656 | var
|
---|
657 | Reg1: TRegIndex;
|
---|
658 | Reg2: TRegIndex;
|
---|
659 | begin
|
---|
660 | Reg1 := ReadReg;
|
---|
661 | Reg2 := ReadReg;
|
---|
662 | Condition := Registers[Reg2] <= Registers[Reg1];
|
---|
663 | end;
|
---|
664 |
|
---|
665 | procedure TCPU.OpcodeTestCarry;
|
---|
666 | var
|
---|
667 | Reg1: TRegIndex;
|
---|
668 | Reg2: TRegIndex;
|
---|
669 | begin
|
---|
670 | Reg1 := ReadReg;
|
---|
671 | Reg2 := ReadReg;
|
---|
672 | Condition := Registers[Reg2] > Registers[Reg1];
|
---|
673 | end;
|
---|
674 |
|
---|
675 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
676 | procedure TCPU.OpcodePrefix8;
|
---|
677 | begin
|
---|
678 | DataWidth := dw8;
|
---|
679 | end;
|
---|
680 |
|
---|
681 | procedure TCPU.OpcodePrefix16;
|
---|
682 | begin
|
---|
683 | DataWidth := dw16;
|
---|
684 | end;
|
---|
685 |
|
---|
686 | procedure TCPU.OpcodePrefix32;
|
---|
687 | begin
|
---|
688 | DataWidth := dw32;
|
---|
689 | end;
|
---|
690 |
|
---|
691 | procedure TCPU.OpcodePrefix64;
|
---|
692 | begin
|
---|
693 | DataWidth := dw64;
|
---|
694 | end;
|
---|
695 | {$ENDIF}
|
---|
696 |
|
---|
697 | procedure TCPU.OpcodeTestNotZero;
|
---|
698 | var
|
---|
699 | Reg: TRegIndex;
|
---|
700 | begin
|
---|
701 | Reg := ReadReg;
|
---|
702 | Condition := Registers[Reg] <> 0;
|
---|
703 | end;
|
---|
704 |
|
---|
705 | {$IFDEF EXT_GENERAL}
|
---|
706 | procedure TCPU.OpcodeExchange;
|
---|
707 | var
|
---|
708 | Dest, Src: TRegIndex;
|
---|
709 | Temp: T;
|
---|
710 | begin
|
---|
711 | Dest := ReadReg;
|
---|
712 | Src := ReadReg;
|
---|
713 | Temp := Registers[Dest];
|
---|
714 | Registers[Dest] := Registers[Src];
|
---|
715 | Registers[Src] := Temp;
|
---|
716 | end;
|
---|
717 | {$ENDIF}
|
---|
718 |
|
---|
719 | {$IFDEF EXT_BLOCK}
|
---|
720 | procedure TCPU.OpcodeLdir;
|
---|
721 | var
|
---|
722 | Dest, Src, Count: TRegIndex;
|
---|
723 | begin
|
---|
724 | Dest := ReadReg;
|
---|
725 | Src := ReadReg;
|
---|
726 | Count := ReadReg;
|
---|
727 | while Count > 0 do begin
|
---|
728 | Memory[Registers[Dest]] := Memory[Registers[Src]];
|
---|
729 | Inc(Registers[Dest]);
|
---|
730 | Inc(Registers[Src]);
|
---|
731 | Dec(Registers[Count]);
|
---|
732 | end;
|
---|
733 | end;
|
---|
734 |
|
---|
735 | procedure TCPU.OpcodeLddr;
|
---|
736 | var
|
---|
737 | Dest, Src, Count: TRegIndex;
|
---|
738 | begin
|
---|
739 | Dest := ReadReg;
|
---|
740 | Src := ReadReg;
|
---|
741 | Count := ReadReg;
|
---|
742 | while Count > 0 do begin
|
---|
743 | Memory[Registers[Dest]] := Memory[Registers[Src]];
|
---|
744 | Dec(Registers[Dest]);
|
---|
745 | Dec(Registers[Src]);
|
---|
746 | Dec(Registers[Count]);
|
---|
747 | end;
|
---|
748 | end;
|
---|
749 | {$ENDIF}
|
---|
750 |
|
---|
751 | {$IFDEF EXT_BIT}
|
---|
752 | procedure TCPU.OpcodeBitSet;
|
---|
753 | var
|
---|
754 | Reg: TRegIndex;
|
---|
755 | Bit: TRegIndex;
|
---|
756 | begin
|
---|
757 | Reg := ReadReg;
|
---|
758 | Bit := ReadReg;
|
---|
759 | Registers[Reg] := Registers[Reg] or (1 shl Registers[Bit]);
|
---|
760 | end;
|
---|
761 |
|
---|
762 | procedure TCPU.OpcodeBitReset;
|
---|
763 | var
|
---|
764 | Reg: TRegIndex;
|
---|
765 | Bit: TRegIndex;
|
---|
766 | begin
|
---|
767 | Reg := ReadReg;
|
---|
768 | Bit := ReadReg;
|
---|
769 | Registers[Reg] := Registers[Reg] and ((1 shl Registers[Bit]) xor -1);
|
---|
770 | end;
|
---|
771 |
|
---|
772 | procedure TCPU.OpcodeBitGet;
|
---|
773 | var
|
---|
774 | Reg: TRegIndex;
|
---|
775 | Bit: TRegIndex;
|
---|
776 | begin
|
---|
777 | Reg := ReadReg;
|
---|
778 | Bit := ReadReg;
|
---|
779 | Condition := ((Registers[Reg] shr Registers[Bit]) and 1) = 1;
|
---|
780 | end;
|
---|
781 |
|
---|
782 | procedure TCPU.OpcodeBitPut;
|
---|
783 | var
|
---|
784 | Reg: TRegIndex;
|
---|
785 | Bit: TRegIndex;
|
---|
786 | begin
|
---|
787 | Reg := ReadReg;
|
---|
788 | Bit := ReadReg;
|
---|
789 | if Condition then
|
---|
790 | Registers[Reg] := Registers[Reg] or (1 shl Registers[Bit]);
|
---|
791 | end;
|
---|
792 | {$ENDIF}
|
---|
793 |
|
---|
794 | procedure TCPU.Step;
|
---|
795 | var
|
---|
796 | Opcode: TOpcode;
|
---|
797 | Handler: TOpcodeHandler;
|
---|
798 | begin
|
---|
799 | Opcode := TOpcode(ReadOpcode);
|
---|
800 | if Opcode <= High(TOpcode) then begin
|
---|
801 | Handler := OpcodeHandlers[Opcode];
|
---|
802 | if Assigned(Handler) then Handler
|
---|
803 | else raise Exception.Create('Missing handler for opcode + ' + IntToStr(Integer(Opcode)));
|
---|
804 | end else raise Exception.Create('Unknown opcode: ' + IntToStr(Integer(Opcode)));
|
---|
805 | {$IFDEF EXT_LOWER_WIDTH}
|
---|
806 | if (Opcode <> opPrefix8) and (Opcode <> opPrefix16) and (Opcode <> opPrefix32) and
|
---|
807 | (Opcode <> opPrefix64) then
|
---|
808 | DataWidth := dwNative;
|
---|
809 | {$ENDIF}
|
---|
810 | end;
|
---|
811 |
|
---|
812 | procedure TCPU.AddOpcode(Opcode: TOpcode; Handler: TOpcodeHandler);
|
---|
813 | begin
|
---|
814 | OpcodeHandlers[Opcode] := Handler;
|
---|
815 | end;
|
---|
816 |
|
---|
817 | procedure TCPU.InitOpcodes;
|
---|
818 | begin
|
---|
819 | AddOpcode(opNop, @OpcodeNoOperation);
|
---|
820 | AddOpcode(opHalt, @OpcodeHalt);
|
---|
821 | AddOpcode(opLD, @OpcodeLoad);
|
---|
822 | AddOpcode(opLDC, @OpcodeLoadConst);
|
---|
823 | AddOpcode(opLDM, @OpcodeLoadMemory);
|
---|
824 | AddOpcode(opSTM, @OpcodeStoreMemory);
|
---|
825 | AddOpcode(opInc, @OpcodeIncrement);
|
---|
826 | AddOpcode(opDec, @OpcodeDecrement);
|
---|
827 | AddOpcode(opTstNZ, @OpcodeTestNotZero);
|
---|
828 | AddOpcode(opTstZ, @OpcodeTestZero);
|
---|
829 | AddOpcode(opTstNC, @OpcodeTestNotCarry);
|
---|
830 | AddOpcode(opTstC, @OpcodeTestCarry);
|
---|
831 | AddOpcode(opJP, @OpcodeJump);
|
---|
832 | AddOpcode(opJpc, @OpcodeJumpConditional);
|
---|
833 | AddOpcode(opJR, @OpcodeJumpRelative);
|
---|
834 | AddOpcode(opJRC, @OpcodeJumpRelativeConditional);
|
---|
835 | {$IFDEF EXT_BIT}
|
---|
836 | AddOpcode(opBitSet, @OpcodeBitSet);
|
---|
837 | AddOpcode(opBitRes, @OpcodeBitReset);
|
---|
838 | AddOpcode(opBitPut, @OpcodeBitPut);
|
---|
839 | AddOpcode(opBitGet, @OpcodeBitGet);
|
---|
840 | {$ENDIF}
|
---|
841 | {$IFDEF EXT_LOGICAL}
|
---|
842 | AddOpcode(opAnd, @OpcodeAnd);
|
---|
843 | AddOpcode(opOr, @OpcodeOr);
|
---|
844 | AddOpcode(opXor, @OpcodeXor);
|
---|
845 | {$ENDIF}
|
---|
846 | {$IFDEF EXT_ADDRESSING_MODES}
|
---|
847 | AddOpcode(opLDMD, @OpcodeLoadMemoryDisplacement);
|
---|
848 | AddOpcode(opSTMD, @OpcodeStoreMemoryDisplacement);
|
---|
849 | {$ENDIF}
|
---|
850 | {$IFDEF EXT_SUBROUTINE}
|
---|
851 | AddOpcode(opCall, @OpcodeCall);
|
---|
852 | AddOpcode(opRet, @OpcodeReturn);
|
---|
853 | {$ENDIF}
|
---|
854 | {$IFDEF EXT_STACK}
|
---|
855 | AddOpcode(opPush, @OpcodePush);
|
---|
856 | AddOpcode(opPop, @OpcodePop);
|
---|
857 | {$ENDIF}
|
---|
858 | {$IFDEF EXT_ARITHMETIC}
|
---|
859 | AddOpcode(opAdd, @OpcodeAddition);
|
---|
860 | AddOpcode(opSub, @OpcodeSubtraction);
|
---|
861 | AddOpcode(opMul, @OpcodeMultiplication);
|
---|
862 | AddOpcode(opDiv, @OpcodeDivision);
|
---|
863 | {$ENDIF}
|
---|
864 | {$IFDEF EXT_GENERAL}
|
---|
865 | AddOpcode(opXchg, @OpcodeExchange);
|
---|
866 | {$ENDIF}
|
---|
867 | {$IFDEF EXT_SHIFT}
|
---|
868 | AddOpcode(opShl, @OpcodeShiftLeft);
|
---|
869 | AddOpcode(opShr, @OpcodeShiftRight);
|
---|
870 | {$ENDIF}
|
---|
871 | {$IFDEF EXT_IO}
|
---|
872 | AddOpcode(opIn, @OpcodeInput);
|
---|
873 | AddOpcode(opOut, @OpcodeOutput);
|
---|
874 | {$ENDIF}
|
---|
875 | {$IFDEF EXT_BLOCK}
|
---|
876 | AddOpcode(opLdir, @OpcodeLdir);
|
---|
877 | AddOpcode(opLddr, @OpcodeLddr);
|
---|
878 | {$ENDIF}
|
---|
879 | end;
|
---|
880 |
|
---|
881 | end.
|
---|
882 |
|
---|