aboutsummaryrefslogtreecommitdiffstats
path: root/src/charmem.py
blob: aaac122b50e88d4ab024224efcbc2eddc434ed90 (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
61
62
63
#!/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 = 8
CHAR_WIDTH = 8

i = 0
while i < len(lines):
  i += 1 # skip comment line
  char = lines[i:i+CHAR_HEIGHT]
  for row in char:
    if len(row) != CHAR_WIDTH:
      raise Exception(f"char row number {i} has width != {CHAR_WIDTH}: {len(row)}\n" + row + "\n" + "\n".join(char))
  charset.append(char)
  i += CHAR_HEIGHT + 1

CHAR_N = len(charset)

TOTAL_BYTEARRAY_SIZE = CHAR_N * CHAR_HEIGHT

def string_to_byte(input_string: str):
    # Replace spaces with '0' and '#' with '1'
    binary_string = input_string.replace(' ', '0').replace('█', '1')

    assert(len(input_string) == CHAR_WIDTH)

    # 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)