Merge branch ucsim-034-pre3 to main trunk; new version 0.4
[fw/sdcc] / sim / ucsim / cmd.src / timer.cc
index 90f879608d7c4f48e125e54cb81ce954ac5655f5..021b08224e5f8b59fad92be9b142bfe01d40ca1e 100644 (file)
@@ -34,8 +34,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "simcl.h"
 
 // local
-#include "cmdsetcl.h"
-#include "newcmdcl.h"
+#include "timercl.h"
 
 
 /*
@@ -43,57 +42,59 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  *----------------------------------------------------------------------------
  */
 
-int
-cl_timer_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con)
+//int
+//cl_timer_cmd::do_work(class cl_sim *sim,
+//                   class cl_cmdline *cmdline, class cl_console *con)
+COMMAND_DO_WORK_UC(cl_timer_cmd)
 {
   char *s;
 
   if (cmdline->param(0) == 0)
     {
       if (long_help)
-       con->printf("%s\n", long_help);
+       con->dd_printf("%s\n", long_help);
       else
-       con->printf("What to do?\n");
+       con->dd_printf("What to do?\n");
       return(0);
     }
   if ((s= cmdline->param(0)->get_svalue()))
     {
       if (cmdline->param(1) == 0)
        {
-         con->printf("Timer number is missing\n");
+         con->dd_printf("Timer number is missing\n");
          return(0);
        }
-      set_ticker(cmdline->param(1));
+      set_ticker(uc, cmdline->param(1));
       if (strstr(s, "c") == s ||
          strstr(s, "m") == s ||
          strstr(s, "a") == s)
-       return(add(cmdline, con));
+       return(add(uc, cmdline, con));
       else if (strstr(s, "d") == s)
-       return(del(cmdline, con));
+       return(del(uc, cmdline, con));
       else if (strstr(s, "g") == s)
-       return(get(cmdline, con));
+       return(get(uc, cmdline, con));
       else if (strstr(s, "r") == s)
-       return(run(cmdline, con));
+       return(run(uc, cmdline, con));
       else if (strstr(s, "s") == s)
-       return(stop(cmdline, con));
+       return(stop(uc, cmdline, con));
       else if (strstr(s, "v") == s)
-       return(val(cmdline, con));
+       return(val(uc, cmdline, con));
       else
-       con->printf("Undefined timer command: \"%s\". Try \"help timer\"\n",
+       con->dd_printf("Undefined timer command: \"%s\". Try \"help timer\"\n",
                    s);
     }
   return(0);
 }
 
 void
-cl_timer_cmd::set_ticker(class cl_cmd_arg *param)
+cl_timer_cmd::set_ticker(class cl_uc *uc,
+                        class cl_cmd_arg *param)
 {
-  if ((name= param->get_svalue()) == 0)
-    what= param->get_ivalue();
-  if (name)
-    ticker= sim->uc->get_counter(name);
+  if ((name= param->get_svalue()))
+    ticker= uc->get_counter(name);
   else
-    ticker= sim->uc->get_counter(what);
+    if (param->get_ivalue(&what))
+      ticker= uc->get_counter(what);
 }
 
 /*
@@ -101,45 +102,54 @@ cl_timer_cmd::set_ticker(class cl_cmd_arg *param)
  */
 
 int
-cl_timer_cmd::add(class cl_cmdline *cmdline, class cl_console *con)
+cl_timer_cmd::add(class cl_uc *uc,
+                 class cl_cmdline *cmdline, class cl_console *con)
 {
   class cl_cmd_arg *params[4]= { cmdline->param(0),
                                 cmdline->param(1),
                                 cmdline->param(2),
                                 cmdline->param(3) };
-  int dir= +1, in_isr= 0;
+  long dir= +1, in_isr= 0;
   
   if (!name &&
       what < 1)
     {
-      con->printf("Timer id must be greater then zero or a string\n");
-      return(0);
+      con->dd_printf("Error: Timer id must be greater then zero or a string\n");
+      return(DD_FALSE);
     }
   if (ticker)
     {
       if (name)
-       con->printf("Timer \"%s\" already exists\n", name);
+       con->dd_printf("Error: Timer \"%s\" already exists\n", name);
       else
-       con->printf("Timer %d already exists\n", what);
+       con->dd_printf("Error: Timer %d already exists\n", what);
       return(0);
     }
-  if (params[2] != 0)
-    dir= (params[2])->get_ivalue();
-  if (params[3] != 0)
-    in_isr= (params[3])->get_ivalue();
+  if (params[2])
+    if (!params[2]->get_ivalue(&dir))
+      {
+       con->dd_printf("Error: Wrong direction\n");
+       return(DD_FALSE);
+      }
+  if (params[3])
+    if (!params[3]->get_ivalue(&in_isr))
+      {
+       con->dd_printf("Error: Wrong parameter\n");
+       return(DD_FALSE);
+      }
 
   if (name)
     {
       ticker= new cl_ticker(dir, in_isr, name);
-      sim->uc->add_counter(ticker, name);
+      uc->add_counter(ticker, name);
     }
   else
     {
       ticker= new cl_ticker(dir, in_isr, 0);
-      sim->uc->add_counter(ticker, what);
+      uc->add_counter(ticker, what);
     }
 
-  return(0);
+  return(DD_FALSE);
 }
 
 /*
@@ -147,20 +157,21 @@ cl_timer_cmd::add(class cl_cmdline *cmdline, class cl_console *con)
  */
 
 int
-cl_timer_cmd::del(class cl_cmdline *cmdline, class cl_console *con)
+cl_timer_cmd::del(class cl_uc *uc,
+                 class cl_cmdline *cmdline, class cl_console *con)
 {
   if (!ticker)
     {
       if (name)
-       con->printf("Timer \"%s\" does not exist\n", name);
+       con->dd_printf("Timer \"%s\" does not exist\n", name);
       else
-       con->printf("Timer %d does not exist\n", what);
+       con->dd_printf("Timer %d does not exist\n", what);
       return(0);
     }
   if (name)
-    sim->uc->del_counter(name);
+    uc->del_counter(name);
   else
-    sim->uc->del_counter(what);
+    uc->del_counter(what);
 
   return(0);
 }
@@ -170,20 +181,21 @@ cl_timer_cmd::del(class cl_cmdline *cmdline, class cl_console *con)
  */
 
 int
-cl_timer_cmd::get(class cl_cmdline *cmdline, class cl_console *con)
+cl_timer_cmd::get(class cl_uc *uc,
+                 class cl_cmdline *cmdline, class cl_console *con)
 {
   if (ticker)
-    ticker->dump(what, sim->uc->xtal, con);
+    ticker->dump(what, uc->xtal, con);
   else
     {
-      sim->uc->ticks->dump(0, sim->uc->xtal, con);
-      sim->uc->isr_ticks->dump(0, sim->uc->xtal, con);
-      sim->uc->idle_ticks->dump(0, sim->uc->xtal, con);
-      for (what= 0; what < sim->uc->counters->count; what++)
+      uc->ticks->dump(0, uc->xtal, con);
+      uc->isr_ticks->dump(0, uc->xtal, con);
+      uc->idle_ticks->dump(0, uc->xtal, con);
+      for (what= 0; what < uc->counters->count; what++)
        {
-         ticker= sim->uc->get_counter(what);
+         ticker= uc->get_counter(what);
          if (ticker)
-           ticker->dump(what, sim->uc->xtal, con);
+           ticker->dump(what, uc->xtal, con);
        }
     }
 
@@ -195,14 +207,15 @@ cl_timer_cmd::get(class cl_cmdline *cmdline, class cl_console *con)
  */
 
 int
-cl_timer_cmd::run(class cl_cmdline *cmdline, class cl_console *con)
+cl_timer_cmd::run(class cl_uc *uc,
+                 class cl_cmdline *cmdline, class cl_console *con)
 {
   if (!ticker)
     {
       if (name)
-       con->printf("Timer %d does not exist\n", name);
+       con->dd_printf("Timer %d does not exist\n", name);
       else
-       con->printf("Timer %d does not exist\n", what);
+       con->dd_printf("Timer %d does not exist\n", what);
       return(0);
     }
   ticker->options|= TICK_RUN;
@@ -215,14 +228,15 @@ cl_timer_cmd::run(class cl_cmdline *cmdline, class cl_console *con)
  */
 
 int
-cl_timer_cmd::stop(class cl_cmdline *cmdline, class cl_console *con)
+cl_timer_cmd::stop(class cl_uc *uc,
+                  class cl_cmdline *cmdline, class cl_console *con)
 {
   if (!ticker)
     {
       if (name)
-       con->printf("Timer %d does not exist\n", name);
+       con->dd_printf("Timer %d does not exist\n", name);
       else
-       con->printf("Timer %d does not exist\n", what);
+       con->dd_printf("Timer %d does not exist\n", what);
       return(0);
     }
   ticker->options&= ~TICK_RUN;
@@ -236,7 +250,8 @@ cl_timer_cmd::stop(class cl_cmdline *cmdline, class cl_console *con)
  */
 
 int
-cl_timer_cmd::val(class cl_cmdline *cmdline, class cl_console *con)
+cl_timer_cmd::val(class cl_uc *uc,
+                 class cl_cmdline *cmdline, class cl_console *con)
 {
   class cl_cmd_arg *params[4]= { cmdline->param(0),
                                 cmdline->param(1),
@@ -246,20 +261,25 @@ cl_timer_cmd::val(class cl_cmdline *cmdline, class cl_console *con)
   if (!ticker)
     {
       if (name)
-       con->printf("Timer %d does not exist\n", name);
+       con->dd_printf("Error: Timer %d does not exist\n", name);
       else
-       con->printf("Timer %d does not exist\n", what);
+       con->dd_printf("Error: Timer %d does not exist\n", what);
       return(0);
     }
-  if (params[2] == 0)
+  if (params[2])
     {
-      con->printf("Value is missing\n");
-      return(0);
+      con->dd_printf("Error: Value is missing\n");
+      return(DD_FALSE);
+    }
+  long val;
+  if (!params[2]->get_ivalue(&val))
+    {
+      con->dd_printf("Error: Wrong parameter\n");
+      return(DD_FALSE);
     }
-  long val= (params[2])->get_ivalue();
   ticker->ticks= val;
 
-  return(0);
+  return(DD_FALSE);
 }