Storing one bit

A bit can have the values 0 or 1. In a computer, these values are represented by a low and a high value of an electrical signal.

The value of a bit can be stored. This means that the value is remembered, as long as it is stored. While the value is stored, the value can be read, and used, for the purpose of performing different operations. Examples of operations could be to store the value somewhere, for example in memory, or using the value in an addition operation.

A D flip-flop

The value of a bit can be stored in a building block called D flip-flop.

A D flip-flop stores one bit of data. A new value can be stored when a clock signal changes value. A component which can change its stored value only when a clock signal changes is called a synchronous component.

A D flip-flop implementation in Verilog is shown in Figure 2.

module d_ff(clk, data_in, data_out);

   input clk;
   input data_in;
   output data_out;

   wire clk, data_in;

   reg 	reg_value;

   always @(posedge clk)
     reg_value <= data_in;
   assign data_out = reg_value;


Figure 2. A D flip-flop in Verilog.

This the Verilog view - other views are VHDL - SystemC-TLM

The code in Figure 2 defines a module. The module has two inputs, called clk and data_in, and one output, called data_out.

The input variables clk and data_in are defined using the keyword input and the output variable data_out is defined using the keyword output.

The input variables clk and data_in are also defined using the keyword wire.

Read more

A testbench

The D flip-flop implementation in Figure 2 has inputs and outputs. An external module, referred to as a testbench, can be used for the purpose of generating input signals to the D flip-flop, and observing output signals from the D-flip-flop.

Read more

Build and run

A system, containing the D flip-flop in Figure 2 and the testbench in Figure 3, can be analyzed and built using the command

iverilog -o d_ff_tb d_ff.v d_ff_tb.v

The simulation can be run by giving the command

vvp d_ff_tb

The resulting printout is shown in Figure 4.

VCD info: dumpfile d_ff_tb_wave.vcd opened for output.
At time                    0, data_in=1, data_out=1
At time                    1, data_in=0, data_out=1
At time                    4, data_in=0, data_out=0
At time                    6, data_in=1, data_out=0
At time                    8, data_in=1, data_out=1
At time                    9, data_in=0, data_out=1
At time                   12, data_in=0, data_out=0

Figure 4. Printout from running the testbench in Figure 3.

This the Verilog view - other views are VHDL - SystemC-TLM

The printout in Figure 4 shows the values of data_in and data_out for a sequence of time instants. The time instants are defined by a $monitor statement inside an initial block in Figure 3, as

   initial begin
      $monitor("At time %t, data_in=%b, data_out=%b", 
               $time, d_ff_data_in, d_ff_data_out);
      #16 $finish;

with the effect that a printout is done whenever the time changes, or one of the variables d_ff_data_in or d_ff_data_out changes value. The changes for the variable d_ff_data_in are defined in an initial block in Figure 3 as

   initial begin
      #1 d_ff_data_in = 0;
      #5 d_ff_data_in = 1;
      #3 d_ff_data_in = 0;

Read more

Making waves

The testbench in Figure 3 generates printouts as shown in Figure 4. The printouts show values of digital signals, each having the value one or zero. We can represent these signals as waveforms, with the level of the waveform being one or zero. Thinking of the value one as a high voltage level, and the value zero as a low voltage level, we can think of the waveforms as representing actual voltages, in an actual digital system.

A waveform can be visualized using the GTKWave program. We can download a GTKWave version for Mac, in the form of a zip-file that contains an executable GTKWave program. The GTKWave program can be started from a Mac Terminal, by giving the command open followed by the app file name of the program. As an example, I could start the program by doing

open /Users/oladahl/prog/gtkwave/

A GTKWave version for Ubuntu can be installed in Ubuntu, by giving the command

sudo apt-get install gtkwave

The program can then be started by giving the command gtkwave.

Read more