/*********************************/
/*                               */
/*            EMI88.c            */
/*                               */
/* Execute MARS Instruction a'la */
/* ICWS'88.                      */
/*                               */
/* July 21, 1991                 */
/* Mark A. Durham                */
/*********************************/

#define FALSE  0
#define TRUE   1

struct Statement_Register {
   unsigned char  Control;    /* Encoded Opcode, A-mode, and B-mode */
   int            AField;
   int            BField;
};

struct CoreNode {
   struct CoreNode *last;
   int PC;
   struct CoreNode *next;
};

enum Opcode {
   DAT,
   MOV,
   ADD,
   SUB,
   JMP,
   JMZ,
   JMN,
   DJN,
   CMP,
   SLT,
   SPL
};

enum Mode {
   IMMEDIATE,
   DIRECT,
   INDIRECT,
   PREDECREMENT
};

int EMI88(
   struct CoreNode            *CLL[],
   struct Statement_Register  CORE[],
   int                        MaxTask,
   int                        memory,
   int                        *Tasks
) {
   struct Statement_Register  SR;
   struct Statement_Register  SRA;
   struct Statement_Register  SRB;
   struct CoreNode            *lastCoreNode;
   struct CoreNode            *tempCoreNode;
   int                        index;
   int                        PC;
   int                        PCA;
   int                        PCAI;
   int                        PCB;
   int                        PCBI;
   enum Opcode                OpCode;
   enum Mode                  AMode;
   int                        AValue;
   enum Mode                  BMode;
   int                        BValue;
   int                        cmp;

   for (index = 0; index < 2; index++) {
      PC = CLL[index]->PC;
      SR = CORE[PC];
      AMode = (SR.Control & 0x0C)/4;
      if (AMode == IMMEDIATE) {
         PCA = PC;
         SRA = CORE[PCA];
         AValue = SRA.AField;
      } else {
         PCA = (PC + SR.AField) % memory;
         SRA = CORE[PCA];
         if (AMode != DIRECT) {
            if (AMode == PREDECREMENT) {
               PCAI = PCA;
               PCA = (PCA + memory - 1) % memory;
               CORE[PCAI].BField = (CORE[PCAI].BField + memory - 1) % memory;
            };
            PCA = (PCA + SRA.BField) % memory;
            SRA = CORE[PCA];
         };
         AValue = SRA.BField;
      };
      BMode = SR.Control & 0x03;
      if (BMode == IMMEDIATE) {
         PCB = PC;
         SRB = CORE[PCB];
      } else {
         PCB = (PC + SR.BField) % memory;
         SRB = CORE[PCB];
         if (BMode != DIRECT) {
            if (BMode == PREDECREMENT) {
               PCBI = PCB;
               PCB = (PCB + memory - 1) % memory;
               CORE[PCBI].BField = (CORE[PCBI].BField + memory - 1) % memory;
            };
            PCB = (PCB + SRB.BField) % memory;
            SRB = CORE[PCB];
         };
      };
      BValue = SRB.BField;
      CLL[index]->PC = (CLL[index]->PC + 1) % memory;
      lastCoreNode = CLL[index];
      CLL[index] = CLL[index]->next;
      OpCode = (SR.Control & 0xF0)/16;
      switch (OpCode) {
      case DAT:
         Tasks[index] -= 1;
         tempCoreNode = lastCoreNode;
         CLL[index]->last = tempCoreNode->last;
         tempCoreNode->last->next = CLL[index];
         free(tempCoreNode);
         break;
      case MOV:
         if (AMode == IMMEDIATE) {
            CORE[PCB].BField = AValue;
         } else {
            CORE[PCB] = SRA;
         };
         break;
      case ADD:
         CORE[PCB].BField = (AValue + BValue) % memory;
         if (AMode != IMMEDIATE) {
            CORE[PCB].AField = (SRA.AField + SRB.AField) % memory;
         };
         break;
      case SUB:
         CORE[PCB].BField = (BValue + memory - AValue) % memory;
         if (AMode != IMMEDIATE) {
            CORE[PCB].AField = (SRA.AField + SRB.AField) % memory;
         };
         break;
      case JMP:
         lastCoreNode->PC = PCA;
         break;
      case JMZ:
         if (BValue == 0) {
            lastCoreNode->PC = PCA;
         };
         break;
      case JMN:
         if (BValue != 0) {
            lastCoreNode->PC = PCA;
         };
         break;
      case DJN:
         CORE[PCB].BField = (CORE[PCB].BField + memory - 1) % memory;
         if (BValue != 1) {    /* ((BValue-1) != 0) */
            lastCoreNode->PC = PCA;
         };
         break;
      case CMP:
         cmp = FALSE;
         if (AMode == IMMEDIATE) {
            if (AValue == BValue) {
               cmp = TRUE;
            };
         } else {
            if (SRA.Control == SRB.Control) {
               if (SRA.AField == SRB.AField) {
                  if (SRA.BField == SRB.BField) {
                     cmp = TRUE;
                  };
               };
            };
         };
         if (cmp) {
            lastCoreNode->PC = (PC + 2) % memory;
         };
         break;
      case SPL:
         if (Tasks[index] < MaxTask) {
            Tasks[index] += 1;
            tempCoreNode = (struct CoreNode *)malloc(sizeof(struct CoreNode));
            tempCoreNode->last = lastCoreNode;
            tempCoreNode->PC = PCA;
            tempCoreNode->next = CLL[index];
            lastCoreNode->next = tempCoreNode;
            CLL[index]->last = tempCoreNode;
         };
         break;
      case SLT :
         if (BValue < AValue) {
            lastCoreNode->PC = (PC + 2) % memory;
         };
         break;
      default:
         return(FALSE);
      };
   };
   return(TRUE);
}

