📄 tm.c
字号:
iMem[loc].iarg1 = arg1;
iMem[loc].iarg2 = arg2;
iMem[loc].iarg3 = arg3;
}
}
return TRUE;
}
/* stepTM */
STEPRESULT stepTM(void)
{
INSTRUCTION curInstruction;
int pc;
int r, s, t, m;
int ok;
pc = reg[PC_REG];
if ((pc < 0) || (pc > IADDR_SIZE))
return srIMEM_ERR;
reg[PC_REG] = pc + 1;
curInstruction = iMem[pc];
switch (opClass(curInstruction.iop)) {
case opclRR:
r = curInstruction.iarg1;
s = curInstruction.iarg2;
t = curInstruction.iarg3;
break;
case opclRM:
r = curInstruction.iarg1;
s = curInstruction.iarg3;
m = curInstruction.iarg2 + reg[s];
if ((m < 0) || (m > DADDR_SIZE))
return srDMEM_ERR;
break;
case opclRA:
r = curInstruction.iarg1;
s = curInstruction.iarg3;
m = curInstruction.iarg2 + reg[s];
break;
}
switch (curInstruction.iop) {
case opHALT:
#ifdef CHINESE
printf("停止: %1d,%1d,%1d\n", r, s, t);
#else
printf("HALT: %1d,%1d,%1d\n", r, s, t);
#endif
return srHALT;
case opIN:
do {
#ifdef CHINESE
printf("输入数据指令: ");
#else
printf("Enter value for IN instruction: ");
#endif
fflush(stdin);
gets(in_Line);
inCol = 0;
ok = getNum();
if (!ok)
#ifdef CHINESE
printf("非法数据\n");
#else
printf("Illegal value\n");
#endif
else
reg[r] = num;
} while (!ok);
break;
case opOUT:
#ifdef CHINESE
printf("输出数据指令: %d\n", reg[r]);
#else
printf("OUT instruction prints: %d\n", reg[r]);
#endif
break;
case opADD:
reg[r] = reg[s] + reg[t];
break;
case opSUB:
reg[r] = reg[s] - reg[t];
break;
case opMUL:
reg[r] = reg[s] * reg[t];
break;
case opDIV:
if (reg[t] != 0)
reg[r] = reg[s] / reg[t];
else
return srZERODIVIDE;
break;
case opLD:
reg[r] = dMem[m];
break;
case opST:
dMem[m] = reg[r];
break;
case opLDA:
reg[r] = m;
break;
case opLDC:
reg[r] = curInstruction.iarg2;
break;
case opJLT:
if (reg[r] < 0)
reg[PC_REG] = m;
break;
case opJLE:
if (reg[r] <= 0)
reg[PC_REG] = m;
break;
case opJGT:
if (reg[r] > 0)
reg[PC_REG] = m;
break;
case opJGE:
if (reg[r] >= 0)
reg[PC_REG] = m;
break;
case opJEQ:
if (reg[r] == 0)
reg[PC_REG] = m;
break;
case opJNE:
if (reg[r] != 0)
reg[PC_REG] = m;
break;
}
return srOKAY;
}
/* doCommand */
int doCommand(void)
{
char cmd;
int stepcnt = 0, i;
int printcnt;
int stepResult;
int regNo, loc;
do {
#ifdef CHINESE
printf("¥");
#else
printf("$");
#endif
fflush(stdin);
gets(in_Line);
lineLen = strlen(in_Line); /* Larry */
inCol = 0;
} while (!getWord());
cmd = word[0];
switch (tolower(cmd)) {
case 't':
traceflag = !traceflag;
#ifdef CHINESE
printf("跟踪");
#else
printf("Tracing now ");
#endif
if (traceflag)
#ifdef CHINESE
printf("打开。\n");
#else
printf("on.\n");
#endif
else
#ifdef CHINESE
printf("关闭。\n");
#else
printf("off.\n");
#endif
break;
case 'h':
#ifdef CHINESE
printf("命令列表:\n");
printf(" s)步进 <n> 执行 n 条指令 (默认为 1 )\n");
printf(" g)运行 运行程序直到停止\n");
printf(" r)寄存器 显示寄存器内容\n");
printf(" i)内存 <b <n>> 显示从 b 开始 n 个 iMem 地址\n");
printf(" d)内存 <b <n>> 显示从 b 开始 n 个 dMem 地址\n");
printf(" t)跟踪 开关指令跟踪功能\n");
printf(" p)显示 开关运行时实时显示指令内容功能\n");
printf(" c)清除 重置模拟器内容\n");
printf(" l)读取 读取 TM 文件 (暂不支持)\n");
printf(" h)帮助 显示本命令列表\n");
printf(" q)退出 终止模拟器\n");
#else
printf("Commands are:\n");
printf(" s)tep <n> Execute n (default 1) TM instructions\n");
printf(" g)o Execute TM instructions until HALT\n");
printf(" r)egs Print the contents of the registers\n");
printf(" i)Mem <b <n>> Print n iMem locations starting at b\n");
printf(" d)Mem <b <n>> Print n dMem locations starting at b\n");
printf(" t)race Toggle instruction trace\n");
printf(" p)rint Toggle print of total instructions executed ('go' only)\n");
printf(" c)lear Reset simulator for new execution of program\n");
printf(" l)oad Load TM file (It isn't supported now)\n");
printf(" h)elp Cause this list of commands to be printed\n");
printf(" q)uit Terminate the simulation\n");
#endif
break;
case 'l':
#ifdef CHINESE
printf("暂不支持读取文件功能。\n");
#else
printf("Load isn't supported now.\n");
#endif
break;
case 'p':
icountflag = !icountflag;
#ifdef CHINESE
printf("实时显示指令功能");
#else
printf("Printing instruction count now ");
#endif
if (icountflag)
#ifdef CHINESE
printf("打开。\n");
#else
printf("on.\n");
#endif
else
#ifdef CHINESE
printf("关闭。\n");
#else
printf("off.\n");
#endif
break;
case 's':
if (atEOL())
stepcnt = 1;
else if (getNum())
stepcnt = abs(num);
else
#ifdef CHINESE
printf("步进记数?\n");
#else
printf("Step count?\n");
#endif
break;
case 'g':
stepcnt = 1;
break;
case 'r':
for (i = 0; i < NO_REGS; i++) {
printf("%1d: %4d ", i, reg[i]);
if ((i % 4) == 3)
printf("\n");
}
break;
case 'i':
printcnt = 1;
if (getNum()) {
iloc = num;
if (getNum())
printcnt = num;
}
if (!atEOL())
#ifdef CHINESE
printf("指令位置?\n");
#else
printf("Instruction locations?\n");
#endif
else {
while ((iloc >= 0) && (iloc < IADDR_SIZE) && (printcnt > 0)) {
writeInstruction(iloc);
iloc++;
printcnt--;
}
}
break;
case 'd':
printcnt = 1;
if (getNum()) {
dloc = num;
if (getNum())
printcnt = num;
}
if (!atEOL())
#ifdef CHINESE
printf("数据位置?\n");
#else
printf("Data locations?\n");
#endif
else {
while ((dloc >= 0) && (dloc < DADDR_SIZE) && (printcnt > 0)) {
printf("%5d: %5d\n", dloc, dMem[dloc]);
dloc++;
printcnt--;
}
}
break;
case 'c':
iloc = 0;
dloc = 0;
stepcnt = 0;
for (regNo = 0; regNo < NO_REGS; regNo++)
reg[regNo] = 0;
dMem[0] = DADDR_SIZE;
for (loc = 1; loc < DADDR_SIZE; loc++)
dMem[loc] = 0;
break;
case 'q':
return FALSE;
default:
#ifdef CHINESE
printf("未知命令 '%c' 。\n", cmd);
#else
printf("Command '%c' unknown.\n", cmd);
#endif
break;
}
stepResult = srOKAY;
if (stepcnt > 0) {
if (cmd == 'g') {
stepcnt = 0;
while (stepResult == srOKAY) {
iloc = reg[PC_REG];
if (traceflag)
writeInstruction(iloc);
stepResult = stepTM();
stepcnt++;
}
if (traceflag)
#ifdef CHINESE
printf("执行指令数目 = %d\n", stepcnt);
#else
printf("Number of instructions executed = %d\n", stepcnt);
#endif
} else {
while ((stepcnt > 0) && (stepResult == srOKAY)) {
iloc = reg[PC_REG];
if (traceflag)
writeInstruction(iloc);
stepResult = stepTM();
stepcnt--;
}
}
printf("%s\n", stepResultTab[stepResult]);
}
return TRUE;
}
/* main */
int main(int argc, char *argv[])
{
if (argc != 2) {
#ifdef CHINESE
printf("用法: %s <文件名>\n", argv[0]);
#else
printf("usage: %s <filename>\n", argv[0]);
#endif
return 1;
}
strcpy(pgmName, argv[1]);
if (strchr(pgmName, '.') == NULL)
strcat(pgmName, ".tm");
pgm = fopen(pgmName, "r");
if (pgm == NULL) {
#ifdef CHINESE
printf("文件 '%s' 没有发现\n", pgmName);
#else
printf("file '%s' not found\n", pgmName);
#endif
return 1;
}
if (!readInstructions()) {
fclose(pgm);
return 1;
}
fclose(pgm);
#ifdef CHINESE
printf("TM 模拟器 (输入 h 获取帮助)...\n");
#else
printf("TM simulation (enter h for help)...\n");
#endif
do
done = !doCommand();
while (!done);
#ifdef CHINESE
printf("模拟器运行完毕。\n");
#else
printf("Simulation done.\n");
#endif
return 0;
}
/* 写在后面的话——zwf
* 一边看着《大话西游之仙履奇缘》,一边输入代码,
* 居然同时结束了。2001.11.5
*/
/* 增加了中文支持,使用 -DCHINESE 编译 2001.11.6 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -