【问题标题】:Code to add two 4-bit integers with verilog doesn't work. What is wrong?用 verilog 添加两个 4 位整数的代码不起作用。怎么了?
【发布时间】:2015-01-25 01:12:43
【问题描述】:

我有一个添加两个 4 位数字的代码;不幸的是,即使公式非常简单而且我没有发现问题,它也不适用于每种情况......

module part2(SW, LEDG, LEDR);

    input [17:0] SW;
    output [17:0] LEDR;
    output [4:0] LEDG;

    //Red lights for each one
    assign LEDR[17:0] = SW[17:0];

    //Wires between adders.
    wire carry[2:0];

    //Add the first digits of A and B
    full_adder F0(SW[4], SW[0], SW[8], LEDG[0], carry[0]);
    //Add the second digits of A and B
    full_adder F1(SW[3], SW[1], carry[0], LEDG[1], carry[1]);
    //Add the third digits of A and B
    full_adder F2(SW[6], SW[2], carry[1], LEDG[2], carry[2]);
    //Add the last digits of A and B
    full_adder F3(SW[7], SW[3], carry[2], LEDG[3], LEDG[4]);

    endmodule

module full_adder(A, B, CI, S, CO);

    input A, B, CI;
    output S, CO;

    //Sum
    assign S = A ^ B ^ CI;

    //Carry out
    assign CO = (A & B) | (CI & A) | (CI & B);

endmodule

这似乎适用于大多数情况,但看看 1 和 10 或 10 和 10 会发生什么:

【问题讨论】:

  • 问题在于wire carry[2:0] 而不是wire [2:0]carry
  • wire carry [2:0]wire [2:0] carry 在这种情况下不会有所作为。 wire [2:0] carry 表示carry 是一个压缩数组(也称为向量),您可以同时访问所有位,单独或切片。 wire carry [2:0] 是一个未打包的单个位数组,这意味着您可以访问任何索引,但不能访问整个数组或切片。

标签: verilog xilinx hdl intel-fpga quartus


【解决方案1】:

假设A 映射到SW[7:4]BSW[3:0]。那么问题出在F1 实例上; SW[3] 应该是 SW[4]

full_adder F1(SW[ <b>4 /*not 3*/</b> ], SW[1], carry[0], LEDG[1], carry[1]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多