我如何连接模块?

How do I wire modules?

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

ALU 应该是:

它必须在门​​级。无法使用 (+/-)。这是我一直在写的代码,但是当我模拟时,我只得到结果:ZZZZZZZZ。如有任何建议,我们将不胜感激,谢谢。

对于Add/Sub 1位:

module Full_Adder_1bit(a,b,cin,sel,sum,cout);
    input a, b, cin;
    input [2:0] sel;
    output sum, cout;
    reg sum, cout;

    reg a_in;

    always @ (a or b or cin or sel)
      begin
       a_in = a^sel[0];
       sum = a^b^cin;
       cout = (a_in&b)|(a_in&cin)|(b&cin);
      end
endmodule

对于 4 位 ADD/SUB:

module Full_Adder_4bits (a, b, cin, sel, sum, cout);

    input [3:0] a, b;
    input [2:0] sel;
    input cin;
    output [3:0] sum;
    output  cout;
    wire c1,c2,c3;

    Full_Adder_1bit FA0(a[0],b[0],cin,sel,sum[0],c1);
    Full_Adder_1bit FA1(a[1],b[1],c1,sel,sum[1],c2);
    Full_Adder_1bit FA2(a[2],b[2],c2,sel,sum[2],c3);
    Full_Adder_1bit FA3(a[3],b[3],c3,sel,sum[3],cout);
endmodule

对于变速杆:

module Shifter(dataIn, shiftOut); 
    output [3:0] shiftOut; 
    input [3:0] dataIn; 
    assign shiftOut = dataIn >> 1; 
endmodule 

对于 XNOR:

module XNOR(a,b,rxnor);
    input [3:0] a,b;
    output [3:0] rxnor;

    reg rxnor;
    always @ (a or b)
       begin
        rxnor= a~^b; //XNOR
       end
endmodule

对于乘数:

module mul4 (i0,i1,prod);
    input [3:0] i0, i1;
    output [7:0] prod;
    assign prod = i0*i1;
endmodule

比较:

module Compare(B,A,R);
    input  [3:0] A,B;
    output [3:0] R;
    reg     R;

    always@(A,B)
      begin
        if  (A==B)
        R = 4'b0001;
        else if (A==B)
        R = 4'b0000;
        else 
        R = 4'b1111;
      end
 endmodule

对于多路复用器(实际上是 5 比 1,尽管名称说 6 比 2):

 module MUX6to2(i0,i1,i2,i3,i4,sel,out);
  input [4:0] i0,i1,i2,i4;
  input [7:0] i3;
  input [2:0] sel;
  output [7:0] out;
  reg [7:0] out;

  always @ (i0 or i1 or i2 or i3 or i4 or sel)

    begin
     case (sel)
     3'b000: out = i0;
     3'b001: out = i0;
     3'b010: out = i1;
     3'b011: out = i2;
     3'b100: out = i3;
     3'b100: out = i4;
     default: out = 8'bx;
     endcase
   end
endmodule

对于 ALU:

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);

Full_Adder_4bitsShifter 等是模块的名称,不是有效的信号名称。你的意思大概是这样的:

Mux6to2 output_mux (adder_out, shifter_out, xnor_out, mul_out, compare_out, sel, r);

为什么输出值 "r" Hi-Z 是你没有连接任何东西到输出所以 wire 的默认值在输出中传播

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

所需的位宽为

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 位,因此其他位应设为常量值,否则它将默认为 X(如果是 reg)或 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

试试代码 http://www.edaplayground.com/x/DaF

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

MUX6to2 output_mux (Full_Adder_4bits, Shifter, XNOR, mul4, Compare, sel[2:0], r); 在此模块中,声明的输出大小为 8 位 r。但是,在某些情况下 add_out, shift_out, xnor_out, compare_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