HDLBits: Serial two‘s complementer

一、前言

菜鸟总结刷hblbits的心得体会,有错误还请指正!

二、Moore FSM

1、原题目

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.

2、设计思路

简单来说是设计一个Morre FSM 将输入转换为它的补码形式输出。

注意:

(1)默认输入x是负数,且无符号位。

(2)输出比输入要晚一个时钟周期。

(3)补码的另一种理解:从低位到高位(不包括最高位),遇到第一个1之后,其余的高位全部取反,最后再加上最高位,即可得到补码。如例子中,输入为011_0100,输出为100_1100

由以上思路设计的状态机如下图所示:

3、我的代码

module top_module (
    input clk,
    input areset,
    input x,
    output z
); 
	parameter S0 = 4'b0001,S1=4'b0010,S2=4'b0100,S3=4'b1000;
    reg [3:0] state,next_state;
    
    always @(*) begin
        case(state) 
            S0: next_state = x ? S1 : S0;
            S1: next_state = x ? S2 : S3;
            S2: next_state = x ? S2 : S3;
            S3: next_state = x ? S2 : S3;
        endcase 
    end
    
    always @(posedge clk or posedge areset) begin
        if(areset)
            state <= S0;
        else
            state <= next_state;
    end
    
    always @(*) begin
        case(state)
            S0: z = 1'b0;
            S1: z = 1'b1;
            S2: z = 1'b0;
            S3: z = 1'b1;
        endcase
    end
endmodule

三、Mealy FSM

1、原题目

2、设计思路

思路跟上面是一样的,只不过要将上面的Morre FSM改为Mealy FSM,得到新的状态机如下图所示:

3、我的代码

module top_module (
    input clk,
    input areset,
    input x,
    output z
); 
	parameter S0=2'b01,S1=2'b10;
    reg [1:0] state,next_state;
    
//state transitiion logic
    always @(*) begin
        case(state)
            S0: next_state = x ? S1 : S0;
            S1: next_state = S1;
        endcase
    end
    
//state flip_flops
    always @(posedge clk or posedge areset) begin
        if(areset)
            state <= S0;
        else 
            state <= next_state;
    end
    
//output logic
    assign z = (state==S0) ? x : (!x);
endmodule

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-13 10:38:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-13 10:38:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-13 10:38:02       82 阅读
  4. Python语言-面向对象

    2023-12-13 10:38:02       91 阅读

热门阅读

  1. logstash同步mysql数据到es(二、jdbc_driver_library问题)

    2023-12-13 10:38:02       52 阅读
  2. 企业核心架构(高可用 ,高并发 ,高复用)

    2023-12-13 10:38:02       62 阅读
  3. 时钟DS1302LCD

    2023-12-13 10:38:02       37 阅读
  4. 力扣-242. 有效的字母异位词

    2023-12-13 10:38:02       69 阅读
  5. Ajax 请求的原理

    2023-12-13 10:38:02       63 阅读
  6. LeetCode 2697. 字典序最小回文串

    2023-12-13 10:38:02       64 阅读