summaryrefslogtreecommitdiff
path: root/lib/simulator/engine/engine_virtual_hw_controller.tcl
blob: 3413eb3d2a6bf3f075a01d54294ecb61813dfc67 (plain)
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
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
#!/usr/bin/tclsh
# Part of MCU 8051 IDE ( http://http://www.moravia-microsystems.com/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@moravia-microsystems.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 _ENGINE_VIRTUAL_HW_CONTROLLER_TCL ] } {
set _ENGINE_VIRTUAL_HW_CONTROLLER_TCL _
# <<< File inclusion guard

# --------------------------------------------------------------------------
# DESCRIPTION
# Part of simulator engine functionality.
#
# --------------------------------------------------------------------------
# VIRTUAL HW CONTROLLER PROCEDURES
# --------------------------------------------------------------------------


## Perform one instruction cycle
 # @return void
private method instruction_cycle {} {
	set rmw_instruction 0
	set stepback_ena 1

	stepback_save_spec

	# Execute instruction
	if {${::DEBUG}} {
		$code($pc)
	} else {
		if {[catch {
			$code($pc)
		} result]} then {
			if {!${::Simulator::ignore_invalid_ins}} {
				$this simulator_invalid_instruction $pc $Line($pc)
				internal_shutdown
				incr_pc 1
			}
			# OP code 165d is invalid on 8051
			if {$code($pc) != 165} {
				puts stderr ${::errorInfo}
			}
		}
	}

	# Adjust simulator action history
	stepback_save_spec_time

	# Increment program time
	set wtd_rst_flag [increment_program_time $time]

	# Interrupts control
	if {$controllers_conf(EA)} {
		# Determinate minimum interrupt priority
		set min_priority -1
		foreach int $interrupts_in_progress {
			set p [interrupt_priority $int]
			if {$p > $min_priority} {
				set min_priority $p
			}
		}

		# Iterate over list of interrupt priorities
		foreach IntName $controllers_conf(IP) {

			# Skip requests with insufficient priority
			if {[interrupt_priority $IntName] <= $min_priority} {
				continue
			}

			# Test iterrupt flag
			set vector__flag [isInterruptActive $IntName]
			# Handle the interrupt
			if {$vector__flag != 0} {
				interrupt_handler $IntName [lindex $vector__flag 0] [lindex $vector__flag 1] 0
				break
			}
		}
	}

	# Send port states to PALE (Peripheral Abstraction Layer Engine)
	set max $time
	if {!$controllers_conf(X2)} {
		set max [expr {$max * 2}]
	}
	for {set i 1} {$i < $max} {incr i} {
		$this pale_simulation_cycle $ports_previous_state
	}
	set rmw_instruction 1
	set ports_previous_state [list]
	for {set i 0; set addr 128} {$i < 5} {incr i; incr addr 16} {
		if {$feature_available(p$i)} {
			lappend ports_previous_state $sfr($addr)
		} else {
			lappend ports_previous_state 0
		}
	}
	$this pale_simulation_cycle $ports_previous_state

	# Analog comparator controller
	if {[$this pale_is_enabled]} {
		if {$controllers_conf(CEN)} {
			anlcmp_controller
		} else {
			set anlcmp_running 0
			$this pale_SLSF $PIN(ANL0) 0
			$this pale_SLSF $PIN(ANL1) 0
		}
	}

	# UART controller
	if {$feature_available(uart)} {
		if {($uart_RX_in_progress || $uart_TX_in_progress) && ${::Simulator::reverse_run_steps}} {
			stepback_reg_change S $symbol(SBUFR)
			stepback_reg_change S $symbol(SBUFT)
			stepback_reg_change S $symbol(SCON)
		}
		uart_controller $time
	}

	# Handle exteranal interrupts
	foreach int_src {0 1} {
		set intx	1	;# State of pin INTx
		set ext_int	0	;# Local external interrupt flag

		# Anylize port state
		for {set i -$time} {$i < 0} {incr i} {
			# Determinate value of input pin INTX
			set intx [$this pale_RRPPV $PIN(INT${int_src}) $i]

			# Detect falling edge on INTx
			if {$controllers_conf(IT${int_src})} {
				set intx_prev $controllers_conf(INT${int_src})
				set controllers_conf(INT${int_src}) $intx

				if {$intx_prev && !$intx} {
					set ext_int 1
				}

			# Just copy inverted value from INTx to IEx
			} else {
				set ext_int [expr {!$intx}]
			}
		}

		# Invoke external interrupt
		if {
			( $controllers_conf(IT${int_src}) && $ext_int )
				||
			( !$controllers_conf(IT${int_src}) && ($controllers_conf(IE${int_src}) != $ext_int) )
		} then {
			setBit $symbol(IE${int_src}) $ext_int
		}
	}

	## Timers control
	 # Timers 0 and 1 are engaged by PWM
	if {$controllers_conf(PWMEN)} {
		if {!$pwm_running} {
			set pwm_running 1
			$this pale_SLSF $PIN(T1) 3
		} else {
			# Make backup for affected SFR
			if {${::Simulator::reverse_run_steps}} {
				stepback_reg_change S $symbol(TL0)
				stepback_reg_change S $symbol(TH0)
				stepback_reg_change S $symbol(TL1)
			}

			# Timer 0 is forced into mode 2 (8-bit autoreload)
			# TL1 is incremented after each T0 overflow
			incr sfr($symbol(TL0)) $time
			while {1} {
				if {$sfr($symbol(TL0)) > 255} {
					set sfr($symbol(TL0)) [expr {$sfr($symbol(TL0)) - 256 + $sfr($symbol(TH0))}]
					# Increment TL1
					incr sfr($symbol(TL1))

					# TL1 overflow -> High PWM pulse
					if {$sfr($symbol(TL1)) > 255} {
						set sfr($symbol(TL1)) 0		;# Clear TL1
						set pwm_OCR $sfr($symbol(TH1))	;# Load OCR
						pale_WPBBL $PIN(T2) 1 -$sfr($symbol(TL0))
					}

					# OCR and TL1 are equal -> Low PWM pulse
					if {$pwm_OCR == $sfr($symbol(TL1))} {
						set time_back [expr {256 - $sfr($symbol(TL0))}]
						if {$time_back >= 0} {
							$this pale_WPBBL $PIN(T1) 0
						} else {
							$this pale_WPBBL $PIN(T1) $time_back
						}
					}
				} else {
					break
				}
			}

			# Synchronize affected SFR
			if {$sync_ena} {
				$this Simulator_GUI_sync S $symbol(TL0)
				$this Simulator_GUI_sync S $symbol(TH0)
				$this Simulator_GUI_sync S $symbol(TL1)
			}
		}

	 # Timers 0 and 1 are free
	} else {
		# Shutdown PWM controller
		if {$pwm_running} {
			set pwm_running 0
			$this pale_SLSF $PIN(T1) 0
		}

		# Make backup for TCON register
		if {($controllers_conf(TR0) || $controllers_conf(TR1)) && ${::Simulator::reverse_run_steps}} {
			stepback_reg_change S $symbol(TCON)
		}

		# Increment timer 0
		if {$controllers_conf(TR0)} {
			if {${::Simulator::reverse_run_steps}} {
				stepback_reg_change S $symbol(TL0)
				stepback_reg_change S $symbol(TH0)
			}
			timer_controller_01 0
			if {$sync_ena} {
				$this Simulator_GUI_sync S $symbol(TL0)
				$this Simulator_GUI_sync S $symbol(TH0)
			}
		# Shutdown timer 0
		} else {
			set timer_0_running 0
		}

		# Increment timer 1
		if {$controllers_conf(TR1)} {
			if {${::Simulator::reverse_run_steps}} {
				stepback_reg_change S $symbol(TL1)
				stepback_reg_change S $symbol(TH1)
				if {$controllers_conf(T0_MOD) == 3} {
					stepback_reg_change S $symbol(TH0)
				}
			}
			timer_controller_01 1
			if {$sync_ena} {
				$this Simulator_GUI_sync S $symbol(TL1)
				$this Simulator_GUI_sync S $symbol(TH1)
				if {$controllers_conf(T0_MOD) == 3} {
					$this Simulator_GUI_sync S $symbol(TH0)
				}
			}
		# Shutdown timer 1
		} else {
			set timer_1_running 0
		}

		# Keep timer 1 running while timer 0 is in mode 3
		if {$controllers_conf(T0_MOD) == 3} {
			if {${::Simulator::reverse_run_steps}} {
				stepback_reg_change S $symbol(TL1)
				stepback_reg_change S $symbol(TH1)
			}
			special_timer1_controller_T0_MOD_3
			if {$sync_ena} {
				$this Simulator_GUI_sync S $symbol(TL1)
				$this Simulator_GUI_sync S $symbol(TH1)
			}
		}
	}

	# Increment timer 2
	if {$feature_available(t2)} {
		if {$controllers_conf(TR2)} {
			if {${::Simulator::reverse_run_steps}} {
				stepback_reg_change S $symbol(TL2)
				stepback_reg_change S $symbol(TH2)
				stepback_reg_change S $symbol(RCAP2L)
				stepback_reg_change S $symbol(RCAP2H)
				stepback_reg_change S $symbol(T2CON)
			}
			timer_controller_2
			if {$sync_ena} {
				$this Simulator_GUI_sync S $symbol(TL2)
				$this Simulator_GUI_sync S $symbol(TH2)
				$this Simulator_GUI_sync S $symbol(RCAP2L)
				$this Simulator_GUI_sync S $symbol(RCAP2H)
			}
		# Shutdown timer 2
		} else {
			set timer_2_running 0
		}
	}

	# Manage interrupt monitor
	if {$::GUI_AVAILABLE} {
		$this interrupt_monitor_intr_flags [simulator_get_active_intr_flags]
	}

	# Adjust stopwatch timer
	incr run_statistics(3)
	if {$::GUI_AVAILABLE} {
		$this stopwatch_refresh
	}

	# Manage stack for stepback stack
	stepback_save_norm
	set stepback_ena 0

	# Manage PALE
	$this pale_finish_simulation_cycle
}

## Analog comparator controller
 # It's safe to call this function even if there is no analog comparator implemented on the MCU
 # @return void
private method anlcmp_controller {} {
	set sample 0

	# Start analog comparator
	if {!$anlcmp_running} {
		$this pale_SLSF $PIN(ANL0) 4
		$this pale_SLSF $PIN(ANL1) 4

		set anlcmp_running 1
		return
	}

	## Sample inputs
	 # Debounce mode
	if {[lsearch {2 3 6} $controllers_conf(AC_MOD)] != -1} {
		incr anlcpm_db_timer $timer1_overflow
		if {$anlcpm_db_timer >= 2} {
			incr anlcpm_db_timer -$anlcpm_db_timer
			set sample 1
		}
	 # Normal mode (sample every S4)
	} else {
		set sample 1
	}
	 # Sample port pins ANL0 and ANL1
	if {!$sample} {
		return
	}
	set anlcmp_output_prev $anlcmp_output
	set anlcmp_output [expr {([$this pale_RRPPV $PIN(ANL0)] - [$this pale_RRPPV $PIN(ANL1)]) > 0 ? 1 : 0}]

	# Conditionaly ionvoke analog comparator interrupt
	switch -- $controllers_conf(AC_MOD) {
		0 {	;# Negative (Low) level
			if {!$anlcmp_output} {
				write_sfr $symbol(ACSR) [expr {$sfr($symbol(ACSR)) | 0x10}]
			}
		}
		1 {	;# Positive edge
			if {!$anlcmp_output_prev && $anlcmp_output} {
				write_sfr $symbol(ACSR) [expr {$sfr($symbol(ACSR)) | 0x10}]
			}
		}
		2 {	;# Toggle with debounce
			if {$anlcmp_output_prev != $anlcmp_output} {
				write_sfr $symbol(ACSR) [expr {$sfr($symbol(ACSR)) | 0x10}]
			}
		}
		3 {	;# Positive edge with debounce
			if {!$anlcmp_output_prev && $anlcmp_output} {
				write_sfr $symbol(ACSR) [expr {$sfr($symbol(ACSR)) | 0x10}]
			}
		}
		4 {	;# Negative edge
			if {$anlcmp_output_prev && !$anlcmp_output} {
				write_sfr $symbol(ACSR) [expr {$sfr($symbol(ACSR)) | 0x10}]
			}
		}
		5 {	;# Toggle
			if {$anlcmp_output_prev != $anlcmp_output} {
				write_sfr $symbol(ACSR) [expr {$sfr($symbol(ACSR)) | 0x10}]
			}
		}
		6 {	;# Negative edge with debounce
			if {$anlcmp_output_prev && !$anlcmp_output} {
				write_sfr $symbol(ACSR) [expr {$sfr($symbol(ACSR)) | 0x10}]
			}
		}
		7 {	;# Positive (High) level
			if {$anlcmp_output} {
				write_sfr $symbol(ACSR) [expr {$sfr($symbol(ACSR)) | 0x10}]
			}
		}
	}
}

## Timer controller for timer 2
 # @retrun void
private method timer_controller_2 {} {
	set timer2_overflow	0
	set increment		0

	## Determinate counter increment
	 # Counter 16-bit
	if {$controllers_conf(CT2)} {
		# Detect 1-to-0 transition on external input
		for {set i -$time} {$i < 0} {incr i} {
			set counter_input_prev $controllers_conf(T2)
			set controllers_conf(T2) [$this pale_RRPPV $PIN(T2) $i]

			if {$counter_input_prev && !$controllers_conf(T2)} {
				incr increment
			}
		}

	 # Timer 16-bit
	} else {
		set increment $time
	}

	# Programmable Clock-output
	if {$controllers_conf(T2OE)} {
		for {set i [expr {-$time + 1}]} {$i <= 0} {incr i} {
			$this pale_WPBBL $PIN(T2) $controllers_conf(T2) $i
		}

		# 16-bit timer
		if {$timer_2_running && !$controllers_conf(CT2)} {
			set increment [expr {$time * 6}]
		} else {
			set increment 0
		}
		if {[increment_timer2 $increment 1]} {
			while {1} {
				set sfr($symbol(TH2)) $sfr($symbol(RCAP2H))
				if {![increment_timer2 $sfr($symbol(RCAP2L)) 1]} {
					break
				}
				incr timer2_overflow
			}

			# Detect transition on external input
			set controllers_conf(T2) [expr {!$controllers_conf(T2)}]
		}

		# External interrupt
		for {set i -$time} {$i < 0} {incr i} {
			# Detect 1-to-0 transition on external input
			set t2ex_prev $controllers_conf(T2EX)
			set controllers_conf(T2EX) [$this pale_RRPPV $PIN(T2EX) $i]

			# Invoke external interrupt
			if {$t2ex_prev && !$controllers_conf(T2EX) && $controllers_conf(EXEN2)} {
				setBit $symbol(EXF2) 1
			}
		}

	# Baud Rate Generator
	} elseif {$controllers_conf(RCLK) || $controllers_conf(TCLK)} {
		if {$controllers_conf(CT2)} {
			set increment [expr {$increment * 6}]
		}
		if {[increment_timer2 $increment 1]} {
			while {1} {
				set sfr($symbol(TH2)) $sfr($symbol(RCAP2H))
				if {![increment_timer2 $sfr($symbol(RCAP2L)) 1]} {
					break
				}
				incr timer2_overflow
			}
		}

		# External interrupt
		for {set i -$time} {$i < 0} {incr i} {
			# Detect 1-to-0 transition on external input
			set t2ex_prev $controllers_conf(T2EX)
			set controllers_conf(T2EX) [$this pale_RRPPV $PIN(T2EX) $i]

			# Invoke external interrupt
			if {$t2ex_prev && !$controllers_conf(T2EX) && $controllers_conf(EXEN2)} {
				setBit $symbol(EXF2) 1
			}
		}

	# 16-bit Capture
	} elseif {$controllers_conf(CPRL2) && $timer_2_running} {
		set capture_flag 0

		# Increment timer registers
		if {[increment_timer2 $increment 1]} {
			setBit $symbol(TF2) 1
		}

		# Detect 1-to-0 transition on external input
		for {set i -$time} {$i < 0} {incr i} {
			set t2ex_prev $controllers_conf(T2EX)
			set controllers_conf(T2EX) [$this pale_RRPPV $PIN(T2EX) $i]

			# Capture TL2 and TH2 to RCAP2L and RCAP2H
			if {$t2ex_prev && !$controllers_conf(T2EX) && $controllers_conf(EXEN2)} {
				setBit $symbol(EXF2) 1

				set sfr($symbol(RCAP2L)) $sfr($symbol(TL2))
				set sfr($symbol(RCAP2H)) $sfr($symbol(TH2))

			}
		}

	# 16-bit Auto-reload (DCEN == 1)
	} elseif {$controllers_conf(DCEN) && $timer_2_running} {
		set updown [$this pale_RRPPV $PIN(T2EX)]
		set result [increment_timer2 $increment $updown]

		# Overflow
		if {$result == 1} {
			while {1} {
				set sfr($symbol(TH2)) $sfr($symbol(RCAP2H))
				if {![increment_timer2 $sfr($symbol(RCAP2L)) $updown]} {
					break
				}
			}

			setBit $symbol(TF2) 1
			setBit $symbol(EXF2) [expr {![getBit $symbol(EXF2)]}]
		}

		# Underflow
		if {!$updown || $result == -1} {
			while {1} {
				set cur_val [expr {$sfr($symbol(TL2)) + ($sfr($symbol(TH2)) << 8)}]
				set min_val [expr {$sfr($symbol(RCAP2L)) + ($sfr($symbol(RCAP2H)) << 8)}]

				set diff [expr {$min_val - $cur_val}]

				if {$diff > 0} {
					incr diff -1
					set sfr($symbol(TL2)) 255
					set sfr($symbol(TH2)) 255

					increment_timer2 $diff 0
					set result -1

				} else {
					break
				}
			}
		}

		if {$result == -1} {
			setBit $symbol(TF2) 1
			setBit $symbol(EXF2) [expr {![getBit $symbol(EXF2)]}]
		}

	# 16-bit Auto-reload (DCEN == 0)
	} elseif {$timer_2_running} {
		if {[increment_timer2 $increment 1]} {
			while {1} {
				set sfr($symbol(TH2)) $sfr($symbol(RCAP2H))
				if {![increment_timer2 $sfr($symbol(RCAP2L)) 1]} {
					break
				}
			}
			setBit $symbol(TF2) 1
			setBit $symbol(EXF2) [expr {![getBit $symbol(EXF2)]}]
		}
	}

	# Start the timer if it is not already started
	if {!$timer_2_running} {
		set timer_2_running 1
		return
	}
}

## Increment timer 2
 # @parm Int increment_by	- value to increment by
 # @parm Bool updown		- 1 == count up; 0 == count down
 # @retrun Int - 1 == Owerflow; -1 == Underflow; 0 == normal
private method increment_timer2 {increment_by updown} {
	if {$updown} {
		incr sfr($symbol(TL2)) $increment_by
	} else {
		incr sfr($symbol(TL2)) -$increment_by
	}

	# Low-order byte overflow
	if {$sfr($symbol(TL2)) > 255} {
		incr sfr($symbol(TH2))
		incr sfr($symbol(TL2)) -256
		if {$sfr($symbol(TH2)) > 255} {
			set sfr($symbol(TH2)) 0
			return 1
		}
	# Low-order byte underflow
	} elseif {$sfr($symbol(TL2)) < 0} {
		incr sfr($symbol(TH2)) -1
		incr sfr($symbol(TL2)) 256
		if {$sfr($symbol(TH2)) < 0} {
			set sfr($symbol(TH2)) 255
			return -1
		}
	}

	# Normal operation
	return 0

}

## Order UART to initialize transmission cycle
 # @return void
private method uart_start_transmission {} {
	# Begin transmission with two instruction cycles delay
	set uart_TX_in_progress -1

	# Initialize internall transmission buffer (shift register)
	switch $controllers_conf(UART_M) {
		0 {
			set uart_TX_shift_reg [expr {$sfr($symbol(SBUFT)) + 0x100}]
		}
		1 {
			set uart_TX_shift_reg [expr {($sfr($symbol(SBUFT)) << 1) + 0x600}]
		}
		2 {
			set uart_TX_shift_reg [expr {($sfr($symbol(SBUFT)) << 1) + 0xC00 + ([getBit $symbol(TB8)] << 9)}]
		}
		3 {
			set uart_TX_shift_reg [expr {($sfr($symbol(SBUFT)) << 1) + 0xC00 + ([getBit $symbol(TB8)] << 9)}]
		}
	}

	# Set line special funtion to data transmission
	if {$controllers_conf(UART_M) == 0} {
		$this pale_SLSF $PIN(TXD) 2
		$this pale_SLSF $PIN(RXD) 1
	} else {
		$this pale_SLSF $PIN(TXD) 1
	}
}

## UART (Universal Asynchronous Receiver Transmitter) controller
 # @parm Int num - Number of machine cycles performed by last set of instructions
 # @return void
private method uart_controller {num} {
	set send_tx_shift_clock_sequence 0

	# Manage UART clock prescaler
	if {$uart_RX_in_progress || $uart_TX_in_progress} {
		if {$controllers_conf(UART_M) == 1 || $controllers_conf(UART_M) == 3} {
			incr uart_clock_prescaler $timer1_overflow
		} elseif {$controllers_conf(UART_M) == 2} {
			incr uart_clock_prescaler $num
		}
	}

	# ----------------------------------------------------------------------
	# RECEPTION PROCEDURE
	# ----------------------------------------------------------------------

	# Reception is already in progress
	if {$uart_RX_in_progress} {
		# Make backup for SBUF-R
		if {${::Simulator::reverse_run_steps}} {
			stepback_reg_change S $symbol(SBUFR)
		}

		# Mode 0
		if {!$controllers_conf(UART_M)} {
			set send_tx_shift_clock_sequence $num
			for {set i -$num} {$i <= 0} {incr i} {
				set uart_RX_shift_reg [expr {$uart_RX_shift_reg << 1}]
				incr uart_RX_shift_reg [$this pale_RRPPV $PIN(RXD) $i]

				if {!($uart_RX_shift_reg & 0x100)} {
					# Stop reception
					set uart_RX_in_progress 0
					$this pale_SLSF $PIN(RXD) 0
					$this pale_SLSF $PIN(TXD) 0

					# Set RI and SBUF
					setBit $symbol(RI) 1
					set sfr($symbol(SBUFR)) [expr {$uart_RX_shift_reg & 0x0FF}]

					break
				}
			}
		# Mode 1, 2 or 3
		} else {
			## Select RX clock source
			if {$controllers_conf(UART_M) == 2} {
				if {$controllers_conf(SMOD)} {
					incr uart_RX_clock [expr {$uart_clock_prescaler / 2}]
				} else {
					incr uart_RX_clock [expr {$uart_clock_prescaler / 4}]
				}
			} else {
				# Timer 2 overflow
				if {$controllers_conf(RCLK)} {
					incr uart_RX_clock $timer2_overflow
				# Timer 1 overflow
				} else {
					if {$controllers_conf(SMOD)} {
						incr uart_RX_clock $timer1_overflow
					} else {
						incr uart_RX_clock [expr {$uart_clock_prescaler / 2}]
					}
				}
			}

			# Prescaler overflew -> Commence 1b reception
			if {$uart_RX_clock >= 16} {
				incr uart_RX_clock -16
				set uart_RX_shift_reg [expr {$uart_RX_shift_reg << 1}]
				incr uart_RX_shift_reg [$this pale_RRPPV $PIN(RXD)]

				# Mode 1
				if {$controllers_conf(UART_M) == 1} {
					if {!($uart_RX_shift_reg & 0x200)} {
						# Stop reception
						set uart_RX_in_progress 0
						$this pale_SLSF $PIN(RXD) 0

						# Set RI and SBUF
						setBit $symbol(RB8) [expr {$uart_RX_shift_reg & 1}]
						set sfr($symbol(SBUFR)) [expr {($uart_RX_shift_reg & 0x1FE) >> 1}]

						if {
							(!$controllers_conf(RI) &&
								(!$controllers_conf(SM2) || ($uart_RX_shift_reg & 1))
							)
						} then {
							setBit $symbol(RI) 1
						# Frame error
						} else {
							$this simulator_uart_invalid_stop_bit $pc $Line($pc)
							internal_shutdown
						}

						# Frame error detection
						if {$feature_available(smod0) && !($uart_RX_shift_reg & 1)} {
							set controllers_conf(FE) 1
							if {$sync_ena} {
								$this Simulator_GUI_sync S $symbol(SCON)
							}
						}
					}
				# Mode 2 or 3
				} elseif {!($uart_RX_shift_reg & 0x400)} {
					# Stop reception
					set uart_RX_in_progress 0
					$this pale_SLSF $PIN(RXD) 0

					# Set RI and SBUF
					setBit $symbol(RB8) [expr {$uart_RX_shift_reg & 2}]
					set sfr($symbol(SBUFR)) [expr {($uart_RX_shift_reg & 0x3FC) >> 2}]
					if {$controllers_conf(SM2)} {
						if {$feature_available(euart)} {
							# Check for broadcast address
							if {$sfr($symbol(SBUFR)) == ($sfr($symbol(SADEN)) | $sfr($symbol(SADDR)))} {
								setBit $symbol(RI) $controllers_conf(RB8)
							# Check for the given address
							} elseif {($sfr($symbol(SBUFR)) & $sfr($symbol(SADEN))) == ($sfr($symbol(SADDR)) & $sfr($symbol(SADEN)))} {
								setBit $symbol(RI) $controllers_conf(RB8)
							}
						} else {
							setBit $symbol(RI) $controllers_conf(RB8)
						}
					} else {
						setBit $symbol(RI) 1
					}

					# Frame error
					if {$feature_available(smod0) && !($uart_RX_shift_reg & 1)} {
						set controllers_conf(FE) 1
						if {$sync_ena} {
							$this Simulator_GUI_sync S $symbol(SCON)
						}
					}
				}
			}
		}

		# Synchronize SBUF-R
		if {$sync_ena} {
			$this Simulator_GUI_sync S $symbol(SBUFR)
		}

	# Reception may begin now ...
	} else {
		# Mode 0 - REN starts reception
		if {!$controllers_conf(UART_M)} {
			if {$controllers_conf(REN) && !$controllers_conf(RI)} {
				set uart_RX_in_progress 1
				$this pale_SLSF $PIN(TXD) 2
				$this pale_SLSF $PIN(RXD) 1

				set uart_RX_shift_reg 0x1FE
			}
		# Mode 1, 2, 3 - Start bit starts reception
		} else {
			if {![$this pale_RRPPV $PIN(RXD)]} {
				set uart_RX_clock 0
				set uart_RX_in_progress 1
				$this pale_SLSF $PIN(RXD) 1

				set uart_RX_shift_reg 0x3FE
			}
		}
	}

	# ----------------------------------------------------------------------
	# TRASMISSION PROCEDURE
	# ----------------------------------------------------------------------

	# Trasmission just began, but with one machine cycle delay
	if {$uart_TX_in_progress == -2} {
		set uart_TX_in_progress 1
		incr num -1

	# Begin transmission on next instruction
	} elseif {$uart_TX_in_progress == -1} {
		set uart_TX_in_progress -2

	# Transmission
	} elseif {$uart_TX_in_progress == 1} {
		# Make bakup for SBUF-T
		if {${::Simulator::reverse_run_steps}} {
			stepback_reg_change S $symbol(SBUFT)
		}

		# Mode 0
		if {!$controllers_conf(UART_M)} {
			set start_pos [expr {$num * (-2)}]

			for {set i $start_pos} {$i < 0} {} {
				incr i
				set bit_to_send [expr {$uart_TX_shift_reg & 1}]
				if {!$controllers_conf(X2)} {
					$this pale_WPBBL $PIN(RXD) $bit_to_send $i
					$this pale_WPBBL $PIN(TXD) 0 $i
					incr i
					$this pale_WPBBL $PIN(RXD) $bit_to_send $i
					$this pale_WPBBL $PIN(TXD) 1 $i
				} else {
					incr i
					$this pale_WPBBL $PIN(TXD) {|} [expr {int($i / 2)}]
					$this pale_WPBBL $PIN(RXD) $bit_to_send [expr {int($i / 2)}]
				}

				set uart_TX_shift_reg [expr {$uart_TX_shift_reg >> 1}]
				set sfr($symbol(SBUFT)) $uart_TX_shift_reg

				if {!($uart_TX_shift_reg & 0x1FE)} {

					# Stop transmission
					set uart_TX_in_progress 0
					$this pale_SLSF $PIN(RXD) 0
					$this pale_SLSF $PIN(TXD) 0

					# Set TI
					setBit $symbol(TI) 1

					break
				}
			}
		# Mode 1, 2, 3
		} else {

			## Select TX clock source
			if {$controllers_conf(UART_M) == 2} {
				if {$controllers_conf(SMOD)} {
					incr uart_TX_clock [expr {$uart_clock_prescaler / 2}]
				} else {
					incr uart_TX_clock [expr {$uart_clock_prescaler / 4}]
				}
			} else {
				# Timer 2 overflow
				if {$controllers_conf(TCLK)} {
					incr uart_TX_clock $timer2_overflow
				# Timer 1 overflow
				} else {
					if {$controllers_conf(SMOD)} {
						incr uart_TX_clock $timer1_overflow
					} else {
						incr uart_TX_clock [expr {$uart_clock_prescaler / 2}]
					}
				}
			}

			# Prescaler overflew -> Commence 1b transmission
			if {$uart_TX_clock >= 16} {
				incr uart_TX_clock -16

				$this pale_WPBBL $PIN(TXD) [expr {$uart_TX_shift_reg & 1}]
				set uart_TX_shift_reg [expr {$uart_TX_shift_reg >> 1}]
				set sfr($symbol(SBUFT)) $uart_TX_shift_reg

				if {!($uart_TX_shift_reg & 0x7FE)} {
					# Stop transmission
					set uart_TX_in_progress 0
					$this pale_SLSF $PIN(TXD) 0

					# Set TI
					setBit $symbol(TI) 1
				}
			}
		}

		# Synchronize SBUF-T
		if {$sync_ena} {
			$this Simulator_GUI_sync S $symbol(SBUFT)
		}
	}

	# Again manage UART clock prescaler
	if {$uart_clock_prescaler && $controllers_conf(UART_M)} {
		if {$controllers_conf(SMOD)} {
			set uart_clock_prescaler [expr {$uart_clock_prescaler % 2}]
		} else {
			set uart_clock_prescaler [expr {$uart_clock_prescaler % 4}]
		}
	}
}

## Timer 1 controller which operate while timer 0 is engaged in mode 3
 # @return void
private method special_timer1_controller_T0_MOD_3 {} {
	set timer1_overflow 0

	set TL1 $symbol(TL1)
	set TH1 $symbol(TH1)

	# Timer 1 mode
	switch -- $controllers_conf(T1_MOD) {
		0 {	;# Mode 0 - 13 bit counter
			incr sfr($TL1) $time
			if {$sfr($TL1) > 31} {
				incr sfr($TH1)
				incr sfr($TL1) -32
				if {$sfr($TH1) > 255} {
					set sfr($TH1) 0
					set timer1_overflow 1
				}
			}
		}
		1 {	;# Mode 1 - 16 bit counter
			incr sfr($TL1) $time
			if {$sfr($TL1) > 255} {
				incr sfr($TH1)
				incr sfr($TL1) -256
				if {$sfr($TH1) > 255} {
					set sfr($TH1) 0
					set timer1_overflow 1
				}
			}
		}
		2 {	;# Mode 2 - 8 bit auto-reload counter
			incr sfr($TL1) $time
			while {1} {
				if {$sfr($TL1) > 255} {
					set sfr($TL1) [expr {$sfr($TL1) - 256 + $sfr($TH1)}]
					incr timer1_overflow
				} else {
					break
				}
			}
		}
		3 {	;# Timer halted
		}
	}
}

## Timers controller for timers 0 and 1
 # -- should be called after each instruction cycle
 # @parm Int timer_num - Number of timer to hadnle ('0' or '1')
 # @return void
private method timer_controller_01 {timer_num} {
	set timer1_overflow	0
	set increment		0

	# Start the timer if it is not already started
	if {$timer_num == 1} {
		if {!$timer_1_running} {
			set timer_1_running 1
			return
		}
	} else {
		if {!$timer_0_running} {
			set timer_0_running 1
			return
		}
	}

	# Determinate counter increment
	if {$controllers_conf(CT${timer_num})} {
		# Detect 1-to-0 transition on external input
		for {set i -$time} {$i < 0} {incr i} {
			set counter_input_prev $controllers_conf(T${timer_num})
			set controllers_conf(T${timer_num}) [$this pale_RRPPV $PIN(T${timer_num}) $i]

			if {$counter_input_prev && !$controllers_conf(T${timer_num})} {
				incr increment
			}
		}

		# Trigered counter
		if {$controllers_conf(GATE${timer_num})} {
			# Read Intx and allow increment only if INTx is 1
			if {![$this pale_RRPPV $PIN(INT${timer_num})]} {
				set increment 0
			}
		}
	} else {
		set increment $time

		# Trigered timer
		if {$controllers_conf(GATE${timer_num})} {
			# Read Intx and allow increment only if INTx is 1
			if {![$this pale_RRPPV $PIN(INT${timer_num})]} {
				set increment 0
			}
		}
	}

	# Inrement timer 0 in mode 3 (Dual timer/counter)
	if {$controllers_conf(T0_MOD) == 3} {
		# Increment TH0
		if {$timer_num == 1} {
			incr sfr($symbol(TH0)) $time
			if {$sfr($symbol(TH0)) > 255} {
				set sfr($symbol(TH0)) [expr {$sfr($symbol(TH0)) - 256}]
				setBit $symbol(TF1) 1
			}
		# Increment TL0
		} else {
			incr sfr($symbol(TL0)) $increment
			if {$sfr($symbol(TL0)) > 255} {
				set sfr($symbol(TL0)) [expr {$sfr($symbol(TL0)) - 256}]
				setBit $symbol(TF0) 1
			}
		}

		return
	}

	# Determinate TLx and THx addresses
	set TL_addr $symbol(TL${timer_num})
	set TH_addr $symbol(TH${timer_num})

	# Increment the timer in mode 0, 1 or 2
	switch -- $controllers_conf(T${timer_num}_MOD) {
		0 {	;# Mode 0 - 13 bit counter

			set TL_upper_3_bits [expr {$sfr($TL_addr) & 0xE0}]
			set sfr($TL_addr) [expr {$sfr($TL_addr) & 0x1F}]

			incr sfr($TL_addr) $increment
			if {$sfr($TL_addr) > 31} {
				incr sfr($TH_addr)
				incr sfr($TL_addr) -32
				if {$sfr($TH_addr) > 255} {
					set sfr($TH_addr) 0
					setBit $symbol(TF${timer_num}) 1
					if {$timer_num} {
						set timer1_overflow 1
					}
				}
			}
			set sfr($TL_addr) [expr {$sfr($TL_addr) | $TL_upper_3_bits}]
		}
		1 {	;# Mode 1 - 16 bit counter
			incr sfr($TL_addr) $increment
			if {$sfr($TL_addr) > 255} {
				incr sfr($TH_addr)
				incr sfr($TL_addr) -256
				if {$sfr($TH_addr) > 255} {
					set sfr($TH_addr) 0
					setBit $symbol(TF${timer_num}) 1
					if {$timer_num} {
						set timer1_overflow 1
					}
				}
			}
		}
		2 {	;# Mode 2 - 8 bit auto-reload counter
			incr sfr($TL_addr) $increment
			while {1} {
				if {$sfr($TL_addr) > 255} {
					set sfr($TL_addr) [expr {$sfr($TL_addr) - 256 + $sfr($TH_addr)}]
					setBit $symbol(TF${timer_num}) 1
					if {$timer_num} {
						incr timer1_overflow
					}
				} else {
					break
				}
			}
		}
		3 {	;# Timer halted (timer 1)
		}
	}
}

## Data EEPROM controller
 # @parm Int num - Number of clock cycles preformed divided by 6
 # @return void
private method eeprom_controller {num} {
	if {!$eeprom_size || !$eeprom_WR} {return}

	# Conditionaly abort write cycle
	if {!$controllers_conf(WRTINH) || !$controllers_conf(EEMWE)} {
		if {!${::Simulator::ignore_EEPROM_WR_abort}} {
			$this simulator_EEPROM_WR_abort $pc $Line($pc)
			internal_shutdown
		}

		# Fill incomplete bytes with random values
		set eeprom_prev_new [list]
		foreach reg $eeprom_prev {
			lappend eeprom_prev_new [lindex $reg 0] [undefined_octet]
		}
		set eeprom_prev $eeprom_prev_new

		simulator_cancel_write_to_eeprom
		return
	}

	set eeprom_WR_time_org [expr {int($eeprom_WR_time)}]
	set eeprom_WR_time [expr {$eeprom_WR_time + $num * (300.0 / $clock_kHz)}]

	# Write cycle complete
	if {$eeprom_WR_time > 100.0} {
		simulator_finalize_write_to_eeprom

	# Write still in progress
	} elseif {$eeprom_WR_time_org != int($eeprom_WR_time)} {
		$this simulator_WTE_prg_set [expr {int($eeprom_WR_time)}]
	}

}

## Watchdog controller
 # @return Bool - true == MCU reseted; false == all in normal
private method watchdog_controller {} {

	# Watchdog timer software controll
	if {!$controllers_conf(HWDT)} {

		# Reset
		if {$controllers_conf(WSWRST)} {
			set watchdog_value -$time
			set controllers_conf(WSWRST) 0
			if {${::Simulator::reverse_run_steps}} {
				stepback_reg_change S 167
			}
			set sfr(167) [expr {$sfr(167) - 2}]
			if {$sync_ena} {
				$this Simulator_GUI_sync S 167
			}
		}

		# Enable / Disable
		if {$controllers_conf(WDTEN) && !$controllers_conf(WatchDogTimer)} {
			incr watchdog_value -$time
			set controllers_conf(WatchDogTimer) 1
		}

	# Hardware control -- WDTEN is read-only (1 == running; 0 == stopped)
	} elseif {$controllers_conf(WatchDogTimer) != $controllers_conf(WDTEN)} {
		set controllers_conf(WDTEN) $controllers_conf(WatchDogTimer)
		if {${::Simulator::reverse_run_steps}} {
			stepback_reg_change S 167
		}
		if {$controllers_conf(WDTEN)} {
			set sfr(167) [expr {$sfr(167) | 1}]
		} else {
			set sfr(167) [expr {$sfr(167) - 1}]
		}
		if {$sync_ena} {
			$this Simulator_GUI_sync S 167
		}
	}

	# Check if watchdog is enabled
	if {!$controllers_conf(WatchDogTimer)} {
		return 0
	}

	# Increment watchdog prescaler first
	set increment 0
	if {$controllers_conf(WatchDogPrescaler)} {
		incr wdt_prescaler_val $time
		while {1} {
			if {$wdt_prescaler_val >= $controllers_conf(WatchDogPrescaler)} {
				incr wdt_prescaler_val -$controllers_conf(WatchDogPrescaler)
				incr increment
			} else {
				break
			}
		}
	} else {
		set increment $time
	}

	# Increment watchdog time
	if {!$increment} {return 0}
	incr watchdog_value $increment
	if {$watchdog_value < 8192} {
		return 0
	}

	# Handle watchdog overflow
	incr watchdog_value -8192
	incr time -$watchdog_value
	incr time 8
	set watchdog_value 0
	master_reset -
	$this Simulator_sync_PC_etc

	# Shutdown simulator and inform user about the situation
	if {!${::Simulator::ignore_watchdog_reset}} {
		internal_shutdown
		$this simulator_watchdog_reset $pc $Line($pc)
	}

	return 1
}

## Resolve interrupt priority
 # @parm String flag - Interrupt name
 # @return Int - 0..3
private method interrupt_priority {IntName} {
	return [lindex $controllers_conf(IP_level)		\
		[lsearch $controllers_conf(IP) $IntName]	\
	]
}

## Translate interrupt name to interrupt vector
 # @parm String IntName		- Interrupt name (one of {PX0 PT0 PX1 PT1 PS PT2 PC})
 # @return Int - Interrupt vector
public method intr2vector {intname} {
	switch -- $intname {
		{PX0}	{return 3}
		{PT0}	{return 11}
		{PX1}	{return 19}
		{PT1}	{return 27}
		{PS}	{return 35}
		{PT2}	{return 43}
		{PC}	{return 51}
	}
}

## Interrupt controller -- handles interrupt requests
 # @parm String IntName		- Interrupt name (one of {PX0 PT0 PX1 PT1 PS PT2 PC})
 # @parm Int vector		- Interrupt vector (eg. '27' (T1 on 0x1B))
 # @parm String flag_bit	- Name of interrupt flag
 # @parm Bool immediately	- Invoked by user
 # @return Bool - 0 == interrupt denied; 1 == interrupt accepted
private method interrupt_handler {IntName vector flag_bit immediately} {

	if {!$immediately} {
		# Set interrupt_on_next if the time is too low
		if {$time == 1 && !$interrupt_on_next} {
			set interrupt_on_next 1
			return 0
		}

		# If the last instruction was RETI or any access to the IE, IP or IPH registers -> SKIP
		if {$skip_interrupt} {
			set skip_interrupt 0
			return 0
		}
	}

	# Adjust program run statistics
	incr run_statistics(5)

	# Set interrupt related variables
	set interrupt_on_next		0		;# Bool: Invoke interrupt on the next instruction
	lappend interrupts_in_progress	$IntName	;# List: Priority flags of interrupts which are in progress
	lappend inter_in_p_flags	$flag_bit	;# List: Interrupt flags of interrupts which are in progress

	# Adjust status bar
	if {$::GUI_AVAILABLE} {
		simulator_Sbar [mc "Interrupt  PC: 0x%s; line: %s; vector 0x%s  " [format %X $pc] [lindex $Line($pc) 0] [format %X $vector]] 1 $this
	}
	$this pale_interrupt $vector

	# Invoke LCALL to interrupt vector
	incr time 2
	if {!$immediately} {
		stepback_save_spec_subprog 1
	}
	uart_controller 2
	if {[increment_program_time 2]} {return}
	stack_push [expr {$pc & 255}]
	stack_push [expr {($pc & 65280) >> 8}]
	$this subprograms_call 2 $pc $vector
	$this stack_monitor_set_last_values_as 2 2
	incr run_statistics(3)

	# Unset interrupt flag
	switch -- $IntName {
		{PX0}	{	;# External 0
			if {$controllers_conf(IT0)} {
				setBit $symbol(IE0) 0
			}
		}
		{PT0}	{	;# Timer 0
			setBit $symbol(TF0) 0
		}
		{PX1}	{	;# External 1
			if {$controllers_conf(IT1)} {
				setBit $symbol(IE1) 0
			}
		}
		{PT1}	{	;# Timer 1
			setBit $symbol(TF1) 0
		}
		{PT2}	{	;# Timer 2
		}
		{PS}	{	;# UART
		}
		{PC}	{	;# Analog comparator
		}
	}

	# Report interrupt to interrupt monitor
	set flag {}
	switch $IntName {
		{PX0}	{set flag IE0}
		{PT0}	{set flag TF0}
		{PX1}	{set flag IE1}
		{PT1}	{set flag TF1}
		{PT2}	{
			foreach flag {TF2 EXF2} {
				if {$controllers_conf($flag)} {break}
			}
		}
		{PS}	{
			foreach flag {SPIF TI RI} {
				if {$controllers_conf($flag)} {break}
			}
		}
		{PC}	{set flag CF}
	}
	if {!$immediately} {
		$this interrupt_monitor_intr $flag
	}

	# Done ...
	set pc $vector
	return 1
}

## Test if the given interrupt is active (routine engaged)
 # @parm String IntName - Interrupt name
 # @return List - Interrupt vector or 0 in there is no interrupt active & Flag bit
private method isInterruptActive {IntName} {
	switch -- $IntName {
		{PX0}	{	;# External 0
			if {!$controllers_conf(EX0)}	{return 0}
			if {$controllers_conf(IE0)}	{return {3 IE0}}
		}
		{PT0}	{	;# Timer 0
			if {!$controllers_conf(ET0)}	{return 0}
			if {$controllers_conf(TF0)}	{return {11 TF0}}
		}
		{PX1}	{	;# External 1
			if {!$controllers_conf(EX1)}	{return 0}
			if {$controllers_conf(IE1)}	{return {19 IE1}}
		}
		{PT1}	{	;# Timer 1
			if {!$controllers_conf(ET1)}	{return 0}
			if {$controllers_conf(TF1)}	{return {27 TF1}}
		}
		{PS}	{	;# UART & SPI
			if {!$controllers_conf(ES)}	{return 0}
			if {$feature_available(uart)} {
				if {$controllers_conf(TI)} {
					return {35 TI}
				} elseif {$controllers_conf(RI)} {
					return {35 RI}
				}
			}
			if {$feature_available(spi) && $controllers_conf(SPIE) && $controllers_conf(SPIF)} {
				return {35 SPIE}
			}
		}
		{PT2}	{	;# Timer 2
			if {!$controllers_conf(ET2)}	{return 0}
			if {$controllers_conf(TF2)} {
				return {43 TF2}
			} elseif {
				$controllers_conf(EXF2) && $timer_2_running && (
					$controllers_conf(T2OE) || $controllers_conf(RCLK) ||
					$controllers_conf(TCLK) || $controllers_conf(CPRL2)
				)
			} then {
				return {43 EXF2}
			} else {
				return 0
			}
		}
		{PC}	{	;# Analog comparator
			if {!$controllers_conf(EC)}	{return 0}
			if {$controllers_conf(CF)}	{return {51 CF}}
		}
	}
	return 0
}

# >>> File inclusion guard
}
# <<< File inclusion guard