草庐IT

【牛客】5 时序逻辑

magnolia666 2023-03-28 原文

VL33 非整数倍数据位宽转换8to12

和上一题一样的,注意valid_out输出时加一个valid_in(其实32题也要加,不过不加仿真也能过)。

`timescale 1ns/1ns

module width_8to12(
    input                    clk         ,   
    input                   rst_n        ,
    input                      valid_in    ,
    input    [7:0]               data_in    ,
 
     output  reg               valid_out,
    output  reg [11:0]   data_out
);
reg [2:0]count;
always@(posedge clk or negedge rst_n)
begin
    if(~rst_n)
        count <= 0;
    else if(valid_in)
        count <= (count<2)?count + 1:0;
end
always@(posedge clk or negedge rst_n)
begin
    if(~rst_n)
        valid_out <= 0;
    else if(valid_in&&(count==1||count==2))
        valid_out <= 1;
    else
        valid_out <= 0;
end
reg [11:0]data_lock;
always@(posedge clk or negedge rst_n)
begin
    if(~rst_n)begin
        data_lock <= 0;
        data_out <= 0;
    end else if(valid_in)begin
        data_lock <= {data_lock[3:0],data_in};
        if(count == 1)
            data_out <= {data_lock[7:0],data_in[7:4]};
        else if(count == 2)
            data_out <= {data_lock[3:0],data_in};
    end
end
endmodule

VL34 整数倍数据位宽转换8to16

越做越简单了,整数倍位宽转换就非常轻松了。

`timescale 1ns/1ns

module width_8to16(
    input                    clk         ,   
    input                    rst_n        ,
    input                      valid_in    ,
    input       [7:0]           data_in    ,
 
     output    reg            valid_out,
    output   reg [15:0]    data_out
);
reg count;
always@(posedge clk or negedge rst_n)
begin
    if(~rst_n)
        count <= 0;
    else if(valid_in)
        count <= count + 1;
end
always@(posedge clk or negedge rst_n)
begin
    if(~rst_n)
        valid_out <= 0;
    else if(valid_in && count)
            valid_out <= 1;
    else
        valid_out <= 0;
end
reg [7:0]data_lock;
always@(posedge clk or negedge rst_n)
begin
    if(~rst_n)begin
        data_lock <= 0;
        data_out <= 0;
    end else if(valid_in)begin
        data_lock <= data_in;
        if(count == 1)
            data_out <= {data_lock,data_in};
    end
end
endmodule

VL35 状态机-非重叠的序列检测

也是一道比较简单的题,不过要注意是非重叠检测,用状态机比用移位寄存器更方便一点。

`timescale 1ns/1ns

module sequence_test1(
    input wire clk  ,
    input wire rst  ,
    input wire data ,
    output reg flag
);
//*************code***********//
reg [2:0]state,next_state;
localparam S0=0,S1=1,S2=2,S3=3,S4=4;
always@(posedge clk or negedge rst)
begin
    if(~rst)
        state <= S0;
    else
        state <= next_state;
end
always@(*)
begin
    case(state)
    S0:next_state = data ? S1 :S0;
    S1:next_state = data ? S1 :S2;
    S2:next_state = data ? S3 :S0;
    S3:next_state = data ? S4 :S2;
    S4:next_state = data ? S0 :S2;
    default:next_state = S0;
    endcase
end
always@(posedge clk or negedge rst)
begin
    if(~rst)
        flag <= 0;
    else if(state == S4 && data)
        flag <= 1;
    else 
        flag <= 0;
end
//*************code***********//
endmodule

VL36 状态机-重叠序列检测

重叠序列用移位寄存器肯定更方便,不过题目要求用状态机,同样也是注意状态跳变就行了。注意输出延后了一个周期,可以通过加一个状态来实现。

`timescale 1ns/1ns

module sequence_test2(
    input wire clk  ,
    input wire rst  ,
    input wire data ,
    output reg flag
);
//*************code***********//
reg [2:0]state,next_state;
localparam S0=0,S1=1,S2=2,S3=3,S4=4;
always@(posedge clk or negedge rst)
begin
    if(~rst)
        state <= S0;
    else
        state <= next_state;
end
always@(*)
begin
    case(state)
    S0:next_state = data ? S1 :S0;
    S1:next_state = data ? S1 :S2;
    S2:next_state = data ? S3 :S0;
    S3:next_state = data ? S4 :S2;
    S4:next_state = data ? S1 :S2;
    default:next_state = S0;
    endcase
end
always@(posedge clk or negedge rst)
begin
    if(~rst)
        flag <= 0;
    else if(state == S4)
        flag <= 1;
    else 
        flag <= 0;
end
//*************code***********//
endmodule

 VL37 时钟分频(偶数)

一眼行波计数器,不过输出定义的是wire,懒得再去改了。

我这里是组合逻辑输出,其实也不大好。

`timescale 1ns/1ns

module even_div
    (
    input     wire rst ,
    input     wire clk_in,
    output    wire clk_out2,
    output    wire clk_out4,
    output    wire clk_out8
    );
//*************code***********//
reg [2:0]count;
always@(posedge clk_in or negedge rst)
begin
    if(~rst)
        count <= 0;
    else
        count <= count + 1;
end
assign clk_out2 = count[0];
assign clk_out4 = count[1:0]>0&&count[1:0]<3;
assign clk_out8 = count>0&&count<5;
//*************code***********//
endmodule

VL38 自动贩售机1 

写完发现题目出的有点垃圾,输入d直接在上升沿给值,下降沿恢复,只有半个周期,晕。

状态机折磨了半天,看了看题解,用计数器确实方便多了,一遍过。

`timescale 1ns/1ns
module seller1(
    input wire clk  ,
    input wire rst  ,
    input wire d1 ,
    input wire d2 ,
    input wire d3 ,
    
    output reg out1,
    output reg [1:0]out2
);
//*************code***********//
reg[2:0]count;
always@(posedge clk or negedge rst)
begin
    if(~rst)begin
        count <= 0;
        out1 <= 0;
        out2 <= 0;
    end else begin
        if(d1)
            count <= count + 1;
        else if(d2)
            count <= count + 2;
        else if(d3)
            count <= count + 4;
        if(count>=3)begin
            count <= 0;
            out1 <= 1;
            out2 <= count -3;
        end else begin
            out1 <= 0;
            out2 <= 0;
        end
    end
end
//*************code***********//
endmodule

VL39 自动贩售机2

和上一题是一样的,加了一个sel的判断。

`timescale 1ns/1ns

module seller2(
    input wire clk  ,
    input wire rst  ,
    input wire d1 ,
    input wire d2 ,
    input wire sel ,
    
    output reg out1,
    output reg out2,
    output reg out3
);
//*************code***********//
reg [2:0]count;
always@(posedge clk or negedge rst)
begin
    if(~rst)begin
        count <= 0;
        out1 <= 0;
        out2 <= 0;
        out3 <= 0;
    end else begin
        if(d1)
            count <= count + 1;
        else if(d2)
            count <= count + 2;
        if(sel)begin
            if(count >= 5)begin
                out1 <= 0;
                out2 <= 1;
                out3 <= count - 5;
                count <= 0;
            end else begin
                out1 <= 0;
                out2 <= 0;
                out3 <= 0;
            end
        end
        else begin
            if(count >= 3)begin
                out1 <= 1;
                out2 <= 0;
                out3 <= count - 3;
                count <= 0;
            end else begin
                out1 <= 0;
                out2 <= 0;
                out3 <= 0;
            end
        end
    end
end
//*************code***********//
endmodule

VL40 占空比50%的奇数分频

用两个计数器,一个上升沿计数,一个下降沿计数即可。

不过感觉题目还是有点问题,第一个下降沿复位还没结束,计数器应该不会累加才对。

`timescale 1ns/1ns

module odo_div_or
   (
    input    wire  rst ,
    input    wire  clk_in,
    output   wire  clk_out7
    );

//*************code***********//
reg clk_out1;
reg clk_out2;
reg [2:0]count1;
reg [2:0]count2;
always@(posedge clk_in or negedge rst)
begin
    if(~rst)begin
        count1 <= 0;
        clk_out1 <= 0;
    end else begin
        count1 <= (count1 < 6)?(count1 + 1):0;
        if(count1 == 3 || count1 ==6)
            clk_out1 <= ~clk_out1;
    end
end
always@(negedge clk_in or negedge rst)
begin
    if(~rst)begin
        count2 <= 0;
        clk_out2 <= 0;
    end else begin
        count2 <= (count2 < 6)?(count2 + 1):0;
        if(count2 == 3 || count2 ==6)
            clk_out2 <= ~clk_out2;
    end
end
assign clk_out7 = clk_out1 | clk_out2;
//*************code***********//
endmodule

 VL41 任意小数分频

题目给了明示,要先3次8分频再7次9分频,连时钟切换点都给出来了。

`timescale 1ns/1ns

module div_M_N(
 input  wire clk_in,
 input  wire rst,
 output wire clk_out
);
parameter M_N = 8'd87; 
parameter c89 = 8'd24; // 8/9时钟切换点
parameter div_e = 5'd8; //偶数周期
parameter div_o = 5'd9; //奇数周期
//*************code***********//
reg [6:0]count;
reg [3:0]count_e;
reg [3:0]count_o;
reg clk_MN;
always@(posedge clk_in or negedge rst)
begin
    if(~rst)
        count <= 0;
    else 
        count <= (count < M_N -1)?(count + 1):0;
end
always@(posedge clk_in or negedge rst)
begin
    if(~rst)begin
        count_e <= 0;
        count_o <= 0;
        clk_MN <= 0;
    end else begin
        if(count <= c89 - 1)begin
            count_e <= (count_e < div_e -1)?(count_e + 1):0;
            if(count_e == 0||count_e == div_e/2)
                clk_MN <= ~clk_MN;
        end else begin
            count_o <= (count_o < div_o -1)?(count_o + 1):0;
            if(count_o == 0||count_o == (div_o-1)/2)
                clk_MN <= ~clk_MN;
        end
    end
end
assign clk_out = clk_MN;
//*************code***********//
endmodule

VL42 无占空比要求的奇数分频

说是无占空比要求,其实tb还是要求的50%,无语。参照VL40即可。

写完发现其实要求占空比40%,乌鱼子。

`timescale 1ns/1ns

module odd_div (    
    input     wire rst ,
    input     wire clk_in,
    output    wire clk_out5
);
//*************code***********//
reg [2:0]count;
reg clk_div;
always@(posedge clk_in or negedge rst)
begin
    if(~rst)begin
        count <= 0;
        clk_div <= 0;
    end else begin
        count <= (count < 4)?(count + 1):0;
        if(count == 0 ||count == 2)
            clk_div <= ~clk_div;
    end
end
assign clk_out5 = clk_div;
//*************code***********//
endmodule

VL43 根据状态转移写状态机-三段式

 
`timescale 1ns/1ns

module fsm1(
    input wire clk  ,
    input wire rst  ,
    input wire data ,
    output reg flag
);
//*************code***********//
reg[1:0]state,next_state;
localparam S0=0,S1=1,S2=2,S3=3;
always@(posedge clk or negedge rst)
begin
    if(~rst)
        state <= S0;
    else
        state <= next_state;
end
always@(*)
begin
    case(state)
    S0:next_state = data?S1:S0;
    S1:next_state = data?S2:S1;
    S2:next_state = data?S3:S2;
    S3:next_state = data?S0:S3;
    default:next_state = S0;
    endcase
end
always@(posedge clk or negedge rst)
begin
    if(~rst)
        flag <= 0;
    else begin
        if(state == S3 && data)
            flag <= 1;
        else
            flag <= 0;
    end
end
//*************code***********//
endmodule

VL44 根据状态转移写状态机-二段式

两段式把输出和状态转移条件写一起,直接通过组合逻辑输出。

`timescale 1ns/1ns

module fsm2(
    input wire clk  ,
    input wire rst  ,
    input wire data ,
    output reg flag
);

//*************code***********//
reg[2:0]state,next_state;
localparam S0=0,S1=1,S2=2,S3=3,S4=4;
always@(posedge clk or negedge rst)
begin
    if(~rst)
        state <= S0;
    else
        state <= next_state;
end
always@(*)
begin
    case(state)
    S0:begin
        next_state = data?S1:S0;
        flag =0;
    end
    S1:begin
        next_state = data?S2:S1;
        flag =0;
    end
    S2:begin
        next_state = data?S3:S2;
        flag =0;
    end
    S3:begin
        next_state = data?S4:S3;
        flag =0;
    end
    S4:begin
        next_state = data?S1:S0;
        flag = 1;
    end
    default:begin
        next_state = S0;
        flag =0;
    end
    endcase
end


//*************code***********//
endmodule

 

有关【牛客】5 时序逻辑的更多相关文章

  1. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  2. 牛客网专项练习30天Pytnon篇第02天 - 2

    1.在Python3中,下列关于数学运算结果正确的是:(B)a=10b=3print(a//b)print(a%b)print(a/b)A.3,3,3.3333...B.3,1,3.3333...C.3.3333...,3.3333...,3D.3.3333...,1,3.3333...解析:    在Python中,//表示地板除(向下取整),%表示取余,/表示除(Python2向下取整返回3)2.如下程序Python2会打印多少个数:(D)k=1000whilek>1:    print(k)k=k/2A.1000 B.10C.11D.9解析:    按照题意每次循环K/2,直到K值小于等

  3. Linux磁盘分区中物理卷(PV)、卷组(VG)、逻辑卷(LV)创建和(LVM)管理 - 2

    文章目录一基础定义二创建逻辑卷2-1准备物理设备2-2创建物理卷2-3创建卷组2-4创建逻辑卷2-5创建文件系统并挂载文件三扩展卷组和缩减卷组3-1准备物理设备3-2创建物理卷3-3扩展卷组3-4查看卷组的详细信息以验证3-5缩减卷组四扩展逻辑卷4-1检查卷组是否有可用的空间4-2扩展逻辑卷4-3扩展文件系统五删除逻辑卷5-1备份数据5-2卸载文件系统5-3删除逻辑卷5-4删除卷组5-5删除物理卷六LVM逻辑卷缩容6-1缩容注意事项6-2标准缩容步骤一基础定义LVM,LogicalVolumeManger,逻辑卷管理,Linux磁盘分区管理的一种机制,建立在硬盘和分区上的一个逻辑层,提高磁盘分

  4. ruby-on-rails - Ruby 和 SQL 中的重复业务逻辑 - 2

    我有一个PORO(普通旧Ruby对象)来处理一些业务逻辑。它接收一个ActiveRecord对象并对其进行分类。为了简单起见,以下面为例:classClassificatorSTATES={1=>"Positive",2=>"Neutral",3=>"Negative"}definitializer(item)@item=itemenddefnameSTATES.fetch(state_id)endprivatedefstate_idreturn1if@item.value>0return2if@item.value==0return3if@item.value但是,我还想根据这些st

  5. ruby - 了解 Ruby 中赋值和逻辑运算符的优先级 - 2

    在以下示例中,我无法理解Ruby运算符的优先级:x=1&&y=2由于&&的优先级高于=,我的理解是类似于+和*运算符:1+2*3+4解析为1+(2*3)+4它应该等于:x=(1&&y)=2但是,所有Ruby源代码(包括内部语法解析器Ripper)都将其解析为x=(1&&(y=2))为什么?编辑[08.01.2016]让我们关注一个子表达式:1&&y=2根据优先规则,我们应该尝试将其解析为:(1&&y)=2这没有意义,因为=需要特定的LHS(变量、常量、[]数组项等)。但是既然(1&&y)是一个正确的表达式,那么解析器应该如何处理呢?我试过咨询Ruby的parse.y,但它太像意大利面条

  6. Ruby每道逻辑题 - 2

    我正在尝试解决来自SevenLanguagesinSevenWeeks的一个简单的Ruby问题Printthecontentsofanarrayofsixteennumbers,fournumbersatatime,usingjusteach这是我想到的,可以用简单的方式完成还是改进它?a=(1..16).to_ai=0j=[]a.eachdo|item|i+=1j可以在一行中使用each_slicea.each_slice(4){|x|px} 最佳答案 Teja,你的解决方案没问题。由于您需要使用每一个,因此算法的复杂性将受限于数

  7. arrays - 给定一个大小为 y 的数组,其中包含大小为 n 的数组,我如何使用 Ruby 返回所有逻辑组合? - 2

    我想做的是处理n个集合,而我在下面提供的代码正好处理4个集合。defshow_combinations@combos=[]['A','noA'].eachdo|a|['B','noB'].eachdo|b|['C','noC'].eachdo|c|['D','noD'].eachdo|d|@combos我如何重构以下代码来处理以下场景:鉴于我有一个大小为y的数组,其中包含大小为n的数组,我想返回所有组合。请务必注意,每个子数组中只能有一个项目出现在结果中。(如“已完成资料”不能同时出现在“未完成资料”的结果中)背景:用户可能有一些任务:例如,“完成配置文件”或“设置电子邮件”或其他任何

  8. ruby-on-rails - 如何使用 searchkick 进行逻辑运算的复杂查询 - 2

    我正在使用searchkick库作为产品搜索的elasticsearch客户端。https://github.com/ankane/searchkick可以创建'OR'条件和'AND'条件;AND运算Product.search其中:{price:{lte:200},in_stock:true}或运算Product.search其中:{或:[[{in_stock:true},{backordered:true}]]}但我坚持使用searchkick创建多个“AND”“OR”条件。我需要类似的东西A或B或(C和D)或者我需要这样,A与B与(C或D)请指导我,如何实现这一目标谢谢

  9. Ruby 逻辑运算符 - 一个但不是两个数组中的元素 - 2

    假设我有两个数组:a=[1,2,3]b=[1,2]我想对这两个数组执行一个逻辑操作,返回不在两个数组中的元素(即3)。谢谢! 最佳答案 Ruby中的数组可以非常方便地重载一些数学运算符和按位运算符。在a中但不在b中的元素a-b#[3]同时在a和b中的元素>a&b#[1,2]a或b中的元素a|b#[1,2,3]数组求和(串联)a+b#[1,2,3,1,2]你明白了。 关于Ruby逻辑运算符-一个但不是两个数组中的元素,我们在StackOverflow上找到一个类似的问题:

  10. ruby - 逻辑回归给出不正确的结果 - 2

    我在一个网站上工作,收集人们玩过的国际象棋比赛的结果。查看玩家的评分以及他们与对手的评分之间的差异,我绘制了一个图表,其中的点代表获胜(绿色)、平局(蓝色)和失败(红色)。根据这些信息,我还实现了逻辑回归算法来对获胜和获胜/平局的截止值进行分类。使用评级和差异作为我的两个特征,我得到了一个分类器,然后在图表上绘制了分类器改变其预测的边界。我的梯度下降、成本函数和sigmoid函数的代码如下。defgradient_descent()oldJ=0newJ=J()alpha=1.0#Learningraterun=0while(run0.001))thenrun-=20end#Do20mo

随机推荐