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_MULS.py,v 1.1 2002/04/02 16:12:55 troth Exp $
26
"""Test the MULS opcode.
30
from registers import Reg, SREG
32
class MULS_TestFail(base_test.TestFail): pass
34
class base_MULS(base_test.opcode_test):
35
"""Generic test case for testing MULS opcode.
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
42
# Set SREG to zero or only C flag set
43
self.setup_regs[Reg.SREG] = 0x0
45
# Set the register values
46
self.setup_regs[self.Rd] = self.Vd & 0xff
47
self.setup_regs[self.Rr] = self.Vr & 0xff
49
# Return the raw opcode
50
return 0x0200 | (self.Rr & 0x0f) | (self.Rd & 0x0f) << 4
52
def analyze_results(self):
53
self.reg_changed.extend( [Reg.R00, Reg.R01, Reg.SREG] )
56
# Vd is a negative 8 bit number, so convert to 32 bit
60
# Vd is a negative 8 bit number, so convert to 32 bit
63
# check that result is correct
64
res = (self.Vd * self.Vr)
67
got = self.anal_regs[Reg.R00] | self.anal_regs[Reg.R01] << 8
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))
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
81
got_sreg = self.anal_regs[Reg.SREG]
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))
88
# Template code for test case.
89
# The fail method will raise a test specific exception.
92
class MULS_rd%02d_vd%02x_rr%02d_vr%02x_TestFail(MULS_TestFail): pass
94
class test_MULS_rd%02d_vd%02x_rr%02d_vr%02x(base_MULS):
100
raise MULS_rd%02d_vd%02x_rr%02d_vr%02x_TestFail, s
104
# Define a list of test values such that we all the cases of SREG bits being set.
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.
125
for d in range(16,32,step):
126
for r in range(17,32,step):
128
args = (d, vd, r, vr)*4
129
code += template % args
132
# Special case when Rd==Rr, make sure Vd==Vr.
134
for d in range(16,32,step):
136
args = (d, vd, d, vd)*4
137
code += template % args