⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pipelinedoc.cpp

📁 计算机体系结构中关于通用5级流水线的模拟实现程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		break;
	case 13: //muli,不要求,没有测试
		REG[27]=id_ex.A*id_ex.Imm;
		break;
	case 14: //divi,不要求,没有测试
		REG[26]=id_ex.A/id_ex.Imm;
		REG[27]=id_ex.A%id_ex.Imm;
		break;
	case 15: //lw
		ex_mem.ALUOutput=id_ex.A+id_ex.Imm;
		break;
	case 16: //sw
		ex_mem.ALUOutput=id_ex.A+id_ex.Imm;
		ex_mem.B=id_ex.B;
		break;
	default:
		break;
	}
}

void CPipelineDoc::MEM_STEP()    //流水线第四步
{	
	mem_wb.IR=ex_mem.IR;		//基本流水线操作
	if(!FUNC(mem_wb.IR))		//当前续指令被cancel
	{
		states.normal[3]=2;		//将接口参数设为cancel状态
		return;					//返回(不执行本步操作)
	}
	else
		states.normal[3]=1;		//没有将接口参数设为normal状态
	switch(FUNC(mem_wb.IR))		//根据指令的func code执行操作
	{
	case 1:    //alu指令
	case 2:
	case 3:
	case 4:
	case 5:
	case 8:
	case 9:
	case 10:
	case 11:
	case 12: 
		mem_wb.ALUOutput=ex_mem.ALUOutput;
		break;
	case 15:	//load
		mem_wb.LMD=DM[ex_mem.ALUOutput];
		break;
	case 16:	//store
		DM[ex_mem.ALUOutput]=ex_mem.B;
		break;
	default:
		break;
	}
}

bool CPipelineDoc::WB_STEP()   //流水线第五步
{
	if(!FUNC(mem_wb.IR))		//当前续指令被cancel
	{
		states.normal[4]=2;		//将接口参数设为cancel状态
		return false;			//返回(不执行本步操作),false标志流水线还没有结束
	}
	else
		states.normal[4]=1;
	switch(FUNC(mem_wb.IR))
	{
	case 21:
		return true;			//trap指令,表明流水线已经结束
	case 1:    //R型指令
	case 2:
	case 3:
	case 4:
	case 5:
		REG[RD(mem_wb.IR)]=mem_wb.ALUOutput;
		break;
	case 8:    //I型指令
	case 9:
	case 10:
	case 11:
	case 12:
		REG[RT(mem_wb.IR)]=mem_wb.ALUOutput;
		break;
	case 15:   //load
		REG[RT(mem_wb.IR)]=mem_wb.LMD;
		break;
	default:
		break;
	}
	return false;				//非trap指令,表明流水线没有结束
}

void CPipelineDoc::fetch_a()
{
	if(load_stall)
	{
		states.normal[2]=0;
		states.normal[1]=0;
		data_stall=1;
		load_stall=false;
	}
	if(FUNC(id_ex.IR)==16&&forwarding==NON_FORWARD)
	{	
		if(FUNC(ex_mem.IR)>=1&&FUNC(ex_mem.IR)<=7)//如果ex_mem正处于1-7的操作中
			if(RD(ex_mem.IR)==RT(id_ex.IR))	//如果这条指令的寄存器与上条目标寄存器冲突
			{
				data_stall=1;           //发生stall
				states.normal[1]=0;      
				return;
			}
		if(FUNC(ex_mem.IR)>=8&&FUNC(ex_mem.IR)<=14)
			if(RT(ex_mem.IR)==RT(id_ex.IR))	
			{
				data_stall=1;
				states.normal[1]=0;
				return;
			}
		if(FUNC(mem_wb.IR)>=1&&FUNC(mem_wb.IR)<=7)
			if(RD(mem_wb.IR)==RT(id_ex.IR))	
			{
				data_stall=1;
				states.normal[1]=0;
				return;
			}
		if(FUNC(mem_wb.IR)>=8&&FUNC(mem_wb.IR)<=14)
			if(RT(mem_wb.IR)==RT(id_ex.IR))	
			{
				data_stall=1;
				states.normal[1]=0;
				return;
			}
	}
	if(RD(ex_mem.IR)==RS(id_ex.IR))						 //寄存器相关
		if(FUNC(ex_mem.IR)>=1&&FUNC(ex_mem.IR)<=7)       //源指令条件
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=16)    //目标指令条件
			{
				if(forwarding==FORWARD)				
					id_ex.A=ex_mem.ALUOutput;				
				else
				{
					data_stall=1;
					states.normal[1]=0;
				}
				return;
			}
	if(RD(mem_wb.IR)==RS(id_ex.IR))
		if(FUNC(mem_wb.IR)>=1&&FUNC(mem_wb.IR)<=7)           //R型
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=18)
			{		
				if(forwarding==FORWARD)
					id_ex.A=mem_wb.ALUOutput;
				else
				{
					data_stall=1;
					states.normal[1]=0;
				}
				return;
			}
	if(RT(ex_mem.IR)==RS(id_ex.IR))
		if(FUNC(ex_mem.IR)>=8&&FUNC(ex_mem.IR)<=14)          //i型
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=18)  
			{
				if(forwarding==FORWARD)
					id_ex.A=ex_mem.ALUOutput;
				else
				{
					data_stall=1;
					states.normal[1]=0;
				}
				return;
			}
    if(RS(mem_wb.IR)==RS(id_ex.IR))
		if(FUNC(mem_wb.IR)>=8&&FUNC(mem_wb.IR)<=14)     
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=18)  
			{
				if(forwarding==FORWARD)
					id_ex.A=mem_wb.ALUOutput;
				else
				{
					data_stall=1;
					states.normal[1]=0;
				}
				return;
			}
	if(RT(ex_mem.IR)==RS(id_ex.IR))
		if(FUNC(ex_mem.IR)==15)                         //load
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=18)  
			{
				if(forwarding==FORWARD)
				{
					load_stall=true;
				}
				else
				{
					data_stall=1;
					states.normal[1]=0;
					id_ex.IR=0;
				}
				return;
			}
	if(RT(mem_wb.IR)==RS(id_ex.IR))
		if(FUNC(mem_wb.IR)==15)                         //load
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=18)  
			{
				if(forwarding==FORWARD)
					id_ex.A=mem_wb.ALUOutput;
				else
				{
					data_stall=1;
					states.normal[1]=0;
					id_ex.IR=0;
				}
				return;
			}
	if(RD(ex_mem.IR)==RS(id_ex.IR))				        //beqz&bneqz		 
		if(FUNC(ex_mem.IR)>=1&&FUNC(ex_mem.IR)<=7)       
			if(FUNC(id_ex.IR)>=17&&FUNC(id_ex.IR)<=18)    
			{
				if(forwarding==FORWARD)				
				{	
					data_stall=1;
					states.normal[1]=0;
				}
				else
				{
					data_stall=2;
					states.normal[1]=0;
				}
				return;
			}

	id_ex.A=REG[RS(if_id.IR)];
}

void CPipelineDoc::fetch_b()
{
	if(FUNC(id_ex.IR)==16&&forwarding==NON_FORWARD)
	{	
		if(FUNC(ex_mem.IR)>=1&&FUNC(ex_mem.IR)<=7)
			if(RD(ex_mem.IR)==RS(id_ex.IR))	
			{
				data_stall=1;
				states.normal[1]=0;
				return;
			}
		if(FUNC(ex_mem.IR)>=8&&FUNC(ex_mem.IR)<=14)
			if(RT(ex_mem.IR)==RT(id_ex.IR))	
			{
				data_stall=1;
				states.normal[1]=0;
				return;
			}
	}
	if(RD(ex_mem.IR)==RT(id_ex.IR))						 //寄存器相关
		if(FUNC(ex_mem.IR)>=1&&FUNC(ex_mem.IR)<=7)       //源指令条件
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=7)    //目标指令条件
			{
				if(forwarding==FORWARD)
					id_ex.B=ex_mem.ALUOutput;
				else
				{
					data_stall=1;
					states.normal[1]=0;
				}
				return;
			}
	if(RD(mem_wb.IR)==RT(id_ex.IR))
		if(FUNC(mem_wb.IR)>=1&&FUNC(mem_wb.IR)<=7)  
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=7)
			{
				if(forwarding==FORWARD)
					id_ex.B=mem_wb.ALUOutput;
				else
				{
					data_stall=1;
					states.normal[1]=0;
				}
				return;
			}
	if(RT(ex_mem.IR)==RT(id_ex.IR))
		if(FUNC(ex_mem.IR)>=8&&FUNC(ex_mem.IR)<=14)     
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=7)  
			{
				if(forwarding==FORWARD)
					id_ex.B=ex_mem.ALUOutput;
				else
				{
					data_stall=1;
					states.normal[1]=0;
				}
				return;
			}
    if(RT(mem_wb.IR)==RT(id_ex.IR))
		if(FUNC(mem_wb.IR)>=8&&FUNC(mem_wb.IR)<=14)     
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=7)  
			{
				if(forwarding==FORWARD)
					id_ex.B=mem_wb.ALUOutput;
				else
				{
					data_stall=1;
					states.normal[1]=0;
				}
				return;
			}
	 if(RT(ex_mem.IR)==RT(id_ex.IR))
		if(FUNC(ex_mem.IR)==15)     
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=7)  
			{
				if(forwarding==FORWARD)
				{
					load_stall=true;
				}
				else
				{
					data_stall=1;
					states.normal[1]=0;
					id_ex.IR=0;
				}
				return;
			}
	if(RT(mem_wb.IR)==RT(id_ex.IR))
		if(FUNC(ex_mem.IR)==15)     
			if(FUNC(id_ex.IR)>=1&&FUNC(id_ex.IR)<=7)  
			{
				if(forwarding==FORWARD)
					id_ex.B=mem_wb.ALUOutput;
				else
				{
					data_stall=1;
					states.normal[1]=0;
					id_ex.IR=0;
				}
				return;
			}
	id_ex.B=REG[RT(if_id.IR)];
}
/////////////////////////////////////////////////////////////////////////////
// CPipelineDoc diagnostics

#ifdef _DEBUG
void CPipelineDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CPipelineDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPipelineDoc commands

void CPipelineDoc::OnStep() 
{
	// TODO: Add your command handler code here
	set_OK=false;			//执行开始、禁用模式和策略设置
	int i=0,j,temp;
	bool end=false;
	if(GoodFile==false)
	{//文件打开失败或尚未打开
			AfxMessageBox("Please load instruction first");
			return;
	}
	//五级流水线
	WB_STEP();
	MEM_STEP();
	EX_STEP();
	IDD_STEP();
	IF_STEP();
	//设置Icon_Rec的值
	if(cycle==0)
		Icon_Rec[1][0]=0;
	else
	{
		i=states.number;// 当前取指条数
		for(;i>0&&i>=states.number-5;i--)
		{
			end=false;
			j=cycle-1;
			while(Icon_Rec[i][j]==5&&j>=0)
				j--;
			if(j<0)
				break;
			if(Icon_Rec[i][j]==-2)
				continue;
			for(temp=j;temp>=0;temp--)
			{
				if(Icon_Rec[i][temp]==4||Icon_Rec[i][temp]==-2)
				{
					end=true;
					break;
				}
			}
			if(end)
				continue;
			else
			{
				temp=Icon_Rec[i][j];
				if(states.normal[temp+1]==1)
					Icon_Rec[i][cycle]=temp+1;
				else if(states.normal[temp+1]==0)
					Icon_Rec[i][cycle]=5;
				else
					Icon_Rec[i][cycle]=-2;
			}
		}
	}
	cycle++;
	this->UpdateAllViews(NULL);
}

void CPipelineDoc::OnDataSet() 
{
	// 设定寄存器和数据内存的值
	DataSet data;
	int i;
	struct mem *temp;
	data.reg_addr=0;
	data.reg_Val=REG[0];
	data.mem_addr=0;
	data.mem_Val=DM[0];
	for(i=0;i<32;i++)
		data.reg[i]=REG[i];		//初始化修改寄存器
	if(data.DoModal()==IDOK)
	{
		for(i=0;i<32;i++)
			REG[i]=data.reg[i]; //回填寄存器的修改值
		temp=data.head;
		while(temp)
		{						//回填内存的修改值
			DM[temp->i]=temp->val;
			temp=temp->next;
		}
	}
	this->UpdateAllViews(NULL);
}

void CPipelineDoc::OnReset() 
{
	//重新设置,但保留指令,参见clear函数

	int i=0,j;
	set_OK=true;
	for(i=0;i<1000;i++)
	{
		DM[i]=0;
		if(i<32)
			REG[i]=0;
		states.instruction[i]=-1;
	}
	for(i=0;i<1000;i++)
		for(j=0;j<1000;j++)
			Icon_Rec[i][j]=-1;

	branching=PREDICTED_UNTAKEN;
	strategy=3;
	forwarding=FORWARD;
	mode=1;
	pc=0;
	data_stall=0;
	load_stall=0;
	states.number=0;
	for(i=0;i<5;i++)
	{	states.normal[i]=1;}
	cycle=0;	
	if_id.IR=0;
	id_ex.IR=0;
	ex_mem.IR=0;
	mem_wb.IR=0;
	this->UpdateAllViews(NULL);
}

void CPipelineDoc::clear()
{//所有参数初始化
	int i=0,j;
	GoodFile=false;		//文件
	set_OK=true;		//参数设置
	for(i=0;i<1000;i++)
	{					//寄存器、内存、指令清空
		attr[i][0]='\0';
		IM[i]=0;
		DM[i]=0;
		if(i<32)
			REG[i]=0;
		states.instruction[i]=-1;
	}
	for(i=0;i<1000;i++)	
		for(j=0;j<1000;j++)
			Icon_Rec[i][j]=-1;

	branching=PREDICTED_UNTAKEN;
	strategy=3;
	forwarding=FORWARD;
	mode=1;
	pc=0;
	data_stall=0;
	load_stall=0;
	states.number=0;
	for(i=0;i<5;i++)
	{	states.normal[i]=1;}
	cycle=0;	
	if_id.IR=0;
	id_ex.IR=0;
	ex_mem.IR=0;
	mem_wb.IR=0;
	this->UpdateAllViews(NULL);
}

//以下设置模式和转移策略
void CPipelineDoc::OnDelay() 
{
	// 设置Delay
	set_branch_delay();
	strategy=4;
}

void CPipelineDoc::OnFlush() 
{
	// 设置OnFlush
	set_branch_taken();
	strategy=1;
}

void CPipelineDoc::OnTaken() 
{
	// 设置Taken
	set_branch_taken();
	strategy=2;
}

void CPipelineDoc::OnNotTaken() 
{
	// 设置NotTaken
	set_branch_untaken();
	strategy=3;
}


void CPipelineDoc::OnForward() 
{
	// 设置Forward
	set_forward();
	mode=1;
}


void CPipelineDoc::OnNoForward() 
{
	// 设置NoForward
	clear_forward();
	mode=2;
}

//以下为修改UI,标识当前选中
void CPipelineDoc::OnUpdateDelay(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(set_OK);
	if(strategy==4)
		pCmdUI->SetCheck(1);
	else
		pCmdUI->SetCheck(0);
}

void CPipelineDoc::OnUpdateFlush(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(set_OK);
	if(strategy==1)
		pCmdUI->SetCheck(1); //表明当前菜单栏中Flush被选中了
	else
		pCmdUI->SetCheck(0);	
}

void CPipelineDoc::OnUpdateForward(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(set_OK);
	if(mode==1)
		pCmdUI->SetCheck(1);
	else
		pCmdUI->SetCheck(0);
}

void CPipelineDoc::OnUpdateNoForward(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(set_OK);
	if(mode==2)
		pCmdUI->SetCheck(1);
	else
		pCmdUI->SetCheck(0);
}

void CPipelineDoc::OnUpdateNotTaken(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(set_OK);
	if(strategy==3)
		pCmdUI->SetCheck(1);
	else
		pCmdUI->SetCheck(0);	
}

void CPipelineDoc::OnUpdateTaken(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(set_OK);
	if(strategy==2)
		pCmdUI->SetCheck(1);
	else
		pCmdUI->SetCheck(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -