Casio_asm/interpreter/decoderImp.c

257 lines
6.4 KiB
C

#include "decoderImp.h"
/*void decodeInstructionImp(proc_t *proc, opcode_t *op, opcode_data_t *instruction, char rule) {
int args=0;
switch(rule) {
case RULE_1:
case RULE_1d:
// case RULE_1s:
args=1;
break;
case RULE_2:
case RULE_2d:
case RULE_2s:
args=2;
}
instruction->argc=args;
if(rule==RULE_2s) {
//pass the first argument as it
instruction->arg0.i=op->arg0;
Stack_pop(&proc->stack, &instruction->args[1]);
} else if(op->nArgs==0) {
//pop the arguments from stack
for(int i=0; i<args; i++) {
Stack_pop(&proc->stack, &instruction->args[i]);
}
} else if(op->nArgs==3&&args!=0) {
//decode the immediate value
if(rule==RULE_1d||rule==RULE_2d) {
//immediate decimal
instruction->arg0.d=(decimal_t) ((((op->imm>>15)&1)?-1:1)*((op->imm>>4)&0x7ff)<<(op->imm&0xf));
} else {
//immediate signed integral
instruction->arg0.i=(integral_t) op->imm;
}
//if we have another argument, pop it
if(args==2) {
Stack_pop(&proc->stack, &instruction->args[1]);
}
} else {
//all arguments are from registers
for(int i=0; i<args; i++) {
instruction->args[i]=proc->registers[op->args[i]];
}
}
}*/
void decodeInstructionImp(proc_t *proc, opcode_t *op, opcode_data_t *instruction, char rule) {
//get the number of arguments
int args=0;
switch(rule) {
case RULE_1:
case RULE_1d:
// case RULE_1s:
args=1;
break;
case RULE_2:
case RULE_2d:
case RULE_2s:
args=2;
}
instruction->argc=args;
if(args==0) {
//do nothing
return;
} else if(args==1) {
//read a single argument
if(op->nArgs==3) {
//read immediate argument
if(rule==RULE_1d||rule==RULE_2d) {
//immediate decimal
instruction->arg0.d=(decimal_t) ((((op->imm>>15)&1)?-1:1)*((op->imm>>4)&0x7ff)<<(op->imm&0xf));
} else {
//immediate signed integral
instruction->arg0.i=(integral_t) op->imm;
}
return;
} else if(op->nArgs==0) {
//pop from stack
Stack_pop(&proc->stack, &instruction->arg0);
return;
} else { //op->nArgs==1||op->nArgs==2
//read from registers
if(rule==RULE_2s) {
//or, in this case, we don't
instruction->arg0.i=op->arg0;
} else {
instruction->arg0=proc->registers[op->arg0%256];
}
return;
}
} else { //args==2
if(op->nArgs==3) {
//read immediate argument
if(rule==RULE_1d||rule==RULE_2d) {
//immediate decimal
instruction->arg0.d=(decimal_t) ((((op->imm>>15)&1)?-1:1)*((op->imm>>4)&0x7ff)<<(op->imm&0xf));
} else {
//immediate signed integral
instruction->arg0.i=(integral_t) op->imm;
}
//pop the other one from the stack
Stack_pop(&proc->stack, &instruction->arg1);
return;
} else if(op->nArgs==0) {
//pop from stack
Stack_pop(&proc->stack, &instruction->arg0);
Stack_pop(&proc->stack, &instruction->arg1);
return;
} else { //op->nArgs==1||op->nArgs==2
//read from the first register
if(rule==RULE_2s) {
//or, in this case, we don't
instruction->arg0.i=op->arg0;
} else {
instruction->arg0=proc->registers[op->arg0%256];
}
if(op->nArgs==2) {
//read from the second register
instruction->arg1=proc->registers[op->arg1%256];
} else {
//pop the second argument from the stack
Stack_pop(&proc->stack, &instruction->arg1);
}
return;
}
}
}
/*void decodeInstructionExtImp(proc_t *proc, opcode_ext_t *op, opcode_data_t *instruction, char rule) {
int args=0;
switch(rule) {
case RULE_1:
case RULE_1d:
// case RULE_1s:
args=1;
break;
case RULE_2:
case RULE_2d:
case RULE_2s:
args=2;
}
instruction->argc=args;
if(rule==RULE_2s) {
//pass the first argument as it
instruction->arg0.i=op->arg0;
Stack_pop(&proc->stack, &instruction->args[1]);
} else if(op->nArgs==0) {
//pop the arguments from stack
for(int i=0; i<args; i++) {
Stack_pop(&proc->stack, &instruction->args[i]);
}
} else if(op->nArgs==3&&args!=0) {
//decode the immediate value
if(rule==RULE_1d||rule==RULE_2d) {
//immediate decimal
instruction->arg0.d=(decimal_t) ((((op->imm>>15)&1)?-1:1)*((op->imm>>4)&0x7ff)<<(op->imm&0xf));
} else {
//immediate signed integral
instruction->arg0.i=(integral_t) op->imm;
}
//if we have another argument, pop it
if(args==2) {
Stack_pop(&proc->stack, &instruction->args[1]);
}
} else {
//all arguments are from registers
for(int i=0; i<args; i++) {
instruction->args[i]=proc->registers[op->args[i]];
}
}
}*/
void decodeInstructionExtImp(proc_t *proc, opcode_ext_t *op, opcode_data_t *instruction, char rule) {
//get the number of arguments
int args=0;
switch(rule) {
case RULE_1:
case RULE_1d:
// case RULE_1s:
args=1;
break;
case RULE_2:
case RULE_2d:
case RULE_2s:
args=2;
}
instruction->argc=args;
if(args==0) {
//do nothing
return;
} else if(args==1) {
//read a single argument
if(op->nArgs==3) {
//read immediate argument
if(rule==RULE_1d||rule==RULE_2d) {
//immediate decimal
instruction->arg0.d=(decimal_t) ((((op->imm>>15)&1)?-1:1)*((op->imm>>4)&0x7ff)<<(op->imm&0xf));
} else {
//immediate signed integral
instruction->arg0.i=(integral_t) op->imm;
}
return;
} else if(op->nArgs==0) {
//pop from stack
Stack_pop(&proc->stack, &instruction->arg0);
return;
} else { //op->nArgs==1||op->nArgs==2
//read from registers
if(rule==RULE_2s) {
//or, in this case, we don't
instruction->arg0.i=op->arg0;
} else {
instruction->arg0=proc->registers[op->arg0%256];
}
return;
}
} else { //args==2
if(op->nArgs==3) {
//read immediate argument
if(rule==RULE_1d||rule==RULE_2d) {
//immediate decimal
instruction->arg0.d=(decimal_t) ((((op->imm>>15)&1)?-1:1)*((op->imm>>4)&0x7ff)<<(op->imm&0xf));
} else {
//immediate signed integral
instruction->arg0.i=(integral_t) op->imm;
}
//pop the other one from the stack
Stack_pop(&proc->stack, &instruction->arg1);
return;
} else if(op->nArgs==0) {
//pop from stack
Stack_pop(&proc->stack, &instruction->arg0);
Stack_pop(&proc->stack, &instruction->arg1);
return;
} else { //op->nArgs==1||op->nArgs==2
//read from the first register
if(rule==RULE_2s) {
//or, in this case, we don't
instruction->arg0.i=op->arg0;
} else {
instruction->arg0=proc->registers[op->arg0%256];
}
if(op->nArgs==2) {
//read from the second register
instruction->arg1=proc->registers[op->arg1%256];
} else {
//pop the second argument from the stack
Stack_pop(&proc->stack, &instruction->arg1);
}
return;
}
}
}