# Asynchronous state machine with active negative edge

**URL:** <https://verificationacademy.com/forums/t/asynchronous-state-machine-with-active-negative-edge/39383>\
**Category:** SystemVerilog\
**Tags:** State-machine-coverage, SystemVerilog, clock\
**Created:** [July 28, 2021, 8:36pm UTC](https://verificationacademy.com/forums/t/asynchronous-state-machine-with-active-negative-edge/39383 "2021-07-28T20:36:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake0041](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/e495f1/32.png) [@jake0041](https://verificationacademy.com/forums/u/jake0041)\
**Post date:** [July 28, 2021, 8:36pm UTC](https://verificationacademy.com/forums/t/asynchronous-state-machine-with-active-negative-edge/39383/1 "2021-07-28T20:36:23Z")

</div>

-2

I am writing a testbench for an asynchronous state machine in Verilog with active falling flank.

My code is currently compiling but all the outputs are getting 0 values when they shouldn’t.

Any help?

Code:

```verilog
`timescale 1ns/1ns

module testUcontrol;

wire testproblema2;
// Definición de data_types
// Entradas
reg Clk, Reset;
reg NA, TV, IT, TT;
// Salidas
wire VE, VA, VS, ML; 
wire C, FIN, T0, T1;

//Vectores de prubeas
reg [3:0] testVectors [10:0]; // formato: NA:TV:IT:TT
integer vectorNum;

// Test Module
Ucontrol dut (
    .Clk(Clk), .Reset(Reset),
    .NA(NA), .TV(TV), .IT(IT), .TT(TT),
    .VE(VE), .VA(VA), .VS(VS), .ML(ML),
    .C(C), .FIN(FIN), .T0(T0), .T1(T1));

// Stimulus
initial
    begin
        // Crear archivo .vcd para gtkwve
        $dumpfile("problema2.vcd");
        $dumpvars(0, testproblema2);

        // Generación de las secuencias de entradas
        testVectors[0] = 4'b0000; // NA=0 ,TV=0 ,IT=0 ,TT=0 (Se queda en el estado a)
        testVectors[1] = 4'b0010; // NA=0 ,TV=0 ,IT=1 ,TT=0 (Pasa al estado b)
        testVectors[2] = 4'b1000; // NA=1 ,TV=0 ,IT=0 ,TT=0 (Pasa al estado c)
        testVectors[3] = 4'b0001; // NA=0 ,TV=0 ,IT=0 ,TT=1 (Pasa al estado d)
        testVectors[4] = 4'b0100; // NA=0 ,TV=1 ,IT=0 ,TT=0 (Pasa al estado e)
        testVectors[5] = 4'b1000; // NA=1 ,TV=0 ,IT=0 ,TT=0 (Pasa al estado f)
        testVectors[6] = 4'b0001; // NA=0 ,TV=0 ,IT=0 ,TT=1 (Pasa al estado g)
        testVectors[7] = 4'b0100; // NA=0 ,TV=1 ,IT=0 ,TT=0 (Pasa al estado h)
        testVectors[8] = 4'b0001; // NA=0 ,TV=1 ,IT=0 ,TT=1 (Pasa al estado i)
        testVectors[9] = 4'b0000; // NA=0 ,TV=0 ,IT=0 ,TT=0 (Se queda en el estado i)
        testVectors[10] = 4'b0010; // NA=0 ,TV=0 ,IT=1 ,TT=0 (Pasa al estado a)

        // Inicia punteros de vectores de prueba
        vectorNum = 0;

        Reset = 1;
        #3 Reset = 0;

        end

// Generación cíclica del Clk
always
begin
    Clk = 1; #5; Clk = 0; #5;
end

// Aplicación de los vectores de prueba en flanco creciente (lectura)
always @(posedge Clk)
begin
    #1; {NA, TV, IT, TT} = testVectors[vectorNum]; // Retardo de #1 para que se carguen las salidas
    $display("Las entradas son NA=%b, TV=%b, IT=%b, TT=%b", NA, TV, IT, TT);
end
// Cambio de estado (flanco activo) y cálculo de salidas de Moore
always @(negedge Clk)
#1 if (!Reset)
    begin
        $display("La salidas activas son:");
        $display("VE = %b", VE);
        $display("VA = %b", VA);
        $display("VS = %b", VS);
        $display("ML = %b", ML);
        $display("C = %b", C);
        $display("FIN = %b",FIN);
        $display("T0 = %b", T0);
        $display("T1 = %b", T1);
        vectorNum = vectorNum + 1;
            if(vectorNum ==11) 
                $finish;
    end
endmodule

testprogram:

module Ucontrol( input Clk, Reset,
                input NA, TV, IT, TT,
                output VE, VA, VS, ML, 
                output C, FIN, T0, T1);

reg [1:0] EstPres, ProxEst;

// Asignacion de estados

parameter a = 4'b0000; // Tanque vacio
parameter b = 4'b0001; // Llenando el tanque
parameter c = 4'b0010; // Tanque lleno
parameter d = 4'b0011; // Evacuando liquido por primera vez
parameter e = 4'b0100; // Llenando el tanque con agua
parameter f = 4'b0101; // Lavando el tanque
parameter g = 4'b0110; // Evacuando el agua por segunda vez
parameter h = 4'b0111; // Centrifugado del tanque
parameter i = 4'b1000; // Proceso terminado

// Memoria de estados

always @(posedge Clk, negedge Reset) 
begin
    if (!Reset) EstPres <= a;
    else EstPres <= ProxEst;
end

// Logica de calculo de proximo estado
always @(*) 
begin
    case (EstPres)
    a: if (IT) ProxEst = b;
        else ProxEst = a;
    b: if (NA) ProxEst = c;
        else ProxEst = b;
    c: if (TT) ProxEst = d;
        else ProxEst = c;
    d: if (TV) ProxEst = e;
        else ProxEst = d;
    e: if (NA) ProxEst = f;
        else ProxEst = e;
    f: if (TT) ProxEst = g;
        else ProxEst = f;
    g: if (TV) ProxEst = h;
        else ProxEst = g;
    h: if (TT) ProxEst = i;
        else ProxEst = h;
    i: if (IT) ProxEst = a;
        else ProxEst = i;
    endcase
end

// Logica de calculo de salidas
assign VE = (EstPres == b);
assign VA = (EstPres == e);
assign VS = (EstPres == d) | (EstPres == g);
assign ML = (EstPres == f);
assign C = (EstPres == c);
assign FIN = (EstPres == i) & ~Clk;
assign T0 = (EstPres == c) | (EstPres == h);
assign T1 = (EstPres == f) | (EstPres == h);
endmodule

```

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [July 29, 2021, 5:29pm UTC](https://verificationacademy.com/forums/t/asynchronous-state-machine-with-active-negative-edge/39383/2 "2021-07-29T17:29:57Z")

</div>

_In reply to [jake0041](https://verificationacademy.com/forums/t/asynchronous-state-machine-with-active-negative-edge/39383):_

Looking at you code, you are declaring your state variables as 2 bits, but you need 4 bits:

```verilog

reg [1:0] EstPres, ProxEst; // 2 bits 
// **** YET you are using it as 4 bits!!!! 
parameter a = 4'b0000; // Tanque vacio
parameter b = 4'b0001; // Llenando el tanque
...
case (EstPres)
        a: if (IT) ProxEst = b;
            else ProxEst = a;

```

Ben Cohen  
[http://www.systemverilog.us/](http://www.systemverilog.us/) [ben@systemverilog.us](mailto:ben@systemverilog.us)  
\*\* SVA Handbook 4th Edition, 2016 ISBN 978-1518681448  
…

1. SVA Package: Dynamic and range delays and repeats [SVA: Package for dynamic and range delays and repeats | Verification Academy](https://rb.gy/a89jlh)
2. Free books: Component Design by Example [FREE BOOK: Component Design by Example … A Step-by-Step Process Using VHDL with UART as Vehicle | Verification Academy](https://rb.gy/9tcbhl)  
Real Chip Design and Verification Using Verilog and VHDL($3) [Amazon.com](https://rb.gy/cwy7nb)
3. Papers:

- Understanding the SVA Engine,  
[Verification Horizons - July 2020 | Verification Academy](https://verificationacademy.com/verification-horizons/july-2020-volume-16-issue-2)
- SVA Alternative for Complex Assertions  
[Verification Horizons - March 2018 Issue | Verification Academy](https://verificationacademy.com/news/verification-horizons-march-2018-issue)
- SVA in a UVM Class-based Environment  
[SVA in a UVM Class-based Environment | Verification Horizons | Verification Academy](https://verificationacademy.com/verification-horizons/february-2013-volume-9-issue-1/SVA-in-a-UVM-Class-based-Environment)

Udemy courses by Srinivasan Venkataramanan ([http://cvcblr.com/home.html](http://cvcblr.com/home.html))  
[https://www.udemy.com/course/sva-basic/](https://www.udemy.com/course/sva-basic/)  
[https://www.udemy.com/course/sv-pre-uvm/](https://www.udemy.com/course/sv-pre-uvm/)
