;; plot a pixel using the firmware screen functions

scr_set_mode equ &bc0e
scr_dot_position equ &bc1d
scr_ink_encode equ &bc2c
kl_u_rom_enable equ &b900
txt_set_paper equ &bb96
txt_clear_window equ &bb6c
scr_get_location equ &bc0b
scr_next_line equ &bc26

pen0_r equ &00
pen1_r  equ &40
pen2_r  equ &04
pen3_r  equ &44
pen4_r  equ &10
pen5_r  equ &50
pen6_r  equ &14
pen7_r  equ &54
pen8_r  equ &01
pen9_r  equ &41
pen10_r equ &05
pen11_r equ &45
pen12_r equ &11
pen13_r equ &51
pen14_r equ &15
pen15_r equ &55
pen0_l equ pen0_r*2
pen1_l equ pen1_r*2
pen2_l equ pen2_r*2
pen3_l equ pen3_r*2
pen4_l equ pen4_r*2
pen5_l equ pen5_r*2
pen6_l equ pen6_r*2
pen7_l equ pen7_r*2
pen8_l equ pen8_r*2
pen9_l equ pen9_r*2
pen10_l equ pen10_r*2
pen11_l equ pen11_r*2
pen12_l equ pen12_r*2
pen13_l equ pen13_r*2
pen14_l equ pen14_r*2
pen15_l equ pen15_r*2

org &4000

;; uncomment to see that this causes an effect
;;call kl_u_rom_enable

;; set mode 0
xor a
call scr_set_mode	

;; set paper
ld a,1
call txt_set_paper

;; clears window to paper colour
call txt_clear_window	

;; plot point
;;
;; for mode 0: x is in the range 0-159
;; for mode 1: x is in the range 0-320
;; for mode 2: x is in the range 0-639
;; for all modes: y is in the range 0-200
;; origin is bottom left

;; get address of pixel
;; change coords for mode 1 and 2 to get same location as mode 0
ld de,80		;; x 
ld hl,100		;; y 
call scr_dot_position
;; HL = address of byte containing pixel
;; C = bit mask for pixel
;; B = number of pixels in byte

push hl
push bc

ld a,3				;; pen 3

;; lookup encoded pixels from array
;; this performs a similar thing to SCR INK ENCODE but
;; is fixed for mode 0
;; a different array would be needed for mode 1
ld l,a
ld h,0
ld de,inks
add hl,de
;; HL = address in array
ld a,(hl)

;; A = pen 3 encoded into a form to poke to screen
;; but which would set all pixels in that byte

pop bc
pop hl
;; isolate just the pixel we want
;; using the pixel mask
and c
ld d,a


;; this avoids SCR PIXELS, but it is more complex.
;; We also require screen memory to be readable and writeable.
;; e.g. upper rom can't be enabled.
;;
;; SCR PIXELS does all the work necessary to enable/disable rom.
;;
;;
;; we need to read from screen
;; mask the pixels we don't want to change
;; combine with our pixel and write back

;; mask for pixel we want to write
ld a,c

;; complement it to make mask for pixels to keep
cpl

;; isolate pixels on screen 
and (hl)

;; combine with our pixel
or d

;; write to screen
ld (hl),a

ret

;; DE = x 
;; HL = y

;; Y*2 to give offset in array
add hl,hl
ld a,l
add a,scr_table AND 255
ld l,a
ld a,h
adc a,scr_table/256
ld h,a

;; read address from array
ld a,(hl)
inc hl
ld h,(hl)
ld l,a


get_scr_addr:

make_scr_table:
call scr_get_location
;; A = most significant byte of screen address
;; HL = offset

;; combine significant byte of screen address with offset
;; to get base
or h
ld h,a

ld ix,scr_table

mst1:
ld (ix+0),l
ld (ix+1),h
call scr_next_line
inc ix
inc ix
djnz mst1
ret


scr_table:
defs 200*2

;; bit mask for pixel that SCR DOT POSITION would give us for mode 0
pixel_mask:
defb &aa		;; left pixel
defb &55		;; right pixel


;; encodings of the pixels (equivalent to SCR INK ENCODE would give us for mode 0)
;; values for left and right pixel (only 2 pixels per byte in mode 0)
inks:
defb pen0_l+pen0_r
defb pen1_l+pen1_r
defb pen2_l+pen2_r
defb pen3_l+pen3_r
defb pen4_l+pen4_r
defb pen5_l+pen5_r
defb pen6_l+pen6_r
defb pen7_l+pen7_r
defb pen8_l+pen8_r
defb pen9_l+pen9_r
defb pen10_l+pen10_r
defb pen11_l+pen11_r
defb pen12_l+pen12_r
defb pen13_l+pen13_r
defb pen14_l+pen14_r
defb pen15_l+pen15_r
