; A few improvements are noted below, toward the end, marked with "***".
; They have the staggering effect of reducing the length of the code by 4
; bytes.

; I also made a note of an optional change which would reduce the code length
; by 2 more bytes, at the cost of 3 clock cycles in the clear-pixel case.

; \\      Darryl Nester     \/ Assoc. Prof. of Mathematics //
; \\  nesterd@bluffton.edu  \/       Bluffton College      //
; \\    ph: 419-358-3483    \/   Bluffton, OH  45817-1196  //

;*********************************************************
;                    plotxy
; Routine to turn on or off a single pixel.  Enter with reg BC
; containing the X,Y value, X = 0-127 Y = 0-63.
; X,Y origin (0,0) is lower left of screen.
; If reg A is zero, clear the pixel, if not zero, turn it on.
; Returns with HL pointing at the LCD byte, reg A containing the byte.
; BC and DE are preserved.
;*********************************************************


plotxy:         push    de                      ;preserve reg
                push    af                      ;preserve on/off flag
                ld      a,7
                and     b                       ;get three LSbits in xpos

lookupref:      ld      hl,(PROGRAM_ADDR)
                ld      de,lookup8
                add     hl,de                   ;make it a ZSHELL relocate
                ld      d,0
                ld      e,a
                add     hl,de
                ld      l,(hl)                  ;translate into bit and
                                                ;preserve it in l
                ld      a,63
                sub     c                       ;invert ypos (63-0)
                ld      d,a
                ld      a,b                     ;xpos (0-127)

                rlca                            ;double xpos, clear carry

                srl     d
                rra
                srl     d
                rra
                srl     d
                rra
                srl     d
                rra                             ;16 bit shift right four bits
                                                ;through carry of da

                ld      e,a                     ;de contains the
                                                ;offset into LCD mem
                pop     af
                and     a
                ld      a,l             ;*** does not affect flags
                ld      hl,$fc00        ;*** does not affect flags
                jr      nz,setpxl               ;if reg a nonzero, go set pixel
                                                ;zero, clear pixel
                cpl                             ;invert the byte
                add     hl,de                   ;point at correct byte
                and     (hl)                    ;clear the bit
;***
;*** Optional change
;*** replace the next three lines with the line
;***    .db $11
;***
                ld      (hl),a                  ;write it back.
                pop     de
                ret

setpxl:         add     hl,de                   ;point at correct byte
                or      (hl)                    ;set the bit
                ld      (hl),a                  ;write it out
                pop     de

                ret

lookup8:        .db     128
                .db     64
                .db     32
                .db     16
                .db     8
                .db     4
                .db     2
                .db     1


