# Reverse Bits

Useful macros for bit reversing and selecting in different ways. Only valid to be used from assembly language (not from C).

Summary

Reverse Bits | Useful macros for bit reversing and selecting in different ways.
Macros |
cpctm_reverse_and_select_bits_of_A | Reorders the bits of A and mixes them letting the user select the new order for the bits by using a selection mask.
cpctm_reverse_bits_of_A | Macro: cpctm_reverse_mode_2_pixels_of_A
cpctm_reverse_mode_1_pixels_of_A | Reverses the order of pixel values contained in register A, assuming A is in screen pixel format, mode 1.
cpctm_reverse_mode_0_pixels_of_A | Reverses the order of pixel values contained in register A, assuming A is in screen pixel format, mode 0.

### Macros

### cpctm_reverse_and_select_bits_of_A

Reorders the bits of A and mixes them letting the user select the new order for the bits by using a selection mask.

#### Parameters

TReg | An 8-bits register that will be used for intermediate calculations. This register may be one of these: B, C, D, E, H, L
SelectionMask | An 8-bits mask that will be used to select the bits to get from the reordered bits. It might be an 8-bit register or even (hl).

#### Input Registers

A | Byte to be reversed
TReg | Should have a copy of A (same exact value)

#### Return Value

A | Resulting value with bits reversed and selected

#### Details

This macro reorders the bits in A and mixes them with the same bits in their original order by using a SelectionMask. The process is as follows:

1. Consider the 8 bits of A = TReg = [01234567] 2. Reorder the 8 bits of A, producing A2 = [32547610] 2. Reorder the bits of TReg, producing TReg2 = [76103254] 3. Combines both reorders into final result using a SelectionMask. Each 0 bit from the selection mask means “select bit from A2”, whereas each 1 bit means “select bit from TReg2”.

#### For instance, a selection mask 0b11001100 will produce this result

>          A2 = [ 32 54 76 10 ]
>       TReg2 = [ 76 10 32 54 ]
>     SelMask = [ 11 00 11 00 ] // 1 = TReg2-bits, 0 = A2-bits
>     ---------------------------
>      Result = [ 76 54 32 10 ]

Therefore, mask 0b11001100 produces the effect of reversing the bits of A completely. Other masks will produce different reorders of the bits in A, for different requirements or needs.

#### Modified Registers

AF, TReg

#### Required memory

16 bytes

#### Time Measures

>      Case | microSecs(us) | CPU Cycles
>     ------------------------------------
>      Any  |      16       |     64
>     ------------------------------------

### cpctm_reverse_bits_of_A

Macro: cpctm_reverse_mode_2_pixels_of_A

Reverses the 8-bits of A, from [01234567] to [76543210]. This also reverses all pixels contained in A when A is in screen pixel format, mode 2.

#### Parameters

TReg | An 8-bits register that will be used for intermediate calculations. This register may be one of these: B, C, D, E, H, L

#### Input Registers

A | Byte to be reversed
TReg | Should have a copy of A (same exact value)

#### Return Value

A | Resulting value with bits reversed

#### Requires

- Uses the macro cpctm_reverse_and_select_bits_of_A.

#### Details

This macro reverses the bits in A. If bits of A = [01234567], the final result after processing this macro will be A = [76543210]. Register TReg is used for intermediate calculations and its value is destroyed.

#### Modified Registers

AF, TReg

#### Required memory

16 bytes

#### Time Measures

>      Case | microSecs(us) | CPU Cycles
>     ------------------------------------
>      Any  |      16       |     64
>     ------------------------------------

### cpctm_reverse_mode_1_pixels_of_A

Reverses the order of pixel values contained in register A, assuming A is in screen pixel format, mode 1.

#### Parameters

TReg | An 8-bits register that will be used for intermediate calculations. This register may be one of these: B, C, D, E, H, L

#### Input Registers

A | Byte with pixel values to be reversed
TReg | Should have a copy of A (same exact value)

#### Return Value

A | Resulting byte with the 4 pixels values reversed in order

#### Requires

- Uses the macro cpctm_reverse_and_select_bits_of_A.

#### Details

This macro considers that A contains a byte that codifies 4 pixels in screen pixel format, mode 1. It modifies A to reverse the order of its 4 contained pixel values left-to-right (1234 -> 4321). With respect to the order of the 8-bits of A, the concrete operations performed is:

>     A = [01234567] == reverse-pixels ==> [32107654] = A2

You may want to check cpct_px2byteM1 to know how bits codify both pixels in one single byte for screen pixel format, mode 1.

TReg is an 8-bit register that will be used for intermediate calculations, destroying its original value (that should be same as A, at the start).

#### Modified Registers

AF, TReg

#### Required memory

16 bytes

#### Time Measures

>      Case | microSecs(us) | CPU Cycles
>     ------------------------------------
>      Any  |      16       |     64
>     ------------------------------------

### cpctm_reverse_mode_0_pixels_of_A

Reverses the order of pixel values contained in register A, assuming A is in screen pixel format, mode 0.

#### Parameters

TReg | An 8-bits register that will be used for intermediate calculations. This register may be one of these: B, C, D, E, H, L

#### Input Registers

A | Byte with pixel values to be reversed
TReg | Should have a copy of A (same exact value)

#### Return Value

A | Resulting byte with the 2 pixels values reversed in order

#### Details

This macro considers that A contains a byte that codifies 2 pixels in screen pixel format, mode 0. It modifies A to reverse the order of its 2 contained pixel values left-to-right (12 -> 21). With respect to the order of the 8-bits of A, the concrete operation performed is:

>     A = [01234567] == reverse-pixels ==> [10325476] = A2

You may want to check cpct_px2byteM0 to know how bits codify both pixels in one single byte for screen pixel format, mode 0.

TReg is an 8-bit register that will be used for intermediate calculations, destroying its original value (that should be same as A, at the start).

#### Modified Registers

AF, TReg

#### Required memory

7 bytes

#### Time Measures

>      Case | microSecs(us) | CPU Cycles
>     ------------------------------------
>      Any  |       7       |     28
>     ------------------------------------

Reorders the bits of A and mixes them letting the user select the new order for the bits by using a selection mask.
Transforms 4 pixel colour values [0-3] into a byte value in the video memory pixel format for Mode 1.
Transforms 2 pixel colour values [0-15] into a byte value in the video memory pixel format for Mode 0.
Close