1 | unit Cpu;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, BigInt, Channel;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TInstruction = (inNop, inHalt,
|
---|
10 | inLoadConst, inLoadConstSize,
|
---|
11 | inLoad, inLoadSize,
|
---|
12 | inLoadMem, inLoadMemSize,
|
---|
13 | inStoreMem, inStoreMemSize,
|
---|
14 | inLoadMemIndex, inLoadMemIndexSize,
|
---|
15 | inStoreMemIndex, inStoreMemIndexSize,
|
---|
16 | inInput, inInputSize,
|
---|
17 | inOutput, inOutputSize,
|
---|
18 | inInc, inIncSize, inDec, inDecSize,
|
---|
19 | inAdd, inAddSize, inSub, inSubSize,
|
---|
20 | inShr, inShrSize, inShl, inShlSize,
|
---|
21 | inXor, inXorSize, inAnd, inAndSize, inOr, inOrSize,
|
---|
22 | inJump, inJumpSize,
|
---|
23 | inJumpNotZero, inJumpNotZeroSize,
|
---|
24 | inJumpZero, inJumpZeroSize,
|
---|
25 | inJumpRel, inJumpRelSize,
|
---|
26 | inCall, inCallSize,
|
---|
27 | inRet, inRetSize,
|
---|
28 | inPush, inPushSize,
|
---|
29 | inPop, inPopSize,
|
---|
30 | inSet, inSetSize,
|
---|
31 | inRes, inResSize,
|
---|
32 | inBit, inBitSize,
|
---|
33 | inMul, inMulSize,
|
---|
34 | inDiv, inDivSize,
|
---|
35 | inLdir, inLdi, inLddr, inLdd,
|
---|
36 | inInir, inIni, inIndr, inInd,
|
---|
37 | inOtir, inOti, inOtdr, inOtd,
|
---|
38 | inCpir, inCpi, inCpdr, inCpd,
|
---|
39 | inEx, inEnableInterrupts, inDisableInterrupts,
|
---|
40 | inCompare);
|
---|
41 |
|
---|
42 | TRegIndex = Byte;
|
---|
43 |
|
---|
44 | TInstructionEvent = procedure of object;
|
---|
45 | TCpuThread = class;
|
---|
46 |
|
---|
47 | { TCpu }
|
---|
48 |
|
---|
49 | TCpu = class
|
---|
50 | private
|
---|
51 | FHalted: Boolean;
|
---|
52 | FRunning: Boolean;
|
---|
53 | FThread: TCpuThread;
|
---|
54 | FInstructions: array[TInstruction] of TInstructionEvent;
|
---|
55 | FTicks: QWord;
|
---|
56 | procedure Push(Value: TBigInt; Size: TBigIntSize);
|
---|
57 | function Pop(Size: TBigIntSize): TBigInt;
|
---|
58 | procedure InstructionNop;
|
---|
59 | procedure InstructionHalt;
|
---|
60 | procedure InstructionLoadConst;
|
---|
61 | procedure InstructionLoadConstSize;
|
---|
62 | procedure InstructionLoad;
|
---|
63 | procedure InstructionLoadSize;
|
---|
64 | procedure InstructionLoadMem;
|
---|
65 | procedure InstructionLoadMemSize;
|
---|
66 | procedure InstructionStoreMem;
|
---|
67 | procedure InstructionStoreMemSize;
|
---|
68 | procedure InstructionLoadMemIndex;
|
---|
69 | procedure InstructionLoadMemIndexSize;
|
---|
70 | procedure InstructionStoreMemIndex;
|
---|
71 | procedure InstructionStoreMemIndexSize;
|
---|
72 | procedure InstructionJump;
|
---|
73 | procedure InstructionJumpSize;
|
---|
74 | procedure InstructionJumpNotZero;
|
---|
75 | procedure InstructionJumpNotZeroSize;
|
---|
76 | procedure InstructionJumpZero;
|
---|
77 | procedure InstructionJumpZeroSize;
|
---|
78 | procedure InstructionJumpRel;
|
---|
79 | procedure InstructionJumpRelSize;
|
---|
80 | procedure InstructionCall;
|
---|
81 | procedure InstructionCallSize;
|
---|
82 | procedure InstructionRet;
|
---|
83 | procedure InstructionRetSize;
|
---|
84 | procedure InstructionPush;
|
---|
85 | procedure InstructionPushSize;
|
---|
86 | procedure InstructionPop;
|
---|
87 | procedure InstructionPopSize;
|
---|
88 | procedure InstructionInput;
|
---|
89 | procedure InstructionInputSize;
|
---|
90 | procedure InstructionOutput;
|
---|
91 | procedure InstructionOutputSize;
|
---|
92 | procedure InstructionInc;
|
---|
93 | procedure InstructionIncSize;
|
---|
94 | procedure InstructionDec;
|
---|
95 | procedure InstructionDecSize;
|
---|
96 | procedure InstructionXor;
|
---|
97 | procedure InstructionXorSize;
|
---|
98 | procedure InstructionAnd;
|
---|
99 | procedure InstructionAndSize;
|
---|
100 | procedure InstructionOr;
|
---|
101 | procedure InstructionOrSize;
|
---|
102 | procedure InstructionAdd;
|
---|
103 | procedure InstructionAddSize;
|
---|
104 | procedure InstructionSub;
|
---|
105 | procedure InstructionSubSize;
|
---|
106 | procedure InitInstructions;
|
---|
107 | procedure SetRunning(AValue: Boolean);
|
---|
108 | procedure Run;
|
---|
109 | procedure Step;
|
---|
110 | public
|
---|
111 | Regs: array[TRegIndex] of TBigInt;
|
---|
112 | PC: TBigInt;
|
---|
113 | SP: TBigInt;
|
---|
114 | DataWidth: TBigIntSize;
|
---|
115 | AddressWidth: TBigIntSize;
|
---|
116 | Memory: TChannel;
|
---|
117 | IO: TChannel;
|
---|
118 | function Read(Size: TBigIntSize): TBigInt;
|
---|
119 | function ReadSize: TBigIntSize;
|
---|
120 | function ReadRegIndex: TRegIndex;
|
---|
121 | procedure Write(Size: TBigIntSize; Value: TBigInt);
|
---|
122 | procedure WriteInstruction(Instruction: TInstruction);
|
---|
123 | procedure WriteRegister(Reg: TRegIndex);
|
---|
124 | procedure Reset;
|
---|
125 | procedure Start;
|
---|
126 | procedure Stop;
|
---|
127 | constructor Create;
|
---|
128 | destructor Destroy; override;
|
---|
129 | property Ticks: QWord read FTicks;
|
---|
130 | property Running: Boolean read FRunning write SetRunning;
|
---|
131 | property Halted: Boolean read FHalted;
|
---|
132 | end;
|
---|
133 |
|
---|
134 | { TCpuThread }
|
---|
135 |
|
---|
136 | TCpuThread = class(TThread)
|
---|
137 | Cpu: TCpu;
|
---|
138 | procedure Execute; override;
|
---|
139 | end;
|
---|
140 |
|
---|
141 |
|
---|
142 | implementation
|
---|
143 |
|
---|
144 | { TCpuThread }
|
---|
145 |
|
---|
146 | procedure TCpuThread.Execute;
|
---|
147 | begin
|
---|
148 | repeat
|
---|
149 | if not Cpu.Halted then Cpu.Step
|
---|
150 | else Sleep(1);
|
---|
151 | //Cpu.CheckInterreupts;
|
---|
152 | until Terminated;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | { TCpu }
|
---|
156 |
|
---|
157 | procedure TCpu.Push(Value: TBigInt; Size: TBigIntSize);
|
---|
158 | begin
|
---|
159 | SP := SP - Size;
|
---|
160 | Memory.Write(SP, Size, Value);
|
---|
161 | end;
|
---|
162 |
|
---|
163 | function TCpu.Pop(Size: TBigIntSize): TBigInt;
|
---|
164 | begin
|
---|
165 | Result := Memory.Read(SP, Size);
|
---|
166 | SP := SP + Size;
|
---|
167 | end;
|
---|
168 |
|
---|
169 | procedure TCpu.InstructionNop;
|
---|
170 | begin
|
---|
171 | // Do nothing
|
---|
172 | end;
|
---|
173 |
|
---|
174 | procedure TCpu.InstructionHalt;
|
---|
175 | begin
|
---|
176 | FHalted := True;
|
---|
177 | end;
|
---|
178 |
|
---|
179 | procedure TCpu.InstructionLoadConst;
|
---|
180 | var
|
---|
181 | RegIndex: TRegIndex;
|
---|
182 | begin
|
---|
183 | RegIndex := ReadRegIndex;
|
---|
184 | Regs[RegIndex] := Read(DataWidth);
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TCpu.InstructionLoadConstSize;
|
---|
188 | var
|
---|
189 | RegIndex: TRegIndex;
|
---|
190 | DataSize: TBigIntSize;
|
---|
191 | begin
|
---|
192 | DataSize := ReadSize;
|
---|
193 | RegIndex := ReadRegIndex;
|
---|
194 | Regs[RegIndex] := Read(DataSize);
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TCpu.InstructionLoad;
|
---|
198 | var
|
---|
199 | RegIndex: TRegIndex;
|
---|
200 | RegIndex2: TRegIndex;
|
---|
201 | begin
|
---|
202 | RegIndex := ReadRegIndex;
|
---|
203 | RegIndex2 := ReadRegIndex;
|
---|
204 | Regs[RegIndex] := Regs[RegIndex2];
|
---|
205 | end;
|
---|
206 |
|
---|
207 | procedure TCpu.InstructionLoadSize;
|
---|
208 | var
|
---|
209 | DataSize: TBigIntSize;
|
---|
210 | RegIndex: TRegIndex;
|
---|
211 | RegIndex2: TRegIndex;
|
---|
212 | begin
|
---|
213 | DataSize := ReadSize;
|
---|
214 | RegIndex := ReadRegIndex;
|
---|
215 | RegIndex2 := ReadRegIndex;
|
---|
216 | Regs[RegIndex] := Regs[RegIndex2].Copy(DataSize);
|
---|
217 | end;
|
---|
218 |
|
---|
219 | procedure TCpu.InstructionLoadMem;
|
---|
220 | var
|
---|
221 | RegIndex1: TRegIndex;
|
---|
222 | RegIndex2: TRegIndex;
|
---|
223 | begin
|
---|
224 | RegIndex1 := ReadRegIndex;
|
---|
225 | RegIndex2 := ReadRegIndex;
|
---|
226 | Regs[RegIndex1] := Memory.Read(Regs[RegIndex2], DataWidth);
|
---|
227 | end;
|
---|
228 |
|
---|
229 | procedure TCpu.InstructionLoadMemSize;
|
---|
230 | var
|
---|
231 | DataSize: TBigIntSize;
|
---|
232 | RegIndex1: TRegIndex;
|
---|
233 | RegIndex2: TRegIndex;
|
---|
234 | begin
|
---|
235 | DataSize := ReadSize;
|
---|
236 | RegIndex1 := ReadRegIndex;
|
---|
237 | RegIndex2 := ReadRegIndex;
|
---|
238 | Regs[RegIndex1] := Memory.Read(Regs[RegIndex2], DataSize);
|
---|
239 | end;
|
---|
240 |
|
---|
241 | procedure TCpu.InstructionStoreMem;
|
---|
242 | var
|
---|
243 | RegIndex1: TRegIndex;
|
---|
244 | RegIndex2: TRegIndex;
|
---|
245 | begin
|
---|
246 | RegIndex1 := ReadRegIndex;
|
---|
247 | RegIndex2 := ReadRegIndex;
|
---|
248 | Memory.Write(Regs[RegIndex1], DataWidth, Regs[RegIndex2]);
|
---|
249 | end;
|
---|
250 |
|
---|
251 | procedure TCpu.InstructionStoreMemSize;
|
---|
252 | var
|
---|
253 | DataSize: TBigIntSize;
|
---|
254 | RegIndex1: TRegIndex;
|
---|
255 | RegIndex2: TRegIndex;
|
---|
256 | begin
|
---|
257 | DataSize := ReadSize;
|
---|
258 | RegIndex1 := ReadRegIndex;
|
---|
259 | RegIndex2 := ReadRegIndex;
|
---|
260 | Memory.Write(Regs[RegIndex1], DataSize, Regs[RegIndex2]);
|
---|
261 | end;
|
---|
262 |
|
---|
263 | procedure TCpu.InstructionLoadMemIndex;
|
---|
264 | var
|
---|
265 | RegIndex1: TRegIndex;
|
---|
266 | RegIndex2: TRegIndex;
|
---|
267 | RelativeAddress: TBigInt;
|
---|
268 | begin
|
---|
269 | RegIndex1 := ReadRegIndex;
|
---|
270 | RegIndex2 := ReadRegIndex;
|
---|
271 | RelativeAddress := Read(AddressWidth);
|
---|
272 | Regs[RegIndex1] := Memory.Read(Regs[RegIndex2] + RelativeAddress, DataWidth);
|
---|
273 | end;
|
---|
274 |
|
---|
275 | procedure TCpu.InstructionLoadMemIndexSize;
|
---|
276 | var
|
---|
277 | DataSize: TBigIntSize;
|
---|
278 | RegIndex1: TRegIndex;
|
---|
279 | RegIndex2: TRegIndex;
|
---|
280 | RelativeAddress: TBigInt;
|
---|
281 | begin
|
---|
282 | DataSize := ReadSize;
|
---|
283 | RegIndex1 := ReadRegIndex;
|
---|
284 | RegIndex2 := ReadRegIndex;
|
---|
285 | RelativeAddress := Read(AddressWidth);
|
---|
286 | Regs[RegIndex1] := Memory.Read(Regs[RegIndex2] + RelativeAddress, DataSize);
|
---|
287 | end;
|
---|
288 |
|
---|
289 | procedure TCpu.InstructionStoreMemIndex;
|
---|
290 | var
|
---|
291 | RegIndex1: TRegIndex;
|
---|
292 | RegIndex2: TRegIndex;
|
---|
293 | RelativeAddress: TBigInt;
|
---|
294 | begin
|
---|
295 | RegIndex1 := ReadRegIndex;
|
---|
296 | RegIndex2 := ReadRegIndex;
|
---|
297 | RelativeAddress := Read(AddressWidth);
|
---|
298 | Memory.Write(Regs[RegIndex1] + RelativeAddress, DataWidth, Regs[RegIndex2]);
|
---|
299 | end;
|
---|
300 |
|
---|
301 | procedure TCpu.InstructionStoreMemIndexSize;
|
---|
302 | var
|
---|
303 | DataSize: TBigIntSize;
|
---|
304 | RegIndex1: TRegIndex;
|
---|
305 | RegIndex2: TRegIndex;
|
---|
306 | RelativeAddress: TBigInt;
|
---|
307 | begin
|
---|
308 | DataSize := ReadSize;
|
---|
309 | RegIndex1 := ReadRegIndex;
|
---|
310 | RegIndex2 := ReadRegIndex;
|
---|
311 | RelativeAddress := Read(AddressWidth);
|
---|
312 | Memory.Write(Regs[RegIndex1] + RelativeAddress, DataSize, Regs[RegIndex2]);
|
---|
313 | end;
|
---|
314 |
|
---|
315 | procedure TCpu.InstructionJump;
|
---|
316 | begin
|
---|
317 | PC := Read(AddressWidth);
|
---|
318 | end;
|
---|
319 |
|
---|
320 | procedure TCpu.InstructionJumpSize;
|
---|
321 | var
|
---|
322 | AddressSize: TBigIntSize;
|
---|
323 | begin
|
---|
324 | AddressSize := Read(SizeOf(TBigIntSize));
|
---|
325 | PC := Read(AddressSize);
|
---|
326 | end;
|
---|
327 |
|
---|
328 | procedure TCpu.InstructionJumpNotZero;
|
---|
329 | var
|
---|
330 | RegIndex: TRegIndex;
|
---|
331 | Address: TBigInt;
|
---|
332 | begin
|
---|
333 | RegIndex := ReadRegIndex;
|
---|
334 | Address := Read(AddressWidth);
|
---|
335 | if Regs[RegIndex] <> 0 then
|
---|
336 | PC := Address;
|
---|
337 | end;
|
---|
338 |
|
---|
339 | procedure TCpu.InstructionJumpNotZeroSize;
|
---|
340 | var
|
---|
341 | RegIndex: TRegIndex;
|
---|
342 | Address: TBigInt;
|
---|
343 | DataSize: TBigIntSize;
|
---|
344 | AddressSize: TBigIntSize;
|
---|
345 | begin
|
---|
346 | DataSize := ReadSize;
|
---|
347 | AddressSize := Read(SizeOf(TBigIntSize));
|
---|
348 | RegIndex := ReadRegIndex;
|
---|
349 | Address := Read(AddressSize);
|
---|
350 | if Regs[RegIndex].Copy(DataSize) <> 0 then
|
---|
351 | PC := Address;
|
---|
352 | end;
|
---|
353 |
|
---|
354 | procedure TCpu.InstructionJumpZero;
|
---|
355 | var
|
---|
356 | RegIndex: TRegIndex;
|
---|
357 | Address: TBigInt;
|
---|
358 | begin
|
---|
359 | RegIndex := ReadRegIndex;
|
---|
360 | Address := Read(AddressWidth);
|
---|
361 | if Regs[RegIndex] = 0 then
|
---|
362 | PC := Address;
|
---|
363 | end;
|
---|
364 |
|
---|
365 | procedure TCpu.InstructionJumpZeroSize;
|
---|
366 | var
|
---|
367 | RegIndex: TRegIndex;
|
---|
368 | Address: TBigInt;
|
---|
369 | DataSize: TBigIntSize;
|
---|
370 | AddressSize: TBigIntSize;
|
---|
371 | begin
|
---|
372 | DataSize := ReadSize;
|
---|
373 | AddressSize := Read(SizeOf(TBigIntSize));
|
---|
374 | RegIndex := ReadRegIndex;
|
---|
375 | Address := Read(AddressSize);
|
---|
376 | if Regs[RegIndex].Copy(DataSize) = 0 then
|
---|
377 | PC := Address;
|
---|
378 | end;
|
---|
379 |
|
---|
380 | procedure TCpu.InstructionJumpRel;
|
---|
381 | begin
|
---|
382 | PC := PC + Read(AddressWidth);
|
---|
383 | end;
|
---|
384 |
|
---|
385 | procedure TCpu.InstructionJumpRelSize;
|
---|
386 | var
|
---|
387 | AddressSize: TBigIntSize;
|
---|
388 | begin
|
---|
389 | AddressSize := ReadSize;
|
---|
390 | PC := PC + Read(AddressSize);
|
---|
391 | end;
|
---|
392 |
|
---|
393 | procedure TCpu.InstructionCall;
|
---|
394 | begin
|
---|
395 | Push(PC, AddressWidth);
|
---|
396 | PC := Read(AddressWidth);
|
---|
397 | end;
|
---|
398 |
|
---|
399 | procedure TCpu.InstructionCallSize;
|
---|
400 | var
|
---|
401 | AddressSize: TBigIntSize;
|
---|
402 | begin
|
---|
403 | AddressSize := ReadSize;
|
---|
404 | Push(PC, AddressSize);
|
---|
405 | PC := Read(AddressSize);
|
---|
406 | end;
|
---|
407 |
|
---|
408 | procedure TCpu.InstructionRet;
|
---|
409 | begin
|
---|
410 | PC := Pop(AddressWidth);
|
---|
411 | end;
|
---|
412 |
|
---|
413 | procedure TCpu.InstructionRetSize;
|
---|
414 | var
|
---|
415 | AddressSize: TBigIntSize;
|
---|
416 | begin
|
---|
417 | AddressSize := ReadSize;
|
---|
418 | PC := Pop(AddressSize);
|
---|
419 | end;
|
---|
420 |
|
---|
421 | procedure TCpu.InstructionPush;
|
---|
422 | var
|
---|
423 | RegIndex: TRegIndex;
|
---|
424 | begin
|
---|
425 | RegIndex := ReadRegIndex;
|
---|
426 | Push(Regs[RegIndex], DataWidth);
|
---|
427 | end;
|
---|
428 |
|
---|
429 | procedure TCpu.InstructionPushSize;
|
---|
430 | var
|
---|
431 | DataSize: TBigIntSize;
|
---|
432 | RegIndex: TRegIndex;
|
---|
433 | begin
|
---|
434 | DataSize := ReadSize;
|
---|
435 | RegIndex := ReadRegIndex;
|
---|
436 | Push(Regs[RegIndex], DataSize);
|
---|
437 | end;
|
---|
438 |
|
---|
439 | procedure TCpu.InstructionPop;
|
---|
440 | var
|
---|
441 | RegIndex: TRegIndex;
|
---|
442 | begin
|
---|
443 | RegIndex := ReadRegIndex;
|
---|
444 | Regs[RegIndex] := Pop(DataWidth);
|
---|
445 | end;
|
---|
446 |
|
---|
447 | procedure TCpu.InstructionPopSize;
|
---|
448 | var
|
---|
449 | DataSize: TBigIntSize;
|
---|
450 | RegIndex: TRegIndex;
|
---|
451 | begin
|
---|
452 | DataSize := ReadSize;
|
---|
453 | RegIndex := ReadRegIndex;
|
---|
454 | Regs[RegIndex] := Pop(DataSize);
|
---|
455 | end;
|
---|
456 |
|
---|
457 | procedure TCpu.InstructionInput;
|
---|
458 | var
|
---|
459 | RegIndex: TRegIndex;
|
---|
460 | RegIndex2: TRegIndex;
|
---|
461 | begin
|
---|
462 | RegIndex := ReadRegIndex;
|
---|
463 | RegIndex2 := ReadRegIndex;
|
---|
464 | Regs[RegIndex] := IO.Read(Regs[RegIndex2], DataWidth);
|
---|
465 | end;
|
---|
466 |
|
---|
467 | procedure TCpu.InstructionInputSize;
|
---|
468 | var
|
---|
469 | RegIndex: TRegIndex;
|
---|
470 | RegIndex2: TRegIndex;
|
---|
471 | DataSize: TBigIntSize;
|
---|
472 | begin
|
---|
473 | DataSize := ReadSize;
|
---|
474 | RegIndex := ReadRegIndex;
|
---|
475 | RegIndex2 := ReadRegIndex;
|
---|
476 | Regs[RegIndex] := IO.Read(Regs[RegIndex2], DataSize);
|
---|
477 | end;
|
---|
478 |
|
---|
479 | procedure TCpu.InstructionOutput;
|
---|
480 | var
|
---|
481 | RegIndex: TRegIndex;
|
---|
482 | RegIndex2: TRegIndex;
|
---|
483 | begin
|
---|
484 | RegIndex := ReadRegIndex;
|
---|
485 | RegIndex2 := ReadRegIndex;
|
---|
486 | IO.Write(Regs[RegIndex], DataWidth, Regs[RegIndex2]);
|
---|
487 | end;
|
---|
488 |
|
---|
489 | procedure TCpu.InstructionOutputSize;
|
---|
490 | var
|
---|
491 | RegIndex: TRegIndex;
|
---|
492 | RegIndex2: TRegIndex;
|
---|
493 | DataSize: TBigIntSize;
|
---|
494 | begin
|
---|
495 | DataSize := ReadSize;
|
---|
496 | RegIndex := ReadRegIndex;
|
---|
497 | RegIndex2 := ReadRegIndex;
|
---|
498 | IO.Write(Regs[RegIndex], DataSize, Regs[RegIndex2]);
|
---|
499 | end;
|
---|
500 |
|
---|
501 | procedure TCpu.InstructionInc;
|
---|
502 | var
|
---|
503 | RegIndex: TRegIndex;
|
---|
504 | begin
|
---|
505 | RegIndex := ReadRegIndex;
|
---|
506 | Regs[RegIndex] := Regs[RegIndex] + 1;
|
---|
507 | end;
|
---|
508 |
|
---|
509 | procedure TCpu.InstructionIncSize;
|
---|
510 | var
|
---|
511 | RegIndex: TRegIndex;
|
---|
512 | DataSize: TBigIntSize;
|
---|
513 | begin
|
---|
514 | DataSize := ReadSize;
|
---|
515 | RegIndex := ReadRegIndex;
|
---|
516 | Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) + 1;
|
---|
517 | end;
|
---|
518 |
|
---|
519 | procedure TCpu.InstructionDec;
|
---|
520 | var
|
---|
521 | RegIndex: TRegIndex;
|
---|
522 | begin
|
---|
523 | RegIndex := ReadRegIndex;
|
---|
524 | Regs[RegIndex] := Regs[RegIndex] - 1;
|
---|
525 | end;
|
---|
526 |
|
---|
527 | procedure TCpu.InstructionDecSize;
|
---|
528 | var
|
---|
529 | RegIndex: TRegIndex;
|
---|
530 | DataSize: TBigIntSize;
|
---|
531 | begin
|
---|
532 | DataSize := ReadSize;
|
---|
533 | RegIndex := ReadRegIndex;
|
---|
534 | Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) - 1;
|
---|
535 | end;
|
---|
536 |
|
---|
537 | procedure TCpu.InstructionXor;
|
---|
538 | var
|
---|
539 | RegIndex: TRegIndex;
|
---|
540 | RegIndex2: TRegIndex;
|
---|
541 | begin
|
---|
542 | RegIndex := ReadRegIndex;
|
---|
543 | RegIndex2 := ReadRegIndex;
|
---|
544 | Regs[RegIndex] := Regs[RegIndex] xor Regs[RegIndex2];
|
---|
545 | end;
|
---|
546 |
|
---|
547 | procedure TCpu.InstructionXorSize;
|
---|
548 | var
|
---|
549 | DataSize: TBigIntSize;
|
---|
550 | RegIndex: TRegIndex;
|
---|
551 | RegIndex2: TRegIndex;
|
---|
552 | begin
|
---|
553 | DataSize := ReadSize;
|
---|
554 | RegIndex := ReadRegIndex;
|
---|
555 | RegIndex2 := ReadRegIndex;
|
---|
556 | Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) xor Regs[RegIndex2].Copy(DataSize);
|
---|
557 | end;
|
---|
558 |
|
---|
559 | procedure TCpu.InstructionAnd;
|
---|
560 | var
|
---|
561 | RegIndex: TRegIndex;
|
---|
562 | RegIndex2: TRegIndex;
|
---|
563 | begin
|
---|
564 | RegIndex := ReadRegIndex;
|
---|
565 | RegIndex2 := ReadRegIndex;
|
---|
566 | Regs[RegIndex] := Regs[RegIndex] and Regs[RegIndex2];
|
---|
567 | end;
|
---|
568 |
|
---|
569 | procedure TCpu.InstructionAndSize;
|
---|
570 | var
|
---|
571 | DataSize: TBigIntSize;
|
---|
572 | RegIndex: TRegIndex;
|
---|
573 | RegIndex2: TRegIndex;
|
---|
574 | begin
|
---|
575 | DataSize := ReadSize;
|
---|
576 | RegIndex := ReadRegIndex;
|
---|
577 | RegIndex2 := ReadRegIndex;
|
---|
578 | Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) and Regs[RegIndex2].Copy(DataSize);
|
---|
579 | end;
|
---|
580 |
|
---|
581 | procedure TCpu.InstructionOr;
|
---|
582 | var
|
---|
583 | RegIndex: TRegIndex;
|
---|
584 | RegIndex2: TRegIndex;
|
---|
585 | begin
|
---|
586 | RegIndex := ReadRegIndex;
|
---|
587 | RegIndex2 := ReadRegIndex;
|
---|
588 | Regs[RegIndex] := Regs[RegIndex] or Regs[RegIndex2];
|
---|
589 | end;
|
---|
590 |
|
---|
591 | procedure TCpu.InstructionOrSize;
|
---|
592 | var
|
---|
593 | DataSize: TBigIntSize;
|
---|
594 | RegIndex: TRegIndex;
|
---|
595 | RegIndex2: TRegIndex;
|
---|
596 | begin
|
---|
597 | DataSize := ReadSize;
|
---|
598 | RegIndex := ReadRegIndex;
|
---|
599 | RegIndex2 := ReadRegIndex;
|
---|
600 | Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) or Regs[RegIndex2].Copy(DataSize);
|
---|
601 | end;
|
---|
602 |
|
---|
603 | procedure TCpu.InstructionAdd;
|
---|
604 | var
|
---|
605 | RegIndex: TRegIndex;
|
---|
606 | RegIndex2: TRegIndex;
|
---|
607 | begin
|
---|
608 | RegIndex := ReadRegIndex;
|
---|
609 | RegIndex2 := ReadRegIndex;
|
---|
610 | Regs[RegIndex] := Regs[RegIndex] + Regs[RegIndex2];
|
---|
611 | end;
|
---|
612 |
|
---|
613 | procedure TCpu.InstructionAddSize;
|
---|
614 | var
|
---|
615 | DataSize: TBigIntSize;
|
---|
616 | RegIndex: TRegIndex;
|
---|
617 | RegIndex2: TRegIndex;
|
---|
618 | begin
|
---|
619 | DataSize := ReadSize;
|
---|
620 | RegIndex := ReadRegIndex;
|
---|
621 | RegIndex2 := ReadRegIndex;
|
---|
622 | Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) + Regs[RegIndex2].Copy(DataSize);
|
---|
623 | end;
|
---|
624 |
|
---|
625 | procedure TCpu.InstructionSub;
|
---|
626 | var
|
---|
627 | RegIndex: TRegIndex;
|
---|
628 | RegIndex2: TRegIndex;
|
---|
629 | begin
|
---|
630 | RegIndex := ReadRegIndex;
|
---|
631 | RegIndex2 := ReadRegIndex;
|
---|
632 | Regs[RegIndex] := Regs[RegIndex] - Regs[RegIndex2];
|
---|
633 | end;
|
---|
634 |
|
---|
635 | procedure TCpu.InstructionSubSize;
|
---|
636 | var
|
---|
637 | DataSize: TBigIntSize;
|
---|
638 | RegIndex: TRegIndex;
|
---|
639 | RegIndex2: TRegIndex;
|
---|
640 | begin
|
---|
641 | DataSize := ReadSize;
|
---|
642 | RegIndex := ReadRegIndex;
|
---|
643 | RegIndex2 := ReadRegIndex;
|
---|
644 | Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) + Regs[RegIndex2].Copy(DataSize);
|
---|
645 | end;
|
---|
646 |
|
---|
647 | procedure TCpu.InitInstructions;
|
---|
648 | begin
|
---|
649 | FInstructions[inNop] := InstructionNop;
|
---|
650 | FInstructions[inHalt] := InstructionHalt;
|
---|
651 | FInstructions[inLoadConst] := InstructionLoadConst;
|
---|
652 | FInstructions[inLoadConstSize] := InstructionLoadConstSize;
|
---|
653 | FInstructions[inLoad] := InstructionLoad;
|
---|
654 | FInstructions[inLoadSize] := InstructionLoadSize;
|
---|
655 | FInstructions[inLoadMem] := InstructionLoadMem;
|
---|
656 | FInstructions[inLoadMemSize] := InstructionLoadMemSize;
|
---|
657 | FInstructions[inStoreMem] := InstructionStoreMem;
|
---|
658 | FInstructions[inStoreMemSize] := InstructionStoreMemSize;
|
---|
659 | FInstructions[inLoadMemIndex] := InstructionLoadMemIndex;
|
---|
660 | FInstructions[inLoadMemIndexSize] := InstructionLoadMemIndexSize;
|
---|
661 | FInstructions[inStoreMemIndex] := InstructionStoreMemIndex;
|
---|
662 | FInstructions[inStoreMemIndexSize] := InstructionStoreMemIndexSize;
|
---|
663 | FInstructions[inJump] := InstructionJump;
|
---|
664 | FInstructions[inJumpSize] := InstructionJumpSize;
|
---|
665 | FInstructions[inJumpNotZero] := InstructionJumpNotZero;
|
---|
666 | FInstructions[inJumpNotZeroSize] := InstructionJumpNotZeroSize;
|
---|
667 | FInstructions[inJumpZero] := InstructionJumpZero;
|
---|
668 | FInstructions[inJumpZeroSize] := InstructionJumpZeroSize;
|
---|
669 | FInstructions[inJumpRel] := InstructionJumpRel;
|
---|
670 | FInstructions[inJumpRelSize] := InstructionJumpRelSize;
|
---|
671 | FInstructions[inCall] := InstructionCall;
|
---|
672 | FInstructions[inCallSize] := InstructionCallSize;
|
---|
673 | FInstructions[inRet] := InstructionRet;
|
---|
674 | FInstructions[inRetSize] := InstructionRetSize;
|
---|
675 | FInstructions[inPush] := InstructionPush;
|
---|
676 | FInstructions[inPushSize] := InstructionPushSize;
|
---|
677 | FInstructions[inPop] := InstructionPop;
|
---|
678 | FInstructions[inPopSize] := InstructionPopSize;
|
---|
679 | FInstructions[inInput] := InstructionInput;
|
---|
680 | FInstructions[inInputSize] := InstructionInputSize;
|
---|
681 | FInstructions[inOutput] := InstructionOutput;
|
---|
682 | FInstructions[inOutputSize] := InstructionOutputSize;
|
---|
683 | FInstructions[inInc] := InstructionInc;
|
---|
684 | FInstructions[inIncSize] := InstructionIncSize;
|
---|
685 | FInstructions[inDec] := InstructionDec;
|
---|
686 | FInstructions[inDecSize] := InstructionDecSize;
|
---|
687 | FInstructions[inXor] := InstructionXor;
|
---|
688 | FInstructions[inXorSize] := InstructionXorSize;
|
---|
689 | FInstructions[inAnd] := InstructionAnd;
|
---|
690 | FInstructions[inAndSize] := InstructionAndSize;
|
---|
691 | FInstructions[inOr] := InstructionOr;
|
---|
692 | FInstructions[inOrSize] := InstructionOrSize;
|
---|
693 | FInstructions[inAdd] := InstructionAdd;
|
---|
694 | FInstructions[inAddSize] := InstructionAddSize;
|
---|
695 | FInstructions[inSub] := InstructionSub;
|
---|
696 | FInstructions[inSubSize] := InstructionSubSize;
|
---|
697 | end;
|
---|
698 |
|
---|
699 | procedure TCpu.SetRunning(AValue: Boolean);
|
---|
700 | begin
|
---|
701 | if FRunning = AValue then Exit;
|
---|
702 | if AValue then Start
|
---|
703 | else Stop;
|
---|
704 | end;
|
---|
705 |
|
---|
706 | function TCpu.Read(Size: TBigIntSize): TBigInt;
|
---|
707 | begin
|
---|
708 | Result := Memory.Read(PC, Size);
|
---|
709 | PC := PC + Size;
|
---|
710 | end;
|
---|
711 |
|
---|
712 | function TCpu.ReadSize: TBigIntSize;
|
---|
713 | begin
|
---|
714 | Result := Read(SizeOf(TBigIntSize));
|
---|
715 | end;
|
---|
716 |
|
---|
717 | function TCpu.ReadRegIndex: TRegIndex;
|
---|
718 | begin
|
---|
719 | Result := TRegIndex(Byte(Read(1)));
|
---|
720 | end;
|
---|
721 |
|
---|
722 | procedure TCpu.Write(Size: TBigIntSize; Value: TBigInt);
|
---|
723 | begin
|
---|
724 | Memory.Write(PC, Size, Value);
|
---|
725 | PC := PC + Size;
|
---|
726 | end;
|
---|
727 |
|
---|
728 | procedure TCpu.WriteInstruction(Instruction: TInstruction);
|
---|
729 | begin
|
---|
730 | Write(1, Byte(Instruction));
|
---|
731 | end;
|
---|
732 |
|
---|
733 | procedure TCpu.WriteRegister(Reg: TRegIndex);
|
---|
734 | begin
|
---|
735 | Write(1, Byte(Reg));
|
---|
736 | end;
|
---|
737 |
|
---|
738 | procedure TCpu.Reset;
|
---|
739 | var
|
---|
740 | I: TRegIndex;
|
---|
741 | begin
|
---|
742 | DataWidth := 1;
|
---|
743 | AddressWidth := 1;
|
---|
744 | for I := Low(TRegIndex) to High(TRegIndex) do
|
---|
745 | Regs[I] := 0;
|
---|
746 | PC := 0;
|
---|
747 | SP := 0;
|
---|
748 | FHalted := False;
|
---|
749 | FTicks := 0;
|
---|
750 | end;
|
---|
751 |
|
---|
752 | procedure TCpu.Run;
|
---|
753 | begin
|
---|
754 | Reset;
|
---|
755 | while not FHalted do
|
---|
756 | Step;
|
---|
757 | end;
|
---|
758 |
|
---|
759 | procedure TCpu.Step;
|
---|
760 | var
|
---|
761 | Instruction: TInstruction;
|
---|
762 | begin
|
---|
763 | Instruction := TInstruction(Byte(Read(1)));
|
---|
764 | FInstructions[Instruction];
|
---|
765 | Inc(FTicks);
|
---|
766 | end;
|
---|
767 |
|
---|
768 | procedure TCpu.Start;
|
---|
769 | begin
|
---|
770 | if not Running then begin
|
---|
771 | Reset;
|
---|
772 | FThread := TCpuThread.Create(True);
|
---|
773 | FThread.Cpu := Self;
|
---|
774 | FThread.Start;
|
---|
775 | FRunning := True;
|
---|
776 | end;
|
---|
777 | end;
|
---|
778 |
|
---|
779 | procedure TCpu.Stop;
|
---|
780 | begin
|
---|
781 | if Running then begin
|
---|
782 | FHalted := True;
|
---|
783 | FThread.Terminate;
|
---|
784 | FThread.WaitFor;
|
---|
785 | FreeAndNil(FThread);
|
---|
786 | FRunning := False;
|
---|
787 | end;
|
---|
788 | end;
|
---|
789 |
|
---|
790 | constructor TCpu.Create;
|
---|
791 | begin
|
---|
792 | Memory := TChannel.Create;
|
---|
793 | IO := TChannel.Create;
|
---|
794 | InitInstructions;
|
---|
795 | Reset;
|
---|
796 | end;
|
---|
797 |
|
---|
798 | destructor TCpu.Destroy;
|
---|
799 | begin
|
---|
800 | Stop;
|
---|
801 | FreeAndNil(Memory);
|
---|
802 | FreeAndNil(IO);
|
---|
803 | inherited;
|
---|
804 | end;
|
---|
805 |
|
---|
806 | end.
|
---|
807 |
|
---|
808 |
|
---|
809 | 32-bit
|
---|
810 |
|
---|
811 | LD A, B
|
---|
812 | LD A1, B1
|
---|
813 | LD A2, $aabb
|
---|
814 | LD A2, (B2)
|
---|
815 | LD A2, ($ffff)
|
---|
816 | LD A.2, ($00ff.2)
|
---|
817 | LD.2.2 A2, ($ffff)
|
---|
818 | JR.128 $ff
|
---|
819 | LD (A2), (B2)
|
---|
820 | LD (n.1), (n.1)
|
---|
821 | LD (n.1), (n.1)
|
---|
822 |
|
---|
823 | LD A, B
|
---|
824 | LD2 A, B
|
---|
825 | LD A2, B2
|
---|
826 | LD.2 A, n
|
---|
827 | LD A, (B)
|
---|
828 | LD.1 A, (n)
|
---|
829 | IN.1 A, (n)
|
---|
830 | IN.1 A, (B)
|
---|
831 | JP n
|
---|
832 | JP.1 n
|
---|
833 | JR n
|
---|
834 | JR.1 n
|
---|
835 | PUSH A
|
---|
836 | PUSH.1 A
|
---|
837 | PUSH.4 A
|
---|
838 | PUSH.128 A
|
---|
839 | RET
|
---|
840 | LD.2 (A), (B)
|
---|
841 | INC A
|
---|
842 | LD.1 A, n
|
---|
843 | SHL.2 A
|
---|
844 | LD.1 A, n
|
---|
845 | AND.2 A, n
|
---|
846 | LD.2 B, A
|
---|
847 | IN C, (B)
|
---|
848 | IN.d1.a1 C, (B)
|
---|
849 | CALL n
|
---|
850 | CALL.4 n
|
---|
851 | LD A, (B + n)
|
---|
852 | LD.1.2.1 A, (B + n)
|
---|
853 | LD.d1.a2.r1 A, (B + n)
|
---|
854 | LD A, (B + n)
|
---|
855 | LDIR A, B, C
|
---|
856 | LDIR.4 (A), (B), C
|
---|
857 | LDI (A), (B), C
|
---|
858 | SET.4 A, 20
|
---|
859 |
|
---|
860 | - bit index 2048 (256 * 8) (8 + 3) bits = 2^11
|
---|
861 |
|
---|
862 | LD R0, R1
|
---|
863 | LD2 R0, R1
|
---|
864 | LD.2 R0, R1
|
---|
865 | LD:2 R0, R1
|
---|
866 | LD D2, R0, R1
|
---|
867 | LD D2, A16, R0, (R1)
|
---|
868 | LD D2:A16: R0, (R1)
|
---|
869 | RET
|
---|
870 | RET2
|
---|
871 | RET.2
|
---|
872 | RET:2
|
---|
873 | RET A2
|
---|
874 | PUSH R0
|
---|
875 | PUSH2 R0
|
---|
876 | PUSH.2 R0
|
---|
877 | PUSH:2 R0
|
---|
878 | PUSH D2, R0
|
---|
879 | PUSH D2:R0
|
---|
880 | PUSH R0.2
|
---|
881 | PUSH R0:2
|
---|
882 | LD (R0), (R2)
|
---|
883 | LD D1, A2, (R0), (R2)
|
---|
884 | LDIR (R0), (R1), R2
|
---|
885 | LDIR D1, A2, (R0), (R1), R2
|
---|
886 | LDI
|
---|
887 | OUT (R0), R1
|
---|
888 | OUT D1, (R0), R1
|
---|
889 | OUT D1, A2, (R0), R1
|
---|
890 |
|
---|
891 |
|
---|
892 |
|
---|
893 | LD A, TEXT
|
---|
894 | CALL PRINT
|
---|
895 |
|
---|
896 | :PRINT
|
---|
897 | PUSH B
|
---|
898 | :PRINT_START
|
---|
899 | LD B, (A)
|
---|
900 | TEST B
|
---|
901 | JR Z, PRINT_END
|
---|
902 | OUT (n), B
|
---|
903 | INC A
|
---|
904 | JR PRINT_START
|
---|
905 | :PRINT_END
|
---|
906 | POP B
|
---|
907 | RET
|
---|
908 |
|
---|