Re: LZ: Question...


[Prev][Next][Index][Thread]

Re: LZ: Question...



> Could some one send me a _GOOD_ explanation of how to work with matrix in
> z80?

  OK, here goes... mind, I'm doing this on the fly.  My source may need
checking.  That said:

1.  I think "matrix" is a misnomer.  It implies similarity to the TI-OS
type.  The term I'll be using herein is "array".  Be advised.

2.  To access a position in an array, you need to know its coordinates
within the array; for example, in a 2d array, (2,3).  This lets you
calculate the offset from the array's start of any element within it (I'll
be numbering with the top-left as (0,0), the one to its right (1,0), and
the one below it (0,1):

    0 1 2 ...
   +---------
  0|1 0 0 1010
  1|0 1 1 0101    (Note that the element values are
  2|1 0 1 0000     completely arbitrary and solely
  .|0 1 0 1111     for purposes of example.)
  .|1 0 1 0101

3.  The formula for offset in a 2d-array is:
  <x_coord> + (<y_coord>*<array_width>)
  Add that to the array location to get the memory location to access.
  (Note that this is for arrays with elements which are 1 byte each).

4.  Some sample code:

  Parameters: D - x coordinate.
              E - y coordinate.
              HL - offset of array to access.
              B - width of array
  Returns:    A - value of byte specified.
  Traps:      Changes the values of DE, HL, and B.
  ---Begin my code---
  get_array_byte:
    ld A, D  ; This next block is just to add D to HL.
    add A, L
    ld L, A
    jr NC, nocarry
    inc H
  nocarry:
    
  addloop: ; There's no "mult" instruction, either.  You might wish
           ; to use someone else's multiplication function here, as
           ; this one will be slow.
    ld A, 0
    sub B
    jr Z, adddone
    add E, L
    jr NC, nocarrytwo
    inc H
  nocarrytwo:
    dec B
    jr NZ, addloop
  addone:

    ld A, (HL)
    ret
  ---End my code---

    Modifying this to set a byte should be fairly easy, but using bits
  instead of bytes you're doing on your own!

5.  Setting up an array in your code:
  Simple:  just put the values, in the order you want them in the array.
  For a byte array, you might have:
    .db 3, 1, 4, 1, 5, 9
    .db 2, 7, 1, 8, 2, 8
    ...
  Note that this will have the same effect as
    .db 3, 1, 4, 1, 5, 9, 2, 7, 1, 8, 2, 8
  - the two are in every respect equivalent.
  For an array of bits, instead, you would prefix each byte (8 bits) with
  a '%':
    .db %11111111
    .db %10000001
    .db %10011001
    ...
  Bit arrays are slightly complicated in this regard: you have to end on a
byte boundary; that is, the width must be a multiple of 8.


  HTH,

                     /***********************************
                     *           Daniel Knapp           *
                     *       FeatherWate Software       *
                     *     email: dankna@bergen.org     *
                     *                                  *
                     *      Whose account this is,      *
                     *         I think I know.          *
                     * The sysop is in Berkely, though. *
                     * She will not see me typing here, *
                     *        To watch this Mac         *
                     *          Refuse to go.           *
                     ************************************
                     *  Watch for a new, shorter sig -  *
                     *          coming soon!            *
                     ***********************************/



References: