📄 dlxdoc.cpp
字号:
cmd.Delete(0,5);
temp = cmd.Left(16);//rs1
cmd.Delete(0,temp.GetLength());
tempNum = btod(temp);
temp.Format("%d",tempNum);
m_OneInstruction.src1 = temp;
m_OneInstruction.cmdString = m_OneInstruction.op + " " + m_OneInstruction.dest + "," + m_OneInstruction.src1;
break;
case JR:
m_OneInstruction.op = OpCode[t].name;
temp = cmd.Left(5);//rd
cmd.Delete(0,temp.GetLength());
tempNum = btod(temp);
temp.Format("%d",tempNum);
temp = "R" + temp;
m_OneInstruction.dest = temp;
m_OneInstruction.cmdString = m_OneInstruction.op + m_OneInstruction.dest;
break;
//unknown
case UNKNOWN:
m_OneInstruction.cmdString = "UNKNOWN";
break;
default:
break;
}
return m_OneInstruction.cmdString;
}
void CDlxDoc:: CompileInstrunction()
{
int i = 0;
for(;i<MAXINSTR;i++)
{
m_OneInstruction.cmdString = m_CmdString[i];
m_CodeMemory[i] = "";
m_OneInstruction.cmdString.TrimLeft();
m_OneInstruction.cmdString.TrimRight();
m_OneInstruction.cmdString.MakeUpper();
CString cmd = m_OneInstruction.cmdString;
m_OneInstruction.op = cmd.SpanExcluding(" ,()");
cmd.Delete(0,m_OneInstruction.op.GetLength());
cmd.Delete(0);
cmd.TrimLeft();
//目标存储地址(或寄存器)
if(cmd.GetLength())
{
m_OneInstruction.dest = cmd.SpanExcluding(" ,()");
cmd.Delete(0,m_OneInstruction.dest.GetLength());
cmd.Delete(0);
cmd.TrimLeft();
}
//数据源1
if(cmd.GetLength())
{
m_OneInstruction.src1 = cmd.SpanExcluding(" ,()");
cmd.Delete(0,m_OneInstruction.src1.GetLength());
cmd.Delete(0);
cmd.TrimLeft();
}
//数据源2
if(cmd.GetLength())
m_OneInstruction.src2 = cmd.SpanExcluding(" ,()");
Tokentype t = ::Lookup(LPCTSTR (m_OneInstruction.op));
// CString temp = m_OneInstruction.dest;
CString temp;
int tempNum;
switch(t)
{
//R--Type | 6 5 5 5 11 |
// | OpCode rs1 rs2 rd func|
case ADD:
case SUB:
case AND:
case MUT:
case DIV:
case OR:
case XOR:
m_CodeMemory[i] = OpMachineCode[t];
temp = m_OneInstruction.src1;//rs1
temp.Delete(0);
tempNum = atoi(temp);
temp = dtob(tempNum,5);
m_CodeMemory[i] += temp;
temp = m_OneInstruction.src2;//rs2
temp.Delete(0);
tempNum = atoi(temp);
temp = dtob(tempNum,5);
m_CodeMemory[i] += temp;
temp = m_OneInstruction.dest;//rd
temp.Delete(0);//delete R
tempNum = atoi(temp);
temp = dtob(tempNum,5);
m_CodeMemory[i] += temp;
//func
m_CodeMemory[i] += "00000000000";//11
break;
//J--Type |6 26 |
// |OpCode offset |
case J:
m_CodeMemory[i] = OpMachineCode[t];
temp = m_OneInstruction.dest;
tempNum = atoi(temp);
temp = dtob(tempNum,26);
m_CodeMemory[i] += temp;
break;
case TRAP:
m_CodeMemory[i] = OpMachineCode[t];
m_CodeMemory[i] += "00000000000000000000000000";//26
break;
//R--Type |6 5 5 16 |
// |Opcode rs1 rd immediate |
case LW:
case SW:
m_CodeMemory[i] = OpMachineCode[t];
temp = m_OneInstruction.src2;//rs1
temp.Delete(0);
tempNum = atoi(temp);
temp = dtob(tempNum,5);
m_CodeMemory[i] += temp;
temp = m_OneInstruction.dest;//rd
temp.Delete(0);
tempNum = atoi(temp);
temp = dtob(tempNum,5);
m_CodeMemory[i] += temp;
temp = m_OneInstruction.src1;//immediate
tempNum = atoi(temp);
temp = dtob(tempNum,16);
m_CodeMemory[i] += temp;
break;
case ADDI:
case SUBI:
case MUTI:
case DIVI:
case ANDI:
case ORI:
case XORI:
m_CodeMemory[i] = OpMachineCode[t];
temp = m_OneInstruction.src1;//rs1
temp.Delete(0);
tempNum = atoi(temp);
temp = dtob(tempNum,5);
m_CodeMemory[i] += temp;
temp = m_OneInstruction.dest;//rd
temp.Delete(0);
tempNum = atoi(temp);
temp = dtob(tempNum,5);
m_CodeMemory[i] += temp;
temp = m_OneInstruction.src2;//immediate
tempNum = atoi(temp);
temp = dtob(tempNum,16);
m_CodeMemory[i] += temp;
break;
case BEQZ:
case BNEZ:
m_CodeMemory[i] = OpMachineCode[t];
temp = m_OneInstruction.dest;//rs1
temp.Delete(0);
tempNum = atoi(temp);
temp = dtob(tempNum,5);
m_CodeMemory[i] += temp;
//rd unused
m_CodeMemory[i] += "11111";//5
temp = m_OneInstruction.src1;//immediate
tempNum = atoi(temp);
temp = dtob(tempNum,16);
m_CodeMemory[i] += temp;
break;
case JR:
m_CodeMemory[i] = OpMachineCode[t];
temp = m_OneInstruction.dest;//rs1
temp.Delete(0);
tempNum = atoi(temp);
temp = dtob(tempNum,5);
m_CodeMemory[i] += temp;
//rd = 0
m_CodeMemory[i] += "00000";//5
//immediate = 0
m_CodeMemory[i] += "0000000000000000";//16
break;
//unknown
case UNKNOWN:
m_CodeMemory[i] = "00000000000000000000000000000000";//32
break;
default:
break;
}
}
}
//////////////////////////////////////////////////////////////
//函数名: OnExeReset() 功能:重置指令执行状态,恢复到执行前
//参数:无 返回值:无
//////////////////////////////////////////////////////////////
void CDlxDoc::OnExeReset()
{
this->m_Clock = 0;
this->m_CurInstr = 0;
this->m_Pc = 0;
for(int i = 0; i < MAXPC; i++)
{
this->m_Instruction[i].exetimes = 0;
this->m_Instruction[i].cmdString = "TRAP";
this->m_Instruction[i].state = _S_STALL;
// this->m_Instruction[i].cmdString = "TRAP";
this->m_Instruction[i].outstate = _S_STALL;
this->m_Instruction[i].period = 1;
this->m_Instruction[i].state = _S_STALL;
for(int j = 0; j< 5; j++)
this->m_Instruction[i].forward[j][0] =
this->m_Instruction[i].forward[j][1] =
CSize(0,0);
for(j = 0; j<MAXCLOCK; j++)
this->m_Instruction[i].states[j] = _S_STALL;
}
for(i = 0; i < MAXREGS; i++)
this->m_Register[i] = 0;
for(i = 0; i < MAXMEM; i++)
this->m_Memory[i] = 0;
UpdateAllViews(NULL);
MyBar->TextOut();
MemBar->MemInsert(true);
RegBar->RegInsert(true);
}
void CDlxDoc::OnSetMem()
{
CMemDlg dlg;
dlg.m_Edit1 = 0;
dlg.m_Edit2 = 0;
if(dlg.DoModal()==IDOK)
{
this->m_Memory[dlg.m_Edit1] = dlg.m_Edit2;
MemBar->MemUpdate(dlg.m_Edit1);
}
}
void CDlxDoc::OnSetCodemem()
{
CCodeMemDlg dlg;
dlg.m_Edit1 = 0;
dlg.m_Edit2 = this->m_CodeMemory[0];
if(dlg.DoModal()==IDOK)
{
this->m_CodeMemory[dlg.m_Edit1] = dlg.m_Edit2;
CodeMemBar->CodeMemUpdate(dlg.m_Edit1);
}
}
void CDlxDoc::OnSetReg()
{
CRegDlg dlg;
dlg.m_Edit1 = 0;
dlg.m_Edit2 = 0;
if(dlg.DoModal()==IDOK)
{
this->m_Register[dlg.m_Edit1] = dlg.m_Edit2;
RegBar->RegUpdate(dlg.m_Edit1);
}
}
void CDlxDoc::OnSave() //compile
{
CString str,temp;
int i = 0;
MyBar->m_wndChild.GetWindowText(str);
str.TrimLeft();
str.TrimRight();
if(str.GetLength() == 0)
MyDoc->SetInstructionNum(0);
else{
while(str.GetLength() != 0)
{
str.TrimLeft();
str.TrimRight();
temp = str.SpanExcluding("\r\n");
if(temp != "")
{
m_CmdString[i] = temp;
i++;
}
str.Delete(0,temp.GetLength());
str.Delete(0,2);
if(temp == "TRAP")
break;
}
if(temp != "TRAP"){
m_CmdString[i] = "TRAP";
}
else
i--;
MyDoc->SetInstructionNum(i);
}
MyBar->TextOut();
this->CompileInstrunction();
CodeMemBar->CodeMemInsert(true);
}
void CDlxDoc::OnIdReset()
{
OnExeReset();
}
void CDlxDoc::OnIdComple()
{
OnSave();
}
void CDlxDoc::OnIdRun()
{
OnExeRunStep();
}
BOOL CDlxDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
CString title = lpszPathName;
int nPos = title.ReverseFind('\\');
if(nPos>=0)
title = title.Right(title.GetLength()-nPos-1);
title = title + szSpace;
theApp.m_pMainWnd->SetWindowText(title);
return FALSE;
}
void CDlxDoc::UpdateListBar(int whichlist)
{
if(whichlist == 0 && MemBar->IsWindowVisible() == 1)
MemBar->MemInsert(true);
else if (whichlist == 1 && RegBar->IsWindowVisible() == 1)
RegBar->RegInsert(true);
}
CString CDlxDoc::dtob(int d,int count)
{
CString b="";
CString temp;
int t = d;
if(d<0)
{
d = abs(d);
d = power(2,10) - d;
}
do{
temp.Format("%d",d%2);
b = temp + b;
d = d/2;
}while(d!=0);
while(b.GetLength()!=count)
{
if(t<0)
b = "1"+b;
else
b="0"+b;
}
return b;
}
int CDlxDoc::btod(CString b)
{
int i=0;
int t,d=0;
if(b.Left(1) == "1")//b 是负数
{
while(b.GetLength()!=0 && i<=12)//取反
{
t = atoi(b.Right(1)) - 1;
t = abs(t);
b.Delete(b.GetLength()-1);
d = t* power(2,i) + d;
i++;
}
d = d +1;//加一
d = -d;
}
else //b 是正数
while(b.GetLength()!=0 && i<12)
{
t = atoi(b.Right(1));
b.Delete(b.GetLength()-1);
d = t* power(2,i) + d;
i++;
}
return d;
}
int CDlxDoc:: power(int base,int n)
{
int i,p;
p=1;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -