1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
|
#!/usr/bin/tclsh
# Part of MCU 8051 IDE ( http://https://sourceforge.net/projects/mcu8051ide/ )
############################################################################
# Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 by Martin Ošmera #
# martin.osmera@gmail.com #
# #
# Copyright (C) 2014 by Moravia Microsystems, s.r.o. #
# martin.osmera@gmail.com #
# #
# 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 2 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, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################
# >>> File inclusion guard
if { ! [ info exists _SIMULATOR_TCL ] } {
set _SIMULATOR_TCL _
# <<< File inclusion guard
# --------------------------------------------------------------------------
# DESCRIPTION
# Implements 8051 Simulator environment.
# Object constist of GUI panel of controls (class Simulator_GUI)
# and Simulator Engine (class Simulator_ENGINE).
# --------------------------------------------------------------------------
# --------------------------------------------------------------------------
# This file was modified & fixed by Kostya V. Ivanov <kostya@istcom.spb.ru>
#
# Special thanks to Kostya V. Ivanov !
# --------------------------------------------------------------------------
# Load sources
source "${::LIB_DIRNAME}/simulator/engine/engine_core.tcl" ;# Simulator engine
source "${::LIB_DIRNAME}/simulator/simulator_gui.tcl" ;# Simulator panel
source "${::LIB_DIRNAME}/simulator/interruptmonitor.tcl" ;# Interrupt monitor
source "${::LIB_DIRNAME}/simulator/uart_monitor.tcl" ;# UART monitor
source "${::LIB_DIRNAME}/simulator/sfrmap.tcl" ;# SFR Map monitor
source "${::LIB_DIRNAME}/simulator/stopwatch.tcl" ;# Stopwatch
source "${::LIB_DIRNAME}/simulator/bitmap.tcl" ;# Map of bit addressable area
source "${::LIB_DIRNAME}/simulator/stackmonitor.tcl" ;# Stack monitor
class Simulator {
inherit Simulator_GUI Simulator_ENGINE InterruptMonitor SFRMap Stopwatch BitMap StackMonitor UARTMonitor
## COMMON
public common highlight_color {#DD8800} ;# Foreground color for changed registers
public common normal_color {#000000} ;# Foreground color for unchanged registers
public common error_dialog_project ;# Object: $this for current addressing error dialog
public common not_again_val 0 ;# Bool: Value of checkbutton "Do not shot this dialog again"
public common reverse_run_steps 10 ;# Int: Number of steps which can be taken back
public common ignore_stack_overflow 0 ;# Bool: Do not show "Stack overflow" dialog
public common ignore_stack_underflow 0 ;# Bool: Do not show "Stack underflow" dialog
public common ignore_watchdog_reset 0 ;# Bool: Ignore reset invoked by watchdog overflow
public common ignore_read_from_wr_only 0 ;# Bool: Ignore reading from read only register
public common ignore_invalid_reti 0 ;# Bool: Ignore invalid return fom interrupt
public common ignore_invalid_ins 0 ;# Bool: Ignore invalid instructions
public common ignore_invalid_IDATA 0 ;# Bool: Ignore access to unimplemented IDATA memory
public common ignore_invalid_EDATA 0 ;# Bool: Ignore access to unimplemented EDATA memory
public common ignore_invalid_XDATA 0 ;# Bool: Ignore access to unimplemented XDATA memory
public common ignore_invalid_BIT 0 ;# Bool: Ignore access to unimplemented bit
public common ignore_invalid_CODE 0 ;# Bool: Ignore access to unimplemented CODE memory
public common ignore_invalid_USB 0 ;# Bool: Ignore "UART: Frame discarded"
public common ignore_invalid_UMC 0 ;# Bool: Ignore "UART mode has been changed while UART was engaged"
public common ignore_invalid_TMC 0 ;# Bool: Ignore "Timer mode has been changed while timer was running"
public common ignore_invalid_brkpoints 0 ;# Bool: Do not warn user about invalid (unreachable) breakpoints
public common ignore_EEPROM_WR_fail 0 ;# Bool: Ignore EEPROM write failure (due to EECON.WRTINH, EECON.RDYBSY or EECON.EEMWE)
public common ignore_EEPROM_WR_abort 0 ;# Bool: Ignore EEPROM write cycle abort
public common undefined_value 2 ;# Int: 2 == Random; 1 == 255; 0 == 0
# Normal font for error dialog
public common error_normal_font [font create \
-family {helvetica} \
-size [expr {int(-14 * $::font_size_factor)}] \
]
# Bold font for error dialog
public common error_bold_font [font create \
-family $::DEFAULT_FIXED_FONT \
-size [expr {int(-14 * $::font_size_factor)}] \
-weight bold \
]
# Header font for error dialog
public common error_header_font [font create \
-family {helvetica} \
-size [expr {int(-17 * $::font_size_factor)}] \
-weight bold \
]
# Main header font
public common error_main_header [font create \
-family {helvetica} \
-size [expr {int(-20 * $::font_size_factor)}] \
-weight bold \
]
## PRIVATE
private variable widgets ;# Array of widgets related to MCU registers
private variable highlight_ena 0 ;# Enable regsters highlighting
private variable error_dialog_textwdg ;# Widget: Text widget used in error dialog
private variable addr_error_save_type ;# Type of file to save
# Bool: Related to warnings dialogues
private variable ignore_warnings_related_to_changes_in_SFR 0
## Obejct constructor
constructor {} {
}
## Synchronize GUI with Engine, Entries: PC, Clock and Watchdog
# @return void
public method Simulator_sync_PC_etc {} {
# Disable GUI synchronization
$this SimGUI_disable_sync
# Get new and old value of Program Counter
set new_val [getPC]
set original_val [subst -nocommands "\$::Simulator_GUI::ENV${obj_idx}_PC_dec"]
# Display new value of PC and highlight it if it has changed
set ::Simulator_GUI::ENV${obj_idx}_PC_dec $new_val
if {$original_val != $new_val} {
if {$highlight_ena} {
mark_entry PC
}
$this sim_eval_PC dec $new_val
}
# Mode program pointer in code mememory hex editor
::X::program_counter_changed $this $new_val
# Synchronize Watchdog
if {[$this get_feature_available wtd]} {
set val [format %X [$this simulator_getWatchDogTimerValue]]
set len [string length $val]
if {$len != 4} {
set val "[string repeat {0} [expr {4 - $len}]]$val"
}
set ::Simulator_GUI::ENV${obj_idx}_WatchDog $val
$this simulator_evaluate_wtd_onoff_switch
$this watchdog_validate $val
if {[$this get_feature_available wdtcon] || [$this get_feature_available wdtprg]} {
set val [format %X [$this simulator_getWatchDogPrescalerValue]]
if {[string length $val] == 1} {
set val "0$val"
}
set ::Simulator_GUI::ENV${obj_idx}_WatchDogP $val
}
}
# Synchronize Clock
Simulator_sync_clock
# Enable GUI synchronization
$this SimGUI_enable_sync
}
## Synchronize GUI with Engine, Entry: Time
# @return void
public method Simulator_sync_clock {} {
# Display the new value of the clock
if {[$this cget -P_option_clock] != {}} {
set ::Simulator_GUI::ENV${obj_idx}_TIME [getTime]
}
}
## Set new clock value (entrybox: Clock)
# @parm Int value - new clock frequency 0..99999 (kHz)
# @return void
public method Simulator_set_clock {value} {
set ::Simulator_GUI::ENV${obj_idx}_CLOCK $value
}
## Synchronize particular register in Internal Data Memory
# @parm Int addr - address of register to synchronize
# @return void
public method Simulator_sync_reg {addr} {
Simulator_GUI_sync I $addr
}
## Synchronize particular register in SFR area
# @parm Int addr - address of register to synchronize
# @return void
public method Simulator_sync_sfr {addr} {
Simulator_GUI_sync S $addr
}
## Synchronize particular register in External and Expanded Data Memory
# @parm Int addr - address of register to synchronize
# @return void
public method Simulator_XDATA_sync {addr} {
# Convert address to 4 and 3 digits HEX number
set addr [format "%X" $addr]
set len [string length $addr]
if {$len < 4} {
$this rightPanel_watch_sync "[string repeat 0 [expr {3 - $len}]]$addr"
set addr "[string repeat 0 [expr {4 - $len}]]$addr"
}
# Synchronize with HEX Editor
X::sync_xram_mem_window $addr $this
# Synchronize with C variables view
$this cvarsview_sync F [expr "0x$addr"]
}
## Synchronize particular register in Internal Data Memory or SFR area
# @pamr Char type - I == IDATA; S == SFR
# @parm Int addr - address of register to synchronize
# @return void
public method Simulator_GUI_sync {type addr} {
# Internal RAM
if {$type == {I}} {
# Get new and old value
set new_val [getDataDEC $addr]
set original_val [simulator_hexeditor get_values $addr $addr]
if {$original_val == {}} {
set original_val 0
}
# Display (and highlight) new value
if {$addr < 32} {
set hexvalue [getData $addr]
set ::Simulator_GUI::ENV${obj_idx}_DATA($addr) $hexvalue
set bnk [expr {$addr / 8}]
set idx [expr {$addr % 8}]
if {$bnk == [getBank]} {
set ::Simulator_GUI::ENV${obj_idx}_R${idx} $hexvalue
if {$highlight_ena && ($original_val != $new_val)} {
mark_entry R${idx}
}
}
}
# Update IDATA hex editor
simulator_hexeditor setValue $addr $new_val
if {$highlight_ena && ($original_val != $new_val)} {
simulator_hexeditor setHighlighted $addr 1
}
# Update RAM help window
set hex_addr [format %X $addr]
if {[string length $hex_addr] == 1} {
set hex_addr "0$hex_addr"
}
help_window_update $hex_addr $new_val
# Synchronize with C variables view
if {$addr < 128} {
$this cvarsview_sync E $addr
} else {
$this cvarsview_sync G $addr
}
# Synchronize watches on right panel and stack monitor
$this rightPanel_watch_sync $hex_addr
$this stack_monitor_sync $addr
# Synchronize with map of bit addressable area
$this bitmap_sync $addr
# Done ..
return
# Program Status Word
} elseif {$addr == 208} {
# Get value of PSW
set psw [getSfr 208]
set psw [expr "0x$psw"]
# Evaluate separate bits and highlight them
set mask 256
foreach bit {C AC F0 RS1 RS0 OV - P} {
# Evaluate bit mask
set mask [expr {$mask >> 1}]
# Skip NULL registers
if {$bit == {-}} {continue}
# Determinate boolean value of the bit
if {$psw == 0} {
set bool 0
} else {
if {[expr {$psw & $mask}] > 0} {
set bool 1
} else {
set bool 0
}
}
# Set bit value
set ::Simulator_GUI::ENV${obj_idx}_SFR($bit) $bool
# Take care of bit color
if {$bool} {
$Simulator_panel_parent._PSW_$bit configure -fg $on_color
} else {
$Simulator_panel_parent._PSW_$bit configure -fg $off_color
}
}
# Set Registers values
for {set i 0} {$i < 8} {incr i} {
set idx [expr {[getBank] * 8 + $i}]
set hexvalue [getData $idx]
set ::Simulator_GUI::ENV${obj_idx}_R${i} $hexvalue
}
# Synchronize with SFR watches
$this sfr_watches_sync 208 $psw
$this sfrmap_map_sync 208 $psw
$this rightPanel_watch_sync_sfr 208
# Update RAM help window
help_window_update {D0 SFR} $psw
# Synchronize with C variables view
$this cvarsview_sync I 208
# Done ..
return
# Special Function Registers
} else {
# Evaluate register name
set name_resolved 1
switch -- $addr {
128 {set regName {P0} }
144 {set regName {P1} }
160 {set regName {P2} }
176 {set regName {P3} }
192 {set regName {P4} }
131 {set regName {DPH} }
130 {set regName {DPL} }
133 {set regName {DP1H} }
132 {set regName {DP1L} }
153 {set regName {SBUFR} }
409 {set regName {SBUFT} }
152 {set regName {SCON} }
141 {set regName {TH1} }
139 {set regName {TL1} }
140 {set regName {TH0} }
138 {set regName {TL0} }
136 {set regName {TCON} }
137 {set regName {TMOD} }
135 {set regName {PCON} }
168 {set regName {IE} }
184 {set regName {IP} }
129 {set regName {SP} }
224 {set regName {A_hex} }
240 {set regName {B_hex} }
200 {set regName {T2CON} }
201 {set regName {T2MOD} }
202 {set regName {RCAP2L} }
203 {set regName {RCAP2H} }
204 {set regName {TL2} }
205 {set regName {TH2} }
162 {set regName {AUXR1} }
166 {set regName {WDTRST} }
142 {set regName {AUXR} }
151 {set regName {ACSR} }
183 {set regName {IPH} }
169 {set regName {SADDR} }
185 {set regName {SADEN} }
213 {set regName {SPCR} }
170 {set regName {SPSR} }
134 {set regName {SPDR} }
150 {set regName {EECON} }
143 {
if {[$this get_feature_available ckcon]} {
set regName {CKCON}
} else {
set regName {CLKREG}
}
}
167 {
if {[$this get_feature_available wdtprg]} {
set regName {WDTPRG}
} else {
set regName {WDTCON}
}
}
default {set name_resolved 0}
}
# Synchronize with SFR watches
set new_val [getSfr $addr]
set dec_val [expr {"0x$new_val"}]
$this sfr_watches_sync $addr $dec_val
# Synchronize with SFR map
$this sfrmap_map_sync $addr $dec_val
# Synchronize with C variables view
$this cvarsview_sync I $addr
# Synchronize with simulator control panel
if {$name_resolved} {
set original_val [subst -nocommands "\$::Simulator_GUI::ENV${obj_idx}_${regName}"]
set ::Simulator_GUI::ENV${obj_idx}_${regName} $new_val
if {$highlight_ena && ($original_val != $new_val)} {
mark_entry $addr
}
# Synchronize special values
switch -- $addr {
152 { ;# SCON
## Synchronize bits FE and SM0
# FE
set bit_FE [$this sim_engine_get_FE]
if {$bit_FE != {}} {
set ::Simulator_GUI::ENV${obj_idx}_SFR(FE) $bit_FE
if {$bit_FE} {
$Simulator_panel_parent._SCON_FE configure -fg $::Simulator_GUI::on_color
} else {
$Simulator_panel_parent._SCON_FE configure -fg $::Simulator_GUI::off_color
}
}
# SM0
if {[$this sim_engine_get_SM0]} {
$Simulator_panel_parent._SCON_SM0 configure -fg $::Simulator_GUI::on_color
set ::Simulator_GUI::ENV${obj_idx}_SFR(SM0) 1
} else {
$Simulator_panel_parent._SCON_SM0 configure -fg $::Simulator_GUI::off_color
set ::Simulator_GUI::ENV${obj_idx}_SFR(SM0) 0
}
}
153 { ;# SBUF
## Synchronize both registers (receive & transmit)
Simulator_GUI_sync S ${::Simulator_ENGINE::symbol(SBUFT)}
}
}
}
# Update RAM help window
set hex_addr [format %X $addr]
if {[string length $hex_addr] == 1} {
set hex_addr "0$hex_addr"
}
help_window_update [list $hex_addr {SFR}] $new_val
$this rightPanel_watch_sync_sfr $addr
# Explicitly call entry box validator
switch -- $addr {
224 {$this sim_eval_AB A hex $new_val}
240 {$this sim_eval_AB B hex $new_val}
205 - 204 - 203 -
202 - 141 - 139 -
140 -
138 {$this validate_Txx $regName $new_val}
128 - 144 - 160 -
176 -
192 {$this sim_eval_Px $regName hex $new_val}
142 - 162 - 151 -
150 - 213 - 170 -
167 - 167 - 183 -
143 - 200 - 201 -
135 - 152 - 168 -
184 - 136 -
137 {$this validate_hex_bitmap_reg $new_val $regName}
}
}
}
## Synchronize all registers in IRAM and SFR
# @return void
public method Simulator_sync {} {
# Synchronize PC and Clock
Simulator_sync_PC_etc
# Synchronize IRAM
set iram_size [lindex [$this cget -procData] 3]
for {set i 0} {$i < $iram_size} {incr i} {
Simulator_sync_reg $i
}
# Synchronize SFR
foreach addr [simulator_get_available_sfr] {
Simulator_GUI_sync S $addr
}
}
## Synchronization after simulator initialization
# @return void
public method Simulator_first_sync {} {
set highlight_ena 0
Simulator_sync
set highlight_ena 1
}
## Reset simulator engine and synchronize all registers
# @parm Int arg - argument for reset procedure
# @return void
public method Simulator_reset {arg} {
# Perform master reset
master_reset $arg
# Synchronize
set highlight_ena 0
Simulator_sync
set highlight_ena 1
# Clear stepback stack
stepback_discard_stack
::X::stepback_button_set_ena 0
# Clear highlight
simulator_clear_highlight
# Clear time entry
set ::Simulator_GUI::ENV${obj_idx}_TIME {}
}
## Clear highlight for all internal registers
# @return void
public method simulator_clear_highlight {} {
simulator_hexeditor clearHighlighting
foreach addr [simulator_get_available_sfr] {
unmark_entry $addr
}
foreach addr {R0 R1 R2 R3 R4 R5 R6 R7} {
unmark_entry $addr
}
unmark_entry PC
}
## Add register entry widget reference to array of registers
# @parm Int addr - Register address
# @parm Widget widget - Register entry widget
# @return void
public method add_sfr_entry {addr widget} {
lappend widgets($addr) $widget
}
## Clear list of registred widgets in simulator control panel
# This list is used for enabling/disabling these widgets on start/shutdown
# @return void
public method sumulator_clear_widgets {} {
array unset widgets
}
## Highlight register entry widget
# @parm Int addr - Register address
# @return void
public method mark_entry {addr} {
# Skip PSW
if {$addr == 208} {return}
foreach wdg $widgets($addr) {
if {[winfo class $wdg] == {TEntry}} {
$wdg configure -style Simulator_HG.TEntry
} else {
$wdg configure -fg $highlight_color
}
}
}
## "Unhighlight" register entry widget
# @parm Int addr - Register address
# @return void
public method unmark_entry {addr} {
# Skip PSW
if {$addr == 208} {return}
foreach wdg $widgets($addr) {
if {[winfo class $wdg] == {TEntry}} {
$wdg configure -style Simulator.TEntry
} else {
$wdg configure -fg $normal_color
}
}
}
## Invokes error message "Undefined result"
# @parm Char location - Memory type
# D == IDATA direct addressing
# I == IDATA indirect addressing (or operations on stack)
# B == Bit area
# E == ERAM
# X == XDATA
# C == CODE
# @parm Int address - Memory address (0..65536)
# @return void
public method invalid_addressing_dialog {location address} {
# Gain error and processor details
set addr_dec $address
set address [format %X $address]
set len [string length $address]
if {$len < 4} {
set address "[string repeat 0 [expr {4 - $len}]]$address"
}
set processor [$this cget -P_option_mcu_type]
switch -- $location {
{D} { ;# IDATA direct addressing
set conf_variable {ignore_invalid_IDATA}
set addressing {direct }
if {$addr_dec > 127} {
set memory {special function registers area}
set mem {SFR}
} else {
set mem {IDATA}
set memory {internal data memory}
}
}
{I} { ;# IDATA indirect addressing (or operations with stack)
set conf_variable {ignore_invalid_IDATA}
set addressing {indirect }
set memory {internal data memory}
set mem {IDATA}
}
{B} { ;# Bit area
set conf_variable {ignore_invalid_BIT}
set addressing {direct }
set memory {bit addressable area}
set mem {Bit area}
}
{X} { ;# XDATA
set conf_variable {ignore_invalid_XDATA}
set addressing {indirect }
set memory {external data memory}
set mem {XDATA}
}
{C} { ;# CODE
set conf_variable {ignore_invalid_CODE}
set addressing {}
set memory {program memory}
set mem {CODE}
}
}
# Create dialog window
set win [toplevel .undefined_result -class {Error dialog} -bg ${::COMMON_BG_COLOR}]
# Create dialog header
set top_frame [frame $win.top_frame]
pack [label $top_frame.left \
-image ::ICONS::32::messagebox_critical \
] -side left
pack [label $top_frame.right \
-text [mc "Undefined result"] \
-font $error_main_header \
] -side left -fill x
# Create middle frame (text widget and scrollbar)
set middle_frame [frame $win.middle_frame]
set text_wdg [text $middle_frame.text \
-height 0 -width 0 -font $error_normal_font \
-yscrollcommand "$middle_frame.scrollbar set" \
-wrap word -relief flat -bg ${::COMMON_BG_COLOR} \
-tabstyle wordprocessor \
]
set error_dialog_textwdg $text_wdg
pack $text_wdg -side left -fill both -expand 1
pack [ttk::scrollbar $middle_frame.scrollbar \
-command "$text_wdg yview" \
-orient vertical \
] -side right -fill y -after $text_wdg
# Create bottom frame (buttons: Save as text, Save as XHTML, Ok)
set bottom_frame [frame $win.bottom_frame]
pack [ttk::button $bottom_frame.save_as_txt \
-image ::ICONS::16::ascii \
-compound left \
-text [mc "Save as plain text"] \
-command "$this simulator_addr_error_save T" \
] -side left -padx 2
pack [ttk::button $bottom_frame.save_as_xhtml \
-image ::ICONS::16::html \
-compound left \
-text [mc "Save as XHTML"] \
-command "$this simulator_addr_error_save X" \
] -side left -padx 2
pack [ttk::button $bottom_frame.ok \
-image ::ICONS::16::ok \
-compound left \
-text [mc "Ok"] \
-command "grab release $win; destroy $win" \
] -side right
$text_wdg tag configure tag_bold -font $error_bold_font
$text_wdg tag configure tag_header -font $error_header_font
# Error summary
$text_wdg insert end [mc "Summary:"]
$text_wdg tag add tag_header {insert linestart} insert
$text_wdg insert end [mc "\nYour program tried ${addressing}access to register at address "]
set index [$text_wdg index insert]
$text_wdg insert end [mc "0x%s in $memory" $address]
$text_wdg tag add tag_bold $index insert
$text_wdg insert end [mc ". This register is not implemented on this processor ("]
set index [$text_wdg index insert]
$text_wdg insert end "$processor"
$text_wdg tag add tag_bold $index insert
$text_wdg insert end [mc ") in this configuration. You can continue in simulation but result of this operation is undefined."]
# Error details
$text_wdg insert end [mc "\n\nError details:"]
$text_wdg tag add tag_header {insert linestart} insert
$text_wdg insert end [mc "\n\tTarget memory:\t\t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end [mc $mem]
$text_wdg insert end [mc "\n\tTarget address: \t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end "0x$address ($addr_dec)"
$text_wdg insert end [mc "\n\tLine:\t\t\t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end [$this editor_actLineNumber]
$text_wdg insert end [mc "\n\tFile:\t\t\t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end [$this editor_procedure {} cget -filename]
$text_wdg insert end [mc "\n\tProject:\t\t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end [$this cget -projectName]
# Processor details
$text_wdg insert end [mc "\n\nProcessor details:"]
$text_wdg tag add tag_header {insert linestart} insert
$text_wdg insert end [mc "\n\tType:\t\t\t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end [$this cget -P_option_mcu_type]
$text_wdg insert end [mc "\n\tRam size:\t\t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end "[lindex [$this cget -procData] 3] B"
$text_wdg insert end [mc "\n\tProgram memory: \t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end "[expr [lindex [$this cget -procData] 2] * 1024 + [$this cget -P_option_mcu_xcode]] B"
$text_wdg insert end [mc "\n\tExternal memory:\t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end "[$this cget -P_option_mcu_xdata] B"
$text_wdg insert end [mc "\n\tExpanded memory:\t"]
$text_wdg tag add tag_bold {insert linestart} insert
$text_wdg insert end "[lindex [$this cget -procData] 8] B"
# Disable text widget and pack dialog frames
$text_wdg configure -state disabled
pack $top_frame -pady 5
pack $middle_frame -fill both -expand 1 -pady 15 -padx 10
pack [ttk::separator $win.sep -orient horizontal] -fill x -padx 5 -pady 5
set ::Simulator::not_again_val 0
pack [checkbutton $win.not_again_checkbutton \
-text [mc "Do not show this dialog again"] \
-variable ::Simulator::not_again_val \
-command "::configDialogues::simulator::set_variable $conf_variable \$::Simulator::not_again_val" \
] -anchor w -padx 15 -pady 5
DynamicHelp::add $win.not_again_checkbutton \
-text [mc "See simulator configuration dialog\nMain Menu -> Configure -> Simulator"]
pack $bottom_frame -fill x -side bottom -after $middle_frame -padx 5 -pady 5
# Show dialog window
bell
focus -force $bottom_frame.ok
wm title $win [mc "Undefined result - MCU 8051 IDE"]
wm iconphoto $win ::ICONS::16::no
wm minsize $win 470 400
wm protocol $win WM_DELETE_WINDOW "
grab release $win
destroy $win
"
wm transient $win .
wm geometry $win "+[expr {([winfo screenwidth .] - 450) / 2}]+[expr {([winfo screenheight .] - 400) / 2}]"
raise $win
catch {
grab $win
}
tkwait window $win
}
## Invoke dialog to save contents of dialog "Undefined result" as plain text or XHTML
# @parm Char type - T == plain text; X == XHTML
# @return void
public method simulator_addr_error_save {type} {
set addr_error_save_type $type
set error_dialog_project $this
if {$type == {T}} {
set init {error.log}
set filetypes [list \
[list [::mc "Log files"] {*.log}] \
[list [::mc "All files"] {*}] \
]
} else {
set init {error.html}
set filetypes [list \
[list [::mc "HTML files"] {*.html}] \
[list [::mc "All files"] {*}] \
]
}
# Invoke the file selection dialog
catch {delete object fsd}
KIFSD::FSD fsd \
-title [mc "Save error log - MCU 8051 IDE"] \
-directory [$this cget -projectPath] \
-initialfile $init -defaultmask 0 \
-filetypes $filetypes -multiple 0
# Open file after press of OK button
fsd setokcmd {
# Get filename
set filename [::Simulator::fsd get]
if {!$::MICROSOFT_WINDOWS} { ;# POSIX way
if {![regexp "^(~|/)" $filename]} {
set filename "[${::Simulator::error_dialog_project} cget -ProjectDir]/$filename"
}
} else { ;# Microsoft windows way
if {![regexp {^\w:} $filename]} {
set filename [file join [${::Simulator::error_dialog_project} cget -ProjectDir] $filename]
}
}
set filename [file normalize $filename]
# Overwrite ?
if {[file exists $filename]} {
if {[tk_messageBox \
-icon question \
-type yesno \
-title [mc "Overwrite file ?"] \
-parent .undefined_result \
-message [mc "Specified file does already exist,\ndo you want to overwrite it ?"]
] != {yes}} then {
return
}
}
# Open the specified file
if {[catch {
${::Simulator::error_dialog_project} simulator_save_error_log $filename
} result]} then {
puts stderr $result
tk_messageBox \
-type ok \
-icon warning \
-parent .undefined_result \
-title [mc "Permission denied"] \
-message [mc "Unable to access file:\n%s" $filename]
}
}
# activate the dialog
fsd activate
}
## Wrap lines in the given text to the specified length
# @parm Int length - Maximum line length
# @parm String txt - Text to wrap
# @return String - Wrapped text
private method line_wrap {length txt} {
set result {}
foreach line [split $txt "\n"] {
set len [string length $line]
if {$len <= $length} {
append result $line "\n"
continue
}
while {$len > $length} {
append result [string range $line 0 [expr {$length - 1}]] "\n"
set line [string range $line $length end]
set len [string length $line]
}
}
return $result
}
## Save contents of dialog "Undefined result" as plain text or XHTML
# Type of file depends on variable $addr_error_save_type
# @parm String filename - target file
# @return void
public method simulator_save_error_log {filename} {
set file [open $filename w]
## SAVE AS PLAIN TEXT
if {$addr_error_save_type == {T}} {
puts -nonewline $file [line_wrap 70 [$error_dialog_textwdg get 1.0 end]]
## SAVE AS XHTML
} else {
# Local variables
set end [$error_dialog_textwdg index end] ;# Widget end index
set last_index 0 ;# Current position (by characters)
set line(1) 0 ;# Map of indexes ($line(num) == scalar_index)
# Create XHTML declaration and header
set html "<?xml version='1.0' encoding='utf-8' standalone='no'?>\n"
append html "<!DOCTYPE html PUBLIC\n"
append html "\t'-//W3C//DTD XHTML 1.1//EN'\n"
append html "\t'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>\n"
append html "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>\n"
append html "\t<head>\n"
append html "\t\t<title>$filename</title>\n"
append html "\t\t<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=UTF-8\">\n"
append html "\t\t<meta name=\"Generator\" content=\"${::APPNAME}\" />\n"
append html "\t</head>\n"
append html "\t<body>\n"
puts -nonewline $file $html
set html {}
# Create map of indexes
for {set i 1; set j 2} {$i < $end} {incr i; incr j} {
# Determinate last column of the line
set idx [$error_dialog_textwdg index [list $i.0 lineend]]
regexp {\d+$} $idx idx
# Adjust map of indexes
incr last_index $idx
incr last_index
set line($j) $last_index
}
## Determinate highlighting tag ranges
set ranges {}
foreach tag {tag_header tag_bold} {
# Local variables
set range [$error_dialog_textwdg tag ranges $tag] ;# List of tag ranges
set len [llength $range] ;# Number of ranges
# If the tag isn't present in the text -> skip
if {$len == 0} {continue}
# Adjust tag name
if {$tag == {tag_header}} {
set tag {h2}
} elseif {$tag == {tag_bold}} {
set tag {b}
}
for {set i 0} {$i < $len} {incr i} {
lappend ranges [list [lindex $range $i] $tag 1]
incr i
lappend ranges [list [lindex $range $i] $tag 0]
}
}
set ranges [lsort -command "::FileList::editor__sort_tag_ranges" $ranges]
# Write XHTML tags to plain text
set i 0
set html [$error_dialog_textwdg get 1.0 end]
foreach range $ranges {
# Local variables
set idx [split [lindex $range 0] {.}] ;# Text index
set row [lindex $idx 0] ;# Line number
set col [lindex $idx 1] ;# Column number
# Determinate scalar text index
set idx [expr {$line($row) + $col}]
# Skip unused tags
if {$idx < 0} {set idx 0}
# Deterinate string to insert
if {[lindex $range 2]} {
set tag [lindex $range 1]
} else {
set tag "/[lindex $range 1]"
}
# Insert XHTML tag into the text
set char [string index $html $idx]
set html [string replace $html $idx $idx "<$tag>$char"]
incr i
}
regsub -all {\n} $html "<br />\n" html
append html "\n\t</body>\n"
append html "</html>\n"
puts -nonewline $file $html
}
close $file
}
## Set flag: ignore_warnings_related_to_changes_in_SFR
# @parm Bool value - New value
# @return void
public method set_ignore_warnings_related_to_changes_in_SFR {value} {
set ignore_warnings_related_to_changes_in_SFR $value
}
## Invoke simulator warning dialog "UART: Frame discarded"
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @return void
public method simulator_uart_invalid_stop_bit {pc line} {
if {$ignore_warnings_related_to_changes_in_SFR || ![$this sim_is_busy]} {
return
}
if {$line == {}} {
set line {-}
}
simulator_warning_dialog \
ignore_invalid_USB \
[mc "UART: Frame discarded (according to MCS-51 manual)\n"] $pc $line
}
## Invoke simulator warning dialog "UART mode has been changed while UART was engaged"
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @return void
public method simulator_invalid_uart_mode_change {pc line} {
if {$ignore_warnings_related_to_changes_in_SFR || ![$this sim_is_busy]} {
return
}
if {$line == {}} {
set line {-}
}
simulator_warning_dialog \
ignore_invalid_UMC \
[mc "UART mode has been changed while UART was engaged.\n"] $pc $line
}
## Invoke simulator warning dialog "Timer mode has been changed while timer was running"
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @parm Int timer - Timer number (0/1/2)
# @return void
public method simulator_invalid_timer_mode_change {timer pc line} {
if {$ignore_warnings_related_to_changes_in_SFR || ![$this sim_is_busy]} {
return
}
if {$line == {}} {
set line {-}
}
simulator_warning_dialog \
ignore_invalid_TMC \
[mc "Timer mode has been changed while timer was running.\nIt is important to stop timer/counter before changing modes.\n\nTimer number: %s\n" $timer] $pc $line
}
## Invoke watchdog reset dialog
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @return void
public method simulator_watchdog_reset {pc line} {
if {$line == {}} {
set line {-}
}
simulator_warning_dialog \
ignore_watchdog_reset \
[mc "WATCHDOG OVERFLOW\n"] $pc $line
}
## Invokes dialog "Stack Overflow" / "Stack underflow"
# @parm String type - {over} == overflow; {under} == underflow
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @return void
public method simulator_stack_warning {type pc line} {
if {$line == {}} {
set line {-}
}
if {$type == {over}} {
set foo {overflow}
set conf_variable {ignore_stack_overflow}
} else {
set foo {underflow}
set conf_variable {ignore_stack_underflow}
}
simulator_warning_dialog \
$conf_variable \
[mc "Stack $foo\n"] $pc $line
}
## Invoke dialog "Invalid instruction OP code"
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @return void
public method simulator_invalid_instruction {pc line} {
$this simulator_warning_dialog \
ignore_invalid_ins \
[mc "Invalid instruction OP code\n"] $pc $line
}
## Invoke dialog "Reading from write-only register"
# @parm Int addr - Register address
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @return void
public method simulator_reading_wr_only {addr pc line} {
if {$line == {}} {
set line {-}
}
simulator_warning_dialog \
ignore_read_from_wr_only \
[mc "Unable to read write-only register.\nRandom value returned.\n\nRegister:\t\t0x%s" [format %X $addr]] $pc $line
}
## Invoke dialog "EEPROM programming cycle abort"
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @return void
public method simulator_EEPROM_WR_abort {pc line} {
if {$line == {}} {
set line {-}
}
simulator_warning_dialog \
ignore_EEPROM_WR_abort \
[mc "Data EEPROM write cycle aborted\n"] $pc $line
}
## Invoke dialog "EEPROM write failed"
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @return void
public method simulator_EEPROM_WR_fail {pc line} {
if {$line == {}} {
set line {-}
}
simulator_warning_dialog \
ignore_EEPROM_WR_fail \
[mc "Unable to initialize EEPROM programming cycle\nbecause EEMWE, RDYBSY and WRTINH must be set\n"] $pc $line
}
## Invoke dialog "Invalid return from interrupt"
# @parm Int pc - Value of program counter
# @parm Int line - Line in source code where this error occurred
# @return void
public method simulator_invalid_reti_dlg {pc line} {
if {$line == {}} {
set line {-}
}
simulator_warning_dialog \
ignore_invalid_reti \
[mc "Invalid return from interrupt"] $pc $line
}
## Invoke simulator warning dialog (like tk_messageBox)
# @parm String conf_variable - Configuration variable for disabling this dialog
# @parm String text - Text to show
# @parm Int pc - Value of program counter
# @parm List line - Line in source code where this error occurred
# @return void
public method simulator_warning_dialog {conf_variable text pc line} {
# Create dialog window
set win {.simulator_warning_dialog}
if {[winfo exists $win]} {
destroy $win
}
toplevel $win -class {Error dialog} -bg ${::COMMON_BG_COLOR}
## Create dialog icon and text
set top_frame [frame $win.top_frame]
pack [label $top_frame.left \
-image ::ICONS::32::messagebox_critical \
] -side left -padx 10
append text "\n[mc {PC:}]\t\t0x" [format %X $pc] "\n[mc {Line:}]\t\t" [lindex $line 0] "\n[mc {File:}]\t\t" [file tail [$this simulator_get_filename [lindex $line 1]]]
pack [label $top_frame.right \
-justify left \
-text $text \
] -side left -fill x
## Create bottom frame
# Checkbutton "Do not show this dialog again"
set bottom_frame [frame $win.bottom_frame]
set ::Simulator::not_again_val 0
pack [checkbutton $bottom_frame.not_again_checkbutton \
-text [mc "Do not show this dialog again"] \
-variable ::Simulator::not_again_val \
-command "::configDialogues::simulator::set_variable $conf_variable \$::Simulator::not_again_val" \
] -anchor w -side left -anchor w
DynamicHelp::add $bottom_frame.not_again_checkbutton \
-text [mc "See simulator configuration dialog\nMain Menu -> Configure -> Simulator"]
# Button "Ok"
pack [ttk::button $bottom_frame.ok \
-image ::ICONS::16::ok \
-compound left \
-text [mc "Ok"] \
-command "grab release $win; destroy $win" \
] -side right -anchor e
# Pack dialog fames
pack $top_frame -padx 5 -pady 5 -fill x -side top
pack $bottom_frame -padx 5 -pady 5 -fill x -side bottom
# Show dialog window
bell
focus -force $bottom_frame.ok
wm iconphoto $win ::ICONS::16::status_unknown
wm title $win [mc "Simulator warning"]
wm minsize $win 350 100
wm protocol $win WM_DELETE_WINDOW "
grab release $win
destroy $win"
wm transient $win .
wm geometry $win "+[expr {([winfo screenwidth .] - 350) / 2}]+[expr {([winfo screenheight .] - 100) / 2}]"
raise $win
catch {
grab $win
}
tkwait window $win
}
## Detect all invalid breakpoints (breakpoints at unreachable locations) and report them via messages panel
# @return void
public method report_invalid_breakpoints {} {
if {$ignore_invalid_brkpoints} {
return
}
$this messages_text_append "\n"
foreach fn_ln [$this simulator_getInvalidBreakpoints] {
$this messages_text_append "[file tail [$this simulator_get_filename [lindex $fn_ln 0]]]:[lindex $fn_ln 1]: warning: Invalid breakpoint"
}
}
}
# >>> File inclusion guard
}
# <<< File inclusion guard
|