📄 alu.h
字号:
#ifndef ALU_H
#define ALU_H
#include "STDAFX.h"
SC_MODULE(ALU)
{
//输入的两个计算数
sc_in<sc_uint<32> > A,B;
//计算函数
sc_in<sc_uint<6> > ALUC;
//计算结果
sc_out<sc_uint<32> > R;
void cal()
{
unsigned int aluc= ALUC.read().to_uint();
sc_uint<32> out;
//判断计算类型
switch(aluc)
{
//add
case 32:
out=A.read()+B.read();
R.write(out);
break;
//sub
case 34:
out=A.read()-B.read();
R.write(out);
break;
//and
case 36:
out=A.read().to_uint() & B.read().to_uint();
R.write(out);
break;
//or
case 37:
out=A.read().to_uint() | B.read().to_uint();
R.write(out);
break;
//sll
case 0:
//此时 sa=A;
out=B.read();
if (A.read()[4]==1) out=out<<16;
if (A.read()[3]==1) out=out<<8;
if (A.read()[2]==1) out=out<<4;
if (A.read()[1]==1) out=out<<2;
if (A.read()[0]==1) out=out<<1;
R.write(out);
break;
//srl
case 2:
out=B.read();
if (A.read()[4]==1) out=out>>16;
if (A.read()[3]==1) out=out>>8;
if (A.read()[2]==1) out=out>>4;
if (A.read()[1]==1) out=out>>2;
if (A.read()[0]==1) out=out>>1;
R.write(out);
break;
//sra
case 3:
out=B.read();
int sign=B.read()[31];
if (A.read()[4]==1) { out=out>>16; if (sign==1) out=out | 0xffff0000; }
if (A.read()[3]==1) { out=out>>8; if (sign==1) out=out | 0xff000000; }
if (A.read()[2]==1) { out=out>>4; if (sign==1) out=out | 0xf0000000; }
if (A.read()[1]==1) { out=out>>2; if (sign==1) out=out | 3221225472; }
if (A.read()[0]==1) { out=out>>1; if (sign==1) out=out | 2147483648; }
R.write(out);
break;
}
}
SC_CTOR(ALU)
{
SC_METHOD(cal);
sensitive<<A<<B<<ALUC;
}
};
/*
SC_MODULE(test_ALU)
{
sc_out<sc_uint<32> > A,B;
sc_out<sc_uint<6> > ALUC;
sc_in<sc_uint<32> > R;
sc_in<bool> isZero;
void test()
{
int i;
cout<<"ALU TEST BEGIN!"<<endl;
//测试加法
A.write(100);
B.write(200);
ALUC.write(32);
wait();
Print(A.read());
Print(B.read());
Print(ALUC.read(),0);
Print(R.read());
cout<<isZero<<endl;
cout<<endl;
//测试加法2
A.write(4000000000);
B.write(4000000000);
ALUC.write(32);
wait();
Print(A.read());
Print(B.read());
Print(ALUC.read(),0);
Print(R.read());
cout<<endl;
//测试减法
A.write(200);
B.write(200);
ALUC.write(34);
wait();
Print(A.read());
Print(B.read());
Print(ALUC.read(),0);
Print(R.read());
cout<<isZero<<endl;
cout<<endl;
//测试sll移位
A.write(7);
B.write(200);
ALUC.write(0);
wait();
Print(A.read());
Print(B.read());
Print(ALUC.read(),0);
Print(R.read());
cout<<endl;
//测试sra移位
A.write(7);
B.write(4043308928);
ALUC.write(3);
wait();
Print(A.read());
Print(B.read());
Print(ALUC.read(),0);
Print(R.read());
}
SC_CTOR(test_ALU)
{
SC_THREAD(test);
sensitive<<R;
//dont_initialize();
}
};
void ALU_test()
{
ALU alu("ALU");
test_ALU test("test_ALU");
sc_signal<sc_uint<32> > A,B,R;
sc_signal<sc_uint<6> > ALUC;
sc_signal<bool> isZero;
alu.A(A);
alu.B(B);
alu.R(R);
alu.ALUC(ALUC);
alu.isZero(isZero);
test.A(A);
test.B(B);
test.R(R);
test.ALUC(ALUC);
test.isZero(isZero);
sc_start(100);
}
*/
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -