
/*
	GIER simulator - Generate tape readable by track 0 loader

	(C) Copyright 2001-2024 by Mogens Kjaer


   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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "GIER.h"

#define MARKa ONE40
#define MARKb ONE41
#define MARKc ONE40_41
#define MARKS ONE40_41

static GIERword Tromle[960*40*10],Ferrit[1024],c;

char *opcodes[]=
  {"QQ", "ZQ", "AR", "SR", "AN", "SN", "AC", "SC",
   "MB", "AB", "MT", "MK", "ML", "DK", "DL", "NK",
   "NL", "HR", "TL", "CK", "CL", "GR", "GA", "GT",
   "TK", "CA", "GM", "PM", "XR", "GI", "PS", "PP",
   "PA", "PT", "HK", "PI", "IS", "IT", "CM", "BT",
   "NS", "NT", "GP", "NC", "IL", "US", "GG", "GC",
   "PC", "BS", "HS", "VY", "LK", "SK", "GK", "VK",
   "HV", "ZJ", "SY", "LY", "HH", "GS", "ZL", "UD"};

extern unsigned char set_parity(unsigned char);

static void SY(unsigned char c)
{
  c=set_parity(c);
  printf("%c",c);
}

static void print_usage()
{
  fprintf(stderr, "Usage: core2boot -1 -3 file.gier fromcell tocell fromtrack totrack\n");
  exit(-1);
}

int main(int argc, char **argv)
{
  FILE *fp;
  char buf[1024];
  char ch1,ch2;
  int uc,i,kind,s,opt,helpmode;
  char *bp,*cp;
  int track,ncells,firstcell,lastcell,firsttrack,lasttrack,cell,drummode;
  GIERword c;

  optind=0;
  helpmode=3;
  while((opt=getopt(argc,argv,"13"))!=-1)
  {
    switch(opt)
    {
      case '1':
	helpmode=1;
	break;
      case '3':
	helpmode=3;
	break;
      default:
	print_usage();
	break;
    }
  }
  printf("optind: %d argc: %d\n",optind,argc);

  if(optind>=argc)
  {
    print_usage();
  }

  fp = fopen(argv[optind++], "r");

  if(fp == NULL)
  {
    fprintf(stderr, "Could not open %s for reading.\n", argv[optind-1]);
    exit(-1);
  }
  firstcell=0;
  lastcell=1023;
  firsttrack=-1;
  lasttrack=-1;

  if(optind<argc) firstcell=atoi(argv[optind++]);
  if(optind<argc) lastcell=atoi(argv[optind++]);
  if(optind<argc) firsttrack=atoi(argv[optind++]);
  if(optind<argc) lasttrack=atoi(argv[optind++]);
//printf("firstcell: %d lastcell: %d firsttrack: %d lasttrack: %d\n",firstcell,lastcell,firsttrack,lasttrack);
//exit(0);

  for(i=0;i<40*960*10;i++) Tromle[i] = 0ULL;
  for(i=0;i<1024;i++) Ferrit[i] = 0ULL;

  while(bp = fgets(buf, sizeof(buf), fp))
  {
    cp=strtok(bp, "\t\n");
    if(!cp) continue;

    if(!strncmp(cp, "Tromle[", 7))
    {
      sscanf(cp+7, "%d", &i);
      cp=strtok(0, "\t\n");
      if(i<960*40*10)
      {
#ifdef WINDOWS
	sscanf(cp, "%I64x", Tromle+i);
#else
	sscanf(cp, "%llx", Tromle+i);
#endif
      }
    }
    else if(!strncmp(cp, "Ferrit[", 7))
    {
      sscanf(cp+7, "%d", &i);
      cp=strtok(0, "\t\n");
#ifdef WINDOWS
      sscanf(cp, "%I64x", Ferrit+i);
#else
      sscanf(cp, "%llx", Ferrit+i);
#endif
    }

  }
  fclose(fp);

  drummode=lasttrack!=-1;
  if(drummode)
  {
    firstcell+=firsttrack*40;
    lastcell+=lasttrack*40;
  }
  ncells=lastcell-firstcell+1;
  for(cell=lastcell;cell>=firstcell;cell--)
  {
    if(drummode)
    {
      c=Tromle[cell];
    }
    else
    {
      c=Ferrit[cell];
    }
  }
}
