2
###############################################################################
4
# simulavr - A simulator for the Atmel AVR family of microcontrollers.
5
# Copyright (C) 2001, 2002 Theodore A. Roth
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
###############################################################################
23
# $Id: test_SBCI.py,v 1.1 2002/02/23 01:35:39 troth Exp $
26
"""Test the SBCI opcode.
30
from registers import Reg, SREG
32
class SBCI_TestFail(base_test.TestFail): pass
34
class base_SBCI(base_test.opcode_test):
35
"""Generic test case for testing SBCI opcode.
37
SBCI - Subtract Immediate with Carry. [Rd <- Rd - K - C]
38
opcode is '0100 kkkk dddd kkkk' where d is 16-31 and K is 0-255
40
Only registers PC, Rd and SREG should be changed.
43
# Set SREG to zero or (Z and/or C flag set)
44
self.setup_regs[Reg.SREG] = (self.C << SREG.C) | (self.Z << SREG.Z)
46
# Set the register values
47
self.setup_regs[self.Rd] = self.Vd
49
# Return the raw opcode
50
return 0x4000 | ((self.Rd - 16) << 4) | ((self.Vk & 0xf0) << 4) | (self.Vk & 0xf)
52
def analyze_results(self):
53
self.reg_changed.extend( [self.Rd, Reg.SREG] )
55
# check that result is correct
56
res = (self.Vd - self.Vk - self.C)
59
got = self.anal_regs[self.Rd]
62
self.fail('SBCI r%02d, 0x%02x: 0x%02x - 0x%02x - %d = (expect=%02x, got=%02x)' % (
63
self.Rd, self.Vk, self.Vd, self.Vk, self.C, expect, got))
67
# calculate what we expect sreg to be (I and T should be zero)
68
carry = ((~self.Vd & self.Vk) | (self.Vk & res) | (res & ~self.Vd))
71
V = (((self.Vd & ~self.Vk & ~res) | (~self.Vd & self.Vk & res)) >> 7) & 1
72
N = ((expect & 0x80) != 0)
73
expect_sreg += H << SREG.H
74
expect_sreg += V << SREG.V
75
expect_sreg += N << SREG.N
76
expect_sreg += (N ^ V) << SREG.S
77
expect_sreg += C << SREG.C
80
expect_sreg += self.Z << SREG.Z
82
got_sreg = self.anal_regs[Reg.SREG]
84
if expect_sreg != got_sreg:
85
self.fail('SBCI r%02d, 0x%02x: 0x%02x - 0x%02x - %d -> SREG (expect=%02x, got=%02x)' % (
86
self.Rd, self.Vk, self.Vd, self.Vk, self.C, expect_sreg, got_sreg))
89
# Template code for test case.
90
# The fail method will raise a test specific exception.
93
class SBCI_r%02d_v%02x_k%02x_C%d_Z%d_TestFail(SBCI_TestFail): pass
95
class test_SBCI_r%02d_v%02x_k%02x_C%d_Z%d(base_SBCI):
102
raise SBCI_r%02d_v%02x_k%02x_C%d_Z%d_TestFail, s
106
# Define a list of test values such that we all the cases of SREG bits being set.
119
# automagically generate the test_SBCI_rNN_vXX_kXX_C[01]_Z[01] class definitions.
120
# For these, we don't want Rd=Rr as that is a special case handled below.
123
for c,z in ((0,0), (1,0), (0,1), (1,1)):
124
for d in range(16,32):
126
args = (d,vd,vk,c,z)*4
127
code += template % args