Copyright (c) Hyperion Entertainment and contributors.

Graphics Minterms

From AmigaOS Documentation Wiki
Jump to navigation Jump to search

Several graphics function calls require a minterm value to be specified which defines a logic function to perform.

Some background knowledge of the classic Amiga blitter hardware is helpful to understand how to derive the parameter and decode what it means.

Classic Amiga Blitter

The blitter can combine the data from the three source DMA channels in up to 256 different ways to generate the values stored by the destination DMA channel. These sources might be one bitplane from each of three separate graphics images. While each of these sources is a rectangular region composed of many points, the same logic operation will be performed on each point throughout the rectangular region. Thus, for purposes of defining the blitter logic operation it is only necessary to consider what happens for all of the possible combinations of one bit from each of the three sources.

There are eight possible combinations of values of the three bits, for each of which we need to specify the corresponding destination bit as a zero or one. This can be visualized with a standard truth table, as shown below. We have listed the three source channels, and the possible values for a single bit from each one.

A B C D BLTCON0
position
Minterm
0 0 0 ? 0 NANBNC
0 0 1 ? 1 NANBC
0 1 0 ? 2 NABNC
0 1 1 ? 3 NABC
1 0 0 ? 4 ANBNC
1 0 1 ? 5 ANBC
1 1 0 ? 6 ABNC
1 1 1 ? 7 ABC

Where 'N' represents 'not'.

This information is collected in a standard format, the LF (Logic Function) control byte in the BLTCON0 register. This byte programs the blitter to perform one of the 256 possible logic operations on three sources for a given blit.

To calculate the LF control byte in BLTCON0, fill in the truth table with desired values for D, and read the function value from the bottom of the table up.

For example, if we wanted to set all bits in the destination where the corresponding A source bit is 1 or the corresponding B source bit is 1, we would fill in the last four entries of the truth table with 1 (because the A bit is set) and the third, fourth, seven, and eight entries with 1 (because the B bit is set), and all others (the first and second) with 0, because neither A nor B is set. Then, we read the truth table from the bottom up, reading 11111100, or $FC.

For another example, an LF control byte of $80 ( = 1000 0000 binary) turns on bits only for those points of the D destination rectangle where the corresponding bits of A, B, and C sources were all on (ABC = 1, bit 7 of LF on). All other points in the rectangle, which correspond to other combinations for A, B, and C, will be 0. This is because bits 6 through 0 of the LF control byte, which specify the D output for these situations, are set to 0.

Graphics Minterms

The blitter functions provided by graphics.library were originally designed to support the classic Amiga blitter with a total of 4 DMA channels. Modern graphics cards have their own blitter(s) which programmers are not allowed direct access to. However, the minterm value still exists to help specify logic operations to perform while performing the copy which may or may not be assisted by the graphics card hardware.

The roles of each of the 4 channels are as follows:

Channel Description
A Non-zero when inside the rectangle
B Source rectangle
C Source mask or destination rectangle
D Destination rectangle

Channel A is non-zero only when inside the rectangle and we don't care about any pixels outside the source rectangle. Therefore, the truth table can be simplified to:

B C D Bit Minterm
0 0 ? 4 NBNC
0 1 ? 5 NBC
1 0 ? 6 BNC
1 1 ? 7 BC

This means only the upper 4 bits (bits 4 to 7) of the minterm are used to select the logic operation. The lower bits (0 to 3) will always be zero because channel A is always non-zero or inside the rectangle.

Creating Logic Functions

Creating your own logic functions is easy if you understand there are four channels (A, B, C, D) and what each channel is assigned to do.

Select channel A

With all of the graphics.library functions channel A is non-zero only when inside the rectangle. This ensures that all pixels outside the rectangle are ignored for purposes of the blit. There is no other choice for channel A.

Select channel B

Channel B is always the source rectangle. You may choose to select pixels as-is (B) or invert them as they are read from the source (NB).

Select channel C

Channel C is either a mask the same dimensions as the channel B source rectangle or it is not used and defined as the destination. When used as a mask, you can choose to select pixels in the mask (C) or not in the mask (NC).

Determine channel D logic function

Channel D is always the destination channel.

Designing the LF Control Byte with Minterms

One approach to designing the LF control byte uses logic equations. Each of the rows in the truth table corresponds to a "minterm", which is a particular assignment of values to the A, B, and C bits. For instance, the first minterm is usually written:

NANBNC

or "not A and not B and not C". The last is written as ABC.

Blitter logic
Two terms that are adjacent are AND'ed, and two terms that are separated by "+" are OR'ed. AND has a higher precedence, so AB + BC is equal to (AB) + (BC).

Any function can be written as a sum of minterms. If we wanted to calculate the function where D is one when the A bit is set and the C bit is clear, or when the B bit is set, we can write that as:

ANC + B

or "A and not C or B". Since "1 and A" is "A":

D = ANC + B

D = A(1)NC + (1)B(1)

Since either A or NA is true (1 = A + NA), and similarly for B, and C; we can expand the above equation further:

D = A(1)NC + (1)B(1)

D = A(B + NB)NC + (A + NA)B(C + NC)

D = ABNC + ANBNC + AB(C + NC) + NAB(C + NC)

D = ABNC + ANBNC + ABC + ABNC + NABC + NABNC

After eliminating duplicates, we end up with the five minterms:

ANC + B = ABNC + ANBNC + ABC + NABC + NABNC

These correspond to bit positions of 6, 4, 7, 3 and 2 according to our truth table, which we would then set, and clear the rest.

The wide range of logic operations allow some sophisticated graphics techniques. For instance, you can move the image of a car across some pre-existing building images with a few blits. Producing this effect requires predrawn images of the car, the buildings (or background), and a car "mask" that contains bits set wherever the car image is not transparent. This mask can be visualized as the shadow of the car from a light source at the same position as the viewer.

Table 6-1 lists some of the most common functions and their values, for easy reference.

               Table 6-1: Table of Common Minterm Values


         Selected      BLTCON0             Selected      BLTCON0 
         Equation      LF Code            Equation      LF Code
         --------      -------            --------      -------
         D = A           $F0              D =  AB         $C0
             _                                  _
         D = A           $0F              D =  AB         $30
                                               _
         D = B           $CC              D =  AB         $0C
             _                                 __
         D = B           $33              D =  AB         $03
         D = C           $AA              D =  BC         $88
             _                                  _
         D = C           $55              D =  BC         $44
                                               _
         D = AC          $A0              D =  BC         $22
              _                                __
         D = AC          $50              D =  AC         $11
             _                                     _
         D = AC          $0A              D =  A + B      $F3
             _                                 _   _
         D = AC          $05              D =  A + B      $3F
                                                   _
         D = A + B       $FC              D =  A + C      $F5
             _                                 _   _
         D = A + B       $CF              D =  A + C      $5F
                                                   _
         D = A + C       $FA              D =  B + C      $DD
             _                                 _   _
         D = A + C       $AF              D =  B + C      $77
                                                    _
         D = B + C       $EE              D =  AB + AC    $CA
             _
         D = B + C       $BB