Debian Codegraph @

lib/leftpanel/sfrwatches.tcl

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/tclsh
2 # Part of MCU 8051 IDE ( http://http://www.moravia-microsystems.com/mcu8051ide )
3
4 ############################################################################
5 # Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 by Martin Ošmera #
6 # [email protected] #
7 # #
8 # Copyright (C) 2014 by Moravia Microsystems, s.r.o. #
9 # [email protected] #
10 # #
11 # This program is free software; you can redistribute it and#or modify #
12 # it under the terms of the GNU General Public License as published by #
13 # the Free Software Foundation; either version 2 of the License, or #
14 # (at your option) any later version. #
15 # #
16 # This program is distributed in the hope that it will be useful, #
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
19 # GNU General Public License for more details. #
20 # #
21 # You should have received a copy of the GNU General Public License #
22 # along with this program; if not, write to the #
23 # Free Software Foundation, Inc., #
24 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
25 ############################################################################
26
27 # >>> File inclusion guard
28 if { ! [ info exists _SFRWATCHES_TCL ] } {
29 set _SFRWATCHES_TCL _
30 # <<< File inclusion guard
31
32 # --------------------------------------------------------------------------
33 # DESCRIPTION
34 # Provides SFR watches for left panel
35 # --------------------------------------------------------------------------
36
37 class SFRWatches {
38
39 ## COMMON
40 # Font for addresses and register names
41 public common main_font [font create \
42 -family $::DEFAULT_FIXED_FONT \
43 -size [expr {int(-14 * $::font_size_factor)}] \
44 -weight bold \
45 ]
46 # Just another font but not bold
47 public common roman_font [font create \
48 -family $::DEFAULT_FIXED_FONT \
49 -size [expr {int(-14 * $::font_size_factor)}] \
50 ]
51 # Fonr for register entry boxes
52 public common entry_font [font create \
53 -family $::DEFAULT_FIXED_FONT \
54 -size [expr {int(-12 * $::font_size_factor)}] \
55 -weight bold \
56 ]
57
58 ## PRIVATE
59 private variable text_widget ;# Widget: Text widget containing SFR watches
60 private variable scrollbar {} ;# Widget: Scrollbar for $text_widget
61 private variable search_entry ;# Widget: Search entry box at the bottom of the panel
62 private variable search_clear_but ;# Widget: Button "Clear" at the bottom of the panel
63 private variable main_left_frame ;# Widget: Frame containing $text_widget and its header label
64 private variable entry_count 0 ;# Int: Number of SFRs
65 private variable validation_ena 1 ;# Bool: SFR entry box validation enabled
66 private variable haddr2idx ;# Array: $haddr2idx($hex_addr) --> row_in_text_widget - 1
67 private variable addr2idx ;# Array: $addr2idx($dec_addr) --> row_in_text_widget - 1
68 private variable reg2idx ;# Array: $addr2idx($register_name_uppercase) --> row_in_text_widget - 1
69 private variable last_selected_line 0 ;# Int: Selected row in the text widget
70 private variable search_ena 1 ;# Bool: Search enabled
71 private variable menu ;# Widget: Popup menu for the text widget
72
73 # Variables related to object initialization
74 private variable parent ;# Parent GUI object (tempotary variable)
75 private variable sfrw_gui_initialized 0 ;# GUI ready
76
77 constructor {} {
78 }
79
80 destructor {
81 if {$sfrw_gui_initialized} {
82 menu_Sbar_remove $menu
83 }
84 }
85
86 ## Prepare object for creating its GUI
87 # @parm Widget _parent - parent container (some frame)
88 # @return void
89 public method PrepareSFRWatches {_parent} {
90 set parent $_parent
91 set sfrw_gui_initialized 0
92 }
93
94 ## Initialize SFR watches GUI
95 # @return void
96 public method CreateSFRWatchesGUI {} {
97 if {$sfrw_gui_initialized} {return}
98 set sfrw_gui_initialized 1
99
100 set validation_ena 0
101 create_GUI
102 fill_gui
103 set validation_ena 1
104
105 if {![$this is_frozen]} {
106 sfr_watches_disable
107 }
108 }
109
110 ## Create entry box to embedd in the text widget
111 # @parm Int i - Entry index (row - 1)
112 # @parm String type - Entry type (hex or dec)
113 # @parm Int addr - SFR address (128..255)
114 # @parm String reg - SFR name (e.g. PSW)
115 # @return Widget - Created entry box
116 private method create_entry {i type addr reg} {
117 # Determine entry box width
118 if {$type == {hex}} {
119 set width 2
120 } else {
121 set width 3
122 }
123
124 # Create entry widget
125 set entry [entry $text_widget.${type}_entry_${i} \
126 -width $width -font $entry_font \
127 -bg {#FFFFFF} -validate all \
128 -takefocus 0 -highlightthickness 0 \
129 -disabledbackground {#FFFFFF} \
130 -vcmd "$this sfr_watches_validate ${type} $addr %P" \
131 -bd 0 -justify right \
132 ]
133
134 # Perform name correction for accumulators
135 if {$reg == {A} || $reg == {B}} {
136 append reg {_hex}
137 }
138
139 # Set event bindins
140 bind $entry <Motion> {help_window_show %X %Y}
141 bind $entry <Leave> {help_window_hide}
142 bind $entry <FocusIn> "$this unmark_entry $addr"
143 bind $entry <Enter> "$this create_help_window_ram $reg"
144 bind $entry <Button-1> "$this sfr_watches_select_line 0 [expr {$i + 1}] $type"
145 bind $entry <Key-Up> "$this sfr_watches_up $type 1"
146 bind $entry <Key-Down> "$this sfr_watches_down $type 1"
147 bind $entry <Key-Next> "$this sfr_watches_down $type 4"
148 bind $entry <Key-Prior> "$this sfr_watches_up $type 4"
149 bind $entry <Button-4> "$text_widget yview scroll -5 units"
150 bind $entry <Button-5> "$text_widget yview scroll +5 units"
151 if {$type == {hex}} {
152 bind $entry <Key-Right> "
153 focus $text_widget.dec_entry_${i}
154 $text_widget.dec_entry_${i} selection clear
155 update
156 $text_widget.dec_entry_${i} icursor 0"
157 } else {
158 bind $entry <Key-Left> "
159 focus $text_widget.hex_entry_${i}
160 $text_widget.hex_entry_${i} selection clear
161 update
162 $text_widget.dec_entry_${i} icursor end"
163 }
164
165 return $entry
166 }
167
168 ## Complite GUI initialization (load all SFRs into the text widget)
169 # @return void
170 private method fill_gui {} {
171 set entry_count 0
172 set validation_ena 0
173
174 # Iterate over defined SFRs ({{addr name} ... })
175 foreach reg [$this simulator_get_sfrs] {
176 # Determine hexadecimal address
177 set addr [lindex $reg 0]
178 set hex_addr [format %X $addr]
179 if {[string length $hex_addr] == 1} {
180 set hex_addr "0$hex_addr"
181 } elseif {[string length $hex_addr] == 3} {
182 set hex_addr [string replace $hex_addr 0 0]
183 }
184 # Determine register name and make it 8 characters long
185 set reg [lindex $reg 1]
186 set reg_org $reg
187 switch -- $reg {
188 SBUFR {
189 set reg {SBUF R}
190 }
191 SBUFT {
192 set reg {SBUF T}
193 }
194 default {
195 set reg $reg
196 }
197 }
198 append reg [string repeat { } [expr {8 - [string length $reg]}]]
199
200 # Register this SFR
201 set haddr2idx($hex_addr) $entry_count
202 set addr2idx($addr) $entry_count
203 set reg2idx($reg_org) $entry_count
204
205 # Insert address and name into the text widget
206 $text_widget insert end $hex_addr
207 $text_widget insert end { }
208 $text_widget insert end $reg
209
210 # Set highlighting tags
211 set line [expr {int([$text_widget index insert])}]
212 $text_widget tag add tag_addr $line.0 $line.2
213 $text_widget tag add tag_name $line.3 $line.11
214
215 # Create and insert embedded entry boxes
216 set entry [create_entry $entry_count hex $addr $reg_org]
217 $entry insert 0 [$this getSfr $addr]
218 $text_widget window create end -window $entry -pady 0
219 $text_widget insert end { }
220 set entry [create_entry $entry_count dec $addr $reg_org]
221 $entry insert 0 [$this getSfrDEC $addr]
222 $text_widget window create end -window $entry -pady 0
223
224 # Finalize this row
225 $text_widget insert end "\n"
226 incr entry_count
227 }
228
229 # Remove the last line (empty line) and disable the text widget
230 $text_widget delete end-1l end
231 set validation_ena 1
232 }
233
234 ## Set value of SFR at current line to the specified value
235 # @parm String value - Hexadecimal value
236 # @return void
237 public method sfr_watches_set_current_to {value} {
238 set idx [expr {$last_selected_line - 1}]
239 set addr [lindex [$this simulator_get_sfrs] [list $idx 0]]
240
241 $this setSfr $addr $value
242 $this Simulator_GUI_sync S $addr
243 }
244
245 ## Create GUI elements of this panel
246 # @return void
247 private method create_GUI {} {
248 # Create and pack panel frames
249 set main_frame [frame $parent.main_frame]
250 set main_left_frame [frame $main_frame.left]
251 pack $main_left_frame -side left -fill both -expand 1
252 pack $main_frame -fill both -expand 1
253
254 # Create text widget and its header
255 pack [label $main_left_frame.header_label \
256 -anchor w -justify left -pady 0 -padx 2 \
257 -fg ${::Simulator_GUI::small_color} \
258 -font ${::Simulator_GUI::smallfont} \
259 -text "[mc {Register}] [mc {HEX}] [mc {DEC}]" -width 1 \
260 ] -fill x -pady 0 -anchor nw
261 set text_widget [text $main_left_frame.text \
262 -bg {#FFFFFF} -font $roman_font -bd 2 \
263 -width 0 -height 0 -wrap none \
264 -yscrollcommand "$this sfr_watches_scroll_set" \
265 -cursor left_ptr \
266 ]
267
268 # Create popup menu for the text widget
269 set menu $text_widget.menu
270 menuFactory {
271 {command {Set to 0x00} {} 9 "sfr_watches_set_current_to 00"
272 {} "Set this register to 0"}
273 {command {Set to 0xFF} {} 9 "sfr_watches_set_current_to FF"
274 {} "Set this register to 255"}
275 } $menu 0 "$this " 0 {} [namespace current]
276
277 # Set event bindings for the text widget
278 bindtags $text_widget $text_widget
279 foreach event {
280 <ButtonRelease-1> <B1-Enter> <B1-Leave>
281 <B2-Motion> <Button-5> <Button-4>
282 <MouseWheel>
283 } {
284 bind $text_widget $event [bind Text $event]
285 }
286 bind $text_widget <Button-1> \
287 "$this sfr_watches_select_line 0 \[expr {int(\[%W index @%x,%y\])}\] hex"
288 bind $text_widget <ButtonRelease-3> \
289 "$this sfr_watches_select_line 0 \[expr {int(\[%W index @%x,%y\])}\] hex
290 tk_popup $menu %X %Y"
291
292 # Pack the text widget and create its scrollbar
293 pack $text_widget -fill both -expand 1
294 set scrollbar [ttk::scrollbar $main_frame.scrollbar \
295 -orient vertical -command "$text_widget yview" \
296 ]
297
298 # Create bottom frame (search bar)
299 set search_frame [frame $parent.top]
300 pack [label $search_frame.lbl \
301 -text [mc "Search:"] \
302 ] -side left
303 set search_entry [ttk::entry $search_frame.entry \
304 -width 0 \
305 -validate all \
306 -validatecommand "$this sfr_watches_search_validate %P" \
307 ]
308 pack $search_entry -side left -fill x -expand 1
309 set search_clear_but [ttk::button $search_frame.button \
310 -style Flat.TButton \
311 -state disabled \
312 -image ::ICONS::16::clear_left \
313 -command "$search_entry delete 0 end" \
314 ]
315 pack $search_clear_but -side right -after $search_entry
316 pack $search_frame -fill x
317
318 # Create highlighting tags for the text widget
319 $text_widget tag configure tag_addr \
320 -foreground {#000000} -font $main_font
321 $text_widget tag configure tag_name \
322 -foreground {#0000DD} -font $main_font
323 $text_widget tag configure tag_curLine \
324 -background ${::RightPanel::selection_color_dark}
325 }
326
327 ## Adjust scrollbar
328 # @parm Float frac0 - 1st fraction
329 # @parm Float frac1 - 2nd fraction
330 # @return void
331 public method sfr_watches_scroll_set {frac0 frac1} {
332 if {$scrollbar == {}} {
333 return
334 }
335
336 # Hide scrollbar
337 if {$frac0 == 0 && $frac1 == 1} {
338 if {[winfo ismapped $scrollbar]} {
339 pack forget $scrollbar
340 }
341 # Show scrollbar
342 } else {
343 if {![winfo ismapped $scrollbar]} {
344 pack $scrollbar \
345 -side left \
346 -fill y \
347 -after $main_left_frame
348 }
349 $scrollbar set $frac0 $frac1
350 }
351 }
352
353 ## Validate content of search entry box at the bottom bar
354 # @parm String string - String to validate
355 # @return Bool - Always 1
356 public method sfr_watches_search_validate {string} {
357 # Check if searching is enabled
358 if {!$search_ena} {return 1}
359
360 # Adjust state of clear button (+ clear selection on the text widget)
361 if {$string == {}} {
362 $search_clear_but configure -state disabled
363 $search_entry configure -style TEntry
364 sfr_watches_select_line 1 0 hex
365 return 1
366 } else {
367 $search_clear_but configure -state normal
368 }
369
370 set string [string toupper $string]
371
372 # Perform case insensitive search for the given chunk of SFR name and address
373 foreach arr {reg2idx haddr2idx} {
374 foreach str [lsort -ascii -increasing [array names $arr]] {
375 # Search successful
376 if {![string first $string $str]} {
377 $search_entry configure -style StringFound.TEntry
378 sfr_watches_select_line 1 [expr {[subst -nocommands "\${${arr}(${str})}"] + 1}] hex
379 return 1
380 }
381 }
382 }
383
384 # Search failed
385 $search_entry configure -style StringNotFound.TEntry
386 return 1
387 }
388
389 ## Select line in the text widget
390 # @parm Bool search - Invoked form search validator (do not clear search entry box)
391 # @parm Int line - Target line
392 # @parm String type - Entry box to select (hex or dec)
393 # @return void
394 public method sfr_watches_select_line {search line type} {
395
396 # Unselect the last selected line and determinate cursor position
397 if {$last_selected_line} {
398 $text_widget tag remove tag_curLine 0.0 end
399 $this simulator_reg_label_set_highlighted $last_selected_line 0
400 incr last_selected_line -1
401 foreach tp {dec hex} {
402 $text_widget.${tp}_entry_$last_selected_line selection clear
403 $text_widget.${tp}_entry_$last_selected_line configure \
404 -bg {#FFFFFF} -disabledbackground {#FFFFFF}
405 }
406 set cursor [$text_widget.${type}_entry_$last_selected_line index insert]
407 } else {
408 set cursor 0
409 }
410
411 # Adjust last selected line (if 0 the return)
412 set last_selected_line $line
413 if {!$line} {
414 return
415 }
416
417
418 # Highlight this line as selected in the text widget
419 $text_widget tag add tag_curLine \
420 $last_selected_line.0 \
421 $last_selected_line.0+1l
422 $text_widget see $last_selected_line.0
423
424 # Highlight SFR on this line in simulator control panel
425 $this simulator_reg_label_set_highlighted $last_selected_line 1
426
427 # Adjust background color for entry boxes at this line
428 incr line -1
429 $text_widget.dec_entry_$line configure \
430 -fg ${Simulator::normal_color} \
431 -bg ${::RightPanel::selection_color_dark} \
432 -disabledbackground ${::RightPanel::selection_color_dark}
433 $text_widget.hex_entry_$line configure \
434 -fg ${Simulator::normal_color} \
435 -bg ${::RightPanel::selection_color_dark} \
436 -disabledbackground ${::RightPanel::selection_color_dark}
437
438 # Clear search entry box and focus and entry box at this line
439 if {!$search} {
440 $text_widget.${type}_entry_$line icursor $cursor
441 $text_widget.${type}_entry_$line selection range 0 end
442 focus $text_widget.${type}_entry_$line
443 set search_ena 0
444 $search_entry delete 0 end
445 set search_ena 1
446 }
447 }
448
449 ## Validator for SFR value entry boxes
450 # @parm String type - Entry box type (hex or dec)
451 # @parm Int addr - Register address
452 # @parm String value - String to validate
453 # @return Bool - Validation result
454 public method sfr_watches_validate {type addr value} {
455 # Prevent recursion
456 if {!$validation_ena} {return 1}
457 set validation_ena 0
458
459 # Validate value
460 if {$value == {}} {
461 set value 0
462 }
463 if {$type == {hex}} {
464 if {![string is xdigit $value]} {
465 set validation_ena 1
466 return 0
467 }
468 set value [expr "0x$value"]
469 } else {
470 if {![string is digit $value]} {
471 set validation_ena 1
472 return 0
473 }
474 }
475 if {$value > 255 || $value < 0} {
476 set validation_ena 1
477 return 0
478 }
479
480 # Synchronize with engine and simulator control panel
481 $this setSfr $addr [format %X $value]
482 $this SimGUI_disable_sync
483 $this Simulator_GUI_sync S $addr
484 $this SimGUI_enable_sync
485
486 # Synchronize with the other one entry box
487 if {$type == {hex}} {
488 $text_widget.dec_entry_$addr2idx($addr) delete 0 end
489 $text_widget.dec_entry_$addr2idx($addr) insert 0 $value
490 } else {
491 set value [format %X $value]
492 if {[string length $value] == 1} {
493 set value "0$value"
494 }
495 $text_widget.hex_entry_$addr2idx($addr) delete 0 end
496 $text_widget.hex_entry_$addr2idx($addr) insert 0 $value
497 }
498
499 # Done
500 set validation_ena 1
501 return 1
502 }
503
504 ## Remove all SFRs for the text widget and unregister them
505 # @return void
506 private method clear_gui {} {
507 if {!$sfrw_gui_initialized} {return}
508
509 # Clear SFR name label highlight in simultor contol panel
510 if {$last_selected_line} {
511 $this simulator_reg_label_set_highlighted $last_selected_line 0
512 }
513
514 # Reset object variables
515 array unset haddr2idx
516 array unset addr2idx
517 array unset reg2idx
518 set last_selected_line 0
519 set entry_count 0
520
521 # Clear the text widget
522 $text_widget delete 1.0 end
523 foreach wdg [$text_widget window names] {
524 destroy $wdg
525 }
526 }
527
528 ## Set new value of certain SFR in this panel
529 # @parm Int addr - SFR address
530 # @parm Int new_val - New SFR value
531 # @return void
532 public method sfr_watches_sync {addr new_val} {
533 if {!$sfrw_gui_initialized} {return}
534
535 # Prevent recursion
536 if {!$validation_ena} {return}
537 set validation_ena 0
538
539 # Check if this SFR is available here
540 if {[lsearch [array names addr2idx] $addr] == -1} {
541 set validation_ena 1
542 return
543 }
544
545 # Determinate references of HEX and DEC entry boxes
546 set hex_entry $text_widget.hex_entry_$addr2idx($addr)
547 set dec_entry $text_widget.dec_entry_$addr2idx($addr)
548
549 # Highlight entry boxes
550 set org_val [$dec_entry get]
551 if {$org_val != $new_val} {
552 $hex_entry configure -fg ${::Simulator_GUI::hcolor}
553 $dec_entry configure -fg ${::Simulator_GUI::hcolor}
554 }
555
556 # Set decimal value
557 $dec_entry delete 0 end
558 $dec_entry insert 0 $new_val
559
560 # Set hexadecimal value
561 set new_val [format %X $new_val]
562 if {[string length $new_val] == 1} {
563 set new_val "0$new_val"
564 }
565 $hex_entry delete 0 end
566 $hex_entry insert 0 $new_val
567
568 # Reenable entry box value validations
569 set validation_ena 1
570 }
571
572 ## Enable this panel
573 # @return vois
574 public method sfr_watches_enable {} {
575 if {!$sfrw_gui_initialized} {return}
576 $menu entryconfigure [::mc "Set to 0x00"] -state normal
577 $menu entryconfigure [::mc "Set to 0xFF"] -state normal
578 for {set i 0} {$i < $entry_count} {incr i} {
579 $text_widget.hex_entry_$i configure -state normal
580 $text_widget.dec_entry_$i configure -state normal
581 }
582 }
583
584 ## Disable this panel
585 # @return vois
586 public method sfr_watches_disable {} {
587 if {!$sfrw_gui_initialized} {return}
588 $menu entryconfigure [::mc "Set to 0x00"] -state disabled
589 $menu entryconfigure [::mc "Set to 0xFF"] -state disabled
590 for {set i 0} {$i < $entry_count} {incr i} {
591 $text_widget.hex_entry_$i configure -state disabled
592 $text_widget.dec_entry_$i configure -state disabled
593 }
594 }
595
596 ## This function shuld be call after processor was changed
597 # Reload available SFRs
598 # @return void
599 public method sfr_watches_commit_new_sfr_set {} {
600 if {!$sfrw_gui_initialized} {return}
601 clear_gui
602 fill_gui
603 }
604
605 ## Move selected line up
606 # @parm String type - Which entry box should be selected (hex or dec)
607 # @parm Int lines - Number of lines to move by
608 # @return void
609 public method sfr_watches_up {type lines} {
610 set line $last_selected_line
611 incr line -$lines
612 if {$line <= 0} {
613 set line $entry_count
614 }
615
616 sfr_watches_select_line 0 $line $type
617 }
618
619 ## Move selected line down
620 # @parm String type - Which entry box should be selected (hex or dec)
621 # @parm Int lines - Number of lines to move by
622 # @return void
623 public method sfr_watches_down {type lines} {
624 set line $last_selected_line
625 incr line $lines
626 if {$line >= $entry_count} {
627 set line 0
628 }
629
630 sfr_watches_select_line 0 $line $type
631 }
632 }
633
634 # >>> File inclusion guard
635 }
636 # <<< File inclusion guard