/*

File: instruction.c
Author: Neil Cafferkey
Copyright (C) 1999-2001 Neil Cafferkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.

*/

#include "exec/types.h"
#include <stdio.h>

#include "instruction_protos.h"
#include "instruction_set_protos.h"
#include "address_set_protos.h"
#include "memory_protos.h"
#include "string_protos.h"
#include "sequence_protos.h"


#define TEXT_BUFFER_SIZE 1000+1
#define DATA_BUFFER_SIZE 1000


/* Function: CreateInstruction
 * ===========================
 * Creates an Instruction.
 */

Instruction CreateInstruction(Sequence prefix,TEXT *disassembly,
   TEXT *translation,LONGBITS critical_flags,LONGBITS possible_flags,
   LONGBITS definite_flags,UBYTE length,ULONG instruction_type)
{
   Instruction instruction=Malloc(sizeof(Instruction_imp));

   instruction->prefix=prefix;
   instruction->disassembly=disassembly;
   instruction->translation=translation;
   instruction->critical_flags=critical_flags;
   instruction->possible_flags=possible_flags;
   instruction->definite_flags=definite_flags;
   instruction->length=length;
   instruction->instruction_type=instruction_type;

   return instruction;
}


/* Function: ReadInstruction
 * =========================
 */

Instruction ReadInstruction(TEXT *string)
{
   Instruction instruction;
   TEXT prefix_buffer[TEXT_BUFFER_SIZE],*disassembly,*translation,
   *temp_string,*substring;
   ULONG instruction_type,i,length;
   LONGBITS critical_flags,possible_flags,definite_flags;
   Sequence prefix;
   UBYTE data_buffer[DATA_BUFFER_SIZE],*prefix_data,instruction_length;

   substring=temp_string=GetLine(&string);

   for(i=0;(substring=(TEXT *)strchr(substring,'$'))!=NULL;i++)
   {
      sscanf(substring,"%s",prefix_buffer);

      data_buffer[i]=StringToULong(prefix_buffer);
      substring++;
   }

   length=i;

   KillString(temp_string);

   prefix_data=Malloc(length*sizeof(UBYTE));

   for(i=0;i<length;i++)
      prefix_data[i]=data_buffer[i];

   prefix=CreateSequence(length,prefix_data);

   /* Get disassembly and translation strings */

   disassembly=GetLine(&string);

   translation=GetLine(&string);

   /* Get critical flags */

   temp_string=GetLine(&string);
   critical_flags=StringToULong(temp_string);
   KillString(temp_string);

   /* Get possibly modified flags */

   temp_string=GetLine(&string);
   possible_flags=StringToULong(temp_string);
   KillString(temp_string);

   /* Get definitely modified flags */

   temp_string=GetLine(&string);
   definite_flags=StringToULong(temp_string);
   KillString(temp_string);

   /* Get length of instruction */

   temp_string=GetLine(&string);
   instruction_length=StringToULong(temp_string);
   KillString(temp_string);

   /* Get type of instruction */

   temp_string=GetLine(&string);
   instruction_type=StringToULong(temp_string);
   KillString(temp_string);

   instruction=CreateInstruction(prefix,disassembly,translation,
      critical_flags,possible_flags,definite_flags,instruction_length,instruction_type);

   return instruction;
}


/* Function: GetInstructionPrefix
 * ==============================
 */

Sequence GetInstructionPrefix(Instruction instruction)
{
   return instruction->prefix;
}


/* Function: GetInstructionLength
 * ==============================
 */

UBYTE GetInstructionLength(Instruction instruction)
{
   return instruction->length;
}


/* Function: GetInstructionType
 * ============================
 */

ULONG GetInstructionType(Instruction instruction)
{
   return instruction->instruction_type;
}


/* Function: GetDisassemblyTemplate
 * ================================
 */

TEXT *GetDisassemblyTemplate(Instruction instruction)
{
   return instruction->disassembly;
}


/* Function: GetTranslationTemplate
 * ================================
 */

TEXT *GetTranslationTemplate(Instruction instruction)
{
   return instruction->translation;
}


/* Function: GetCriticalFlags
 * ==========================
 */

LONGBITS GetCriticalFlags(Instruction instruction)
{
   return instruction->critical_flags;
}


/* Function: GetModifiedFlags
 * ==========================
 */

LONGBITS GetModifiedFlags(Instruction instruction)
{
   return instruction->possible_flags|instruction->definite_flags;
}


/* Function: KillInstruction
 * =========================
 */

VOID KillInstruction(Instruction instruction)
{
   KillSequence(instruction->prefix);
   KillString(instruction->disassembly);
   KillString(instruction->translation);

   Free(instruction,sizeof(Instruction_imp));

   return;
}


#ifndef NDEBUG

/* Function: ShowInstruction
 * =========================
 */

VOID ShowInstruction(Instruction instruction)
{
   printf("Instruction %u:",instruction);
   printf("\tdisassembly=%s",instruction->disassembly);
   printf("\ttype=%u\t",instruction->instruction_type);
   ShowSequence(instruction->prefix);

   return;
}

#endif

