当前位置: 首页 > article >正文

HDLBits训练6

时间:2024.12.25

Rotate100

错误代码

module top_module(
    input clk,
    input load,
    input [1:0] ena,
    input [99:0] data,
    output reg [99:0] q); 
    always@(posedge clk)
        begin
            if(load) q<=data;
         else begin   
             case(ena)
             2'b01:q=q>>1;
             2'b10:q=q<<1;
                 default:q<=q;
             endcase
             end     
        end
endmodule

运行结果

正确代码

module top_module(
    input clk,
    input load,
    input [1:0] ena,
    input [99:0] data,
    output reg [99:0] q); 
	
    always@(posedge clk)begin
        if(load)begin
            q<=data;
        end else begin
            case(ena)
            	2'b00:q<=q;
                2'b01:q<={q[0],q[99:1]};
                2'b10:q<={q[98:0],q[99]};
            	2'b11:q<=q;
            	default:q<=q;
        	endcase
        end
    end
endmodule

运行结果

Fsm1

代码

module top_module(
    input clk,
    input areset,    // Asynchronous reset to state B
    input in,
    output out);//  

    parameter A=0, B=1; 
    reg state, next_state;

    always @(*) begin    // This is a combinational always block
        case(state)  // State transition logic
            A:next_state=in?A:B;
            B:next_state=in?B:A;
        endcase
    end

    always @(posedge clk, posedge areset) begin    // This is a sequential always block
        if(areset) state<=B;// State flip-flops with asynchronous reset
        else state<=next_state;
    end
            assign out=(state==B)?1'b1:1'b0;
    // Output logic
    // assign out = (state == ...);

endmodule

按照Fsm1的逻辑书写也可以 

module top_module(
    input clk,
    input reset,    
    input in,
    output out);//  

    parameter A=0, B=1; 
    reg state, next_state;

    always @(*) begin    // This is a combinational always block
        case(state)  // State transition logic
            A:next_state=in?A:B;
            B:next_state=in?B:A;
        endcase
    end

    always @(posedge clk) begin    // This is a sequential always block
        if(reset) state<=B;// State flip-flops with asynchronous reset
        else state<=next_state;
    end
            assign out=(state==B)?1'b1:1'b0;
    // Output logic
    // assign out = (state == ...);

endmodule

运行结果

 Alwaysblock2

 代码

// synthesis verilog_input_version verilog_2001
module top_module(
    input clk,
    input a,
    input b,
    output wire out_assign,
    output reg out_always_comb,
    output reg out_always_ff   );
assign out_assign=a^b;
     always@(posedge clk)  
         begin
             out_always_ff=a^b;
         end
    always@(*)
        out_always_comb=a^b;
endmodule

运行结果

Fsm1s

代码

// Note the Verilog-1995 module declaration syntax here:
module top_module(clk, reset, in, out);
    input clk;
    input reset;    // Synchronous reset to state B
    input in;
    output out;
    reg out;

    parameter A=1'b0, B=1'b1;
    reg present_state, next_state;

    //状态转移
    always @(posedge clk) begin
        if (reset) begin  
        	present_state <= B;
        end else begin
            present_state <= next_state;
        end
    end
    
    //状态转移逻辑
    always @(*) begin
        case( {in,present_state} )
            {1'b0,A}: next_state <= B;
            {1'b0,B}: next_state <= A;
            {1'b1,A}: next_state <= A;
            {1'b1,B}: next_state <= B;
            default: next_state <= present_state;
        endcase
    end
    
    //状态输出
    always @(*) begin
        out = present_state;
    end
endmodule

运行结果

 Fsm2

代码

module top_module(
    input clk,
    input areset,    // Asynchronous reset to OFF
    input j,
    input k,
    output out); //  

    parameter OFF=0, ON=1; 
    reg state, next_state;

    always @(*) begin
        if(state==OFF)
            next_state<=j?ON:OFF;
        else next_state<=k?OFF:ON;
    end

    always @(posedge clk, posedge areset) begin
        if(areset) state<=OFF;
        else state<=next_state;
    end
assign out=state;
   
endmodule
module top_module(
    input clk,
    input areset,    // Asynchronous reset to OFF
    input j,
    input k,
    output out);
	
    parameter OFF=1'b0, ON=1'b1;
    reg state, next_state;
    
    //状态转移
    always @(posedge clk or posedge areset) begin
        if(areset)begin
            state <= OFF;
        end else begin
            state <= next_state;
        end
    end
    
    //状态转移逻辑
    always @(*) begin
        if(j==1 && state==OFF) begin
            next_state = ON; //OFF→ON
        end else if(k==1 && state==ON) begin
            next_state = OFF; //ON→OFF
        end else begin
            next_state = state; //状态不变
        end
    end
    
    //状态输出
    always @(*) begin
        out = state;
    end
endmodule

运行结果

Fsm3comb

代码

module top_module(
    input in,
    input [1:0] state,
    output [1:0] next_state,
    output out); //

    parameter A=0, B=1, C=2, D=3;
    always@(*)
        begin
            case(state)
                A:next_state<=in?B:A;
                B:next_state<=in?B:C;
                C:next_state<=in?D:A;
                D:next_state<=in?B:C;
                default:next_state<=A;
            endcase
         
        end
    // State transition logic: next_state = f(state, in)

    assign out=(state==D)? 1'b1:1'b0;   
        
    // Output logic:  out = f(state) for a Moore state machine

endmodule

运行结果

Fsm3onehot

代码

module top_module(
    input in,
    input [3:0] state,
    output [3:0] next_state,
    output out);

    parameter A=0, B=1, C=2, D=3;

    //状态转移逻辑:采用独热码,写出下一状态每个位的表达式
    assign next_state[A] = state[A]&(~in) | state[C]&(~in);
    assign next_state[B] = state[A]&in | state[B]&in | state[D]&in;
    assign next_state[C] = state[B]&(~in) | state[D]&(~in);
    assign next_state[D] = state[C]&in;

    //状态输出
    assign out = state[D];
endmodule

整体功能概述

这段代码实现了一个简单的有限状态机(FSM)的逻辑部分,根据输入in以及当前状态state来确定下一状态next_state,同时根据当前状态产生相应的输出out。该有限状态机使用了独热码(One-Hot Encoding)来对状态进行编码,也就是每个状态用一个单独的位来表示,在任意时刻只有一位为1,其余位为0

状态转移逻辑

  1. next_state[A]的逻辑
    assign next_state[A] = state[A]&(~in) | state[C]&(~in);
    这行代码确定了在下一时刻状态转移到状态A(也就是next_state中表示A的位变为1)的条件。从逻辑表达式来看,有两种情况会使得下一状态变为A

    • 当当前状态就是A(即state[A]1),并且输入in0~in1)时,意味着在当前处于A状态且接收到特定输入(这里是低电平)时,下一时刻保持在A状态或者跳转到A状态。
    • 当当前状态是C(即state[C]1),并且输入in0时,会从C状态跳转到A状态。
  2. next_state[B]的逻辑
    assign next_state[B] = state[A]&in | state[B]&in | state[D]&in;
    对于状态B,以下三种情况会使得下一时刻状态变为B

    • 当前状态是Astate[A]1),并且输入in1时,会从A状态跳转到B状态。
    • 当前状态已经是Bstate[B]1),同时输入in1,则保持在B状态。
    • 当前状态是Dstate[D]1),并且输入in1,会从D状态跳转到B状态。
  3. next_state[C]的逻辑
    assign next_state[C] = state[B]&(~in) | state[D]&(~in);
    要使下一状态变为C,存在两种可能:

    • 当前状态是Bstate[B]1),且输入in0,会从B状态跳转到C状态。
    • 当前状态是Dstate[D]1),并且输入in0,会从D状态跳转到C状态。
  4. next_state[D]的逻辑
    assign next_state[D] = state[C]&in;
    仅当当前状态是Cstate[C]1),并且输入in1时,下一时刻状态会变为D

状态输出逻辑

assign out = state[D];
这行代码定义了输出逻辑,即输出out的值取决于当前状态中表示D的那一位。当当前状态为D(也就是state[D]1,其他位为0,因为是独热码表示)时,输出out1,否则为0。意味着只有在有限状态机处于状态D时,才会产生有效的输出(这里设为高电平),其他状态下输出为低电平。

总体而言,这段代码通过布尔逻辑表达式清晰地描述了有限状态机的状态转移规则以及输出与状态之间的对应关系,是一种常见的基于 Verilog 语言实现有限状态机逻辑的方式。

运行结果

Fsm3

代码

module top_module(
    input clk,
    input in,
    input areset,
    output out);
	
    /*//方法一:非独热码编码,四个state分别为:A=2'd0,B=2'd1,C=2'd2,D=2'd3
    reg [1:0]state, next_state;
    parameter A=2'd0, B=2'd1, C=2'd2, D=2'd3;
    
    //状态转移
    always@(posedge clk or posedge areset)begin
        if(areset)begin
            state <= A;
        end else begin
            state <= next_state;
        end
    end

    //状态转移逻辑
    always@(*)begin
        if(in==0)begin
            case(state)
                A: next_state = A;
                B: next_state = C;
                C: next_state = A;
                D: next_state = C;
                default: next_state = state; //保持当前状态不变
            endcase
        end else begin
            case(state)
                A: next_state = B;
                B: next_state = B;
                C: next_state = D;
                D: next_state = B;
                default: next_state = state; //保持当前状态不变
            endcase
        end
    end
    
    //状态输出
    always@(*)begin
        case(state)
            A: out = 1'b0;
            B: out = 1'b0;
            C: out = 1'b0;
            D: out = 1'b1;
            default: out = out; //保持当前状态不变
        endcase
    end*/
    
    //方法二:独热码编码,四个state分别为:4'b0001,4'b0010,4'b0100,4'b1000
    reg [3:0]state,next_state;
    parameter A=4'd0, B=4'd1, C=4'd2, D=4'd3;
    
    //状态转移
    always@(posedge clk or posedge areset)begin
        if(areset)begin
            state <= 4'b0001; //注意复位到state的第一个状态4'b0001
        end else begin
            state <= next_state;
        end
    end

    //状态转移逻辑
    always@(*)begin
    	next_state[A] = state[A]&(~in) | state[C]&(~in);
    	next_state[B] = state[A]&in | state[B]&in | state[D]&in;
    	next_state[C] = state[B]&(~in) | state[D]&(~in);
    	next_state[D] = state[C]&in;
    end

    //状态输出
    always@(*)begin
    	out = state[D];
    end
endmodule

运行结果


http://www.kler.cn/a/458440.html

相关文章:

  • SpringBoot使用TraceId日志链路追踪
  • WPF编程excel表格操作
  • 计算机网络-L2TP VPN基础实验配置
  • ArcGIS教程(009):ArcGIS制作校园3D展示图
  • Mac 环境 VVenC 编译与编码命令行工具使用教程
  • Android MediaExtractor JNI 实现详解
  • Java爬虫实战:获取亚马逊商品详情
  • 五.Springboot通过AOP实现API接口的签名验证
  • Go IO之文件处理,TCPUDP讲解
  • CF2043b-B. Digits
  • ASP.NET Core Web API Hangfire
  • C# OpenCV机器视觉:漫水填充
  • 春招快速准备和是否考研建议
  • 深度学习实战102-基于深度学习的网络入侵检测系统,利用各种AI模型和pytorch框架实现网络入侵检测
  • STM32高级 以太网通讯案例1:网络搭建(register代码)
  • leetcode 面试经典 150 题:删除有序数组中的重复项
  • 基于SSM的“一汽租车辆共享平台”的设计与实现(源码+数据库+文档+PPT)
  • vue-复制剪贴板
  • pytorch整体环境打包安装到另一台电脑上
  • 高级技巧-使用Mysql 实现根据条件过滤整个分组数据
  • 正则化强度的倒数C——让模型学习更准确
  • Bash 脚本教程
  • 【Python】什么是元组(Tuple)?
  • TCP/IP原理
  • OpenCV-Python实战(4)——图像处理基础知识
  • 数据资产运营平台如何搭建?数据要素资产运营平台解决方案 解读