/*

File: machine.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 "machine_protos.h"
#include "memory_protos.h"
#include "string_protos.h"
#include "cpu_protos.h"
#include "instruction_set_protos.h"
#include "config_protos.h"


/* Function: CreateMachine
 * =======================
 */

Machine CreateMachine(TEXT *name,CPU cpu,LONGBITS address_mask,
   InstructionSet instructions,ULONG entry_vector)
{
   Machine machine=Malloc(sizeof(Machine_imp));

   machine->name=name;
   machine->cpu=cpu;
   machine->address_mask=address_mask;
   machine->instructions=instructions;
   machine->entry_vector=entry_vector;

   return machine;
}


/* Function: ReadMachine
 * =====================
 */
#if 0
Machine ReadMachine(TEXT *file_name)
{
   return CreateMachine(file_name,ReadCPU("MOS6507"),0x1fff,
      CreateSet());
}
#endif

/* Function: ReadMachine
 * =====================
 */

Machine ReadMachine(TEXT *file_name)
{
   Config config=ReadConfig(file_name);
   TEXT *name=GetStringConfigOption(config,"Name:");
   CPU cpu=ReadCPU(GetStringConfigOption(config,"CPU:"));
   LONGBITS mask=(LONGBITS)GetNumericConfigOption(config,"Address Mask:");
   InstructionSet instructions=
      ReadInstructionSet(GetStringSetConfigOption(config,"Instructions:"));
   ULONG entry_vector=GetNumericConfigOption(config,"Entry Vector:");

   KillConfig(config);

   return CreateMachine(name,cpu,mask,instructions,entry_vector);
}


/* Function: GetMachineInstruction
 * ===============================
 */

Instruction GetMachineInstruction(Machine machine,Address address,
   SourceProgram src_prog)
{
   Instruction instruction=GetMatchingInstruction(machine->instructions,
   address,src_prog);

   if(instruction==NULL)
      instruction=GetCPUInstruction(machine->cpu,address,src_prog);

   return instruction;
}


/* Function: GetMachineAddressMask
 * ===============================
 */

LONGBITS GetMachineAddressMask(Machine machine)
{
   return(machine->address_mask);
}


/* Function: GetMachineEntryVector
 * ===============================
 */

ULONG GetMachineEntryVector(Machine machine)
{
   return machine->entry_vector;
}


/* Function: GetMachineInterruptVector
 * ===================================
 */

ULONG GetMachineInterruptVector(Machine machine)
{
   return GetCPUInterruptVector(machine->cpu)&machine->address_mask;
}


/* Function: KillMachine
 * =====================
 */

VOID KillMachine(Machine machine)
{
   KillString(machine->name);
   KillCPU(machine->cpu);
   KillSet(machine->instructions);
   Free(machine,sizeof(Machine_imp));

   return;
}


