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"
63 cl_ticker::cl_ticker(int adir, int in_isr, char *aname)
73 cl_ticker::~cl_ticker(void) {}
76 cl_ticker::tick(int nr)
84 cl_ticker::get_rtime(double xtal)
88 d= (double)ticks/xtal;
93 cl_ticker::dump(int nr, double xtal, class cl_console *con)
95 con->dd_printf("timer #%d(\"%s\") %s%s: %g sec (%lu clks)\n",
96 nr, get_name("unnamed"),
97 (options&TICK_RUN)?"ON":"OFF",
98 (options&TICK_INISR)?",ISR":"",
99 get_rtime(xtal), ticks);
107 cl_xtal_option::cl_xtal_option(class cl_uc *the_uc):
114 cl_xtal_option::option_changed(void)
119 option->get_value(&d);
125 * Abstract microcontroller
126 ******************************************************************************
129 cl_uc::cl_uc(class cl_sim *asim):
134 //mems= new cl_list(MEM_TYPES, 1);
135 memchips= new cl_list(2, 2, "memchips");
136 address_spaces= new cl_address_space_list(this);
137 //address_decoders= new cl_list(2, 2);
141 //options= new cl_list(2, 2);
142 //for (i= MEM_ROM; i < MEM_TYPES; i++) mems->add(0);
143 xtal_option= new cl_xtal_option(this);
145 ticks= new cl_ticker(+1, 0, "time");
146 isr_ticks= new cl_ticker(+1, TICK_INISR, "isr");
147 idle_ticks= new cl_ticker(+1, TICK_IDLE, "idle");
148 counters= new cl_list(2, 2, "counters");
149 it_levels= new cl_list(2, 2, "it levels");
150 it_sources= new cl_irqs(2, 2);
151 class it_level *il= new it_level(-1, 0, 0, 0);
153 stack_ops= new cl_list(2, 2, "stack operations");
154 errors= new cl_list(2, 2, "errors in uc");
155 events= new cl_list(2, 2, "events in uc");
171 events->disconn_all();
181 delete address_spaces;
183 //delete address_decoders;
192 set_name("controller");
194 if (xtal_option->use("xtal"))
195 xtal= xtal_option->get_value(xtal);
199 rom= address_space(MEM_ROM_ID);
200 ebrk= new brk_coll(2, 2, rom);
201 fbrk= new brk_coll(2, 2, rom);
202 fbrk->Duplicates= DD_FALSE;
206 class cl_cmdset *cs= sim->app->get_commander()->cmdset;
209 for (i= 0; i < sim->app->in_files->count; i++)
211 char *fname= (char *)(sim->app->in_files->at(i));
213 if ((l= read_hex_file(fname)) >= 0)
215 sim->app->get_commander()->all_printf("%ld words read from %s\n",
223 cl_uc::id_string(void)
225 return("unknown microcontroller");
237 idle_ticks->ticks= 0;
238 /*FIXME should we clear user counters?*/
239 il= (class it_level *)(it_levels->top());
243 il= (class it_level *)(it_levels->pop());
245 il= (class it_level *)(it_levels->top());
250 stack_ops->free_all();
253 for (i= 0; i < hws->count; i++)
255 class cl_hw *hw= (class cl_hw *)(hws->at(i));
265 cl_uc::make_memories(void)
269 cl_uc::get_mem_size(char *id)
271 class cl_memory *m= memory(id);
272 return(m?(m->get_size()):0);
276 cl_uc::get_mem_width(char *id)
278 class cl_memory *m= memory(id);
279 return(m?(m->width):8);
283 cl_uc::mk_hw_elements(void)
288 cl_uc::build_cmdset(class cl_cmdset *cmdset)
291 class cl_super_cmd *super_cmd;
292 class cl_cmdset *cset;
294 cmdset->add(cmd= new cl_state_cmd("state", 0,
295 "state State of microcontroller",
296 "long help of state"));
300 cmdset->add(cmd= new cl_statistic_cmd("statistic", 0,
301 "statistic [mem [startaddr [endaddr]]]\n"
302 " Statistic of memory accesses",
303 "long help of statistic"));
307 cmdset->add(cmd= new cl_file_cmd("file", 0,
308 "file \"FILE\" Load FILE into ROM",
309 "long help of file"));
311 cmd->add_name("load");
313 cmdset->add(cmd= new cl_dl_cmd("download", 0,
314 "download Load (intel.hex) data",
315 "long help of download"));
319 cmdset->add(cmd= new cl_pc_cmd("pc", 0,
320 "pc [addr] Set/get PC",
324 cmdset->add(cmd= new cl_reset_cmd("reset", 0,
326 "long help of reset"));
329 cmdset->add(cmd= new cl_dump_cmd("dump", DD_TRUE,
330 "dump memory_type [start [stop [bytes_per_line]]]\n"
331 " Dump memory of specified type\n"
332 "dump bit... Dump bits",
333 "long help of dump"));
336 cmdset->add(cmd= new cl_dch_cmd("dch", DD_TRUE,
337 "dch [start [stop]] Dump code in hex form",
338 "long help of dch"));
341 cmdset->add(cmd= new cl_dc_cmd("dc", DD_TRUE,
342 "dc [start [stop]] Dump code in disass form",
346 cmdset->add(cmd= new cl_disassemble_cmd("disassemble", DD_TRUE,
347 "disassemble [start [offset [lines]]]\n"
349 "long help of disassemble"));
352 cmdset->add(cmd= new cl_fill_cmd("fill", 0,
353 "fill memory_type start end data\n"
354 " Fill memory region with data",
355 "long help of fill"));
358 cmdset->add(cmd= new cl_where_cmd("where", 0,
359 "where memory_type data...\n"
360 " Case unsensitive search for data",
361 "long help of where"));
364 cmdset->add(cmd= new cl_Where_cmd("Where", 0,
365 "Where memory_type data...\n"
366 " Case sensitive search for data",
367 "long help of Where"));
370 cmdset->add(cmd= new cl_break_cmd("break", 0,
371 "break addr [hit] Set fix breakpoint\n"
372 "break mem_type r|w addr [hit]\n"
373 " Set fix event breakpoint",
374 "long help of break"));
377 cmdset->add(cmd= new cl_tbreak_cmd("tbreak", 0,
378 "tbreak addr [hit] Set temporary breakpoint\n"
379 "tbreak mem_type r|w addr [hit]\n"
380 " Set temporary event breakpoint",
381 "long help of tbreak"));
384 cmdset->add(cmd= new cl_clear_cmd("clear", 0,
385 "clear [addr...] Clear fix breakpoint",
386 "long help of clear"));
389 cmdset->add(cmd= new cl_delete_cmd("delete", 0,
390 "delete [nr...] Delete breakpoint(s)",
391 "long help of clear"));
395 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("get"));
397 cset= super_cmd->commands;
399 cset= new cl_cmdset();
402 cset->add(cmd= new cl_get_sfr_cmd("sfr", 0,
403 "get sfr address...\n"
404 " Get value of addressed SFRs",
405 "long help of get sfr"));
407 /*cset->add(cmd= new cl_get_option_cmd("option", 0,
409 " Get value of an option",
410 "long help of get option"));
415 cmdset->add(cmd= new cl_super_cmd("get", 0,
416 "get subcommand Get, see `get' command for more help",
417 "long help of get", cset));
422 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("set"));
424 cset= super_cmd->commands;
426 cset= new cl_cmdset();
429 cset->add(cmd= new cl_set_mem_cmd("memory", 0,
430 "set memory memory_type address data...\n"
431 " Place list of data into memory",
432 "long help of set memory"));
434 cset->add(cmd= new cl_set_bit_cmd("bit", 0,
435 "set bit addr 0|1 Set specified bit to 0 or 1",
436 "long help of set bit"));
438 cset->add(cmd= new cl_set_hw_cmd("hardware", 0,
439 "set hardware cathegory params...\n"
440 " Set parameters of specified hardware element",
441 "long help of set hardware"));
447 cmdset->add(cmd= new cl_super_cmd("set", 0,
448 "set subcommand Set, see `set' command for more help",
449 "long help of set", cset));
454 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("info"));
456 cset= super_cmd->get_subcommands();
458 cset= new cl_cmdset();
461 cset->add(cmd= new cl_info_bp_cmd("breakpoints", 0,
462 "info breakpoints Status of user-settable breakpoints",
463 "long help of info breakpoints"));
466 cset->add(cmd= new cl_info_reg_cmd("registers", 0,
467 "info registers List of integer registers and their contents",
468 "long help of info registers"));
470 cset->add(cmd= new cl_info_hw_cmd("hardware", 0,
471 "info hardware cathegory\n"
472 " Status of hardware elements of the CPU",
473 "long help of info hardware"));
476 cset->add(cmd= new cl_info_stack_cmd("stack", 0,
477 "info stack Status of stack of the CPU",
478 "long help of info stack"));
480 cset->add(cmd= new cl_info_memory_cmd("memory", 0,
481 "info memory Information about memory system",
482 "long help of info memory"));
486 cmdset->add(cmd= new cl_super_cmd("info", 0,
487 "info subcommand Information, see `info' command for more help",
488 "long help of info", cset));
493 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("timer"));
495 cset= super_cmd->get_subcommands();
497 cset= new cl_cmdset();
500 cset->add(cmd= new cl_timer_add_cmd("add", 0,
501 "timer add id [direction [in_isr]]\n"
502 " Create a clock counter (timer)",
503 "log help of timer add"));
505 cmd->add_name("create");
506 cmd->add_name("make");
507 cset->add(cmd= new cl_timer_delete_cmd("delete", 0,
508 "timer delete id Delete a timer",
509 "long help of timer delete"));
511 cmd->add_name("remove");
512 cset->add(cmd= new cl_timer_get_cmd("get", 0,
513 "timer get [id] Get value of a timer, or all",
514 "long help of timer get"));
516 cset->add(cmd= new cl_timer_run_cmd("run", 0,
517 "timer start id Start a timer",
518 "long help of timer run"));
520 cmd->add_name("start");
521 cset->add(cmd= new cl_timer_stop_cmd("stop", 0,
522 "timer stop id Stop a timer",
523 "long help of timer stop"));
525 cset->add(cmd= new cl_timer_value_cmd("set", 0,
526 "timer set id value\n"
527 " Set a timer value",
528 "long help of timer set"));
530 cmd->add_name("value");
533 cmdset->add(cmd= new cl_super_cmd("timer", 0,
534 "timer subcommand Manage timers",
535 "long help of timer", cset));
540 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("memory"));
542 cset= super_cmd->get_subcommands();
544 cset= new cl_cmdset();
548 cset->add(cmd= new cl_memory_cmd("_no_parameters_", 0,
549 "memory Information about memory system",
550 "long help of memory"));
553 cset->add(cmd= new cl_memory_createchip_cmd("createchip", 0,
554 "memory createchip id size cellsize\n"
555 " Create a new memory chip",
556 "long help of memory createchip"));
558 cmd->add_name("cchip");
559 cset->add(cmd= new cl_memory_createaddressspace_cmd("createaddressspace", 0,
560 "memory createaddressspace id startaddr size\n"
561 " Create address space",
562 "long help of memory createaddressspace"));
564 cmd->add_name("createaddrspace");
565 cmd->add_name("createaspace");
566 cmd->add_name("caddressspace");
567 cmd->add_name("caddrspace");
568 cmd->add_name("caspace");
569 cset->add(cmd= new cl_memory_createaddressdecoder_cmd("createaddressdecoder", 0,
570 "memory createaddressdecoder addressspace begin end chip begin\n"
571 " Create address decoder",
572 "long help of memory createaddressdecoder"));
574 cmd->add_name("createaddrdecoder");
575 cmd->add_name("createadecoder");
576 cmd->add_name("caddressdecoder");
577 cmd->add_name("caddrdecoder");
578 cmd->add_name("cadecoder");
581 cmdset->add(cmd= new cl_super_cmd("memory", 0,
582 "memory subcommand Manage memory chips and address spaces",
583 "long help of memory", cset));
590 * Read/write simulated memory
594 cl_uc::read_mem(char *id, t_addr addr)
596 class cl_address_space *m= address_space(id);
598 return(m?(m->read(addr)):0);
602 cl_uc::get_mem(char *id, t_addr addr)
604 class cl_address_space *m= address_space(id);
606 return(m?(m->get(addr)):0);
610 cl_uc::write_mem(char *id, t_addr addr, t_mem val)
612 class cl_address_space *m= address_space(id);
619 cl_uc::set_mem(char *id, t_addr addr, t_mem val)
621 class cl_address_space *m= address_space(id);
630 cl_uc::mem(enum mem_class type)
634 if (mems->count < type)
635 m= (class cl_m *)(mems->at(MEM_DUMMY));
637 m= (class cl_m *)(mems->at(type));
642 class cl_address_space *
643 cl_uc::address_space(char *id)
650 for (i= 0; i < address_spaces->count; i++)
652 class cl_address_space *m= (cl_address_space *)(address_spaces->at(i));
654 !m->have_real_name())
656 if (m->is_inamed(id))
663 cl_uc::memory(char *id)
670 for (i= 0; i < address_spaces->count; i++)
672 class cl_memory *m= (cl_memory *)(address_spaces->at(i));
674 !m->have_real_name())
676 if (m->is_inamed(id))
679 for (i= 0; i < memchips->count; i++)
681 class cl_memory *m= (cl_memory *)(memchips->at(i));
683 !m->have_real_name())
685 if (m->is_inamed(id))
693 ReadInt(FILE *f, bool *ok, int bytes)
701 if (fscanf(f, "%2c", &s2[0]) == EOF)
704 l= l*256 + strtol(s2, NULL, 16);
713 * Reading intel hexa file into EROM
714 *____________________________________________________________________________
716 * If parameter is a NULL pointer, this function reads data from `cmd_in'
721 cl_uc::read_hex_file(const char *nam)
725 long written= 0, recnum= 0;
727 uchar dnum; // data number
728 uchar rtyp=0; // record type
729 uint addr= 0; // address
730 uchar rec[300]; // data record
731 uchar sum ; // checksum
739 sim->app->get_commander()->
740 dd_printf("No ROM address space to read in.\n");
746 sim->app->get_commander()->
747 dd_printf("cl_uc::read_hex_file File name not specified\n");
751 if ((f= fopen(nam, "r")) == NULL)
753 fprintf(stderr, "Can't open `%s': %s\n", nam, strerror(errno));
757 //memset(inst_map, '\0', sizeof(inst_map));
762 while (((c= getc(f)) != ':') &&
765 {fprintf(stderr, ": not found\n");break;}
767 dnum= ReadInt(f, &ok, 1);//printf("dnum=%02x",dnum);
769 addr= ReadInt(f, &ok, 2);//printf("addr=%04x",addr);
771 chk+= ((addr >> 8) & 0xff);
772 rtyp= ReadInt(f, &ok, 1);//printf("rtyp=%02x ",rtyp);
774 for (i= 0; ok && (i < dnum); i++)
776 rec[i]= ReadInt(f, &ok, 1);//printf("%02x",rec[i]);
781 sum= ReadInt(f, &ok, 1);//printf(" sum=%02x\n",sum);
784 if (((sum + chk) & 0xff) == 0)
790 for (i= 0; i < dnum; i++)
794 rom->set(addr, rec[i]);
798 else if (rom->width <= 16)
808 rom->set(addr, (high*256)+low);
818 application->debug("Unknown record type %d(0x%x)\n",
822 application->debug("Checksum error (%x instead of %x) in "
823 "record %ld.\n", chk, sum, recnum);
826 application->debug("Read error in record %ld.\n", recnum);
829 if (rom->width > 8 &&
835 application->debug("%ld records have been read\n", recnum);
842 * Handling instruction map
844 * `inst_at' is checking if the specified address is in instruction
845 * map and `set_inst_at' marks the address in the map and
846 * `del_inst_at' deletes the mark. `there_is_inst' cheks if there is
847 * any mark in the map
851 cl_uc::inst_at(t_addr addr)
855 return(rom->get_cell_flag(addr, CELL_INST));
859 cl_uc::set_inst_at(t_addr addr)
862 rom->set_cell_flag(addr, DD_TRUE, CELL_INST);
866 cl_uc::del_inst_at(t_addr addr)
869 rom->set_cell_flag(addr, DD_FALSE, CELL_INST);
873 cl_uc::there_is_inst(void)
879 for (addr= 0; rom->valid_address(addr) && !got; addr++)
880 got= rom->get_cell_flag(addr, CELL_INST);
886 * Manipulating HW elements of the CPU
887 *****************************************************************************
890 /* Register callback hw objects for mem read/write */
893 cl_uc::register_hw_read(enum mem_class type, t_addr addr, class cl_hw *hw)
898 if ((m= (class cl_m*)mems->at(type)))
900 if ((l= m->read_locs->get_loc(addr)) == 0)
902 l= new cl_memloc(addr);
904 m->read_locs->add(l);
909 printf("cl_uc::register_hw_read TROUBLE\n");
913 cl_uc::register_hw_write(enum mem_class type, t_addr addr, class cl_hw *hw)
917 /* Looking for a specific HW element */
920 cl_uc::get_hw(enum hw_cath cath, int *idx)
927 for (; i < hws->count; i++)
929 hw= (class cl_hw *)(hws->at(i));
930 if (hw->cathegory == cath)
941 cl_uc::get_hw(char *id_string, int *idx)
948 for (; i < hws->count; i++)
950 hw= (class cl_hw *)(hws->at(i));
951 if (strstr(hw->id_string, id_string) == hw->id_string)
962 cl_uc::get_hw(enum hw_cath cath, int hwid, int *idx)
969 hw= get_hw(cath, &i);
974 hw= get_hw(cath, &i);
983 cl_uc::get_hw(char *id_string, int hwid, int *idx)
990 hw= get_hw(id_string, &i);
995 hw= get_hw(id_string, &i);
1005 * Help of the command interpreter
1009 cl_uc::dis_tbl(void)
1011 static struct dis_entry empty= { 0, 0, 0, 0, NULL };
1016 cl_uc::sfr_tbl(void)
1018 static struct name_entry empty= { 0, 0 };
1023 cl_uc::bit_tbl(void)
1025 static struct name_entry empty= { 0, 0 };
1030 cl_uc::disass(t_addr addr, char *sep)
1034 buf= (char*)malloc(100);
1035 strcpy(buf, "uc::disass() unimplemented\n");
1040 cl_uc::print_disass(t_addr addr, class cl_console *con)
1049 t_mem code= rom->get(addr);
1051 dis= disass(addr, NULL);
1053 con->dd_printf("%c", (b->perm == brkFIX)?'F':'D');
1055 con->dd_printf(" ");
1056 con->dd_printf("%c ", inst_at(addr)?' ':'?');
1057 con->dd_printf(rom->addr_format, addr); con->dd_printf(" ");
1058 con->dd_printf(rom->data_format, code);
1059 for (i= 1; i < inst_length(addr); i++)
1061 con->dd_printf(" ");
1062 con->dd_printf(rom->data_format, rom->get(addr+i));
1064 int li= longest_inst();
1068 j= rom->width/4 + ((rom->width%4)?1:0) + 1;
1070 con->dd_printf(" "), j--;
1073 con->dd_printf(" %s\n", dis);
1078 cl_uc::print_regs(class cl_console *con)
1080 con->dd_printf("No registers\n");
1084 cl_uc::inst_length(t_addr addr)
1086 struct dis_entry *tabl= dis_tbl();
1093 code = rom->get(addr);
1094 for (i= 0; tabl[i].mnemonic && (code & tabl[i].mask) != tabl[i].code; i++) ;
1095 return(tabl[i].mnemonic?tabl[i].length:1);
1099 cl_uc::inst_branch(t_addr addr)
1101 struct dis_entry *tabl= dis_tbl();
1108 code = rom->get(addr);
1109 for (i= 0; tabl[i].mnemonic && (code & tabl[i].mask) != tabl[i].code; i++)
1111 return tabl[i].branch;
1115 cl_uc::longest_inst(void)
1117 struct dis_entry *de= dis_tbl();
1123 if (de->length > max)
1131 cl_uc::get_name(t_addr addr, struct name_entry tab[], char *buf)
1136 while (tab[i].name &&
1137 (!(tab[i].cpu_type & type) ||
1138 (tab[i].addr != addr)))
1141 strcpy(buf, tab[i].name);
1142 return(tab[i].name != NULL);
1146 cl_uc::symbol2address(char *sym, struct name_entry tab[],
1155 while (tab[i].name &&
1156 (!(tab[i].cpu_type & type) ||
1157 strcasecmp(sym, tab[i].name) != 0))
1169 cl_uc::symbolic_bit_name(t_addr bit_address,
1170 class cl_memory *mem,
1178 while (bit_tbl()[i].name &&
1179 (bit_tbl()[i].addr != bit_address))
1181 if (bit_tbl()[i].name)
1183 sym_name= strdup(bit_tbl()[i].name);
1188 mem->have_real_name() &&
1189 strstr(mem->get_name(), "sfr") == mem->get_name())
1192 while (sfr_tbl()[i].name &&
1193 (sfr_tbl()[i].addr != mem_addr))
1195 if (sfr_tbl()[i].name)
1196 sym_name= strdup(sfr_tbl()[i].name);
1202 sym_name= (char *)malloc(16);
1203 sprintf(sym_name, mem?(mem->addr_format):"0x%06x", mem_addr);
1205 sym_name= (char *)realloc(sym_name, strlen(sym_name)+2);
1206 strcat(sym_name, ".");
1208 while (bit_mask > 1)
1214 sprintf(bitnumstr, "%1d", i);
1215 strcat(sym_name, bitnumstr);
1221 * Messages to broadcast
1225 cl_uc::handle_event(class cl_event &event)
1229 case ev_address_space_added:
1232 class cl_event_address_space_added &e=
1233 dynamic_cast<class cl_event_address_space_added &>(event);
1234 address_space_added(e.as);
1242 return(pass_event_down(event));
1250 cl_uc::mem_cell_changed(class cl_address_space *mem, t_addr addr)
1253 hws->mem_cell_changed(mem, addr);
1255 printf("JAJ uc\n");//FIXME
1260 for (i= 0; i < mems->count; i++)
1268 cl_uc::address_space_added(class cl_address_space *as)
1271 hws->address_space_added(as);
1273 printf("JAJ uc\n");//FIXME
1282 cl_uc::error(class cl_error *error)
1285 if ((error->inst= inst_exec))
1290 cl_uc::check_errors(void)
1293 class cl_commander *c= sim->app->get_commander();
1294 bool must_stop= DD_FALSE;
1298 for (i= 0; i < errors->count; i++)
1300 class cl_error *error= (class cl_error *)(errors->at(i));
1301 if (!error->is_on())
1304 must_stop= must_stop || (error->get_type() & err_stop);
1307 class cl_console *con;
1308 con= c->actual_console;
1310 con= c->frozen_console;
1313 con->dd_printf("Erronouse instruction: ");
1314 print_disass(error->PC, con);
1321 fprintf(stderr, "no actual console, %d errors\n", errors->count);
1323 sim->stop(resERROR);
1328 * Converting bit address into real memory
1331 class cl_address_space *
1332 cl_uc::bit2mem(t_addr bitaddr, t_addr *memaddr, t_mem *bitmask)
1337 *bitmask= 1 << (bitaddr & 0x7);
1338 return(0); // abstract...
1347 cl_uc::tick_hw(int cycles)
1350 int i;//, cpc= clock_per_cycle();
1353 for (i= 0; i < hws->count; i++)
1355 hw= (class cl_hw *)(hws->at(i));
1356 if (hw->flags & HWF_INSIDE)
1359 do_extra_hw(cycles);
1364 cl_uc::do_extra_hw(int cycles)
1368 cl_uc::tick(int cycles)
1371 int i, cpc= clock_per_cycle();
1374 ticks->tick(cycles * cpc);
1375 class it_level *il= (class it_level *)(it_levels->top());
1377 isr_ticks->tick(cycles * cpc);
1378 if (state == stIDLE)
1379 idle_ticks->tick(cycles * cpc);
1380 for (i= 0; i < counters->count; i++)
1382 class cl_ticker *t= (class cl_ticker *)(counters->at(i));
1385 if ((t->options&TICK_INISR) ||
1387 t->tick(cycles * cpc);
1391 // tick for hardwares
1392 inst_ticks+= cycles;
1397 cl_uc::get_counter(int nr)
1399 if (nr >= counters->count)
1401 return((class cl_ticker *)(counters->at(nr)));
1405 cl_uc::get_counter(char *nam)
1411 for (i= 0; i < counters->count; i++)
1413 class cl_ticker *t= (class cl_ticker *)(counters->at(i));
1416 strcmp(t->get_name(), nam) == 0)
1423 cl_uc::add_counter(class cl_ticker *ticker, int nr)
1425 while (counters->count <= nr)
1427 counters->put_at(nr, ticker);
1431 cl_uc::add_counter(class cl_ticker *ticker, char */*nam*/)
1435 if (counters->count < 1)
1437 for (i= 1; i < counters->count; i++)
1439 class cl_ticker *t= (class cl_ticker *)(counters->at(i));
1442 counters->put_at(i, ticker);
1446 counters->add(ticker);
1450 cl_uc::del_counter(int nr)
1454 if (nr >= counters->count)
1456 if ((t= (class cl_ticker *)(counters->at(0))) != 0)
1458 counters->put_at(nr, 0);
1462 cl_uc::del_counter(char *nam)
1468 for (i= 0; i < counters->count; i++)
1470 class cl_ticker *t= (class cl_ticker *)(counters->at(i));
1473 strcmp(t->get_name(), nam) == 0)
1476 counters->put_at(i, 0);
1483 * Fetch without checking for breakpoint hit
1494 code= rom->read(PC);
1495 PC= rom->inc_address(PC);
1500 * Fetch but checking for breakpoint hit first, returns TRUE if
1501 * a breakpoint is hit
1505 cl_uc::fetch(t_mem *code)
1512 if ((sim->state & SIM_GO) &&
1515 if (rom->get_cell_flag(PC, CELL_FETCH_BRK) &&
1516 (brk= fbrk->get_bp(PC, &idx)) &&
1519 if (brk->perm == brkDYNAMIC)
1529 cl_uc::do_inst(int step)
1548 cl_uc::pre_inst(void)
1552 events->disconn_all();
1556 cl_uc::exec_inst(void)
1563 cl_uc::post_inst(void)
1565 tick_hw(inst_ticks);
1570 inst_exec= DD_FALSE;
1575 * Time related functions
1579 cl_uc::get_rtime(void)
1583 d= (double)ticks/xtal;
1585 return(ticks->get_rtime(xtal));
1589 cl_uc::clock_per_cycle(void)
1596 * Stack tracking system
1600 cl_uc::stack_write(class cl_stack_op *op)
1602 if (op->get_op() & stack_read_operation)
1604 class cl_error_stack_tracker_wrong_handle *e= new
1605 cl_error_stack_tracker_wrong_handle(DD_FALSE);
1606 //fprintf(stderr, "%06"_A_"x cl_uc::stack_read() should be called for "
1607 //"%s\n", op->get_pc(), op->get_op_name());
1612 stack_ops->push(op);
1616 cl_uc::stack_read(class cl_stack_op *op)
1618 class cl_stack_op *top= (class cl_stack_op *)(stack_ops->top());
1620 if (op->get_op() & stack_write_operation)
1622 class cl_error_stack_tracker_wrong_handle *e= new
1623 cl_error_stack_tracker_wrong_handle(DD_TRUE);
1626 //fprintf(stderr, "%06"_A_"x cl_uc::stack_write() should be called for "
1627 //"%s\n", op->get_pc(), op->get_op_name());
1632 class cl_error *e= new cl_error_stack_tracker_empty(op);
1633 /*printf("0x%06"_A_"x %s operation on stack but no operation was before\n
1635 op->get_pc(), op->get_op_name());*/
1643 if (!top->match(op))
1645 class cl_error *e= new cl_error_stack_tracker_unmatch(top, op);
1648 /*printf("0x%06"_A_"x %s operation on stack but last was %s\n",
1649 op->get_pc(), op->get_op_name(), top->get_op_name());*/
1651 int top_size= top->data_size(), op_size= op->data_size();
1652 if (top_size != op_size)
1654 application->debug("0x%06"_A_"x %d bytes to read out of stack "
1655 "but %d was pushed in last operation\n",
1656 op->get_pc(), op_size, top_size);
1662 top->can_removed(op))
1664 top= (class cl_stack_op *)stack_ops->pop();
1666 top= (class cl_stack_op *)stack_ops->top();
1671 application->debug("0x%06"_A_"x %d ops removed from stack-tracker "
1672 "when %s happened, top pc=0x%06"_A_"x "
1673 "top before=0x%06"_A_"x op after=0x%06"_A_"x\n",
1674 op->get_pc(), removed, op->get_op_name(),
1675 top?(top->get_pc()):0, top?(top->get_before()):0,
1681 int ta= top->get_after(), oa= op->get_after();
1684 application->debug("0x%06"_A_"x stack still inconsistent after %s, "
1685 "%d byte(s) should be read out; top after"
1686 "=0x%06"_A_"x op after=0x%06"_A_"x\n",
1687 op->get_pc(), op->get_op_name(), abs(ta-oa),
1690 new cl_error_stack_tracker_inconsistent(op, abs(ta-oa));
1701 * Breakpoint handling
1704 class cl_fetch_brk *
1705 cl_uc::fbrk_at(t_addr addr)
1709 return((class cl_fetch_brk *)(fbrk->get_bp(addr, &idx)));
1713 cl_uc::ebrk_at(t_addr addr, char *id)
1716 class cl_ev_brk *eb;
1718 for (i= 0; i < ebrk->count; i++)
1720 eb= (class cl_ev_brk *)(ebrk->at(i));
1721 if (eb->addr == addr &&
1722 !strcmp(eb->id, id))
1729 cl_uc::rm_fbrk(long addr)
1734 /* Get a breakpoint specified by its number */
1737 cl_uc::brk_by_nr(int nr)
1741 if ((bp= fbrk->get_bp(nr)))
1743 if ((bp= ebrk->get_bp(nr)))
1748 /* Get a breakpoint from the specified collection by its number */
1751 cl_uc::brk_by_nr(class brk_coll *bpcoll, int nr)
1755 if ((bp= bpcoll->get_bp(nr)))
1760 /* Remove an event breakpoint specified by its address and id */
1763 cl_uc::rm_ebrk(t_addr addr, char *id)
1766 class cl_ev_brk *eb;
1768 for (i= 0; i < ebrk->count; i++)
1770 eb= (class cl_ev_brk *)(ebrk->at(i));
1771 if (eb->addr == addr &&
1772 !strcmp(eb->id, id))
1777 /* Remove a breakpoint specified by its number */
1780 cl_uc::rm_brk(int nr)
1784 if ((bp= brk_by_nr(fbrk, nr)))
1786 fbrk->del_bp(bp->addr);
1789 else if ((bp= brk_by_nr(ebrk, nr)))
1791 ebrk->del_bp(ebrk->index_of(bp), 0);
1798 cl_uc::put_breaks(void)
1801 /* Remove all fetch and event breakpoints */
1804 cl_uc::remove_all_breaks(void)
1808 class cl_brk *brk= (class cl_brk *)(fbrk->at(0));
1809 fbrk->del_bp(brk->addr);
1812 ebrk->del_bp(ebrk->count-1, 0);
1816 cl_uc::make_new_brknr(void)
1818 if (brk_counter == 0)
1819 return(brk_counter= 1);
1820 if (fbrk->count == 0 &&
1822 return(brk_counter= 1);
1823 return(++brk_counter);
1827 cl_uc::mk_ebrk(enum brk_perm perm, class cl_address_space *mem,
1828 char op, t_addr addr, int hit)
1833 b= new cl_ev_brk(mem, make_new_brknr(), addr, perm, hit, op);
1839 cl_uc::check_events(void)
1842 for (i= 0; i < events->count; i++)
1844 class cl_ev_brk *brk=
1845 dynamic_cast<class cl_ev_brk *>(events->object_at(i));
1848 sim->stop(resBREAKPOINT);
1854 *----------------------------------------------------------------------------
1857 ERROR_CLASS_DEF_PARENT_ON(err_error, unknown_code, "unknown_code",
1858 error_class_base, ERROR_ON);
1860 cl_error_unknown_code::cl_error_unknown_code(class cl_uc *the_uc)
1863 classification= &error_unknown_code_class;
1867 cl_error_unknown_code::print(class cl_commander *c)
1869 c->dd_printf(get_type_name());
1870 c->dd_printf(": unknown instruction code at ");
1873 c->dd_printf(uc->rom->addr_format, PC);
1875 c->dd_printf(uc->rom->data_format, uc->rom->get(PC));
1879 c->dd_printf("0x%06x", PC);