2 // This file is part of the PicoDrive Megadrive Emulator
\r
4 // Copyright (c) 2011 FinalDave (emudave (at) gmail.com)
\r
6 // This code is licensed under the GNU General Public License version 2.0 and the MAME License.
\r
7 // You can choose the license that has the most advantages for you.
\r
9 // SVN repository can be found at http://code.google.com/p/cyclone68000/
\r
11 #include "PicoInt.h"
\r
13 typedef unsigned char u8;
\r
14 typedef unsigned short u16;
\r
15 typedef unsigned int u32;
\r
17 static int PicoMemBase(u32 pc)
\r
21 if (pc<Pico.romsize)
\r
23 membase=(int)Pico.rom; // Program Counter in Rom
\r
25 else if ((pc&0xe00000)==0xe00000)
\r
27 membase=(int)Pico.ram-(pc&0xff0000); // Program Counter in Ram
\r
31 // Error - Program Counter is invalid
\r
32 membase=(int)Pico.rom;
\r
39 extern "C" u8 *OP_ROM=NULL,*OP_RAM=NULL;
\r
41 static void CPU_CALL PicoCheckPc(u32 pc)
\r
43 OP_ROM=(u8 *)PicoMemBase(pc);
\r
45 // don't bother calling us back unless it's outside the 64k segment
\r
46 M68000_regs.AsmBank=(pc>>16);
\r
51 static u32 PicoCheckPc(u32 pc)
\r
53 pc-=PicoCpu.membase; // Get real pc
\r
56 PicoCpu.membase=PicoMemBase(pc);
\r
58 return PicoCpu.membase+pc;
\r
63 static u32 PicoCheckPc(u32) { return 0; }
\r
67 int PicoInitPc(u32 pc)
\r
73 // -----------------------------------------------------------------
\r
74 static int PadRead(int i)
\r
77 pad=~PicoPad[i]; // Get inverse of pad
\r
79 if (Pico.m.padSelect[i]) value=0x40|(pad&0x3f); // 01CB RLDU
\r
80 else value=((pad&0xc0)>>2)|(pad&3); // 00SA 00DU
\r
82 return (value<<8)|value; // Mirror bytes
\r
85 static u32 OtherRead16(u32 a)
\r
89 if ((a&0xffe000)==0xa00000)
\r
92 d=*(u16 *)(Pico.zram+(a&0x1fff));
\r
94 if (Pico.m.rotate&2) { d=(Pico.m.rotate>>2)&0xff; d|=d<<8; } // Fake z80
\r
100 if ((a&0xffe000)==0xa00000)
\r
103 d=((Pico.m.rotate++)>>2)&0xff; d|=d<<8;
\r
107 if (a==0xa04000) { d=Pico.m.rotate&3; Pico.m.rotate++; goto end; } // Fudge
\r
108 if (a==0xa10000) { d=Pico.m.hardware; goto end; } // Hardware value
\r
109 if (a==0xa10002) { d=PadRead(0); goto end; }
\r
110 if (a==0xa10004) { d=PadRead(1); goto end; }
\r
111 if (a==0xa11100) { d=((Pico.m.rotate++)&4)<<6; goto end; } // Fudge z80 reset
\r
113 if ((a&0xffffe0)==0xc00000) { d=PicoVideoRead(a); goto end; }
\r
119 static void OtherWrite8(u32 a,u32 d)
\r
121 if ((a&0xffe000)==0xa00000) { Pico.zram[(a^1)&0x1fff]=(u8)d; return; } // Z80 ram
\r
122 if ((a&0xfffffc)==0xa04000) { PsndFm(a,d); return; } // FM Sound
\r
124 if (a==0xa11100) { Pico.m.z80Run=(u8)(d^1); return; }
\r
125 if (a==0xa10003) { Pico.m.padSelect[0]=(u8)((d>>6)&1); return; } // Joypad 1 select
\r
126 if (a==0xa10005) { Pico.m.padSelect[1]=(u8)((d>>6)&1); return; } // Joypad 2 select
\r
128 if ((a&0xffffe0)==0xc00000) { PicoVideoWrite(a,d|(d<<8)); return; } // Byte access gets mirrored
\r
131 static void OtherWrite16(u32 a,u32 d)
\r
133 if ((a&0xffffe0)==0xc00000) { PicoVideoWrite(a,d); return; }
\r
134 if ((a&0xffe000)==0xa00000) { *(u16 *)(Pico.zram+(a&0x1ffe))=(u16)d; return; } // Z80 ram
\r
136 OtherWrite8(a, d>>8);
\r
137 OtherWrite8(a+1,d&0xff);
\r
140 // -----------------------------------------------------------------
\r
141 // Read Rom and read Ram
\r
143 static u8 CPU_CALL PicoRead8(u32 a)
\r
148 if (a<Pico.romsize) return *(u8 *)(Pico.rom+(a^1)); // Rom
\r
149 if ((a&0xe00000)==0xe00000) return *(u8 *)(Pico.ram+((a^1)&0xffff)); // Ram
\r
151 d=OtherRead16(a&~1); if ((a&1)==0) d>>=8;
\r
155 u16 CPU_CALL PicoRead16(u32 a)
\r
159 if (a<Pico.romsize) { u16 *pm=(u16 *)(Pico.rom+a); return pm[0]; } // Rom
\r
160 if ((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xffff)); return pm[0]; } // Ram
\r
162 return (u16)OtherRead16(a);
\r
165 u32 CPU_CALL PicoRead32(u32 a)
\r
169 if (a<Pico.romsize) { u16 *pm=(u16 *)(Pico.rom+a); return (pm[0]<<16)|pm[1]; } // Rom
\r
170 if ((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xffff)); return (pm[0]<<16)|pm[1]; } // Ram
\r
172 return (OtherRead16(a)<<16)|OtherRead16(a+2);
\r
175 // -----------------------------------------------------------------
\r
178 static void CPU_CALL PicoWrite8(u32 a,u8 d)
\r
180 if ((a&0xe00000)==0xe00000) { u8 *pm=(u8 *)(Pico.ram+((a^1)&0xffff)); pm[0]=d; return; } // Ram
\r
186 static void CPU_CALL PicoWrite16(u32 a,u16 d)
\r
188 if ((a&0xe00000)==0xe00000) { *(u16 *)(Pico.ram+(a&0xfffe))=d; return; } // Ram
\r
194 static void CPU_CALL PicoWrite32(u32 a,u32 d)
\r
196 if ((a&0xe00000)==0xe00000)
\r
199 u16 *pm=(u16 *)(Pico.ram+(a&0xfffe));
\r
200 pm[0]=(u16)(d>>16); pm[1]=(u16)d;
\r
205 OtherWrite16(a, (u16)(d>>16));
\r
206 OtherWrite16(a+2,(u16)d);
\r
210 // -----------------------------------------------------------------
\r
214 // Setup memory callbacks:
\r
215 PicoCpu.checkpc=PicoCheckPc;
\r
216 PicoCpu.fetch8 =PicoCpu.read8 =PicoRead8;
\r
217 PicoCpu.fetch16=PicoCpu.read16=PicoRead16;
\r
218 PicoCpu.fetch32=PicoCpu.read32=PicoRead32;
\r
219 PicoCpu.write8 =PicoWrite8;
\r
220 PicoCpu.write16=PicoWrite16;
\r
221 PicoCpu.write32=PicoWrite32;
\r
230 u8 (__fastcall *Read8) (u32 a);
\r
231 u16 (__fastcall *Read16)(u32 a);
\r
232 u32 (__fastcall *Read32)(u32 a);
\r
233 void (__fastcall *Write8) (u32 a,u8 d);
\r
234 void (__fastcall *Write16) (u32 a,u16 d);
\r
235 void (__fastcall *Write32) (u32 a,u32 d);
\r
236 void (__fastcall *ChangePc)(u32 a);
\r
237 u8 (__fastcall *PcRel8) (u32 a);
\r
238 u16 (__fastcall *PcRel16)(u32 a);
\r
239 u32 (__fastcall *PcRel32)(u32 a);
\r
240 u16 (__fastcall *Dir16)(u32 a);
\r
241 u32 (__fastcall *Dir32)(u32 a);
\r
244 extern "C" struct A68KInter a68k_memory_intf=
\r
257 PicoRead16, // unused
\r
258 PicoRead32, // unused
\r