Coverage for MC6809/utils/byte_word_values.py: 39%
55 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-06 19:50 +0100
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-06 19:50 +0100
1#!/usr/bin/env python
3"""
4 DragonPy - Dragon 32 emulator in Python
5 =======================================
7 some code is borrowed from:
8 XRoar emulator by Ciaran Anscomb (GPL license) more info, see README
10 :copyleft: 2013-2014 by the DragonLib team, see AUTHORS for more details.
11 :license: GNU GPL v3 or above, see LICENSE for more details.
12"""
15import string
18def signed5(x):
19 """ convert to signed 5-bit """
20 if x > 0xf: # 0xf == 2**4-1 == 15
21 x = x - 0x20 # 0x20 == 2**5 == 32
22 return x
25def signed8(x):
26 """ convert to signed 8-bit """
27 if x > 0x7f: # 0x7f == 2**7-1 == 127
28 x = x - 0x100 # 0x100 == 2**8 == 256
29 return x
32def unsigned8(x):
33 """ convert a signed 8-Bit value into a unsigned value """
34 if x < 0:
35 x = x + 0x0100 # 0x100 == 2**8 == 256
36 return x
39def signed16(x):
40 """ convert to signed 16-bit """
41 if x > 0x7fff: # 0x7fff == 2**15-1 == 32767
42 x = x - 0x10000 # 0x100 == 2**16 == 65536
43 return x
46def word2bytes(value):
47 """
48 >>> word2bytes(0xff09)
49 (255, 9)
51 >>> [hex(i) for i in word2bytes(0xffab)]
52 ['0xff', '0xab']
54 >>> word2bytes(0xffff +1)
55 Traceback (most recent call last):
56 ...
57 AssertionError
58 """
59 assert 0 <= value <= 0xffff
60 return (value >> 8, value & 0xff)
63def bytes2word(byte_list):
64 """
65 >>> bytes2word([0xff,0xab])
66 65451
68 >>> hex(bytes2word([0xff,0xab]))
69 '0xffab'
70 """
71 assert len(byte_list) == 2
72 return (byte_list[0] << 8) + byte_list[1]
75def bin2hexline(data, add_addr=True, width=16):
76 """
77 Format binary data to a Hex-Editor like format...
79 e.g.:
80 with open(r"C:\\Python27\\python.exe", "rb") as f:
81 data = f.read(150)
83 print("\n".join(bin2hexline(data, width=16)))
85 0000 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00 MZ..............
86 0016 b8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 ........@.......
87 0032 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
88 0048 00 00 00 00 00 00 00 00 00 00 00 00 e8 00 00 00 ................
89 0064 0e 1f ba 0e 00 b4 09 cd 21 b8 01 4c cd 21 54 68 ........!..L.!Th
90 0080 69 73 20 70 72 6f 67 72 61 6d 20 63 61 6e 6e 6f is.program.canno
91 0096 74 20 62 65 20 72 75 6e 20 69 6e 20 44 4f 53 20 t.be.run.in.DOS.
92 0112 6d 6f 64 65 2e 0d 0d 0a 24 00 00 00 00 00 00 00 mode....$.......
93 0128 9d 68 ba 89 d9 09 d4 da d9 09 d4 da d9 09 d4 da .h..............
94 0144 d0 71 41 da d8 09 .qA...
95 """
96 assert isinstance(data, bytes), (
97 f"is type: {type(data)} and not bytes/str: {repr(data)}"
98 )
100 addr = 0
101 lines = []
102 run = True
103 line_width = 4 + (width * 3) + 1
104 while run:
105 if add_addr:
106 line = [f"{addr:04d}"]
107 else:
108 line = []
110 ascii_block = ""
111 for i in range(width):
112 b = data[addr]
114 if chr(b) in string.printable:
115 ascii_block += chr(b)
116 else:
117 ascii_block += "."
119 line.append(f"{b:02x}")
121 addr += 1
122 if addr >= len(data):
123 run = False
124 break
126 line = " ".join(line)
127 line = line.ljust(line_width)
128 line += ascii_block
129 lines.append(line)
130 return lines
133def _bin2hexline_example():
134 import sys
136 with open(sys.executable, "rb") as f:
137 data = f.read(500)
139 print("\n".join(bin2hexline(data, width=16)))