Easy 6502 example codes

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Easy 6502 example codes

Post by MrSterlingBS »

Hello folks,

I'm new to the forum and in the process of learning a little bit more about the 6502. Nick Morgan's page 6502 https://skilldrick.github.io/easy6502/ was a good place to start. I tried to port some programs to the VIC 20 from the http://www.6502asm.com page. Unfortunately I don't get it 100% to work. Who can help here? I have used the same size 32x32 chars from the example. My first code is the software sprite engine. It works fine. But the second code, the Sierpinsky has an error. Who can help?

My coding area is from the https://www.chibiakumas.com/6502/ beginners webpage. I often use the VIC-ROM method to have a faster assembly time.

Code: Select all

; software sprites works fine
; by PJP

* = $A000
		dw ProgramStart
		dw ProgramStart
		db $41,$30,$C3,$C2,$CD	;ROM Header
		
ProgramStart:
	sei							; Stop interrupt
ScreenInit:
	ldx #16						;We're going to copy 16 registers 
ScreenInitAgain:
	dex
	lda VicScreenSettings,x		;Get A parameter
	sta $9000,x					;Store to the video registers at $9000
	txa
	bne ScreenInitAgain
	
	ldx #$00
	lda #160
InitChar:						; Init Screen, like in 6502asm.com 
	sta $1C00,x					; 32x32 characters, space
	sta $1C00+256,x				; color code 9400 is like char in 6502asm.com
	sta $1C00+512,x
	sta $1C00+768,x
	inx
	bne InitChar
	
looper:	
	ldx $90
	inx
	stx $90

	lda #11          ; *** NUMBER OF SPRITES
	sta $3
	lda #0
	sta $4

multiple:
	lda $90
	clc
	adc $4
	tax

	lda sinus,x
	ldy cosinus,x
	asl
	tax
	lda ypos,x
	sta $00
	inx
	lda ypos,x
	sta $01
	ldx #0
	lda #6    ; **** HEIGHT OF EACH SPRITE
	sta $2
draw:
	jsr drawimage
	jsr drawimage
	jsr drawimage
	jsr drawimage
	jsr drawimage
	jsr drawimage
	
;	lda image,x
;	sta ($0),y
;	inx
;	iny
;	lda image,x
;	sta ($0),y
;	inx
;	iny
;	lda image,x
;	sta ($0),y
;	inx
;	iny
;	lda image,x
;	sta ($0),y
;	inx
;	iny
;	lda image,x
;	sta ($0),y
;	inx
;	iny
;	lda image,x
;	sta ($0),y
	dex
	dey
	
	tya
	clc
	adc #27
	tay
	inx
	dec $2
	bne draw

	lda $4 
	clc
	adc #22        ; *** DISTANCE BETWEEN SPRITES (FROM TABLE)
	sta $4
	
	dec $3
	bne multiple
	
		
	jmp looper
	
drawimage:
	lda image,x
	sta ($0),y
	inx
	iny
	rts
	
PauseXY:
	dex
	bne PauseXY
	dey 
	bne PauseXY
	rts

; SINUS (AND COSINUS) 

sinus:
 db $0e, $0e, $0e, $0f, $0f, $0f, $10, $10, $10, $11
 db $11, $11, $12, $12, $12, $13, $13, $13, $14, $14
 db $14, $14, $15, $15, $15, $16, $16, $16, $16, $17
 db $17, $17, $17, $18, $18, $18, $18, $19, $19, $19
 db $19, $19, $1a, $1a, $1a, $1a, $1a, $1a, $1a, $1b
 db $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b
 db $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b
cosinus:
 db $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b
 db $1a, $1a, $1a, $1a, $1a, $1a, $19, $19, $19, $19
 db $19, $18, $18, $18, $18, $18, $17, $17, $17, $17
 db $16, $16, $16, $15, $15, $15, $15, $14, $14, $14
 db $13, $13, $13, $12, $12, $12, $11, $11, $11, $10
 db $10, $10, $0f, $0f, $0f, $0e, $0e, $0e, $0d, $0d
 db $0d, $0c, $0c, $0c, $0b, $0b, $0b, $0a, $0a, $0a
 db $09, $09, $09, $08, $08, $08, $07, $07, $07, $06
 db $06, $06, $06, $05, $05, $05, $04, $04, $04, $04
 db $03, $03, $03, $03, $03, $02, $02, $02, $02, $02
 db $01, $01, $01, $01, $01, $01, $00, $00, $00, $00
 db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
 db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
 db $00, $00, $00, $00, $00, $00, $00, $01, $01, $01
 db $01, $01, $01, $01, $02, $02, $02, $02, $02, $03
 db $03, $03, $03, $04, $04, $04, $04, $05, $05, $05
 db $05, $06, $06, $06, $07, $07, $07, $07, $08, $08
 db $08, $09, $09, $09, $0a, $0a, $0a, $0b, $0b, $0b
 db $0c, $0c, $0c, $0d, $0d

 db $0e, $0e, $0e, $0f, $0f, $0f, $10, $10, $10, $11
 db $11, $11, $12, $12, $12, $13, $13, $13, $14, $14
 db $14, $14, $15, $15, $15, $16, $16, $16, $16, $17
 db $17, $17, $17, $18, $18, $18, $18, $19, $19, $19
 db $19, $19, $1a, $1a, $1a, $1a, $1a, $1a, $1a, $1b
 db $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b
 db $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b
 db $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b, $1b
 db $1a, $1a, $1a, $1a, $1a, $1a, $19, $19, $19, $19

; 5x5 BYTES

image:
 db $0,$0,$0,$0,$0,$0
 db $0,$2,$3,$2,$4,$0
 db $0,$2,$1,$2,$4,$0
 db $0,$2,$2,$2,$4,$0
 db $0,$2,$2,$2,$4,$0
 db $0,$0,$0,$0,$0,$0


; YPOS LOOKUP TABLE

ypos:
 db $00,$94,$20,$94,$40,$94,$60,$94
 db $80,$94,$a0,$94,$c0,$94,$e0,$94
 db $00,$95,$20,$95,$40,$95,$60,$95
 db $80,$95,$a0,$95,$c0,$95,$e0,$95
 db $00,$96,$20,$96,$40,$96,$60,$96
 db $80,$96,$a0,$96,$c0,$96,$e0,$96
 db $00,$97,$20,$97,$40,$97,$60,$97
 db $80,$97,$a0,$97,$c0,$97,$e0,$97

; 32x32 Screen
VicScreenSettings:
	db $02		;$9000 - 36864 - horizontal centering
	db $11		;$9001 - 36865 - vertical centering
	db $20		;$9002 - 36866 - set # of columns / 
					;Bit7 = screen base bit ($16 for screen at $1000)
	db $40		;$9003 - 36867 - set # of rows
	db $7A		;$9004 - TV raster beam line
	db $F0		;$9005 - 36869 - bits 0-3 start of character memory /  
					;bits 4-7 is rest of video address 
					;$(CF for screen at $1000)
	db $57		;$9006 - horizontal position of light pen
	db $EA		;$9007 - vertical position of light pen
	db $FF		;$9008 - Digitized value of paddle X
	db $FF		;$9009 - Digitized value of paddle Y
	db $00		;$900A - Frequency for oscillator 1 (low)
	db $00		;$900B - Frequency for oscillator 2 (medium)
	db $00		;$900C - Frequency for oscillator 3 (high)
	db $00		;$900D - Frequency of noise source
	db $00		;$900E - bit 0-3 sets volume of all sound / 
					;bits 4-7 are auxiliary color information
	db $08	 	;$900F - Screen and border color register
	
*********************************************************************************************************
The sub routine PlotCHar is not used in this example.

; Sierpinski-like triangles
; The original screen can be found here http://www.6502asm.com/
; Submitted by Anonymous

;  PAL-B systems:
;           Chip      Crystal  Dot      Processor Cycles/ Lines/
;   Host    ID        freq/Hz  clock/Hz clock/Hz  line    frame
;   ------  --------  -------- -------- --------- ------- ------ ----------
;   VIC-20  6561-101   4433618  4433618   1108405      71    312

zp 	equ $FB
zp1 equ $FC

* = $A000
		dw ProgramStart
		dw ProgramStart
		db $41,$30,$C3,$C2,$CD	;ROM Header
		
ProgramStart:
	sei							; Stop interrupt
ScreenInit:
	ldx #16						;We're going to copy 16 registers 
ScreenInitAgain:
	dex
	lda VicScreenSettings,x		;Get A parameter
	sta $9000,x					;Store to the video registers at $9000
	txa
	bne ScreenInitAgain
	
	ldx #$00
	lda #160
InitChar:						; Init Screen, like in 6502asm.com 
	sta $1C00,x					; 32x32 characters, space
	sta $1C00+256,x				; color code 9400 is like char in 6502asm.com
	sta $1C00+512,x
	sta $1C00+768,x
	inx
	bne InitChar
	

Sierstart:
	ldx #0
	lda #$00		; low-byte
	sta $0
	lda #$94		; high-byte
	sta $1
write:
	jsr point
	sta ($0,x)
	inc $0
	beq highbyte
	jmp write

highbyte:
	inc $1
;	ldy $1
;	cpy #$06
	bne write
	rts

point:
	lda $0
	and #$1f
	sta $2 ;x
	lda $0
	lsr
	lsr
	lsr
	lsr
	lsr
	sta $3
	lda $1
	sec
	sbc #2
	asl
	asl
	asl
	ora $3
	and $2
	beq okay
	lda #2		; Background Color
	rts
okay:
	lda #7		; Foreground Color
	rts
	
	
Start:						; Set a char with color ~51 µs ???
	lda #18			; 2 cyl		Char
	ldy #3				; 2 cyl		x-Position !!!
	ldx #5				; 2 cyl		y-Position !!!
PlotChar:
	pha					; 3 cyl
	lda screenLo,x		; 4 cyl
	sta zp				; 3 cyl
	lda screenHi,x		; 4 cyl
	sta zp1				; 3 cyl
	pla					; 4 cyl
	sta (zp),y			; 6 cyl
	
	pha					; 3 cyl
	lda zp1				; 3 cyl
	clc					; 2 cyl
	adc #$78			; 2 cyl				add Offset to Color Ram ($9600)
	sta zp1				; 3 cyl
	lda #3				; 2 cyl					Color
	sta (zp),y			; 6 cyl					Set Tile Color
	pla					; 4 cyl
	;rts					; =====
						; 57 cyl 
			
WaitChar:
	lda $C5
    cmp #$11
    bne WaitChar
	rts
	
PauseXY:
	dex
	bne PauseXY
	dey 
	bne PauseXY
	rts

; 32x32 Screen
VicScreenSettings:
	db $02		;$9000 - 36864 - horizontal centering
	db $11		;$9001 - 36865 - vertical centering
	db $20		;$9002 - 36866 - set # of columns / 
					;Bit7 = screen base bit ($16 for screen at $1000)
	db $40		;$9003 - 36867 - set # of rows
	db $7A		;$9004 - TV raster beam line
	db $F0		;$9005 - 36869 - bits 0-3 start of character memory /  
					;bits 4-7 is rest of video address 
					;$(CF for screen at $1000)
	db $57		;$9006 - horizontal position of light pen
	db $EA		;$9007 - vertical position of light pen
	db $FF		;$9008 - Digitized value of paddle X
	db $FF		;$9009 - Digitized value of paddle Y
	db $00		;$900A - Frequency for oscillator 1 (low)
	db $00		;$900B - Frequency for oscillator 2 (medium)
	db $00		;$900C - Frequency for oscillator 3 (high)
	db $00		;$900D - Frequency of noise source
	db $00		;$900E - bit 0-3 sets volume of all sound / 
					;bits 4-7 are auxiliary color information
	db $08	 	;$900F - Screen and border color register
	
screenLo:
		DB $00,$20,$40,$60,$80,$A0,$C0,$E0
		DB $00,$20,$40,$60,$80,$A0,$C0,$E0
		DB $00,$20,$40,$60,$80,$A0,$C0,$E0
		DB $00,$20,$40,$60,$80,$A0,$C0,$E0
screenHi:
		DB $1C,$1C,$1C,$1C,$1C,$1C,$1C,$1C
		DB $1D,$1D,$1D,$1D,$1D,$1D,$1D,$1D
		DB $1E,$1E,$1E,$1E,$1E,$1E,$1E,$1E
		DB $1F,$1F,$1F,$1F,$1F,$1f,$1F,$1F
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: Easy 6502 example codes

Post by MrSterlingBS »

Here is another code snipet from easy 6502

Code: Select all

* = $A000
		dw ProgramStart
		dw ProgramStart
		db $41,$30,$C3,$C2,$CD	;ROM Header
		
ProgramStart:
	sei							; Stop interrupt
ScreenInit:
	ldx #16						;We're going to copy 16 registers 
ScreenInitAgain:
	dex
	lda VicScreenSettings,x		;Get A parameter
	sta $9000,x					;Store to the video registers at $9000
	txa
	bne ScreenInitAgain
	
	ldx #$00
	lda #160
InitChar:						; Init Screen, like in 6502asm.com 
	sta $1C00,x					; 32x32 characters, space
	sta $1C00+256,x				; color code 9400 is like char in 6502asm.com
	sta $1C00+512,x
	sta $1C00+768,x
	inx
	bne InitChar
		
	ldx #0
looplogo:
	lda logo,x
	sta $9400,x
	lda logo+256,x
	sta $9400+256,x
	lda logo+512,x
	sta $9400+512,x
	lda logo+768,x
	sta $9400+768,x
	
	txa
	pha
	ldx #25
	ldy #25
	jsr PauseXY		;Wait a bit!	
	pla
	tax
	inx
	bne looplogo
				
Waiter:
	jmp Waiter
	
PauseXY:
	dex
	bne PauseXY
	dey 
	bne PauseXY
	rts

; 32x32 Screen
VicScreenSettings:
	db $02		;$9000 - 36864 - horizontal centering
	db $11		;$9001 - 36865 - vertical centering
	db $20		;$9002 - 36866 - set # of columns / 
					;Bit7 = screen base bit ($16 for screen at $1000)
	db $40		;$9003 - 36867 - set # of rows
	db $7A		;$9004 - TV raster beam line
	db $F0		;$9005 - 36869 - bits 0-3 start of character memory /  
					;bits 4-7 is rest of video address 
					;$(CF for screen at $1000)
	db $57		;$9006 - horizontal position of light pen
	db $EA		;$9007 - vertical position of light pen
	db $FF		;$9008 - Digitized value of paddle X
	db $FF		;$9009 - Digitized value of paddle Y
	db $00		;$900A - Frequency for oscillator 1 (low)
	db $00		;$900B - Frequency for oscillator 2 (medium)
	db $00		;$900C - Frequency for oscillator 3 (high)
	db $00		;$900D - Frequency of noise source
	db $00		;$900E - bit 0-3 sets volume of all sound / 
					;bits 4-7 are auxiliary color information
	db $08	 	;$900F - Screen and border color register

logo:
 db 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
 db 1,1,1,1,1,1,1,1,1,1,1,1,1,6,6,6,6,6,6,6,1,1,1,1,1,1,1,1,1,1,1,1
 db 1,1,1,1,1,1,1,1,1,1,6,6,6,6,6,6,6,6,6,6,6,1,1,1,1,1,1,1,1,1,1,1
 db 1,1,1,1,1,1,1,1,6,6,6,6,6,6,6,6,6,6,6,6,6,1,1,1,1,1,1,1,1,1,1,1
 db 1,1,1,1,1,1,1,6,6,6,6,6,6,6,6,6,6,6,6,6,6,1,1,1,1,1,1,1,1,1,1,1
 db 1,1,1,1,1,1,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,1,1,1,1,1,1,1,1,1,1,1
 db 1,1,1,1,1,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,1,1,1,1,1,1,1,1,1,1,1
 db 1,1,1,1,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,1,1,1,1,1,1,1,1,1,1,1
 db 1,1,1,6,6,6,6,6,6,6,6,6,6,6,6,1,1,1,1,6,6,1,1,1,1,1,1,1,1,1,1,1
 db 1,1,1,6,6,6,6,6,6,6,6,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