OpenCores
URL https://opencores.org/ocsvn/s80186/s80186/trunk

Subversion Repositories s80186

[/] [s80186/] [trunk/] [tests/] [python/] [Runner.py] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jamieiles
# Copyright Jamie Iles, 2017
2
#
3
# This file is part of s80x86.
4
#
5
# s80x86 is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# s80x86 is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with s80x86.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
import os
19
import sys
20
 
21
from py8086sim.Cpu import GPR
22
 
23
class Runner(object):
24
    binary = None
25
    load_address = 0
26
 
27
    def __init__(self, simname):
28
        simtype = getattr(sys.modules['py8086sim.Cpu'], simname)
29
        self.cpu = simtype(self.binary)
30
        self.cpu.reset()
31
        self.cpu.write_reg(GPR.CS, 0x0000)
32
 
33
        with open(os.path.join('${CMAKE_CURRENT_BINARY_DIR}', 'programs', self.binary)) as b:
34
            bytevec = [ord(v) for v in b.read()]
35
            if len(bytevec) % 2 == 1:
36
                bytevec.append(0)
37
            wordvec = []
38
            for a, b in zip(bytevec[::2], bytevec[1::2]):
39
                wordvec.append(a | (b << 8))
40
            self.cpu.write_vector16(self.cpu.read_reg(GPR.CS),
41
                                    self.load_address, wordvec)
42
 
43
    def run(self, max_cycles=1000):
44
        self.setup()
45
 
46
        for i in xrange(max_cycles):
47
            if self.cpu.has_trapped():
48
                self.int3_hook()
49
                self.validate_result()
50
                return
51
            self.cpu.step()
52
 
53
        raise Exception('Test timed out')
54
 
55
    def setup(self):
56
        pass
57
 
58
    def int3_hook(self):
59
        pass
60
 
61
    def validate_result(self):
62
        pass
63
 
64
    def assert_reg_equal(self, regnum, expected):
65
        v = self.cpu.read_reg(regnum)
66
        if v != expected:
67
            raise ValueError('Register ' + str(regnum) + ': {0:x} != {1:x}'.format(v, expected))
68
 
69
    def assert_mem_equal(self, segment, address, expected, width=8):
70
        if width == 8:
71
            v = self.cpu.read_mem8(segment, address)
72
        elif width == 16:
73
            v = self.cpu.read_mem16(segment, address)
74
        else:
75
            raise ValueError('Invalid memory width')
76
        if v != expected:
77
            raise ValueError('mem[{0:x}]: {1:x} != {2:x}'.format(address, v, expected))
78
 
79
    def write_reg(self, reg, val):
80
        self.cpu.write_reg(reg, val)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.