Previous Page

Up One Level

Next Page

The MyHDL manual

Contents

Index

Previous: 2. Introduction to MyHDL Up: 2. Introduction to MyHDL Next: 2.2 Concurrent generators and


  
2.1 A basic MyHDL simulation

We will introduce MyHDL with a classic Hello World style example. Here are the contents of a MyHDL simulation script called hello1.py:

from myhdl import delay, now, Simulation

 

def sayHello():

    while True:          ###1

        yield delay(10)

        print "%s Hello World!" % now()

 

gen = sayHello()

sim = Simulation(gen)

sim.run(30)

All example code can be found in the distribution directory under example/manual. When we run this simulation, we get the following output:

% python hello1.py

10 Hello World!

20 Hello World!

30 Hello World!

StopSimulation: Simulated for duration 30

The first line of the script imports a number of objects from the myhdl package. In good Python style, and unlike most other languages, we can only use identifiers that are literally defined in the source file 2.1.

Next, we define a generator function called sayHello. This is a generator function (as opposed to a classic Python function) because it contains a yield statement (instead of return statement). When called, a generator function returns a generator, which is the basic simulation object in MyHDL.

The yield statement in MyHDL has a similar meaning as the wait statement in VHDL: the statement suspends execution of a generator, and its clauses specify the conditions on which the generator should wait before resuming. In this case, it should wait for a delay. ###Q1

To make sure that a generator runs ``forever'', we wrap its behavior in a "while True" loop. ###1 This is a standard Python idiom, and it is the MyHDL equivalent of the implicit looping behavior of a Verilog always block and a VHDL process.

In the example, variable gen refers to a generator. To simulate it, we pass it as an argument to a Simulation object constructor. We then run the simulation for the desired amount of time. In MyHDL, time is modeled as a natural integer.

 


###1 In the latest versions of Python, the constants True and False are preferred.

Footnotes

... file2.1

The exception is the "from module import *" syntax, that imports all the symbols from a module. Although this is generally considered bad practice, it can be tolerated for large modules that export a lot of symbols. One may argue that myhdl falls into that category.


Previous Page

Up One Level

Next Page

The MyHDL manual

Contents

Index

Previous: 2. Introduction to MyHDL Up: 2. Introduction to MyHDL Next: 2.2 Concurrent generators and


Release 0.4, documentation updated on February 4, 2004.

About this document

1