/* cmd.c - top level commands and auto startup.
 *  Copyright (C) 2022 by Jed Marti
 *   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 3 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, see https://www.gnu.org/licenses/.
 */


#include <C8051F380.h>
#include <stdint.h>

#include "lex.h"
#include "lfxmit.h"
#include "serial.h"
#include "strings.h"
#include "fs.h"
#include "storage.h"
#include "ds1085.h"

// Indicates error occurred during execution.
extern __bit errorThrown;

// Lexical interface.
extern __xdata char    STR[];
extern __xdata uint8_t ttype;

// Debug stuff.
//void setDAC(void);
//void incoff(void);
//void decoff(void);


static void help(void)
{
  putstring("Commands\n");
  putstring("  carrier on off  Turn the carrier on or orff\n");
  putstring("  dir             List directory (ls)\n");
  putstring("  del fname       Delete file (rm fname)\n");
  putstring("  download fname  Download file\n");
  putstring("  format          Erase file system\n");
  putstring("  freq n          Set frequency in hertz\n");
  putstring("  gc              Run garbage collector\n");
  putstring("  more fname      List file name\n");
  putstring("  reset           Reset to starting state\n");
  putstring("  run fname       Run program\n");
  putstring("  send            Send a file as Morse code\n");
}

  
void cmd(void)
//     Top level command loop. Read from terminal and get things starteed. A mix of parsing
// and actual work. Varying levels of debugging commands.
{

  // Master loop, get a new line.
 loop:
  errorThrown = 0;
  newLine();
  ntok();

  // Blank lines, continue.
  if (ttype == LEXEOL) goto loop;

  // ? means help.
  if (ttype == LEXPUNCT && strCaseEq("?")) help();

  // Debug code for offset.
  //  else if (ttype == LEXPUNCT && strCaseEq("+")) incoff();
  //  else if (ttype == LEXPUNCT && strCaseEq("-")) decoff();
  
  // Must be an identifier.
  else if (ttype != LEXID) putstring("???\n");

  // File system commands.
  else if (strCaseEq("ls") || strCaseEq("dir")) listDir();
  else if (strCaseEq("format")) formatFS();
  else if (strCaseEq("rm") || strCaseEq("del"))
  {
    ntok();
    if (ttype != LEXID && ttype != LEXSTRING)
    {
      putstring("Missing file name\n");
      goto loop;
    }
    if (rmFS(STR)) putstring("No such file\n");
  }

  // Useful for debugging.
  //  else if (strCaseEq("load")) loadProgram();
  
  else if (strCaseEq("run")) runProgram();
  else if (strCaseEq("download")) download();

  // Debugging of garbage collector.
  //  else if (strCaseEq("gc")) gc();
  
  else if (strCaseEq("more")) more();
  else if (strCaseEq("send")) sendFile();

  else if (strCaseEq("freq")) setFrequency();
  else if (strCaseEq("carrier")) runCarrier();

  // Do a system reset.
  else if (strCaseEq("reset")) RSTSRC = 0x10;

  // Debug commands.
  //  else if (strCaseEq("rst"))
  //    { putstring("RSTSRC = 0x"); puthexbyte(RSTSRC); terpri();  RSTSRC |= 0x10; }
  //  else if (strCaseEq("r")) dumpRAM();
   else if (strCaseEq("i")) dumpProgram();
  //  else if (strCaseEq("dac")) setDAC();
  //  else if (strCaseEq("ds")) readDS1085();
  //  else if (strCaseEq("s")) readSector();
  //  else if (strCaseEq("w")) writeSector();
  //  else if (strCaseEq("f")) writeFAT();
  //  else if (strCaseEq("e")) spiErase();

  // Unknown command.
  else
  {
    putch('\'');
    putstring(STR);
    putstring("' ???\n");
  }
  
  goto loop;
}



// EOF
