home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / jsage / zsus / progpack / bitmath.lbr / BITMATH.ZZ0 / BITMATH.Z80
Encoding:
Text File  |  1989-10-31  |  5.3 KB  |  234 lines

  1. ;BIT manipulation routines to set, reset, and test a
  2. ; bit in a byte in memory, to set bit (A) in A
  3.  
  4. ;Module Name: BITMATH
  5. ;Author: Al Hawley
  6. ;Date: 10/31/89
  7.  
  8. ;This module contains the following routines:
  9.     public res_m,set_m,tst_m
  10.     public exp_a,mask8,andm2m,orm2m,xorm2m
  11.     public mirror,mask16
  12.  
  13. ;  (A) means 'the contents of the A register'
  14. ;    1) set, reset, and test a bit in the byte at HL
  15. ;       SET_M, RES_M, TST_M
  16. ;    2) set bit (A) in A, reset others. This is the EXP function
  17. ;       EXP_A
  18. ;    3) Return LOG(A) in A, ignoring all but highest bit.
  19. ;       LOG2_A
  20. ;    4) set (A) least significant bits in A, others reset.
  21. ;       MASK8   this is equivalent to binary [[LOG(A+1)] -1]
  22. ;    5) set (A) least significant bits in BC, others reset.
  23. ;       MASK16   Same as MASK8, but returns 16 bit instead of 8.
  24. ;    6) logical AND, OR, XOR with (HL), result in (HL)
  25. ;       ANDM2M, ORM2M, XORM2M routines
  26. ;    7) reverse the bit order in the byte at HL. (mirror image)
  27. ;       MIRROR routine
  28.  
  29. ;=================================================
  30.  
  31. ;routines to set, reset, or test a bit.
  32. ;on entry:
  33. ;    a  = bit number (0-7)
  34. ;    hl = address of the byte
  35. ;on exit:
  36. ;    hl, de are preserved
  37. ;    b  = 46h(test) or 0c6h(set) or 86h(res)
  38. ;if z,    c = a = 0
  39. ;if nz,    c = a = 0ffh
  40. ; the z/nz returns are significant only when
  41. ; the test operation is performed.
  42.  
  43. ;these routines work by synthesizing the opcode
  44. ;for a z80 'cb' instruction.
  45.  
  46. res_m:    ;entry point to reset a bit
  47.     ld    b,86h
  48.     jr    makop
  49.  
  50. set_m:    ;entry point to set a bit
  51.     ld    b,0c6h
  52.  
  53. makop:    rlc    a        ;rotate the bit number
  54.     rlc    a        ;into the 3-4-5
  55.     rlc    a        ;position in the accumulator
  56.     or    a,b
  57.     ld    (opcode),a    ;install the opcode
  58.     db    0cbh        ;and execute the instruction
  59. opcode:    db    0        ;..to do a bit, res, or set
  60.     ret
  61.  
  62. tst_m:    ;entry point to test a bit
  63.     ld    b,46h
  64.     call    makop        ;do the test
  65.     ld    c,0ffh        ;adjust regs c and a
  66.     ld    a,c
  67.     ret    nz
  68.     inc    c
  69.     ld    a,c
  70.     ret
  71.  
  72. ;=================================================
  73.  
  74. exp_a:
  75. ;set a single bit in reg A, reset all others.
  76. ;on entry,
  77. ;    a    = bit position to set (0-7)
  78. ;on exit,
  79. ;    a    = requested bit set, others 0
  80. ;    flags are undefined
  81. ;all other registers preserved.
  82.  
  83.     push    bc
  84.     ld    b,a
  85.     inc    b
  86.     xor    a
  87.     scf
  88. expa1:    rla
  89.     djnz    expa1
  90.     pop    bc
  91.     ret
  92.  
  93. ;=================================================
  94.  
  95. ;convert a byte with a single bit set to the ordinal
  96. ;number in binary corresponding to the bit position.
  97. ;this is a special case of log n, where n= 0...7
  98. ;on entry the byte to convert is in reg A.
  99. ;on exit, reg A contains the log of A (bit position)
  100.  
  101. log2_a:    push    bc
  102.     ld    b,7        ;7 bits to decrement at most
  103.     or    a,a        ;clear carry
  104. log2a:    rl    a        ;rotate left through carry
  105.     jr    c,log2ax    ;done if bit shifted into cy
  106.     djnz    log2a        ;the 8th dec never occurs
  107. log2ax:    ld    a,b
  108.     or    a        ;reset cy
  109.     pop    bc
  110.     ret
  111.  
  112. ;=================================================
  113.  
  114. mask16:
  115. ;set the n least significant bits
  116. ;in register pair BC according to the
  117. ;value (0..15) in A. A is not preserved
  118.  
  119.     ld    b,a    ;save for test of bit 3
  120.     and    7    ;use 3 lsbits
  121.     call    mask8    ;return mask in A
  122.     ld    c,a
  123.     xor    a        ;make a zero
  124.     bit    3,b        ;high byte?
  125.     ld    b,a        ;in case not
  126.     ret    z        ;low byte has the mask
  127.     ld    b,c        ;mask in high byte
  128.     dec    a        ;make 0ff for low byte
  129.     ld    c,a        ;low byte
  130.     ret
  131.  
  132. ;=================================================
  133.  
  134. mask8:
  135. ;set n least significant bits in reg A.
  136. ;Remaining bits are reset.
  137. ; n is specified as a number, 0-7 which
  138. ; defines the highest bit set.
  139. ;on entry,
  140. ;    a    = highest bit to set (0-7)
  141. ;on exit,
  142. ;    a    = filled with 1's from position
  143. ;        0 up through position specified
  144. ; all other registers preserved
  145.     push    bc
  146.     ld    b,a
  147.     inc    b
  148.     xor    a
  149. bcmsk1:    scf
  150.     rla
  151.     djnz    bcmsk1
  152.     pop    bc
  153.     or    a    ;reset cy (=no error)
  154.     ret
  155.  
  156. ;=================================================
  157.  
  158. andm2m:
  159. ;perform the logical and of the byte
  160. ;in the accumulator with the byte @hl
  161. ;preserves all registers
  162.     push    af
  163.     and    a,(hl)
  164.     ld    (hl),a
  165.     pop    af
  166.     ret
  167.  
  168. ;=================================================
  169.  
  170. orm2m:
  171. ;perform the logical or of the byte
  172. ;in the accumulator with the byte @hl
  173. ;preserves all registers
  174.     push    af
  175.     or    a,(hl)
  176.     ld    (hl),a
  177.     pop    af
  178.     ret
  179.  
  180. ;=================================================
  181.  
  182. xorm2m:
  183. ;perform the logical xor of the byte
  184. ;in the accumulator with the byte @hl
  185. ;preserves all registers
  186.     push    af
  187.     xor    a,(hl)
  188.     ld    (hl),a
  189.     pop    af
  190.     ret
  191.  
  192. ;=================================================
  193.  
  194. mirror:
  195. ;invert the order of the bits at (hl)
  196. ;input: hl = addr of byte to invert
  197. ;output: mirror image in (hl)
  198. ;  swap bits 0-7,1-6,2-5,3-4
  199. ; reg a is destroyed
  200. ;all other registers preserved
  201.  
  202. ;test for common symetrical cases and
  203. ;do nothing (it's faster)
  204.     ld    a,(hl)
  205.     or    a    ;00000000b?
  206.     ret    z
  207.     cp    0ffh    ;11111111b?
  208.     ret    z
  209. ;probably not symetrical. make mirror image.
  210.     push    bc
  211.     rra        ;lsb->cy, rotate right
  212.     rl    c    ;cy-lsb, rotate left
  213.     rra        ;..repeat 7 more times
  214.     rl    c
  215.     rra
  216.     rl    c    ;this code would be shorter
  217.     rra        ;..if done in a loop, but
  218.     rl    c    ;..also slower
  219.     rra
  220.     rl    c
  221.     rra
  222.     rl    c
  223.     rra
  224.     rl    c
  225.     rra
  226.     rl    c    ;reversed pattern in c
  227.     ld    (hl),c    ;store at memory loc
  228.     pop    bc
  229.     ret
  230.  
  231. ;=================================================
  232.  
  233.     end
  234.