cpu 6502
nam Plot
page 128,128
	
* = $0900  ; assemble at $900
code
	
.top	ldy #$27
	lda #$57
	ldx #$18
	jsr .plot

	ldy #$00
	ldx #$00
	lda #$57
	jsr .plot

	ldy #$00
	ldx #$27
	lda #$57
	jsr .plot

	ldy #$00
	ldx #$18
	lda #$57
	jsr .plot
		
	rts

	;; ===================================================
	;; .plot : plot a character somewhere in screen memory
	;;         given the column (col), row, and ascii (chr)
	;;
	;;  Im using the formula:
	;;    offset = ( ( row - cy ) * 40 ) + ( col - cr ))
	;; 
.plot	sta chr
	sta plota
	
	stx col
	stx plotc
	
	sty row
	
	;; col=x  row=y   chr=a

	
	;; ========================================
	;; multiply the row by 40
.by40	lda row
	ldx base
	ldy subs, x
	inx
	stx base
	;; 
	sec			;set the carry flag for subtraction
	sty thesub
	sbc thesub
	;; 
	sta mltplcnd
	jsr .muladd
	ldx overflow
	cpx #$00
	bne .by40		;if overflow... then do it again
	
.okay	clc
	clv
	lda result
	adc plotc		;add the column number to it
	bcs .by40		; if it overflows, try it again
	bvs .by40		;in the next chunk
	
	sta offset		; store that as the offset
	
	ldx base		; which screen chunk?
	dex
	ldy subscol, x		; find out what to subtract
	sty thesub		; store it in a specific place

	sec			; set the carry flag
	lda offset		;
	sbc thesub		; subtract the chunk offset
	sta offset		; store it in the offset spot
	
	lda plota		;a = character
	ldy base		;y = the base vector
	ldx offset		;x = the offset

	cpy #$01		;base vector 1 ($0400)
	bne .ok1
	sta $0400,x
	jmp .rtn
.ok1	cpy #$02		;base vector 2 ($0500)
	bne .ok2
	sta $0500,x
	jmp .rtn
.ok2	cpy #$03		;base vector 3 ($0600)
	bne .ok3
	sta $0600,x
	jmp .rtn
.ok3	sta $0700,x		;base vector 4 ($0700)
	jmp .rtn

.rtn	ldx #$00
	stx base
	rts

	;; =====================================
	;; 
	;;     .muladd
	;; 
	;; MULTIPLY A BY 40
	;; 
	;; mltplcnd = the number to multiply
	;; result = answer
mltplcnd db $00
overflow db $00
result   db $00
	
.muladd	ldx #$00		; clear the homemade overflow byte
	stx overflow		;
	;; 
	clc			; clear the carry flag
	clv			; clear the overflow flag
	;; 
	lda mltplcnd		; load the number to mult from mem

	;; multiply by 32 decimal
	ldx #$05
.mltlp1 asl			; multiply by 2

	bcs .cry		; jump on carry
	dex			; decrement index

	cpx #$00		; compare the index against #$00
	bne .mltlp1		; if not equal, repeat the loop
	
	
	;; add the number 8 times to it
	ldx #$08		; set index for 8
.multlp clc
	clv
	adc mltplcnd
	bvs .cry		; jump out on overflow
	dex
	cpx #$00
	bne .multlp
	sta result
	rts			; then return
	
.cry	ldy #$01			; set the homemade oveflow byte
	sty overflow
	sta result
	rts
	;; DATA
	row	db $00
	col	db $00
	chr	db $00
	
	plotr	db $00			;row
	plotc	db $00			;column
	plota	db $00			;character
	
	base	db $00
	offset  db $00
	
	subs	db $00, $07, $0D, $14
	subscol db $00, $12, $22, $0A
	thesub  db $00
	
	count 	db $00

	index 	db $27

	test	db $0400
	nop
	nop
	nop
	
code