~sforman/PythonOberon

ref: 6d0e2724faf9a0b433b3dfafcf8fcf7267fe768d PythonOberon/oberon/IDE/newcpu.py -rw-r--r-- 1.6 KiB
6d0e2724Simon Forman Py 2 to 3? 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from pkg_resources import resource_filename
from io import BytesIO
from oberon.bootloader import bootloader
from oberon.risc import (
    ByteAddressed32BitRAM,
    Clock,
    Disk,
    FakeSPI,
    LEDs,
    Mouse,
    RISC,
    Serial,
    )


DISKIMG = resource_filename(__name__, '../disk.img')
# FILLSCR = resource_filename(__name__, '../../fillscreen.bin')
FILLSCR = resource_filename(__name__, '../../joy_asmii.bin')


def make_cpu(disk_image, serial=None):
    '''
    Build and return a :py:class:`RISC` object with peripherals.
    '''
    ram = ByteAddressed32BitRAM()
    disk = Disk(disk_image)
    risc_cpu = RISC(bootloader, ram)
    risc_cpu.io_ports[0] = Clock()
    risc_cpu.io_ports[4] = switches = LEDs()
    if serial:
        switches.switches |= 1
        risc_cpu.io_ports[8] = serial_port = Serial(serial)
        risc_cpu.io_ports[12] = serial_port.status
    risc_cpu.io_ports[20] = fakespi = FakeSPI()
    risc_cpu.io_ports[16] = fakespi.data
    fakespi.register(1, disk)
    risc_cpu.io_ports[24] = mouse = Mouse()
    mouse.set_coords(450, 474)
    return risc_cpu


def strfi(fn):
    # Files can't be pickled, but BytesIO objects can.
    with open(fn, 'rb') as file_obj:
        return BytesIO(file_obj.read())


def newcpu():
    cpu = make_cpu(strfi(DISKIMG), strfi(FILLSCR))
    cpu.breakpoints = 'PC == 0'
    cpu.watches = ''
    cpu.decode(0)  # Ensure that all attributes of the cpu have been created.
    return cpu


if __name__ == '__main__':
    import pickle
    cpu = newcpu()
    for _ in range(100):
        cpu.cycle()
    with open('default+100.pickle', 'wb') as f:
        pickle.dump(cpu, f)