Is there a way to specify memory address for C programming?

Building your first 6502-based project? We'll help you get started here.
Post Reply
Aloha6502
Posts: 69
Joined: 17 Feb 2021

Is there a way to specify memory address for C programming?

Post by Aloha6502 »

Like the ORG (or *=) in assembly, is there anything similar for C, to specify that from this point on, start compiling opcodes at this address, until it is specified again later?

(I am of course experimenting with C for 65xx)
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Is there a way to specify memory address for C programmi

Post by BigEd »

If you’re using cc65 then it’s the linker which puts code at addresses - check your machine description or linker control script or command line.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Is there a way to specify memory address for C programmi

Post by barrym95838 »

There definitely is a method to bind a hard-coded address to an ISR or an I/O port using a somewhat convoluted cast, but I haven't done so in over 30 years, and the details elude me at the moment (the applicable program listings are stashed somewhere in my attic).
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)
User avatar
CountChocula
Posts: 101
Joined: 07 Nov 2021
Location: Toronto, Canada

Re: Is there a way to specify memory address for C programmi

Post by CountChocula »

It might be helpful if you could tell us a bit more about what you're trying to do.

One possible solution is to use a trampoline written in a separate assembler file, where you can specify the origin, that simply jumps to the address of your C function; that way, you can just rely on the the linker to figure out where your code should go.

So if you have this code in, say, myfunc.c:

Code: Select all

void __fastcall__ myFunction()
You could write something like this (from memory and untested, but hopefully you get the gist) in myfunc_trampoline.s:

Code: Select all

.proc _myFunctionTramponline:
  .org $C000
  jmp _myFunction
.endproc
The problem, however, is that defining the origin of any piece of code doesn't mean that it will be automatically relocated there when loaded from media; it just makes the compiler generate code that is pinned to a specific location for the purpose of calculating absolute addresses. It is then your responsibility (or your OS's, if it supports dynamic loading) to place it in a particular location in memory.

Hope this helps!
Post Reply