Skip to content

Commit 518dccc

Browse files
committed
Add HMIRROR.BAS library
1 parent 8ad506c commit 518dccc

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

docs/library/hmirror.bas.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#HMirror.bas
2+
3+
This Function takes a byte in, and returns the byte that has reflected the bits around the x axis,
4+
such that bits 7,6,5,4,3,2,1,0 become bits 0,1,2,3,4,5,6,7.
5+
6+
This can be useful if you elect to make all your graphics face in one direction -
7+
you can mirror the bytes (perhaps to a buffer or a UDG) before you print them.
8+
It's faster to store them facing both ways, but you can make quite a memory saving if you just choose one way.
9+
10+
11+
```
12+
Function fastcall hMirror (number as uByte) as uByte
13+
asm
14+
ld c,a
15+
; unrolled loop for speed. Still quite small - costs 10 bytes over the loop version, and saves over half the time.
16+
; 25 bytes and 96 clock cycles
17+
18+
RR C
19+
RLA
20+
RR C
21+
RLA
22+
RR C
23+
RLA
24+
RR C
25+
RLA
26+
RR C
27+
RLA
28+
RR C
29+
RLA
30+
RR C
31+
RLA
32+
RR C
33+
RLA
34+
end asm
35+
END FUNCTION
36+
```
37+
38+
The above function is basically deprecated, but may be easier to understand than the following.
39+
This one below is faster, and smaller. You should use this one:
40+
41+
42+
```
43+
Function fastcall hMirror (number as uByte) as uByte
44+
asm
45+
;17 bytes and 66 clock cycles
46+
Reverse:
47+
ld b,a ;b=ABCDEFGH
48+
rrca ;a=HABCDEFG
49+
rrca ;a=GHABCDEF
50+
xor b
51+
and %10101010
52+
xor b ;a=GBADCFEH
53+
ld b,a ;b=GBADCFEH
54+
rrca ;a=HGBADCFE
55+
rrca ;a=EHGBADCF
56+
rrca ;a=FEHGBADC
57+
rrca ;a=CFEHGBAD
58+
xor b
59+
and %01100110
60+
xor b ;a=GFEDCBAH
61+
rrca ;a=HGFEDCBA
62+
end asm
63+
end function
64+
```

0 commit comments

Comments
 (0)