OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [orpmon/] [common/] [common.c] - Rev 816

Go to most recent revision | Compare with Previous | Blame | View Log

#include "common.h"
#include "uart.h"
#include "screen.h"
#include "support.h"
 
#define MAX_COMMANDS  100
 
bd_t bd;
 
int num_commands = 0;
 
struct command_struct {
  const char *name;
  const char *params;
  const char *help;
  int (*func)(int argc, char *argv[]);
} command[MAX_COMMANDS];
 
void putc (const char c)
{
  debug ("getc %i, %i = %c\n", bd.bi_console_type, c, c);
  switch (bd.bi_console_type) {
  case CT_NONE:
    break;
  case CT_UART:
    uart_putc (c);
    break;
  case CT_CRT:
    screen_putc (c);
    break;
  case CT_SIM:
    __printf ("%c", c);
    break;
  }
}
 
int getc ()
{
  debug ("getc %i\n", bd.bi_console_type);
  switch (bd.bi_console_type) {
  case CT_NONE:
  case CT_CRT:
  case CT_SIM:
    return -1;
  case CT_UART:
    return uart_getc ();
  }
  return -1;
}
 
int testc ()
{
  debug ("testc %i\n", bd.bi_console_type);
  switch (bd.bi_console_type) {
  case CT_NONE:
  case CT_CRT:
  case CT_SIM:
    return -1;
  case CT_UART:
    return uart_testc ();
  }
  return -1;
}
 
int ctrlc ()
{
  if (testc ()) {
    switch (getc ()) {
      case 0x03:    /* ^C - Control C */
        return 1;
      default:
        break;
    }
  }
  return 0;
}
 
unsigned long parse_ip (char *ip)
{
  unsigned long num;
  num = strtoul (ip, &ip, 10) & 0xff;
  if (*ip++ != '.') return 0;
  num = (num << 8) | (strtoul (ip, &ip, 10) & 0xff);
  if (*ip++ != '.') return 0;
  num = (num << 8) | (strtoul (ip, &ip, 10) & 0xff);
  if (*ip++ != '.') return 0;
  num = (num << 8) | (strtoul (ip, &ip, 10) & 0xff);
  return num;
}
 
void change_console_type (enum bi_console_type_t con_type)
{
  debug ("Console change %i -> %i\n", bd.bi_console_type, con_type);
  /* Close previous */
  switch (bd.bi_console_type) {
  case CT_NONE:
  case CT_UART:
  case CT_CRT:
  case CT_SIM:
    break;
  }
  bd.bi_console_type = con_type;
  /* Initialize new */
  switch (bd.bi_console_type) {
  case CT_NONE:
    break;
  case CT_UART:
    uart_init ();
    break;
  case CT_CRT:
    screen_init ();
    break;
  case CT_SIM:
    break;
  }
}
 
void register_command_func (const char *name, const char *params, const char *help, int (*func)(int argc, char *argv[]))
{
  debug ("register_command '%s'\n", name);
  if (num_commands < MAX_COMMANDS) {
    command[num_commands].name = name;
    command[num_commands].params = params;
    command[num_commands].help = help;
    command[num_commands].func = func;
    num_commands++;
  } else printf ("Command '%s' ignored; MAX_COMMANDS limit reached\n", name);
}
 
/* Process command and arguments by executing
   specific function. */
void mon_command(void)
{
  char c = '\0';
  char str[1000];
  char *pstr = str;
  char *command_str;
  char *argv[20];
  int argc = 0;
  int end = 0;
 
  /* Show prompt */
#ifdef XESS
  printf ("\norp-xsv> ");
#else
  printf ("\nbender> ");
#endif
 
  /* Get characters from UART */
  c = getc();
  while (c != '\r' && c != '\f' && c != '\n')
  {
    if (c == '\b')
      pstr--;
    else
      *pstr++ = c;
    putc(c);
    c = getc();
  }
  *pstr = '\0';
  printf ("\n");
 
  /* Skip leading blanks */
  pstr = str;
  while (*pstr == ' ' && *pstr != '\0') pstr++;
 
  /* Get command from the string */
  command_str = pstr;
  while (*pstr != '\0' && *pstr != ' ') pstr++;
  if (*pstr == '\0') end = 1;
  *pstr = '\0';
 
  while (!end) {
    /* Go to next argument */
    while (*pstr == ' ' && *pstr != '\0') pstr++;
    if (*pstr) argv[argc++] = pstr;
    else end = 1;
  }
 
  {
    int i, found = 0;
    for (i = 0; i < num_commands; i++)
      if (strcmp (command_str, command[i].name) == 0) {
        switch (command[i].func (argc, &argv[0])) {
        case -1:
          printf ("Missing/wrong parameters, usage: %s %s\n", command[i].name, command[i].params);
          break;
        }
        found = 1;
        break;
      }
    if (!found) printf ("Unknown command. Type 'help' for help.\n");
  }
}
 
#if HELP_ENABLED
/* Displays help screen */
int help_cmd (int argc, char *argv[])
{
  int i;
  for (i = 0; i < num_commands; i++)
    printf ("%-10s %-20s - %s\n", command[i].name, command[i].params, command[i].help);
  return 0;
}
#endif /* HELP_ENABLED */
 
void module_cpu_init (void);
void module_memory_init (void);
void module_eth_init (void);
void module_dhry_init (void);
void module_camera_init (void);
void module_load_init (void);
void tick_init(void);
 
/* List of all initializations */
void mon_init (void)
{
  /* Set defaults */
  global.erase_method = 2; /* as needed */
  global.src_addr = 
  global.dst_addr = FLASH_BASE_ADDR;
 
  /* Init modules */
  module_cpu_init ();
  module_memory_init ();
  module_eth_init ();
  module_dhry_init ();
  module_camera_init ();
  module_load_init ();
//  tick_init();
}
 
/* Main shell loop */
int main(int argc, char **argv)
{
  /* Initialize controller */
  change_console_type (CT_UART);
  mon_init ();
 
  if (HELP_ENABLED) register_command ("help", "", "shows this help", help_cmd);
 
#ifdef XESS
  printf ("\nORP-XSV Monitor (type 'help' for help)\n");
#else
  printf ("\nBender Monitor (type 'help' for help)\n");
#endif
 
  while(1) mon_command();
}
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.