forked from mstrand/ostrich2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ostrich2.py
executable file
·172 lines (147 loc) · 5.8 KB
/
ostrich2.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python3
import logging, sys, argparse
from ostrich2 import Ostrich2
def _parse_int(value):
return int(value, 0)
def _parse_input_file(value):
return open(value, 'rb')
def _parse_output_file(value):
return open(value, 'wb')
class OstrichCLI(object):
def process_command_line(self):
##
## Parse command line
##
parser = argparse.ArgumentParser(description = 'Command line interface to an Ostrich 2.0 EPROM emulator')
parser.add_argument(
'-v', '--verbose',
action = 'count',
help = 'increase logging level')
parser.add_argument(
'--device',
metavar = 'DEVICE',
default = Ostrich2.default_device(),
help = 'device [default: %(default)s]')
subparsers = parser.add_subparsers(
dest = 'command',
title = 'Ostrich operations',
description = '(See "%(prog)s COMMAND -h" for more info)',
help = '')
subparser = subparsers.add_parser(
'write',
help = 'write data to device')
subparser.add_argument(
'--address',
type = _parse_int,
default = 0,
help = 'memory address')
subparser.add_argument(
'file',
nargs = '?',
type = _parse_input_file,
default = sys.stdin.buffer,
help = 'input filename [default: use stdin]')
subparser = subparsers.add_parser(
'read',
help = 'read memory')
subparser.add_argument(
'start',
type = _parse_int,
help = 'start address (inclusive)')
subparser.add_argument(
'end',
type = _parse_int,
help = 'end address (inclusive)')
subparser.add_argument(
'file',
nargs = '?',
type = _parse_output_file,
default = sys.stdout.buffer,
help = 'output filename [default: use stdout]')
subparser = subparsers.add_parser(
'setbank',
help = 'set bank for emulation and I/O')
subparser.add_argument(
'bank',
type = _parse_int,
help = 'bank number (0-8)')
subparser = subparsers.add_parser('version',
help = 'display device version number and exit',
add_help = False)
args = parser.parse_args()
if not args.verbose:
level = logging.ERROR
elif args.verbose == 1:
level = logging.INFO
else:
level = logging.DEBUG
logging.basicConfig(stream = sys.stderr, level = level, format = '%(message)s')
##
## Handle command
##
logging.info('Connecting to device')
self.ostrich = Ostrich2(args.device)
if args.command == 'version':
self.display_version()
elif args.command == 'read':
self.read_from_device(args.start, args.end, args.file)
elif args.command == 'write':
self.write_to_device(args.file, args.address)
elif args.command == 'setbank':
self.set_bank(args.bank)
self.ostrich.disconnect()
logging.info('Disconnected from device')
def display_version(self):
(device_type, version, device_id) = self.ostrich.version()
(vendor_id, serial_number) = self.ostrich.serial_number()
io_bank = self.ostrich.get_io_bank()
emulation_bank = self.ostrich.get_emulation_bank()
emulation_bank_persistent = self.ostrich.get_emulation_bank(True)
print('%10s: %s' % ('type', device_type))
print('%10s: %s' % ('version', version))
print('%10s: %s' % ('id', device_id))
print('%10s: %s' % ('vendor id', vendor_id))
print('%10s: %s' % ('serial', serial_number))
print('%10s: %s' % ('i/o bank', io_bank))
print('%10s: %s' % ('emu bank', emulation_bank))
print('%10s: %s' % ('[emu bank]', emulation_bank_persistent))
def write_to_device(self, stream, address):
data = stream.read()
progress = ProgressBar(len(data))
for count in self.ostrich.write_memory(data, address):
progress.add(count)
def read_from_device(self, start, end, stream):
progress = ProgressBar(end + 1 - start)
for data in self.ostrich.read_memory(start, end):
stream.write(data)
progress.add(len(data))
def set_bank(self, bank):
self.ostrich.set_io_bank(bank)
self.ostrich.set_emulation_bank(bank, True)
self.ostrich.set_emulation_bank(bank, False)
class ProgressBar:
def __init__(self, max_value, stream = sys.stderr):
self.stream = stream
self.current = 0
self.max_value = max_value
self.add(0)
def add(self, progress):
self.current += progress
self.__update(self.current, self.max_value)
def __update(self, current, max_value):
width = 80
width -= len(' 100%')
padding = len('[>]')
percent = (100 * current) // max_value
percent_string = '%d%%' % percent
arrow_length = (current * width) // max_value - padding
if arrow_length < 0:
arrow_length = 0
bar = '[%s>%s] %4s' % ('=' * arrow_length, '#' * (width - arrow_length - padding), percent_string)
self.stream.write('\r')
self.stream.write(bar)
if current == max_value:
self.stream.write('\n')
self.stream.flush()
if __name__ == '__main__':
OstrichCLI().process_command_line()