ALU——加、减、与、或、移位运算
程序
module hhh (opcode,a,b,out,clk);
input[2:0]opcode;
inout[2:0] a,b;
input clk;
output out;
reg [3:0]out;
always @(posedge clk)
begin
case (opcode)
3'b001: out=a+b;
3'b010:out=a-b;
3'b011: out=a&b;
3'b100:out=a|b;
3'b101:out=a<<1;
3'b110:out=a>>1;
default:out=4'bx;
endcase
end
endmodule
加法运算
减法运算
与运算
或运算
左移运算
右移运算
5-7——四选一使用行为描述,十六选一使用结构描述
module hhh4x1(d0,d1,d2,d3,s0,s1,z);
input d0,d1,d2,d3,s0,s1;
output z;
reg
z;
always @(d0 or d1 or d2 or d3 or s0 or s1)
begin
case({s1,s0})
2'b00: z = d0;
2'b01: z = d1;
2'b10: z = d2;
2'b11: z = d3;
endcase
end
endmodule
module mux16x1(data, s, out);
input[15:0] data;
input[3:0]
wire[3:0]
output
s;
w;
out;
mux4x1(data[0], data[1], data[2], data[3], s[0],s[1],w[0]);
mux4x1(data[4], data[5], data[6], data[7], s[0],s[1],w[1]);
mux4x1(data[8], data[9], data[10],data[11],s[0],s[1],w[2]);
mux4x1(data[12],data[13],data[14],data[15],s[0],s[1],w[3]);
mux4x1(w[0],w[1],w[2],w[3],s[2],s[3],out);
endmodule