Relocating MTX Machine Code

Everything about programming, including VDP and Sound programming.
Post Reply
Bill B
Posts: 593
Joined: 26 Jan 2014 16:31

Relocating MTX Machine Code

Post by Bill B »

This arose from a question Paul asked me.

If writing a program containing mixed Basic and Machine Code, the first line of the program typically looks something like:

Code: Select all

10 CODE 

4007         RET		; Do nothing when executed
4008         JR ENCODE		; Jump table to M/C routines
400A         JR DECODE
This works fine when working on a single machine. However, if you move the code onto a machine with a different amount of memory (32K or 64K) and try and run it, it will fail because all the subroutine calls (and long jumps) are to the wrong address. Before running the code on a different amount of memory you normally have to do "ASSEM 10", and then exit the assembler again to update all the addresses.

I have now worked out how to get the MTX do this automatically. Change the start of the Machine Code to:

Code: Select all

10 CODE 

4007         JR REBASE		; This must be JR not JP
4009         JR ENCODE		; Jump table to M/C routines
400B         JR DECODE
400D REBASE: LD DE,#FFF9	; On entry HL contains start address
4010         ADD HL,DE		; We want the start of CODE line, which is 7 bytes earlier
4011         CALL #1BE3		; Call ROM routine CODETEST (Enter assembler)
4014         RET NZ		; This should never happen
4015         LD A,(#FAD2)	; We now need to call a routine in ROM page 1
4018         PUSH AF		; Save old PAGE register
4019         AND #0F		; Select page 1
401B         OR #10
401D         LD (#FAD2),A	; Update page register
4020         OUT (#00),A
4022         CALL #2277		; Call ROM routine LINK (Exit assembler and update addresses)
4025         POP AF		; Restore page register
4026         LD (#FAD2),A
4029         OUT (#00),A
402B         RET
; Other M/C routines go here
(Edit to fix a minor issue with the code above.)

It is still necessary to take account of the entry points changing, so the next two lines of the program should probably be something like:

Code: Select all

20 LET BASBOT=16384
30 IF (PEEK(64122)=0) THEN  LET BASBOT=32768
And the Machine Code calls similar to:

Code: Select all

3010 LET XERR=USR(BASBOT+11)
Last edited by Bill B on 26 Mar 2019 10:06, edited 1 time in total.
Bill B
Posts: 593
Joined: 26 Jan 2014 16:31

Re: Relocating MTX Machine Code

Post by Bill B »

It occurs to me that the reason that MC on the MTX is always at the top of a program is that if you put it anywhere else, editing the Basic code usually moves the MC and invalidates all the addresses.

Building upon the code above, it should be possible to write a routine to put at the top of a program, which searches for all of the CODE lines and updates the addresses in each of them. This would make it much easier to interleave Basic and MC.

I guess that BBC Basic does something similar automatically when you type RUN.
User avatar
thewiz
Posts: 137
Joined: 12 Aug 2012 16:08

Re: Relocating MTX Machine Code

Post by thewiz »

Nice Bill. It could also check if it is on a machine it was already recompiled on and skip the relink.

Some commercial software would load a block of code at 0x8100 from a basic loader to get around the problem.
Some would stuff it into an ASSEM line with a bit of code to copy it 0x8100 before running it.
Some would stuff it into an ASSEM line padded so that the code started at 0x8100 on a MTX500 and 0x4100 on a MTX512. Then on the MTX512 it would copy it to 0x8100.

Paul
THIS is what Memotech is doing now.
Martin A
Posts: 799
Joined: 09 Nov 2013 21:03

Re: Relocating MTX Machine Code

Post by Martin A »

Bill B wrote: 25 Mar 2019 16:21 I guess that BBC Basic does something similar automatically when you type RUN.
BBC Basic has all sorts of nice things built into the assembler that's missing from the MTX Basic. You can:
  • set it up to assemble dynamically and hide the output.
  • Set it to assemble at one address with a totally different run address, and save the resulting code block to be loaded back later at the run address
  • Access all the labels as basic variables
Its not that MTX basic is bad - Having a built in assembler at all puts it well ahead of the other Z80 offerings. But the BBC one is better, and probably THE best of the era.
User avatar
1024MAK
Posts: 757
Joined: 24 Dec 2012 03:01
Location: Looking forward to summer, in Somerset, UK

Re: Relocating MTX Machine Code

Post by 1024MAK »

Great work Bill :D

Your findings and code example are most useful. So thank you 🙏 8-)

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer in Somerset later in the year :D

Not as many MTXs as Dave! :lol:
User avatar
Crazyboss
Site Admin
Posts: 274
Joined: 09 Aug 2012 21:45
Location: Sweden
Contact:

Re: Relocating MTX Machine Code

Post by Crazyboss »

my game conversions, are compressed, and decompress to 80ff. and executed.
the compressed data is loaded to 4021/8021 (if i remember correct) and then moved up, to higher space, and then decompresses, I add the checksum to be sure that nothing goes wrong :)

Zombie Near, is a MTX512 only game, I created a tape loader for it, and uses compressed data too :)

My memory is a bit fussy but i think made a "basic loader", and later it loads one part of the game into high memory, and then loads the next block to lower memory and decompress the game, I am quite sure you will get a message before the game loads the two blocks, if you try to load it on a mtx500 :) I know that is not what we are used to now a days, now the games will install until 99% and then you will get the error message in the end :)
//CLAUS - Webmaster at www.mtxworld.dk
Post Reply