;Dazzle Doodle Reconstruction

	DAZADR	EQU	0EH	;SET DAZZLER MEMORY ADDRESS. MSB IS ENABLE.
	DAZMOD	EQU	0FH	;SET DAZZLER MODE

	VIDMEM	EQU	0800H	;VIDEO MEMORY STARTING ADDRESS

	DAZINIT	EQU	VIDMEM/512 + 80H ;VIDEO START ADDRESS, DAZZLER ENABLED

	DAZ64	EQU	30H	;DAZZLER SET FOR 64x64

	D7ASW	EQU	18H	;D+7A: LEFT JS-1 SWITCHES IN BITS 0-3
	D7ALX	EQU	19H	;D+7a: LEFT JS-1 X AXIS
	D7ALY	EQU	1AH	;D+7A: LEFT JS-1 Y AXIS

;
	ORG	100H

;INITIALIZE DAZZLER

	MVI	A,DAZINIT
	OUT	DAZADR

MAIN:	MVI	A,DAZ64
	OUT	DAZMOD

;READ JS-1 BUTTONS TO GET THE COLOR, AND SAVE RESULT IN B

	IN	D7ASW		;READ SWITCHES
	CMA			;SO THAT 1 MEANS PUSHED
	ORI	10H		;SO THAT WE DRAW WITH BRIGHT COLORS
	RAR			;COLOR SWITCHES ARE 0-2, SWITCH 1 IS IN CARRY BIT
	MOV	B,A		;COLORS SAVED IN B

	JC	DOCLR		;GO DO CLEAR IF SWITCH 1 PUSHED

;READ JS-1 X AXIS, SHIFT IT TO BE A POSITIVE NUMBER, CLIP AT SCREEN SIZE
;AND SAVE RESULT IN E

	IN	D7ALX		;READ X AXIS
	ADI	40H		;SCREEN IS 40HX40H, BUT WE BLOW OFF LSB
	JP	XNCLIP		;IF RESULT IS BELOW 80H, DRAW IN SCREEN

	MVI	B,0		;IF OFF-SCREEN, CLEAR COLOR SO WE DON'T DRAW

XNCLIP:	RAR			;BLOW OFF LSB
	MOV	E,A		;SAVE X DISPLACEMENT IN E

;READ JS-1 Y AXIS, SHIFT IT TO BE A POSITIVE NUMBER, CLIP AT SCREEN SIZE
;AND SAVE RESULT IN A (AND D)

	IN	D7ALY		;READ Y AXIS
	ADI	40H		;SCREEN IS 40HX40H, BUT WE BLOW OFF LSB
	JP	YNCLIP		;IF RESULT IS BELOW 80H, DRAW IN SCREEN

	MVI	B,0		;IF OFF-SCREEN, CLEAR COLOR SO WE DON'T DRAW

YNCLIP:	RAR			;BLOW OFF LSB
	CMA			;CORRECT Y DIRECTION: UP=LOWER ADDRESS
	MOV	D,A		;SAVE Y DISPLACEMENT IN D FOR NO REASON

;THREE MYSTERY NOPS
	NOP
	NOP
	NOP

; THE ADDRESS BIT ORDER FOR A DAZZLER MEMORY ADDRESS IS THIS:
;
; X COMPONENT = X<5..0>
; Y COMPONENT = Y<5..0>
; MEMORY ADDRESS
;     A<15..11> = BEGINNING ADDRESS OF DAZZLER MEMORY
;         A<10> = Y<5>
;         A<9>  = X<5>
;       A<8..4> = Y<4..0>
;       A<3..0> = X<4..1>
;
; WITHIN THIS ADDRESS, D<7..4> SELECTED IF X<0>=1
;                      D<3..0> SELECTED IF X<0>=0


	ANI	3FH		;REMOVE UPPER TRASH BITS
	MOV	H,A		;H NOW HAS Y DISPLACEMENT
	ANI	20H		;GET JUST Y MSB
	ADD	H		;MOVE Y MSB LEFT 1 BIT FOR DAZZLER ADDRESS
	MOV	H,A		;H HAS Y BITS WITH CORRECT SPACING

; H = 0 Y<5> 0 Y<4> Y<3> Y<2> Y<1> Y<0>

	MOV	A,E		;GET X DISPLACEMENT
	ANI	20H		;JUST MSB
	ORA	H		;PUT X MSB IN PLACE FOR DAZZLER ADDRESS
	RRC			;SWAP NIBBLES
	RRC
	RRC
	RRC			;LOW NIBBLE IS CORRECT FOR DAZZLER HIGH BYTE
	MOV	H,A

; H = Y<3> Y<2> Y<1> Y<0> 0 Y<5> X<5> Y<4>

	MOV	A,E		;GET X DISPLACEMENT AGAIN
	RRC			;BECAUSE LOW BIT IS ACTUALLY LEFT/RIGHT NIBBLE
	ANI	0FH		;X DISPLACEMENT BITS 1-4 ARE IN PLACE FOR L
	MOV	L,A

; L = 0 0 0 0 X<4> X<3> X<2> X<1>

	MOV	A,H		;GET LOW NIBBLE OF Y DISPLACEMENT
	ANI	0F0H
	ORA	L		;COMBINE WITH X DISPLACEMENT 1-4
	MOV	L,A		;L IS ALL DONE NOW

; L = Y<3> Y<2> Y<1> Y<0> X<4> X<3> X<2> X<1>

	MOV	A,H		;GET HIGH BITS OF X AND Y
	ANI	7		;STRIP OFF TRASH
	ORI	8		;SET MEMORY ADDRESS TO 800H
	MOV	H,A		;H IS ALL DONE NOW

; H = 0 0 0 0 1 Y<5> X<5> Y<4>

;FETCH ONE BYTE (2 PIXELS) FROM MEMORY INT C, AND DETERMINE WHICH NIBBLE WE ARE ON

	MOV	C,M

		MOV	A,E	;GET X POSITION
		RRC		;TEST LSB
		JC	HINIB

;PASTE NEW COLOR INTO LOW NIBBLE

		MVI	A,0FH
		ANA	B	;B<3..0> HAS THE NEW COLOR
		ORA	C	;COMBINE WITH HIGH NIBBLE
		MOV	M,A	;PUT BOTH NIBBLES BACK

		JMP	MAIN	;ALL DONE


;PASTE NEW COLOR INTO HIGH NIBBLE

HINIB:		MVI	A,0FH
		ANA	B	;B<3..0> HAS THE NEW COLOR
		RLC		;MOVE COLOR TO HIGH-NIBBLE POSITION
		RLC
		RLC
		RLC
		ORA	C	;COMBINE WITH LOW NIBBLE
		MOV	M,A	;PUT BOTH NIBBLES BACK

		JMP	MAIN	;ALL DONE

;CLEAR (FILL) THE SCREEEN TO THE SPECIFIED COLOR
	
DOCLR:		LXI	H,VIDMEM
		MVI	A,0FH	;GET THE FILL COLOR FROM THE SWITCH READ
		ANA	B
		MOV	C,A	;PUT THE COLOR INTO BOTH NIBBLES
		RRC
		RRC
		RRC
		RRC
		ORA	C
		MOV	C,A	;C HAS COLOR IN BOTH NIBBLES

CLRLUP:		MOV	M,C	;CLEAR 2 NIBBLES
		INX	H	;NEXT ADDRESS
		MOV	A,H	;DONE YET?
		CPI	10H	;THIS CLEARS TOO MUCH MEMORY...
		JNC	MAIN	;ALL DONE?

		JMP	CLRLUP	;KEEP CLEARING

