Verilog刷题HDLBits——Exams/2013 q2afsm

这篇博客介绍了如何使用Verilog编写一个有限状态机(FSM),该FSM作为仲裁电路,控制三个请求设备对资源的访问。设备通过设置r[i]信号请求资源,FSM根据优先级(设备1最高,设备3最低)决定哪个设备获得权限,并通过g[i]信号输出。代码中展示了状态转移逻辑和输出赋值,并提供了FSM的工作流程示例。
摘要由CSDN通过智能技术生成

Verilog刷题HDLBits——Exams/2013 q2afsm

题目描述

Consider the FSM described by the state diagram shown below:
在这里插入图片描述
This FSM acts as an arbiter circuit, which controls access to some type of resource by three requesting devices. Each device makes its request for the resource by setting a signal r[i] = 1, where r[i] is either r[1], r[2], or r[3]. Each r[i] is an input signal to the FSM, and represents one of the three devices. The FSM stays in state A as long as there are no requests. When one or more request occurs, then the FSM decides which device receives a grant to use the resource and changes to a state that sets that device’s g[i] signal to 1. Each g[i] is an output from the FSM. There is a priority system, in that device 1 has a higher priority than device 2, and device 3 has the lowest priority. Hence, for example, device 3 will only receive a grant if it is the only device making a request when the FSM is in state A. Once a device, i, is given a grant by the FSM, that device continues to receive the grant as long as its request, r[i] = 1.

Write complete Verilog code that represents this FSM. Use separate always blocks for the state table and the state flip-flops, as done in lectures. Describe the FSM outputs, g[i], using either continuous assignment statement(s) or an always block (at your discretion). Assign any state codes that you wish to use.

代码

module top_module (
    input clk,
    input resetn,    // active-low synchronous reset
    input [3:1] r,   // request
    output [3:1] g   // grant
); 
    
    parameter A=0,B=1,C=2,D=3;
    reg[1:0] state,next_state;
    
    always@(*)
        case(state)
            A:next_state=r[1]?B:(r[2]?C:(r[3]?D:A));
            B:next_state=r[1]?B:A;
            C:next_state=r[2]?C:A;
            D:next_state=r[3]?D:A;
        endcase
    
    always@(posedge clk)
        if(~resetn)
            state<=A;
    	else
            state<=next_state;
    
    assign g = {(state==D),(state==C),(state==B)};

endmodule

结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值