View Javadoc
1 /* 2 * MythSim 3 * 4 * Copyright (C) 2002-2004 Jason Vroustouris <jasonv@jasonv.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 21 package org.mythsim.core; 22 23 import java.io.*; 24 25 /*** 26 * The CPU Simulator. 27 * @author Jason Vroustouris 28 * @todo Change next opcode into a breakpoint system. 29 */ 30 public class MythSim { 31 32 /*** The curent name and version number. */ 33 public static final String version_title = "MythSim 3.1.1"; 34 35 /*** 36 * Creates a MythSim simulator. 37 */ 38 public MythSim() { 39 System.out.print( 40 version_title + " - http://www.mythsim.org/\n" + 41 "\n" + 42 "Copyright (C) 2002-2004 Jason Vroustouris <jasonv@jasonv.com>\n" + 43 "\n" + 44 "This program is free software; you can redistribute it and/or modify\n" + 45 "it under the terms of the GNU General Public License as published by\n" + 46 "the Free Software Foundation; either version 2 of the License, or\n" + 47 "(at your option) any later version.\n" + 48 "\n" + 49 "This program is distributed in the hope that it will be useful,\n" + 50 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + 51 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + 52 "GNU General Public License for more details.\n" + 53 "\n" + 54 "You should have received a copy of the GNU General Public License\n" + 55 "along with this program; if not, write to the Free Software\n" + 56 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"); 57 58 } 59 /* ****************************************************** 60 Setup (constructors and variables) 61 ****************************************************** */ 62 63 /*** The status array. */ 64 int s[] = new int[MythSim.LENGTH]; 65 66 /*** The previous status array. */ 67 int p[] = new int[MythSim.LENGTH]; 68 69 /*** The message string. */ 70 String message = ""; 71 72 /*** The micoprogram in array form. */ 73 int _ucode[][] = new int[1][MythSim.CONTROL_WORD_LENGTH]; 74 75 /*** The memory source file. */ 76 File memoryFile = new File("."); 77 String memorySource = ""; 78 79 /*** The microcode source file. */ 80 File microcodeFile = new File("."); 81 String microcodeSource = ""; 82 83 /*** Sets the Microcode File */ 84 public void setFileMicrocode(File file) throws FileNotFoundException { 85 microcodeFile = file; 86 FileReader fr = new FileReader(microcodeFile); 87 try { 88 fr.close(); 89 } 90 catch (IOException ex) { 91 System.out.print("Microcode file cant be closed."); 92 } 93 } 94 95 /*** Sets the Memory File */ 96 public void setFileMemory(File file) throws FileNotFoundException { 97 memoryFile = file; 98 FileReader fr = new FileReader(memoryFile); 99 try { 100 fr.close(); 101 } 102 catch (IOException ex) { 103 System.out.print("Memory file cant be closed."); 104 } 105 } 106 107 MythParser mp = new MythParser(); 108 109 /* ****************************************************** 110 Infomation 111 ****************************************************** */ 112 113 /*** @return memory source file in string */ 114 public String getMemorySource() { 115 return memorySource; 116 } 117 118 /*** @return microcode source file in string */ 119 public String getMicrocodeSource() { 120 return microcodeSource; 121 } 122 123 /*** @return memory source file path */ 124 public File getMemoryFile() { 125 return memoryFile; 126 } 127 128 /*** @return microcode source file path */ 129 public File getMicrocodeFile() { 130 return microcodeFile; 131 } 132 133 /*** @return memory source file path */ 134 public String getMemoryFilePath() { 135 return memoryFile.getAbsolutePath(); 136 } 137 138 /*** @return microcode source file path */ 139 public String getMicrocodeFilePath() { 140 return microcodeFile.getAbsolutePath(); 141 } 142 143 /*** @return memory source file name */ 144 public String getMemoryFileName() { 145 return memoryFile.getName(); 146 } 147 148 /*** @return microcode source file name */ 149 public String getMicrocodeFileName() { 150 return microcodeFile.getName(); 151 } 152 153 /* ****************************************************** 154 Status Array (constants and functions) 155 ****************************************************** */ 156 /* ====================================================== 157 Registers 158 ===================================================== */ 159 /*** 160 * <b>Register:</b> 0. 161 * <p>Location in the status array for the value in Register 0.</p> 162 * */ 163 public static final int R_0 = 0; 164 /*** 165 * <b>Register:</b> 1. 166 * <p>Location in the status array for the value in Register 1.</p> 167 * */ 168 public static final int R_1 = 1; 169 /*** 170 * <b>Register:</b> 2. 171 * <p>Location in the status array for the value in Register 2.</p> 172 * */ 173 public static final int R_2 = 2; 174 /*** 175 * <b>Register:</b> 3. 176 * <p>Location in the status array for the value in Register 3.</p> 177 * */ 178 public static final int R_3 = 3; 179 /*** 180 * <b>Register:</b> 4. 181 * <p>Location in the status array for the value in Register 4.</p> 182 * */ 183 public static final int R_4 = 4; 184 /*** 185 * <b>Register:</b> 5. 186 * <p>Location in the status array for the value in Register 5.</p> 187 * */ 188 public static final int R_5 = 5; 189 /*** 190 * <b>Register:</b> 6. 191 * <p>Location in the status array for the value in Register 6.</p> 192 * */ 193 public static final int R_6 = 6; 194 /*** 195 * <b>Register:</b> 7. 196 * <p>Location in the status array for the value in Register 7.</p> 197 * */ 198 public static final int R_7 = 7; 199 /*** 200 * Register: Instruction Register 0. 201 * <p>Location in the status array for the value in 202 * Instruction Register 0.</p> 203 * */ 204 public static final int IR_0 = 8; 205 /*** 206 * Register: Instruction Register 1. 207 * <p>Location in the status array for the value in Instruction 208 * Register 1.</p> 209 * */ 210 public static final int IR_1 = 9; 211 /*** 212 * Register: Memory Data Register. 213 * <p>Location in the status array for the value in Memory Data 214 * Register.</p> 215 * */ 216 public static final int MDR = 10; 217 /*** 218 * Register: Memory Address Register. 219 * <p>Location in the status array for the value in Memory Address 220 * Register.</p> 221 * */ 222 public static final int MAR = 11; 223 /* ====================================================== 224 Busses 225 ===================================================== */ 226 /*** 227 * <b>Bus:</b> ALU (Arithmetic Logic Unit) Output Bus. 228 * <p>Location in the status array for the value in alu_bus. 229 * Register.</p> 230 * */ 231 public static final int ALU_BUS = 12; 232 /*** 233 * <b>Bus:</b> ALU (Arithmetic Logic Unit) Input A Bus. 234 * <p>Location in the status array for the value in a_bus. 235 * Register.</p> 236 * */ 237 public static final int A_BUS = 13; 238 /*** 239 * <b>Bus:</b> ALU (Arithmetic Logic Unit) Input B Bus. 240 * <p>Location in the status array for the value in b_bus. 241 * Register.</p> 242 * */ 243 public static final int B_BUS = 14; 244 /*** 245 * <b>Bus:</b> Result Bus. 246 * <p>Location in the status array for the value in result_bus. 247 * Register.</p> 248 * */ 249 public static final int RESULT_BUS = 15; 250 /*** 251 * <b>Bus:</b> Write Bus. 252 * <p>Location in the status array for the value in write_bus. 253 * Register.</p> 254 * */ 255 public static final int WRITE_BUS = 16; 256 /*** 257 * <b>Bus:</b> Memory Bus. 258 * <p>Location in the status array for the value in mem_bus. 259 * Register.</p> 260 * */ 261 public static final int MEMORY_BUS = 17; 262 /* ====================================================== 263 Control Signals 264 ===================================================== */ 265 /*** 266 * <b>Control Signal:</b> Stores the result_bus value into r_0. 267 * <p>Stores the index of this control signal.</p> 268 * */ 269 public static final int R0_WRITE = 18; 270 /*** 271 * <b>Control Signal:</b> Stores the result_bus value into r_1. 272 * <p>Stores the index of this control signal.</p> 273 * */ 274 public static final int R1_WRITE = 19; 275 /*** 276 * <b>Control Signal:</b> Stores the result_bus value into r_2. 277 * <p>Stores the index of this control signal.</p> 278 * */ 279 public static final int R2_WRITE = 20; 280 /*** 281 * <b>Control Signal:</b> Stores the result_bus value into r_3. 282 * <p>Stores the index of this control signal.</p> 283 * */ 284 public static final int R3_WRITE = 21; 285 /*** 286 * <b>Control Signal:</b> Stores the result_bus value into r_4. 287 * <p>Stores the index of this control signal.</p> 288 * */ 289 public static final int R4_WRITE = 22; 290 /*** 291 * <b>Control Signal:</b> Stores the result_bus value into r_5. 292 * <p>Stores the index of this control signal.</p> 293 * */ 294 public static final int R5_WRITE = 23; 295 /*** 296 * <b>Control Signal:</b> Stores the result_bus value into r_6. 297 * <p>Stores the index of this control signal.</p> 298 * */ 299 public static final int R6_WRITE = 24; 300 /*** 301 * <b>Control Signal:</b> Stores the result_bus value into r_7. 302 * <p>Stores the index of this control signal.</p> 303 * */ 304 public static final int R7_WRITE = 25; 305 /*** 306 * <b>Control Signal:</b> Places the value in register n(0-7) on the a_bus. 307 * <p>Stores the index of this control signal.</p> 308 * */ 309 public static final int A_SEL = 26; 310 /*** 311 * <b>Control Signal:</b> Places the value in register n(0-7) on the b_bus. 312 * <p>Stores the index of this control signal.</p> 313 * */ 314 public static final int B_SEL = 27; 315 /*** 316 * <b>Control Signal:</b> Stores result_bus value in the register defined in ir_ri. 317 * <p>Stores the index of this control signal.</p> 318 * */ 319 public static final int RI_SEL = 28; 320 /*** 321 * <b>Control Signal:</b> Places the value in register defined by ir_rj on the a_bus. 322 * <p>Stores the index of this control signal.</p> 323 * */ 324 public static final int RJ_SEL = 29; 325 /*** 326 * <b>Control Signal:</b> Places the value in register defined by ir_rk on the a_bus. 327 * <p>Stores the index of this control signal.</p> 328 * */ 329 public static final int RK_SEL = 30; 330 /*** 331 * <b>Control Signal:</b> Sets the c_in to 1 for use by ALU operations. 332 * <p>Stores the status array index of this control signal.</p> 333 */ 334 public static final int C_IN = 31; 335 /*** 336 * <b>Control Signal:</b> Sets the ALU operation. 337 * <p>Stores the status array index of this control signal.</p> 338 * @see #ALU_NOT 339 * @see #ALU_OR 340 * @see #ALU_AND 341 * @see #ALU_XOR 342 * @see #ALU_ADD 343 * @see #ALU_SUB 344 * @see #ALU_ADDA 345 * @see #ALU_SUBA 346 */ 347 public static final int ALU_SEL = 32; 348 /*** 349 * <b>Control Signal:</b> Sets Memory Data Register (MAR). 350 * <p>Stores the status array index of this control signal.</p> 351 * @see #MDR 352 */ 353 public static final int MDR_SEL = 33; 354 /*** 355 * <b>Control Signal:</b> Sets Memory Address Register (MDR). 356 * <p>Stores the status array index of this control signal.</p> 357 * @see #MAR 358 */ 359 public static final int MAR_SEL = 34; 360 /*** 361 * <b>Control Signal:</b> Selects value for result_bus. 362 * <p>Stores the status array index of this control signal.</p> 363 * @see #RESULT_ALU 364 * @see #RESULT_MDR 365 * @see #RESULT_IR_CONST4 366 * @see #RESULT_IR_CONST8 367 */ 368 public static final int RESULT_SEL = 35; 369 /*** 370 * <b>Control Signal:</b> Sets Instruction Register 0 (ir_0). 371 * <p>Stores the status array index of this control signal.</p> 372 * @see #IR_0 373 */ 374 public static final int IR0_SEL = 36; 375 /*** 376 * <b>Control Signal:</b> Sets Instruction Register 1 (ir_1). 377 * <p>Wires: 1<br> 378 * Values: 0 to 1 </p> 379 * <p>Stores the status array index of this control signal.</p> 380 * @see #IR_1 381 */ 382 public static final int IR1_SEL = 37; 383 /*** 384 * <b>Control Signal:</b> Read from memory. 385 * <p>Wires: 1<br> 386 * Values: 0 to 1 </p> 387 * <p>Stores the status array index of this control signal.</p> 388 */ 389 public static final int READ = 38; 390 /*** 391 * <b>Control Signal:</b> Write to memory. 392 * <p>Wires: 1<br> 393 * Values: 0 to 1 </p> 394 * <p>Stores the status array index of this control signal.</p> 395 */ 396 public static final int WRITE = 39; 397 /*** 398 * <b>Control Signal:</b> Add the opcode to the next address. 399 * <p>Wires: 1<br> 400 * Values: 0 to 1 </p> 401 * <p>Stores the status array index of this control signal.</p> 402 */ 403 public static final int INDEX_SEL = 40; 404 /*** 405 * <b>Control Signal:</b> Select the argument for the if statement. 406 * <p>Wires: 2<br> 407 * Values: 0 to 3 </p> 408 * <p>Stores the status array index of this control signal.</p> 409 * @see #COND_M7 410 * @see #COND_C_OUT 411 * @see #COND_V 412 * @see #COND_WAIT 413 */ 414 public static final int COND = 41; 415 /*** 416 * <b>Control Signal:</b>. 417 * <p>Stores the status array index of this control signal.</p> 418 */ 419 public static final int ADDRESS_TRUE = 42; 420 /*** 421 * <b>Control Signal:</b> . 422 * <p>Stores the status array index of this control signal.</p> 423 */ 424 public static final int ADDRESS_FALSE = 43; 425 /*** 426 * <b>Control Signal: (not used) </b> . 427 * <p>Use CURRENT_ADDESS </p> 428 */ 429 public static final int ADDRESS = 44; 430 431 public static final int R0_WRITE_SET = 45; 432 public static final int R1_WRITE_SET = 46; 433 public static final int R2_WRITE_SET = 47; 434 public static final int R3_WRITE_SET = 48; 435 public static final int R4_WRITE_SET = 49; 436 public static final int R5_WRITE_SET = 50; 437 public static final int R6_WRITE_SET = 51; 438 public static final int R7_WRITE_SET = 52; 439 public static final int A_SEL_SET = 53; 440 public static final int B_SEL_SET = 54; 441 public static final int RI_SEL_SET = 55; 442 public static final int RJ_SEL_SET = 56; 443 public static final int RK_SEL_SET = 57; 444 public static final int C_IN_SET = 58; 445 public static final int ALU_SEL_SET = 59; 446 public static final int MDR_SEL_SET = 60; 447 public static final int MAR_SEL_SET = 61; 448 public static final int RESULT_SEL_SET = 62; 449 public static final int IR0_SEL_SET = 63; 450 public static final int IR1_SEL_SET = 64; 451 public static final int READ_SET = 65; 452 public static final int WRITE_SET = 66; 453 public static final int INDEX_SEL_SET = 67; 454 public static final int COND_SET = 68; 455 public static final int ADDRESS_TRUE_SET = 69; 456 public static final int ADDRESS_FALSE_SET = 70; 457 public static final int ADDRESS_SET = 71; 458 459 /* Status Signals */ 460 /*** 461 * <b>Status Signal:</b> Carry bit from the ALU. 462 * <p>Stores the status array index of this status signal.</p> 463 */ 464 public static final int C_OUT = 72; 465 /*** 466 * <b>Status Signal:</b> Most significant bit from alu_bus. 467 * <p>Stores the status array index of this status signal.</p> 468 */ 469 public static final int M_7 = 73; 470 /*** 471 * <b>Status Signal:</b> Overflow. 472 * <p>Stores the status array index of this status signal.</p> 473 */ 474 public static final int V = 74; 475 /*** 476 * <b>Status Signal:</b> Wait. 477 * <p>Stores the status array index of this status signal.</p> 478 */ 479 public static final int WAIT = 75; 480 481 public static final int CURRENT_ADDRESS = 76; 482 /*** 483 * The system clock. 484 */ 485 public static final int CLOCK = 77; 486 public static final int LOCKED = 78; 487 488 /* ====================================================== 489 Virtual Buses 490 ===================================================== */ 491 /*** 492 * <b>Virtual Bus:</b> Places the value in register n(0-7) on the a_bus. 493 * <p>Wires: 3<br> 494 * Values: 0 to 7 </p> 495 * <p>Stores the status array index of this control signal.</p> 496 */ 497 public static final int VA_SEL_BUS = 79; 498 /*** 499 * <b>Virtual Bus:</b> Places the value in register n(0-7) on the b_bus. 500 * <p>Wires: 3<br> 501 * Values: 0 to 7 </p> 502 * <p>Stores the status array index of this control signal.</p> 503 */ 504 public static final int VB_SEL_BUS = 80; 505 /*** 506 * <b>Virtual Control Signal:</b> Stores the result_bus value into r_0. 507 * <p>Wires: 1<br> 508 * Values: 0 or 1 </p> 509 * <p>Stores the status array index of this control signal.</p> 510 */ 511 public static final int VR0_WRITE_BUS = 81; 512 /*** 513 * <b>Virtual Control Signal:</b> Stores the result_bus value into r_1. 514 * <p>Wires: 1<br> 515 * Values: 0 or 1 </p> 516 * <p>Stores the status array index of this control signal.</p> 517 */ 518 public static final int VR1_WRITE_BUS = 82; 519 /*** 520 * <b>Virtual Control Signal:</b> Stores the result_bus value into r_2. 521 * <p>Wires: 1<br> 522 * Values: 0 or 1 </p> 523 * <p>Stores the status array index of this control signal.</p> 524 */ 525 public static final int VR2_WRITE_BUS = 83; 526 /*** 527 * <b>Virtual Control Signal:</b> Stores the result_bus value into r_3. 528 * <p>Wires: 1<br> 529 * Values: 0 or 1 </p> 530 * <p>Stores the status array index of this control signal.</p> 531 */ 532 public static final int VR3_WRITE_BUS = 84; 533 public static final int STATUS_ARRAY_LENGTH = 85; 534 535 /* Main Values */ 536 public static final int MAIN_MEMORY = 256; 537 538 /* Lengths */ 539 public static final int LENGTH = 512; 540 public static final int CONTROL_WORD_START = 18; 541 public static final int CONTROL_WORD_END = 71; 542 public static final int CONTROL_WORD_LENGTH = 54; 543 544 /* ====================================================== 545 File Modes 546 ===================================================== */ 547 public static final int NO_FILES_MODE = 0; 548 public static final int INVALID_FILES_MODE = 1; 549 public static final int VALID_FILES_MODE = 2; 550 protected int mode = NO_FILES_MODE; 551 552 /* Functions for Geting Status Array Values */ 553 /*** @return the current status array */ 554 public int[] getStatus() { 555 return s; 556 } 557 558 /*** @return the current status array */ 559 public int[] getPreviousStatus() { 560 return p; 561 } 562 563 /*** @return the current status array element n */ 564 public int getStatus(int index) { 565 return s[index]; 566 } 567 568 /*** @return the diplay name of status array element n */ 569 public static String name(int i) { 570 return displayNames[i]; 571 } 572 573 /*** @return the official name of status array element n */ 574 public static String funcName(int i) { 575 if (i >= 0 && i <= 7) { 576 return aluFuncs[i]; 577 } 578 else { 579 return "n/a"; 580 } 581 } 582 583 /*** @return true when current_address is zero*/ 584 public boolean ucodeZero() { 585 return s[MythSim.INDEX_SEL] == 1; 586 } 587 588 /*** @return the value of a location in memory */ 589 private int getMem(int index) throws MythError { 590 if (index > 255 || index < 0) { 591 throw new MythError(5, s, index); 592 } 593 return s[MythSim.MAIN_MEMORY + index]; 594 } 595 596 /*** set the value of a location in memory */ 597 private void setMem(int index, int value) throws MythError { 598 if (index > 255 || index < 0) { 599 throw new MythError(5, s, index); 600 } 601 s[MythSim.MAIN_MEMORY + index] = value; 602 } 603 604 /*** official names of status array elements */ 605 private static final String[] lineNames = { 606 "R_0", 607 "R_1", "R_2", "R_3", "R_4", "R_5", "R_6", "R_7", 608 "IR_0", "IR_1", 609 "MDR", "MAR", 610 "ALU_BUS", "A_BUS", "B_BUS", "RESULT_BUS", "WRITE_BUS", "MEMORY_BUS", 611 "R0_WRITE", "R1_WRITE", "R2_WRITE", "R3_WRITE", 612 "R4_WRITE", "R5_WRITE", "R6_WRITE", "R7_WRITE", 613 "A_SEL", "B_SEL", 614 "RI_SEL", "RJ_SEL", "RK_SEL", 615 "C_IN", 616 "ALU_SEL", "MDR_SEL", "MAR_SEL", "RESULT_SEL", "IR0_SEL", "IR1_SEL", 617 "READ", "WRITE", "INDEX_SEL", "COND", "ADDRESS_TRUE", "ADDRESS_FALSE", 618 "ADDRESS", 619 "R0_WRITE_SET", "R1_WRITE_SET", "R2_WRITE_SET", "R3_WRITE_SET", 620 "R4_WRITE_SET", "R5_WRITE_SET", "R6_WRITE_SET", "R7_WRITE_SET", 621 "A_SEL_SET", "B_SEL_SET", 622 "RI_SEL_SET", "RJ_SEL_SET", "RK_SEL_SET", 623 "C_IN_SET", 624 "ALU_SEL_SET", "MDR_SEL_SET", "MAR_SEL_SET", "RESULT_SEL_SET", 625 "IR0_SEL_SET", "IR1_SEL_SET", 626 "READ_SET", "WRITE_SET", "INDEX_SEL_SET", "COND_SET", "ADDRESS_TRUE_SET", 627 "ADDRESS_FALSE_SET", "ADDRESS_SET", 628 "C_OUT", "M_7", "V", "WAIT", "CURRENT_ADDRESS", "CLOCK", "STOPPED", 629 "VA_SEL_BUS", 630 "VB_SEL_BUS", "VR0_WRITE_BUS", "VR1_WRITE_BUS", "VR2_WRITE_BUS", 631 "VR3_WRITE_BUS"}; 632 633 /*** display names of status array elements */ 634 private static final String[] displayNames = { 635 "R_0", 636 "R_1", "R_2", "R_3", "R_4", "R_5", "R_6", "R_7", 637 "ir0", "ir1", 638 "mdr", "mar", 639 "ALUbus", 640 "Abus", "Bbus", 641 "result", "WRITE_BUS", "MEMORY_BUS", 642 "R0_WRITE", "R1_WRITE", "R2_WRITE", "R3_WRITE", "r4write", 643 "r5write", "r6write", "r7write", "A_SEL", 644 "B_SEL", "RI_SEL", "RJ_SEL", "RK_SEL", 645 "Cin", "ALUsel", "MDRsel", "MARsel", "ResultSel", 646 "ir0sel", "ir1sel", 647 "read", "write", "INDEX_SEL", "COND", 648 "ADDRESS_TRUE", "ADDRESS_FALSE", "ADDRESS", 649 "R0_WRITE_set", "R1_WRITE_set", "R2_WRITE_set", "R3_WRITE_set", 650 "r4write_set", 651 "r5write_set", "r6write_set", "r7write_set", "A_SEL_set", 652 "B_SEL_set", "RI_SEL_set", "RJ_SEL_set", "RK_SEL_set", 653 "Cin_set", "ALUsel_set", "MDRsel_set", "MARsel_set", "ResultSel_set", 654 "ir0sel_set", "ir1sel_set", 655 "read_set", "write_set", "INDEX_SEL_set", "COND_set", 656 "ADDRESS_TRUE_set", "ADDRESS_FALSE_set", "ADDRESS_set", 657 "Cout", "M7", "v", "wait", 658 "CURRENT_ADDRESS", "CLOCK", "STOPPED", "vAsel", "vBsel", 659 "vr0write", "vr1write", "vr2write", "vr3write"}; 660 661 public int getRegA() { 662 if (s[VA_SEL_BUS] >= 0 && s[VA_SEL_BUS] <= 7) { 663 return s[s[VA_SEL_BUS]]; 664 } 665 else { 666 return -1; 667 } 668 } 669 670 public int getRegB() { 671 if (s[VB_SEL_BUS] >= 0 && s[VB_SEL_BUS] <= 7) { 672 return s[s[VB_SEL_BUS]]; 673 } 674 else { 675 return -1; 676 } 677 } 678 679 /* ****************************************************** 680 ALU Constants 681 ****************************************************** */ 682 683 public static final String[] aluFuncs = { 684 "NOT", 685 "OR", "AND", "XOR", "ADD", "SUB", "ADDA", "SUBA"}; 686 687 /*** 688 * <b>ALU Operation:</b> NOT. 689 * <p>NOT(a_bus) => alu_bus</p> 690 * @see #ALU_SEL 691 */ 692 public static final int ALU_NOT = 0; 693 /*** 694 * <b>ALU Operation:</b> OR. 695 * <p>OR(a_bus,b_bus) => alu_bus</p> 696 * @see #ALU_SEL 697 */ 698 public static final int ALU_OR = 1; 699 /*** 700 * <b>ALU Operation:</b> AND. 701 * <p>AND(a_bus,b_bus) => alu_bus</p> 702 * @see #ALU_SEL 703 */ 704 public static final int ALU_AND = 2; 705 /*** 706 * <b>ALU Operation:</b> XOR. 707 * <p>XOR(a_bus,b_bus) => alu_bus</p> 708 * @see #ALU_SEL 709 */ 710 public static final int ALU_XOR = 3; 711 /*** 712 * <b>ALU Operation:</b> ADD. 713 * <p>a_bus + b_bus + c_in => alu_bus</p> 714 * @see #ALU_SEL 715 */ 716 public static final int ALU_ADD = 4; 717 /*** 718 * <b>ALU Operation:</b> SUB. 719 * <p>a_bus + NOT(b_bus) + c_in => alu_bus</p> 720 * @see #ALU_SEL 721 */ 722 public static final int ALU_SUB = 5; 723 /*** 724 * <b>ALU Operation:</b> ADDA. 725 * <p>a_bus + c_in => alu_bus</p> 726 * @see #ALU_SEL 727 */ 728 public static final int ALU_ADDA = 6; 729 /*** 730 * <b>ALU Operation:</b> SUBA. 731 * <p>a_bus - 1 + c_in => alu_bus</p> 732 * @see #ALU_SEL 733 */ 734 public static final int ALU_SUBA = 7; 735 736 /* ****************************************************** 737 Result Options 738 ****************************************************** */ 739 /*** 740 * <b>Result Option:</b> Place the alu_bus value on the result_bus. 741 * @see #RESULT_SEL 742 */ 743 public static final int RESULT_ALU = 0; 744 /*** 745 * <b>Result Option:</b> Place the MDR value on the result_bus. 746 * @see #RESULT_SEL 747 */ 748 public static final int RESULT_MDR = 1; 749 /*** 750 * <b>Result Option:</b> Place the ir_const4 value on the result_bus. 751 * @see #RESULT_SEL 752 */ 753 public static final int RESULT_IR_CONST4 = 2; 754 /*** 755 * <b>Result Option:</b> Place the ir_const8 value on the result_bus. 756 * @see #RESULT_SEL 757 */ 758 public static final int RESULT_IR_CONST8 = 3; 759 760 /* ****************************************************** 761 Cond Options 762 ****************************************************** */ 763 /*** 764 * <b>Cond Option:</b> Uses most significant bit from alu_bus. 765 * @see #COND 766 */ 767 public static final int COND_M7 = 0; 768 /*** 769 * <b>Cond Option:</b> Uses carry bit from the alu_bus. 770 * @see #COND 771 * @see #C_OUT 772 */ 773 public static final int COND_C_OUT = 1; 774 /*** 775 * <b>Cond Option:</b> Uses overflow bit from ALU. 776 * @see #COND 777 */ 778 public static final int COND_V = 2; 779 /*** 780 * <b>Cond Option:</b> Uses wait signal from Main Memory. 781 * @see #COND 782 */ 783 public static final int COND_WAIT = 3; 784 785 /* ****************************************************** 786 Operations 787 ****************************************************** */ 788 789 /*** Parse ucode and mem files. */ 790 public void parse() throws Exception { 791 try { 792 mp = new MythParser(microcodeFile, memoryFile, this); 793 memorySource = mp.getMemorySource(); 794 microcodeSource = mp.getMicrocodeSource(); 795 } 796 catch (java.io.FileNotFoundException ex) { 797 mode = NO_FILES_MODE; 798 throw ex; 799 } 800 catch (Exception ex) { 801 throw ex; 802 } 803 } 804 805 /*** Step forward N time to next opcode */ 806 public void step(int a) throws MythError { 807 if (a == 0) { 808 return; 809 } 810 if (a > 0) { 811 for (int i = 0; i < a; i++) { 812 step(); 813 } 814 return; 815 } 816 if (a < 0) { 817 int target = s[CLOCK] + a; 818 //System.out.println(">:>:" + target); 819 boot(); 820 if (target > 0) { 821 for (int i = 0; i < target; i++) { 822 step(); 823 } 824 } 825 return; 826 } 827 } 828 829 /*** Steps forward to next opcode. */ 830 public void next() throws MythError { 831 step(); 832 while (ucodeZero() != true) { 833 step(); 834 } 835 } 836 837 /*** Steps backward to last opcode. */ 838 public void last() throws MythError { 839 step( -1); 840 while (ucodeZero() != true && s[MythSim.CURRENT_ADDRESS] != 0) { 841 step( -1); 842 } 843 } 844 845 /*** Runs comptuer. */ 846 public void run() throws MythError { 847 for (int i = 0; i < 1000; i++) { 848 step(); 849 } 850 throw new MythError( 851 "Paused. (Press run again for the next 1000 clock cycles.)"); 852 } 853 854 /*** Boots comptuer. */ 855 public void boot() throws MythError { 856 if (mode == NO_FILES_MODE) { 857 System.out.println("Attempted to boot in no files mode."); 858 return; 859 } 860 if (mode == INVALID_FILES_MODE) { 861 System.out.println("Attempted to boot in invalid files mode."); 862 return; 863 } 864 _ucode = mp.toUcodeIntArray(); 865 //for (int i = 0; i < 3; i++) { 866 // System.out.print("a_sel:" + _ucode[i][ControlWord.A_SEL] + ","); 867 // System.out.print("b_sel:" + _ucode[i][ControlWord.B_SEL] + ","); 868 // System.out.println("c_in:" + _ucode[i][ControlWord.C_IN]); 869 //} 870 int temp[] = mp.toMemIntArray(); 871 for (int i = 0; i < 256; i++) { 872 s[i] = 0; 873 } 874 for (int i = 0; i < 256; i++) { 875 s[MythSim.MAIN_MEMORY + i] = temp[i]; 876 } 877 s[MythSim.CLOCK] = -1; 878 step(); 879 } 880 881 /*** Step One Clock Cycle*/ 882 public void step() throws MythError { 883 if (s[MythSim.LOCKED] == 1) { 884 return; 885 } 886 // Copy previous array. 887 if (s[MythSim.CLOCK] == -1) { 888 for (int i = 0; i < MythSim.LENGTH; i++) { 889 p[i] = 0; 890 } 891 } else { 892 for (int i = 0; i < MythSim.LENGTH; i++) { 893 p[i] = s[i]; 894 } 895 } 896 897 s[MythSim.WAIT] = 0; 898 899 s[MythSim.A_BUS] = s[MythSim.R_0 + s[MythSim.VA_SEL_BUS]]; 900 s[MythSim.B_BUS] = s[MythSim.R_0 + s[MythSim.VB_SEL_BUS]]; 901 902 switch (s[MythSim.ALU_SEL]) { 903 case ALU_NOT: 904 s[MythSim.ALU_BUS] = not(s[MythSim.A_BUS]); 905 break; 906 case ALU_OR: 907 s[MythSim.ALU_BUS] = or(s[MythSim.A_BUS], s[MythSim.B_BUS]); 908 break; 909 case ALU_AND: 910 s[MythSim.ALU_BUS] = and(s[MythSim.A_BUS], s[MythSim.B_BUS]); 911 break; 912 case ALU_XOR: 913 s[MythSim.ALU_BUS] = xor(s[MythSim.A_BUS], s[MythSim.B_BUS]); 914 break; 915 case ALU_ADD: 916 s[MythSim.ALU_BUS] = s[MythSim.A_BUS] + s[MythSim.B_BUS] + 917 s[MythSim.C_IN]; 918 break; 919 case ALU_SUB: 920 s[MythSim.ALU_BUS] = s[MythSim.A_BUS] + not(s[MythSim.B_BUS]) + 921 s[MythSim.C_IN]; 922 break; 923 case ALU_ADDA: 924 s[MythSim.ALU_BUS] = s[MythSim.A_BUS] + s[MythSim.C_IN]; 925 break; 926 case ALU_SUBA: 927 s[MythSim.ALU_BUS] = s[MythSim.A_BUS] - 1 + s[MythSim.C_IN]; 928 break; 929 default: 930 break; 931 } 932 933 s[MythSim.C_OUT] = carry(s[MythSim.ALU_SEL], s[MythSim.A_BUS], 934 s[MythSim.B_BUS], s[MythSim.C_IN]); 935 936 s[MythSim.V] = 0; 937 if (s[MythSim.ALU_BUS] > 127) { 938 System.out.println("Overflow:" + s[MythSim.ALU_BUS]); 939 s[MythSim.V] = 1; 940 s[MythSim.ALU_BUS] -= 256; 941 } 942 if (s[MythSim.ALU_BUS] < -128) { 943 System.out.println("Underflow:" + s[MythSim.ALU_BUS]); 944 s[MythSim.V] = 1; 945 s[MythSim.ALU_BUS] += 256; 946 } 947 948 s[MythSim.M_7] = m7(s[MythSim.ALU_BUS]); 949 950 switch (s[MythSim.WRITE]) { 951 case 0: 952 break; 953 //case 1: setMem(s[MythSim.MAR],tc2normal(s[MythSim.MDR])); break; 954 /* 3.01) Reed Class Bug that blocked last 128 bytes of memory. 955 case 1: setMem(tc2normal(s[MythSim.MAR]),tc2normal(s[MythSim.MDR])); break;*/ 956 /* 3.02) Reed Class Bug that messed up memory display.*/ 957 case 1: 958 setMem(tc2normal(s[MythSim.MAR]), s[MythSim.MDR]); 959 break; 960 default: 961 break; 962 } 963 switch (s[MythSim.READ]) { 964 case 0: 965 break; 966 case 1: 967 s[MythSim.MEMORY_BUS] = getMem(tc2normal(s[MythSim.MAR])); 968 break; 969 default: 970 break; 971 } 972 switch (s[MythSim.MDR_SEL]) { 973 case 0: 974 break; 975 case 1: 976 s[MythSim.MDR] = s[MythSim.ALU_BUS]; 977 break; 978 case 2: 979 s[MythSim.MDR] = s[MythSim.MEMORY_BUS]; 980 break; 981 default: 982 break; 983 } 984 switch (s[MythSim.MAR_SEL]) { 985 case 0: 986 break; 987 case 1: 988 s[MythSim.MAR] = s[MythSim.ALU_BUS]; 989 break; 990 default: 991 break; 992 } 993 switch (s[MythSim.IR0_SEL]) { 994 case 0: 995 break; 996 case 1: 997 s[MythSim.IR_0] = s[MythSim.MEMORY_BUS]; 998 break; 999 default: 1000 break; 1001 } 1002 switch (s[MythSim.IR1_SEL]) { 1003 case 0: 1004 break; 1005 case 1: 1006 s[MythSim.IR_1] = s[MythSim.MEMORY_BUS]; 1007 break; 1008 default: 1009 break; 1010 } 1011 switch (s[MythSim.RESULT_SEL]) { 1012 case RESULT_ALU: 1013 s[MythSim.RESULT_BUS] = s[MythSim.ALU_BUS]; 1014 break; 1015 case RESULT_MDR: 1016 s[MythSim.RESULT_BUS] = s[MythSim.MDR]; 1017 break; 1018 case RESULT_IR_CONST4: 1019 s[MythSim.RESULT_BUS] = lownibble(s[MythSim.IR_0]); 1020 break; 1021 case RESULT_IR_CONST8: 1022 s[MythSim.RESULT_BUS] = s[MythSim.IR_0]; 1023 break; 1024 default: 1025 break; 1026 } 1027 1028 switch (s[MythSim.VR0_WRITE_BUS]) { 1029 case 0: 1030 break; 1031 case 1: 1032 s[MythSim.R_0] = s[MythSim.RESULT_BUS]; 1033 break; 1034 default: 1035 break; 1036 } 1037 switch (s[MythSim.VR1_WRITE_BUS]) { 1038 case 0: 1039 break; 1040 case 1: 1041 s[MythSim.R_1] = s[MythSim.RESULT_BUS]; 1042 break; 1043 default: 1044 break; 1045 } 1046 switch (s[MythSim.VR2_WRITE_BUS]) { 1047 case 0: 1048 break; 1049 case 1: 1050 s[MythSim.R_2] = s[MythSim.RESULT_BUS]; 1051 break; 1052 default: 1053 break; 1054 } 1055 switch (s[MythSim.VR3_WRITE_BUS]) { 1056 case 0: 1057 break; 1058 case 1: 1059 s[MythSim.R_3] = s[MythSim.RESULT_BUS]; 1060 break; 1061 default: 1062 break; 1063 } 1064 switch (s[MythSim.R4_WRITE]) { 1065 case 0: 1066 break; 1067 case 1: 1068 s[MythSim.R_4] = s[MythSim.RESULT_BUS]; 1069 break; 1070 default: 1071 break; 1072 } 1073 switch (s[MythSim.R5_WRITE]) { 1074 case 0: 1075 break; 1076 case 1: 1077 s[MythSim.R_5] = s[MythSim.RESULT_BUS]; 1078 break; 1079 default: 1080 break; 1081 } 1082 switch (s[MythSim.R6_WRITE]) { 1083 case 0: 1084 break; 1085 case 1: 1086 s[MythSim.R_6] = s[MythSim.RESULT_BUS]; 1087 break; 1088 default: 1089 break; 1090 } 1091 switch (s[MythSim.R7_WRITE]) { 1092 case 0: 1093 break; 1094 case 1: 1095 s[MythSim.R_7] = s[MythSim.RESULT_BUS]; 1096 break; 1097 default: 1098 break; 1099 } 1100 1101 /* Infinite Loop Check*/ 1102 if ( 1103 (s[MythSim.CURRENT_ADDRESS] == s[MythSim.ADDRESS_TRUE]) && 1104 (s[MythSim.CURRENT_ADDRESS] == s[MythSim.ADDRESS_FALSE]) && 1105 (s[MythSim.CLOCK] != -1) 1106 ) { 1107 throw new MythError(0, s, 0); 1108 } 1109 1110 s[MythSim.CLOCK]++; 1111 int selection = 0; 1112 int next = 0; 1113 switch (s[MythSim.COND]) { 1114 case COND_M7: 1115 selection = s[MythSim.M_7]; 1116 break; 1117 case COND_C_OUT: 1118 selection = s[MythSim.C_OUT]; 1119 break; 1120 case COND_V: 1121 selection = s[MythSim.V]; 1122 break; 1123 case COND_WAIT: 1124 selection = s[MythSim.WAIT]; 1125 break; 1126 default: 1127 break; 1128 } 1129 switch (selection) { 1130 case 0: 1131 next = s[MythSim.ADDRESS_FALSE]; 1132 break; 1133 case 1: 1134 next = s[MythSim.ADDRESS_TRUE]; 1135 break; 1136 default: 1137 break; 1138 } 1139 switch (s[MythSim.INDEX_SEL]) { 1140 case 0: 1141 break; 1142 case 1: 1143 next += opcode(s[MythSim.IR_1]); 1144 //System.out.println("--" + next + "--" + opcode(s[MythSim.IR_1]) + "--"); 1145 break; 1146 default: 1147 break; 1148 } 1149 if (next >= _ucode.length) { 1150 s[MythSim.CLOCK]--; 1151 //_notDone = false; 1152 throw new MythError("Stopped."); 1153 } 1154 s[MythSim.CURRENT_ADDRESS] = next; 1155 //for (int i = 0; i <= 25; i++) { 1156 // s[i + 18] = _ucode[s[MythSim.CURRENT_ADDRESS]][i]; 1157 //} 1158 for (int i = 0; i < CONTROL_WORD_LENGTH; i++) { 1159 s[i + CONTROL_WORD_START] = _ucode[s[MythSim.CURRENT_ADDRESS]][i]; 1160 } 1161 switch (s[MythSim.RJ_SEL]) { 1162 case 0: 1163 s[MythSim.VA_SEL_BUS] = s[MythSim.A_SEL]; 1164 break; 1165 case 1: 1166 s[MythSim.VA_SEL_BUS] = rj(s[MythSim.IR_0]); 1167 break; 1168 default: 1169 break; 1170 } 1171 switch (s[MythSim.RK_SEL]) { 1172 case 0: 1173 s[MythSim.VB_SEL_BUS] = s[MythSim.B_SEL]; 1174 break; 1175 case 1: 1176 s[MythSim.VB_SEL_BUS] = rk(s[MythSim.IR_0]); 1177 break; 1178 default: 1179 break; 1180 } 1181 1182 s[MythSim.VR0_WRITE_BUS] = s[MythSim.R0_WRITE]; 1183 s[MythSim.VR1_WRITE_BUS] = s[MythSim.R1_WRITE]; 1184 s[MythSim.VR2_WRITE_BUS] = s[MythSim.R2_WRITE]; 1185 s[MythSim.VR3_WRITE_BUS] = s[MythSim.R3_WRITE]; 1186 if (s[MythSim.RI_SEL] == 0) { 1187 } 1188 else if (s[MythSim.RI_SEL] == 1) { 1189 switch (ri(s[MythSim.IR_1])) { 1190 case 0: 1191 s[MythSim.VR0_WRITE_BUS] = 1; 1192 break; 1193 case 1: 1194 s[MythSim.VR1_WRITE_BUS] = 1; 1195 break; 1196 case 2: 1197 s[MythSim.VR2_WRITE_BUS] = 1; 1198 break; 1199 case 3: 1200 s[MythSim.VR3_WRITE_BUS] = 1; 1201 break; 1202 default: 1203 break; 1204 } 1205 } 1206 else { 1207 } 1208 1209 } 1210 1211 /* ****************************************************** 1212 Logic Functions 1213 ****************************************************** */ 1214 1215 public static boolean NOT(boolean a) { 1216 return a == false; 1217 } 1218 1219 public static boolean AND(boolean a, boolean b) { 1220 return a && b; 1221 } 1222 1223 public static boolean OR(boolean a, boolean b) { 1224 return a || b; 1225 } 1226 1227 public static boolean XOR(boolean a, boolean b) { 1228 return (a && !b) || (!a && b); 1229 } 1230 1231 /* CONVERSION */ 1232 public static boolean[] int2bit(int a) { 1233 boolean temp[] = new boolean[8]; 1234 int value = a; 1235 if (a < 0) { 1236 temp[7] = true; 1237 a += 128; 1238 } 1239 else { 1240 temp[7] = false; 1241 } 1242 if (a >= 64) { 1243 temp[6] = true; 1244 a -= 64; 1245 } 1246 else { 1247 temp[6] = false; 1248 } // 1249 if (a >= 32) { 1250 temp[5] = true; 1251 a -= 32; 1252 } 1253 else { 1254 temp[5] = false; 1255 } 1256 if (a >= 16) { 1257 temp[4] = true; 1258 a -= 16; 1259 } 1260 else { 1261 temp[4] = false; 1262 } 1263 if (a >= 8) { 1264 temp[3] = true; 1265 a -= 8; 1266 } 1267 else { 1268 temp[3] = false; 1269 } 1270 if (a >= 4) { 1271 temp[2] = true; 1272 a -= 4; 1273 } 1274 else { 1275 temp[2] = false; 1276 } 1277 if (a >= 2) { 1278 temp[1] = true; 1279 a -= 2; 1280 } 1281 else { 1282 temp[1] = false; 1283 } 1284 if (a >= 1) { 1285 temp[0] = true; 1286 a -= 1; 1287 } 1288 else { 1289 temp[0] = false; 1290 } 1291 if (a == 0) { 1292 return temp; 1293 } 1294 else { 1295 return temp; 1296 } 1297 } 1298 1299 public static int bit2int(boolean a[]) { 1300 if (a.length == 8) { 1301 int temp = 0; 1302 if (a[7]) { 1303 temp -= 128; 1304 } 1305 if (a[6]) { 1306 temp += 64; 1307 } 1308 if (a[5]) { 1309 temp += 32; 1310 } 1311 if (a[4]) { 1312 temp += 16; 1313 } 1314 if (a[3]) { 1315 temp += 8; 1316 } 1317 if (a[2]) { 1318 temp += 4; 1319 } 1320 if (a[1]) { 1321 temp += 2; 1322 } 1323 if (a[0]) { 1324 temp += 1; 1325 } 1326 return temp; 1327 } 1328 else { 1329 return -129; 1330 } 1331 } 1332 1333 public static int tc2normal(int a) { 1334 int b; 1335 if (a >= 0) { 1336 b = a; 1337 } 1338 else { 1339 b = 256 + a; 1340 } 1341 //System.out.print("tc2normal:(" + a + "," + b + ")\n"); 1342 return b; 1343 } 1344 1345 public static int normal2tc(int a) { 1346 int b; 1347 if (a >= 128) { 1348 b = a - 256; 1349 } 1350 else { 1351 b = a; 1352 } 1353 //System.out.print("normal2tc:(" + a + "," + b + ")\n"); 1354 return b; 1355 } 1356 1357 /* ****************************************************** 1358 Partition Functions 1359 ****************************************************** */ 1360 1361 public static int ri(int a) { 1362 boolean bit[] = int2bit(a); 1363 int temp = 0; 1364 if (bit[1]) { 1365 temp += 2; 1366 } 1367 if (bit[0]) { 1368 temp += 1; 1369 } 1370 return temp; 1371 } 1372 1373 public static int rj(int a) { 1374 boolean bit[] = int2bit(a); 1375 int temp = 0; 1376 if (bit[7]) { 1377 temp += 2; 1378 } 1379 if (bit[6]) { 1380 temp += 1; 1381 } 1382 return temp; 1383 } 1384 1385 public static int rk(int a) { 1386 boolean bit[] = int2bit(a); 1387 int temp = 0; 1388 if (bit[5]) { 1389 temp += 2; 1390 } 1391 if (bit[4]) { 1392 temp += 1; 1393 } 1394 return temp; 1395 } 1396 1397 public static int opcode(int a) { 1398 boolean bit[] = int2bit(a); 1399 int temp = 0; 1400 if (bit[7]) { 1401 temp += 32; 1402 } 1403 if (bit[6]) { 1404 temp += 16; 1405 } 1406 if (bit[5]) { 1407 temp += 8; 1408 } 1409 if (bit[4]) { 1410 temp += 4; 1411 } 1412 if (bit[3]) { 1413 temp += 2; 1414 } 1415 if (bit[2]) { 1416 temp += 1; 1417 } 1418 return temp; 1419 } 1420 1421 /* Rename to irconst4*/ 1422 public static int lownibble(int a) { 1423 boolean bit[] = int2bit(a); 1424 int temp = 0; 1425 if (bit[3]) { 1426 temp += 128; 1427 } 1428 if (bit[3]) { 1429 temp += 64; 1430 } 1431 if (bit[3]) { 1432 temp += 32; 1433 } 1434 if (bit[3]) { 1435 temp += 16; 1436 1437 } 1438 if (bit[3]) { 1439 temp += 8; 1440 } 1441 if (bit[2]) { 1442 temp += 4; 1443 } 1444 if (bit[1]) { 1445 temp += 2; 1446 } 1447 if (bit[0]) { 1448 temp += 1; 1449 } 1450 return temp; 1451 } 1452 1453 /* ORIGINAL FUNCTION 1454 public static int lownibble(int a) { 1455 boolean bit[] = int2bit(a); 1456 int temp = 0; 1457 if (bit[3]) temp += 8; 1458 if (bit[2]) temp += 4; 1459 if (bit[1]) temp += 2; 1460 if (bit[0]) temp += 1; 1461 return temp; 1462 }*/ 1463 1464 public static int m7(int a) { 1465 boolean bit[] = int2bit(a); 1466 if (bit[7]) { 1467 return 1; 1468 } 1469 return 0; 1470 } 1471 1472 public static int normal(int a) { 1473 boolean bit[] = int2bit(a); 1474 int temp = 0; 1475 if (bit[7]) { 1476 temp += 128; 1477 } 1478 if (bit[6]) { 1479 temp += 64; 1480 } 1481 if (bit[5]) { 1482 temp += 32; 1483 } 1484 if (bit[4]) { 1485 temp += 16; 1486 } 1487 if (bit[3]) { 1488 temp += 8; 1489 } 1490 if (bit[2]) { 1491 temp += 4; 1492 } 1493 if (bit[1]) { 1494 temp += 2; 1495 } 1496 if (bit[0]) { 1497 temp += 1; 1498 //System.out.println("Normal=" + temp); 1499 } 1500 return temp; 1501 } 1502 1503 /* ****************************************************** 1504 Display Functions 1505 ****************************************************** */ 1506 public static String[] opcodeTags = { 1507 "no-op", 1508 "add", 1509 "li", 1510 "bz", 1511 "bzn", 1512 "jump", 1513 "move", 1514 "sb", 1515 "lb", 1516 "stop" 1517 }; 1518 1519 /* ****************************************************** 1520 ALU Operations 1521 ****************************************************** */ 1522 1523 public static int not(int x) { 1524 boolean a[] = int2bit(x); 1525 for (int i = 0; i <= 7; i++) { 1526 a[i] = (a[i] == false); 1527 } 1528 return bit2int(a); 1529 } 1530 1531 public static int or(int x, int y) { 1532 boolean a[] = int2bit(x); 1533 boolean b[] = int2bit(y); 1534 for (int i = 0; i <= 7; i++) { 1535 a[i] = (a[i] || b[i]); 1536 } 1537 return bit2int(a); 1538 } 1539 1540 public static int and(int x, int y) { 1541 boolean a[] = int2bit(x); 1542 boolean b[] = int2bit(y); 1543 for (int i = 0; i <= 7; i++) { 1544 a[i] = (a[i] && b[i]); 1545 } 1546 return bit2int(a); 1547 } 1548 1549 public static int xor(int x, int y) { 1550 boolean a[] = int2bit(x); 1551 boolean b[] = int2bit(y); 1552 for (int i = 0; i <= 7; i++) { 1553 a[i] = ( (a[i] == true && b[i] == false) || (a[i] == false && b[i] == true)); 1554 } 1555 return bit2int(a); 1556 } 1557 1558 public static int overflow(int x) { 1559 if (x > 127) { 1560 return 1; 1561 } 1562 if (x < -128) { 1563 return 1; 1564 } 1565 return 0; 1566 } 1567 1568 public static int fix(int x) { 1569 if (x > 127) { 1570 return x - 256; 1571 } 1572 if (x < -128) { 1573 return x + 256; 1574 } 1575 return x; 1576 } 1577 1578 public static int carry(int x, int y) { 1579 if ( (x < 0) && (y < 0)) { 1580 return 1; 1581 } 1582 return 0; 1583 } 1584 1585 /* */ 1586 public static int carry(int alu_sel, int a_bus, int b_bus, int c_in) { 1587 //System.out.print("Carry: "); 1588 int a = normal(a_bus); 1589 int b = normal(b_bus); 1590 int c = c_in; 1591 int r = 0; 1592 //System.out.print(a + "," + b + "," + c); 1593 //System.out.print(a_bus + "," + b_bus + "," + c_in); 1594 //(ms 1.0) case ALU_SUB: if ((a+not(b)+c) <0 ) {r=1;} break; 1595 //(ms 1.0) case ALU_SUBA: if ((a-1 +c) <0 ) {r=1;} break; 1596 switch (alu_sel) { 1597 case ALU_ADD: 1598 if ( (a + b + c) > 255) { 1599 r = 1; 1600 } 1601 break; 1602 case ALU_SUB: 1603 if ( (a + not(b) + c) > 255) { 1604 r = 1; 1605 } 1606 break; 1607 case ALU_ADDA: 1608 if ( (a + c) > 255) { 1609 r = 1; 1610 } 1611 break; 1612 case ALU_SUBA: 1613 if ( (a + 255 + c) > 255) { 1614 r = 1; 1615 } 1616 break; 1617 default: 1618 r = 0; 1619 } 1620 //System.out.println(""); 1621 return r; 1622 } 1623 1624 /* ****************************************************** 1625 Other Functions 1626 ****************************************************** */ 1627 1628 public static int[] empty() { 1629 int temp[] = new int[LENGTH]; 1630 for (int i = 0; i < LENGTH; i++) { 1631 temp[i] = 0; 1632 } 1633 return temp; 1634 } 1635 1636 public Object[][] getUstore() { 1637 return mp.toUcodeObjectArray(); 1638 } 1639 1640 public Object[][] getUcode() { 1641 return mp.toCodeArray(); 1642 } 1643 1644 public Object[][] getMemory() { 1645 return mp.toMemObjectArray(); 1646 } 1647 1648 }

This page was automatically generated by Maven