2 * Simulator of microcontrollers (uc.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
48 #include "cmdstatcl.h"
64 cl_ticker::cl_ticker(int adir, int in_isr, char *aname)
74 cl_ticker::~cl_ticker(void) {}
77 cl_ticker::tick(int nr)
85 cl_ticker::get_rtime(double xtal)
89 d= (double)ticks/xtal;
94 cl_ticker::dump(int nr, double xtal, class cl_console *con)
96 con->dd_printf("timer #%d(\"%s\") %s%s: %g sec (%lu clks)\n",
97 nr, get_name("unnamed"),
98 (options&TICK_RUN)?"ON":"OFF",
99 (options&TICK_INISR)?",ISR":"",
100 get_rtime(xtal), ticks);
108 cl_xtal_option::cl_xtal_option(class cl_uc *the_uc):
115 cl_xtal_option::option_changed(void)
120 option->get_value(&d);
126 * Abstract microcontroller
127 ******************************************************************************
130 cl_uc::cl_uc(class cl_sim *asim):
135 //mems= new cl_list(MEM_TYPES, 1);
136 memchips= new cl_list(2, 2, "memchips");
137 address_spaces= new cl_address_space_list(this);
138 //address_decoders= new cl_list(2, 2);
142 //options= new cl_list(2, 2);
143 //for (i= MEM_ROM; i < MEM_TYPES; i++) mems->add(0);
144 xtal_option= new cl_xtal_option(this);
146 ticks= new cl_ticker(+1, 0, "time");
147 isr_ticks= new cl_ticker(+1, TICK_INISR, "isr");
148 idle_ticks= new cl_ticker(+1, TICK_IDLE, "idle");
149 counters= new cl_list(2, 2, "counters");
150 it_levels= new cl_list(2, 2, "it levels");
151 it_sources= new cl_irqs(2, 2);
152 class it_level *il= new it_level(-1, 0, 0, 0);
154 stack_ops= new cl_list(2, 2, "stack operations");
155 errors= new cl_list(2, 2, "errors in uc");
156 events= new cl_list(2, 2, "events in uc");
172 events->disconn_all();
182 delete address_spaces;
184 //delete address_decoders;
193 set_name("controller");
195 if (xtal_option->use("xtal"))
196 xtal= xtal_option->get_value(xtal);
200 rom= address_space(MEM_ROM_ID);
201 ebrk= new brk_coll(2, 2, rom);
202 fbrk= new brk_coll(2, 2, rom);
203 fbrk->Duplicates= DD_FALSE;
207 class cl_cmdset *cs= sim->app->get_commander()->cmdset;
210 for (i= 0; i < sim->app->in_files->count; i++)
212 char *fname= (char *)(sim->app->in_files->at(i));
214 if ((l= read_hex_file(fname)) >= 0)
216 sim->app->get_commander()->all_printf("%ld words read from %s\n",
224 cl_uc::id_string(void)
226 return("unknown microcontroller");
238 idle_ticks->ticks= 0;
239 /*FIXME should we clear user counters?*/
240 il= (class it_level *)(it_levels->top());
244 il= (class it_level *)(it_levels->pop());
246 il= (class it_level *)(it_levels->top());
251 stack_ops->free_all();
254 for (i= 0; i < hws->count; i++)
256 class cl_hw *hw= (class cl_hw *)(hws->at(i));
266 cl_uc::make_memories(void)
270 cl_uc::get_mem_size(char *id)
272 class cl_memory *m= memory(id);
273 return(m?(m->get_size()):0);
277 cl_uc::get_mem_width(char *id)
279 class cl_memory *m= memory(id);
280 return(m?(m->width):8);
284 cl_uc::mk_hw_elements(void)
289 cl_uc::build_cmdset(class cl_cmdset *cmdset)
292 class cl_super_cmd *super_cmd;
293 class cl_cmdset *cset;
295 cmdset->add(cmd= new cl_state_cmd("state", 0,
296 "state State of microcontroller",
297 "long help of state"));
301 cmdset->add(cmd= new cl_statistic_cmd("statistic", 0,
302 "statistic [mem [startaddr [endaddr]]]\n"
303 " Statistic of memory accesses",
304 "long help of statistic"));
308 cmdset->add(cmd= new cl_file_cmd("file", 0,
309 "file \"FILE\" Load FILE into ROM",
310 "long help of file"));
312 cmd->add_name("load");
314 cmdset->add(cmd= new cl_dl_cmd("download", 0,
315 "download Load (intel.hex) data",
316 "long help of download"));
320 cmdset->add(cmd= new cl_pc_cmd("pc", 0,
321 "pc [addr] Set/get PC",
325 cmdset->add(cmd= new cl_reset_cmd("reset", 0,
327 "long help of reset"));
330 cmdset->add(cmd= new cl_dump_cmd("dump", DD_TRUE,
331 "dump memory_type [start [stop [bytes_per_line]]]\n"
332 " Dump memory of specified type\n"
333 "dump bit... Dump bits",
334 "long help of dump"));
337 cmdset->add(cmd= new cl_dch_cmd("dch", DD_TRUE,
338 "dch [start [stop]] Dump code in hex form",
339 "long help of dch"));
342 cmdset->add(cmd= new cl_dc_cmd("dc", DD_TRUE,
343 "dc [start [stop]] Dump code in disass form",
347 cmdset->add(cmd= new cl_disassemble_cmd("disassemble", DD_TRUE,
348 "disassemble [start [offset [lines]]]\n"
350 "long help of disassemble"));
353 cmdset->add(cmd= new cl_fill_cmd("fill", 0,
354 "fill memory_type start end data\n"
355 " Fill memory region with data",
356 "long help of fill"));
359 cmdset->add(cmd= new cl_where_cmd("where", 0,
360 "where memory_type data...\n"
361 " Case unsensitive search for data",
362 "long help of where"));
365 cmdset->add(cmd= new cl_Where_cmd("Where", 0,
366 "Where memory_type data...\n"
367 " Case sensitive search for data",
368 "long help of Where"));
371 cmdset->add(cmd= new cl_break_cmd("break", 0,
372 "break addr [hit] Set fix breakpoint\n"
373 "break mem_type r|w addr [hit]\n"
374 " Set fix event breakpoint",
375 "long help of break"));
378 cmdset->add(cmd= new cl_tbreak_cmd("tbreak", 0,
379 "tbreak addr [hit] Set temporary breakpoint\n"
380 "tbreak mem_type r|w addr [hit]\n"
381 " Set temporary event breakpoint",
382 "long help of tbreak"));
385 cmdset->add(cmd= new cl_clear_cmd("clear", 0,
386 "clear [addr...] Clear fix breakpoint",
387 "long help of clear"));
390 cmdset->add(cmd= new cl_delete_cmd("delete", 0,
391 "delete [nr...] Delete breakpoint(s)",
392 "long help of clear"));
396 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("get"));
398 cset= super_cmd->commands;
400 cset= new cl_cmdset();
403 cset->add(cmd= new cl_get_sfr_cmd("sfr", 0,
404 "get sfr address...\n"
405 " Get value of addressed SFRs",
406 "long help of get sfr"));
408 /*cset->add(cmd= new cl_get_option_cmd("option", 0,
410 " Get value of an option",
411 "long help of get option"));
416 cmdset->add(cmd= new cl_super_cmd("get", 0,
417 "get subcommand Get, see `get' command for more help",
418 "long help of get", cset));
423 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("set"));
425 cset= super_cmd->commands;
427 cset= new cl_cmdset();
430 cset->add(cmd= new cl_set_mem_cmd("memory", 0,
431 "set memory memory_type address data...\n"
432 " Place list of data into memory",
433 "long help of set memory"));
435 cset->add(cmd= new cl_set_bit_cmd("bit", 0,
436 "set bit addr 0|1 Set specified bit to 0 or 1",
437 "long help of set bit"));
439 cset->add(cmd= new cl_set_hw_cmd("hardware", 0,
440 "set hardware cathegory params...\n"
441 " Set parameters of specified hardware element",
442 "long help of set hardware"));
448 cmdset->add(cmd= new cl_super_cmd("set", 0,
449 "set subcommand Set, see `set' command for more help",
450 "long help of set", cset));
455 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("info"));
457 cset= super_cmd->get_subcommands();
459 cset= new cl_cmdset();
462 cset->add(cmd= new cl_info_bp_cmd("breakpoints", 0,
463 "info breakpoints Status of user-settable breakpoints",
464 "long help of info breakpoints"));
467 cset->add(cmd= new cl_info_reg_cmd("registers", 0,
468 "info registers List of integer registers and their contents",
469 "long help of info registers"));
471 cset->add(cmd= new cl_info_hw_cmd("hardware", 0,
472 "info hardware cathegory\n"
473 " Status of hardware elements of the CPU",
474 "long help of info hardware"));
477 cset->add(cmd= new cl_info_stack_cmd("stack", 0,
478 "info stack Status of stack of the CPU",
479 "long help of info stack"));
481 cset->add(cmd= new cl_info_memory_cmd("memory", 0,
482 "info memory Information about memory system",
483 "long help of info memory"));
487 cmdset->add(cmd= new cl_super_cmd("info", 0,
488 "info subcommand Information, see `info' command for more help",
489 "long help of info", cset));
494 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("timer"));
496 cset= super_cmd->get_subcommands();
498 cset= new cl_cmdset();
501 cset->add(cmd= new cl_timer_add_cmd("add", 0,
502 "timer add id [direction [in_isr]]\n"
503 " Create a clock counter (timer)",
504 "log help of timer add"));
506 cmd->add_name("create");
507 cmd->add_name("make");
508 cset->add(cmd= new cl_timer_delete_cmd("delete", 0,
509 "timer delete id Delete a timer",
510 "long help of timer delete"));
512 cmd->add_name("remove");
513 cset->add(cmd= new cl_timer_get_cmd("get", 0,
514 "timer get [id] Get value of a timer, or all",
515 "long help of timer get"));
517 cset->add(cmd= new cl_timer_run_cmd("run", 0,
518 "timer start id Start a timer",
519 "long help of timer run"));
521 cmd->add_name("start");
522 cset->add(cmd= new cl_timer_stop_cmd("stop", 0,
523 "timer stop id Stop a timer",
524 "long help of timer stop"));
526 cset->add(cmd= new cl_timer_value_cmd("set", 0,
527 "timer set id value\n"
528 " Set a timer value",
529 "long help of timer set"));
531 cmd->add_name("value");
534 cmdset->add(cmd= new cl_super_cmd("timer", 0,
535 "timer subcommand Manage timers",
536 "long help of timer", cset));
541 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("memory"));
543 cset= super_cmd->get_subcommands();
545 cset= new cl_cmdset();
549 cset->add(cmd= new cl_memory_cmd("_no_parameters_", 0,
550 "memory Information about memory system",
551 "long help of memory"));
554 cset->add(cmd= new cl_memory_createchip_cmd("createchip", 0,
555 "memory createchip id size cellsize\n"
556 " Create a new memory chip",
557 "long help of memory createchip"));
559 cmd->add_name("cchip");
560 cset->add(cmd= new cl_memory_createaddressspace_cmd("createaddressspace", 0,
561 "memory createaddressspace id startaddr size\n"
562 " Create address space",
563 "long help of memory createaddressspace"));
565 cmd->add_name("createaddrspace");
566 cmd->add_name("createaspace");
567 cmd->add_name("caddressspace");
568 cmd->add_name("caddrspace");
569 cmd->add_name("caspace");
570 cset->add(cmd= new cl_memory_createaddressdecoder_cmd("createaddressdecoder", 0,
571 "memory createaddressdecoder addressspace begin end chip begin\n"
572 " Create address decoder",
573 "long help of memory createaddressdecoder"));
575 cmd->add_name("createaddrdecoder");
576 cmd->add_name("createadecoder");
577 cmd->add_name("caddressdecoder");
578 cmd->add_name("caddrdecoder");
579 cmd->add_name("cadecoder");
582 cmdset->add(cmd= new cl_super_cmd("memory", 0,
583 "memory subcommand Manage memory chips and address spaces",
584 "long help of memory", cset));
591 * Read/write simulated memory
595 cl_uc::read_mem(char *id, t_addr addr)
597 class cl_address_space *m= address_space(id);
599 return(m?(m->read(addr)):0);
603 cl_uc::get_mem(char *id, t_addr addr)
605 class cl_address_space *m= address_space(id);
607 return(m?(m->get(addr)):0);
611 cl_uc::write_mem(char *id, t_addr addr, t_mem val)
613 class cl_address_space *m= address_space(id);
620 cl_uc::set_mem(char *id, t_addr addr, t_mem val)
622 class cl_address_space *m= address_space(id);
631 cl_uc::mem(enum mem_class type)
635 if (mems->count < type)
636 m= (class cl_m *)(mems->at(MEM_DUMMY));
638 m= (class cl_m *)(mems->at(type));
643 class cl_address_space *
644 cl_uc::address_space(char *id)
651 for (i= 0; i < address_spaces->count; i++)
653 class cl_address_space *m= (cl_address_space *)(address_spaces->at(i));
655 !m->have_real_name())
657 if (m->is_inamed(id))
664 cl_uc::memory(char *id)
671 for (i= 0; i < address_spaces->count; i++)
673 class cl_memory *m= (cl_memory *)(address_spaces->at(i));
675 !m->have_real_name())
677 if (m->is_inamed(id))
680 for (i= 0; i < memchips->count; i++)
682 class cl_memory *m= (cl_memory *)(memchips->at(i));
684 !m->have_real_name())
686 if (m->is_inamed(id))
694 ReadInt(FILE *f, bool *ok, int bytes)
702 if (fscanf(f, "%2c", &s2[0]) == EOF)
705 l= l*256 + strtol(s2, NULL, 16);
714 * Reading intel hexa file into EROM
715 *____________________________________________________________________________
717 * If parameter is a NULL pointer, this function reads data from `cmd_in'
722 cl_uc::read_hex_file(const char *nam)
726 long written= 0, recnum= 0;
728 uchar dnum; // data number
729 uchar rtyp=0; // record type
730 uint addr= 0; // address
731 uchar rec[300]; // data record
732 uchar sum ; // checksum
740 sim->app->get_commander()->
741 dd_printf("No ROM address space to read in.\n");
747 sim->app->get_commander()->
748 dd_printf("cl_uc::read_hex_file File name not specified\n");
752 if ((f= fopen(nam, "r")) == NULL)
754 fprintf(stderr, "Can't open `%s': %s\n", nam, strerror(errno));
758 //memset(inst_map, '\0', sizeof(inst_map));
763 while (((c= getc(f)) != ':') &&
766 {fprintf(stderr, ": not found\n");break;}
768 dnum= ReadInt(f, &ok, 1);//printf("dnum=%02x",dnum);
770 addr= ReadInt(f, &ok, 2);//printf("addr=%04x",addr);
772 chk+= ((addr >> 8) & 0xff);
773 rtyp= ReadInt(f, &ok, 1);//printf("rtyp=%02x ",rtyp);
775 for (i= 0; ok && (i < dnum); i++)
777 rec[i]= ReadInt(f, &ok, 1);//printf("%02x",rec[i]);
782 sum= ReadInt(f, &ok, 1);//printf(" sum=%02x\n",sum);
785 if (((sum + chk) & 0xff) == 0)
791 for (i= 0; i < dnum; i++)
795 rom->set(addr, rec[i]);
799 else if (rom->width <= 16)
809 rom->set(addr, (high*256)+low);
819 application->debug("Unknown record type %d(0x%x)\n",
823 application->debug("Checksum error (%x instead of %x) in "
824 "record %ld.\n", chk, sum, recnum);
827 application->debug("Read error in record %ld.\n", recnum);
830 if (rom->width > 8 &&
836 application->debug("%ld records have been read\n", recnum);
843 * Handling instruction map
845 * `inst_at' is checking if the specified address is in instruction
846 * map and `set_inst_at' marks the address in the map and
847 * `del_inst_at' deletes the mark. `there_is_inst' cheks if there is
848 * any mark in the map
852 cl_uc::inst_at(t_addr addr)
856 return(rom->get_cell_flag(addr, CELL_INST));
860 cl_uc::set_inst_at(t_addr addr)
863 rom->set_cell_flag(addr, DD_TRUE, CELL_INST);
867 cl_uc::del_inst_at(t_addr addr)
870 rom->set_cell_flag(addr, DD_FALSE, CELL_INST);
874 cl_uc::there_is_inst(void)
880 for (addr= 0; rom->valid_address(addr) && !got; addr++)
881 got= rom->get_cell_flag(addr, CELL_INST);
887 * Manipulating HW elements of the CPU
888 *****************************************************************************
891 /* Register callback hw objects for mem read/write */
894 cl_uc::register_hw_read(enum mem_class type, t_addr addr, class cl_hw *hw)
899 if ((m= (class cl_m*)mems->at(type)))
901 if ((l= m->read_locs->get_loc(addr)) == 0)
903 l= new cl_memloc(addr);
905 m->read_locs->add(l);
910 printf("cl_uc::register_hw_read TROUBLE\n");
914 cl_uc::register_hw_write(enum mem_class type, t_addr addr, class cl_hw *hw)
918 /* Looking for a specific HW element */
921 cl_uc::get_hw(enum hw_cath cath, int *idx)
928 for (; i < hws->count; i++)
930 hw= (class cl_hw *)(hws->at(i));
931 if (hw->cathegory == cath)
942 cl_uc::get_hw(char *id_string, int *idx)
949 for (; i < hws->count; i++)
951 hw= (class cl_hw *)(hws->at(i));
952 if (strstr(hw->id_string, id_string) == hw->id_string)
963 cl_uc::get_hw(enum hw_cath cath, int hwid, int *idx)
970 hw= get_hw(cath, &i);
975 hw= get_hw(cath, &i);
984 cl_uc::get_hw(char *id_string, int hwid, int *idx)
991 hw= get_hw(id_string, &i);
996 hw= get_hw(id_string, &i);
1006 * Help of the command interpreter
1010 cl_uc::dis_tbl(void)
1012 static struct dis_entry empty= { 0, 0, 0, 0, NULL };
1017 cl_uc::sfr_tbl(void)
1019 static struct name_entry empty= { 0, 0 };
1024 cl_uc::bit_tbl(void)
1026 static struct name_entry empty= { 0, 0 };
1031 cl_uc::disass(t_addr addr, char *sep)
1035 buf= (char*)malloc(100);
1036 strcpy(buf, "uc::disass() unimplemented\n");
1041 cl_uc::print_disass(t_addr addr, class cl_console *con)
1050 t_mem code= rom->get(addr);
1052 dis= disass(addr, NULL);
1054 con->dd_printf("%c", (b->perm == brkFIX)?'F':'D');
1056 con->dd_printf(" ");
1057 con->dd_printf("%c ", inst_at(addr)?' ':'?');
1058 con->dd_printf(rom->addr_format, addr); con->dd_printf(" ");
1059 con->dd_printf(rom->data_format, code);
1060 for (i= 1; i < inst_length(addr); i++)
1062 con->dd_printf(" ");
1063 con->dd_printf(rom->data_format, rom->get(addr+i));
1065 int li= longest_inst();
1069 j= rom->width/4 + ((rom->width%4)?1:0) + 1;
1071 con->dd_printf(" "), j--;
1074 con->dd_printf(" %s\n", dis);
1079 cl_uc::print_regs(class cl_console *con)
1081 con->dd_printf("No registers\n");
1085 cl_uc::inst_length(t_addr addr)
1087 struct dis_entry *tabl= dis_tbl();
1094 code = rom->get(addr);
1095 for (i= 0; tabl[i].mnemonic && (code & tabl[i].mask) != tabl[i].code; i++) ;
1096 return(tabl[i].mnemonic?tabl[i].length:1);
1100 cl_uc::inst_branch(t_addr addr)
1102 struct dis_entry *tabl= dis_tbl();
1109 code = rom->get(addr);
1110 for (i= 0; tabl[i].mnemonic && (code & tabl[i].mask) != tabl[i].code; i++)
1112 return tabl[i].branch;
1116 cl_uc::longest_inst(void)
1118 struct dis_entry *de= dis_tbl();
1124 if (de->length > max)
1132 cl_uc::get_name(t_addr addr, struct name_entry tab[], char *buf)
1137 while (tab[i].name &&
1138 (!(tab[i].cpu_type & type) ||
1139 (tab[i].addr != addr)))
1142 strcpy(buf, tab[i].name);
1143 return(tab[i].name != NULL);
1147 cl_uc::symbol2address(char *sym, struct name_entry tab[],
1156 while (tab[i].name &&
1157 (!(tab[i].cpu_type & type) ||
1158 strcasecmp(sym, tab[i].name) != 0))
1170 cl_uc::symbolic_bit_name(t_addr bit_address,
1171 class cl_memory *mem,
1179 while (bit_tbl()[i].name &&
1180 (bit_tbl()[i].addr != bit_address))
1182 if (bit_tbl()[i].name)
1184 sym_name= strdup(bit_tbl()[i].name);
1189 mem->have_real_name() &&
1190 strstr(mem->get_name(), "sfr") == mem->get_name())
1193 while (sfr_tbl()[i].name &&
1194 (sfr_tbl()[i].addr != mem_addr))
1196 if (sfr_tbl()[i].name)
1197 sym_name= strdup(sfr_tbl()[i].name);
1203 sym_name= (char *)malloc(16);
1204 sprintf(sym_name, mem?(mem->addr_format):"0x%06x", mem_addr);
1206 sym_name= (char *)realloc(sym_name, strlen(sym_name)+2);
1207 strcat(sym_name, ".");
1209 while (bit_mask > 1)
1215 sprintf(bitnumstr, "%1d", i);
1216 strcat(sym_name, bitnumstr);
1222 * Messages to broadcast
1226 cl_uc::handle_event(class cl_event &event)
1230 case ev_address_space_added:
1233 class cl_event_address_space_added &e=
1234 dynamic_cast<class cl_event_address_space_added &>(event);
1235 address_space_added(e.as);
1243 return(pass_event_down(event));
1251 cl_uc::mem_cell_changed(class cl_address_space *mem, t_addr addr)
1254 hws->mem_cell_changed(mem, addr);
1256 printf("JAJ uc\n");//FIXME
1261 for (i= 0; i < mems->count; i++)
1269 cl_uc::address_space_added(class cl_address_space *as)
1272 hws->address_space_added(as);
1274 printf("JAJ uc\n");//FIXME
1283 cl_uc::error(class cl_error *error)
1286 if ((error->inst= inst_exec))
1291 cl_uc::check_errors(void)
1294 class cl_commander *c= sim->app->get_commander();
1295 bool must_stop= DD_FALSE;
1299 for (i= 0; i < errors->count; i++)
1301 class cl_error *error= (class cl_error *)(errors->at(i));
1302 if (!error->is_on())
1305 must_stop= must_stop || (error->get_type() & err_stop);
1308 class cl_console *con;
1309 con= c->actual_console;
1311 con= c->frozen_console;
1314 con->dd_printf("Erronouse instruction: ");
1315 print_disass(error->PC, con);
1322 fprintf(stderr, "no actual console, %d errors\n", errors->count);
1324 sim->stop(resERROR);
1329 * Converting bit address into real memory
1332 class cl_address_space *
1333 cl_uc::bit2mem(t_addr bitaddr, t_addr *memaddr, t_mem *bitmask)
1338 *bitmask= 1 << (bitaddr & 0x7);
1339 return(0); // abstract...
1348 cl_uc::tick_hw(int cycles)
1351 int i;//, cpc= clock_per_cycle();
1354 for (i= 0; i < hws->count; i++)
1356 hw= (class cl_hw *)(hws->at(i));
1357 if (hw->flags & HWF_INSIDE)
1360 do_extra_hw(cycles);
1365 cl_uc::do_extra_hw(int cycles)
1369 cl_uc::tick(int cycles)
1372 int i, cpc= clock_per_cycle();
1375 ticks->tick(cycles * cpc);
1376 class it_level *il= (class it_level *)(it_levels->top());
1378 isr_ticks->tick(cycles * cpc);
1379 if (state == stIDLE)
1380 idle_ticks->tick(cycles * cpc);
1381 for (i= 0; i < counters->count; i++)
1383 class cl_ticker *t= (class cl_ticker *)(counters->at(i));
1386 if ((t->options&TICK_INISR) ||
1388 t->tick(cycles * cpc);
1392 // tick for hardwares
1393 inst_ticks+= cycles;
1398 cl_uc::get_counter(int nr)
1400 if (nr >= counters->count)
1402 return((class cl_ticker *)(counters->at(nr)));
1406 cl_uc::get_counter(char *nam)
1412 for (i= 0; i < counters->count; i++)
1414 class cl_ticker *t= (class cl_ticker *)(counters->at(i));
1417 strcmp(t->get_name(), nam) == 0)
1424 cl_uc::add_counter(class cl_ticker *ticker, int nr)
1426 while (counters->count <= nr)
1428 counters->put_at(nr, ticker);
1432 cl_uc::add_counter(class cl_ticker *ticker, char */*nam*/)
1436 if (counters->count < 1)
1438 for (i= 1; i < counters->count; i++)
1440 class cl_ticker *t= (class cl_ticker *)(counters->at(i));
1443 counters->put_at(i, ticker);
1447 counters->add(ticker);
1451 cl_uc::del_counter(int nr)
1455 if (nr >= counters->count)
1457 if ((t= (class cl_ticker *)(counters->at(0))) != 0)
1459 counters->put_at(nr, 0);
1463 cl_uc::del_counter(char *nam)
1469 for (i= 0; i < counters->count; i++)
1471 class cl_ticker *t= (class cl_ticker *)(counters->at(i));
1474 strcmp(t->get_name(), nam) == 0)
1477 counters->put_at(i, 0);
1484 * Fetch without checking for breakpoint hit
1495 code= rom->read(PC);
1496 PC= rom->inc_address(PC);
1501 * Fetch but checking for breakpoint hit first, returns TRUE if
1502 * a breakpoint is hit
1506 cl_uc::fetch(t_mem *code)
1513 if ((sim->state & SIM_GO) &&
1516 if (rom->get_cell_flag(PC, CELL_FETCH_BRK) &&
1517 (brk= fbrk->get_bp(PC, &idx)) &&
1520 if (brk->perm == brkDYNAMIC)
1530 cl_uc::do_inst(int step)
1549 cl_uc::pre_inst(void)
1553 events->disconn_all();
1557 cl_uc::exec_inst(void)
1564 cl_uc::post_inst(void)
1566 tick_hw(inst_ticks);
1571 inst_exec= DD_FALSE;
1576 * Time related functions
1580 cl_uc::get_rtime(void)
1584 d= (double)ticks/xtal;
1586 return(ticks->get_rtime(xtal));
1590 cl_uc::clock_per_cycle(void)
1597 * Stack tracking system
1601 cl_uc::stack_write(class cl_stack_op *op)
1603 if (op->get_op() & stack_read_operation)
1605 class cl_error_stack_tracker_wrong_handle *e= new
1606 cl_error_stack_tracker_wrong_handle(DD_FALSE);
1607 //fprintf(stderr, "%06"_A_"x cl_uc::stack_read() should be called for "
1608 //"%s\n", op->get_pc(), op->get_op_name());
1613 stack_ops->push(op);
1617 cl_uc::stack_read(class cl_stack_op *op)
1619 class cl_stack_op *top= (class cl_stack_op *)(stack_ops->top());
1621 if (op->get_op() & stack_write_operation)
1623 class cl_error_stack_tracker_wrong_handle *e= new
1624 cl_error_stack_tracker_wrong_handle(DD_TRUE);
1627 //fprintf(stderr, "%06"_A_"x cl_uc::stack_write() should be called for "
1628 //"%s\n", op->get_pc(), op->get_op_name());
1633 class cl_error *e= new cl_error_stack_tracker_empty(op);
1634 /*printf("0x%06"_A_"x %s operation on stack but no operation was before\n
1636 op->get_pc(), op->get_op_name());*/
1644 if (!top->match(op))
1646 class cl_error *e= new cl_error_stack_tracker_unmatch(top, op);
1649 /*printf("0x%06"_A_"x %s operation on stack but last was %s\n",
1650 op->get_pc(), op->get_op_name(), top->get_op_name());*/
1652 int top_size= top->data_size(), op_size= op->data_size();
1653 if (top_size != op_size)
1655 application->debug("0x%06"_A_"x %d bytes to read out of stack "
1656 "but %d was pushed in last operation\n",
1657 op->get_pc(), op_size, top_size);
1663 top->can_removed(op))
1665 top= (class cl_stack_op *)stack_ops->pop();
1667 top= (class cl_stack_op *)stack_ops->top();
1672 application->debug("0x%06"_A_"x %d ops removed from stack-tracker "
1673 "when %s happened, top pc=0x%06"_A_"x "
1674 "top before=0x%06"_A_"x op after=0x%06"_A_"x\n",
1675 op->get_pc(), removed, op->get_op_name(),
1676 top?(top->get_pc()):0, top?(top->get_before()):0,
1682 int ta= top->get_after(), oa= op->get_after();
1685 application->debug("0x%06"_A_"x stack still inconsistent after %s, "
1686 "%d byte(s) should be read out; top after"
1687 "=0x%06"_A_"x op after=0x%06"_A_"x\n",
1688 op->get_pc(), op->get_op_name(), abs(ta-oa),
1691 new cl_error_stack_tracker_inconsistent(op, abs(ta-oa));
1702 * Breakpoint handling
1705 class cl_fetch_brk *
1706 cl_uc::fbrk_at(t_addr addr)
1710 return((class cl_fetch_brk *)(fbrk->get_bp(addr, &idx)));
1714 cl_uc::ebrk_at(t_addr addr, char *id)
1717 class cl_ev_brk *eb;
1719 for (i= 0; i < ebrk->count; i++)
1721 eb= (class cl_ev_brk *)(ebrk->at(i));
1722 if (eb->addr == addr &&
1723 !strcmp(eb->id, id))
1730 cl_uc::rm_fbrk(long addr)
1735 /* Get a breakpoint specified by its number */
1738 cl_uc::brk_by_nr(int nr)
1742 if ((bp= fbrk->get_bp(nr)))
1744 if ((bp= ebrk->get_bp(nr)))
1749 /* Get a breakpoint from the specified collection by its number */
1752 cl_uc::brk_by_nr(class brk_coll *bpcoll, int nr)
1756 if ((bp= bpcoll->get_bp(nr)))
1761 /* Remove an event breakpoint specified by its address and id */
1764 cl_uc::rm_ebrk(t_addr addr, char *id)
1767 class cl_ev_brk *eb;
1769 for (i= 0; i < ebrk->count; i++)
1771 eb= (class cl_ev_brk *)(ebrk->at(i));
1772 if (eb->addr == addr &&
1773 !strcmp(eb->id, id))
1778 /* Remove a breakpoint specified by its number */
1781 cl_uc::rm_brk(int nr)
1785 if ((bp= brk_by_nr(fbrk, nr)))
1787 fbrk->del_bp(bp->addr);
1790 else if ((bp= brk_by_nr(ebrk, nr)))
1792 ebrk->del_bp(ebrk->index_of(bp), 0);
1799 cl_uc::put_breaks(void)
1802 /* Remove all fetch and event breakpoints */
1805 cl_uc::remove_all_breaks(void)
1809 class cl_brk *brk= (class cl_brk *)(fbrk->at(0));
1810 fbrk->del_bp(brk->addr);
1813 ebrk->del_bp(ebrk->count-1, 0);
1817 cl_uc::make_new_brknr(void)
1819 if (brk_counter == 0)
1820 return(brk_counter= 1);
1821 if (fbrk->count == 0 &&
1823 return(brk_counter= 1);
1824 return(++brk_counter);
1828 cl_uc::mk_ebrk(enum brk_perm perm, class cl_address_space *mem,
1829 char op, t_addr addr, int hit)
1834 b= new cl_ev_brk(mem, make_new_brknr(), addr, perm, hit, op);
1840 cl_uc::check_events(void)
1843 for (i= 0; i < events->count; i++)
1845 class cl_ev_brk *brk=
1846 dynamic_cast<class cl_ev_brk *>(events->object_at(i));
1849 sim->stop(resBREAKPOINT);
1855 *----------------------------------------------------------------------------
1858 ERROR_CLASS_DEF_PARENT_ON(err_error, unknown_code, "unknown_code",
1859 error_class_base, ERROR_ON);
1861 cl_error_unknown_code::cl_error_unknown_code(class cl_uc *the_uc)
1864 classification= &error_unknown_code_class;
1868 cl_error_unknown_code::print(class cl_commander *c)
1870 FILE *f= c->get_out();
1871 cmd_fprintf(f, "%s: unknown instruction code at ", get_type_name());
1874 cmd_fprintf(f, uc->rom->addr_format, PC);
1875 cmd_fprintf(f, " (");
1876 cmd_fprintf(f, uc->rom->data_format, uc->rom->get(PC));
1877 cmd_fprintf(f, ")");
1880 cmd_fprintf(f, "0x%06x", PC);
1881 cmd_fprintf(f, "\n");