Featured image of post 改变f6最后vga.hex显示的图片

改变f6最后vga.hex显示的图片

效果展示

用ChatGPT写了个py小脚本,用于替换vga.hex的像素帧

使用方法:

1
$ python3 change_vga.py <image_file>

使用时需要确保所在目录下有vga.hex文件,且安装了ImageMagick工具集

话不多说直接上源码😋

  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
import subprocess
import sys
import os

# ---------------------------
# 1. 参数处理
# ---------------------------
if len(sys.argv) != 2:
    print(f"Usage: python {sys.argv[0]} <image_file>")
    sys.exit(1)

img_file = sys.argv[1]

if not os.path.exists(img_file):
    print(f"Error: file '{img_file}' not found")
    sys.exit(1)

# ---------------------------
# 2. 转换图片为 raw RGB
# ---------------------------
proc = subprocess.run(
    [
        'convert',
        img_file,
        '-resize', '256x256!',
        '-depth', '8',
        'rgb:-'
    ],
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

if proc.returncode != 0:
    print("ImageMagick error:")
    print(proc.stderr.decode())
    sys.exit(1)

raw = proc.stdout

pixel_count = 256 * 256
assert len(raw) == pixel_count * 3, "RGB data size mismatch"

# ---------------------------
# 3. 生成 hex 行
# ---------------------------
words_per_line = 8
pixel_start_addr = 0x12bc0

new_lines = []

for i in range(0, pixel_count, words_per_line):
    addr = pixel_start_addr + i
    words = []

    for j in range(words_per_line):
        idx = (i + j) * 3
        r, g, b = raw[idx], raw[idx+1], raw[idx+2]
        words.append(f"00{r:02x}{g:02x}{b:02x}")

    new_lines.append(f"{addr:05x}: {' '.join(words)}")

# ---------------------------
# 4. 读取 vga.hex
# ---------------------------
with open('vga.hex', 'r') as f:
    lines = f.readlines()

# ---------------------------
# 5. 找 pixel 区域起点
# ---------------------------
start_idx = None
for i, line in enumerate(lines):
    s = line.strip()
    if s.startswith('12bc0:'):
        start_idx = i
        break

if start_idx is None:
    print("Error: start address 12bc0 not found in vga.hex")
    sys.exit(1)

# ---------------------------
# 6. 找结束位置
# ---------------------------
end_idx = None
for i in range(len(lines)-1, -1, -1):
    s = lines[i].strip()
    if s and ':' in s and not s.startswith('v'):
        end_idx = i
        break

if end_idx is None:
    print("Error: cannot determine end of vga.hex")
    sys.exit(1)

# ---------------------------
# 7. 替换区域
# ---------------------------
new_hex = (
    lines[:start_idx] +
    [line + "\n" for line in new_lines] +
    lines[end_idx+1:]
)

# ---------------------------
# 8. 写回文件
# ---------------------------
with open('vga.hex', 'w') as f:
    f.writelines(new_hex)

print(f"Updated vga.hex with image: {img_file}")
Licensed under CC BY-NC-SA 4.0