single pulse generator

上一篇 / 下一篇  2018-12-10 12:41:30 / 个人分类:FSM

最近看了看 FSM based Digital Design using Verilog HDL, 根据Frame. 1.11的状态转移图写了一个module;

module one_pulse(//input
clk,s,rst_n,
//output
P,L);
input s;
input clk;
input rst_n;
output  P;
output  L;
reg [1:0] state;
reg [1:0] next_state;

//
always @(*) begin
    case(state)
        2'b00: begin
            if (s) begin
                next_state = 2'b10;
            end
            else begin
                next_state = 2'b00;
            end
        end
        2'b10: begin
            next_state = 2'b01; // clk sensitive
            end
        2'b01: begin
            if(!s) begin
                next_state = 2'b00;
            end
            else begin
                next_state = 2'b01;
            end
        end
        default: begin
                next_state =2'b00;
            end
            endcase
end
    always @(posedge clk or negedge rst_n) begin
        if (~rst_n) begin
            state <= 2'b00;
        end
        else begin
            state <= next_state;
        end
end   
assign P = state[1]&~state[0];
assign L = state[1]^state[0];
endmodule

// testbench


module tb ();
reg clk;
reg rstn;
//reg seed;
reg enable;
initial begin
          clk = 0;
          rstn = 1;
          enable = 0;
    #10  rstn = 0;
    #10  rstn = 1;
 
    @(posedge clk) enable = 1;
    @(posedge clk);
    @(posedge clk) enable = 0;
    @(posedge clk);
    @(posedge clk);
    @(posedge clk) enable = 1;
    @(posedge clk) enable = 0;
    @(posedge clk);
    @(posedge clk);
    @(posedge clk) enable = 1;
    @(posedge clk) enable = 0;
    @(posedge clk) enable = 1;  
    #150 $finish;
end
always #5 clk = ~clk;

one_pulse u_one_pulse (//input
.clk(clk),
.s(enable),
.rst_n(rstn),
//output
.P(),
.L());
// wire gclk_and = enable & clk;
endmodule


来自书中的状态转移图:


P: s 拉高后产生一个单脉冲;

有一处存疑就是如果s拉高一个cycle后立即拉低一个cycle, 然后再拉高;行为就会跟预想的不一样。
这里隐含着一个条件是,两次s 拉高要大于两个cycle

multiple pulse generation with resume pin added

module one_pulse_r(//input
clk,s,r,rst_n,
//output
P,L);
input s;
input r;
input clk;
input rst_n;
output  P;
output  L;
reg [1:0] state;
reg [1:0] next_state;
parameter S0 = 2'b00;
parameter S1 = 2'b10;
parameter S2 = 2'b11;
parameter S3 = 2'b01;
//
always @(*) begin
    case(state)
        S0: begin
            if (s) begin
                next_state = S1;
            end
            else begin
                next_state = S0;
            end
        end
        S1: begin
            next_state = S2; // clk sensitive
            end
        S2: begin
            if(r) begin
                next_state = S1;
            end
            else begin
                next_state = S3;
            end
        end
        S3: begin
            if (s) begin
                next_state = S3;
            end
            else begin
                next_state = S0;
            end           
        end
        default: begin
                next_state =S0;
            end
            endcase
end
    always @(posedge clk or negedge rst_n) begin
        if (~rst_n) begin
            state <= 2'b00;
        end
        else begin
            state <= next_state;
        end
end   
assign P = state[1]&~state[0];
assign L = state[1]^state[0];
endmodule


//testbench

    @(posedge clk) enable = 0;
    @(negedge clk) rstn_r = 1;
   
    @(posedge clk) enable = 1;
    // @(posedge clk);
    @(posedge clk) resume = 1;
    @(posedge clk) enable = 0;
    @(posedge clk);
    @(posedge clk);   
    @(posedge clk) resume = 0;

 

// waveform
https://img-blog.csdnimg.cn/20181210170930655.png
resume

// 状态转移图



TAG: fsm FSM

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的栏目

日历

« 2019-01-12  
  12345
6789101112
13141516171819
20212223242526
2728293031  

数据统计

  • 访问量: 1990
  • 日志数: 5
  • 建立时间: 2016-12-14
  • 更新时间: 2018-12-15

RSS订阅

Open Toolbar
魔域私服 魔域私服 魔域私服 魔域私服 魔域私服

    <dir id='73da8'><del id='xlm31'><del id='kzyyu'></del><pre id='6ttfy'><pre id='qml1n'><option id='nrxfm'><address id='1s8tb'></address><bdo id='nz0nc'><tr id='586g3'><acronym id='0rp26'><pre id='4k5ta'></pre></acronym><div id='lsp37'></div></tr></bdo></option></pre><small id='bnrrs'><address id='0db0c'><u id='1dtoy'><legend id='4b9z2'><option id='a8511'><abbr id='dy4ic'></abbr><li id='vb3b6'><pre id='orq0s'></pre></li></option></legend><select id='98d3m'></select></u></address></small></pre></del><sup id='2uqtb'></sup><blockquote id='1g4q6'><dt id='khx28'></dt></blockquote><blockquote id='b2xvc'></blockquote></dir><tt id='4hnng'></tt><u id='a6f5q'><tt id='xax79'><form id='l6yu0'></form></tt><td id='63scr'><dt id='sywwg'></dt></td></u>
  1. <code id='zt5jg'><i id='140u7'><q id='706hl'><legend id='da5k9'><pre id='872o7'><style id='1ico0'><acronym id='1odqp'><i id='4t88i'><form id='n417l'><option id='31hau'><center id='pjyme'></center></option></form></i></acronym></style><tt id='8evv4'></tt></pre></legend></q></i></code><center id='0d8fz'></center>

      <dd id='wqk89'></dd>

        <style id='xbq0l'></style><sub id='5a9h8'><dfn id='sb8h2'><abbr id='7z503'><big id='u5lyf'><bdo id='b35e8'></bdo></big></abbr></dfn></sub>
        <dir id='yyc7h'></dir>