initialize_msp.c 3.47 KB
#include <R4300.h>
#include <rcp.h>
#include <ramrom.h>
#include "osint.h"
#include "piint.h"
#include "rdb.h"

typedef struct {
	unsigned int inst1;		/* lui	k0,XXXX */
	unsigned int inst2;		/* addiu k0,k0,XXXX */
	unsigned int inst3;		/* jr k0 */
	unsigned int inst4;		/* nop */
} __osExceptionVector;


extern u64 osClockRate ; 
extern int osViClock ;	/* Video clock rate (default is NTSC) */
extern s32 __osShutdown ;   /* 1 = pre-NMI interrupt has occured */
extern OSIntMask __OSGlobalIntMask ;
#ifdef _FINALROM
extern s32 __osFinalrom;
#else
extern s32 __kmc_pt_mode;
extern void *__printfunc;
#endif

extern void __createSpeedParam(void);

#ifndef _FINALROM

/* 機種依存の print (callback) */

volatile static unsigned *stat= (unsigned *)0xbff08004;
volatile static unsigned *wport=(unsigned *)0xbff08000;
#if 0
volatile static unsigned *piok= (unsigned *)0xa4600010;
#endif

extern s32 __kmc_pt_mode;

static void rmonPutchar(char c)
{
#if 0
    while((*piok & 3)!=0) ;
    while((*stat & 4)==0) ;
    *wport=c;
#else
    u32 data;
    while(1){
	osPiReadIo((u32)stat,&data);
	if (data & 4){
	  osPiWriteIo((u32)wport,c);
	  break;
      }
    }
#endif
}

static void *msp_proutSyncPrintf(void *str, const char *buf, int n)
{
    int i;
    char c,*p,*q,xbuf[128];
    static int column = 0;

    p=xbuf;
    for(i=0;i<n;++i){
	c=*(buf++);
	switch (c) {
	case '\n':
	    *(p++)='\n';
	    column = 0;
	    break;
	case '\t':
	    do{
		*(p++)=' ';
	    }while(++column % 8);
	    break;
	default:
	    column++;
	    *(p++)=c;
	    break;
	}
	if(c=='\n' || (p-xbuf)>100){
	    rmonPutchar((p-xbuf)-1);
	    q=xbuf;
	    while(q!=p){
		rmonPutchar(*(q++));
	    }
	    p=xbuf;
	}
    }
    if(p!=xbuf){
	rmonPutchar((p-xbuf)-1);
	q=xbuf;
	while(q!=p){
	    rmonPutchar(*(q++));
	}
    }
    return ((void *) 1);   /* return a fake pointer so that it's not NULL */
}
#endif

void
__osInitialize_msp(void)
{
#ifndef _FINALROM

	/* デバッガ機種依存の初期化 */
	if(__kmc_pt_mode==0){
		int (*fnc)();
		unsigned *src,*dst;
		unsigned monadr;
		extern unsigned __ptExceptionPreamble[];
		volatile unsigned *mon;
		volatile unsigned *stat;

		stat= (unsigned *)0xbff08004;
		mon=(unsigned *)0xbff00000;

		if(*mon!=0x4b4d4300) return;

		/*
		 * Copy partner exception preamble to 0x8000_0180-019f
		 * Since leoBootID variable starts from 0x8000_01a0,
		 * exception preamble should not be overwrapped.
		 */
		src=(unsigned *)__ptExceptionPreamble;
		dst=(unsigned *)E_VEC;

		*(dst++)=*(src++); 	/* 0x8000_0180 */
		*(dst++)=*(src++); 	/* 0x8000_0184 */
		*(dst++)=*(src++); 	/* 0x8000_0188 */
		src+=2;
		dst+=2;
		*(dst++)=*(src++); 	/* 0x8000_0194 */
		*(dst++)=*(src++); 	/* 0x8000_0198 */
		*(dst++)=*(src++); 	/* 0x8000_019c */

		osWritebackDCache((void*)E_VEC,9*sizeof(unsigned));
		osInvalICache((void*)E_VEC,9*sizeof(unsigned));

		__kmc_pt_mode=1;

		if((*stat & 0x10)==0){	/* RUN OFF ? */
		    monadr=*(mon+1);
		    if(monadr!=0xbff00000){
			unsigned *src,*dst;
			unsigned ct;

			dst=(unsigned *)(monadr | 0x20000000);
			ct=0x2000/4;
			src=(unsigned *)0xbff00000;
			while(ct){
			    *(dst++)=*(src++);
			    --ct;
			}
		    }
		    (unsigned int *)fnc = monadr+8;
		    (*fnc)(0x4b4d4300,0);
		}
		
		__printfunc = (void*)&msp_proutSyncPrintf;
	}
#endif
}

/* デバッガの接続チェック */
int __checkHardware_msp(void)
{
    volatile unsigned *mon;
    
    mon=(unsigned *)0xbff00000;

    if(*mon==0x4b4d4300){

	mon = (unsigned *)0xbff00010;
	if(*mon == 0x00000000)
	  return 1;
	else
	  return 0;
    }
    else
    return 0;
}