\title{Counters in myHDL} \author{Steven K Armour} \maketitle

Counters play a vital role in Digital Hardware, ranging from Clock Dividers; (see below) to event triggers by recording the number of events that have occurred or will still need to occur (all the counters here in use a clock as the counting event but this is easily changed). Presented below are some basic HDL counters (Up, Down, Hybridized Up-Down) in myHDL.

Refrances

@misc{loi le_2017, title={Verilog code for counter with testbench}, url={http://www.fpga4student.com/2017/03/verilog-code-for-counter-with-testbench.html}, journal={Fpga4student.com}, author={Loi Le, Van}, year={2017} }

@misc{digilent_2018, title={Learn.Digilentinc | Counter and Clock Divider}, url={https://learn.digilentinc.com/Documents/262}, journal={Learn.digilentinc.com}, author={Digilent}, year={2018} }

Libraries and Helper functions


In [1]:
from myhdl import *
from myhdlpeek import Peeker
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

from sympy import *
init_printing()

import random

#https://github.com/jrjohansson/version_information
%load_ext version_information
%version_information myhdl, myhdlpeek, numpy, pandas, matplotlib, sympy, random


Out[1]:
SoftwareVersion
Python3.6.2 64bit [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
IPython6.2.1
OSLinux 4.15.0 30 generic x86_64 with debian stretch sid
myhdl0.10
myhdlpeek0.0.6
numpy1.13.3
pandas0.23.3
matplotlib2.1.0
sympy1.1.2.dev
randomThe 'random' distribution was not found and is required by the application
Tue Aug 14 05:49:04 2018 MDT

In [2]:
#helper  functions to read in the .v and .vhd generated files into python
def VerilogTextReader(loc, printresult=True):
    with open(f'{loc}.v', 'r') as vText:
        VerilogText=vText.read()
    if printresult:
        print(f'***Verilog modual from {loc}.v***\n\n', VerilogText)
    return VerilogText

def VHDLTextReader(loc, printresult=True):
    with open(f'{loc}.vhd', 'r') as vText:
        VerilogText=vText.read()
    if printresult:
        print(f'***VHDL modual from {loc}.vhd***\n\n', VerilogText)
    return VerilogText

Counter Specs


In [3]:
CountVal=17
BitSize=int(np.log2(CountVal))+1; BitSize


Out[3]:
$$5$$

myHDL modules bitvector type behavior

up counting behavior


In [4]:
ModBV=modbv(0)[BitSize:]
IntBV=intbv(0)[BitSize:]
print(f"`ModBV` max is {ModBV.max}; min is {ModBV.min}")
print(f"`IntBV` max is {IntBV.max}; min is {IntBV.min}")


`ModBV` max is 32; min is 0
`IntBV` max is 32; min is 0

In [5]:
for _ in range(ModBV.max*2):
    try:
        ModBV+=1; IntBV+=1
        print(f"`ModBV` value is {ModBV}; `IntBV` value is {IntBV}")
    except ValueError:
        ModBV+=1
        print(f"`ModBV` value is {ModBV}; `IntBV` value is {IntBV} and INVALID")


`ModBV` value is 01; `IntBV` value is 01
`ModBV` value is 02; `IntBV` value is 02
`ModBV` value is 03; `IntBV` value is 03
`ModBV` value is 04; `IntBV` value is 04
`ModBV` value is 05; `IntBV` value is 05
`ModBV` value is 06; `IntBV` value is 06
`ModBV` value is 07; `IntBV` value is 07
`ModBV` value is 08; `IntBV` value is 08
`ModBV` value is 09; `IntBV` value is 09
`ModBV` value is 0a; `IntBV` value is 0a
`ModBV` value is 0b; `IntBV` value is 0b
`ModBV` value is 0c; `IntBV` value is 0c
`ModBV` value is 0d; `IntBV` value is 0d
`ModBV` value is 0e; `IntBV` value is 0e
`ModBV` value is 0f; `IntBV` value is 0f
`ModBV` value is 10; `IntBV` value is 10
`ModBV` value is 11; `IntBV` value is 11
`ModBV` value is 12; `IntBV` value is 12
`ModBV` value is 13; `IntBV` value is 13
`ModBV` value is 14; `IntBV` value is 14
`ModBV` value is 15; `IntBV` value is 15
`ModBV` value is 16; `IntBV` value is 16
`ModBV` value is 17; `IntBV` value is 17
`ModBV` value is 18; `IntBV` value is 18
`ModBV` value is 19; `IntBV` value is 19
`ModBV` value is 1a; `IntBV` value is 1a
`ModBV` value is 1b; `IntBV` value is 1b
`ModBV` value is 1c; `IntBV` value is 1c
`ModBV` value is 1d; `IntBV` value is 1d
`ModBV` value is 1e; `IntBV` value is 1e
`ModBV` value is 1f; `IntBV` value is 1f
`ModBV` value is 01; `IntBV` value is 20 and INVALID
`ModBV` value is 03; `IntBV` value is 21 and INVALID
`ModBV` value is 05; `IntBV` value is 22 and INVALID
`ModBV` value is 07; `IntBV` value is 23 and INVALID
`ModBV` value is 09; `IntBV` value is 24 and INVALID
`ModBV` value is 0b; `IntBV` value is 25 and INVALID
`ModBV` value is 0d; `IntBV` value is 26 and INVALID
`ModBV` value is 0f; `IntBV` value is 27 and INVALID
`ModBV` value is 11; `IntBV` value is 28 and INVALID
`ModBV` value is 13; `IntBV` value is 29 and INVALID
`ModBV` value is 15; `IntBV` value is 2a and INVALID
`ModBV` value is 17; `IntBV` value is 2b and INVALID
`ModBV` value is 19; `IntBV` value is 2c and INVALID
`ModBV` value is 1b; `IntBV` value is 2d and INVALID
`ModBV` value is 1d; `IntBV` value is 2e and INVALID
`ModBV` value is 1f; `IntBV` value is 2f and INVALID
`ModBV` value is 01; `IntBV` value is 30 and INVALID
`ModBV` value is 03; `IntBV` value is 31 and INVALID
`ModBV` value is 05; `IntBV` value is 32 and INVALID
`ModBV` value is 07; `IntBV` value is 33 and INVALID
`ModBV` value is 09; `IntBV` value is 34 and INVALID
`ModBV` value is 0b; `IntBV` value is 35 and INVALID
`ModBV` value is 0d; `IntBV` value is 36 and INVALID
`ModBV` value is 0f; `IntBV` value is 37 and INVALID
`ModBV` value is 11; `IntBV` value is 38 and INVALID
`ModBV` value is 13; `IntBV` value is 39 and INVALID
`ModBV` value is 15; `IntBV` value is 3a and INVALID
`ModBV` value is 17; `IntBV` value is 3b and INVALID
`ModBV` value is 19; `IntBV` value is 3c and INVALID
`ModBV` value is 1b; `IntBV` value is 3d and INVALID
`ModBV` value is 1d; `IntBV` value is 3e and INVALID
`ModBV` value is 1f; `IntBV` value is 3f and INVALID
`ModBV` value is 01; `IntBV` value is 40 and INVALID

down counting behavior


In [6]:
ModBV=modbv(2**BitSize -1)[BitSize:]
IntBV=intbv(2**BitSize -1)[BitSize:]
print(f"`ModBV` max is {ModBV.max}; min is {ModBV.min}")
print(f"`IntBV` max is {IntBV.max}; min is {IntBV.min}")


`ModBV` max is 32; min is 0
`IntBV` max is 32; min is 0

In [7]:
for _ in range(ModBV.max*2):
    try:
        ModBV-=1; IntBV-=1
        print(f"`ModBV` value is {ModBV}; `IntBV` value is {IntBV}")
    except ValueError:
        ModBV-=0
        print(f"`ModBV` value is {ModBV}; `IntBV` value is {IntBV} and INVALID")


`ModBV` value is 1e; `IntBV` value is 1e
`ModBV` value is 1d; `IntBV` value is 1d
`ModBV` value is 1c; `IntBV` value is 1c
`ModBV` value is 1b; `IntBV` value is 1b
`ModBV` value is 1a; `IntBV` value is 1a
`ModBV` value is 19; `IntBV` value is 19
`ModBV` value is 18; `IntBV` value is 18
`ModBV` value is 17; `IntBV` value is 17
`ModBV` value is 16; `IntBV` value is 16
`ModBV` value is 15; `IntBV` value is 15
`ModBV` value is 14; `IntBV` value is 14
`ModBV` value is 13; `IntBV` value is 13
`ModBV` value is 12; `IntBV` value is 12
`ModBV` value is 11; `IntBV` value is 11
`ModBV` value is 10; `IntBV` value is 10
`ModBV` value is 0f; `IntBV` value is 0f
`ModBV` value is 0e; `IntBV` value is 0e
`ModBV` value is 0d; `IntBV` value is 0d
`ModBV` value is 0c; `IntBV` value is 0c
`ModBV` value is 0b; `IntBV` value is 0b
`ModBV` value is 0a; `IntBV` value is 0a
`ModBV` value is 09; `IntBV` value is 09
`ModBV` value is 08; `IntBV` value is 08
`ModBV` value is 07; `IntBV` value is 07
`ModBV` value is 06; `IntBV` value is 06
`ModBV` value is 05; `IntBV` value is 05
`ModBV` value is 04; `IntBV` value is 04
`ModBV` value is 03; `IntBV` value is 03
`ModBV` value is 02; `IntBV` value is 02
`ModBV` value is 01; `IntBV` value is 01
`ModBV` value is 00; `IntBV` value is 00
`ModBV` value is 1f; `IntBV` value is 1f and INVALID
`ModBV` value is 1e; `IntBV` value is 1e and INVALID
`ModBV` value is 1d; `IntBV` value is 1d and INVALID
`ModBV` value is 1c; `IntBV` value is 1c and INVALID
`ModBV` value is 1b; `IntBV` value is 1b and INVALID
`ModBV` value is 1a; `IntBV` value is 1a and INVALID
`ModBV` value is 19; `IntBV` value is 19 and INVALID
`ModBV` value is 18; `IntBV` value is 18 and INVALID
`ModBV` value is 17; `IntBV` value is 17 and INVALID
`ModBV` value is 16; `IntBV` value is 16 and INVALID
`ModBV` value is 15; `IntBV` value is 15 and INVALID
`ModBV` value is 14; `IntBV` value is 14 and INVALID
`ModBV` value is 13; `IntBV` value is 13 and INVALID
`ModBV` value is 12; `IntBV` value is 12 and INVALID
`ModBV` value is 11; `IntBV` value is 11 and INVALID
`ModBV` value is 10; `IntBV` value is 10 and INVALID
`ModBV` value is 0f; `IntBV` value is 0f and INVALID
`ModBV` value is 0e; `IntBV` value is 0e and INVALID
`ModBV` value is 0d; `IntBV` value is 0d and INVALID
`ModBV` value is 0c; `IntBV` value is 0c and INVALID
`ModBV` value is 0b; `IntBV` value is 0b and INVALID
`ModBV` value is 0a; `IntBV` value is 0a and INVALID
`ModBV` value is 09; `IntBV` value is 09 and INVALID
`ModBV` value is 08; `IntBV` value is 08 and INVALID
`ModBV` value is 07; `IntBV` value is 07 and INVALID
`ModBV` value is 06; `IntBV` value is 06 and INVALID
`ModBV` value is 05; `IntBV` value is 05 and INVALID
`ModBV` value is 04; `IntBV` value is 04 and INVALID
`ModBV` value is 03; `IntBV` value is 03 and INVALID
`ModBV` value is 02; `IntBV` value is 02 and INVALID
`ModBV` value is 01; `IntBV` value is 01 and INVALID
`ModBV` value is 00; `IntBV` value is 00 and INVALID
`ModBV` value is 1f; `IntBV` value is -1 and INVALID

Up-Counter

up counters are counters that count up to a target value from a lower starting value. The following counter is a simple one that uses the clock as incrementer (think one clock cycle as one swing of an old grandfather clock pendulum). But more complicated counters can use any signal as an incrementer. This Counter also has a signal the indicates that the counter has been triggered before the modulus values for the internal counter is reset. This is because this counter tries to reproduce the behavior of timers found on common apps that show how much time has elapsed since the counter has run up

\begin{figure} \centerline{\includegraphics[width=10cm]{Up_Counter.png}} \caption{\label{fig:RP} Up_Counter Functianl Digram } \end{figure}

In [8]:
@block
def Up_Counter(count, Trig, clk, rst, CountVal, BitSize):
    """
    UpCounter
    
    Input:
        clk(bool): system clock feed
        rst(bool): clock reset signal
    Ouput:
        count (bit vector): current count value; count 
        Trig(bool)
    
    Parmeter(Python Only):
        CountVal(int): value to count to
        BitSize (int): Bitvalue size is log_2(CountVal)+1
        
    """
    #internals
    count_i=Signal(modbv(0)[BitSize:])
    Trig_i=Signal(bool(0))
    
    @always(clk.posedge, rst.negedge)
    def logic():
        if rst:
            count_i.next=0
            Trig_i.next=0
        
        elif count_i%CountVal==0 and count_i!=0:
            Trig_i.next=1
            count_i.next=0
        
        else:
            count_i.next=count_i+1
       
    
    @always_comb
    def OuputBuffer():
        count.next=count_i
        Trig.next=Trig_i
    
    return instances()

myHDL testing


In [9]:
Peeker.clear()
clk=Signal(bool(0)); Peeker(clk, 'clk')
rst=Signal(bool(0)); Peeker(rst, 'rst')
Trig=Signal(bool(0)); Peeker(Trig, 'Trig')
count=Signal(modbv(0)[BitSize:]); Peeker(count, 'count')

DUT=Up_Counter(count, Trig, clk, rst, CountVal, BitSize)

def Up_CounterTB():
    """
    myHDL only Testbench for `Up_Counter` module
    """
    @always(delay(1))
    def ClkGen():
        clk.next=not clk
    
    @instance
    def stimules():
        i=0
        while True:
            if i==int(CountVal*1.5):
                rst.next=1
            elif i==int(CountVal*1.5)+1:
                rst.next=0
            
            if i==int(CountVal*2.5):
                raise StopSimulation()
                
            i+=1
            yield clk.posedge
    
    return instances()

sim=Simulation(DUT, Up_CounterTB(), *Peeker.instances()).run()

In [10]:
Peeker.to_wavedrom()



In [11]:
Up_CounterData=Peeker.to_dataframe()
Up_CounterData=Up_CounterData[Up_CounterData['clk']==1]
Up_CounterData.drop('clk', axis=1, inplace=True)
Up_CounterData.reset_index(drop=True, inplace=True)
Up_CounterData


Out[11]:
Trig count rst
0 0 1 0
1 0 2 0
2 0 3 0
3 0 4 0
4 0 5 0
5 0 6 0
6 0 7 0
7 0 8 0
8 0 9 0
9 0 10 0
10 0 11 0
11 0 12 0
12 0 13 0
13 0 14 0
14 0 15 0
15 0 16 0
16 0 17 0
17 1 0 0
18 1 1 0
19 1 2 0
20 1 3 0
21 1 4 0
22 1 5 0
23 1 6 0
24 1 7 1
25 0 1 0
26 0 2 0
27 0 3 0
28 0 4 0
29 0 5 0
30 0 6 0
31 0 7 0
32 0 8 0
33 0 9 0
34 0 10 0
35 0 11 0
36 0 12 0
37 0 13 0
38 0 14 0
39 0 15 0
40 0 16 0

Verilog Code


In [12]:
DUT.convert()
VerilogTextReader('Up_Counter');


***Verilog modual from Up_Counter.v***

 // File: Up_Counter.v
// Generated by MyHDL 0.10
// Date: Tue Aug 14 05:49:05 2018


`timescale 1ns/10ps

module Up_Counter (
    count,
    Trig,
    clk,
    rst
);
// UpCounter
// 
// Input:
//     clk(bool): system clock feed
//     rst(bool): clock reset signal
// Ouput:
//     count (bit vector): current count value; count 
//     Trig(bool)
// 
// Parmeter(Python Only):
//     CountVal(int): value to count to
//     BitSize (int): Bitvalue size is log_2(CountVal)+1
//     

output [4:0] count;
wire [4:0] count;
output Trig;
wire Trig;
input clk;
input rst;

reg [4:0] count_i;
reg Trig_i;



always @(posedge clk, negedge rst) begin: UP_COUNTER_LOGIC
    if (rst) begin
        count_i <= 0;
        Trig_i <= 0;
    end
    else if ((((count_i % 17) == 0) && (count_i != 0))) begin
        Trig_i <= 1;
        count_i <= 0;
    end
    else begin
        count_i <= (count_i + 1);
    end
end



assign count = count_i;
assign Trig = Trig_i;

endmodule

\begin{figure} \centerline{\includegraphics[width=10cm]{Up_CounterRTL.png}} \caption{\label{fig:UCRTL} Up_Counter RTL Schematic; Xilinx Vivado 2017.4} \end{figure}
\begin{figure} \centerline{\includegraphics[width=10cm]{Up_CounterSYN.png}} \caption{\label{fig:UCSYN} Up_Counter Synthesized Schematic; Xilinx Vivado 2017.4} \end{figure}

Verilog Testbench


In [13]:
ResetAt=int(CountVal*1.5)+1
StopAt=int(CountVal*2.5)

@block
def Up_CounterTBV():
    """
    myHDL -> Verilog Testbench for `Up_Counter` module
    """
    clk=Signal(bool(0))
    rst=Signal(bool(0))
    Trig=Signal(bool(0))
    count=Signal(modbv(0)[BitSize:])
    
    @always_comb
    def print_data():
        print(clk, rst, Trig, count)

    DUT=Up_Counter(count, Trig, clk, rst, CountVal, BitSize)


    @instance
    def clk_signal():
        while True:
            clk.next = not clk
            yield delay(1)
            
    @instance
    def stimules():
        i=0
        while True:
            if i==ResetAt:
                rst.next=1
            elif i==(ResetAt+1):
                rst.next=0
            else:
                pass
            
            if i==StopAt:
                raise StopSimulation()
                
            i+=1
            yield clk.posedge
    
    return instances()

TB=Up_CounterTBV()
TB.convert(hdl="Verilog", initial_values=True)
VerilogTextReader('Up_CounterTBV');


<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
***Verilog modual from Up_CounterTBV.v***

 // File: Up_CounterTBV.v
// Generated by MyHDL 0.10
// Date: Tue Aug 14 05:49:06 2018


`timescale 1ns/10ps

module Up_CounterTBV (

);
// myHDL -> Verilog Testbench for `Up_Counter` module


reg clk = 0;
reg rst = 0;
wire Trig;
wire [4:0] count;
reg [4:0] Up_Counter0_0_count_i = 0;
reg Up_Counter0_0_Trig_i = 0;



always @(Trig, count, clk, rst) begin: UP_COUNTERTBV_PRINT_DATA
    $write("%h", clk);
    $write(" ");
    $write("%h", rst);
    $write(" ");
    $write("%h", Trig);
    $write(" ");
    $write("%h", count);
    $write("\n");
end


always @(posedge clk, negedge rst) begin: UP_COUNTERTBV_UP_COUNTER0_0_LOGIC
    if (rst) begin
        Up_Counter0_0_count_i <= 0;
        Up_Counter0_0_Trig_i <= 0;
    end
    else if ((((Up_Counter0_0_count_i % 17) == 0) && (Up_Counter0_0_count_i != 0))) begin
        Up_Counter0_0_Trig_i <= 1;
        Up_Counter0_0_count_i <= 0;
    end
    else begin
        Up_Counter0_0_count_i <= (Up_Counter0_0_count_i + 1);
    end
end



assign count = Up_Counter0_0_count_i;
assign Trig = Up_Counter0_0_Trig_i;


initial begin: UP_COUNTERTBV_CLK_SIGNAL
    while (1'b1) begin
        clk <= (!clk);
        # 1;
    end
end


initial begin: UP_COUNTERTBV_STIMULES
    integer i;
    i = 0;
    while (1'b1) begin
        case (i)
            'h1a: begin
                rst <= 1;
            end
            (-'h1): begin
                rst <= 0;
            end
            default: begin
                // pass
            end
        endcase
        if ((i == 42)) begin
            $finish;
        end
        i = i + 1;
        @(posedge clk);
    end
end

endmodule

Down Counter

Down Counters Count Down from a set upper value to a set target lower value. The following Down Counter is a simple revamp of the previous Up Counter. Thus it starts from the CountVal and counts down to zero to trigger the trigger signal that it has completed one countdown cycle before the internal counter resets to restart the countdown.

\begin{figure} \centerline{\includegraphics[width=10cm]{}} \caption{\label{fig:RP} Down_Counter Functianl Digram (ToDo) } \end{figure}

In [14]:
@block
def Down_Counter(count, Trig, clk, rst, StartVal, BitSize):
    """
    DownCounter
    
    Input:
        clk(bool): system clock feed
        rst(bool): clock reset signal
    Ouput:
        count (bit vector): current count value; count 
        Trig(bool)
    
    Parmeter(Python Only):
        StartVal(int): value to count from
        BitSize (int): Bitvalue size is log_2(CountVal)+1
    CatButt
        
    """
    #internal counter value
    count_i=Signal(modbv(StartVal)[BitSize:])
    
    @always(clk.posedge, rst.negedge)
    def logic():
        if rst:
            count_i.next=StartVal
            Trig.next=0
             
        elif count_i==0:
            Trig.next=1
            count_i.next=StartVal
            
        else:
            count_i.next=count_i-1
       
    
    @always_comb
    def OuputBuffer():
        count.next=count_i
    
    return instances()

myHDL Testing


In [15]:
Peeker.clear()
clk=Signal(bool(0)); Peeker(clk, 'clk')
rst=Signal(bool(0)); Peeker(rst, 'rst')
Trig=Signal(bool(0)); Peeker(Trig, 'Trig')
count=Signal(modbv(0)[BitSize:]); Peeker(count, 'count')

DUT=Down_Counter(count, Trig, clk, rst, CountVal, BitSize)

def Down_CounterTB():
    """
    myHDL only Testbench for `Down_Counter` module
    """
    @always(delay(1))
    def ClkGen():
        clk.next=not clk
    
    @instance
    def stimules():
        i=0
        while True:
            if i==int(CountVal*1.5):
                rst.next=1
            elif i==int(CountVal*1.5)+1:
                rst.next=0
            
            if i==int(CountVal*2.5):
                raise StopSimulation()
                
            i+=1
            yield clk.posedge
    
    return instances()

sim=Simulation(DUT, Down_CounterTB(), *Peeker.instances()).run()

In [16]:
Peeker.to_wavedrom()



In [17]:
Down_CounterData=Peeker.to_dataframe()
Down_CounterData=Down_CounterData[Down_CounterData['clk']==1]
Down_CounterData.drop('clk', axis=1, inplace=True)
Down_CounterData.reset_index(drop=True, inplace=True)
Down_CounterData


Out[17]:
Trig count rst
0 0 16 0
1 0 15 0
2 0 14 0
3 0 13 0
4 0 12 0
5 0 11 0
6 0 10 0
7 0 9 0
8 0 8 0
9 0 7 0
10 0 6 0
11 0 5 0
12 0 4 0
13 0 3 0
14 0 2 0
15 0 1 0
16 0 0 0
17 1 17 0
18 1 16 0
19 1 15 0
20 1 14 0
21 1 13 0
22 1 12 0
23 1 11 0
24 1 10 1
25 0 16 0
26 0 15 0
27 0 14 0
28 0 13 0
29 0 12 0
30 0 11 0
31 0 10 0
32 0 9 0
33 0 8 0
34 0 7 0
35 0 6 0
36 0 5 0
37 0 4 0
38 0 3 0
39 0 2 0
40 0 1 0

Verilog Code


In [18]:
DUT.convert()
VerilogTextReader('Down_Counter');


***Verilog modual from Down_Counter.v***

 // File: Down_Counter.v
// Generated by MyHDL 0.10
// Date: Tue Aug 14 05:49:07 2018


`timescale 1ns/10ps

module Down_Counter (
    count,
    Trig,
    clk,
    rst
);
// DownCounter
// 
// Input:
//     clk(bool): system clock feed
//     rst(bool): clock reset signal
// Ouput:
//     count (bit vector): current count value; count 
//     Trig(bool)
// 
// Parmeter(Python Only):
//     StartVal(int): value to count from
//     BitSize (int): Bitvalue size is log_2(CountVal)+1
// CatButt
//     

output [4:0] count;
wire [4:0] count;
output Trig;
reg Trig;
input clk;
input rst;

reg [4:0] count_i = 17;



always @(posedge clk, negedge rst) begin: DOWN_COUNTER_LOGIC
    if (rst) begin
        count_i <= 17;
        Trig <= 0;
    end
    else if ((count_i == 0)) begin
        Trig <= 1;
        count_i <= 17;
    end
    else begin
        count_i <= (count_i - 1);
    end
end



assign count = count_i;

endmodule

\begin{figure} \centerline{\includegraphics[width=10cm]{Down_CounterRTL.png}} \caption{\label{fig:DCRTL} Down_Counter RTL schematic; Xilinx Vivado 2017.4} \end{figure}
\begin{figure} \centerline{\includegraphics[width=10cm]{Down_CounterSYN.png}} \caption{\label{fig:DCSYN} Down_Counter Synthesized Schematic; Xilinx Vivado 2017.4} \end{figure}

Verilog Testbench


In [19]:
ResetAt=int(CountVal*1.5)
StopAt=int(CountVal*2.5)

@block
def Down_CounterTBV():
    """
    myHDL -> Verilog Testbench for `Down_Counter` module
    """
    clk=Signal(bool(0))
    rst=Signal(bool(0))
    Trig=Signal(bool(0))
    count=Signal(modbv(0)[BitSize:])
    
    @always_comb
    def print_data():
        print(clk, rst, Trig, count)

    DUT=Down_Counter(count, Trig, clk, rst, CountVal, BitSize)


    @instance
    def clk_signal():
        while True:
            clk.next = not clk
            yield delay(1)
            
    @instance
    def stimules():
        i=0
        while True:
            if i==ResetAt:
                rst.next=1
            elif i==(ResetAt+1):
                rst.next=0
            else:
                pass
            
            if i==StopAt:
                raise StopSimulation()
                
            i+=1
            yield clk.posedge
    
    return instances()

TB=Down_CounterTBV()
TB.convert(hdl="Verilog", initial_values=True)
VerilogTextReader('Down_CounterTBV');


<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
***Verilog modual from Down_CounterTBV.v***

 // File: Down_CounterTBV.v
// Generated by MyHDL 0.10
// Date: Tue Aug 14 05:49:08 2018


`timescale 1ns/10ps

module Down_CounterTBV (

);
// myHDL -> Verilog Testbench for `Down_Counter` module


reg clk = 0;
reg rst = 0;
reg Trig = 0;
wire [4:0] count;
reg [4:0] Down_Counter0_0_count_i = 17;



always @(Trig, count, clk, rst) begin: DOWN_COUNTERTBV_PRINT_DATA
    $write("%h", clk);
    $write(" ");
    $write("%h", rst);
    $write(" ");
    $write("%h", Trig);
    $write(" ");
    $write("%h", count);
    $write("\n");
end


always @(posedge clk, negedge rst) begin: DOWN_COUNTERTBV_DOWN_COUNTER0_0_LOGIC
    if (rst) begin
        Down_Counter0_0_count_i <= 17;
        Trig <= 0;
    end
    else if ((Down_Counter0_0_count_i == 0)) begin
        Trig <= 1;
        Down_Counter0_0_count_i <= 17;
    end
    else begin
        Down_Counter0_0_count_i <= (Down_Counter0_0_count_i - 1);
    end
end



assign count = Down_Counter0_0_count_i;


initial begin: DOWN_COUNTERTBV_CLK_SIGNAL
    while (1'b1) begin
        clk <= (!clk);
        # 1;
    end
end


initial begin: DOWN_COUNTERTBV_STIMULES
    integer i;
    i = 0;
    while (1'b1) begin
        case (i)
            'h19: begin
                rst <= 1;
            end
            (-'h1): begin
                rst <= 0;
            end
            default: begin
                // pass
            end
        endcase
        if ((i == 42)) begin
            $finish;
        end
        i = i + 1;
        @(posedge clk);
    end
end

endmodule

Up/Down Counter

This Counter incorporates both an Up Counter and Down Counter via hybridizing between the two via a direction control state machine

\begin{figure} \centerline{\includegraphics[width=10cm]{}} \caption{\label{fig:RP} UpDown_Counter Functianl Digram (ToDo) } \end{figure}

In [20]:
#Create the Direction States for UpDown Counter
DirStates=enum('Up', 'Down')
print(f"`Up` state repersentation is {bin(DirStates.Up)}")
print(f"`Down` state repersentation is {bin(DirStates.Down)}")


`Up` state repersentation is 0
`Down` state repersentation is 1

In [21]:
@block
def UpDown_Counter(Dir, count, Trig, clk, rst, 
                   CountVal, StartVal, BitSize):
    """
    UpDownCounter, hybrid of a simple Up Counter and 
    a simple Down Counter using `Dir` to control Up/Down 
    count Direction 
    
    Input:
        Dir(): 
        clk(bool): system clock feed
        rst(bool): clock reset signal
    Ouput:
        count (bit vector): current count value; count 
        Trig(bool)
    
    Parmeter(Python Only):
        CountVal(int): Highest Value for counter
        StartVal(int): starting value for internal counter
        BitSize (int): Bitvalue size is log_2(CountVal)+1
        
    """
    #internal counter value
    count_i=Signal(modbv(StartVal)[BitSize:])
    
    
    
    @always(clk.posedge, rst.negedge)
    def logic():
        if rst:
            count_i.next=StartVal
            Trig.next=0
        
        
        
        #counter contanment
        elif count_i//CountVal==1 and rst==0:
            count_i.next=StartVal
        
        #up behavior
        elif Dir==DirStates.Up:
            count_i.next=count_i+1
            #simple Triger at ends 
            if count_i%CountVal==0:
                Trig.next=1
    
        #down behavior
        elif Dir==DirStates.Down:
            count_i.next=count_i-1
            #simple Triger at ends 
            if count_i%CountVal==0:
                Trig.next=1
        
        
        
            
    
    @always_comb
    def OuputBuffer():
        count.next=count_i
    
    return instances()

myHDL Testing


In [22]:
Peeker.clear()
clk=Signal(bool(0)); Peeker(clk, 'clk')
rst=Signal(bool(0)); Peeker(rst, 'rst')
Trig=Signal(bool(0)); Peeker(Trig, 'Trig')
count=Signal(modbv(0)[BitSize:]); Peeker(count, 'count')
Dir=Signal(DirStates.Up); Peeker(Dir, 'Dir')

DUT=UpDown_Counter(Dir, count, Trig, clk, rst, 
                   CountVal, StartVal=CountVal//2, BitSize=BitSize)

def UpDown_CounterTB():
    """
    myHDL only Testbench for `UpDown_Counter` module
    """
    @always(delay(1))
    def ClkGen():
        clk.next=not clk
    
    @instance
    def stimules():
        i=0
        while True:
            if i==int(CountVal*1.5):
                Dir.next=DirStates.Down
            elif i==int(CountVal*2.5):
                rst.next=1
            elif i==int(CountVal*2.5)+1:
                rst.next=0
            
            
            if i==int(CountVal*3.5):
                raise StopSimulation()
                
            i+=1
            yield clk.posedge
    
    return instances()

sim=Simulation(DUT, UpDown_CounterTB(), *Peeker.instances()).run()

In [23]:
Peeker.to_wavedrom()



In [24]:
UpDown_CounterData=Peeker.to_dataframe()
UpDown_CounterData=UpDown_CounterData[UpDown_CounterData['clk']==1]
UpDown_CounterData.drop('clk', axis=1, inplace=True)
UpDown_CounterData.reset_index(drop=True, inplace=True)
UpDown_CounterData


Out[24]:
Dir Trig count rst
0 Up 0 9 0
1 Up 0 10 0
2 Up 0 11 0
3 Up 0 12 0
4 Up 0 13 0
5 Up 0 14 0
6 Up 0 15 0
7 Up 0 16 0
8 Up 0 17 0
9 Up 0 8 0
10 Up 0 9 0
11 Up 0 10 0
12 Up 0 11 0
13 Up 0 12 0
14 Up 0 13 0
15 Up 0 14 0
16 Up 0 15 0
17 Up 0 16 0
18 Up 0 17 0
19 Up 0 8 0
20 Up 0 9 0
21 Up 0 10 0
22 Up 0 11 0
23 Up 0 12 0
24 Down 0 13 0
25 Down 0 12 0
26 Down 0 11 0
27 Down 0 10 0
28 Down 0 9 0
29 Down 0 8 0
30 Down 0 7 0
31 Down 0 6 0
32 Down 0 5 0
33 Down 0 4 0
34 Down 0 3 0
35 Down 0 2 0
36 Down 0 1 0
37 Down 0 0 0
38 Down 1 31 0
39 Down 1 8 0
40 Down 1 7 0
41 Down 1 6 1
42 Down 0 7 0
43 Down 0 6 0
44 Down 0 5 0
45 Down 0 4 0
46 Down 0 3 0
47 Down 0 2 0
48 Down 0 1 0
49 Down 0 0 0
50 Down 1 31 0
51 Down 1 8 0
52 Down 1 7 0
53 Down 1 6 0
54 Down 1 5 0
55 Down 1 4 0
56 Down 1 3 0
57 Down 1 2 0

Verilog Code


In [25]:
DUT.convert()
VerilogTextReader('UpDown_Counter');


***Verilog modual from UpDown_Counter.v***

 // File: UpDown_Counter.v
// Generated by MyHDL 0.10
// Date: Tue Aug 14 05:49:08 2018


`timescale 1ns/10ps

module UpDown_Counter (
    Dir,
    count,
    Trig,
    clk,
    rst
);
// UpDownCounter, hybrid of a simple Up Counter and 
// a simple Down Counter using `Dir` to control Up/Down 
// count Direction 
// 
// Input:
//     Dir(): 
//     clk(bool): system clock feed
//     rst(bool): clock reset signal
// Ouput:
//     count (bit vector): current count value; count 
//     Trig(bool)
// 
// Parmeter(Python Only):
//     CountVal(int): Highest Value for counter
//     StartVal(int): starting value for internal counter
//     BitSize (int): Bitvalue size is log_2(CountVal)+1
//     

input [0:0] Dir;
output [4:0] count;
wire [4:0] count;
output Trig;
reg Trig;
input clk;
input rst;

reg [4:0] count_i = 8;



always @(posedge clk, negedge rst) begin: UPDOWN_COUNTER_LOGIC
    if (rst) begin
        count_i <= 8;
        Trig <= 0;
    end
    else if ((((count_i / 17) == 1) && (rst == 0))) begin
        count_i <= 8;
    end
    else if ((Dir == 1'b0)) begin
        count_i <= (count_i + 1);
        if (((count_i % 17) == 0)) begin
            Trig <= 1;
        end
    end
    else if ((Dir == 1'b1)) begin
        count_i <= (count_i - 1);
        if (((count_i % 17) == 0)) begin
            Trig <= 1;
        end
    end
end



assign count = count_i;

endmodule

\begin{figure} \centerline{\includegraphics[width=10cm]{UpDown_CounterRTL.png}} \caption{\label{fig:UDCRTL} UpDown_Counter RTL schematic; Xilinx Vivado 2017.4} \end{figure}
\begin{figure} \centerline{\includegraphics[width=10cm]{UpDown_CounterSYN.png}} \caption{\label{fig:UDCSYN} UpDown_Counter Synthesized schematic; Xilinx Vivado 2017.4} \end{figure}

Verilog Testbench


In [26]:
StateChangeAt=int(CountVal*1.5)
ResetAt=int(CountVal*2.5)
StopAt=int(CountVal*3.5)

@block
def UpDown_CounterTBV():
    """
    myHDL -> Verilog Testbench for `Down_Counter` module
    """
    clk=Signal(bool(0))
    rst=Signal(bool(0))
    Trig=Signal(bool(0))
    count=Signal(modbv(0)[BitSize:])
    Dir=Signal(DirStates.Up)

    DUT=UpDown_Counter(Dir, count, Trig, clk, rst, 
                   CountVal, StartVal=CountVal//2, BitSize=BitSize)
    
    @always_comb
    def print_data():
        print(clk, rst, Trig, count)

    DUT=Down_Counter(count, Trig, clk, rst, CountVal, BitSize)


    @instance
    def clk_signal():
        while True:
            clk.next = not clk
            yield delay(1)
            
    @instance
    def stimules():
        i=0
        while True:
            
            if i==StateChangeAt:
                Dir.next=DirStates.Down
            elif i==ResetAt:
                rst.next=1
            elif i==ResetAt+1:
                rst.next=0
            else:
                pass
            
            
            if i==StopAt:
                raise StopSimulation()
                
            i+=1
            yield clk.posedge
    
    return instances()

TB=UpDown_CounterTBV()
TB.convert(hdl="Verilog", initial_values=True)
VerilogTextReader('UpDown_CounterTBV');


<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
***Verilog modual from UpDown_CounterTBV.v***

 // File: UpDown_CounterTBV.v
// Generated by MyHDL 0.10
// Date: Tue Aug 14 05:49:09 2018


`timescale 1ns/10ps

module UpDown_CounterTBV (

);
// myHDL -> Verilog Testbench for `Down_Counter` module


reg clk = 0;
reg rst = 0;
reg Trig = 0;
wire [4:0] count;
reg [0:0] Dir = 1'b0;
reg [4:0] Down_Counter0_0_1_count_i = 17;



always @(posedge clk, negedge rst) begin: UPDOWN_COUNTERTBV_DOWN_COUNTER0_0_1_LOGIC
    if (rst) begin
        Down_Counter0_0_1_count_i <= 17;
        Trig <= 0;
    end
    else if ((Down_Counter0_0_1_count_i == 0)) begin
        Trig <= 1;
        Down_Counter0_0_1_count_i <= 17;
    end
    else begin
        Down_Counter0_0_1_count_i <= (Down_Counter0_0_1_count_i - 1);
    end
end



assign count = Down_Counter0_0_1_count_i;


always @(Trig, count, clk, rst) begin: UPDOWN_COUNTERTBV_PRINT_DATA
    $write("%h", clk);
    $write(" ");
    $write("%h", rst);
    $write(" ");
    $write("%h", Trig);
    $write(" ");
    $write("%h", count);
    $write("\n");
end


initial begin: UPDOWN_COUNTERTBV_CLK_SIGNAL
    while (1'b1) begin
        clk <= (!clk);
        # 1;
    end
end


initial begin: UPDOWN_COUNTERTBV_STIMULES
    integer i;
    i = 0;
    while (1'b1) begin
        case (i)
            'h19: begin
                Dir <= 1'b1;
            end
            'h2a: begin
                rst <= 1;
            end
            (-'h1): begin
                rst <= 0;
            end
            default: begin
                // pass
            end
        endcase
        if ((i == 59)) begin
            $finish;
        end
        i = i + 1;
        @(posedge clk);
    end
end

endmodule

/home/iridium/anaconda3/lib/python3.6/site-packages/myhdl/conversion/_toVerilog.py:327: ToVerilogWarning: Signal is driven but not read: Dir
  category=ToVerilogWarning

Application: Clock Divider

On common application in HDL for counters in build clock dividers. And while there are more specialized and advanced means to perform up or down frequency generation from a reference clock (see for example digital Phase Lock Loops). A simple clock divider is very useful HDL code to drive other HDL IPs that should/need a slower event rate than the Megahertz+ speeds of today's FPGAs

\begin{figure} \centerline{\includegraphics[width=10cm]{}} \caption{\label{fig:RP} ClockDivider Functianl Digram (ToDo) } \end{figure}

In [27]:
@block
def ClockDivider(Divisor, clkOut, count, clk,rst):
    """
    Simple Clock Divider based on the Digilint Clock Divider
    https://learn.digilentinc.com/Documents/262
    
    Input:
        Divisor(32 bit): the clock frequncy divide by value
        clk(bool): The input clock
        rst(bool): clockDivider Reset
    
    Ouput:
        clkOut(bool): the divided clock ouput
        count(32bit): the value of the internal counter
    """
    
    count_i=Signal(modbv(0)[32:])
    @always(clk.posedge, rst.posedge)
    def counter():
        if rst:
            count_i.next=0
        elif count_i==(Divisor-1):
            count_i.next=0
        else:
            count_i.next=count_i+1
    
    clkOut_i=Signal(bool(0))
    @always(clk.posedge, rst.posedge)
    def clockTick():
        if rst:
            clkOut_i.next=0
        elif count_i==(Divisor-1):
            clkOut_i.next=not clkOut_i
        else:
            clkOut_i.next=clkOut_i
        
        
    
    @always_comb
    def OuputBuffer():
        count.next=count_i
        clkOut.next=clkOut_i
       
    return instances()

myHDL Testing


In [28]:
Peeker.clear()
clk=Signal(bool(0)); Peeker(clk, 'clk')
Divisor=Signal(intbv(0)[32:]); Peeker(Divisor, 'Divisor')
count=Signal(intbv(0)[32:]); Peeker(count, 'count')
clkOut=Signal(bool(0)); Peeker(clkOut, 'clkOut')
rst=Signal(bool(0)); Peeker(rst, 'rst')

DUT=ClockDivider(Divisor, clkOut, count, clk,rst)

def ClockDividerTB():
    """
    myHDL only Testbench for `ClockDivider` module
    """
    @always(delay(1))
    def ClkGen():
        clk.next=not clk
    
    @instance
    def stimules():
        for i in range(2,6+1):
            Divisor.next=i
            rst.next=0
            #run clock time
            for _ in range(4*2**(i-1)):
                yield clk.posedge
            
            for j in range(1):
                if j==0:
                    rst.next=1
                
                yield clk.posedge
        
        raise StopSimulation()
        
                
            
    return instances()

sim=Simulation(DUT, ClockDividerTB(), *Peeker.instances()).run()

In [29]:
Peeker.to_wavedrom()



In [30]:
ClockDividerData=Peeker.to_dataframe()
ClockDividerData


Out[30]:
Divisor clk clkOut count rst
0 2 0 0 0 0
1 2 1 0 1 0
2 2 0 0 1 0
3 2 1 1 0 0
4 2 0 1 0 0
5 2 1 1 1 0
6 2 0 1 1 0
7 2 1 0 0 0
8 2 0 0 0 0
9 2 1 0 1 0
10 2 0 0 1 0
11 2 1 1 0 0
12 2 0 1 0 0
13 2 1 1 1 0
14 2 0 1 1 0
15 2 1 0 0 1
16 2 0 0 0 1
17 3 1 0 0 0
18 3 0 0 0 0
19 3 1 0 1 0
20 3 0 0 1 0
21 3 1 0 2 0
22 3 0 0 2 0
23 3 1 1 0 0
24 3 0 1 0 0
25 3 1 1 1 0
26 3 0 1 1 0
27 3 1 1 2 0
28 3 0 1 2 0
29 3 1 0 0 0
... ... ... ... ... ...
475 6 1 1 0 0
476 6 0 1 0 0
477 6 1 1 1 0
478 6 0 1 1 0
479 6 1 1 2 0
480 6 0 1 2 0
481 6 1 1 3 0
482 6 0 1 3 0
483 6 1 1 4 0
484 6 0 1 4 0
485 6 1 1 5 0
486 6 0 1 5 0
487 6 1 0 0 0
488 6 0 0 0 0
489 6 1 0 1 0
490 6 0 0 1 0
491 6 1 0 2 0
492 6 0 0 2 0
493 6 1 0 3 0
494 6 0 0 3 0
495 6 1 0 4 0
496 6 0 0 4 0
497 6 1 0 5 0
498 6 0 0 5 0
499 6 1 1 0 0
500 6 0 1 0 0
501 6 1 1 1 0
502 6 0 1 1 0
503 6 1 0 0 1
504 6 0 0 0 1

505 rows × 5 columns


In [31]:
ClockDividerData_2=ClockDividerData[ClockDividerData['Divisor']==2]
ClockDividerData_2.reset_index(drop=True, inplace=True)
ClockDividerData_2.plot(y=['clk', 'clkOut']);



In [32]:
ClockDividerData_3=ClockDividerData[ClockDividerData['Divisor']==3]
ClockDividerData_3.reset_index(drop=True, inplace=True)
ClockDividerData_3.plot(y=['clk', 'clkOut']);



In [33]:
ClockDividerData_4=ClockDividerData[ClockDividerData['Divisor']==4]
ClockDividerData_4.reset_index(drop=True, inplace=True)
ClockDividerData_4.plot(y=['clk', 'clkOut']);



In [34]:
ClockDividerData_5=ClockDividerData[ClockDividerData['Divisor']==5]
ClockDividerData_5.reset_index(drop=True, inplace=True)
ClockDividerData_5.plot(y=['clk', 'clkOut']);



In [35]:
ClockDividerData_6=ClockDividerData[ClockDividerData['Divisor']==6]
ClockDividerData_6.reset_index(drop=True, inplace=True)
ClockDividerData_6.plot(y=['clk', 'clkOut']);



In [36]:
DUT.convert()
VerilogTextReader('ClockDivider');


***Verilog modual from ClockDivider.v***

 // File: ClockDivider.v
// Generated by MyHDL 0.10
// Date: Tue Aug 14 05:49:11 2018


`timescale 1ns/10ps

module ClockDivider (
    Divisor,
    clkOut,
    count,
    clk,
    rst
);
// Simple Clock Divider based on the Digilint Clock Divider
// https://learn.digilentinc.com/Documents/262
// 
// Input:
//     Divisor(32 bit): the clock frequncy divide by value
//     clk(bool): The input clock
//     rst(bool): clockDivider Reset
// 
// Ouput:
//     clkOut(bool): the divided clock ouput
//     count(32bit): the value of the internal counter

input [31:0] Divisor;
output clkOut;
wire clkOut;
output [31:0] count;
wire [31:0] count;
input clk;
input rst;

reg [31:0] count_i = 0;
reg clkOut_i = 0;



always @(posedge clk, posedge rst) begin: CLOCKDIVIDER_COUNTER
    if (rst) begin
        count_i <= 0;
    end
    else if (($signed({1'b0, count_i}) == ($signed({1'b0, Divisor}) - 1))) begin
        count_i <= 0;
    end
    else begin
        count_i <= (count_i + 1);
    end
end


always @(posedge clk, posedge rst) begin: CLOCKDIVIDER_CLOCKTICK
    if (rst) begin
        clkOut_i <= 0;
    end
    else if (($signed({1'b0, count_i}) == ($signed({1'b0, Divisor}) - 1))) begin
        clkOut_i <= (!clkOut_i);
    end
    else begin
        clkOut_i <= clkOut_i;
    end
end



assign count = count_i;
assign clkOut = clkOut_i;

endmodule

Verilog Code

\begin{figure} \centerline{\includegraphics[width=10cm]{ClockDividerRTL.png}} \caption{\label{fig:clkDivRTL} ClockDivider RTL schematic; Xilinx Vivado 2017.4} \end{figure}
\begin{figure} \centerline{\includegraphics[width=10cm]{ClockDividerSYN.png}} \caption{\label{fig:clkDivRTL} ClockDivider synthesized schematic; Xilinx Vivado 2017.4} \end{figure}

Verilog Testbench


In [37]:
@block
def ClockDividerTBV():
    """
    myHDL -> Verilog Testbench for `ClockDivider` module
    """

    clk=Signal(bool(0)); 
    Divisor=Signal(intbv(0)[32:])
    count=Signal(intbv(0)[32:])
    clkOut=Signal(bool(0))
    rst=Signal(bool(0))

    
    @always_comb
    def print_data():
        print(clk, Divisor, count, clkOut, rst)

    DUT=ClockDivider(Divisor, clkOut, count, clk,rst)


    @instance
    def clk_signal():
        while True:
            clk.next = not clk
            yield delay(1)
    
    @instance
    def stimules():
        for i in range(2,6+1):
            Divisor.next=i
            rst.next=0
            #run clock time
            for _ in range(4*2**(i-1)):
                yield clk.posedge
            
            for j in range(1):
                if j==0:
                    rst.next=1
                else:
                    pass
                
                yield clk.posedge
        
        raise StopSimulation()
        
                
            
    return instances()

TB=ClockDividerTBV()
TB.convert(hdl="Verilog", initial_values=True)
VerilogTextReader('ClockDividerTBV');


<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
<class 'myhdl._Signal._Signal'> <class '_ast.Name'>
***Verilog modual from ClockDividerTBV.v***

 // File: ClockDividerTBV.v
// Generated by MyHDL 0.10
// Date: Tue Aug 14 05:49:12 2018


`timescale 1ns/10ps

module ClockDividerTBV (

);
// myHDL -> Verilog Testbench for `ClockDivider` module


reg clk = 0;
reg rst = 0;
wire [31:0] count;
reg [31:0] Divisor = 0;
wire clkOut;
reg [31:0] ClockDivider0_0_count_i = 0;
reg ClockDivider0_0_clkOut_i = 0;



always @(rst, clkOut, Divisor, count, clk) begin: CLOCKDIVIDERTBV_PRINT_DATA
    $write("%h", clk);
    $write(" ");
    $write("%h", Divisor);
    $write(" ");
    $write("%h", count);
    $write(" ");
    $write("%h", clkOut);
    $write(" ");
    $write("%h", rst);
    $write("\n");
end


always @(posedge clk, posedge rst) begin: CLOCKDIVIDERTBV_CLOCKDIVIDER0_0_COUNTER
    if (rst) begin
        ClockDivider0_0_count_i <= 0;
    end
    else if (($signed({1'b0, ClockDivider0_0_count_i}) == ($signed({1'b0, Divisor}) - 1))) begin
        ClockDivider0_0_count_i <= 0;
    end
    else begin
        ClockDivider0_0_count_i <= (ClockDivider0_0_count_i + 1);
    end
end


always @(posedge clk, posedge rst) begin: CLOCKDIVIDERTBV_CLOCKDIVIDER0_0_CLOCKTICK
    if (rst) begin
        ClockDivider0_0_clkOut_i <= 0;
    end
    else if (($signed({1'b0, ClockDivider0_0_count_i}) == ($signed({1'b0, Divisor}) - 1))) begin
        ClockDivider0_0_clkOut_i <= (!ClockDivider0_0_clkOut_i);
    end
    else begin
        ClockDivider0_0_clkOut_i <= ClockDivider0_0_clkOut_i;
    end
end



assign count = ClockDivider0_0_count_i;
assign clkOut = ClockDivider0_0_clkOut_i;


initial begin: CLOCKDIVIDERTBV_CLK_SIGNAL
    while (1'b1) begin
        clk <= (!clk);
        # 1;
    end
end


initial begin: CLOCKDIVIDERTBV_STIMULES
    integer i;
    integer _;
    integer j;
    for (i=2; i<(6 + 1); i=i+1) begin
        Divisor <= i;
        rst <= 0;
        for (_=0; _<(4 * (2 ** (i - 1))); _=_+1) begin
            @(posedge clk);
        end
        for (j=0; j<1; j=j+1) begin
            if ((j == 0)) begin
                rst <= 1;
            end
            else begin
                // pass
            end
            @(posedge clk);
        end
    end
    $finish;
end

endmodule