Module 如何连接模块?

Module 如何连接模块?,module,verilog,alu,Module,Verilog,Alu,我已经写了所有的代码,包括模块,但我不知道如何将模块连接到主程序 ALU应为: A(4位)和B(4位)作为输入,sel(3位) 当sel=000时的第一个模块(A+B或A-B) 当sel=010时,第二个模块右移1 当sel=011相乘时的第三个模块(A*B) 当sel=100时的第四个模块 当sel=101时,第五个模块比较A==B 我还用Mux6to1制作了第六个模块 它必须在门的水平面上。无法使用(+/-)。这是我一直在写的代码,但当我模拟时,我只得到结果:zzzzzz。如果您有任何建

我已经写了所有的代码,包括模块,但我不知道如何将模块连接到主程序

ALU应为:

  • A
    (4位)和
    B
    (4位)作为输入,
    sel
    (3位)
  • 当sel=000时的第一个模块(A+B或A-B)
  • 当sel=010时,第二个模块右移1
  • 当sel=011相乘时的第三个模块(A*B)
  • 当sel=100时的第四个模块
  • 当sel=101时,第五个模块比较A==B
  • 我还用Mux6to1制作了第六个模块
它必须在门的水平面上。无法使用(+/-)。这是我一直在写的代码,但当我模拟时,我只得到结果:
zzzzzz
。如果您有任何建议,我们将不胜感激,谢谢

对于增加/减少1位: 对于4位添加/订阅: 对于换档杆: 对于XNOR: 对于乘数: 供比较: 对于多路复用器(实际上是5比1,尽管名称是6比2): 对于ALU:
你的代码编译正确吗?我可以看出这一行存在一个问题:

MUX6to2 output_mux (Full_Adder_4bits, Shifter, XNOR, mul4, Compare, sel[2:0], r);
全加器\u 4位
移位器
等是模块的名称,不是有效的信号名称。你的意思可能是:

Mux6to2 output_mux (adder_out, shifter_out, xnor_out, mul_out, compare_out, sel, r);
为什么输出“r”Hi-Z的值是您没有将任何东西连接到输出,所以导线的默认值在输出中传播

当涉及到您的设计时,需要一个解码器,对于算术运算,必须注意操作数的大小

所需的位宽度为

addition    4 bit + 4 bit = 4 bit + carry,
subtraction 4 bit - 4 bit = 4 bit + borrow,
shifting which of them should be shifted and required bit width needs to be calculated,
multiplication 4*4 bit = 8 bit is needed,
xnor 4 bit xnot 4 bit = 4 bit needed,
compare it is up how we represent if equal with 1 bit or any bit widths
在Mux逻辑中,您有

 3'b100: out = i3;
 3'b100: out = i4;
在这种情况下,这两个项是相同的,因此合成器优化并只考虑第一个语句,而忽略第二个语句。一般情况下,不应以这种方式进行编码,并且不需要对多路复用器本身进行更多的编码

对于顶级模块ALU,语法是错误的,这种类型的赋值在verilog HDL中是不允许的

 MUX6to2 output_mux (Full_Adder_4bits, Shifter, XNOR, mul4, Compare, sel[2:0], r);
在集成所有模块时,你必须清楚你要连接什么,以及思维导图中硬件的宽度

考虑你的设计 加法器将产生4位+进位,但输出“r”为8位,因此其他位应设置为常量值,否则,在reg情况下,它将默认为X,在输出中的wire情况下,它将默认为Hi-z,类似于其他操作

我用解码器对设计做了一些修改,发现功能齐全

A (4bits) and B (4bits) as inputs, sel (3bits)
When sel = 000 => Add/ sel= 001 => Sub (A+B or A-B)
When sel = 010 => Shift right by 1
when sel = 011 => Multiply (A*B)
When sel = 100 => A XNOR B
When sel = 101 => Compare A==B

尝试模块ALU中的代码,检查映射到多路复用器模块的端口列表,如emman所说

MUX6to2输出(全加器4位、移位器、XNOR、mul4、比较、选择[2:0],r)
在此模块中,声明的输出大小为8位
r
。但是,在某些情况下,
add\u out、shift\u out、xnor\u out、compare\u out
大小仅为4位。因此,在这些输出的情况下,输出‘r’显示4 X

    module ALU(a,b,cin,sel,r,cout);

      input [3:0] a, b;
      input [2:0] sel;
      input cin;
      output [7:0] r;
      output cout;

      wire [3:0] add_out, shift_out, xnor_out, compare_out;
      wire [7:0] mul_out;
      wire cout;

     // MUX6to2 output_mux (Full_Adder_4bits, Shifter, XNOR, mul4, Compare, sel[2:0], r);
  MUX6to2 output_mux (add_out, shift_out, xnor_out, mul_out, compare_out, sel[2:0], r);

      Full_Adder_4bits output_adder (a,b,cin,sel [2:0],add_out,cout);
      Shifter output_shifter (a,shift_out);
      XNOR output_XNOR (a,b,xnor_out);
      mul4  output_mul4 (a,b,mul_out);
      Compare output_Compare (a,b,compare_out);

     endmodule

除了mux之外,我看不到任何问题。
3'b100有两个案例。第二个应该是
3'b101
。r永远不应该驱动Z。如果你没有正确驱动输入或逻辑不好,我可以理解X,但是这里没有任何东西会驱动Z。您可以在顶部模块中使用
case
语句并调用其他模块作为函数。您的
compare
模块在
if else
中也有一些奇怪的条件。对于门级实现,您不能使用
*
运算符使用乘法,使用吠陀或布斯乘法IP。谢谢,我更正了这两个:3'b100:out=i3;3'b100:out=i4;
module ALU(a,b,cin,sel,r,cout);

  input [3:0] a, b;
  input [2:0] sel;
  input cin;
  output [7:0] r;
  output cout;

  wire [3:0] add_out, shift_out, xnor_out, compare_out;
  wire [7:0] mul_out;
  wire cout;

  MUX6to2 output_mux (Full_Adder_4bits, Shifter, XNOR, mul4, Compare, sel[2:0], r);
  Full_Adder_4bits output_adder (a,b,cin,sel [2:0],add_out,cout);
  Shifter output_shifter (a,shift_out);
  XNOR output_XNOR (a,b,xnor_out);
  mul4  output_mul4 (a,b,mul_out);
  Compare output_Compare (a,b,compare_out);

 endmodule
MUX6to2 output_mux (Full_Adder_4bits, Shifter, XNOR, mul4, Compare, sel[2:0], r);
Mux6to2 output_mux (adder_out, shifter_out, xnor_out, mul_out, compare_out, sel, r);
addition    4 bit + 4 bit = 4 bit + carry,
subtraction 4 bit - 4 bit = 4 bit + borrow,
shifting which of them should be shifted and required bit width needs to be calculated,
multiplication 4*4 bit = 8 bit is needed,
xnor 4 bit xnot 4 bit = 4 bit needed,
compare it is up how we represent if equal with 1 bit or any bit widths
 3'b100: out = i3;
 3'b100: out = i4;
 MUX6to2 output_mux (Full_Adder_4bits, Shifter, XNOR, mul4, Compare, sel[2:0], r);
A (4bits) and B (4bits) as inputs, sel (3bits)
When sel = 000 => Add/ sel= 001 => Sub (A+B or A-B)
When sel = 010 => Shift right by 1
when sel = 011 => Multiply (A*B)
When sel = 100 => A XNOR B
When sel = 101 => Compare A==B
    module ALU(a,b,cin,sel,r,cout);

      input [3:0] a, b;
      input [2:0] sel;
      input cin;
      output [7:0] r;
      output cout;

      wire [3:0] add_out, shift_out, xnor_out, compare_out;
      wire [7:0] mul_out;
      wire cout;

     // MUX6to2 output_mux (Full_Adder_4bits, Shifter, XNOR, mul4, Compare, sel[2:0], r);
  MUX6to2 output_mux (add_out, shift_out, xnor_out, mul_out, compare_out, sel[2:0], r);

      Full_Adder_4bits output_adder (a,b,cin,sel [2:0],add_out,cout);
      Shifter output_shifter (a,shift_out);
      XNOR output_XNOR (a,b,xnor_out);
      mul4  output_mul4 (a,b,mul_out);
      Compare output_Compare (a,b,compare_out);

     endmodule