栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HDLBits在线练习题之Exams/ece241 2014 q5a

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HDLBits在线练习题之Exams/ece241 2014 q5a

Exams/ece241 2014 q5a
  • 题目
  • 分析
  • 代码

题目

地址:HDLBits - Exams/ece241 2014 q5a
详细:

You are to design a one-input one-output serial 2’s complementer Moore state machine. The input (x) is a series of bits (one per clock cycle) beginning with the least-significant bit of the number, and the output (Z) is the 2’s complement of the input. The machine will accept input numbers of arbitrary length. The circuit requires an asynchronous reset. The conversion begins when Reset is released and stops when Reset is asserted.
For example:
![在这里插入图片描述](https://img-blog.csdnimg.cn/f03e4fef2a234e7090e739e72a902d65.png#pic_center

Module Declaration
module top_module (
input clk,
input areset,
input x,
output z
);

分析
  1. 对一个二进制数取补码有两种方法,一种是取反加1,另一种是将从LSB开始的第一个1之后的位全部取反。
  2. 分为三种状态,s0:全为0,没有1,则补码为本身;s1:最高位为1,其他为0,即仅接收到第一个1,补码为本身;s2:接收到第一个1之后的状态,补码为第一个1之后的位取反。
  3. 根据2中分析,对于下一状态为s0和s1的,在上升沿取输入值为输出值,即本身;对于下一状态为s2的,上升沿取输入值的反为输出。
代码
module top_module (
    input clk,
    input areset,
    input x,
    output z
); 

    parameter s0=2'b00,s1=2'b01,s2=2'b11;
    // s0: all inputs are 0 from beginning to this cycle;
    // s1: get the first "1" from beginning to this cycle;
    // s2: already get the first "1".
    reg [1:0] current_state, next_state;
    always @(*) begin
        case (current_state)
            s0: next_state = x?s1:s0;
            s1: next_state = s2;
            s2: next_state = s2;
            default: next_state = s0;
        endcase
    end
    always @(posedge clk or posedge areset) begin
        if (areset)
            current_state <= s0;
        else 
            current_state <= next_state;
    end
    always @(posedge clk or posedge areset) begin
        if (areset)
            z <= 1'b0;
        else begin
            if (next_state==s2)
                z <= ~x;
            else 
                z <= x;
        end
    end
    
endmodule
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302108.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号