M
Mahurshi Akilla
Guest
Hey Guys,
I am pasting my code for an ALU implementation based on my old
undergrad textbook. I took some suggestions from the folks here from
my older posts, so I am hoping this code should be a bit better than
the first one in style and taste
Anyways, two questions:
1) Do you suggeston using assign statements to registers in always @*
blocks? If not, is there an alternative? I am trying to work this
out just like it is in the textbook and the module diagram doesn't
show a clock input.
2) Are there any suggestions to improvise on this piece of code?
`timescale 1ns / 1ps
module alu(
opcode,
op1,
op2,
alu_result,
zero);
parameter OW = 4; // opcode width
parameter BW = 32; // bus width
parameter op_AND = 4'b0000; //bitwise and
parameter op_OR = 4'b0001; //bitwise or
parameter op_ADD = 4'b0010;
parameter op_SUB = 4'b0110;
parameter op_SLT = 4'b0111;
parameter op_NOR = 4'b1100; //bitwise nor
input [OW-1:0] opcode;
input [BW-1:0] op1;
input [BW-1:0] op2;
output [BW-1:0] alu_result;
output zero;
reg [BW-1:0] alu_result;
reg zero;
always @*
begin
if (opcode == op_AND)
assign alu_result = op1 & op2;
else if (opcode == op_OR)
assign alu_result = op1 | op2;
else if (opcode == op_ADD)
assign alu_result = op1 + op2;
else if (opcode == op_SUB)
begin
assign alu_result = op1 - op2;
assign zero = (op1 == op2) ? 1 : 0;
end
else if (opcode == op_SLT)
assign alu_result = (op1 < op2) ? 1 : 0;
else if (opcode == op_NOR)
assign alu_result = ~(op1 | op2);
end
endmodule
Mahurshi Akilla
I am pasting my code for an ALU implementation based on my old
undergrad textbook. I took some suggestions from the folks here from
my older posts, so I am hoping this code should be a bit better than
the first one in style and taste
Anyways, two questions:
1) Do you suggeston using assign statements to registers in always @*
blocks? If not, is there an alternative? I am trying to work this
out just like it is in the textbook and the module diagram doesn't
show a clock input.
2) Are there any suggestions to improvise on this piece of code?
`timescale 1ns / 1ps
module alu(
opcode,
op1,
op2,
alu_result,
zero);
parameter OW = 4; // opcode width
parameter BW = 32; // bus width
parameter op_AND = 4'b0000; //bitwise and
parameter op_OR = 4'b0001; //bitwise or
parameter op_ADD = 4'b0010;
parameter op_SUB = 4'b0110;
parameter op_SLT = 4'b0111;
parameter op_NOR = 4'b1100; //bitwise nor
input [OW-1:0] opcode;
input [BW-1:0] op1;
input [BW-1:0] op2;
output [BW-1:0] alu_result;
output zero;
reg [BW-1:0] alu_result;
reg zero;
always @*
begin
if (opcode == op_AND)
assign alu_result = op1 & op2;
else if (opcode == op_OR)
assign alu_result = op1 | op2;
else if (opcode == op_ADD)
assign alu_result = op1 + op2;
else if (opcode == op_SUB)
begin
assign alu_result = op1 - op2;
assign zero = (op1 == op2) ? 1 : 0;
end
else if (opcode == op_SLT)
assign alu_result = (op1 < op2) ? 1 : 0;
else if (opcode == op_NOR)
assign alu_result = ~(op1 | op2);
end
endmodule
Mahurshi Akilla