The Code Box

Scrolling

1×2 Variable Speed Scroller
TEXT1 = $4000 ;wherever you load your text into memory..
SCROLLER LDA XPOS1
         SEC
         SBC SPEED
         STA XPOS1
         BPL EXITSC1
         AND #$07
         STA XPOS1
         LDX #$00
MOVE1    LDA SCREEN1+1,X
         STA SCREEN1,X
         LDA SCREEN1+41,X
         STA SCREEN1+40,X
         INX
         CPX #$27
         BNE MOVE1
         JSR GETCHAR
         LDA TEXT1
TP1      = *-2
         BEQ RESTART1
         INC TP1
         BNE ST1
         INC TP1+1
         BNE ST1
RESTART1 LDA #<TEXT1
         LDX #>TEXT1
         STA TP1
         STX TP1+1
         LDA #$20
ST1      STA SCREEN1+39
         CLC
         ADC #$40
         STA SCREEN1+79
EXITSC1  RTS

GETCHAR  LDA SCREEN1+39
         CMP #">"
         BEQ SPEEDUP
         CMP #"<"
         BEQ SLOWDOWN
         RTS

SPEEDUP  INC SPEED
         RTS

SLOWDOWN DEC SPEED
         RTS

SPEED    .BYTE $02
XPOS1    .BYTE $00

Miscellaneous

Copy Chunk of Memory
LDA #$00
	STA $FC
	STA $FE
	LDA #$80 ;copies from $8000
	STA $FF
	LDA #$20 ;copies to $2000
	STA $FD
LOOP 	LDY #0
MOV256	LDA ($FE),Y
	STA ($FC),Y
	INY
	BNE MOV256
	INC $FD
	INC $FF
	LDA $FF
	CMP #$84 ;ending address goes here.. right now it stops at $8400
	BNE LOOP
	RTS
BASIC Line Header – execute assembly code by typing “RUN”
*=$0801

BYTE $0C,$08,$0A,$00,$9E,$20,$31,$36,$33,$38,$34,$00,$00
BYTE $00,$00 ;SYS 16384 (31,36,33,38,34)
Draw Boxes – Menus, RPG Word Boxes, etc
;set the box size

        lda #8 	;y pos
	sta rectangle+6
	lda #2 	;x pos
	sta rectangle+7
	lda #22
	sta rectangle+8 ;width 
	lda #6
	sta rectangle+9 ;height
	jsr drawbox
	rts



drawbox
	ldy rectangle+6
	lda #$00
	sta $fa
	lda #$04
	sta $fb

box0	cpy #0
	beq box1
	clc
	lda $fa
	adc #$28
	sta $fa
	bcs box2
box00	dey
	jmp box0
box2	inc $fb
	jmp box00
	
box1	ldy rectangle+7
	lda rectangle
	sta ($fa),y
	lda rectangle+5
	ldx rectangle+8
box1a	iny
	dex
	sta ($fa),y
	cpx #0
	bne box1a
	iny 
	lda rectangle+1
	sta ($fa),y
	sty recttemp
	
	ldx rectangle+9
box1d	cpx #0
	beq box1e
	clc
	lda $fa 
	adc #$28
	sta $fa
	bcs box1b
box1c lda rectangle+4
	ldy recttemp
	sta ($fa),y
	ldy rectangle+7
	sta ($fa),y
	dex
	jmp box1d
	
box1b inc $fb
	jmp box1c
	
box1e	clc
	lda $fa 
	adc #$28
	sta $fa
	bcs box1f
box1g 	lda rectangle+3
	ldy recttemp
	sta ($fa),y
	lda rectangle+2
	ldy rectangle+7
	sta ($fa),y
	ldy recttemp
	dey
	lda rectangle+5
box1h	sta ($fa),y
	dey
	cpy rectangle+7
	bne box1h
	rts
	
box1f inc $fb
	jmp box1g



rectangle	.byte 112,110,109,125,93,67
	.byte 0,0,0,0 ;y,x,length,height
recttemp .byte 0
;top left, top right, bottom left, bottom right, width piece, height piece
NTSC/PAL/SCPU/U64 Detection
cpudetect  	lda $d012
cpu1  	cmp $d012
    	beq cpu1
    	bmi cpudetect
	
	cmp #$37
	beq paldetected
		
	lda $d0bc
	and #%10000000 ;bit 7 is enabled on stock c64
	cmp #$00	   ;so 0 is SCPU
	beq scpudetected
	
	lda $d030
	cmp #$ff
	bne u64detected
	
	lda #$01		;NTSC detected
	sta cputype
	jmp cpux
	
u64detected
	lda #$02
	sta cputype
	lda #15
	sta $d031 ;set CPU to 48MHz
	jmp cpux
	
scpudetected lda #$03
	sta cputype
	sta $d07b 	;set 20mhz mode on SCPU
	jmp cpux
	
paldetected
	lda #$00
	sta cputype

cpux	rts

cputype	.byte $ff ;0 PAL, 1 NTSC, 2 Ultimate64, 3 SCPU