📄 mpc.c
字号:
/********************************************************************/
static int
crb_value (char *crbstr)
{
int crb;
char *p, *p2;
crb = strtoul(crbstr, &p, 10);
if ((crb == 0) && (p == crbstr))
{
/* invalid numeric value, check for 'cr' */
p2 = crbstr;
if (strncasecmp(crbstr,"c",1) == 0)
p2 = &crbstr[1];
if (strncasecmp(crbstr,"cr",2) == 0)
p2 = &crbstr[2];
crb = strtoul(p2, &p, 10);
if ((crb == 0) && (p == p2))
return -1;
}
if ((crb < 0) || (crb > 31))
return -1;
return crb;
}
/********************************************************************/
static int
sr_value (char *srstr)
{
int sr;
char *p, *p2;
sr = strtoul(srstr, &p, 10);
if ((sr == 0) && (p == srstr))
{
/* invalid numeric value, check for 'cr' */
p2 = srstr;
if (strncasecmp(srstr,"s",1) == 0)
p2 = &srstr[1];
if (strncasecmp(srstr,"sr",2) == 0)
p2 = &srstr[2];
sr = strtoul(p2, &p, 10);
if ((sr == 0) && (p == p2))
return -1;
}
if ((sr < 0) || (sr > 15))
return -1;
return sr;
}
/********************************************************************/
static int
fivebit_value (char *numstr)
{
char *p;
int num;
num = strtoul(numstr,&p,10);
if ((num == 0) && (p == numstr))
return -1;
if ((num < 0) || (num > 31))
return -1;
return num;
}
/********************************************************************/
static int
eightbit_value (char *numstr)
{
char *p;
int num;
num = strtoul(numstr,&p,10);
if ((num == 0) && (p == numstr))
return -1;
if ((num < 0) || (num > 255))
return -1;
return num;
}
/********************************************************************/
static int
crf_value (char *crfstr)
{
int crf;
char *p, *p2;
crf = strtoul(crfstr, &p, 10);
if ((crf == 0) && (p == crfstr))
{
/* invalid numeric value, check for 'cr' */
p2 = crfstr;
if (strncasecmp(crfstr,"c",1) == 0)
p2 = &crfstr[1];
if (strncasecmp(crfstr,"cr",2) == 0)
p2 = &crfstr[2];
crf = strtoul(p2, &p, 10);
if ((crf == 0) && (p == p2))
return -1;
}
if ((crf < 0) || (crf > 7))
return -1;
return crf;
}
/********************************************************************/
static int
afunc (void)
{
return TRUE;
}
/********************************************************************/
static int
afunc_rD_rA_rB (void)
{
/*
* This works for instructions with the following formats:
*
* insn rD,rA,rB (rD bits 6-10, rA bits 11-15, rB bits 16-20)
* insn rS,rA,rB (rS bits 6-10, rA bits 11-15, rB bits 16-20)
* insn rD,rA,NB
*/
char *rDs, *rAs, *rBs;
int rD, rA, rB;
if (parse_args(&rDs,&rAs,&rBs,NULL,NULL) != 3)
return FALSE;
if ((rD = gpr_value(rDs)) < 0)
return FALSE;
if ((rA = gpr_value(rAs)) < 0)
return FALSE;
if ((rB = gpr_value(rBs)) < 0)
return FALSE;
asm_insn |= (rD << rDshift);
asm_insn |= (rA << rAshift);
asm_insn |= (rB << rBshift);
return TRUE;
}
/********************************************************************/
static int
afunc_rD_rA_SIMM (void)
{
/*
* This works for instructions with the following formats:
*
* insn rD,rA,SIMM (rD bits 6-10, rA bits 11-15, SIMM bits 16-31)
*/
char *rDs, *rAs, *p, *simm;
int rD, rA;
uint32 value;
if (parse_args(&rDs,&rAs,&simm,NULL,NULL) != 3)
return FALSE;
if ((rD = gpr_value(rDs)) < 0)
return FALSE;
if ((rA = gpr_value(rAs)) < 0)
return FALSE;
value = strtoul(simm,&p,0);
if ((value == 0) && (p == simm))
return FALSE;
/* FIX !!! check for SIMM out of bounds */
asm_insn |= (rD << rDshift);
asm_insn |= (rA << rAshift);
asm_insn |= (value & 0x0000FFFF);
return TRUE;
}
/********************************************************************/
static int
afunc_rA_rS_rB (void)
{
/*
* This works for instructions with the following formats:
*
* insn rA,rS,rB (rA bits 11-15, rS bits 6-10, rB bits 16-20)
*/
char *rAs, *rSs, *rBs;
int rA, rS, rB;
if (parse_args(&rAs,&rSs,&rBs,NULL,NULL) != 3)
return FALSE;
if ((rA = gpr_value(rAs)) < 0)
return FALSE;
if ((rS = gpr_value(rSs)) < 0)
return FALSE;
if ((rB = gpr_value(rBs)) < 0)
return FALSE;
asm_insn |= (rA << rAshift);
asm_insn |= (rS << rSshift);
asm_insn |= (rB << rBshift);
return TRUE;
}
/********************************************************************/
static int
afunc_rD_rA (void)
{
/*
* This works for instructions with the following formats:
*
* insn rD,rA (rD bits 6-10, rA bits 11-15)
*/
char *rDs, *rAs;
int rD, rA;
if (parse_args(&rDs,&rAs,NULL,NULL,NULL) != 2)
return FALSE;
if ((rD = gpr_value(rDs)) < 0)
return FALSE;
if ((rA = gpr_value(rAs)) < 0)
return FALSE;
asm_insn |= (rD << rDshift);
asm_insn |= (rA << rAshift);
return TRUE;
}
/********************************************************************/
static int
afunc_rA_rS_UIMM (void)
{
/*
* This works for instructions with the following formats:
*
* insn rA,rS,UIMM (rS bits 6-10, rA bits 11-15, UIMM bits 16-31)
*/
char *rAs, *rSs, *p, *uimm;
int rA, rS;
uint32 value;
if (parse_args(&rAs,&rSs,&uimm,NULL,NULL) != 3)
return FALSE;
if ((rA = gpr_value(rAs)) < 0)
return FALSE;
if ((rS = gpr_value(rSs)) < 0)
return FALSE;
value = strtoul(uimm,&p,0);
if ((value == 0) && (p == uimm))
return FALSE;
if (value & 0xFFFF0000)
return FALSE;
asm_insn |= (rA << rAshift);
asm_insn |= (rS << rSshift);
asm_insn |= (value & 0x0000FFFF);
return TRUE;
}
/********************************************************************/
static int
afunc_taddr (void)
{
/*
* This works for instructions with the following formats:
*
* insn target_addr (target_addr bits 6-29)
*/
char *taddr;
int success;
uint32 value, LI;
if (parse_args(&taddr,NULL,NULL,NULL,NULL) != 1)
return FALSE;
value = get_value(taddr, &success, 0);
if (!success)
return FALSE;
LI = value - asm_pc;
asm_insn |= (LI & ~0xFC000003);
return TRUE;
}
/********************************************************************/
static int
afunc_ataddr (void)
{
/*
* This works for instructions with the following formats:
*
* ba target_addr
* bla target_addr
*/
char *taddr;
int success;
uint32 LI;
if (parse_args(&taddr,NULL,NULL,NULL,NULL) != 1)
return FALSE;
LI = get_value(taddr, &success, 0);
if (!success)
return FALSE;
asm_insn |= (LI & ~0xFC000003);
return TRUE;
}
/********************************************************************/
static int
afunc_BO_BI_taddr (void)
{
/*
* This works for instructions with the following formats:
*
* insn BO,BI,target_addr (BO bits 6-10, BI bits 11-15, taddr bits 16-29)
*/
char *BOs, *BIs, *taddr;
int success, BI, BO;
uint32 value, LI;
if (parse_args(&BOs,&BIs,&taddr,NULL,NULL) != 3)
return FALSE;
if ((BI = fivebit_value(BIs)) < 0)
return FALSE;
if ((BO = fivebit_value(BOs)) < 0)
return FALSE;
value = get_value(taddr, &success, 0);
if (!success)
return FALSE;
LI = value - asm_pc;
if (LI & 0x00000003)
return FALSE;
if (LI & 0xFFFF8000)
{
if ((LI & 0xFFFF8000) != 0xFFFF8000) /* out of range */
return FALSE;
}
asm_insn |= (BO << BOshift);
asm_insn |= (BI << BIshift);
asm_insn |= (LI & ~0xFFFF0003);
return TRUE;
}
/********************************************************************/
static int
afunc_BO_BI_ataddr (void)
{
/*
* This works for instructions with the following formats:
*
* bca BO,BI,target_addr
* bcla BO,BI,target_addr
*/
char *BOs, *BIs, *taddr;
int success, BI, BO;
uint32 LI;
if (parse_args(&BOs,&BIs,&taddr,NULL,NULL) != 3)
return FALSE;
if ((BI = fivebit_value(BIs)) < 0)
return FALSE;
if ((BO = fivebit_value(BOs)) < 0)
return FALSE;
LI = get_value(taddr, &success, 0);
if (!success)
return FALSE;
if (LI & 0x00000003)
return FALSE;
if (LI & 0xFFFF8000)
{
if ((LI & 0xFFFF8000) != 0xFFFF8000) /* out of range */
return FALSE;
}
asm_insn |= (BO << BOshift);
asm_insn |= (BI << BIshift);
asm_insn |= (LI & ~0xFFFF0003);
return TRUE;
}
/********************************************************************/
static int
afunc_crfD_L_rA_rB (void)
{
/*
* This works for instructions with the following formats:
*
* insn crfD,L,rA,rB
*/
char *crfDs, *Ls, *rAs, *rBs;
int crfD, L, rA, rB;
if (parse_args(&crfDs,&Ls,&rAs,&rBs,NULL) != 4)
return FALSE;
if ((crfD = crf_value(crfDs)) < 0)
return FALSE;
if ((L = fivebit_value(Ls)) < 0)
return FALSE;
if ((L != 0) && (L != 1))
return FALSE;
if ((rA = gpr_value(rAs)) < 0)
return FALSE;
if ((rB = gpr_value(rBs)) < 0)
return FALSE;
asm_insn |= (crfD << crfDshift);
asm_insn |= (L << Lshift);
asm_insn |= (rA << rAshift);
asm_insn |= (rB << rBshift);
return TRUE;
}
/********************************************************************/
static int
afunc_crfD_L_rA_SIMM (void)
{
/*
* This works for instructions with the following formats:
*
* insn crfD,L,rA,SIMM
*/
char *crfDs, *Ls, *rAs, *SIMMs, *p;
int crfD, L, rA, SIMM;
if (parse_args(&crfDs,&Ls,&rAs,&SIMMs,NULL) != 4)
return FALSE;
if ((crfD = crf_value(crfDs)) < 0)
return FALSE;
if ((L = fivebit_value(Ls)) < 0)
return FALSE;
if ((L != 0) && (L != 1))
return FALSE;
if ((rA = gpr_value(rAs)) < 0)
return FALSE;
SIMM = strtoul(SIMMs,&p,0);
if ((SIMM == 0) && (p == SIMMs))
return FALSE;
asm_insn |= (crfD << crfDshift);
asm_insn |= (L << Lshift);
asm_insn |= (rA << rAshift);
asm_insn |= (SIMM & 0x0000FFFF);
return TRUE;
}
/********************************************************************/
static int
afunc_crfD_L_rA_UIMM (void)
{
/*
* This works for instructions with the following formats:
*
* insn crfD,L,rA,UIMM
*/
char *crfDs, *Ls, *rAs, *uimm, *p;
int crfD, L, rA;
uint32 value;
if (parse_args(&crfDs,&Ls,&rAs,&uimm,NULL) != 4)
return FALSE;
if ((crfD = crf_value(crfDs)) < 0)
return FALSE;
if ((L = fivebit_value(Ls)) < 0)
return FALSE;
if ((L != 0) && (L != 1))
return FALSE;
if ((rA = gpr_value(rAs)) < 0)
return FALSE;
value = strtoul(uimm,&p,0);
if ((value == 0) && (p == uimm))
return FALSE;
if (value & 0xFFFF0000)
return FALSE;
asm_insn |= (crfD << crfDshift);
asm_insn |= (L << Lshift);
asm_insn |= (rA << rAshift);
asm_insn |= (value && 0x0000FFFF);
return TRUE;
}
/********************************************************************/
static int
afunc_crbD_crbA_crbB (void)
{
/*
* This works for instructions with the following formats:
*
* insn crbD,crbA,crbB
*/
char *crbDs, *crbAs, *crbBs;
int crbD, crbA, crbB;
if (parse_args(&crbDs,&crbAs,&crbBs,NULL,NULL) != 3)
return FALSE;
if ((crbD = crb_value(crbDs)) < 0)
return FALSE;
if ((crbA = crb_value(crbAs)) < 0)
return FALSE;
if ((crbB = crb_value(crbBs)) < 0)
return FALSE;
asm_insn |= (crbD << crbDshift);
asm_insn |= (crbA << crbAshift);
asm_insn |= (crbB << crbBshift);
return TRUE;
}
/********************************************************************/
static int
afunc_BO_BI (void)
{
/*
* This works for instructions with the following formats:
*
* insn BO,BI (BO bits 6-10, BI bits 11-15)
*/
char *BOs, *BIs;
int BI, BO;
if (parse_args(&BOs,&BIs,NULL,NULL,NULL) != 2)
return FALSE;
if ((BI = fivebit_value(BIs)) < 0)
return FALSE;
if ((BO = fivebit_value(BOs)) < 0)
return FALSE;
asm_insn |= (BO << BOshift);
asm_insn |= (BI << BIshift);
return TRUE;
}
/********************************************************************/
static int
afunc_rA_rS (void)
{
/*
* This works for instructions with the following formats:
*
* insn rA,rS (rA bits 11-15, rS bits 6-10)
*/
char *rAs, *rSs;
int rA, rS;
if (parse_args(&rAs,&rSs,NULL,NULL,NULL) != 2)
return FALSE;
if ((rA = gpr_value(rAs)) < 0)
return FALSE;
if ((rS = gpr_value(rSs)) < 0)
return FALSE;
asm_insn |= (rA << rAshift);
asm_insn |= (rS << rSshift);
return TRUE;
}
/********************************************************************/
static int
afunc_rA_rB (void)
{
/*
* This works for instructions with the following formats:
*
* insn rA,rB (rA bits 11-15, rB bits 16-20)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -