An "unemulatable" program

Troubles with your machine? Just want to speak about the latest improvements? This is the place!

Moderators: Mug UK, Zorro 2, Greenious, spiny, Moderator Team

czietz
Hardware Guru
Hardware Guru
Posts: 941
Joined: Tue May 24, 2016 6:47 pm

An "unemulatable" program

Postby czietz » Fri Jan 26, 2018 9:29 pm

A little riddle for the weekend:

Generally, ST emulation is considered to be quite good, with the emulators reaching cycle-accuracy and being able to run even many "tricky" demos and such pixel perfectly. However, I wrote the attached 250 byte program and I have yet to find an emulator that runs it correctly. On real hardware, i.e. on a real Atari ST, when run, the program will greet you with a friendly message. On emulators -- I tested Hatari, Steem, SainT -- it'll crash or terminate silently. (Note that the program is ST only and will not run on a TT, Falcon, etc.)

Have fun figuring out what happens here! :D

PS: Those reading my posts on the Hatari list will know a hint about the root cause; but that's cheating. :wink:
You do not have the required permissions to view the files attached to this post.

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2427
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: An "unemulatable" program

Postby lp » Fri Jan 26, 2018 10:02 pm

Oddly it ran on aranym. Is that cheating? ;)

czietz
Hardware Guru
Hardware Guru
Posts: 941
Joined: Tue May 24, 2016 6:47 pm

Re: An "unemulatable" program

Postby czietz » Fri Jan 26, 2018 10:07 pm

lp wrote:Oddly it ran on aranym. Is that cheating? ;)


Odd, indeed. I'll see if I can "fix" that tomorrow. :)

User avatar
viking272
Captain Atari
Captain Atari
Posts: 388
Joined: Mon Oct 13, 2008 12:50 pm
Location: west of London, UK

Re: An "unemulatable" program

Postby viking272 » Fri Jan 26, 2018 10:42 pm

If emulation_detected then crash_horribly?
:)

czietz
Hardware Guru
Hardware Guru
Posts: 941
Joined: Tue May 24, 2016 6:47 pm

Re: An "unemulatable" program

Postby czietz » Sat Jan 27, 2018 8:51 am

viking272 wrote:If emulation_detected then crash_horribly?
:)


It does not use any functions (NatFeats, special XBIOS calls) etc. to specifically detect an emulator. It just exploits something that seemingly all ST emulators get wrong.

lp wrote:Oddly it ran on aranym. Is that cheating? ;)


Interestingly, I can't get it to run on Aranym 1.0.2. It crashes. However, it would probably also crash on a real 68040 CPU; the program is ST/68000 only, as wrote. Thus, I wonder what special Aranym configuration you have that it allows it to run.

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2427
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: An "unemulatable" program

Postby lp » Sat Jan 27, 2018 9:22 am

I don't use fvdi or host file system. I use videl mode with NVDI and hard images. Perhaps those matter?

ThorstenOtto
Atari Super Hero
Atari Super Hero
Posts: 607
Joined: Sun Aug 03, 2014 5:54 pm

Re: An "unemulatable" program

Postby ThorstenOtto » Sat Jan 27, 2018 11:38 am

lp wrote:Oddly it ran on aranym. Is that cheating? ;)


Crashes here with as bus error. On StonX, it crashes with an illegal instruction error.

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2427
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: An "unemulatable" program

Postby lp » Sat Jan 27, 2018 11:56 am

I ran it again and snapped an image.

Screen Shot 2018-01-27 at 6.54.25 AM.png
You do not have the required permissions to view the files attached to this post.

helmut
Captain Atari
Captain Atari
Posts: 175
Joined: Thu Jan 07, 2010 4:30 pm

Re: An "unemulatable" program

Postby helmut » Sat Jan 27, 2018 12:34 pm

Runs fine on my aranym too (jit,fVDI only).
You do not have the required permissions to view the files attached to this post.

czietz
Hardware Guru
Hardware Guru
Posts: 941
Joined: Tue May 24, 2016 6:47 pm

Re: An "unemulatable" program

Postby czietz » Sat Jan 27, 2018 4:20 pm

Hatari fixed this in their Mercurial repo about 3 hours ago. So my test program becomes less "unemulatable".

User avatar
TheNameOfTheGame
Atari God
Atari God
Posts: 1209
Joined: Mon Jul 23, 2012 8:57 pm
Location: Almost Heaven, West Virginia

Re: An "unemulatable" program

Postby TheNameOfTheGame » Sun Jan 28, 2018 4:27 pm

So can we get an explanation of the unemulatable part?

czietz
Hardware Guru
Hardware Guru
Posts: 941
Joined: Tue May 24, 2016 6:47 pm

Re: An "unemulatable" program

Postby czietz » Sun Jan 28, 2018 5:29 pm

Here comes the explanation: The program exploits the fact that during ADDX.L -(An),-(Ay) the reads happen in descending order. I.e. consider...

Code: Select all

LEA $2004, A0
LEA $1004, A1
ADDX.L -(A0),-(A1)


The CPU will first read the least significant word (16 bits, LSW) from address $2002, then the most significant word (MSW) from $2000. This is in fact different from e.g. "ADD.L -(A0),D0" where the CPU will first read the MSW from $2000, then the LSW from $2002. This is by no means a new finding, it is documented in the YACHT ("Yet Another Cycle Hunting Table") and probably also in Motorola's patent US4,325,121. However, emulators did not emulate the order of bus accesses correctly for ADDX.L (and SUBX.L) -- in case of Hatari until I reported this on the mailing list.

Thus, if you force the CPU to take a bus error exception, a different fault address will be in the stack frame on a real 68000 compared to an emulated one. The rest of the program is only "smoke and mirrors" to disguise this difference. It is left as exercise to the reader to find out how the code reacts differently to those different exception stack frames.

There are still more subtle differences between emulation and real CPU in the processing of bus and address error exceptions.

BlankVector
Captain Atari
Captain Atari
Posts: 473
Joined: Wed Oct 24, 2007 7:52 pm
Location: France
Contact:

Re: An "unemulatable" program

Postby BlankVector » Sun Jan 28, 2018 5:43 pm

Brilliant 8)
Subscribe to my Vretrocomputing channel on YouTube and Facebook. Latest video: Emulators.

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2427
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: An "unemulatable" program

Postby lp » Sun Jan 28, 2018 6:25 pm

Fascinating reading. I have jit disabled on aranym. Able to shed any light as to why it reacts differently on various aranym setups?

czietz
Hardware Guru
Hardware Guru
Posts: 941
Joined: Tue May 24, 2016 6:47 pm

Re: An "unemulatable" program

Postby czietz » Sun Jan 28, 2018 6:49 pm

You don't happen to have a debug build of Aranym where you can single-step through my program?
My guess is that in some cases (compare the crash that Thorsten saw with your successful run) the 68040 bus error stack frame -- which is completely different -- happens to do just the right thing.

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2427
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Re: An "unemulatable" program

Postby lp » Sun Jan 28, 2018 6:54 pm

I have the build you made with some nice fixes for the mac. I assume the debug stuff is missing.


Social Media

     

Return to “Hardware”

Who is online

Users browsing this forum: No registered users and 3 guests