~ubuntu-branches/ubuntu/dapper/simulavr/dapper

« back to all changes in this revision

Viewing changes to regress/test_opcodes/test_SBCI.py

  • Committer: Bazaar Package Importer
  • Author(s): Shaun Jackman
  • Date: 2004-04-10 13:54:17 UTC
  • Revision ID: james.westby@ubuntu.com-20040410135417-zywapjyz252y65se
Tags: upstream-0.1.2.1
ImportĀ upstreamĀ versionĀ 0.1.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
###############################################################################
 
3
#
 
4
# simulavr - A simulator for the Atmel AVR family of microcontrollers.
 
5
# Copyright (C) 2001, 2002  Theodore A. Roth
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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
 
20
#
 
21
###############################################################################
 
22
#
 
23
# $Id: test_SBCI.py,v 1.1 2002/02/23 01:35:39 troth Exp $
 
24
#
 
25
 
 
26
"""Test the SBCI opcode.
 
27
"""
 
28
 
 
29
import base_test
 
30
from registers import Reg, SREG
 
31
 
 
32
class SBCI_TestFail(base_test.TestFail): pass
 
33
 
 
34
class base_SBCI(base_test.opcode_test):
 
35
        """Generic test case for testing SBCI opcode.
 
36
 
 
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
 
39
 
 
40
        Only registers PC, Rd and SREG should be changed.
 
41
        """
 
42
        def setup(self):
 
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)
 
45
 
 
46
                # Set the register values
 
47
                self.setup_regs[self.Rd] = self.Vd
 
48
 
 
49
                # Return the raw opcode
 
50
                return 0x4000 | ((self.Rd - 16) << 4) | ((self.Vk & 0xf0) << 4) | (self.Vk & 0xf)
 
51
 
 
52
        def analyze_results(self):
 
53
                self.reg_changed.extend( [self.Rd, Reg.SREG] )
 
54
                
 
55
                # check that result is correct
 
56
                res = (self.Vd - self.Vk - self.C)
 
57
                expect = res & 0xff
 
58
 
 
59
                got = self.anal_regs[self.Rd]
 
60
                
 
61
                if expect != got:
 
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))
 
64
 
 
65
                expect_sreg = 0
 
66
 
 
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))
 
69
                H = (carry >> 3) & 1
 
70
                C = (carry >> 7) & 1
 
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
 
78
 
 
79
                if expect == 0:
 
80
                        expect_sreg += self.Z << SREG.Z
 
81
 
 
82
                got_sreg = self.anal_regs[Reg.SREG]
 
83
 
 
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))
 
87
 
 
88
#
 
89
# Template code for test case.
 
90
# The fail method will raise a test specific exception.
 
91
#
 
92
template = """
 
93
class SBCI_r%02d_v%02x_k%02x_C%d_Z%d_TestFail(SBCI_TestFail): pass
 
94
 
 
95
class test_SBCI_r%02d_v%02x_k%02x_C%d_Z%d(base_SBCI):
 
96
        Rd = %d
 
97
        Vd = 0x%x
 
98
        Vk = 0x%x
 
99
        C  = %d
 
100
        Z  = %d
 
101
        def fail(self,s):
 
102
                raise SBCI_r%02d_v%02x_k%02x_C%d_Z%d_TestFail, s
 
103
"""
 
104
 
 
105
#
 
106
# Define a list of test values such that we all the cases of SREG bits being set.
 
107
#
 
108
vals = (
 
109
( 0x00, 0x00 ),
 
110
( 0xff, 0x00 ),
 
111
( 0xfe, 0x01 ),
 
112
( 0x0f, 0x00 ),
 
113
( 0x0f, 0xf0 ),
 
114
( 0x01, 0x02 ),
 
115
( 0x80, 0x01 )
 
116
)
 
117
 
 
118
#
 
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.
 
121
#
 
122
code = ''
 
123
for c,z in ((0,0), (1,0), (0,1), (1,1)):
 
124
        for d in range(16,32):
 
125
                for vd,vk in vals:
 
126
                        args = (d,vd,vk,c,z)*4
 
127
                        code += template % args
 
128
 
 
129
exec code