This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bs83bdis.py
426 lines (363 loc) · 16.2 KB
/
bs83bdis.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env python3
# Copyright (c) 2016 Alessandro Gatti
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgement in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
from enum import Enum
from functools import partial
import os
import struct
import io
import argparse
CODE_ADDRESS_START = 0x0000
CODE_ADDRESS_END = 0x07FF
CODE_FILE_MAX_SIZE = 0x1000
class Format(Enum):
SPECIAL = 1
M2A = 2
A2M = 3
LITERAL = 4
ADDRESS = 5
BIT = 6
MEMORY = 7
INVALID = 8
class Instruction(object):
def __init__(self, address, opcode_name, opcode_type, word):
if (address < CODE_ADDRESS_START) or (address > CODE_ADDRESS_END):
raise Exception('Address 0x%04X out of range' % address)
self.address = address
self.word = word
self.first = None
self.second = None
if opcode_type == Format.SPECIAL:
pass
elif opcode_type == Format.M2A:
self.first = Instruction.get_data_address(word)
elif opcode_type == Format.A2M:
self.first = Instruction.get_data_address(word)
elif opcode_type == Format.LITERAL:
self.first = word & 0xFF
elif opcode_type == Format.ADDRESS:
self.first = word & 0x7FF
elif opcode_type == Format.BIT:
self.first = Instruction.get_data_address(word)
self.second = Instruction.get_bit(word)
elif opcode_type == Format.MEMORY:
self.first = Instruction.get_data_address(word)
elif opcode_type == Format.INVALID:
pass
else:
raise Exception('Invalid or unrecognised opcode format')
self.type = opcode_type
self.name = None
if opcode_name:
self.name = opcode_name.strip()
if (opcode_type != Format.INVALID) and not self.name:
raise Exception('Invalid or missing opcode name')
@staticmethod
def get_data_address(word):
return (word & 0x7F) + (0x80 if word & (1 << 14) else 0)
@staticmethod
def get_bit(word):
return (word >> 7) & 7
class Disassembler(object):
def __init__(self, input_path, labels=True, listing=False):
self._words = self._load_binary_file(input_path)
self._code_address = 0
self._listing = listing
self._instructions = [self._decode_word(word, address)
for (word, address) in enumerate(self._words)]
self._labels = {}
self._variables = {}
self._has_labels = labels
if labels:
self._assign_labels()
self._map_variables()
def generate_output(self):
return self._produce_listing() if self._listing else \
self._produce_source()
_TEMPLATE = '''\t\tinclude BS83B08-3.inc
\t\tds\t.section\t'data'
%s
\t\tcs\t.section\tat 000h\t'code'
%s'''
def _produce_source(self):
code = self._produce_output(partial(self._format_header_source))
data = ''
with io.StringIO() as output:
last = 0
for location in sorted(self._variables.keys()):
if location != last:
output.write('\t\tORG %04Xh\n' % location)
last = location
last = location + 1
output.write('\t\t%s\tDB ?\n' % self._variables[location])
data = output.getvalue()
return Disassembler._TEMPLATE % (data, code)
def _produce_listing(self):
return self._produce_output(partial(self._format_header_listing))
def _produce_output(self, formatter):
with io.StringIO() as output:
for (address, instruction) in enumerate(self._instructions):
output.write(self._format_instruction(
formatter, address, instruction))
return output.getvalue()
def _load_binary_file(self, path):
if not os.path.isfile(path):
raise Exception('%s is not a file' % path)
statinfo = os.stat(path)
if statinfo.st_size == 0:
raise Exception('%s does not contain any code' % path)
elif (statinfo.st_size % 2) == 1:
raise Exception('%s is not word-aligned' % path)
elif statinfo.st_size > CODE_FILE_MAX_SIZE:
raise Exception('%s is too big to fit in the MCU memory' % path)
with open(path, 'rb') as source:
source_bytes = source.read(statinfo.st_size)
if len(source_bytes) != statinfo.st_size:
raise Exception('I/O error when reading %s' % path)
return struct.unpack('<%dH' % (statinfo.st_size / 2), source_bytes)
_MEMORY_MAP = [
# 00H
'IAR0', 'MP0', 'IAR1', 'MP1', 'BP', 'ACC', 'PCL', 'TBLP', 'TBLH',
'TBHP', 'STATUS', 'SMOD', 'CTRL', 'INTEG', 'INTC0', 'INTC1',
# 10H
None, None, None, 'LVRC', 'PA', 'PAC', 'PAPU', 'PAWU', None, None,
'WDTC', 'TBC', 'TMR', 'TMRC', 'EEA', 'EED', 'PB',
# 20H
'PBC', 'PBPU', 'I2CTOC', 'SIMC0', 'SIMC1', 'SIMD', 'SIMC2', None, None,
None, None, None, None, None, None, None,
# 30H
None, None, None, None, None, None, None, None, None, None, None, None,
None, None, None, None,
# 40H
None, None, None, 'TKTMR', 'TKC0', 'TK16DL', 'TK16DH', 'TKC1',
'TKM016DL', 'TKM016DH', 'TKM0ROL', 'TKM0ROH', 'TKM0C0', 'TKM0C1',
'TKM116DL', 'TKM116DH',
# 50H
'TKM1ROL', 'TKM1ROH', 'TKM1C0', 'TKM1C1', None, None, None, None,
None, None, None, None, None, None, None, None
]
_SPECIAL_MASK = 0b1111111111111000
_SPECIAL_MARK = 0b0000000000000000
_SPECIAL_LIST = 0b0000000000000111
_SPECIAL_OPCODES = {
0b0000000000000000: 'NOP',
0b0000000000000001: 'CLR WDT1',
0b0000000000000010: 'HALT',
0b0000000000000011: 'RET',
0b0000000000000100: 'RETI',
0b0000000000000101: 'CLR WDT2',
}
_BIT_MASK = 0b1011000000000000
_BIT_MARK = 0b0011000000000000
_BIT_LIST = 0b0011110000000000
_BIT_OPCODES = {
0b0011000000000000: 'SET',
0b0011010000000000: 'CLR',
0b0011100000000000: 'SNZ',
0b0011110000000000: 'SZ',
}
_ADDRESS_MASK = 0b1111000000000000
_ADDRESS_MARK = 0b0010000000000000
_ADDRESS_LIST = 0b1111100000000000
_ADDRESS_OPCODES = {
0b0010000000000000: 'CALL',
0b0010100000000000: 'JMP',
}
_LITERAL_MASK = 0b1000100000000000
_LITERAL_MARK = 0b0000100000000000
_LITERAL_LIST = 0b0000111100000000
_LITERAL_OPCODES = {
0b0000100100000000: 'RET',
0b0000101000000000: 'SUB',
0b0000101100000000: 'ADD',
0b0000110000000000: 'XOR',
0b0000110100000000: 'OR',
0b0000111000000000: 'AND',
0b0000111100000000: 'MOV',
}
_M2A_MASK = 0b1001111110000000
_M2A_MARK = 0b0000000010000000
_M2A_LIST = 0b0001111110000000
_M2A_OPCODES = {
0b0000000010000000: 'MOV',
}
_OTHER_OPCODES_MASK = 0b1001111110000000
_OTHER_OPCODES = {
0b0000000010000000: (Format.M2A, 'MOV'),
0b0000000100000000: (Format.MEMORY, 'CPLA'),
0b0000000110000000: (Format.MEMORY, 'CPL'),
0b0000001000000000: (Format.A2M, 'SUB'),
0b0000001010000000: (Format.A2M, 'SUBM'),
0b0000001100000000: (Format.A2M, 'ADD'),
0b0000001110000000: (Format.A2M, 'ADDM'),
0b0000010000000000: (Format.A2M, 'XOR'),
0b0000010010000000: (Format.A2M, 'XORM'),
0b0000010100000000: (Format.A2M, 'OR'),
0b0000010110000000: (Format.A2M, 'ORM'),
0b0000011000000000: (Format.A2M, 'AND'),
0b0000011010000000: (Format.A2M, 'ANDM'),
0b0000011100000000: (Format.A2M, 'MOV'),
0b0001000000000000: (Format.MEMORY, 'SZA'),
0b0001000010000000: (Format.MEMORY, 'SZ'),
0b0001000100000000: (Format.MEMORY, 'SWAPA'),
0b0001000110000000: (Format.MEMORY, 'SWAP'),
0b0001001000000000: (Format.A2M, 'SBC'),
0b0001001010000000: (Format.A2M, 'SBCM'),
0b0001001100000000: (Format.A2M, 'ADC'),
0b0001001110000000: (Format.A2M, 'ADCM'),
0b0001010000000000: (Format.MEMORY, 'INCA'),
0b0001010010000000: (Format.MEMORY, 'INC'),
0b0001010100000000: (Format.MEMORY, 'DECA'),
0b0001010110000000: (Format.MEMORY, 'DEC'),
0b0001011000000000: (Format.MEMORY, 'SIZA'),
0b0001011010000000: (Format.MEMORY, 'SIZ'),
0b0001011100000000: (Format.MEMORY, 'SDZA'),
0b0001011110000000: (Format.MEMORY, 'SDZ'),
0b0001100000000000: (Format.MEMORY, 'RLA'),
0b0001100010000000: (Format.MEMORY, 'RL'),
0b0001100100000000: (Format.MEMORY, 'RRA'),
0b0001100110000000: (Format.MEMORY, 'RR'),
0b0001101000000000: (Format.MEMORY, 'RLCA'),
0b0001101010000000: (Format.MEMORY, 'RLC'),
0b0001101100000000: (Format.MEMORY, 'RRCA'),
0b0001101110000000: (Format.MEMORY, 'RRC'),
0b0001110100000000: (Format.MEMORY, 'TABRDC'),
0b0001110110000000: (Format.MEMORY, 'TABRDL'),
0b0001111010000000: (Format.MEMORY, 'DAA'),
0b0001111100000000: (Format.MEMORY, 'CLR'),
0b0001111110000000: (Format.MEMORY, 'SET'),
}
def _decode_word(self, address, word):
if (word & Disassembler._SPECIAL_MASK) == Disassembler._SPECIAL_MARK:
opcode_word = word & Disassembler._SPECIAL_LIST
opcode_name = Disassembler._SPECIAL_OPCODES.get(opcode_word, None)
if opcode_name:
return Instruction(address, opcode_name, Format.SPECIAL, word)
if (word & Disassembler._BIT_MASK) == Disassembler._BIT_MARK:
opcode_word = word & Disassembler._BIT_LIST
opcode_name = Disassembler._BIT_OPCODES.get(
opcode_word, None)
if opcode_name:
return Instruction(address, opcode_name, Format.BIT, word)
if (word & Disassembler._ADDRESS_MASK) == Disassembler._ADDRESS_MARK:
opcode_word = word & Disassembler._ADDRESS_LIST
opcode_name = Disassembler._ADDRESS_OPCODES.get(opcode_word, None)
if opcode_name:
return Instruction(address, opcode_name, Format.ADDRESS, word)
opcode_word = word & Disassembler._OTHER_OPCODES_MASK
(opcode_type, opcode_name) = Disassembler._OTHER_OPCODES.get(
opcode_word, (None, None))
if (opcode_type is not None) and (opcode_name is not None):
return Instruction(address, opcode_name, opcode_type, word)
if (word & Disassembler._LITERAL_MASK) == Disassembler._LITERAL_MARK:
opcode_word = word & Disassembler._LITERAL_LIST
opcode_name = Disassembler._LITERAL_OPCODES.get(opcode_word, None)
if opcode_name:
return Instruction(address, opcode_name, Format.LITERAL, word)
if (word & Disassembler._M2A_MASK) == Disassembler._M2A_MARK:
opcode_word = word & Disassembler._M2A_LIST
opcode_name = Disassembler._M2A_OPCODES.get(
opcode_word, None)
if opcode_name:
return Instruction(address, opcode_name, Format.M2A, word)
return Instruction(address, None, Format.INVALID, word)
def _assign_labels(self):
counter = 0
for instruction in self._instructions:
if instruction.type == Format.ADDRESS:
if instruction.first in self._labels:
continue
self._labels[instruction.first] = 'label%04X' % counter
counter += 1
def _map_variables(self):
counter = 0
for instruction in self._instructions:
if instruction.type in (Format.M2A, Format.A2M, Format.BIT,
Format.MEMORY):
if instruction.first >= (len(Disassembler._MEMORY_MAP) - 1):
if instruction.first not in self._variables:
variable_name = 'variable%04X' % counter
self._variables[instruction.first] = variable_name
counter += 1
else:
continue
def _lookup_memory_location(self, location):
if location < 0:
raise Exception('Invalid memory location %d' % location)
result = None
if location < len(Disassembler._MEMORY_MAP):
result = Disassembler._MEMORY_MAP[location]
if not result:
if location in self._variables:
result = '[%s]' % self._variables[location]
else:
result = '[0%02Xh]' % location
return result
def _format_header_listing(self, output, address, instruction):
if address in self._labels:
output.write('\n%s:\n\n' % self._labels[address])
output.write('%04X\t%04X\t' % (address, instruction.word))
def _format_header_source(self, output, address, instruction):
if address in self._labels:
output.write('\n%s:\n\n' % self._labels[address])
output.write('\t\t')
def _format_instruction(self, header_formatter, address, instruction):
with io.StringIO() as output:
header_formatter(output, address, instruction)
if instruction.type == Format.SPECIAL:
output.write(instruction.name)
elif instruction.type == Format.M2A:
target = self._lookup_memory_location(instruction.first)
output.write('%s\t%s, A' % (instruction.name, target))
elif instruction.type == Format.A2M:
target = self._lookup_memory_location(instruction.first)
output.write('%s\tA, %s' % (instruction.name, target))
elif instruction.type == Format.LITERAL:
output.write('%s\tA, 0%02Xh' % (instruction.name,
instruction.first))
elif instruction.type == Format.ADDRESS:
target = self._labels[instruction.first] \
if self._has_labels else ('0%04Xh' % instruction.first)
output.write('%s\t%s' % (instruction.name, target))
elif instruction.type == Format.BIT:
target = self._lookup_memory_location(instruction.first)
output.write('%s\t%s.%d' % (instruction.name, target,
instruction.second))
elif instruction.type == Format.MEMORY:
target = self._lookup_memory_location(instruction.first)
output.write('%s\t%s' % (instruction.name, target))
else:
output.write('dc\t%04X\t; (%s) Invalid opcode' %
(instruction.word,
bin(instruction.word)[2:].zfill(16)))
output.write('\n')
return output.getvalue()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='A Holtek BS83B08A-3 binary code disassembler')
parser.add_argument('binary', metavar='FILE', type=str,
help='the binary file to disassemble')
parser.add_argument('--no-labels', action='store_false',
help='do not generate labels')
parser.add_argument('--listing', action='store_true',
help='emit code listing instead of assembler source')
arguments = parser.parse_args()
disassembler = Disassembler(arguments.binary, labels=arguments.no_labels,
listing=arguments.listing)
print(disassembler.generate_output())
# vim:et:syn=python:fdm=marker:ff=unix:number:ai:sta:fenc=utf-8