Getting started with CC65

You need an actual VIC.

Moderator: Moderators

rhurst
Omega Star Commander
Posts: 1371
Joined: Thu Jan 31, 2008 2:12 pm
Website: https://robert.hurst-ri.us
Location: Providence, RI
Occupation: Tech & Innovation

Post by rhurst »

Yet another useful cc65 feature worth pointing out is its integration with the VICE monitor. That is, its linker can generate labels suitable for loading into the monitor for practical debugging.

Code: Select all

ld65.exe -C BASIC-8K.cfg -Ln hello.sym -m hello.map -o hello.prg BASIC-8K.o VIC-SSS.o
The -Ln option outputs to the label file, hello.sym. From within the VICE monitor (press ALT-M):

Code: Select all

help
help ll
ll "hello.sym"

(C:$238a) (C:$23f1) m .SPRITE .SPRITEY
>C:2361  05 40 80 00  18 b0 1e 60   .@.....`
>C:2369  1f 02 0c 01  08 10 10      .......

(C:$21fd) d .SSSIMAGE
.C:282e   .SSSIMAGE:
.C:282e   48         PHA
.C:282f   29 1F      AND #$1F
.C:2831   0A         ASL A
.C:2832   0A         ASL A
.C:2833   0A         ASL A
.C:2834   AA         TAX
.C:2835   A0 1C      LDY #$1C
.C:2837   68         PLA
.C:2838   C9 80      CMP #$80
.C:283a   90 02      BCC $283E
.C:283c   A0 80      LDY #$80
.C:283e   8C 0E 03   STY $030E
.C:2841   29 60      AND #$60
.C:2843   4A         LSR A
.C:2844   4A         LSR A
.C:2845   4A         LSR A
.C:2846   4A         LSR A
.C:2847   4A         LSR A
.C:2848   0D 0E 03   ORA $030E
.C:284b   A8         TAY
.C:284c   60         RTS
.C:284d   .SSSREAD:
.C:284d   A9 A0      LDA #$A0
.C:284f   E0 10      CPX #$10
.C:2851   B0 01      BCS $2854
.C:2853   60         RTS
.C:2854   EC 9E 02   CPX $029E
Setting break and watch points:

Code: Select all

(C:$2370) w .SPRITE
WATCH: 1 C:$2361 load store   enabled

(C:$2370) break .SSSUPDATE
BREAK: 2 C:$2553   enabled

(C:$2370) x
Type RUN on VIC and the monitor will break when it hits any of those addresses, for example:

Code: Select all

#1 (Watch-load) .C:229b   20 53 25   JSR .SSSUPDATE

** Monitor 083 048
Any technology distinguishable from magic is insufficiently advanced.
https://robert.hurst-ri.us/rob/retrocomputing
User avatar
amramsey
Vic 20 Hobbyist
Posts: 117
Joined: Sat Apr 14, 2007 9:38 pm

Post by amramsey »

Nice! I'm off to China for a week for business so I'm hoping to get some code done while I'm away from the family and kids. :D I'll have to try this out.
User avatar
hawk
Vic 20 Afficionado
Posts: 342
Joined: Mon Jun 20, 2005 7:32 pm

Post by hawk »

Bump! :lol:

I was going to start a new thread for this, but I decided to bump this thread instead, since it seems fairly relevent. (If the moderator disagrees them by all means move it to a new thread.)

I've started trying to capture some of the VIC-20 specifics of coding in C using CC65 in the Wiki.

So I guess this is an invitation to those others out there that are using CC65 to contribute...even if just to say what you'd like to see in it.

I hadn't actually read this thread before, which is a shame, as I've been playing with CC65 in a not too serious way for a few years now.

I'd have to agree with carlsson about the handling of the screen routines for the VIC-20 version. I think that they just ported the C64 routines without modifying them to the capabilities of the VIC. I've been trying to use them recently and had some terrible teething issues. I'm currently looking at how I can change them to meet my needs. Maybe this is something that we can get patched back into the VIC-20 distribution package.

Basically, the routines don't handle the screen being re-sized, so the standard library I/O routines get messed up. (Something that the kernal routines never did correctly either!)

The other gotchya that I came across was the translation of strings constants during compilation...from the ASCII representation to the native target. It makes redefining the character set a little more interesting.

Something else that could get added to the standard CC65 VIC distribution is the Linker Config file and crt0.s file for an autostart ROM file. Maybe we could come up with a acceptable generic solution.

Anyway, I've started to add info to the Wiki. If you see any issues with what I've put, please let me know (or fix...as required). I'm no expert here, but it's all relative...to someone who's just starting out with CC65 some of it may be useful.

For me playing with CC65 on the VIC keeps my embedded programming skills up-to-date when work has me managing rather than doing.
rhurst
Omega Star Commander
Posts: 1371
Joined: Thu Jan 31, 2008 2:12 pm
Website: https://robert.hurst-ri.us
Location: Providence, RI
Occupation: Tech & Innovation

Post by rhurst »

I've done a ROM autostart in assembler for cc65 a few times. Maybe this can help jumpstart your efforts with a C library:

Code: Select all

;*********************************************************************
; YOUR PROJECT INFORMATION GOES HERE
; and then save file as: yourgame.s
;
        .fileopt author,     "Your Name"
        .fileopt comment,    "Your Game Title"
        .fileopt compiler,   "VIC 20 ASSEMBLER"

;*********************************************************************
; VIC 20 ROM autostart game cartridge
; written by Robert Hurst <robert@hurst-ri.us>
; updated version: 01-Mar-2009
;
; ca65.exe --cpu 6502 -t vic20 --listing --include-dir . yourgame.s
;
; - for smaller 4K ROM games:
; ld65.exe -C GAME-4K.cfg -o yourgame.a0 yourgame.0
;
; - for larger 8K ROM games:
; ld65.exe -C GAME-8K.cfg -o yourgame.a0 yourgame.0
;
;*********************************************************************
; Commodore ROM cartridge boot sequence
; load address of startup & restore key:
;
		.segment "BOOT"

		.word	MAIN
		.word	NMI
		; power-up signature
A0CBM:	.byte	$41, $30, $C3, $C2, $CD

;*********************************************************************
; Starting entry point for this program
;
		.segment "STARTUP"

MAIN:
		; initialize VIC Kernal
		JSR $FD8D		; ramtas	Initialise System Constants (memory pointers)
		JSR $FD52		; restor	Restore Kernal vectors (at 0314)
		JSR $FDF9		; ioinit	Initialise I/O (timers are enabled)
		JSR $E518		; cint1		Initialize I/O (VIC reset, must follow ramtas)
		;LDA #$7F
		;STA $911E		; disable NMIs (Restore key)
  		;
		; initialize VIC BASIC
		JSR $E45B		; initv		Initialize vectors
		JSR $E3A4		; initcz	Initialize BASIC RAM
		JSR $E404		; initms	Output power-up message
		;
		LDA $EDE4		; NTSC=$05, PAL=$0C
		CMP #$05
		BEQ NTSC
		CMP #$0C
		BEQ PAL
		JMP READY		; not a VIC?
		;
		; NTSC setup
NTSC:	LDX #<@NTSC		; load the timer low-byte latches
		STX $9126
		LDX #>@NTSC
		LDA #$75		; raster line 234/235
		BNE IRQSYNC
@NTSC = $4243			; (261 * 65 - 2)
		;
		; PAL setup
PAL:	LDX #<@PAL		; load the timer low-byte latches
		STX $9126
		LDX #>@PAL
		LDA #$82		; raster line 260/261
		BNE IRQSYNC
@PAL = $5686			; (312 * 71 - 2)
		;
IRQSYNC:
		CMP $9004
		BNE IRQSYNC
		STX $9125		; load T1 latch high, and transfer both bytes to T1 counter
		JMP RESTART

;*********************************************************************
; enter VIC BASIC mode
;
READY:
		JSR $E518		; cint1		Initialize I/O (VIC reset, must follow ramtas)
		JSR $E45B		; initv		Initialize vectors
		JSR $E3A4		; initcz	Initialize BASIC RAM
		LDA #$01
		STA $C7		; character reverse flag
		LDA #$03		; set color to CYAN
		STA $0286		; just to have some fun
		JSR $E404		; initms	Output power-up message
		LDA #$06		; set color back to BLUE
		STA $0286		; because we're used to it
		JMP $E467		; bassft	BASIC Warm Start

;*********************************************************************
; RESTORE key was pressed
NMI:
		;JMP $FEC7		; continue
		CLD
		PLA				; restore Y register
		TAY
		PLA				; restore X register
		TAX
		PLA				; restore Accumulator
		;RTI			; continue
		PLA
		PLA
		LDA #$FF		; acknowledge and clear
		STA $9122		; interrupts
		LDY $9111
		JMP RESTART

;*********************************************************************
; Your program code starts here
;
		.segment "CODE"

RESTART:
		.include "yourgame.s"		; remove/replace this

;*********************************************************************
; Your program data defined here is appended to CODE segment (above)
;
		.segment "RODATA"
... and then the linker configuration:

Code: Select all

# VIC 20 8K ROM autostart game cartridge
MEMORY {
    ZP:   start = $0000, size = $0100, type = rw;
    RAM0: start = $0400, size = $0C00, type = rw;
    RAM:  start = $1000, size = $1000, type = rw;
    RAM1: start = $2000, size = $2000, type = rw;
    RAM2: start = $4000, size = $2000, type = rw;
    RAM3: start = $6000, size = $2000, type = rw;
    RAM4: start = $8000, size = $2000, type = rw;
    ROM1: start = $A000, size = $1000, type = ro, fill = yes;
    ROM2: start = $B000, size = $1000, type = ro, fill = yes;
}
SEGMENTS {
    BOOT:     load = ROM1, type = ro, define = yes, optional = no;
    STARTUP:  load = ROM1, type = ro, define = yes, optional = no;
    CODE:     load = ROM1, type = ro, define = yes, optional = no;
    RODATA:   load = ROM2, type = ro, define = yes, optional = no;
}
FEATURES {
	STARTADDRESS:	default = $A000;
}
Any technology distinguishable from magic is insufficiently advanced.
https://robert.hurst-ri.us/rob/retrocomputing
User avatar
hawk
Vic 20 Afficionado
Posts: 342
Joined: Mon Jun 20, 2005 7:32 pm

Post by hawk »

rhurst wrote:I've done a ROM autostart in assembler for cc65 a few times. Maybe this can help jumpstart your efforts with a C library:
This is great Robert. It will require some modification to allow it to be used with C code, but it includes some really good features.

I'm looking at how the features could be included/excluded at compile time via .IFDEF or .IFNDEF.

I'm not sure whether to include or exclude by default and then allow code to be included via a definition.

Code: Select all

.IFDEF INCLUDE_VIDEO_DETECT
<code>
.ENDIF
or

Code: Select all

.IFDEF VIDEO_EITHER
.DEFINE VIDEO_PAL
.DEFINE VIDEO_NTSC
<video detect>
.ENDIF

.IFDEF VIDEO_NTSC
<ntsc code>
.ENDIF

.IFDEF VIDEO_PAL
<pal code>
.ENDIF
or

Code: Select all

.IF VIDEO_MODE=EITHER
<either code>
.ENDIF

.IF VIDEO_MODE=NTSC OR VIDEO_MODE=EITHER
<ntsc code>
.ENDIF

.IF VIDEO_MODE=PAL OR VIDEO_MODE=EITHER
<pal code>
.ENDIF
Other possibilities for startup code include RAM expansion detect. Not so important for a loadable program, but for a cart that expects working RAM to be present...

I've started work in the Wiki in this location.

http://sleepingelephant.com/denial/wiki ... using_CC65

Maybe the section of the Wiki I created called "C using CC65" should be more generically labelled "Programming the VIC-20 using CC65" and inlcude the assembler specific stuff.

Are the possibilities for the various configurations of the VIC-20 too difficult to capture in a set of generic files?

Should we just document the "hows", and not the "whats"?
rhurst
Omega Star Commander
Posts: 1371
Joined: Thu Jan 31, 2008 2:12 pm
Website: https://robert.hurst-ri.us
Location: Providence, RI
Occupation: Tech & Innovation

Post by rhurst »

I've started work in the Wiki in this location.
Very nice with great examples.
Should we just document the "hows", and not the "whats"?
I like the format already, the hows with a brief example. It looks comprehensive enough to me to get jump-started. What do you feel is missing?

I think the page and its title warrants staying put as-is. Writing C on the 6502 is different and specific enough. And the cc65 project can always be included in another assembler page.
Any technology distinguishable from magic is insufficiently advanced.
https://robert.hurst-ri.us/rob/retrocomputing
Post Reply