Programming NES games in C article
Moderator: Moderators
Shiru, do you have the source code anywhere for the runtime.lib code?
Namely, I am interested in seeing what popa and popax do.
I noticed when looking at the code for some of the neslib.s (I was looking at oam_spr) routines that there are lots of calls to "jsr popa" because that call takes 4 arguments.
Typically jsr can be a bit heavy (6 cycles for JSR plus another 6 for its RTS) and I wanted to see if any of the routines could be optimized.
Al
Namely, I am interested in seeing what popa and popax do.
I noticed when looking at the code for some of the neslib.s (I was looking at oam_spr) routines that there are lots of calls to "jsr popa" because that call takes 4 arguments.
Typically jsr can be a bit heavy (6 cycles for JSR plus another 6 for its RTS) and I wanted to see if any of the routines could be optimized.
Al
It is in the CC65 sources, you can download them, there are many functions. I quote these two here:
Code: Select all
;
; Ullrich von Bassewitz, 25.10.2000
;
; CC65 runtime: Pop a from stack
;
.export popa
.importzp sp
.macpack cpu
.proc popa
.if (.cpu .bitand ::CPU_ISET_65SC02)
lda (sp)
.else
ldy #0 ; (2)
lda (sp),y ; (7) Read byte
.endif
inc sp ; (12)
beq @L1 ; (14)
rts ; (20)
@L1: inc sp+1
rts
.endprocCode: Select all
;
; Ullrich von Bassewitz, 25.10.2000
;
; CC65 runtime: Increment the stackpointer by 2. For performance reasons,
; this modules does also contain the popax function.
.export popax, incsp2
.importzp sp
.macpack cpu
; Pop a/x from stack. This function will run directly into incsp2
.proc popax
ldy #1
lda (sp),y ; get hi byte
tax ; into x
.if (.cpu .bitand ::CPU_ISET_65SC02)
lda (sp) ; get lo byte
.else
dey
lda (sp),y ; get lo byte
.endif
.endproc
.proc incsp2
inc sp ; 5
beq @L1 ; 2
inc sp ; 5
beq @L2 ; 2
rts
@L1: inc sp ; 5
@L2: inc sp+1 ; 5
rts
.endprocThanks for the great article, however I've got some problems with linking:
If I'm linking without runtime.lib I get:
"ld65: Error: nes.cfg(82): Attribute expected"
82 is the line with "__STACKSIZE__ = $0500; # 5 pages stack"
I've never developed on the NES platform (or used the cc65 binutils) before, also I'm trying to build the examples on Ubuntu.
Code: Select all
$ ld65 --version
ld65 V2.13.9 - (C) Copyright 1998-2009, Ullrich von Bassewitz
$ ld65 -o main.nes crt0.o example1.o runtime.lib -C nes.cfg
ld65: Error: Wrong data version in `runtime.lib
"ld65: Error: nes.cfg(82): Attribute expected"
82 is the line with "__STACKSIZE__ = $0500; # 5 pages stack"
I've never developed on the NES platform (or used the cc65 binutils) before, also I'm trying to build the examples on Ubuntu.
Version 2.13.3 is uploaded to the FTP site. Here's the changelog summary from 2.13.2 to 2.13.3 from the 2.13.3 release announcement.
cc65:
cc65:
- Fixed some macro issues.
- Static const local data goes into RODATA, not DATA.
- Fix problem when converting function to void pointer.
- Fix register info for several runtime functions. Result was invalid code.
- Fix an error in the runtime division routine.
- Fix internal error caused by error recovery (or lack of).
- Fix invalid handling of signed int types in some cases.
- Fix and improve the code for compares.
- Disallow __asm__ on global level.
- Fix problem with access to structs returned by functions.
- Fix an internal error in the code generator.
- Fix error handling in .LEFT.
- Fix problem with .REPEAT.
- Fix problem with alignments >= 256 bytes.
- Fix some macro issues.
- Fixed problems with 80 column mode (C128 library).
- Remove final jump to RESTOR for all CBM platforms (cc65 libraries).
- Merge back Olivers C interrupt handling code (cc65 libraries).
- The LINE function mistakenly enabled the BASIC ROM (TGI driver for C64).
- Fix some issues with conio scrolling (C128 library).
- Add missing export for CBM510 platform (cbm510 library).
- Merge back POSIX directory routines for the Atari (atari library).
- Fix smbx and rmbx instructions.
- Fixed/improved several error messages (all tools).
- Several documentation changes.
- Several documentation fixes and improvements.
I got it from a repo at http://www.trikaliotis.net/debian... but I guess building it from source would be a better idea.
- rainwarrior
- Posts: 8062
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Programming NES games in C article
Shiru, I was wondering what you ended up putting in your runtime.lib.
Is it just runtime/ plus common/zerobss.s ?
Is it just runtime/ plus common/zerobss.s ?
Re: Programming NES games in C article
It is standart cc65 runtime, recomplied by me. Nothing is added there, as far as I remember - none of my code for sure. I don't really remember why I did recompile it. I think I used nes.lib initially that did contain the runtime, but a ton of unneeded stuff as well, so I replaced it with the runtime only, that does not have anything platform-specific (600 to 100K in size).
- rainwarrior
- Posts: 8062
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Programming NES games in C article
I've been looking over it, compiling my own version. I had been using yours, but I wanted to get a better idea of what's in there.
There is basically nothing useful in the nes/ lib source, unless you want to make a text only program maybe, and I think it's a good idea to replace crt0. Luckily all the runtime/ stuff is modularized, so stuff you don't used is not linked in anyway.
Apparently there's a few things I need from common/ so far zerobss.s and copydata.s were needed. Probably I will need to add more as I go along and I get link errors, but I think most of common/ I want to leave out. I'm keeping a list as I go. I'll release source and information once I've finished my current project, so anyone who's interested might learn from it.
There is basically nothing useful in the nes/ lib source, unless you want to make a text only program maybe, and I think it's a good idea to replace crt0. Luckily all the runtime/ stuff is modularized, so stuff you don't used is not linked in anyway.
Apparently there's a few things I need from common/ so far zerobss.s and copydata.s were needed. Probably I will need to add more as I go along and I get link errors, but I think most of common/ I want to leave out. I'm keeping a list as I go. I'll release source and information once I've finished my current project, so anyone who's interested might learn from it.
Re: Programming NES games in C article
Here are the runtime sources that I used to compile the runtime.lib.
- rainwarrior
- Posts: 8062
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Programming NES games in C article
Ah, that looks exactly like runtime/ + common/zerobss.s like I suspected. That answers my earlier question.