Mac68k Forums

Home


Welcome, Guest
Guest Settings
Help

Mac68k Forums » Development » Software Hacking

Thread: 32-bit memory accesses from 24-bit code


Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 3 - Pages: 1 - Last Post: May 7, 2016 8:42 PM Last Post By: bbraun Threads: [ Previous | Next ]
bigmessowires


Posts: 217
Registered: 10/29/13
32-bit memory accesses from 24-bit code
Posted: Apr 27, 2016 8:41 PM
Click to report abuse...   Click to reply to this thread Reply
While playing around with the rom disk driver, I discovered a nice way for 24-bit mode code to safely call code that makes 32-bit memory accesses. You can't just call SwapMMUMode() in the middle of 24-bit code that's executing from ROM, because the PC isn't at a valid 32-bit address, so it'll crash as soon as the addressing mode is changed. The rom disk driver gets around this by copying a small function to RAM, where 24- and 32-bit addresses are the same, and having it call SwapMMUMode() from there. I worked out an alternate way that keeps everything in ROM. Just wrap this around the code that needs to make 32-bit memory accesses:

// if we're not already in 32-bit addressing mode, then change modes
	tst.b 0xCB2				// current mmu mode, 0 = 24-bit, 1 = 32-bit
	bne.s _already32bit
	
	pea 20(pc)				// push address of _now32bitSafePC on the stack
	move.l (sp)+, d2		// pop it into D2
	andi.l #0x00FFFFFF, d2
	ori.l #0x40000000, d2	// force high byte to be 0x40 = ROM
	move.l d2, -(sp)		// push modified address onto stack
	rts						// "return" to the modified address
	
_now32bitSafePC:
	move.l d0, -(sp)		// save d0
	move.l #1, d0			// now the PC is a 32-bit safe address
	_SwapMMUMode			// OK to switch to 32-bit addressing mode without crashing
	move.l (sp)+, d0		// restore d0
	move.w #0, -(sp) 		// save old addressing mode (0) on the stack, for restoration at exit
	bra _function
	
_already32bit:
	move.w #1, -(sp) 		// save old addressing mode (1) on the stack, for restoration at exit
	bra _function
 
_function:
	// your code goes here
 
_exit:
	move.w (sp)+, d0			// retrieve original addressing mode
	bne.s _modeOK
	_SwapMMUMode
_modeOK:
	rts
 


Maybe this will be useful for other things somewhere...
bigmessowires


Posts: 217
Registered: 10/29/13
Re: 32-bit memory accesses from 24-bit code
Posted: Apr 27, 2016 9:18 PM   in response to: bigmessowires in response to: bigmessowires
Click to report abuse...   Click to reply to this thread Reply
I just thought of an even easier solution:

asm void MakeSafeRomPC()
{
    // modify the return address on the stack
    move.l (sp)+, d0
    andi.l d0, 0x00FFFFFF
    ori.l d0, 0x40000000
    move.l d0, -(sp)
    rts
}


You can call that from any regular C function that you've put into ROM, that may be executing in 24-bit mode but needs to do 32-bit mode stuff:

void MyRomFunction()
{
    char mode;
    MakeSafeRomPC();
    mode = SwapMMUMode(true32bit);
 
    // do 32-bit stuff here
 
    SwapMMUMode(mode);
}
bigmessowires


Posts: 217
Registered: 10/29/13
Re: 32-bit memory accesses from 24-bit code
Posted: May 6, 2016 12:39 PM   in response to: bigmessowires in response to: bigmessowires
Click to report abuse...   Click to reply to this thread Reply
I've been using the second technique successfully. There were some mistakes in the example code above, though. Here's a corrected and improved version:
asm void MakeSafeRomPC()
{
    // modify the return address on the stack
    andi.l #0x00FFFFFF, (sp)
    ori.l #0x40000000, (sp)
    rts
}
 
void MyRomFunction()
{
    signed char addressMode = true32b;
		
    // make sure we're in 32-bit mode
    MakeSafeRomPC();
    SwapMMUMode(&addressMode);
		
    // do 32-bit stuff here
		
    // restore the old addressing mode
    SwapMMUMode(&addressMode);
}

Also, extra care must be taken with the "do 32-bit stuff here" code. If the code involves pointers to memory blocks that were allocated in 24-bit mode, you'll need to call StripAddress on the pointers before using them. Global variable references may also need extra care, since in my experience the compiler will use an address register (with a non-32-bit-safe address) as a base pointer for globals. So from looking at the C code, it's not obvious that some snippet of code will involve a non-32-bit-safe address dereference. If your code assigns global values to temporary variables on the stack before calling SwapMMUMode, and then operates on the temporary variables, it should be OK.
bbraun


Posts: 493
Registered: 7/25/12
Re: 32-bit memory accesses from 24-bit code
Posted: May 7, 2016 8:42 PM   in response to: bigmessowires in response to: bigmessowires
Click to report abuse...   Click to reply to this thread Reply
Nice!

Point your RSS reader here for a feed of the latest messages in all forums