/ MCUME_teensy / teensycastaway / m68k_intrf.cpp
m68k_intrf.cpp
  1  #include <stdio.h>
  2  #include <stdlib.h>
  3  #include <string.h>
  4  
  5  #include <Arduino.h>
  6  
  7  #include "dcastaway.h"
  8  #include "mem.h"
  9  #include "st.h"
 10  
 11  #include "m68k_intrf.h"
 12  
 13  #ifdef DEBUG_FAME
 14  FILE *fame_debug_file=stdout;
 15  unsigned fame_debug_count=0;
 16  #ifdef DEBUG_FAME_START
 17  int fame_debug_output=0;
 18  #else
 19  int fame_debug_output=1;
 20  #endif
 21  #endif
 22  
 23  #ifdef DEBUG_FAME
 24  #include "dis.h"
 25  #endif
 26  
 27  char GetMemB(unsigned long address);
 28  short GetMemW(unsigned long address);
 29  long GetMemL(unsigned long address);
 30  void SetMemB(unsigned long address, unsigned char value);
 31  void SetMemW(unsigned long address, unsigned short value);
 32  void SetMemL(unsigned long address, unsigned long value);
 33  
 34  
 35  static void SetMemWW(unsigned long address, unsigned short value)
 36  {  
 37      address &= MEMADDRMASK;
 38    if (address<MEM1SIZE)
 39      WriteW(address + membase, value);
 40    else
 41      WriteW(address-MEM1SIZE + membase2, value);
 42  }
 43  
 44  static void SetMemBB (unsigned long address, unsigned char value) {
 45      address &= MEMADDRMASK;
 46    if (address<MEM1SIZE)
 47      WriteB(address + membase, value);
 48    else
 49      WriteB(address-MEM1SIZE + membase2, value);
 50  }
 51  
 52  static char GetMemBB(unsigned long address)
 53  {
 54      address &= MEMADDRMASK;
 55    if (address<MEM1SIZE)
 56      return ReadB(address + membase);
 57    else  
 58      return ReadB(address-MEM1SIZE + membase2);
 59  }
 60  
 61  
 62  static short GetMemWW(unsigned long address)
 63  {
 64      address &= MEMADDRMASK;
 65    if (address<MEM1SIZE)
 66      return ReadW(address + membase);
 67    else  
 68      return ReadW(address-MEM1SIZE + membase2);
 69  }
 70  
 71  
 72  #if defined(FAME_SINGLE_MEMORY) && defined(DEBUG_FAME)
 73  
 74  static char MyGetMemB(unsigned long address)
 75  {
 76  	char ret;
 77  
 78  	if (fame_debug_output)
 79  		{ fprintf(fame_debug_file,"GetMemB(0x%.6X)",address); DEBUG_FAME_FFLUSH; }
 80  
 81  	ret=GetMemB(address);
 82  
 83  	if (fame_debug_output)
 84  		{ fprintf(fame_debug_file," = 0x%.2X\n",ret); DEBUG_FAME_FFLUSH; }
 85  
 86  	return ret;
 87  }
 88  
 89  static short MyGetMemW(unsigned long address)
 90  {
 91  	short ret;
 92  
 93  	if (fame_debug_output)
 94  		{ fprintf(fame_debug_file,"GetMemW(0x%.6X)",address); DEBUG_FAME_FFLUSH; }
 95  	
 96  	ret=GetMemW(address);
 97  
 98  	if (fame_debug_output)
 99  		{ fprintf(fame_debug_file," = 0x%.4X\n",ret); DEBUG_FAME_FFLUSH; }
100  
101  	return ret;
102  }
103  
104  static void MySetMemB(unsigned long address, unsigned value)
105  {
106  	value&=0xff;
107  	if (fame_debug_output)
108  		{ fprintf(fame_debug_file,"SetMemB(0x%.6X,0x%.2X)",address,value); DEBUG_FAME_FFLUSH; }
109  
110  	SetMemB(address,value);
111  
112  	if (fame_debug_output)
113  		{ fputs(fame_debug_file,"."); DEBUG_FAME_FFLUSH; }
114  }
115  
116  static void MySetMemW(unsigned long address, unsigned value)
117  {
118  	value&=0xffff;
119  	if (fame_debug_output)
120  		{ fprintf(fame_debug_file,"SetMemW(0x%.6X,0x%.4X)",address,value); DEBUG_FAME_FFLUSH; }
121  
122  	SetMemW(address,value);
123  
124  	if (fame_debug_output)
125  		{ fputs(".",fame_debug_file); DEBUG_FAME_FFLUSH; }
126  }
127  
128  #else
129  
130  #define MyGetMemW GetMemW
131  #define MyGetMemB GetMemB
132  #define MySetMemW SetMemW
133  #define MySetMemB SetMemB
134  
135  #endif
136  
137  #ifndef USE_FAME_CORE
138  int recalc_int=1; //0;
139  #endif
140  int in_fame_core=0;
141  int number_exg0_fame=0;
142  static unsigned addr_exg0_fame=0;
143  static unsigned pc_exg0_fame=0;
144  static unsigned sr_exg0_fame=0;
145  static int rw_exg0_fame=0;
146  static unsigned long exaddress=0x12345678;
147  
148  void HWReset(void)
149  {
150  	m68k_reset();
151  	exaddress=0x12345678;
152  }
153  
154  static int inexec=0;
155  
156  PROGMEM void ExceptionGroup0_execute(void)
157  {
158  	short context = 0;
159  
160  #ifdef DEBUG_FAME
161  	if (fame_debug_output)
162  		{ fputs("ExceptionGroup0_execute",fame_debug_file); DEBUG_FAME_FFLUSH; }
163  #endif
164  
165  	M68KCONTEXT.execinfo&=0x7F;
166  
167  	if ((!((exaddress+1!=addr_exg0_fame)&&(exaddress+2!=addr_exg0_fame)&&(exaddress+3!=addr_exg0_fame))) || (!inexec))
168  	{
169  		if (!inexec)
170  			m68k_reset();
171  		return;
172  	}
173  	inexec=0;
174  	
175  	context |= 0x8;
176  	if (rw_exg0_fame)
177  		context |= 0x10;
178  	if (GetS())
179  		context |= 0x4;
180  	if (rw_exg0_fame && addr_exg0_fame == pc_exg0_fame)
181  		context |= 0x2;
182  	else
183  		context |= 0x1;
184  
185  	M68KCONTEXT.sr |= (1<<13);
186  	M68KCONTEXT.sr &= ~(1<<15);
187  
188  	M68KCONTEXT.areg[7]-=14;
189  	SetMemW(M68KCONTEXT.areg[7], context);
190  	SetMemL(M68KCONTEXT.areg[7] + 2, addr_exg0_fame);
191  	SetMemW(M68KCONTEXT.areg[7] + 6, GetMemW(pc_exg0_fame));
192  	SetMemW(M68KCONTEXT.areg[7] + 8, sr_exg0_fame);
193  	SetMemL(M68KCONTEXT.areg[7] + 10, pc_exg0_fame);
194  
195  	M68KCONTEXT.pc=GetMemL((long)number_exg0_fame * 4);
196  
197  	exaddress=addr_exg0_fame;
198  	number_exg0_fame=0;
199  }
200  
201  PROGMEM void    ExceptionGroup0(
202          int number,             /* trap number */
203          unsigned long address,  /* fault address */
204          int rw)         /* read = true, write = false */
205  {
206  #ifdef DEBUG_FAME
207  	if (fame_debug_output)
208  		{ fprintf(fame_debug_file,"ExceptionGroup0(%i,0x%X,%i)\n",number,address,rw); DEBUG_FAME_FFLUSH; }
209  #endif
210  	if ((exaddress+1!=address)&&(exaddress+2!=address)&&(exaddress+3!=address)&&(!inexec)){
211  		inexec=1;
212  		number_exg0_fame=number;
213  		addr_exg0_fame=address;
214  		rw_exg0_fame=rw;
215  		pc_exg0_fame=M68KCONTEXT.pc;
216  		sr_exg0_fame=M68KCONTEXT.sr;
217  		if (!in_fame_core)
218  			ExceptionGroup0_execute();
219  		else
220  			IO_CYCLE=0;
221  	}
222  }
223  
224  
225  static M68K_CONTEXT context;
226  
227  static M68K_PROGRAM program[]= {
228    {0, MEM1SIZE-1, (unsigned)membase},
229    {MEM1SIZE, MEMSIZE-1, (unsigned)membase2},  
230  /*  
231  	{0, MEMSIZE-1, (unsigned)membase},
232  	{MEMSIZE, (MEMSIZE*2)-1, ((unsigned)membase)-MEMSIZE},
233  	{MEMSIZE*2, (MEMSIZE*3)-1, ((unsigned)membase)-(MEMSIZE*2)},
234  #if MEMSIZE*4 <= ROMBASE2
235  	{MEMSIZE*3, (MEMSIZE*4)-1, ((unsigned)membase)-(MEMSIZE*3)},
236  #if MEMSIZE*5 <= ROMBASE2
237  	{MEMSIZE*4, (MEMSIZE*5)-1, ((unsigned)membase)-(MEMSIZE*4)},
238  #if MEMSIZE*6 <= ROMBASE2
239  	{MEMSIZE*5, (MEMSIZE*6)-1, ((unsigned)membase)-(MEMSIZE*5)},
240  #if MEMSIZE*7 <= ROMBASE2
241  	{MEMSIZE*6, (MEMSIZE*7)-1, ((unsigned)membase)-(MEMSIZE*6)},
242  #if MEMSIZE*8 <= ROMBASE2
243  	{MEMSIZE*7, (MEMSIZE*8)-1, ((unsigned)membase)-(MEMSIZE*7)},
244  #if MEMSIZE*9 <= ROMBASE2
245  	{MEMSIZE*8, (MEMSIZE*9)-1, ((unsigned)membase)-(MEMSIZE*8)},
246  #if MEMSIZE*10 <= ROMBASE2
247  	{MEMSIZE*9, (MEMSIZE*10)-1, ((unsigned)membase)-(MEMSIZE*9)},
248  #if MEMSIZE*11 <= ROMBASE2
249  	{MEMSIZE*10, (MEMSIZE*11)-1, ((unsigned)membase)-(MEMSIZE*10)},
250  #if MEMSIZE*12 <= ROMBASE2
251  	{MEMSIZE*11, (MEMSIZE*12)-1, ((unsigned)membase)-(MEMSIZE*11)},
252  #if MEMSIZE*13 <= ROMBASE2
253  	{MEMSIZE*12, (MEMSIZE*13)-1, ((unsigned)membase)-(MEMSIZE*12)},
254  #if MEMSIZE*14 <= ROMBASE2
255  	{MEMSIZE*13, (MEMSIZE*14)-1, ((unsigned)membase)-(MEMSIZE*13)},
256  #if MEMSIZE*15 <= ROMBASE2
257  	{MEMSIZE*14, (MEMSIZE*15)-1, ((unsigned)membase)-(MEMSIZE*14)},
258  #if MEMSIZE*16 <= ROMBASE2
259  	{MEMSIZE*15, (MEMSIZE*16)-1, ((unsigned)membase)-(MEMSIZE*15)},
260  #endif
261  #endif
262  #endif
263  #endif
264  #endif
265  #endif
266  #endif
267  #endif
268  #endif
269  #endif
270  #endif
271  #endif
272  #endif
273  */
274  	{ROMBASE2, (IOBASE-1), (unsigned)rombase},
275  	{IOBASE, 0x00FFFFFF, (unsigned)rombase},
276  	{(unsigned)-1,(unsigned)-1,(unsigned)NULL}
277  };
278  
279  #ifndef FAME_SINGLE_MEMORY
280  static M68K_DATA read8[] = {
281    {0, MEMSIZE-1, (void *)GetMemBB, NULL},
282  //  {0, MEMSIZE-1, NULL, (void *)membase},
283  //  {0, MEM1SIZE-1, NULL, (void *)membase},
284  //  {MEM1SIZE, MEMSIZE-1, NULL, (void *)membase2},
285    {MEMSIZE, (ROMBASE2)-1, (void *)GetMemB, NULL},
286    {ROMBASE2, IOBASE-1, NULL, (void *)rombase},
287    {IOBASE, 0x00FFFFFF, (void *)DoIORB, NULL},
288    {(unsigned)-1,(unsigned)-1,NULL,NULL}
289  };
290  
291  static M68K_DATA read16[] = {
292    {0, MEMSIZE-1, (void *)GetMemWW, NULL},
293  //  {0, MEMSIZE-1, NULL, (void *)membase},
294  //  {0, MEM1SIZE-1, NULL, (void *)membase},
295  //  {MEM1SIZE, MEMSIZE-1, NULL, (void *)membase2},
296    {MEMSIZE, (ROMBASE2)-1, (void *)GetMemW, NULL},
297    {ROMBASE2, IOBASE-1, NULL, (void *)rombase},
298    {IOBASE, 0x00FFFFFF, (void *)DoIORW, NULL},
299    {(unsigned)-1,(unsigned)-1,NULL,NULL}
300  };
301  
302  static M68K_DATA write8[] = {
303    {0, MEMSIZE-1,  (void *)SetMemBB, NULL},
304  //  {0, MEMSIZE-1, NULL, (void *)membase},
305  //  {0, MEM1SIZE-1, NULL, (void *)membase},
306  //  {MEM1SIZE, MEMSIZE-1, NULL, (void *)membase2},
307    {MEMSIZE, (IOBASE)-1, (void *)SetMemB, NULL},
308    {IOBASE, 0x00FFFFFF, (void *)DoIOWB, NULL},
309    {(unsigned)-1,(unsigned)-1,NULL,NULL}
310  };
311  
312  static M68K_DATA write16[] = {
313    {0, MEMSIZE-1, (void *)SetMemWW, NULL},
314  //  {0, MEMSIZE-1, NULL, (void *)membase},
315  //  {0, MEM1SIZE-1, NULL, (void *)membase},
316  //  {MEM1SIZE, MEMSIZE-1, NULL, (void *)membase2},
317    {MEMSIZE, (IOBASE)-1, (void *)SetMemW, NULL},
318    {IOBASE, 0x00FFFFFF, (void *)DoIOWW, NULL},
319    {(unsigned)-1,(unsigned)-1,NULL,NULL}
320  };
321  
322  #else
323  
324  static M68K_DATA read8[] = {
325  	{ 0, 0xFFFFFF, (void *)MyGetMemB, NULL },
326  	{(unsigned)-1,(unsigned)-1,NULL,NULL}
327  };
328  
329  static M68K_DATA read16[] = {
330  	{ 0, 0xFFFFFF, (void *)MyGetMemW, NULL },
331  	{(unsigned)-1,(unsigned)-1,NULL,NULL}
332  };
333  
334  static M68K_DATA write8[] = {
335  	{ 0, 0xFFFFFF, (void *)MySetMemB, NULL },
336  	{(unsigned)-1,(unsigned)-1,NULL,NULL}
337  };
338  
339  static M68K_DATA write16[] = {
340  	{ 0, 0xFFFFFF, (void *)MySetMemW, NULL },
341  	{(unsigned)-1,(unsigned)-1,NULL,NULL}
342  };
343  
344  #endif
345  
346  PROGMEM void initialize_memmap(void)
347  {
348  	int i;
349  	memset(&context,0,sizeof(M68K_CONTEXT));
350  	context.fetch=context.sv_fetch=context.user_fetch=(M68K_PROGRAM*)&program;
351  	context.read_byte=context.sv_read_byte=context.user_read_byte=(M68K_DATA*)&read8;
352  	context.read_word=context.sv_read_word=context.user_read_word=(M68K_DATA*)&read16;
353  	context.write_byte=context.sv_write_byte=context.user_write_byte=(M68K_DATA*)&write8;
354  	context.write_word=context.sv_write_word=context.user_write_word=(M68K_DATA*)&write16;
355  	for(i=0;program[i].low_addr != ((unsigned)-1);i++)
356    {
357  		if (program[i].low_addr >= ROMBASE2)
358  			program[i].offset= (((unsigned)rombase)+ROMBASE2)-program[i].low_addr;
359  		//else
360  		//	program[i].offset= ((unsigned)membase) - (i*MEMSIZE);
361    }
362    program[0].offset= ((unsigned)membase) ;
363    program[1].offset= ((unsigned)membase2 - MEM1SIZE) ;
364  
365  
366  #ifndef FAME_SINGLE_MEMORY
367  //	read8[0].data=read16[0].data=write8[0].data=write16[0].data=(void *)((unsigned)membase);
368  	read8[2].data=read16[2].data=(void *)((unsigned)rombase);
369  //  read8[0].data=read16[0].data=write8[0].data=write16[0].data=(void *)((unsigned)membase);
370  //  read8[1].data=read16[1].data=write8[1].data=write16[1].data=(void *)((unsigned)membase2-MEM1SIZE);
371  //  read8[3].data=read16[3].data=(void *)((unsigned)rombase);
372  
373  #endif
374  
375  	m68k_init();
376  	m68k_set_context(&context);
377  	m68k_reset();
378  }