blob: 25eeec97c204bc85b449c0d03ad1913344f3bdee (
plain)
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
|
#!/usr/bin/env python3
# Generate .bin file with ATK16 CPU TPU charmem data
# Addressed by _CCC CCCC CYYY
# Data width 8bit: ___X XXXX
# where _ = unused, C = 8bit character code, Y = character y coordinate, X = pixel value
import sys
if len(sys.argv) != 2:
print("usage: charmem.py <outfile.bin>")
sys.exit(1)
outfile_path = sys.argv[1]
with open("charset.txt", "r") as f:
lines = [s.strip("\n") for s in f.readlines()]
header_chars = lines[0]
charset: list[list[str]] = []
CHAR_HEIGHT_BITS = 3
CHAR_HEIGHT = 2 ** CHAR_HEIGHT_BITS
CHAR_WIDTH = 5
i = CHAR_HEIGHT
while i < len(lines):
char = lines[i:i+CHAR_HEIGHT]
for row in char:
assert len(row) == CHAR_WIDTH
charset.append(char)
i += CHAR_HEIGHT
CHAR_N = len(charset)
TOTAL_BYTEARRAY_SIZE = CHAR_N * CHAR_HEIGHT
print("TOTAL_BYTEARRAY_SIZE:", TOTAL_BYTEARRAY_SIZE)
def string_to_byte(input_string: str):
# Replace spaces with '0' and '#' with '1'
binary_string = input_string.replace(' ', '0').replace('#', '1') + "000"
# Check if the string contains only '0' or '1'
if not all(c in '01' for c in binary_string):
raise ValueError("Input string must contain only spaces and '#' characters.")
# Convert the binary string to an unsigned integer byte
return int(binary_string[::-1], 2)
res_b = bytearray(TOTAL_BYTEARRAY_SIZE)
for cn, char in enumerate(charset):
for cy in range(0, CHAR_HEIGHT):
addr = (cn << 3) + cy
byte = string_to_byte(char[cy])
print(f"{byte:>08b}")
res_b[addr] = byte
with open(outfile_path, "wb") as f:
f.write(res_b)
|