Finish migrating to new mem handling. Make carthw db external.
[picodrive.git] / pico / z80if.c
1 #include "pico_int.h"
2 #include "sound/sn76496.h"
3
4 #define Z80_MEM_SHIFT 13
5
6 unsigned long z80_read_map [0x10000 >> Z80_MEM_SHIFT];
7 unsigned long z80_write_map[0x10000 >> Z80_MEM_SHIFT];
8
9 #ifdef _USE_MZ80
10
11 // memhandlers for mz80 core
12 unsigned char mz80_read(UINT32 a,  struct MemoryReadByte *w)  { return z80_read(a); }
13 void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); }
14
15 // structures for mz80 core
16 static struct MemoryReadByte mz80_mem_read[]=
17 {
18   {0x0000,0xffff,mz80_read},
19   {(UINT32) -1,(UINT32) -1,NULL}
20 };
21 static struct MemoryWriteByte mz80_mem_write[]=
22 {
23   {0x0000,0xffff,mz80_write},
24   {(UINT32) -1,(UINT32) -1,NULL}
25 };
26 static struct z80PortRead mz80_io_read[] ={
27   {(UINT16) -1,(UINT16) -1,NULL}
28 };
29 static struct z80PortWrite mz80_io_write[]={
30   {(UINT16) -1,(UINT16) -1,NULL}
31 };
32
33 int mz80_run(int cycles)
34 {
35   int ticks_pre = mz80GetElapsedTicks(0);
36   mz80exec(cycles);
37   return mz80GetElapsedTicks(0) - ticks_pre;
38 }
39
40 #endif
41
42 #ifdef _USE_DRZ80
43 struct DrZ80 drZ80;
44 #endif
45
46
47 PICO_INTERNAL void z80_init(void)
48 {
49 #ifdef _USE_MZ80
50   struct mz80context z80;
51
52   // z80
53   mz80init();
54   // Modify the default context
55   mz80GetContext(&z80);
56
57   // point mz80 stuff
58   z80.z80Base=Pico.zram;
59   z80.z80MemRead=mz80_mem_read;
60   z80.z80MemWrite=mz80_mem_write;
61   z80.z80IoRead=mz80_io_read;
62   z80.z80IoWrite=mz80_io_write;
63
64   mz80SetContext(&z80);
65 #endif
66 #ifdef _USE_DRZ80
67   memset(&drZ80, 0, sizeof(drZ80));
68   drZ80.z80_rebasePC=NULL; // unused, handled by xmap
69   drZ80.z80_rebaseSP=NULL;
70   drZ80.z80_read8   =(void *)z80_read_map;
71   drZ80.z80_read16  =NULL;
72   drZ80.z80_write8  =(void *)z80_write_map;
73   drZ80.z80_write16 =NULL;
74   drZ80.z80_irq_callback=NULL;
75 #endif
76 #ifdef _USE_CZ80
77   memset(&CZ80, 0, sizeof(CZ80));
78   Cz80_Init(&CZ80);
79   Cz80_Set_ReadB(&CZ80, NULL); // unused (hacked in)
80   Cz80_Set_WriteB(&CZ80, NULL);
81 #endif
82 }
83
84 PICO_INTERNAL void z80_reset(void)
85 {
86 #ifdef _USE_MZ80
87   mz80reset();
88 #endif
89 #ifdef _USE_DRZ80
90   memset(&drZ80, 0, 0x54);
91   drZ80.Z80F  = (1<<2);  // set ZFlag
92   drZ80.Z80F2 = (1<<2);  // set ZFlag
93   drZ80.Z80IX = 0xFFFF << 16;
94   drZ80.Z80IY = 0xFFFF << 16;
95   drZ80.Z80IM = 0; // 1?
96   drZ80.z80irqvector = 0xff0000; // RST 38h
97   drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0] << 1;
98   // drZ80 is locked in single bank
99   drZ80.Z80SP_BASE = ((PicoAHW & PAHW_SMS) ?
100     z80_read_map[0xc000 >> Z80_MEM_SHIFT] : z80_read_map[0]) << 1;
101 //  drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ?
102 #endif
103 #ifdef _USE_CZ80
104   Cz80_Reset(&CZ80);
105   Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff);
106   Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff);
107   Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000);
108 #endif
109 }
110
111 // XXX TODO: should better use universal z80 save format
112 PICO_INTERNAL void z80_pack(unsigned char *data)
113 {
114 #if defined(_USE_MZ80)
115   struct mz80context mz80;
116   *(int *)data = 0x00005A6D; // "mZ"
117   mz80GetContext(&mz80);
118   memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers
119 #elif defined(_USE_DRZ80)
120   *(int *)data = 0x015A7244; // "DrZ" v1
121 //  drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
122 //  drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
123   memcpy(data+4, &drZ80, 0x54);
124 #elif defined(_USE_CZ80)
125   *(int *)data = 0x00007a43; // "Cz"
126   *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC);
127   memcpy(data+8, &CZ80, (INT32)&CZ80.BasePC - (INT32)&CZ80);
128 #endif
129 }
130
131 PICO_INTERNAL void z80_unpack(unsigned char *data)
132 {
133 #if defined(_USE_MZ80)
134   if (*(int *)data == 0x00005A6D) { // "mZ" save?
135     struct mz80context mz80;
136     mz80GetContext(&mz80);
137     memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4);
138     mz80SetContext(&mz80);
139   } else {
140     z80_reset();
141     z80_int();
142   }
143 #elif defined(_USE_DRZ80)
144   if (*(int *)data == 0x015A7244) { // "DrZ" v1 save?
145     int pc, sp;
146     memcpy(&drZ80, data+4, 0x54);
147     pc = (drZ80.Z80PC - drZ80.Z80PC_BASE) & 0xffff;
148     sp = (drZ80.Z80SP - drZ80.Z80SP_BASE) & 0xffff;
149     // update bases
150     drZ80.Z80PC_BASE = z80_read_map[pc >> Z80_MEM_SHIFT];
151     if (drZ80.Z80PC & (1<<31)) {
152       elprintf(EL_STATUS|EL_ANOMALY, "bad PC in z80 save: %04x", pc);
153       drZ80.Z80PC_BASE = drZ80.Z80PC = z80_read_map[0];
154     } else {
155       drZ80.Z80PC_BASE <<= 1;
156       drZ80.Z80PC = drZ80.Z80PC_BASE + pc;
157     }
158     drZ80.Z80SP_BASE = z80_read_map[sp >> Z80_MEM_SHIFT];
159     if (drZ80.Z80SP & (1<<31)) {
160       elprintf(EL_STATUS|EL_ANOMALY, "bad SP in z80 save: %04x", sp);
161       drZ80.Z80SP_BASE = z80_read_map[0];
162       drZ80.Z80SP = drZ80.Z80SP_BASE + (1 << Z80_MEM_SHIFT);
163     } else {
164       drZ80.Z80SP_BASE <<= 1;
165       drZ80.Z80SP = drZ80.Z80SP_BASE + sp;
166     }
167   } else {
168     z80_reset();
169     drZ80.Z80IM = 1;
170     z80_int(); // try to goto int handler, maybe we won't execute trash there?
171   }
172 #elif defined(_USE_CZ80)
173   if (*(int *)data == 0x00007a43) { // "Cz" save?
174     memcpy(&CZ80, data+8, (INT32)&CZ80.BasePC - (INT32)&CZ80);
175     Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4));
176   } else {
177     z80_reset();
178     z80_int();
179   }
180 #endif
181 }
182
183 PICO_INTERNAL void z80_exit(void)
184 {
185 #if defined(_USE_MZ80)
186   mz80shutdown();
187 #endif
188 }
189
190 PICO_INTERNAL void z80_debug(char *dstr)
191 {
192 #if defined(_USE_DRZ80)
193   sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE);
194 #elif defined(_USE_CZ80)
195   sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", CZ80.PC - CZ80.BasePC, CZ80.SP.W);
196 #endif
197 }