|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
乘累加器
module MAC(out,opa,opb,clk,clr);
output[15:0] out;
input[7:0] opa,opb;
input clk,clr;
wire[15:0] sum;
reg[15:0] out;
function[15:0] mult; //函数定义,mult 函数完成乘法操作
input[7:0] opa,opb; //函数只能定义输入端,输出端口为函数名本身
reg[15:0] result;
integer i;
begin
result = opa[0]? opb : 0;
for(i= 1; i <= 7; i = i+1)
begin
if(opa==1) result=result+(opb<<(i-1));
end
mult=result;
end
endfunction
assign sum=mult(opa,opb)+out;
always @(posedge clk or posedge clr)
begin
if(clr) out<=0;
else out<=sum;
end
endmodule |
|