我需要在Verilog中开发一个ALU,我需要在结构上进行。换句话说,我不能使用像if语句或case和( - ,+,/,*)之类的操作。我想解决一些问题。
8b_2to1
多路复用器。module xor_gate
就是这样。我的代码:
module alu;
reg [7:0] a, b;
reg [2:0] op;
output [7:0] out;
wire [7:0] shift_out;
wire [7:0] and_out;
wire [7:0] or_out;
wire [7:0] xor_out;
wire [7:0] sum;
wire [7:0] out1, out2, out3;
rca add_sub (a, b, op[0], sum);
shifter shift (a, b, shift_out);
and_gate gate1 (a, b, and_out);
or_gate gate2 (a, b, or_out);
xor_gate gate3 (a, b, xor_out);
8b_2to1 first (shift_out, xor_out, op[0], out1);
8b_2to1 second (or_out, and_out, op[0], out2);
8b_2to1 third (sum, out2, op[1], out3);
8b_2to1 fourth (out3, out2, op[2], out);
initial
begin
$monitor($time,,"a=%b, b=%b, op=%b, out=%b",a,b,op,out);
#10 a=8'b00011001; b=8'b00011110; op=3'b000;
#10 a=8'b00011110; b=8'b00011001; op=3'b001;
#10 a=8'b00001010; b=8'b00000011; op=3'b010;
#10 a=8'b00001111; b=8'b00000011; op=3'b011;
#10 a=8'b00011111; b=8'b00001100; op=3'b100;
#10 a=8'b00001100; b=8'b00000101; op=3'b101;
#10 $finish;
end
endmodule
module shifter(a,b,out);
input [7:0] a, b;
output [7:0] out;
endmodule
module fa(a,b,cin,cout,sum);
input a;
input b;
input cin;
output cout;
output sum;
assign sum = ((a^b)^(cin));
assign cout = ((a&b)|((a^b)&cin));
endmodule
module rca(a,b,cin,cout,sum);
input cin;
output cout;
output [7:0] sum;
input [7:0] a, b;
wire [6:0] c;
wire [7:0] invert_b;
inverter invert (.b(b),.invert_b(invert_b));
8b_2to1 mux (a, b, cin, out);
fa first(a[0],b[0],cin,c[0],sum[0]);
fa second(a[1],b[1],c[0],c[1],sum[1]);
fa third(a[2],b[2],c[1],c[2],sum[2]);
fa fourth(a[3],b[3],c[2],c[3],sum[3]);
fa fifth(a[4],b[4],c[3],c[4],sum[4]);
fa sixth(a[5],b[5],c[4],c[5],sum[5]);
fa seventh(a[6],b[6],c[5],c[6],sum[6]);
fa eighth(a[7],b[7],c[6],cout,sum[7]);
endmodule
module inverter(a, out);
input [7:0] a;
output [7:0] out;
assign out = ~a;
endmodule
module xor_gate(a,b,logic);
input a;
input b;
output logic;
assign logic = (a^b);
endmodule
module and_gate(a,b,logic);
input a;
input b;
output logic;
assign logic = (a&b);
endmodule
module or_gate(a,b, logic);
input a;
input b;
output logic;
assign logic = (a|b);
endmodule
module 8b_2to1(a, b, select, out);
input [7:0] a, b;
input select;
output [7:0] out;
mux_1b_2to1 mux0(a[0],b[0],select,out[0]);
mux_1b_2to1 mux1(a[1],b[1],select,out[1]);
mux_1b_2to1 mux2(a[2],b[2],select,out[2]);
mux_1b_2to1 mux3(a[3],b[3],select,out[3]);
mux_1b_2to1 mux4(a[4],b[4],select,out[4]);
mux_1b_2to1 mux5(a[5],b[5],select,out[5]);
mux_1b_2to1 mux6(a[6],b[6],select,out[6]);
mux_1b_2to1 mux7(a[7],b[7],select,out[7]);
endmodule
module 1b_2to1(a, b, select, out);
input a, b, select;
output out;
assign out = (b & select) | (a & ~select);
endmodule