前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【日更计划084】数字IC基础题【HDL部分】

【日更计划084】数字IC基础题【HDL部分】

作者头像
空白的贝塔
发布2021-04-07 10:32:08
4640
发布2021-04-07 10:32:08
举报
文章被收录于专栏:摸鱼范式

上期答案

[172] 编写verilog代码,检测序列10110

首先设计状态机。

  1. 没有检测到序列
  2. 检测到1
  3. 检测到10
  4. 检测到101
  5. 检测到1011

状态机如下

代码语言:javascript
复制
module seq_detector(z,x,clock,reset);
    output z;
    input x,clock;
    input reset; //active high
    reg [2:0] state,nextstate;
    parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100; 
 
  always @ (posedge clock) begin
      if(reset) begin
          state <=s0;
          nextstate<=s0;
      end else begin
          state<=nextstate;
      end
  end
 
  always @ (x or state)
      case(state)
          s0:  if(x) nextstate=s1; else nextstate=s0;
          s1:  if(x) nextstate=s1; else nextstate=s2;
          s2:  if(x) nextstate=s3; else nextstate=s0;
          s3:  if(x) nextstate=s4; else nextstate=s2;
          s4:  if(x) nextstate=s1; else nextstate=s2;
      endcase
    
   always @ (x or state)
       case(state)
           s4: if(x) z=1'b0; else z=1'b1;
           s0,s1,s2,s3: z=1'b0;
       endcase
endmodule
[173] 写一段verilog代码,根据输入的n计算斐波那契数列

斐波那契数列是一种数列,每一项是通过将前两项相加而得到的。从0和1开始,顺序为0、1、1、2、3、5、8、13、21、34,依此类推。通常,表达式为xn = xn-1 + xn-2。假设最大值n = 256,以下代码将生成第n个斐波那契数。值“n”作为输入传递给模块(nth_number)

代码语言:javascript
复制
module fibonacci(input clock, reset, input [7:0] nth_number, output [19:0] fibonacci_number, output number_ready);
    reg [19:0] previous_value, current_value;
    reg [7:0] internal_counter;
    reg number_ready_r;
 
    always @(posedge clock or posedge reset)  begin
        if(reset) begin
      		previous_value <='d0; //1st Fibonacci Number
      		current_value <='d1; //2nd Fibonacci Number
      		internal_counter <='d1;
             number_ready_r <= 0;
        end else begin
            if (internal_counter == (nth_number-2)) begin
          		number_ready_r <= 1;
             end else begin
      			internal_counter <= internal_counter + 1;
	  			current_value <= current_value + previous_value;
      			previous_value <= current_value;
         		number_ready_r <= 0;
             end
        end
  	end
 
  assign fibonacci_number = current_value;
  assign number_ready = number_ready_r
    
endmodule

你答对了吗

本期题目

[174] 写一段verilog代码,用半加器组成全加器
[175] verilog中的task和function有什么区别?

欢迎在留言区给出你的答案,正确答案将在下一期公布,或者到下面的文章获取答案

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-03-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 摸鱼范式 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 上期答案
  • 本期题目
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档