Pipelining Transactors
----------------------
There are three main sections to this readme:

  - The Pipelining Example 
      - covers the Pipelining.hpj project shipped with TestBencher.

  - How Pipelining Boundary Markers Work 
      - details on general implementation.

  - Verilog Specific Details 
      - details on Verilog implementation



The Pipelining Example
----------------------
The purpose of the Pipelining example is to demonstrate how pipelined 
transactors can be created and how they work.  There is no model under test 
used in this example.  Instead a simple protocol was invented just to allow 
the demonstration of "Pipeline Boundary" markers.  The address, control, and 
data signals are driven in three successive clock cycles.  There are three 
diagrams in the Pipelining project:

CLK_generator - Drives clock signal

read          - Performs a three cycle read.  Since there isn't a MUT, the 
                data bus will actually be tri-stated during the data phase.

write         - Performs a three cycle write.  

The read and write diagrams are very similar.  There are two differences:
  1) The "WRITE" signal is asserted in the write diagram and de-asserted
     in the "READ" diagram.
  2) The "DATA" signal is specified to be driven in the write diagram, 
     but not in the read diagram.

To create the pipeline phases, "Pipeline Boundary" Markers need to be placed 
on each clock edge that started and/or ended a pipeline phase.  For each 
"Pipeline Boundary" Marker that starts a phase, a semaphore name needs to be
specified.  This name can be any valid Verilog identifier.  For the last 
marker, the name is left blank to indicate that it just ends a phase 
and doesn't start a new one. 
 
There are three phases in each of the diagrams: addr, control, and data.  
This allows for the following simulation results:

   CLK1   |    CLK2    |    CLK3    |    CLK4    |    CLK5
--------------------------------------------------------------
   addr1  |  control1  |   data1    |            |
          |   addr2    |  control2  |   data2    |
          |            |   addr3    |  control3  |   data3

Apply-nowait diagram calls are also necessary to get these results.  For 
example, to perform 3 pipelined writes, you'd have the following task calls 
in your sequencer process:

Apply_write_nowait(addr1, data1);      
Apply_write_nowait(addr2, data2);   
Apply_write_nowait(addr3, data3); 

Note that the final apply call does not need to be a "nowait" call.  In 
fact, if it's the last apply call in your sequencer process you should 
probably use the "blocking" call, Apply_write, so that the sequencer is 
blocked from finishing until the last write completes.

           
How Pipelining Boundary Markers Work
------------------------------------

Details on how diagrams operate in general can be found in the TestBench 
Generation Help in section 3.5 "Transaction Architecture".  Here, we'll 
describe specifically how the "Pipeline Boundary" Markers work.  

There are two basic features that allow pipelining to work:
1) Multiple instantiations of a single diagram. 

     This is what allows the diagram to run multiple times in parallel.  The 
     diagram is automatically instantiated once per pipeline phase, so you 
     don't need to worry about this detail when using "Pipeline Boundary" 
     markers.  If you're interested in setting the instantiation number     
     manually, you can do so by setting the Instance Count in the Diagram 
     Settings dialog.

2) Semaphores

     These are used to block regions of the diagram from running at the same 
     time.  Actually, when you specify a semaphore name in the marker 
     dialog, we create a semaphore with an initial count set to 1.  This 
     special type of semaphore is called a mutex (for mutually exclusive).  
     
     For example, there can only be one addr phase running at a time. When 
     the write is applied, it will decrement the addr semaphore count and 
     enter the addr phase.  When it leaves the addr phase, it will increment 
     addr back to 1.  In the meantime, if any other diagram tries to grab 
     the addr semaphore, it will wait until the addr semaphore's count is 
     incremented.  
   
     Also, the semaphores that are used in TestBencher are queue based.  So, 
     the order is maintained for all the waiters of a given semaphore.   


     There are "Semaphore" markers that can be used to wait on or post a 
     specific semaphore.  So, you could use "Semaphore" markers to implement 
     a pipelined transacter.  But, "Pipeline Boundary" markers reduce the 
     amount of markers that are necessary and avoid the overall complexity 
     of the diagram.  Here's a comparison between the two methods:
     
      Using Semaphore Markers           Using Pipeline Boundary Markers
      -----------------------           -------------------------------
      Semaphore (Wait) : addr               Pipeline Boundary : addr

        perform addr cycle                    perform addr cycle

      Semaphore (Wait) : control            Pipeline Boundary : control
      Semaphore (Post) : addr

        perform control cycle                 perform control cycle

      Semaphore (Wait) : data               Pipeline Boundary : data
      Semaphore (Post) : control

        perform data cycle                    perform control data

      Semaphore (Post) : data               Pipeline Boundary

    
    
     Note how the wait for the next semaphore must happen before the post of 
     the previous semaphore.  This detail is automated when using Pipeline 
     Boundary markers.  
     
     For each semaphore needed a tbfifosemaphore module is instantiated in 
     the component model.  That allows the semaphore to be shared between 
     different diagrams in the same project.  Currently, there is no way to 
     share one semaphore across multiple projects.  
     
     
Verilog Specific Details
------------------------
The definition of the tbfifosemaphore module can be found in definition found 
in lib\verilog\tbfifosemaphore.v.  This module's purpose is to preserve the 
order in which diagrams are waiting for a given semaphore.  Here is an 
example of what is generated for a "Wait Semaphore":

    // Wait for Semaphore: pipelining.pipeline_phase_control
    tb_pid = pipelining.pipeline_phase_control.GetPidIfNecessary(0);
    if (tb_pid != 0)
      begin
      while(pipelining.pipeline_phase_control.resume_pid != tb_pid)
        begin
        @(pipelining.pipeline_phase_control.resume_pid);
        end
      end
    pipelining.pipeline_phase_control.WaitComplete(tb_pid);

GetPidIfNecessary will return 0 if the semaphore's count is > 0, which means 
that the transaction doesn't have to wait for the semaphore to be posted 
(i.e. incremented).  If GetPidIfNecessary returns a non-zero number then 
that number represents the resume_pid to wait for.  When a tbfifosemaphore 
is posted, it will set the resume_pid to the next waiter in line (if there 
is one).  Once the wait is complete, WaitComplete is called on the 
tbfifosemaphore which will decrement the count.  When the transaction wants 
to release the semaphore, it simply calls Post which will increment the 
semaphore and set the appropriate resume_pid if necessary.