📄 sim1.c
字号:
printf("\nType RETURN to continue: "); gets(buf); printf("\n"); } else {while ((fscanf(in_file,"%s", &input) != EOF) && found == false) if (strcmp(buf, input) == 0) {found = true; for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) fscanf(in_file, "%d ", &memory[i][j]); } else for (i = 0; i < 256; i++) fscanf(in_file, "%d ", &temp); } fclose(in_file); if (found == false) {printf("\n\nProgram %s not found\n", buf); printf("Type RETURN to continue: "); gets(buf); }}/*******************************************************//* *//* save_program() places a program (memory image) *//* in the file "SaveFile" under the name selected. *//* *//*******************************************************/void save_program(){FILE *out_file; FILE *in_file; int i, j; int saved = false; long temp; char new_prog[80]; printf("\n\nEnter the name of the program to be saved: "); gets(new_prog); printf("\n"); out_file = fopen("NewFile", "w"); if ((in_file = fopen("SaveFile", "r")) != NULL) {while (fscanf(in_file,"%s", &buf) != EOF) {if (strcmp(buf, new_prog) == 0) {saved = true; fprintf(out_file, "%s\n", new_prog); for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) fprintf(out_file, "%d ", memory[i][j]); for (i = 0; i < 256; i++) fscanf(in_file, "%d ", &temp); } if ((strcmp(buf, new_prog) < 0) || ((strcmp(buf, new_prog) > 0) && (saved == true))) {fprintf(out_file, "%s\n", buf); for (i = 0; i < 256; i++) {fscanf(in_file, "%d ", &temp); fprintf(out_file, "%d ", temp); } } if ((strcmp(buf, new_prog) > 0) && (saved == false)) {saved = true; fprintf(out_file, "%s\n", new_prog); for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) fprintf(out_file, "%d ", memory[i][j]); fprintf(out_file, "%s\n", buf); for (i = 0; i < 256; i++) {fscanf(in_file, "%d ", &temp); fprintf(out_file, "%d ", temp); } } } fclose(in_file); } if (saved == false) {fprintf(out_file, "%s\n", new_prog); for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) fprintf(out_file, "%d ", memory[i][j]); } fclose(out_file);#if (ANSI == 1) remove("SaveFile"); rename("NewFile", "SaveFile");#else system("rm SaveFile"); system("mv NewFile SaveFile");#endif}/*******************************************************//* *//* delete_program() removes a specified program *//* from the file "Save File." *//* *//*******************************************************/void delete_program(){FILE *out_file; FILE *in_file; int i; long temp; char del_prog[80]; printf("\nEnter the name of the program to be deleted: "); gets(del_prog); printf("\n"); out_file = fopen("NewFile", "w"); if ((in_file = fopen("SaveFile", "r")) != NULL) {while (fscanf(in_file,"%s", &buf) != EOF) {if (strcmp(buf, del_prog) != 0) {fprintf(out_file, "%s\n", buf); for (i = 0; i < 256; i++) {fscanf(in_file, "%d ", &temp); fprintf(out_file, "%d ", temp); } } else for (i = 0; i < 256; i++) fscanf(in_file, "%d ", &temp); } fclose(in_file); } fclose(out_file);#if (ANSI == 1) remove("SaveFile"); rename("NewFile", "SaveFile");#else system("rm SaveFile"); system("mv NewFile SaveFile");#endif}/*******************************************************//* *//* list_programs() presents a listing of the *//* names of those programs stored in the file *//* "SaveFile." *//* *//*******************************************************/void list_programs(){ FILE *in_file; char input[80]; int i, j; long count = 0; long temp; if ((in_file = fopen("SaveFile", "r")) == NULL) printf("\n\nNo programs are stored.\n"); else {printf("\n\n\nPrograms available are as follows:\n\n"); while ((fscanf(in_file, "%s", &input) != EOF)) {printf("%s\n", input); count++; for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) fscanf(in_file, "%d ", &temp); } if (count == 0) printf(" Program file is empty.\n"); } fclose(in_file); printf("\n**Type <Enter> to return to main display."); gets(buf); printf("\n");}/*******************************************************//* *//* single_step() executes a single machine *//* instruction and then returns to the main *//* display. *//* *//*******************************************************/void single_step(){ fetch(); decode(); execute();}/*******************************************************//* *//* run_to_halt() simulates machine cycles until *//* a halt instruction is executed or until 500 *//* machine cycles have been simulated. In the *//* second case, the option of stopping or *//* continuing for 500 more cycles is given. *//* *//*******************************************************/void run_to_halt(){ char symbol[80]; int i = 0; do { fetch(); decode(); if (execute() == 1) break; if (i < 500) i++; else {printf("\n\n**Five hundred machine cycles have been executed**"); printf("\n\n**Continue executing machine language program? (Y/N) "); gets(symbol); printf("\n\n"); if (symbol[0] == 'N' || symbol[0] == 'n') break; else i = 0; } } while (instr_reg != 0xC000);}/*******************************************************//* *//* fetch() simulates the fecth part of the *//* machine cycle. *//* *//*******************************************************/void fetch(){ int a,b; b = pc % 16; a = pc/16; instr_reg = (long) memory[a][b]; instr_reg *= 256; pc = (pc + 1) % 256; b = pc % 16; a = pc/16; instr_reg += (long) memory[a][b]; pc = (pc + 1) % 256;}/*******************************************************//* *//* decode() simulates the decode part of the *//* machine cycle. *//* *//*******************************************************/void decode(){ long temp = instr_reg; y = temp % 16; temp = temp/16; x = temp % 16; temp = temp/16; operand_1 = temp % 16; op_code = temp/16;}/*******************************************************//* *//* execute() oversees the simulation of the *//* execution part of the machine cycle. *//* *//*******************************************************/int execute(){switch(op_code) { case 1: reg[operand_1] = memory[x][y]; break; /* load from mem*/ case 2: reg[operand_1] = (x*16) + y; break; /* load immediate*/ case 3: memory[x][y] = reg[operand_1]; break; /* store*/ case 4: reg[y] = reg[x]; break; /* move*/ case 5: reg[operand_1] = (reg[x] + reg[y]) % 256; break; case 6: add_float(); break; case 7: reg[operand_1] = reg[x] | reg[y]; break; /* or*/ case 8: reg[operand_1] = reg[x] & reg[y]; break; /* and*/ case 9: reg[operand_1] = reg[x] ^ reg[y]; break; /* exclusive or*/ case 10: rotate(); break; case 11: jump(); break; case 12: break; case 13: reg[operand_1] = memory[reg[y]/16][reg[y]%16]; /* loadindirect */ break; case 14: memory[reg[y]/16][reg[y]%16] = reg[operand_1]; /* storeindirect */ break; default: printf("\n\n\n"); printf("*** Invalid instruction in instruction register ***"); printf("\n\n\n"); printf("Type RETURN to continue: "); gets(buf); printf("\n"); return(1); } return(0);}/*******************************************************//* *//* add_float() simulates the addition of two *//* values stored in floating-point notation. *//* *//*******************************************************/void add_float(){ int sign1, sign2, exp1, exp2; long mantissa1, mantissa2; int answer, exponent = 4, sign_ans = 0; mantissa1 = reg[x]%16; mantissa2 = reg[y]%16; /* separate intocomponents */ sign1 = reg[x]/128; sign2 = (int) reg[y]/128; exp1 = reg[x]/16; exp1 = (exp1 % 8) - 4; exp2 = reg[y]/16; exp2 = (exp2 % 8) - 4; mantissa1 = mantissa1 << 4; /* Get ready to alignmantissas */ mantissa2 = mantissa2 << 4; if (exp1 > 0) mantissa1 = mantissa1 << exp1; /* Alignmantissas */ else mantissa1 = mantissa1 >> (-1 * exp1); if (exp2 > 0) mantissa2 = mantissa2 << exp2; else mantissa2 = mantissa2 >> (-1 * exp2); if (sign1 == 1) /* Negate mantissasthat */ mantissa1 *= -1; /* represent negativevalues, */ if (sign2 == 1) mantissa2 *= -1; answer = mantissa1 + mantissa2; /* and then addthem. */ if (answer < 0) /* Get absolute valueof */ {answer *= -1; /*answer. */ sign_ans = 1; } while((answer > 255 || answer < 128) && answer != 0) {if(answer > 255) /* Move answer so leftmost1 */ {answer = answer >> 1; /* is on 8th bit (tonormalize */ exponent++; /*answer). */ } else {answer = answer << 1; exponent--; } } answer = answer >> 4; /* Align answer in rightmost fourbits. */ if(answer == 0) /* Make entire pattern 0 if answer is0. */ {exponent = 0; sign_ans = 0; }if (sign_ans == 1) /* Add 1 to 8th bit if answer isnegative. */ answer = answer + 128; exponent = exponent << 4; /* Insert exponent intoanswer. */ answer = answer + exponent; reg[operand_1] = answer;}/*******************************************************//* *//* rotate() rotates the contents of the register *//* indicated by operand_1 by one bit y times. *//* *//*******************************************************/void rotate(){ int i, p; for(i=1; i<=y; i++) {p = reg[operand_1] % 2; p = p << 7; reg[operand_1] = reg[operand_1] / 2; reg[operand_1] = reg[operand_1] | p; }}/*******************************************************//* *//* jump() simulates the machine language branch *//* instruction by changing the program counter *//* if reg[operand_1] equals reg[0]. *//* *//*******************************************************/void jump(){ if(reg[operand_1] == reg[0]) pc = (x*16) + y;}/*******************************************************//* *//* That's all folks! *//* *//*******************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -