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

« back to all changes in this revision

Viewing changes to regress/test_opcodes/test_MULS.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_MULS.py,v 1.1 2002/04/02 16:12:55 troth Exp $
 
24
#
 
25
 
 
26
"""Test the MULS opcode.
 
27
"""
 
28
 
 
29
import base_test
 
30
from registers import Reg, SREG
 
31
 
 
32
class MULS_TestFail(base_test.TestFail): pass
 
33
 
 
34
class base_MULS(base_test.opcode_test):
 
35
        """Generic test case for testing MULS opcode.
 
36
 
 
37
        description: multiply two signed numbers and save the result in R1:R0 (highb:lowbyte)
 
38
        opcode:      0000 0010 dddd rrrr   16 <= d,r <= 31
 
39
        changes:     R1,R0,SREG: C set if R15 set, Z set if result 0x0000
 
40
        """
 
41
        def setup(self):
 
42
                # Set SREG to zero or only C flag set
 
43
                self.setup_regs[Reg.SREG] = 0x0
 
44
 
 
45
                # Set the register values
 
46
                self.setup_regs[self.Rd] = self.Vd & 0xff
 
47
                self.setup_regs[self.Rr] = self.Vr & 0xff
 
48
 
 
49
                # Return the raw opcode
 
50
                return 0x0200 | (self.Rr & 0x0f) | (self.Rd & 0x0f) << 4 
 
51
 
 
52
        def analyze_results(self):
 
53
                self.reg_changed.extend( [Reg.R00, Reg.R01, Reg.SREG] )
 
54
 
 
55
                if (self.Vd & 0x80):
 
56
                        # Vd is a negative 8 bit number, so convert to 32 bit
 
57
                        self.Vd |= -256
 
58
 
 
59
                if (self.Vr & 0x80):
 
60
                        # Vd is a negative 8 bit number, so convert to 32 bit
 
61
                        self.Vr |= -256
 
62
 
 
63
                # check that result is correct
 
64
                res = (self.Vd * self.Vr)
 
65
                expect = res & 0xffff
 
66
 
 
67
                got = self.anal_regs[Reg.R00] | self.anal_regs[Reg.R01] << 8
 
68
                
 
69
                if expect != got:
 
70
                        self.fail('MULS calc r%02d, r%02d: %d * %d = (expect=%04x, got=%04x)' % (
 
71
                                self.Rd, self.Rr, self.Vd, 
 
72
                                self.Vr, expect, got))
 
73
 
 
74
                expect_sreg = 0
 
75
 
 
76
                # calculate what we expect sreg to be 
 
77
                C = (expect & 0x8000) >> 15
 
78
                expect_sreg += (expect == 0) << SREG.Z
 
79
                expect_sreg += C  << SREG.C
 
80
                
 
81
                got_sreg = self.anal_regs[Reg.SREG]
 
82
 
 
83
                if expect_sreg != got_sreg:
 
84
                        self.fail('MULS flag setting: 0x%d * 0x%d -> SREG (expect=%02x, got=%02x)' % (
 
85
                                self.Vd, self.Vr, expect_sreg, got_sreg))
 
86
                
 
87
#
 
88
# Template code for test case.
 
89
# The fail method will raise a test specific exception.
 
90
#
 
91
template = """
 
92
class MULS_rd%02d_vd%02x_rr%02d_vr%02x_TestFail(MULS_TestFail): pass
 
93
 
 
94
class test_MULS_rd%02d_vd%02x_rr%02d_vr%02x(base_MULS):
 
95
        Rd = %d
 
96
        Vd = %d
 
97
        Rr = %d
 
98
        Vr = %d
 
99
        def fail(self,s):
 
100
                raise MULS_rd%02d_vd%02x_rr%02d_vr%02x_TestFail, s
 
101
"""
 
102
 
 
103
#
 
104
# Define a list of test values such that we all the cases of SREG bits being set.
 
105
#
 
106
vals = (
 
107
( 0x00, 0x00),
 
108
( 0xff, 0x01),
 
109
( 0x01, 0xff),
 
110
( 0xff, 0xff),
 
111
( 0xff, 0x00),
 
112
( 0x00, 0xb3),
 
113
( 0x80, 0x7f),
 
114
( 0x80, 0x80),
 
115
( 0x7f, 0x7f),
 
116
( 0x4d, 0x4d),
 
117
)
 
118
 
 
119
#
 
120
# automagically generate the test_MULS_rdNN_vdXX_rrNN_vrXX_C[01] class definitions.
 
121
# For these, we don't want Rd=Rr as that is a special case handled below.
 
122
#
 
123
code = ''
 
124
step = 3
 
125
for d in range(16,32,step):
 
126
        for r in range(17,32,step):
 
127
                for vd,vr in vals:
 
128
                        args = (d, vd, r, vr)*4
 
129
                        code += template % args
 
130
 
 
131
#
 
132
# Special case when Rd==Rr, make sure Vd==Vr.
 
133
#
 
134
for d in range(16,32,step):
 
135
        for vd,vr in vals:
 
136
                args = (d, vd, d, vd)*4
 
137
                code += template % args
 
138
exec code