首页 > 其他分享 >HDLBits_6.25

HDLBits_6.25

时间:2023-06-26 11:11:24浏览次数:43  
标签:HDLBits module 6.25 endmodule output input sel out

4.验证:读取模拟

4.1查找代码中的错误

4.1.1 多用复路器(2_1_Mux)

//Wrong
module top_module (
    input sel,
    input [7:0] a,
    input [7:0] b,
    output out  );

    assign out = (~sel & a) | (sel & b);

endmodule

//Right:out位宽不对,表达式不对
module top_module (
    input sel,
    input [7:0] a,
    input [7:0] b,
    output [7:0] out);

    assign out = sel ? a : b;

endmodule

 

4.1.2 用5与非实现3与非(NAND)

//Wrong
module top_module (input a, input b, input c, output out);//

    andgate inst1 ( a, b, c, out );

endmodule

//Right:输入输出的对应关系不对,且参数数量也不对
module top_module (input a, input b, input c, output out);//
    
	wire and_out;
    
    andgate inst1 ( and_out, a, b, c, 1, 1);
    
    assign out = ~and_out;

endmodule

  

4.1.3 四路选择器(8bit_4_1_Mux)

//Wrong
module top_module (
    input [1:0] sel,
    input [7:0] a,
    input [7:0] b,
    input [7:0] c,
    input [7:0] d,
    output [7:0] out  ); //

    wire mux0, mux1;
    mux2 mux0 ( sel[0],    a,    b, mux0 );
    mux2 mux1 ( sel[1],    c,    d, mux1 );
    mux2 mux2 ( sel[1], mux0, mux1,  out );

endmodule

//Right:sel[1]区分不了c和d,此处应该还是sel[0]。此外例化名与变量名不能重复;且wire信号的位宽也不对
module top_module (
    input [1:0] sel,
    input [7:0] a,
    input [7:0] b,
    input [7:0] c,
    input [7:0] d,
    output [7:0] out  ); //

    wire [7:0] mux00, mux11;
    mux2 mux0 ( sel[0],    a,    b, mux00 );
    mux2 mux1 ( sel[0],    c,    d, mux11 );
    mux2 mux2 ( sel[1], mux00, mux11,  out );

endmodule

 

4.1.4 Add/Sub

//Wrong:
// synthesis verilog_input_version verilog_2001
module top_module ( 
    input do_sub,
    input [7:0] a,
    input [7:0] b,
    output reg [7:0] out,
    output reg result_is_zero
);//

    always @(*) begin
        case (do_sub)
          0: out = a+b;
          1: out = a-b;
        endcase

        if (~out)
            result_is_zero = 1;
    end

endmodule

//Right:因为result_is_zero为reg型,当其为1后一直为1,因为没有其他状态能使其改变,且需锁存状态,因为if未遍历所有状态
module top_module ( 
    input do_sub,
    input [7:0] a,
    input [7:0] b,
    output reg [7:0] out,
    output reg result_is_zero
);

    always @(*) begin
        case (do_sub)
          0: out = a+b;
          1: out = a-b;
        endcase

        if (out == 8'd0) begin
            result_is_zero = 1;
        end
        else begin
            result_is_zero = 0;
        end
    end

endmodule

 

4.1.5 case statement

//Wrong
module top_module (
    input [7:0] code,
    output reg [3:0] out,
    output reg valid=1 );//

     always @(*)
        case (code)
            8'h45: out = 0;
            8'h16: out = 1;
            8'h1e: out = 2;
            8'd26: out = 3;
            8'h25: out = 4;
            8'h2e: out = 5;
            8'h36: out = 6;
            8'h3d: out = 7;
            8'h3e: out = 8;
            6'h46: out = 9;
            default: valid = 0;
        endcase

endmodule

//Right:默认输出valid=1不能按上图所示的编写代码,默认的输入可以;其次进制8'd26需改成8进制
module top_module (
    input [7:0] code,
    output reg [3:0] out,
    output reg valid);

     always @(*) begin
         case (code)
            8'h45: out = 4'd0;
            8'h16: out = 4'd1;
            8'h1e: out = 4'd2;
            8'h26: out = 4'd3;
            8'h25: out = 4'd4;
            8'h2e: out = 4'd5;
            8'h36: out = 4'd6;
            8'h3d: out = 4'd7;
            8'h3e: out = 4'd8;
            8'h46: out = 4'd9;
            default: begin
                out = 4'd0;
            end
        endcase

         if(out == 4'd0 && code!= 8'h45) begin
            valid = 1'b0;
        end
        else begin
            valid = 1'b1;
        end
     end
     		              
endmodule

 

标签:HDLBits,module,6.25,endmodule,output,input,sel,out
From: https://www.cnblogs.com/LhTian/p/17502476.html

相关文章

  • 上周热点回顾(6.19-6.25)
    热点随笔:· 【网站公告】园子被处罚,请大家不要发布/转载任何网络小说 (博客园团队)· 程序员有没有必要成为业务领域专家? (勇哥编程游记)· 在这个大环境下我是如何找工作的 (crossoverJie)· 适合Windows桌面、MaterialDesign设计风格、WPF美观控件库【强烈推荐】 (chi......
  • 2023.6.25 圆和矩形是否有重叠
    原问题可以转换为,判断圆心到矩形的最短距离是否小于等于半径。根据这张图,可以得到矩形到圆心的距离是\(\sqrt{x^2+y^2}\),其中x和y分别是圆心和矩形的横纵坐标之差。求横纵坐标之差其实也很简单,以横坐标为例,圆心的坐标为x,矩形的坐标是x1和x2。那么就是\(min(|x-x_1|,|x-......
  • 6.25数据类型
    数字类型整数int浮点数float  如:13.14-13.14复数complex  如:4+3j以j结尾表示复数布尔bool  表达现实生活中的逻辑,即真和假,True表示真,False表示假。True本质上是一个数字记作1,False记作0字符串String 描述文本的一种数字类型,是由任意数量的字符如中文、英文、各......
  • HDLBits(16)4.18
    3电路3.2时序逻辑3.2.2计数器 Count1to10(Decadecounteragain)与上题一样,区别是复位为1moduletop_module(inputclk,inputreset,output[3:0]q);always@(posedgeclk)beginif(reset)q<=4'b0001......
  • HDLBits(1)——Modules:Hierarchy
    HDLBits——Modules:Hierarchy目录HDLBits——Modules:Hierarchy问题19Module将信息连接到端口BypositionByname问题20Connectingportsbyposition(Modulepos)问题21Connectingportsbyname(Modulename)问题22Threemodules(Moduleshift)问题23Modulesandvectors(Mod......
  • HDLBits(15)3.9
    3电路3.2时序逻辑3.2.1锁存器与触发器(LatchesandFlip-Flops)CreatecircuitfromtruthtableJK触发器的真值表如下图所示,仅使用D触发器和门电路来实现该JK触发......
  • HDLBits(13)2.24
    3电路3.1组合逻辑3.1.4卡诺线路图(KarnaughMaptoCircuit)Kmap1(3-variable)out=a+ab+ac=a&(a^b)&(a^c)=a|b|c moduletop_module(inpu......
  • HDLBits(11)2.17
    3电路3.1组合逻辑3.1.1基础门Ringorvibrate(静音)若手机处于震动模式则振动(motor),否则打开铃声(Ringer)assignringer=ring&(~vibrate_mode);assignmotor=ri......
  • HDLBits(9)10.13
    2Verilog语言2.5更多特点2.5.4组合for循环翻转输出一个长度为100的向量(使用组合always块)moduletop_module(input[99:0]in,output[99:0]out);......
  • unable to find local peer: 172.16.26.250:8848
    unabletofindlocalpeer:172.16.26.250:8848☞​​博客导航​​,​​带你有序的阅读和学习!​​文章目录​​问题描述​​​​解决方案​​​​停节点​​问题描述当我......