32位ALU设计(verilog实现)
32位ALU设计
算术逻辑单元(arithmetic and logic unit) 是能实现多组算术运算和逻辑运算的组合逻辑电路,简称ALU。算术逻辑单元是中央处理器(CPU)的执⾏单元,是所有中央处理器的核⼼组成部分,由"And Gate"(与门) 和"Or Gate"(或门)构成的算术逻辑单元,主要功能是进⾏⼆位元的算术运算,如加减乘(不包括整数除法)。基本上,在所有现代CPU体系结构中,⼆进制都以补码的形式来表⽰。
设计思路
设计实现
仿真测试
⽂章⽬录
⼀、设计思路
1. 算术运算
addu、subu
addu: 最⾼有效位向⾼位有进位,产⽣进位carryout
subu: 当被减数⼩于减数时,最⾼有效位向⾼位有借位,产⽣借位carryout
add、sub爱情礼物
[x]补+[y]补=[x+y]补
[x-y]补=[x]补-[y]补=[x]补+[-y]补
[-y补]=~[y]补+1
儿童网上学英语哪个网站好
add溢出:两个正数相加产⽣负数0111(7)+0110(6)=1101
两个负数相加产⽣正数1000(-8)+1001(-7)=0001
⼀个正数与⼀个负数相加不产⽣溢出
sub溢出:正数 - 正数(不产⽣溢出)
正数 - 负数,结果为负数(上溢)
负数 - 负数(不产⽣溢出)
负数 - 正数,结果为正数(下溢)
fortminor2. 逻辑运算
~:按位取反
&(and):按位与操作
|(or):按位或操作
^(xor):按位异或操作
~(|)(nor):按位或⾮操作
指令对标志位影响:指令执⾏后,CF和OF置0,ZF根据结果是否为0设置
3.⽐较运算
有符号数⽐较 slt
指令对标志位影响:in0⼩于in1置CF为1
in0为正数,in1为负数,out为0
in0为负数,in1为正数,out为1
in0和in1为负数,alu进⾏补码运算,1111(-1)、1110(-2),直接进⾏数值⽐较的结果与其代表的有符号数⽐较结果相同in0和in1为正数,直接进⾏⽐较
通过分析,后两种情况可以合并
⽆符号数⽐较 sltu
可直接进⾏⽐较
指令对标志位影响:in0⼩于in1置OF为1
4.移位运算
逻辑左移shl (shift logical left):sll,sllv
将数据向左移动,最低位⽤0补充
逻辑右移shr (shift logical right):srl,srlv
将数据向右移动,最⾼位⽤0补充
算术右移sar (shift arithmetic right):sra,srav
将各位依次右移指定位数,然后在左侧⽤原符号位补齐
指令对标志的影响:将最后移出的移位写⼊CFbatter
总结来说,这六条移位操作指令可以分为两种情况:sllv、srav、srlv这3条指令的助记符最后有“v”,表⽰移位位数是通过寄存器的值确定的,sll、sra、srl这3条指令的助记符最后没有“v”,表⽰移位位数就是指令中6-10bit的sa的值。通过循环的⽅式,将移位后还在[31:0]部分的位置移到新的rd中,其他位置补上应补的0或1。
⼆、设计实现
module ALU32(
op,in0,in1,
carryout,overflow,zero,out
);
input [31:0] in0,in1;
input [10:0] op;
output reg[31:0] out;
output reg carryout,overflow,zero;
always@(*)
begin
ca(op)
//add
11'b00000100000:
begin
out=in0+in1;
overflow=((in0[31]==in1[31])&&(~out[31]==in0[31]))?1:0;
zero=(out==0)?1:0;
carryout=0;
end
end
//addu
11'b00000100001:
begin
{carryout,out}=in0+in1;
zero=(out==0)?1:0;
overflow=0;
end
//sub
11'b00000100010:
begin
out=in0-in1;
overflow=((in0[31]==0&&in1[31]==1&&out[31]==1)|| (in0[31]==1&&in1[31]==0&&out[31]==0))?1:0; zero=(in0==in1)?1:0;
carryout=0;
end
//subu
11'b00000100011:
begin
{carryout,out}=in0-in1;
zero=(out==0)?1:0;
overflow=0;
end
//and
11'b00000100100:
begin
out=in0&in1;
zero=(out==0)?1:0;
carryout=0;
overflow=0;
end
//or
11'b00000100101:
begin
out=in0|in1;
zero=(out==0)?1:0;
carryout=0;
overflow=0;
end
//xor
11'b00000100110:
begin
out=in0^in1;
zero=(out==0)?1:0;
carryout=0;
overflow=0;
end
//nor
11'b00000100111:
begin
out=~(in0|in1);
zero=(out==0)?1:0;
carryout=0;
overflow=0;
end
rides//slt
11'b00000101010:
begin
if(in0[31]==1&&in1[31]==0)
out=1;
el if(in0[31]==0&&in1[31]==1)
out=0;
el
out=(in0<in1)?1:0;
overflow=out;empty怎么读
zero=(out==0)?1:0;
carryout=0;
end
//sltu
11'b00000101011:
begin
out=(in0<in1)?1:0;
carryout=out;
zero=(out==0)?1:0;
overflow=0;
end
//shl
11'b00000000100:
begin
{carryout,out}=in0<<in1;
overflow=0;
zero=(out==0)?1:0;
put out
end
//shr
11'b00000000110:
begin
out=in0>>in1;
carryout=in0[in1-1];
overflow=0;
zero=(out==0)?1:0;
沈阳外语培训end
//sar
11'b00000000111:
begin
out=($signed(in0))>>>in1;
carryout=in0[in1-1];
overflow=0;
zero=(out==0)?1:0;
end
endca
end
endmodule
module ALU32_test(
);
reg [10:0] op;
reg [31:0] in0,in1;
wire [31:0] out;
wire carryout,overflow,zero;
ALU32 alu(op,in0,in1,carryout,overflow,zero,out); initial
begin
//add
op=11'b00000100000;
in0=32'hf2340000;
in1=32'h80000000;
#20 in0=32'h7fffffff;
in1=32'h70000001;
#20 in0=32'h7fffffff;
in1=32'hf0000001;
#20 in0=32'hffffffff;
in1=32'h00000001;
//addu
#20 op=11'b00000100001;
in0=32'hf2340000;
in1=32'h80000000;
#20 in0=32'h7fffffff;
in1=32'h70000001;
#20 in0=32'hffffffff;
#20 in0=32'hffffffff;
in1=32'h00000001;
//sub
#20 op=11'b00000100010; in0=32'h72340000;
in1=32'h60000000;
#20 in0=32'h7fffffff;
in1=32'hf0000001;
#20 in0=32'hf00fffff;
in1=32'h7ffffff1;
#20 in0=32'hffffffff;
in1=32'hffffffff;
#20 in0=32'hf0000000;
in1=32'h0fffffff;
菜谱英文翻译//subu
#20 op=11'b00000100011; in0=32'h72340000;
in1=32'h60000000;
#20 in0=32'h7fffffff;
in1=32'hf0000001;
#20 in0=32'hffffffff;
in1=32'hffffffff;
#20 in0=32'hf0000000;
in1=32'h0fffffff;
//and
#20 op=11'b00000100100; in0=32'h72340000;
in1=32'h60000000;
#20 in0=32'h7fffffff;
in1=32'h00000000;
//or
#20 op=11'b00000100101; in0=32'h00000000;
in1=32'h00000000;
#20 in0=32'h7fffffff;
in1=32'hf0000001;
/
/xor
#20 op=11'b00000100110; in0=32'ha0000000;
in1=32'h50000000;
#20 in0=32'h7fffffff;
in1=32'hf0000001;
//nor
#20 op=11'b00000100111; in0=32'h123451ff;
in1=32'h60000000;
#20 in0=32'h7fffffff;
in1=32'hf0000001;
//slt
#20 op=11'b00000101010; in0=32'h72340000;
in1=32'hf0000000;
#20 in0=32'h7000000f;
in1=32'h7f000001;
#20 in0=32'hf0001231;
in1=32'h7ac34545;
//sltu
#20 op=11'b00000101011; in0=32'h72340000;
in1=32'hf0000000;
#20 in0=32'h7000000f;
in1=32'h7f000001;
散文朗诵技巧
#20 in0=32'hf0001231;
in1=32'h7ac34545;
//shl
#20 op=11'b00000000100;