在数字芯片设计中,通常把完成特定功能且相对独立的代码编写成子模块,在需要的时候再在主模块中例化使用,以提高代码的可复用性和设计的层次性,方便后续的修改。
请编写一个子模块,将输入两个8bit位宽的变量data_a,data_b,并输出data_a,data_b之中较小的数。并在主模块中例化,实现输出三个8bit输入信号的最小值的功能。
子模块的信号接口图如下:
主模块的信号接口图如下:
使用Verilog HDL实现以上功能并编写testbench验证。
在数字芯片设计中,通常把完成特定功能且相对独立的代码编写成子模块,在需要的时候再在主模块中例化使用,以提高代码的可复用性和设计的层次性,方便后续的修改。
请编写一个子模块,将输入两个8bit位宽的变量data_a,data_b,并输出data_a,data_b之中较小的数。并在主模块中例化,实现输出三个8bit输入信号的最小值的功能。
主模块的信号接口图如下:
使用Verilog HDL实现以上功能并编写testbench验证。
clk:系统时钟rst_n:异步复位信号,低电平有效a,b,c:8bit位宽的无符号数
d:8bit位宽的无符号数,表示a,b,c中的最小值
wire [7:0] min_a_b; gen_min Inst_a_b ( .clk (clk), .rst_n (rst_n), .a (a), .b (b), .c (min_a_b) ); wire [7:0] min_a_b_c; gen_min Inst_a_b_c ( .clk (clk), .rst_n (rst_n), .a (min_a_b), .b (c), .c (min_a_b_c) ); reg [7:0] min_abc_1d; reg [7:0] min_abc_2d; always @ (posedge clk&nbs***bsp;negedge rst_n) begin if (~rst_n) begin min_abc_1d <= 'd0; min_abc_2d <= 'd0; end else begin min_abc_1d <= min_a_b_c; min_abc_2d <= min_abc_1d; end end assign d = min_abc_2d; endmodule
module gen_min ( input clk, input rst_n, input [7:0] a, input [7:0] b, output reg [7:0] c ); always @ (*) begin if (~rst_n) begin c <= 'd0; end else begin if (a <= b) begin c <= a; end else begin c <= b; end end end endmodule
`timescale 1ns/1ns module sub_mod ( input clk, input rst_n, input [7:0] a, input [7:0] b, output reg [7:0] c ); wire [8:0] less; wire [7:0] d; wire [7:0] e; generate genvar i; assign less[0] = 0; for(i=7;i>=0;i=i-1)begin // assign less[i+1]= (a[i]^b[i])?a[i]:less[i]; assign e[i] = a[i]^b[i]; assign less[i+1]= (e[i]&a[i])|((~e[i])&less[i]); assign d[i] = (less[8]&b[i])|((~less[8])&a[i]); end endgenerate always@(posedge clk&nbs***bsp;negedge rst_n) begin if(!rst_n) c <= 8'b0; else c <= d; end endmodule module main_mod( input clk, input rst_n, input [7:0]a, input [7:0]b, input [7:0]c, output wire [7:0]d ); wire [7:0] min_ab; wire [7:0] min_ac; wire [7:0] min_abc; sub_mod sub_mod_U0( .clk (clk), .rst_n (rst_n), .a (a), .b (b), .c (min_ab) ); sub_mod sub_mod_U1( .clk (clk), .rst_n (rst_n), .a (a), .b (c), .c (min_ac) ); sub_mod sub_mod_U2( .clk (clk), .rst_n (rst_n), .a (min_ab), .b (min_ac), .c (min_abc) ); assign d = min_abc; endmodule
`timescale 1ns/1ns module main_mod( input clk, input rst_n, input [7:0]a, input [7:0]b, input [7:0]c, output [7:0]d ); wire [ 7:0] tmp; reg [ 7:0] a_tmp; reg [ 7:0] b_tmp; reg [ 7:0] c_tmp; reg [ 1:0] cnt; always @(posedge clk, negedge rst_n) begin if (rst_n == 1'h0) cnt <= 1'h0; else if (cnt == 'h2) cnt <= 1'h0; else cnt <= cnt + 1'h1; end always @(posedge clk, negedge rst_n) begin if (rst_n == 1'h0) begin a_tmp <= 1'h0; b_tmp <= 1'h0; c_tmp <= 1'h0; end else if (cnt == 1'h0) begin a_tmp <= a; b_tmp <= b; c_tmp <= c; end end sub_mod sub_mod_inst ( clk, rst_n, a_tmp, b_tmp, tmp ); sub_mod sub_mod_inst1 ( clk, rst_n, tmp, c_tmp, d ); endmodule //********************************************************* module sub_mod ( input clk, input rst_n, input [ 7:0] a, input [ 7:0] b, output [ 7:0] c ); assign c = a > b ? b : a; endmodule
有大佬可以解释一下为什么12,13行报错吗。我感觉我的语法没出现啥问题。 timescale 1ns/1ns module main_mod( input clk, input rst_n, input [7:0]a, input [7:0]b, input [7:0]c, output [7:0]d ); wire [7:0]w1; compare_1(.clk(clk),.rst_n(rst_n),.a(a),.b(b),.c(w1)); compare_2(.clk(clk),.rst_n(rst_n),.a(w1),.b(c),.c(d)); endmodule module compare( input clk, input rst_n, input [7:0]a, input [7:0]b, output [7:0]c ); always @(posedge clk&nbs***bsp;negedge rst_n) begin if(~rst_n) c=0; else if (a<b) c=a; else c=b; end endmodule