::Tutorial #2: Making/ Using Macros::  
 

In programming, we typically aim to maximize code reuse. This allows us to use the same core code in multiple places in some larger algorithm. This same sort of reuse can also be applied in Digital Works and these are known as macros. The term macro in Digital Works is synonymous with the functions of a program. They allow you to design the logic once and use it multiple places. Another way to think about macros is as logic chips. You could easily build a macro to, say, add two bits and then use this chip as a part of a larger design such as a circuit that adds two 4 bit numbers.

 

 

::Creating a Macro::  
 

Step 1

Create a new Digital Works file using File->New Macro

Step 2

Design the logic you wish for the macro to implement. In this tutorial, we will be building a macro that has 2 inputs / 2 outputs. It will take both inputs and add them together, producing a sum output and a carry output. We will call our two inputs A and B. Our two outputs will be called C (carry out bit) and S (sum bit). The following is a truth table relating the outputs to the possible inputs.

A
B
S
C

0

0
0
0
0
1
1
0
1
0
1
0
1
1
1
1

S = A'B + AB'

C = AB

Step 3

Now we must assemble the logic circuits for these two functions in Digital Works itself. We start by placing all of the logic gates that we will need. I count 3 AND gates, 1 OR gates, and 2 NOT gates.

The first thing that you should notice is that I have omitted all inputs and outputs from the circuit. Building a macro is different from building a regular circuit. Instead of using switches for input and LED's for output, they use macro tags. These macro tags are used to specify the inputs and outputs of the circuit.

Step 4

Select the macro tag button from the toolbar (a tool tip will appear that says 'macro tag' if you put your mouse over it. Now place a macro tag for each input and output needed for the circuit. In our example, this will mean that 4 macro tags will be placed.

For convenience, I have also labeled the inputs/outputs. Macro tags can have wires connected to them just as normal inputs and outputs can.

Step 5

All we have to do now is connect all the wiring, as shown below.


Step 6

The next step is to design what the macro will look like when embedded into another circuit. Select the arrow icon and then right click on any of the macro tags in the circuit. Select Template Editor...

This will bring up the following window. Here is where we can change the appearance of the macro and also add the pins (input / output connections).

It is good practice to label the macro itself with some suitable name. In this example, we will call it 'Half Adder'. It also good practice to label the inputs / outputs, as below.

You will notice that there is an icon the toolbar in this window for inserting pins into the macro overview drawing. It looks like this

Insert one of these for each input and output in the circuit. In this case, you will need 4 (A,B,C,S). It should something like this.

Close out this window, which will bring you back to the logic diagram for the macro.

Step 7

Now, we must associate each macro tag in the logic schematic to a specify pin in the overview diagram. Right click on a single macro tag, go back into Template Editor...

Select the pin you wish to associate it with, right click on it and select Associate With Tag

You will notice that this changes the color of the pin from white to yellow. Close out the template editor and repeat this process for each of the macro tags. In the end, your template editor drawing should look something like this, where some of the pins will show up as red and one will show up as yellow.

The color of the pin designates its state. Red pins mean it is associated but it is not the current one selected. A yellow pin means that it is associated and it is the current one selected. A white pin means that you have not yet associated it. In the above picture, we selected the macro tag for output C and then opened the template editor with it selected. After all are done, close out the template editor.

 

You will now notice that each macro tag in the logic diagram has a number near it. This is the pin number that it is associated to.

Our macro is now complete. Save the circuit using File->Save and name it something descriptive. I will name this macro 'halfadd'.

 

 

 

::Embedding a Macro::  
 

Step 1

Now that we have a macro to work with, we will put it to work in another circuit. Start a new macro using File->New Macro. The first thing to do is embed the macros that we will want to work with. This is done by using the Embed Macro button on the toolbar () and then select where you would like it placed in the circuit. This will then bring up an Embed Macro dialog that will require you to navigate to the file where the macro is stored.

Select the appropriate file and press the Open button. This will place the macro as a 'chip' in your circuit. For demonstrational purposes, I will embed two of these macros into our circuit to show that you can do it multiple times. The result should look like this.

Since we intend to simulate this circuit, switches will have to be added for input and LED's for output.

Step 2

The final circuit looks like this.

You will notice that each of the macros acts as if it were the logic contained within. This allows us to reuse the same logic in multiple locations saving not only time but also space in our circuit since some equations can become cumbersome.