如何编写连续的 case 语句?
How to write consecutive case statements?
我接到一个任务,要在 Verilog 中制作一个带有无符号输入的 4 位 booth 乘法器。
在此之前我只用过几次verilog,所以我不太熟悉在里面写case
语句。
module booth(ina,inb,out);
input [3:0] ina, inb;
output[7:0] out;
reg[2:0] p1,p2;
reg[5:0] temp1, temp2;
assign p1={inb[2],inb[1],0};
assign p2={inb[3],inb[2],inb[1]};
always @(*)
begin
out = 8'b00000000;
case(p1)
3'b000: temp1=0;
3'b010: temp1=ina;
3'b100:temp1=-2 * ina;
3'b110:temp1= -ina;
endcase
end
begin
case(p2)
3'b000,3'b111: temp2=0;
3'b001,3'b010: temp2=ina;
3'b011:temp2=2 * ina;
3'b100:temp2=-2 * ina;
3'b101,3'b110:temp2= -ina;
endcase
temp2 = temp2<<2;
end
assign out=temp1+temp2;
endmodule
我应该如何连续写两个 case
语句?
我收到语法错误:
syntax error near text: "begin"; expecting "endmodule". Check for and fix any syntax errors that appear immediately before or at the specified keyword.
有几个编译错误。
您的错误消息可能指的是您的 always
语句之后的 2 个连续 begin/end
块。解决此问题的一种方法是添加第二个 always
语句。以这种方式分离 temp1
和 temp2
逻辑是个好主意。
我也收到 p1
的错误,因为您使用 assign
语句对其进行了连续赋值。您应该将信号声明为 wire
,而不是 reg
。变化:
reg [2:0] p1,p2;
至:
wire [2:0] p1,p2;
另一个问题是您对 out
信号进行了多次赋值。我认为您可能想将其从 always
块中删除。
最后,您需要在 p1
语句中为 0 使用大小常量。变化:
assign p1={inb[2],inb[1],0};
至:
assign p1={inb[2],inb[1],1'b0};
这段代码对我来说编译得很干净:
module booth (ina, inb, out);
input [3:0] ina, inb;
output [7:0] out;
wire [2:0] p1,p2;
reg [5:0] temp1, temp2;
assign p1 = {inb[2], inb[1], 1'b0};
assign p2 = {inb[3], inb[2], inb[1]};
always @(*) begin
case (p1)
3'b000: temp1 = 0;
3'b010: temp1 = ina;
3'b100: temp1 = -2 * ina;
3'b110: temp1 = -ina;
endcase
end
always @(*) begin
case(p2)
3'b000,3'b111: temp2 = 0;
3'b001,3'b010: temp2 = ina;
3'b011: temp2 = 2 * ina;
3'b100: temp2 = -2 * ina;
3'b101,3'b110: temp2 = -ina;
endcase
temp2 = temp2<<2;
end
assign out = temp1+temp2;
endmodule
我接到一个任务,要在 Verilog 中制作一个带有无符号输入的 4 位 booth 乘法器。
在此之前我只用过几次verilog,所以我不太熟悉在里面写case
语句。
module booth(ina,inb,out);
input [3:0] ina, inb;
output[7:0] out;
reg[2:0] p1,p2;
reg[5:0] temp1, temp2;
assign p1={inb[2],inb[1],0};
assign p2={inb[3],inb[2],inb[1]};
always @(*)
begin
out = 8'b00000000;
case(p1)
3'b000: temp1=0;
3'b010: temp1=ina;
3'b100:temp1=-2 * ina;
3'b110:temp1= -ina;
endcase
end
begin
case(p2)
3'b000,3'b111: temp2=0;
3'b001,3'b010: temp2=ina;
3'b011:temp2=2 * ina;
3'b100:temp2=-2 * ina;
3'b101,3'b110:temp2= -ina;
endcase
temp2 = temp2<<2;
end
assign out=temp1+temp2;
endmodule
我应该如何连续写两个 case
语句?
我收到语法错误:
syntax error near text: "begin"; expecting "endmodule". Check for and fix any syntax errors that appear immediately before or at the specified keyword.
有几个编译错误。
您的错误消息可能指的是您的 always
语句之后的 2 个连续 begin/end
块。解决此问题的一种方法是添加第二个 always
语句。以这种方式分离 temp1
和 temp2
逻辑是个好主意。
我也收到 p1
的错误,因为您使用 assign
语句对其进行了连续赋值。您应该将信号声明为 wire
,而不是 reg
。变化:
reg [2:0] p1,p2;
至:
wire [2:0] p1,p2;
另一个问题是您对 out
信号进行了多次赋值。我认为您可能想将其从 always
块中删除。
最后,您需要在 p1
语句中为 0 使用大小常量。变化:
assign p1={inb[2],inb[1],0};
至:
assign p1={inb[2],inb[1],1'b0};
这段代码对我来说编译得很干净:
module booth (ina, inb, out);
input [3:0] ina, inb;
output [7:0] out;
wire [2:0] p1,p2;
reg [5:0] temp1, temp2;
assign p1 = {inb[2], inb[1], 1'b0};
assign p2 = {inb[3], inb[2], inb[1]};
always @(*) begin
case (p1)
3'b000: temp1 = 0;
3'b010: temp1 = ina;
3'b100: temp1 = -2 * ina;
3'b110: temp1 = -ina;
endcase
end
always @(*) begin
case(p2)
3'b000,3'b111: temp2 = 0;
3'b001,3'b010: temp2 = ina;
3'b011: temp2 = 2 * ina;
3'b100: temp2 = -2 * ina;
3'b101,3'b110: temp2 = -ina;
endcase
temp2 = temp2<<2;
end
assign out = temp1+temp2;
endmodule