⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cyclone.txt

📁 motorola ezx 平台下的fba模拟器
💻 TXT
📖 第 1 页 / 共 2 页
字号:
that would fail.
So it's best to use byteswapped ROM and RAM if the 68000 can access it directly.
It's also faster for the memory handlers, because you can do this:
  
  return *(unsigned short *)(mem+a)


Declaring Memory handlers
-------------------------

Before you can reset or execute 68000 opcodes you must first set up a set of memory handlers.
There are 7 functions you have to set up per CPU, like this:

  static unsigned int   MyCheckPc(unsigned int pc)
  static unsigned char  MyRead8  (unsigned int a)
  static unsigned short MyRead16 (unsigned int a)
  static unsigned int   MyRead32 (unsigned int a)
  static void MyWrite8 (unsigned int a,unsigned char  d)
  static void MyWrite16(unsigned int a,unsigned short d)
  static void MyWrite32(unsigned int a,unsigned int   d)

You can think of these functions representing the 68000's memory bus.
The Read and Write functions are called whenever the 68000 reads or writes memory.
For example you might set MyRead8 like this:

  unsigned char MyRead8(unsigned int a)
  {
    a&=0xffffff; // Clip address to 24-bits

    if (a<RomLength) return RomData[a^1]; // ^1 because the memory is byteswapped
    if (a>=0xe00000) return RamData[(a^1)&0xffff];
    return 0xff; // Out of range memory access
  }

The other 5 read/write functions are similar. I'll describe the CheckPc function later on.

Declaring a CPU Context
-----------------------

To declare a CPU simple declare a struct Cyclone in your code. For example to declare
two 68000s:

  struct Cyclone MyCpu;
  struct Cyclone MyCpu2;

It's probably a good idea to initialise the memory to zero:

  memset(&MyCpu, 0,sizeof(MyCpu));
  memset(&MyCpu2,0,sizeof(MyCpu2));

Next point to your memory handlers:

  MyCpu.checkpc=MyCheckPc;
  MyCpu.read8  =MyRead8;
  MyCpu.read16 =MyRead16;
  MyCpu.read32 =MyRead32;
  MyCpu.write8 =MyWrite8;
  MyCpu.write16=MyWrite16;
  MyCpu.write32=MyWrite32;

You also need to point the fetch handlers - for most systems out there you can just
point them at the read handlers:
  MyCpu.fetch8  =MyRead8;
  MyCpu.fetch16 =MyRead16;
  MyCpu.fetch32 =MyRead32;

( Why a different set of function pointers for fetch?
  Well there are some systems, the main one being CPS2, which return different data
  depending on whether the 'fetch' line on the 68000 bus is high or low.
  If this is the case, you can set up different functions for fetch reads.
  Generally though you don't need to. )

Now you are nearly ready to reset the 68000, except you need one more function: checkpc().

The checkpc() function
----------------------

When Cyclone reads opcodes, it doesn't use a memory handler every time, this would be
far too slow, instead it uses a direct pointer to ARM memory.
For example if your Rom image was at 0x3000000 and the program counter was $206,
Cyclone's program counter would be 0x3000206.

The difference between an ARM address and a 68000 address is also stored in a variable called
'membase'. In the above example it's 0x3000000. To retrieve the real PC, Cyclone just
subtracts 'membase'.

When a long jump happens, Cyclone calls checkpc(). If the PC is in a different bank,
for example Ram instead of Rom, change 'membase', recalculate the new PC and return it:

static int MyCheckPc(unsigned int pc)
{
  pc-=MyCpu.membase; // Get the real program counter

  if (pc<RomLength) MyCpu.membase=(int)RomMem;          // Jump to Rom
  if (pc>=0xff0000) MyCpu.membase=(int)RamMem-0xff0000; // Jump to Ram

  return MyCpu.membase+pc; // New program counter
}

Notice that the membase is always ARM address minus 68000 address.

The above example doesn't consider mirrored ram, but for an example of what to do see
PicoDrive (in Memory.cpp).


Almost there - Reset the 68000!
-------------------------------

Next we need to Reset the 68000 to get the initial Program Counter and Stack Pointer. This
is obtained from addresses 000000 and 000004.

Here is code which resets the 68000 (using your memory handlers):

  MyCpu.srh=0x27; // Set supervisor mode
  MyCpu.a[7]=MyCpu.read32(0); // Get Stack Pointer
  MyCpu.membase=0;
  MyCpu.pc=MyCpu.checkpc(MyCpu.read32(4)); // Get Program Counter

And that's ready to go.


Executing the 68000
-------------------

To execute the 68000, set the 'cycles' variable to the number of cycles you wish to execute,
and then call CycloneRun with a pointer to the Cyclone structure.

e.g.:
  // Execute 1000 cycles on the 68000:
  MyCpu.cycles=1000; CycloneRun(&MyCpu);

For each opcode, the number of cycles it took is subtracted and the function returns when
it reaches 0.

e.g.
  // Execute one instruction on the 68000:
  MyCpu.cycles=0; CycloneRun(&MyCpu);
  printf("  The opcode took %d cycles\n", -MyCpu.cycles);

You should try to execute as many cycles as you can for maximum speed.
The number actually executed may be slightly more than requested, i.e. cycles may come
out with a small negative value:

e.g.
  int todo=12000000/60; // 12Mhz, for one 60hz frame
  MyCpu.cycles=todo; CycloneRun(&MyCpu);
  printf("  Actually executed %d cycles\n", todo-MyCpu.cycles);

To calculate the number of cycles executed, use this formula:
  Number of cycles requested - Cycle counter at the end


Interrupts
----------

Causing an interrupt is very simple, simply set the irq variable in the Cyclone structure
to the IRQ number.
To lower the IRQ line, set it to zero.

e.g:
  MyCpu.irq=6; // Interrupt level 6
  MyCpu.cycles=20000; CycloneRun(&MyCpu);

Note that the interrupt is not actually processed until the next call to CycloneRun,
and the interrupt may not be taken until the 68000 interrupt mask is changed to allow it.

( The IRQ isn't checked on exiting from a memory handler: I don't think this will cause
  me any trouble because I've never needed to trigger an interrupt from a memory handler,
  but if someone needs to, let me know...)


Accessing Cycle Counter
-----------------------

The cycle counter in the Cyclone structure is not, by default, updated before
calling a memory handler, only at the end of an execution.

*update*
Now this is configurable in config.h, there is no 'debug' variable.


Accessing Program Counter and registers
---------------------------------------

You can read Cyclone's registers directly from the structure at any time (as far as I know).

The Program Counter, should you need to read or write it, is stored with membase
added on. So use this formula to calculate the real 68000 program counter:

  pc = MyCpu.pc - MyCpu.membase;

The program counter is stored in r4 during execution, and isn't written back to the
structure until the end of execution, which means you can't read normally real it from
a memory handler.

*update*
Now this is configurable in config.h, there is no 'debug' variable. You can even enable
access to SR if you need. However changing PC in memhandlers is still not safe, you should
better clear cycles, wait untill CycloneRun() returns and then do whatever you need.


Emulating more than one CPU
---------------------------

Since everything is based on the structures, emulating more than one cpu at the same time
is just a matter of declaring more than one structures and timeslicing. You can emulate
as many 68000s as you want.
Just set up the memory handlers for each cpu and run each cpu for a certain number of cycles.

e.g.
  // Execute 1000 cycles on 68000 #1:
  MyCpu.cycles=1000; CycloneRun(&MyCpu);

  // Execute 1000 cycles on 68000 #2:
  MyCpu2.cycles=1000; CycloneRun(&MyCpu2);


Thanks to...
------------

* All the previous code-generating assembler cpu core guys!
  Who are iirc... Neill Corlett, Neil Bradley, Mike Coates, Darren Olafson
    and Bart Trzynadlowski

* Charles Macdonald, for researching just about every console ever
* MameDev+FBA, for keeping on going and going and going


-------------

Dave  - 17th April 2004
notaz - 17th July 2006

Homepage: http://www.finalburn.com/
Dave's e-mail: dev(atsymbol)finalburn.com
Replace (atsymbol) with @

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -