summaryrefslogtreecommitdiff
path: root/lib/simulator/engine/engine_auxiliary_alo_functions.tcl
blob: d73b57e717cca04834487cac8cddbc2763dcaa65 (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
#!/usr/bin/tclsh
# Part of MCU 8051 IDE ( http://mcu8051ide.sf.net )

############################################################################
#    Copyright (C) 2007-2009 by Martin Ošmera                              #
#    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.             #
############################################################################

# --------------------------------------------------------------------------
# DESCRIPTION
# Part of simulator engine functionality.
#
# --------------------------------------------------------------------------
# AUXILIARY ALO FUNCTIONS
# --------------------------------------------------------------------------

## Generate random octet
 # @return Int - random value in range 0..255
private method undefined_octet {} {
	switch -- ${::Simulator::undefined_value} {
		0 {	;# Return 0
			return 0
		}
		1 {	;# Return 255
			return 255
		}
		2 {	;# Return random value
			set result [expr {int(rand() * 256)}]
			if {$result == 256} {set result 255}
		}
	}
	return $result
}

## Add value to accumulator and affect PSW flags
 # @parm Int val - value to add
 # @return void
private method alo_add {val} {

	# Adjust stepback stack
	if {${::Simulator::reverse_run_steps}} {
		stepback_reg_change S 224
	}

	# Local variables
	set A_h [expr {($sfr(224) & 240) >> 4}]	;# High-order nibble of Acc
	set A_l [expr {$sfr(224) & 15}]		;# Low-order  nibble of Acc
	set val_h [expr {($val & 0x1f0) >> 4}]	;# High-order nibble of val
	set val_l [expr {$val & 15}]		;# Low-order  nibble of val

	# Compute low-order nibble of result
	set result [expr {$val_l + $A_l}]

	# Flag AC
	if {$result > 15} {
		incr val_h
		incr result -16
		setBit $symbol(AC) 1
	} {
		setBit $symbol(AC) 0
	}

	# Compute high-order nibble of result
	incr result [expr {($val_h + $A_h) << 4}]

	# Flag C
	if {$result > 255} {
		incr result -256
		setBit $symbol(C) 1
	} {
		setBit $symbol(C) 0
	}

	# Flag OV
	if {($val < 128) && ($sfr(224) < 128) && ($result > 127)} {
		setBit $symbol(OV) 1
	} elseif {($val > 127) && ($sfr(224) > 127) && ($result < 128)} {
		setBit $symbol(OV) 1
	} {
		setBit $symbol(OV) 0
	}

	# Set Acc
	set sfr(224) $result
	evaluate_sfr 224
}

## Add value to accumulator with carry and affect PSW flags
 # @parm Int val - value to add
 # @return void
private method alo_addc {val} {
	if {[getBit $symbol(C)]} {
		incr val
	}
	alo_add $val
}

## Subtract tegister from ACC with borrow and affect PSW flags
 # @parm Int val - value to subtract
 # @return void
private method alo_subb {val} {

	# Adjust stepback stack
	if {${::Simulator::reverse_run_steps}} {
		stepback_reg_change S 224
	}

	# Flag PSW.C
	if {[getBit $symbol(C)]} {
		incr val
	}

	# Local variables
	set A_h [expr {($sfr(224) & 240) >> 4}]	;# High-order nibble of Acc
	set A_l [expr {$sfr(224) & 15}]		;# Low-order  nibble of Acc
	set val_h [expr {($val & 0x1f0) >> 4}]	;# High-order nibble of val
	set val_l [expr {$val & 15}]		;# Low-order  nibble of val

	# Compute low-order nibble of result
	set result_l [expr {$A_l - $val_l}]

	# Flag AC
	if {$result_l < 0} {
		incr result_l 16
		incr val_h
		setBit $symbol(AC) 1
	} {
		setBit $symbol(AC) 0
	}

	# Compute high-order nibble of result
	set result_h [expr {$A_h - $val_h}]

	# Flag C
	if {$result_h < 0} {
		incr result_h 16
		setBit $symbol(C) 1
	} {
		setBit $symbol(C) 0
	}

	# Compute high-order nibble of result
	set result [expr {($result_h << 4) + $result_l}]

	# Flag OV
	if {($val > 127) && ($sfr(224) < 128) && ($result > 127)} {
		setBit $symbol(OV) 1
	} elseif {($val < 128) && ($sfr(224) > 127) && ($result < 128)} {
		setBit $symbol(OV) 1
	} {
		setBit $symbol(OV) 0
	}

	# Set Acc
	set sfr(224) $result
}