In [3]:
!mkdir -p ../testbench/dac

In [5]:
%%writefile ../testbench/dac/test_dac.py

import random
import cocotb
from cocotb.clock import Clock
from cocotb.triggers import Timer, RisingEdge, ReadOnly
from cocotb.result import TestFailure, ReturnValue

@cocotb.coroutine
def rst_dac(dut):
    """This coroutine performs a reset"""
    yield RisingEdge(dut.clk)                  # Synchronise to the read clock
    dut.rst = 1
    dut.conv = 0
    dut.dac_in = 0  
    yield RisingEdge(dut.clk)                  # Synchronise to the read clock
    dut.rst = 0
    yield RisingEdge(dut.clk)                  # Synchronise to the read clock
    
@cocotb.coroutine
def conv_dac(dut, value):
    """This coroutine performs a DAC conversion"""
    yield RisingEdge(dut.clk)                  # Synchronise to the read clock
    dut.rst = 0
    dut.conv = 1
    dut.dac_in = 65 #value                        # Drive the values
    yield RisingEdge(dut.clk)                  # Wait 1 clock cycle
#    yield ReadOnly()                           # Wait until all events have executed for this timestep
    raise ReturnValue(int(dut.dac_out.value))  # Read back the value

@cocotb.test()
def test_dac(dut):
    """Try converting digital data"""
    SIG = {}
    # Read the parameters back from the DUT to set up our model
    resolution = dut.RES.value.integer
    dut.log.info("Found %d bit resolution DAC" % (resolution))
    # Set up independent read/write clocks
    cocotb.fork(Clock(dut.clk, 10).start())
    
    yield rst_dac(dut)
    
    for cycle in xrange(4):
        yield RisingEdge(dut.clk)
        
    dut.log.info("Writing in random values")
    for i in xrange(20):
        SIG[i] = int(random.getrandbits(resolution))
        analog = yield conv_dac(dut, SIG[i])
        dut.log.info("%d   %d" % (analog, SIG[i]))
        
    dut.log.info("Conversion done")


Overwriting ../testbench/dac/test_dac.py

In [8]:
%%writefile ../testbench/dac/Makefile

TOPLEVEL = dac
PWD=$(shell pwd)
COCOTB=/home/jayant/devel/cocotb
SRC=$(PWD)/../../hdl

VERILOG_SOURCES =  $(SRC)/dac.v $(SRC)/tb.v
MODULE=test_dac #cocotb,test_discovery,test_external,test_regression
EXTRA_ARGS=-I ../../include -I ../../hdl
SIM_ARGS=-lxt2
      
include $(COCOTB)/makefiles/Makefile.inc
include $(COCOTB)/makefiles/Makefile.sim


Overwriting ../testbench/dac/Makefile

In [9]:
!pushd ../testbench/dac; make; popd


~/devel/git_reps/dac/testbench/dac ~/devel/git_reps/dac/doc
make results.xml
make[1]: Entering directory `/home/jayant/devel/git_reps/dac/testbench/dac'
iverilog -o sim_build/sim.vvp -D COCOTB_SIM=1  -I ../../include -I ../../hdl /home/jayant/devel/git_reps/dac/testbench/dac/../../hdl/dac.v /home/jayant/devel/git_reps/dac/testbench/dac/../../hdl/tb.v
PYTHONPATH=/home/jayant/devel/cocotb/build/libs/x86_64:/home/jayant/devel/cocotb:/home/jayant/devel/git_reps/dac/testbench/dac: LD_LIBRARY_PATH=/home/jayant/devel/cocotb/build/libs/x86_64: MODULE=test_dac  \
        TESTCASE= TOPLEVEL=dac \
        vvp -M /home/jayant/devel/cocotb/build/libs/x86_64 -m gpivpi sim_build/sim.vvp -lxt2 -I ../../include -I ../../hdl 
     0.00ns INFO     cocotb.gpi                                  gpi_embed.c:205  in embed_sim_init                  Running on Icarus Verilog version 0.9.6 
     0.00ns INFO     cocotb.gpi                                  gpi_embed.c:206  in embed_sim_init                  Python interpreter initialised and cocotb loaded!
     0.00ns INFO     cocotb.gpi                                  __init__.py:101  in _initialise_testbench           Seeding Python random module with 1417709744
     0.00ns INFO     cocotb.gpi                                  __init__.py:115  in _initialise_testbench           Running tests with Cocotb v0.5a from /home/jayant/devel/cocotb
     0.00ns INFO     cocotb.regression                         regression.py:123  in initialise                      Found test test_dac.test_dac
     0.00ns INFO     cocotb.regression                         regression.py:194  in execute                         Running test 1/1: test_dac
     0.00ns INFO     ..coroutine.test_dac.0x7f7411851090       decorators.py:176  in send                            Starting test: "test_dac"
                                                                                                                               Description: Try converting digital data
     0.00ns INFO     cocotb.dac                                  test_dac.py:36   in test_dac                        Found 8 bit resolution DAC
LXT2 info: dumpfile dump.lxt opened for output.
     0.06ns INFO     cocotb.dac                                  test_dac.py:45   in test_dac                        Writing in random values
     0.08ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   178
     0.10ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   90
     0.12ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   215
     0.14ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   94
     0.16ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   150
     0.18ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   235
     0.20ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   51
     0.22ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   73
     0.24ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   170
     0.26ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   173
     0.28ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   248
     0.30ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   89
     0.32ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   26
     0.34ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   150
     0.36ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   46
     0.38ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   90
     0.40ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   8
     0.42ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   56
     0.44ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   36
     0.46ns INFO     cocotb.dac                                  test_dac.py:49   in test_dac                        0   121
     0.46ns INFO     cocotb.dac                                  test_dac.py:51   in test_dac                        Conversion done
     0.46ns INFO     cocotb.regression                         regression.py:156  in handle_result                   Test Passed: test_dac
     0.46ns INFO     cocotb.regression                         regression.py:132  in tear_down                       Passed 1 tests (0 skipped)
     0.46ns INFO     cocotb.regression                         regression.py:133  in tear_down                       Shutting down...
make[1]: Leaving directory `/home/jayant/devel/git_reps/dac/testbench/dac'
~/devel/git_reps/dac/doc

In [ ]: