Debian Codegraph @

sim.cpp

raw symbols SCIP
1 // File: sim.cpp
2 /*
3 Pep8-1 is a virtual machine for writing machine language and assembly
4 language programs.
5
6 Copyright (C) 2009 J. Stanley Warford, Pepperdine University
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "sim.h"
22 #include "pep.h"
23 #include <QMessageBox>
24 #include <QSet>
25
26 using namespace Enu;
27
28 // The machine
29 QVector<int> Sim::Mem(65536);
30 bool Sim::nBit, Sim::zBit, Sim::vBit, Sim::cBit;
31 int Sim::accumulator;
32 int Sim::indexRegister;
33 int Sim::stackPointer;
34 int Sim::programCounter;
35 int Sim::instructionSpecifier;
36 int Sim::operandSpecifier;
37 int Sim::operand;
38 int Sim::operandDisplayFieldWidth;
39
40 QString Sim::inputBuffer;
41 QString Sim::outputBuffer;
42
43 QSet<int> Sim::modifiedBytes;
44 bool Sim::trapped;
45 bool Sim::tracingTraps;
46
47 Enu::EExecState Sim::executionState;
48
49 int Sim::toSignedDecimal(int value)
50 {
51 return value > 32767 ? value - 65536 : value;
52 }
53
54 int Sim::fromSignedDecimal(int value)
55 {
56 return value < 0 ? value + 65536 : value;
57 }
58
59 int Sim::nzvcToInt()
60 {
61 int i = 0;
62 if (nBit) i |= 8;
63 if (zBit) i |= 4;
64 if (vBit) i |= 2;
65 if (cBit) i |= 1;
66 return i;
67 }
68
69 void Sim::loadMem(QList<int> objectCodeList) {
70 for (int i = 0; objectCodeList.length() > 0; i++) {
71 Mem[i] = objectCodeList.takeAt(0);
72 }
73 }
74
75 int Sim::add(int lhs, int rhs)
76 {
77 return (lhs + rhs) & 0xffff;
78 }
79
80 int Sim::addAndSetNZVC(int lhs, int rhs)
81 {
82 int result = lhs + rhs;
83 if (result >= 65536) {
84 Sim::cBit = 1;
85 result = result & 0xffff;
86 }
87 else {
88 Sim::cBit = 0;
89 }
90 Sim::nBit = result < 32768 ? false : true;
91 Sim::zBit = result == 0 ? true : false;
92 Sim::vBit = (lhs < 32768 && rhs < 32768 && result >= 32768) ||
93 (lhs >= 32768 && rhs >= 32768 && result < 32768);
94 return result;
95 }
96
97 int Sim::readByte(int memAddr)
98 {
99 return Mem[memAddr & 0xffff];
100 }
101
102 int Sim::readWord(int memAddr)
103 {
104 return 256 * Mem[memAddr & 0xffff] + Mem[(memAddr + 1) & 0xffff];
105 }
106
107 int Sim::readByteOprnd(Enu::EAddrMode addrMode)
108 {
109 switch (addrMode) {
110 case Enu::NONE:
111 break;
112 case Enu::I:
113 return operandSpecifier;
114 case Enu::D:
115 return readByte(operandSpecifier);
116 case Enu::N:
117 return readByte(readWord(operandSpecifier));
118 case Enu::S:
119 return readByte(add(stackPointer, operandSpecifier));
120 case Enu::SF:
121 return readByte(readWord(add(stackPointer, operandSpecifier)));
122 case Enu::X:
123 return readByte(add(operandSpecifier, indexRegister));
124 case Enu::SX:
125 return readByte(add(add(stackPointer, operandSpecifier), indexRegister));
126 case Enu::SXF:
127 return readByte(add(readWord(add(stackPointer, operandSpecifier)), indexRegister));
128 case Enu::ALL:
129 break;
130 }
131 return 0;
132 }
133
134 int Sim::readWordOprnd(Enu::EAddrMode addrMode)
135 {
136 switch (addrMode) {
137 case Enu::NONE:
138 break;
139 case Enu::I:
140 return operandSpecifier;
141 case Enu::D:
142 return readWord(operandSpecifier);
143 case Enu::N:
144 return readWord(readWord(operandSpecifier));
145 case Enu::S:
146 return readWord(add(stackPointer, operandSpecifier));
147 case Enu::SF:
148 return readWord(readWord(add(stackPointer, operandSpecifier)));
149 case Enu::X:
150 return readWord(add(operandSpecifier, indexRegister));
151 case Enu::SX:
152 return readWord(add(add(stackPointer, operandSpecifier), indexRegister));
153 case Enu::SXF:
154 return readWord(add(readWord(add(stackPointer, operandSpecifier)), indexRegister));
155 case Enu::ALL:
156 break;
157 }
158 return 0;
159 }
160
161 void Sim::writeByte(int memAddr, int value)
162 {
163 if (memAddr < Pep::romStartAddress) {
164 Mem[memAddr & 0xffff] = value;
165 modifiedBytes.insert(memAddr & 0xffff);
166 }
167 }
168
169 void Sim::writeWord(int memAddr, int value)
170 {
171 if (memAddr < Pep::romStartAddress) { // There is an intentional inaccuracy here (it is possible to overwrite the first byte of ROM).
172 Mem[memAddr & 0xffff] = value / 256;
173 Mem[(memAddr + 1) & 0xffff] = value % 256;
174 modifiedBytes.insert(memAddr & 0xffff);
175 modifiedBytes.insert((memAddr + 1) & 0xffff);
176 }
177 }
178
179 void Sim::writeByteOprnd(Enu::EAddrMode addrMode, int value)
180 {
181 switch (addrMode) {
182 case Enu::NONE:
183 break;
184 case Enu::I:
185 // illegal
186 break;
187 case Enu::D:
188 writeByte(operandSpecifier, value);
189 break;
190 case Enu::N:
191 writeByte(readWord(operandSpecifier), value);
192 break;
193 case Enu::S:
194 writeByte(add(stackPointer, operandSpecifier), value);
195 break;
196 case Enu::SF:
197 writeByte(readWord(add(stackPointer, operandSpecifier)), value);
198 break;
199 case Enu::X:
200 writeByte(add(operandSpecifier, indexRegister), value);
201 break;
202 case Enu::SX:
203 writeByte(add(add(stackPointer, operandSpecifier), indexRegister), value);
204 break;
205 case Enu::SXF:
206 writeByte(add(readWord(add(stackPointer, operandSpecifier)), indexRegister), value);
207 break;
208 case Enu::ALL:
209 break;
210 }
211 }
212
213 void Sim::writeWordOprnd(Enu::EAddrMode addrMode, int value)
214 {
215 switch (addrMode) {
216 case Enu::NONE:
217 break;
218 case Enu::I:
219 // illegal
220 break;
221 case Enu::D:
222 writeWord(operandSpecifier, value);
223 break;
224 case Enu::N:
225 writeWord(readWord(operandSpecifier), value);
226 break;
227 case Enu::S:
228 writeWord(add(stackPointer, operandSpecifier), value);
229 break;
230 case Enu::SF:
231 writeWord(readWord(add(stackPointer, operandSpecifier)), value);
232 break;
233 case Enu::X:
234 writeWord(add(operandSpecifier, indexRegister), value);
235 break;
236 case Enu::SX:
237 writeWord(add(add(stackPointer, operandSpecifier), indexRegister), value);
238 break;
239 case Enu::SXF:
240 writeWord(add(readWord(add(stackPointer, operandSpecifier)), indexRegister), value);
241 break;
242 case Enu::ALL:
243 break;
244 }
245 }
246
247 int Sim::cellSize(Enu::ESymbolFormat symbolFormat)
248 {
249 switch (symbolFormat) {
250 case Enu::F_1C:
251 return 1;
252 case Enu::F_1D:
253 return 1;
254 case Enu::F_2D:
255 return 2;
256 case Enu::F_1H:
257 return 1;
258 case Enu::F_2H:
259 return 2;
260 default:
261 // Should not occur
262 return 0;
263 }
264 }
265
266 bool Sim::vonNeumannStep(QString &errorString)
267 {
268 modifiedBytes.clear();
269 EMnemonic mnemonic;
270 EAddrMode addrMode;
271 int temp;
272 bool bTemp;
273 // Fetch
274 instructionSpecifier = readByte(programCounter);
275 // Increment
276 programCounter = add(programCounter, 1);
277 // Decode
278 mnemonic = Pep::decodeMnemonic[instructionSpecifier];
279 addrMode = Pep::decodeAddrMode[Sim::instructionSpecifier];
280 if (!Pep::isUnaryMap[mnemonic]) {
281 operandSpecifier = readWord(programCounter);
282 programCounter = add(programCounter, 2);
283 }
284 // Execute
285 //qDebug() << Pep::enumToMnemonMap[Pep::decodeMnemonic[instructionSpecifier]];
286 if (!Pep::isUnaryMap[mnemonic] && !Pep::isTrapMap[mnemonic] && !(Pep::addrModesMap.value(mnemonic) & addrMode )) {
287 errorString = "Invalid Addressing Mode.";
288 return false;
289 }
290
291 switch (Pep::decodeMnemonic[instructionSpecifier]) {
292 case ADDA:
293 operand = readWordOprnd(addrMode);
294 operandDisplayFieldWidth = 4;
295 accumulator = addAndSetNZVC(accumulator, operand);
296 return true;
297 case ADDSP:
298 operand = readWordOprnd(addrMode);
299 operandDisplayFieldWidth = 4;
300 stackPointer = addAndSetNZVC(stackPointer, operand);
301 return true;
302 case ADDX:
303 operand = readWordOprnd(addrMode);
304 operandDisplayFieldWidth = 4;
305 indexRegister = addAndSetNZVC(indexRegister, operand);
306 return true;
307 case ANDA:
308 operand = readWordOprnd(addrMode);
309 operandDisplayFieldWidth = 4;
310 accumulator = accumulator & operand;
311 nBit = accumulator > 32768;
312 zBit = accumulator == 0;
313 return true;
314 case ANDX:
315 operand = readWordOprnd(addrMode);
316 operandDisplayFieldWidth = 4;
317 indexRegister = indexRegister & operand;
318 nBit = indexRegister > 32768;
319 zBit = indexRegister == 0;
320 return true;
321 case ASLA:
322 vBit = (accumulator >= 0x4000 && accumulator < 0x8000) || // prefix is 01 (bin)
323 (accumulator >= 0x8000 && accumulator < 0xC000); // prefix is 10 (bin)
324 accumulator *= 2;
325 if (accumulator >= 65536) {
326 cBit = 1;
327 accumulator = accumulator & 0xffff;
328 }
329 else {
330 cBit = 0;
331 }
332 nBit = accumulator >= 32768;
333 zBit = accumulator == 0;
334 return true;
335 case ASLX:
336 vBit = (indexRegister >= 0x4000 && indexRegister < 0x8000) || // prefix is 01 (bin)
337 (indexRegister >= 0x8000 && indexRegister < 0xC000); // prefix is 10 (bin)
338 indexRegister *= 2;
339 if (indexRegister >= 65536) {
340 cBit = 1;
341 indexRegister = indexRegister & 0xffff;
342 }
343 else {
344 cBit = 0;
345 }
346 nBit = indexRegister >= 32768;
347 zBit = indexRegister == 0;
348 return true;
349 case ASRA:
350 cBit = (accumulator % 2) == 1;
351 if (accumulator < 32768) {
352 accumulator /= 2;
353 }
354 else {
355 accumulator = accumulator / 2 + 32768;
356 }
357 nBit = accumulator >= 32768;
358 zBit = accumulator == 0;
359 return true;
360 case ASRX:
361 cBit = (indexRegister % 2) == 1;
362 if (indexRegister < 32768) {
363 indexRegister /= 2;
364 }
365 else {
366 indexRegister = indexRegister / 2 + 32768;
367 }
368 nBit = indexRegister >= 32768;
369 zBit = indexRegister == 0;
370 return true;
371 case BR:
372 operand = readWordOprnd(addrMode);
373 operandDisplayFieldWidth = 4;
374 programCounter = operand;
375 return true;
376 case BRC:
377 operand = readWordOprnd(addrMode);
378 operandDisplayFieldWidth = 4;
379 if (cBit) {
380 programCounter = operand;
381 }
382 return true;
383 case BREQ:
384 operand = readWordOprnd(addrMode);
385 operandDisplayFieldWidth = 4;
386 if (zBit) {
387 programCounter = operand;
388 }
389 return true;
390 case BRGE:
391 operand = readWordOprnd(addrMode);
392 operandDisplayFieldWidth = 4;
393 if (!nBit) {
394 programCounter = operand;
395 }
396 return true;
397 case BRGT:
398 operand = readWordOprnd(addrMode);
399 operandDisplayFieldWidth = 4;
400 if (!nBit && !zBit) {
401 programCounter = operand;
402 }
403 return true;
404 case BRLE:
405 operand = readWordOprnd(addrMode);
406 operandDisplayFieldWidth = 4;
407 if (nBit || zBit) {
408 programCounter = operand;
409 }
410 return true;
411 case BRLT:
412 operand = readWordOprnd(addrMode);
413 operandDisplayFieldWidth = 4;
414 if (nBit) {
415 programCounter = operand;
416 }
417 return true;
418 case BRNE:
419 operand = readWordOprnd(addrMode);
420 operandDisplayFieldWidth = 4;
421 if (!zBit) {
422 programCounter = operand;
423 }
424 return true;
425 case BRV:
426 operand = readWordOprnd(addrMode);
427 operandDisplayFieldWidth = 4;
428 if (vBit) {
429 programCounter = operand;
430 }
431 return true;
432 case CALL:
433 operand = readWordOprnd(addrMode);
434 operandDisplayFieldWidth = 4;
435 stackPointer = add(stackPointer, 65534); // SP <- SP - 2
436 writeWord(stackPointer, programCounter); // Mem[SP] <- PC
437 programCounter = operand; // PC <- Oprnd
438 return true;
439 case CHARI:
440 if (Sim::inputBuffer.size() != 0) {
441 QString ch = Sim::inputBuffer.left(1);
442 Sim::inputBuffer.remove(0, 1);
443 int value = QChar(ch[0]).toLatin1();
444 value += value < 0 ? 256 : 0;
445 Sim::writeByteOprnd(addrMode, value);
446 operand = readByteOprnd(addrMode);
447 operandDisplayFieldWidth = 2;
448 }
449 else {
450 Sim::writeByteOprnd(addrMode, 0);
451 operand = readByteOprnd(addrMode);
452 operandDisplayFieldWidth = 2;
453 // errorString = "Error: Attempt to read past end of input.";
454 // return false;
455 }
456 return true;
457 case CHARO:
458 operand = readByteOprnd(addrMode);
459 operandDisplayFieldWidth = 2;
460 Sim::outputBuffer = QString(operand);
461 return true;
462 case CPA:
463 operand = readWordOprnd(addrMode);
464 operandDisplayFieldWidth = 4;
465 addAndSetNZVC(accumulator, (~operand + 1) & 0xffff);
466 if (vBit) { // Extend compare range. nBit and zBit are not adjusted in subtract instructions.
467 nBit = !nBit;
468 }
469 return true;
470 case CPX:
471 operand = readWordOprnd(addrMode);
472 operandDisplayFieldWidth = 4;
473 addAndSetNZVC(indexRegister, (~operand + 1) & 0xffff);
474 if (vBit) { // Extend compare range. nBit and zBit are not adjusted in subtract instructions.
475 nBit = !nBit;
476 }
477 return true;
478 case DECI: case DECO: case STRO:
479 case NOP: case NOP0: case NOP1: case NOP2: case NOP3:
480 temp = readWord(Pep::dotBurnArgument - 5);
481 writeByte(temp - 1, instructionSpecifier);
482 writeWord(temp - 3, stackPointer);
483 writeWord(temp - 5, programCounter);
484 writeWord(temp - 7, indexRegister);
485 writeWord(temp - 9, accumulator);
486 writeByte(temp - 10, nzvcToInt());
487 stackPointer = temp - 10;
488 programCounter = readWord(Pep::dotBurnArgument - 1);
489 return true;
490 case LDA:
491 operand = readWordOprnd(addrMode);
492 operandDisplayFieldWidth = 4;
493 accumulator = operand & 0xffff;
494 nBit = accumulator >= 32768;
495 zBit = accumulator == 0;
496 return true;
497 case LDBYTEA:
498 operand = readByteOprnd(addrMode);
499 operandDisplayFieldWidth = 2;
500 accumulator = accumulator & 0xff00;
501 accumulator |= operand & 255;
502 nBit = accumulator >= 32768;
503 zBit = accumulator == 0;
504 return true;
505 case LDBYTEX:
506 operand = readByteOprnd(addrMode);
507 operandDisplayFieldWidth = 2;
508 indexRegister = indexRegister & 0xff00;
509 indexRegister |= operand & 255;
510 nBit = indexRegister >= 32768;
511 zBit = indexRegister == 0;
512 return true;
513 case LDX:
514 operand = readWordOprnd(addrMode);
515 operandDisplayFieldWidth = 4;
516 indexRegister = operand & 0xffff;
517 nBit = indexRegister >= 32768;
518 zBit = indexRegister == 0;
519 return true;
520 case MOVFLGA:
521 accumulator = 0;
522 accumulator |= cBit ? 1 : 0;
523 accumulator |= vBit ? 2 : 0;
524 accumulator |= zBit ? 4 : 0;
525 accumulator |= nBit ? 8 : 0;
526 return true;
527 case MOVSPA:
528 accumulator = stackPointer;
529 return true;
530 case NEGA:
531 accumulator = (~accumulator + 1) & 0xffff;
532 nBit = accumulator >= 32768;
533 zBit = accumulator == 0;
534 vBit = accumulator == 32768;
535 return true;
536 case NEGX:
537 indexRegister = (~indexRegister + 1) & 0xffff;
538 nBit = indexRegister >= 32768;
539 zBit = indexRegister == 0;
540 vBit = indexRegister == 32768;
541 return true;
542 case NOTA:
543 accumulator = ~accumulator & 0xffff;
544 nBit = accumulator >= 32768;
545 zBit = accumulator == 0;
546 return true;
547 case NOTX:
548 indexRegister = ~indexRegister & 0xffff;
549 nBit = indexRegister >= 32768;
550 zBit = indexRegister == 0;
551 return true;
552 case ORA:
553 operand = readWordOprnd(addrMode);
554 operandDisplayFieldWidth = 4;
555 accumulator = accumulator | operand;
556 nBit = accumulator > 32768;
557 zBit = accumulator == 0;
558 return true;
559 case ORX:
560 operand = readWordOprnd(addrMode);
561 operandDisplayFieldWidth = 4;
562 indexRegister = indexRegister | operand;
563 nBit = indexRegister > 32768;
564 zBit = indexRegister == 0;
565 return true;
566 case RET0:
567 programCounter = readWord(stackPointer); // PC <- Mem[SP]
568 stackPointer = add(stackPointer, 2); // SP <- SP + 2
569 return true;
570 case RET1:
571 stackPointer = add(stackPointer, 1); // SP <- SP + 1
572 programCounter = readWord(stackPointer); // PC <- Mem[SP]
573 stackPointer = add(stackPointer, 2); // SP <- SP + 2
574 return true;
575 case RET2:
576 stackPointer = add(stackPointer, 2); // SP <- SP + 2
577 programCounter = readWord(stackPointer); // PC <- Mem[SP]
578 stackPointer = add(stackPointer, 2); // SP <- SP + 2
579 return true;
580 case RET3:
581 stackPointer = add(stackPointer, 3); // SP <- SP + 3
582 programCounter = readWord(stackPointer); // PC <- Mem[SP]
583 stackPointer = add(stackPointer, 2); // SP <- SP + 2
584 return true;
585 case RET4:
586 stackPointer = add(stackPointer, 4); // SP <- SP + 4
587 programCounter = readWord(stackPointer); // PC <- Mem[SP]
588 stackPointer = add(stackPointer, 2); // SP <- SP + 2
589 return true;
590 case RET5:
591 stackPointer = add(stackPointer, 5); // SP <- SP + 5
592 programCounter = readWord(stackPointer); // PC <- Mem[SP]
593 stackPointer = add(stackPointer, 2); // SP <- SP + 2
594 return true;
595 case RET6:
596 stackPointer = add(stackPointer, 6); // SP <- SP + 6
597 programCounter = readWord(stackPointer); // PC <- Mem[SP]
598 stackPointer = add(stackPointer, 2); // SP <- SP + 2
599 return true;
600 case RET7:
601 stackPointer = add(stackPointer, 7); // SP <- SP + 7
602 programCounter = readWord(stackPointer); // PC <- Mem[SP]
603 stackPointer = add(stackPointer, 2); // SP <- SP + 2
604 return true;
605 case RETTR:
606 temp = readByte(stackPointer);
607 nBit = (temp & 8) != 0;
608 zBit = (temp & 4) != 0;
609 vBit = (temp & 2) != 0;
610 cBit = (temp & 1) != 0;
611 accumulator = readWord(stackPointer + 1);
612 indexRegister = readWord(stackPointer + 3);
613 programCounter = readWord(stackPointer + 5);
614 stackPointer = readWord(stackPointer + 7);
615 return true;
616 case ROLA:
617 bTemp = accumulator >= 32768;
618 accumulator = (accumulator * 2) & 0xffff;
619 accumulator |= cBit ? 1 : 0;
620 cBit = bTemp;
621 return true;
622 case ROLX:
623 bTemp = indexRegister >= 32768;
624 indexRegister = (indexRegister * 2) & 0xffff;
625 indexRegister |= cBit ? 1 : 0;
626 cBit = bTemp;
627 return true;
628 case RORA:
629 bTemp = accumulator % 2 == 1;
630 accumulator = (accumulator / 2);
631 accumulator |= cBit ? 0x8000 : 0;
632 cBit = bTemp;
633 return true;
634 case RORX:
635 bTemp = indexRegister % 2 == 1;
636 indexRegister = (indexRegister / 2);
637 indexRegister |= cBit ? 0x8000 : 0;
638 cBit = bTemp;
639 return true;
640 case STA:
641 writeWordOprnd(addrMode, accumulator);
642 operand = readWordOprnd(addrMode);
643 operandDisplayFieldWidth = 4;
644 return true;
645 case STBYTEA:
646 writeByteOprnd(addrMode, accumulator & 0x00ff);
647 operand = readByteOprnd(addrMode);
648 operandDisplayFieldWidth = 2;
649 return true;
650 case STBYTEX:
651 writeByteOprnd(addrMode, indexRegister & 0x00ff);
652 operand = readByteOprnd(addrMode);
653 operandDisplayFieldWidth = 2;
654 return true;
655 case STOP:
656 return true;
657 case STX:
658 writeWordOprnd(addrMode, indexRegister);
659 operand = readWordOprnd(addrMode);
660 operandDisplayFieldWidth = 4;
661 return true;
662 case SUBA:
663 operand = readWordOprnd(addrMode);
664 operandDisplayFieldWidth = 4;
665 accumulator = addAndSetNZVC(accumulator, (~operand + 1) & 0xffff);
666 return true;
667 case SUBSP:
668 operand = readWordOprnd(addrMode);
669 operandDisplayFieldWidth = 4;
670 stackPointer = addAndSetNZVC(stackPointer, (~operand + 1) & 0xffff);
671 return true;
672 case SUBX:
673 operand = readWordOprnd(addrMode);
674 operandDisplayFieldWidth = 4;
675 indexRegister = addAndSetNZVC(indexRegister, (~operand + 1) & 0xffff);
676 return true;
677 default:
678 return false;
679 }
680 return false;
681 }