verilog - Verilog 中的子模块

标签 verilog

在 Verilog 中,您可以简单地使用模块语法创建模块。如何创建多个模块并调用另一个模块?

我的主模块有以下模块:

module Lab7Part1(SW, HEX0, HEX1, HEX2, HEX3);
        input  [15:0] SW;
        output [7:0] HEX0;
        output [7:0] HEX1;
        output [7:0] HEX2;
        output [7:0] HEX3;

        reg [7:0] OUTT;

        reg [7:0] ONE    = 8'b1001111;

        //SingleConverter sin_1(SW[3:0],   HEX0);
        //SingleConverter sin_2(SW[7:4],   HEX1);
        //SingleConverter sin_3(SW[11:8],  HEX2);
        //SingleConverter sin_4(SW[15:12], HEX3);
endmodule

以及我希望在主模块中“调用”的以下“子模块”:

module SingleConverter(SW, HEX);
    input  SW;
    output HEX;

    reg [7:0] OUT;

    reg [7:0] ZERO   = 8'b1000000;
    reg [7:0] ONE    = 8'b1001111;
    reg [7:0] TWO    = 8'b0100100;
    reg [7:0] THREE  = 8'b0110000;
    reg [7:0] FOUR   = 8'b0011001;
    reg [7:0] FIVE   = 8'b0010010;
    reg [7:0] SIX    = 8'b0000010;
    reg [7:0] SEVEN  = 8'b1111000;
    reg [7:0] EIGHT  = 8'b0000000;
    reg [7:0] NINE   = 8'b0011000;
    reg [7:0] DC     = 8'b1111111;

    always@(1'b0==1'b0)
    begin
        // SW
        if (SW == 4'b0000)
        begin
        // 0
            OUT[7:0] = ZERO;
        end
        if (SW == 4'b0001)
        begin
        // 1
            OUT[7:0] = ONE;
        end
        if (SW == 4'b0010)
        begin
        //2
            OUT[7:0] = TWO;
        end
        if (SW == 4'b0011)
        begin
        // 3
            OUT[7:0] = THREE;
        end
        if (SW == 4'b0100)
        begin
        // 4
            OUT[7:0] = FOUR;
        end
        if (SW == 4'b0101)
        begin
        //5
            OUT[7:0] = FIVE;
        end
        if (SW == 4'b0110)
        begin
        // 6
            OUT[7:0] = SIX;
        end
        if (SW == 4'b0111)
        begin
        // 7
            OUT[7:0] = SEVEN;
        end
        if (SW == 4'b1000)
        begin
        //8
            OUT[7:0] = EIGHT;
        end
        if (SW == 4'b1001)
        begin
        // 9
            OUT[7:0] = NINE;
        end
        if (SW > 4'b1001)
        begin
        // dc
            OUT[7:0] = DC;
        end
    end

    assign HEX = OUT;

endmodule

最佳答案

您已经在顶部模块中放置了 4 个 SingleConverter 实例;您所需要做的就是取消它们的注释。

然后,在 SingleConverter 中您需要更改:

input  SW;
output HEX;

至:

input  [3:0] SW;
output [7:0] HEX;

这看起来很奇怪:

always@(1'b0==1'b0)

您可能想要:

always @*

关于verilog - Verilog 中的子模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357756/

相关文章:

verilog - 2 个连续的非阻塞赋值

command-line-arguments - 如何在systemverilog中获取作为plusargs的值数组?

verilog - 如何每 n 个时钟周期切换一个采样时钟?

verilog - 在verilog中判断总线是否包含单个x的最佳方法是什么?

verilog - 多项式流水线

verilog - 在 Verilog 中编码 RAM 的更好方法

function - Verilog 函数 - 无法找出我的错误

ruby - 使用 Ruby 创建 Verilog 解析器

tcl - TCl脚本错误的解决办法是什么?

verilog - 为什么 begin/end 允许我在 SystemVerilog 任务的中途声明变量?