首页 > 其他分享 >【HDLBits刷题笔记】10 Counters

【HDLBits刷题笔记】10 Counters

时间:2022-10-30 21:33:20浏览次数:42  
标签:reset 10 enable clk module output input Counters 刷题

Count15

module top_module (
    input clk,
    input reset,      // Synchronous active-high reset
    output [3:0] q);
    always@(posedge clk)
    begin
        if(reset)
            q <= 4'd0;
        else
            q <= q + 1'b1;
    end
endmodule

Count10

题目给的答案把q清零和reset放在了一个if里if (reset || q == 9)。

module top_module (
    input clk,
    input reset,        // Synchronous active-high reset
    output [3:0] q);
    always@(posedge clk)
    begin
        if(reset)
            q <= 4'd0;
        else
            q <= (q<9)?(q+1):0;
    end
endmodule

Count1to10

module top_module (
    input clk,
    input reset,
    output [3:0] q);
    always@(posedge clk)
    begin
        if(reset)
            q <= 4'd1;
        else
            q <= (q<10)?(q+1):1;
    end
endmodule

Countslow

module top_module (
    input clk,
    input slowena,
    input reset,
    output [3:0] q);
    always@(posedge clk)
    begin
        if(reset)
            q <= 4'd0;
        else if(slowena)
            q <= (q<9)?(q+1):0;
    end
endmodule

Exams/ece241 2014 q7a

这道题感觉怪怪的,一开始我写的c_load = (Q==12)|reset;由于题目说了count4模块中的load优先级比enable高,所以enable为0时仍可以置数。

而题目貌似是希望load和enable不能冲突,enable为0时就不应该置数了,所以此时的load应该为0,所以我又把信号与上了一个enable。

module top_module (
    input clk,
    input reset,
    input enable,
    output [3:0] Q,
    output c_enable,
    output c_load,
    output [3:0] c_d
); //
    count4 the_counter (clk, c_enable, c_load, c_d , Q );
    assign c_d = 1;
    assign c_enable = enable;
    assign c_load = enable&(Q==12)|reset;
endmodule

Exams/ece241 2014 q7b

把三个BCD计数器级联在一起即可。一开始我写的c_enable[2]=(Q2==9);OneHertz=(Q3==9);而Q2、Q3都会持续多个周期,只有Q1是一个一直变的信号。

module top_module (
    input clk,
    input reset,
    output OneHertz,
    output [2:0] c_enable
); //
    wire [3:0]Q1;
    wire [3:0]Q2;
    wire [3:0]Q3;
    bcdcount counter0 (clk, reset, c_enable[0],Q1);
    bcdcount counter1 (clk, reset, c_enable[1],Q2);
    bcdcount counter2 (clk, reset, c_enable[2],Q3);
    assign c_enable[0]=1'b1;
    assign c_enable[1]=(Q1==9);
    assign c_enable[2]=(Q2==9)&&(Q1==9);
    assign OneHertz=(Q3==9)&&(Q2==9)&&(Q1==9);
endmodule

Countbcd

感觉写的稍微有点复杂,利用了verilog里写多个if优先下面的语句的特点,实际不会对一个数重复操作。

实际例化多个BCD计数器会简单很多。

module top_module (
    input clk,
    input reset,   // Synchronous active-high reset
    output [3:1] ena,
    output [15:0] q);
    always@(posedge clk)
    begin
        if(reset)
            q <= 'd0;
        else begin
            q[3:0] <= q[3:0] + 1;
            if(ena[1])begin
                q[3:0] <= 0;
                q[7:4] <= q[7:4]+1;
            end
            if(ena[2])begin
                q[3:0] <= 0;
                q[7:4] <= 0;
                q[11:8] <= q[11:8]+1;
            end
            if(ena[3])begin
                q[3:0] <= 0;
                q[7:4] <= 0;
                q[11:8] <= 0;
                q[15:12] <= (q[15:12]<9)?q[15:12] +1:0;
            end
        end
    end
    assign ena[1] = (q[3:0]==9);
    assign ena[2] = (q[7:4]==9)&&ena[1];
    assign ena[3] = (q[11:8]==9)&&ena[2];
endmodule

Count clock

这道题稍微有点复杂,难度也不算特别大,但是需要耐心debug,把情况全部考虑清楚。

我提交了五次才通过,可恶。

module top_module(
    input clk,
    input reset,
    input ena,
    output reg pm,
    output reg[7:0] hh,
    output reg[7:0] mm,
    output reg[7:0] ss); 
    
    always@(posedge clk)
    begin
        if(reset)begin
            pm <= 1'b0;
            hh <= {4'd1,4'd2};
            mm <= 0;
            ss <= 0;
        end
        else if(ena)begin
            ss[3:0] <= ss[3:0] +1'b1;
            if(ss[3:0] == 9)begin
                ss[3:0] <= 'd0;
                ss[7:4] <= (ss[7:4]==5)?0:(ss[7:4]+1);
            end
            if(ss[3:0] == 9&&ss[7:4] == 5)begin
                if(mm[3:0]<9)
                    mm[3:0] <= mm[3:0] + 1;
                else begin
                    mm[3:0] <= 'd0;
                    mm[7:4] <= (mm[7:4]==5)?0:(mm[7:4]+1);
                end
            end
            if(ss[3:0] == 9&&ss[7:4] == 5&&mm[3:0] == 9&&mm[7:4] == 5)begin
                if(hh[3:0]<9&&hh[7:4] == 0)
                    hh[3:0] <= hh[3:0] + 1;
                else if(hh[3:0]<2&&hh[7:4] == 1)
                    hh[3:0] <= hh[3:0] + 1;
                else begin
                    if(hh[7:4] == 1)
                        hh[3:0] <= 'd1;//注意这里是1
                    else
                        hh[3:0] <= 'd0;
                    hh[7:4] <= (hh[7:4]==1)?0:1;
                end                
            end
            if(ss[3:0] == 9&&ss[7:4] == 5&&mm[3:0] == 9&&mm[7:4] == 5&&hh[3:0]==1&&hh[7:4]==1)
                pm<=~pm;
        end
    end

endmodule

 

标签:reset,10,enable,clk,module,output,input,Counters,刷题
From: https://www.cnblogs.com/magnolia666/p/16842297.html

相关文章

  • 求出 1- 1/2 + 1/3 -1/4 ... 1/100 的和
    importjava.util.Scanner;publicclassEext{ publicstaticvoidmain(String[]args){ /* 求出1-1/2+1/3-1/4...1/100的和 思路分析 1.1-1/2+1/3-1......
  • 10月30总结
    10月30总结一、正则表达式正则表达式是一些特殊的符号组合在一起产生一些特殊含义,它能帮助我们方便的检查一个字符串中符合条件的数据值。正则表达式线上测试网址:http:......
  • 10.30
    #include<stdio.h>intyin(ints);intmain(){ ints; scanf("%d",&s); if(s==yin(s)) printf("YES");else printf("NO"); return0;}intyin(ints){inti,......
  • 力扣 105. 从前序与中序遍历序列构造二叉树
    105.从前序与中序遍历序列构造二叉树给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树......
  • cv学习总结(SVM,softmax)10.24-10.30
          本周完成了SVM课程笔记的阅读,包括SVM的基本原理以及SVM的优化过程,以及实现了SVM的两种损失函数(svm以及softmax)的线性分类器,以及学习了反向传播以及神经网......
  • 2022-10-24 ClickHouse 源码解析-查询引擎经典理论
    ​​ClickHouse源码解析:综述​​​​ClickHouse源码解析:MergeTreeWrite-Path​​​​ClickHouse源码解析:MergeTreeRead-Path​​ClickHouse源码解析:查询引擎......
  • 10.常规流
    #常规流盒模型:规定单个盒子的规则视觉格式化模型(布局规则):页面中的多个盒子排列规则视觉格式化模型,大体上将页面中盒子的排列分为三种方式:1.常规流2.浮动3.......
  • 10月30周报
    本周总结json模块补充针对中文会自动转码我们在查看的时候不方便我们要让他不自动转码ensure_ascii=False正则表达式字符组[0123456789] 匹配0到9任意一个数(全......
  • 2022.10代码大全阅读心得1
    第11章:变量名的力量问题:怎样给一个变量命名?长名字还是短名字?命名的最佳实践有哪些?有哪些常见的命名方法?在命名中应该要避免的东西有哪些?怎样给一个变量命名?通......
  • 2022.10代码大全阅读心得2
    第十四章组织直线型代码14.1必须有明确顺序的代码对于具有明显的顺序关系的代码,应该使用顺序结构。对于隐含的顺序关系,应该:去除不合理的依赖关系(如不应该在Calculat......