/*

File: disassembled_program.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 "disassembled_program_protos.h"
#include "source_program_protos.h"
#include "string_protos.h"
#include "address_set_protos.h"
#include "set_protos.h"
#include "address_protos.h"
#include "reverse_graph_protos.h"
#include "bank_protos.h"
#include <stdio.h>


/* Function: WriteDisassembledProgram
 * ==================================
 */

VOID WriteDisassembledProgram(SourceProgram src_prog,TEXT *directory_name)
{
   Address address;
   Set bank_set=GetSourceProgramBanks(src_prog);
   Bank bank;
   AddressSet rev_adj;
   ULONG base,end,i,bank_count;
   FILE *file;
   BOOL first_instruction;

   TEXT *prog_name=GetSourceProgramName(src_prog);
   TEXT *path=CreateString(strlen(directory_name)+strlen(prog_name)
      +strlen(".asm")+3);

   /* Write disassembly file for each bank */

   for(bank_count=0;(bank=TakeFromSet(bank_set))!=NULL;bank_count++)
   {

      /* Get file path */

      sprintf(path,"%s%s%03u%s",directory_name,prog_name,bank_count,".asm");

      /* Create disassembly file */

      file=fopen(path,"w");

      /* Get start and end of this bank */

      base=GetBankBase(bank);
      end=base+GetBankLength(bank);

      /* Print origin at top of file */

      fprintf(file,"\n\tprocessor\t6502\n\n\tinclude\t\"vcs.i\"\n\n");
      fprintf(file,"\n\torg $%04x\n",base);

      first_instruction=TRUE;

      /* Print instructions and data from this bank */

      for(i=base;i<end;i++)
      {
         address=GetBankAddress(bank,i);

         /* Print data sequence */

         if(address==NULL)
         {
            fprintf(file,"\n\n");
            for(;(address==NULL)&&(i<end);address=GetBankAddress(bank,++i))
               fprintf(file,".$%04x\tdc.b\t$%02x\n",i,
                  *GetBankContents(bank,i));
            i--;
            fprintf(file,"\n\n");
            first_instruction=TRUE;
         }

         /* Print a single instruction */

         else
         {
            rev_adj=GetReverseAdjacent(GetSourceProgramReverseGraph(src_prog),
               address);
            fprintf(file,".$%04x",i);
            first_instruction=FALSE;
            KillSet(rev_adj);
            fprintf(file,"\t%s\n",GetAddressDisassembly(address));

            i+=GetAddressLength(address)-1;
         }
      }

      fclose(file);
   }


   KillSet(bank_set);
   KillString(path);

   return;
}


/* Function: KillDisassembledProgram
 * =================================
 */
#if 0
VOID KillDisassembledProgram(DisassembledProgram disassembled_prog)
{
   return;
}
#endif

