Jeffrey's Log

Archives | Subscribe

Older posts

Designing a D flip-flop using Migen

If you haven’t installed Migen or don’t know about Migen, read my previous blog post Installing Migen – the Python based hardware description language

D flip-flops

D flip-flop

A D flip-flop spits the data out which is kept in the data input pin when the clock is applied(in positive edge or negative edge). The D flip-flop that we are going to design has the input pin D, output pins Q and Qi(inverted of Q). It also has a clock clock input and a reset pin which makes the flip-flop synchronous.

The D flip-flop shown over here is a positive edge triggered flip-flop. The truth table represents the mapping between the input pins to the output pins based on the reset pin and clock pin state. Since its a positive edge triggered flip-flop, the clock pin is shown as a transition from low to high.

Just for a reference, this is the datasheet of a D flip-flop IC.

A D flip-flop using Migen

Create a file named and add this code to it.

from migen.fhdl.std import *

class Dflipflop(Module):
  def __init__(self, D, Q, Qi):
    self.sync += Q.eq(D)
    self.comb += Qi.eq(~Q)

Digital circuits can be divided into synchronous(i.e works based on the clock input) and asynchronous(independent of clock) circuits. Asynchronous circuits are also known as combinational logic. The Python syntax used for describing the D flip-flop can be more clearly understood by reading the Migen user guide. So I won’t be explaining here the syntax used. Only describing the synchronous and combinational(asynchronous) statements are in my scope.

self.sync += Q.eq(D)
Q.eq(D) equates/copies the content of D to Q which are the input and output of our flip-flop. This is then assigned as a synchronous using self.sync +=. As mentioned before, a D flip-flop copies the data from the input pin to the output pin in synchronous to the clock transition. Hence this circuit is a synchronous circuit.

self.comb += Qi.eq(~Q)
Qi.eq(~Q) is used to invert Q and copy to Qi. This is assigned as a combinational logic. This means Qi is independent of the clock. But out flip-flop is a synchronous. This doesn’t means that Qi will change asynchronously since the input of Qi is from a synchronous logic Q.

Testing the D flip-flop

Update your with the below code.

from migen.fhdl.std import *
from migen.fhdl import verilog
from migen.sim.generic import Simulator, TopLevel
from random import randrange

class Dflipflop(Module):
  def __init__(self, D, Q, Qi):
    self.sync += Q.eq(D)
    self.comb += Qi.eq(~Q)

  def do_simulation(self,s):

#Simulation and verilog conversion
D  = Signal()
Q  = Signal()
Qi = Signal()

#print(verilog.convert(Dflipflop(D, Q, Qi), ios={D,Q,Qi}))

sim = Simulator(Dflipflop(D,Q,Qi), TopLevel("Dff.vcd"))

Execute this python script using the below command. This will generate a Dff.vcd which contains the test result. The vcd file can be viewed using GTKWave tool.
gtkwave Dff.vcd


As I mentioned before, read the official Migen user guide and tutorial to understand what the code means.

To test the D flip-flop, we need to check if the input (D) is copied to the output(Q and also Qi). For this the input D should be fed with values(0 or 1). We can use the module random in Python to generate some random values.

The function randrange(2) generates a random number – either 0 or 1. s.wr writes the random number to D. The write happens only after the positive edge of the clock cycle.

Verilog equivalent of the D flip-flop

As I mentioned in my previous blog post (Installing Migen – the Python based hardware description language), Migen converts the Python code to equivalent Verilog code. Since the FPGA tools which we currently have only understands only Verilog/VHDL, this is required. The generated Verilog code can be loaded to your FPGA vendor’s software and verified on real hardware. Mibuild(a tool inside Migen) supports few FPGA vendors for which you don’t have to manually paste your Verilog code to the FPGA vendor’s software. Mibuild will do it for you(by accessing vendor tools via command line) and even it can load the bit stream file to FPGA. Usage of Mibuild will be explained later.

Uncomment the line in your file and run the python code again. This line will print the Verilog equivalent code of our D flip-flop.
print(verilog.convert(Dflipflop(D, Q, Qi), ios={D,Q,Qi}))

Below is the Verilog output.

/* Machine-generated using Migen */
module top(
        input D,
        output reg Q,
        output Qi,
        input sys_clk,
        input sys_rst

// synthesis translate_off
reg dummy_s;
initial dummy_s <= 1'd0;
// synthesis translate_on
assign Qi = (~Q);

always @(posedge sys_clk) begin
        if (sys_rst) begin
                Q <= 1'd0;
        end else begin
                Q <= D;


Another way of testing D flip-flops: A frequency divider

Note: Below shown is not the right way to do a frequency division. This is just an example to show Migen's testing capabilities.


A D filp-flop can be used as a frequency divider (frequency / 2). Read this for a more clear explanation. Make a connection from Qi to D to divide the frequency(fed to the clock pin) by 2. Connecting a wire between pins is very easy in Migen - just use the same Signal() for both the pins.

Create a file named and add the below code.

from migen.fhdl.std import *
from migen.fhdl import verilog
from migen.sim.generic import Simulator, TopLevel

class Dflipflop(Module):
  def __init__(self, D, Q, Qi):
    self.sync += Q.eq(D)
    self.comb += Qi.eq(~Q)

#Simulation and verilog conversion part
Connect = Signal()
Q = Signal()

sim = Simulator(Dflipflop(Connect,Q,Connect), TopLevel("Dff_Divider.vcd"))
Posted in: Electronics, FPGA, GNU/Linux | Tagged under: , , , | 5 Comments

STM32F4Discovery examples for GCC on GNU/Linux

STM32F4DISCOVERY is an ARM Cortex M4 development kit.

I don’t plan to write again a toolchain setup instruction for this board. You can install toolchain and flash tool as per instructions provided in the below link Read the section – Building the toolchain on Debian using “Summon Arm Toolchain”

While installing the toolchain I got an error zlib.h: No such file or directory. On a Debian system you can remove this error by issuing apt-get install zlib1g-dev

The ARM toolchain is build using summon-arm-toolchain script. This toolchain also installs a free GPL V3 based firmware library known as libopencm3.

As I mentioned above, the summon arm toolchain will also install libopencm3. libopencm3 generates some header files during installation. These header files needs python yaml support. The build might stop because of this. You can fix this issue by running apt-get install python-yaml

You can find examples for STM32F4DISCOVERY from

Additional link for reference
1) This link uses the examples from proprietary firmware library of ST.

Posted in: GNU/Linux, Hacking | Tagged under: | 1 Comment

LPC2148 (ARM 7) Test bench setup

This is my LPC2148 (ARM 7) test bench setup.

The LPC2148 header board was bought from Rhydo Labz. You cannot directly power up and start to use. It lacks communication interface. To flash the controller, you need a serial port or a JTAG port. Since this board doesn’t have these port connectors, you need to solder them. So it might be hard for a newbie.

Instead of this board, you can try the BlueBoard which has all the port connectors. I will recommend this for a newbie. I hope that board is worth for its price.

The next thing which you need is a power supply to power the board. You can build your own power supply (If someone needs to know how to build a power supply, I will write a tutorial. But please put a comment in this post saying that you need the tutorial.) ,or you can buy 5V DC power supply (adapters).

Once the board is powered up, it can start to execute your code in its memory. But a fresh controller wont be having any code in its memory to execute. You need a programmer to load the code into its memory. But the LPC series controllers have built-in serial boot-loaders. These boot-loaders can get the code from the serial port and write into its flash memory. So you don’t need a programmer.

To program the controller using the serial boot-loader, you just need to connect the controller to the host PC and the host PC needs to run a serial programmer software. You can get a lot of serial programmer software for LPC controllers. Some of them are

So thats all. Power up the board and connect the serial port to the controller. Run the serial programmer software in the PC. You controller will execute the code flashed into its memory.

But wait. From where you can get the hex file to flash into its memory? Its simple. You just need a cross-compiler. To the cross-compiler, you just need to feed your C source file. It will compile and give you *.out files, which later can be converted into *.hex files.

I will soon try to post a wiki page about how to compile and flash the code into the memory.

Posted in: Circuit, Electronics, Embedded Linux, GNU/Linux, Hacking, open hardware, Projects, tutorials | Tagged under: , , , , , | 6 Comments

GNUduino – Arduino made with gEDA

GNUduino is an Arduino board (Arduino Single-Sided Serial Board Version 3) redrawn using gEDA. GNUduino functions same as that of an Arduino board (It’s just a clone of Arduino ).

When I saw the existing Arduino boards, I felt there is still a freedom missing. The existing Arduino boards are designed using a proprietary CAD tool. But GNUduino is free from those. It is designed using a complete free Schematic and PCB CAD tool named gEDA. This is my first Open Hardware project.

I would like to say special thanks to Pramode Sir for suggesting this name for the board.

You can download the schematic and PCB layout files from the below link

Update: Cooking GNUduino

Posted in: Arduino, Electronics, Embedded Linux, GNU/Linux, GNUduino, Hacking, open hardware, Projects | Tagged under: , , , , | 3 Comments

Electronic Design and Fabrication with gEDA

“Electronic Design and Fabrication with gEDA” is my first article in LINUX For You Magazine.

This is the first time I am forgetting to blog about my article. Actually it was published in last month(NOVEMBER 2009) issue. Please read it if possible.

This article mainly gives a quick introduction about gEDA. I have also given some snaps of my designs.

Posted in: Circuit, Electronics, GNU/Linux, open hardware | Tagged under: , , , | 1 Comment

Older posts