font-compile.py
author Tero Marttila <terom@paivola.fi>
Thu, 03 Apr 2014 19:44:13 +0300
changeset 51 ec6271f0637b
parent 39 d7eac199d323
permissions -rw-r--r--
make: fix build-deps, and avr-objdump -d build/src/hello.elf

build/src/hello.elf: file format elf32-avr


Disassembly of section .text:

00000000 <__vectors>:
0: 0c 94 34 00 jmp 0x68 ; 0x68 <__ctors_end>
4: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
8: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
10: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
14: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
18: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
1c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
20: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
24: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
28: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
2c: 0c 94 58 00 jmp 0xb0 ; 0xb0 <__vector_11>
30: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
34: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
38: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
3c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
40: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
44: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
48: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
4c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
50: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
54: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
58: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
5c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
60: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
64: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>

00000068 <__ctors_end>:
68: 11 24 eor r1, r1
6a: 1f be out 0x3f, r1 ; 63
6c: cf ef ldi r28, 0xFF ; 255
6e: d8 e0 ldi r29, 0x08 ; 8
70: de bf out 0x3e, r29 ; 62
72: cd bf out 0x3d, r28 ; 61
74: 0e 94 90 00 call 0x120 ; 0x120 <main>
78: 0c 94 a3 00 jmp 0x146 ; 0x146 <_exit>

0000007c <__bad_interrupt>:
7c: 0c 94 00 00 jmp 0 ; 0x0 <__vectors>

00000080 <timer_init>:
80: 10 92 80 00 sts 0x0080, r1
84: 88 e0 ldi r24, 0x08 ; 8
86: 80 93 81 00 sts 0x0081, r24
8a: 10 92 82 00 sts 0x0082, r1
8e: 08 95 ret

00000090 <timer1_start>:
90: 10 92 85 00 sts 0x0085, r1
94: 10 92 84 00 sts 0x0084, r1
98: 90 93 89 00 sts 0x0089, r25
9c: 80 93 88 00 sts 0x0088, r24
a0: f1 9a sbi 0x1e, 1 ; 30
a2: 82 e0 ldi r24, 0x02 ; 2
a4: 80 93 6f 00 sts 0x006F, r24
a8: 8d e0 ldi r24, 0x0D ; 13
aa: 80 93 81 00 sts 0x0081, r24
ae: 08 95 ret

000000b0 <__vector_11>:
b0: 1f 92 push r1
b2: 0f 92 push r0
b4: 0f b6 in r0, 0x3f ; 63
b6: 0f 92 push r0
b8: 11 24 eor r1, r1
ba: 10 92 81 00 sts 0x0081, r1
be: f1 98 cbi 0x1e, 1 ; 30
c0: 0f 90 pop r0
c2: 0f be out 0x3f, r0 ; 63
c4: 0f 90 pop r0
c6: 1f 90 pop r1
c8: 18 95 reti

000000ca <timer_sleep>:
ca: 0e 94 48 00 call 0x90 ; 0x90 <timer1_start>
ce: 81 e0 ldi r24, 0x01 ; 1
d0: 83 bf out 0x33, r24 ; 51
d2: 01 c0 rjmp .+2 ; 0xd6 <timer_sleep+0xc>
d4: 88 95 sleep
d6: 8e b3 in r24, 0x1e ; 30
d8: 81 fd sbrc r24, 1
da: fc cf rjmp .-8 ; 0xd4 <timer_sleep+0xa>
dc: 13 be out 0x33, r1 ; 51
de: 08 95 ret

000000e0 <serial_init>:
e0: 10 92 c0 00 sts 0x00C0, r1
e4: 10 92 c1 00 sts 0x00C1, r1
e8: 86 e0 ldi r24, 0x06 ; 6
ea: 80 93 c2 00 sts 0x00C2, r24
ee: 87 e6 ldi r24, 0x67 ; 103
f0: 90 e0 ldi r25, 0x00 ; 0
f2: 90 93 c5 00 sts 0x00C5, r25
f6: 80 93 c4 00 sts 0x00C4, r24
fa: 08 95 ret

000000fc <serial_enable>:
fc: 88 e1 ldi r24, 0x18 ; 24
fe: 80 93 c1 00 sts 0x00C1, r24
102: 08 95 ret

00000104 <serial_read>:
104: 80 91 c0 00 lds r24, 0x00C0
108: 87 ff sbrs r24, 7
10a: fc cf rjmp .-8 ; 0x104 <serial_read>
10c: 80 91 c6 00 lds r24, 0x00C6
110: 08 95 ret

00000112 <serial_write>:
112: 90 91 c0 00 lds r25, 0x00C0
116: 95 ff sbrs r25, 5
118: fc cf rjmp .-8 ; 0x112 <serial_write>
11a: 80 93 c6 00 sts 0x00C6, r24
11e: 08 95 ret

00000120 <main>:
120: 1f 93 push r17
122: 0e 94 40 00 call 0x80 ; 0x80 <timer_init>
126: 0e 94 70 00 call 0xe0 ; 0xe0 <serial_init>
12a: 25 9a sbi 0x04, 5 ; 4
12c: 0e 94 7e 00 call 0xfc ; 0xfc <serial_enable>
130: 78 94 sei
132: 88 e5 ldi r24, 0x58 ; 88
134: 10 e2 ldi r17, 0x20 ; 32
136: 0e 94 89 00 call 0x112 ; 0x112 <serial_write>
13a: 85 b1 in r24, 0x05 ; 5
13c: 81 27 eor r24, r17
13e: 85 b9 out 0x05, r24 ; 5
140: 0e 94 82 00 call 0x104 ; 0x104 <serial_read>
144: f8 cf rjmp .-16 ; 0x136 <main+0x16>

00000146 <_exit>:
146: f8 94 cli

00000148 <__stop_program>:
148: ff cf rjmp .-2 ; 0x148 <__stop_program>
def read_block (fh) :
    """
        Yield a series of non-empty lines from the given file, ignoring any leading empty lines, and stopping after the first empty line
    """

    leading = True

    for line in fh :
        if line.strip() :
            leading = False

            # yield non-empty
            yield line

        elif leading :
            # skip leading empty
            continue

        else :
            # stop on empty
            return

    else :
        # EOF
        return

def read_charblock (lines) :
    """
        Read in a char from the given lines, returning an
            (ascii, rows)

        tuple, or None, if there weren't any more blocks
    """

    # the ascii code as a char
    ascii = ''

    # row data as ints
    rows = []

    for line in lines :
        line = line.strip()

        if line.startswith(';') :
            # set ascii code
            ascii = line.replace(';', '').strip()

            if not ascii :
                print 'read_charblock', 'empty'

                # skip
                return None

            elif len(ascii) == 1 :
                print 'read_charblock', 'simplechar', ascii

            else :
                ascii = ascii.decode('string_escape')
        
                print 'read_charblock', 'decodechar', ascii

            assert len(ascii) == 1
        
        else :
            # convert
            row = line.replace('#', '1').replace('-', '0')

            print 'read_charblock', 'row', row

            # 6 columns
            assert len(row) == 6

            # from binary
            row = int(row, 2)

            rows.append(row)
    
    # got data?
    if ascii and rows :
        # 8 rows
        assert len(rows) == 8

        return ascii, rows

    else :
        # nope, empty block, EOF
        return None

def read_charblocks (fh) :
    """
        Read in all char blocks as (ascii, rows) tuples from given file
    """

    while True :
        out = read_charblock(read_block(fh))

        if out :
            yield out

        else :
            break

def decode_rows (inrows) :
    """
        Decode char def data from its 6x8 row format into the format we need (6x8 col format)
    """

    outcols = [0x00] * 6

    for rowidx, row in enumerate(inrows) :
        
        for colidx, col in enumerate(outcols) :
            # get bit from row
            bit = (row >> (5 - colidx)) & 1

            # set bit into column
            outcols[colidx] |= (bit << rowidx)

    # ok...
    return outcols

def write_chardef (fh, ascii, cols) :
    """
        Write out character definition block to given .def file, using given char code and column data
    """

    fh.write(
            ("; %r\n" % ascii)
        +   (".db %s\n" % (', '.join(bin(col) for col in cols)))
        +   ("\n")
    )

def compile_fonts (infh, outf) :
    """
        Compile char blocks from infh, writing out definitions to outf
    """

    charmap = dict()

    # decode in
    for charblock in read_charblocks(infh) :
        # unpack
        ascii, rows = charblock

        # convert
        cols = decode_rows(rows)

        # map
        charmap[ascii] = cols

        print 'compile_fonts', 'read', ascii

    # detect min/max syms
    syms = charmap.keys()
    font_start = min(syms)
    font_end = max(syms)

    assert(ord(font_start) < ord(font_end))
    
    # write out
    outf.write(
            ";; AUTOMATICALLY GENERATED - Do not edit!\n"
            ";; 8x6 font, '0' - '1', rows-by-col format\n"
        +  (".equ FONT_8x6_START = %d ; %r\n" % (ord(font_start), font_start))
        +  (".equ FONT_8x6_END = %d ; %r\n" % (ord(font_end), font_end))
        +  (".equ FONT_8x6_COLS = %d\n" % (6, ))
        +  (".equ FONT_8x6_ROWS = %d\n" % (8, ))
        +   "FONT_8x6:\n"
            "\n"
    )

    # default symbol for unknown chars
    defsym = charmap['\0']

    for char in xrange(ord(font_start), ord(font_end) + 1) :
        ascii = chr(char)
        cols = charmap.get(ascii, defsym)

        write_chardef(outf, ascii, cols)

def main () :
    import sys, getopt

    opts, args = getopt.getopt(sys.argv[1:], '')

    inpath, outpath = args

    # run
    compile_fonts(open(inpath, 'r'), open(outpath, 'w'))

if __name__ == '__main__' :
    main()