How to deal with code that will be relocated?

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
NormalLuser
Posts: 48
Joined: 24 Sep 2023

How to deal with code that will be relocated?

Post by NormalLuser »

Hi all. I’m working on my Bad Apple! video decoder and have this version just about finished up.
One of the last things I wanted was a self contained ROM that boots right up to the demo.
The problem is that I have a little bit of self modifying code so I want to move some to RAM and I don't quite know what the correct way is to deal with this.
I know how to move data from ROM to RAM, but how do I deal with it from the source side?

IE, I can do a:

Code: Select all

  .org $500
  .. source code ..
assemble, then on my ROM file do something like

Code: Select all

 .org $5000
 .. source code ..
 .org $6000 
 include file.bin
but I was wondering if there was a way to keep everything in the same source file?
Is there a 'Assemble To' command that I can't seem to figure out?
Maybe something like:

Code: Select all

 .org $6000 
 .. source code..
.RelocateLoop ;routine to move code
 LDA RelocateCode,x
 STA $500,x
 inx
 bne .RelocateLoop

RelocateCode:  ; location in output is after above code
 .assembleto $500  ; but the code uses $500 as base address
.Top ; location $500
.. source code ..
jmp .Top ;IE jmp $500
.Bottom
endassembleto
I can’t quite seem to find what I’m looking for from the source code management side.
Thanks!
jgharston
Posts: 181
Joined: 22 Feb 2004

Re: How to deal with code that will be relocated?

Post by jgharston »

Relocated or copied? If it's some code you copy elsewhere to execute, if it's small enough, I usually just embed the difference into the code. Eg:

LDX #codeend-code-1
.initlp
LDA code,X:STA &100,X:DEX:BPL initlp
CALL &100
...
.code ; this code will be copied to &100 onwards
LDA &F4:PHA
LDA #0:JSR romsel-code+&100 ; dest address will be at &100+(distance from code to romsel)
JSR sidecall:PLA
.romsel ; this point will be at &100+(distance from code to here)
STA &F4:STA &FE30:RTS
.codeend

If it's more complicated than that, I think the thing you are looking for your assembler to support is "REPHASE".

org &8000
...
LDX #codeend-code-1
.initlp
LDA code,X:STA &100,X:DEX:BPL initlp
CALL &100
...
phase &100 ; this will be stored here but targetted at &100
.code ; this code will be copied to &100 onwards
LDA &F4:PHA
LDA #0:JSR romsel ; dest address will be at &100+(distance from code to romsel)
JSR sidecall:PLA
.romsel ; this point will be at &100+(distance from code to here)
STA &F4:STA &FE30:RTS
.codeend
dephase ; we now return to storing at &8000+whatever onwards
vbc
Posts: 80
Joined: 23 Apr 2020

Re: How to deal with code that will be relocated?

Post by vbc »

NormalLuser wrote:
there was a way to keep everything in the same source file?
Is there a 'Assemble To' command that I can't seem to figure out?
I don't know what assembler you are using, but with vasm this can be done using rorg/rend directives:

Code: Select all

 Source: "x.s"
                                     1:  .org $6000 
                                     2: ; .. source code..
                                     3: test1
00:6000 4C0060                       4:  jmp test1
                                     5: .RelocateLoop ;routine to move code
00:6003 BD0C60                       6:  LDA RelocateCode,x
00:6006 9D0005                       7:  STA $500,x
00:6009 E8                           8:  inx
00:600A D0F7                         9:  bne .RelocateLoop
                                    10: 
                                    11: RelocateCode:  ; location in output is after above code
                                    12: ; .assembleto $500  ; but the code uses $500 as base address
                                    13:  .rorg $500
                                    14: Top ; location $500
                                    15: ;.. source code ..
00:0500 4C0005                      16:  jmp Top ;IE jmp $500
                                    17: .Bottom
                                    18:  .rend
                                    19: ;endassembleto
                                    20: test2
00:600F 4C0F60                      21:  jmp test2
                                    22: 


Symbols by name:
RelocateCode                     A:600C
Top                              A:0500
test1                            A:6000
test2                            A:600F

> hexdump -C x.out 
00000000  4c 00 60 bd 0c 60 9d 00  05 e8 d0 f7 4c 00 05 4c  |L.`..`......L..L|
00000010  0f 60

NormalLuser
Posts: 48
Joined: 24 Sep 2023

Re: How to deal with code that will be relocated?

Post by NormalLuser »

Thanks guys!
.rorg $1234 and .rend is exactly what I needed.
Don't know how I missed that in the vasm manual.
Worked great!
fachat
Posts: 1123
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Re: How to deal with code that will be relocated?

Post by fachat »

In xa65 you can just do a
*=1234
to redefine the PC at any time.

And even use
*=
to enter relocatable mode if using -R to create relocatable o65 files (relocatable at load time)
Author of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/
Post Reply