Debian Codegraph @

examples/ctm7.asm

raw
1 ; Charpad CTM v7 file parser function
2 ;
3 ; The following attributes (depending on file type) may be present in the
4 ; output:
5 ;
6 ;COLORS - global colours
7 ;COLORING.MAP - using per map colours?
8 ;COLORING.TILE - using per tile colours?
9 ;COLORING.CHAR - using per character colours?
10 ;MODE.HIRES - using hires mode?
11 ;MODE.MULTI - using multi mode?
12 ;MODE.ECM - using ecm mode?
13 ;FLAGS.TILES - using tiles?
14 ;CHARS.COUNT - character count
15 ;CHARS.DATA - character bitmaps
16 ;CHARS.ATTRIBUTES - packed attributes
17 ;CHARS.COLORS - character colours
18 ;CHARS.MATERIALS - character material
19 ;TILES.COUNT - tile count
20 ;TILES.WIDTH - tile width in characters
21 ;TILES.HEIGHT - tile height in characters
22 ;TILES.DATA - character index words
23 ;TILES.COLOUR - per tile colours
24 ;TILES.TAGS - tile tag bytes
25 ;TILES.NAMES - tile name strings
26 ;MAP.WIDTH - map width in tiles/characters
27 ;MAP.HEIGHT - map heigh in tiles/characters
28 ;MAP.DATA - map tile/character index (alternating low/high bytes)
29 ;
30 ; For detail check the original specs.
31 ;
32 ctm7 .function data : binary
33 ; helper function which reads count bytes
34 last := ? ; last read
35 pos := 0 ; read position
36 read .function count
37 last ::= data[pos : pos + count]
38 pos += count
39 .endf last
40
41 ; helper function to read count names
42 readnames .function count
43 names := []
44 .rept count
45 name := []
46 .while read(1) != 0
47 name ..= [last]
48 .endwhile
49 names ..= [format("%c", name) .. ...]; strings
50 .endrept
51 .endf names
52
53 ; helper macro to verify block markers
54 marker := x'DAB0' ; first marker
55 blockstart .segment name
56 .cerror read(2) != marker, "Wrong ", (\name), " block marker: ", last
57 marker += x'0001'
58 .endsegment
59
60 ; check if it's a CTM v7 file
61 .cerror read(3) != x'43544D', "Wrong ID: ", last
62 .cerror read(1) != 7, "Wrong version: ", int(last)
63 ; interpret map data
64 mapdata .namespace
65 COLORS = read(5) ; global colours
66 .cerror read(1) > 2, "Unknown coloring method ", int(last)
67 COLORING = {.MAP: last == 0, .TILE: last == 1, .CHAR: last == 2}
68 .cerror read(1) > 2, "Unknown graphics mode ", int(last)
69 MODE = {.HIRES: last == 0, .MULTI: last == 1, .ECM: last == 2}
70 .cerror read(1) > %1, "Unknown flags ", bits(last)
71 FLAGS = {.TILES: bool(last & %1)}
72 ;----
73 CHARS .namespace
74 #blockstart "character"
75 COUNT = read(2) + 1 ; character count
76 DATA = read(COUNT * 8) ; character bitmaps
77
78 #blockstart "attribute"
79 ATTRIBUTES = read(COUNT) ; packed attributes
80 COLORS = ATTRIBUTES & (x'0f' x len(ATTRIBUTES)); colours in low nibble
81 MATERIALS = bytes((ATTRIBUTES ^ COLORS) >> 4); materials in high nibble
82 .endnamespace
83 ;----
84 .if FLAGS.TILES
85 TILES .namespace
86 #blockstart "tile data"
87 COUNT = read(2) + 1 ; tile count
88 WIDTH = int(read(1)) ; tile width
89 HEIGHT = int(read(1)) ; tile height
90 DATA = read(WIDTH * HEIGHT * COUNT * 2); character index words
91
92 .if COLORING.TILE
93 #blockstart "tile colours"
94 COLORS = read(COUNT) ; per tile colours
95 .fi
96
97 #blockstart "tile tags"
98 TAGS = read(COUNT) ; tile tag bytes
99
100 #blockstart "tile names"
101 NAMES = readnames(COUNT) ; tile names
102 .endnamespace
103 .fi
104 ;----
105 MAP .namespace
106 #blockstart "map data"
107 WIDTH = int(read(2)) ; map width
108 HEIGHT = int(read(2)) ; map height
109 DATA = read(WIDTH * HEIGHT * 2); map tile/character index low/high bytes
110 .endnamespace
111
112 .cerror len(data) != pos, "file size wrong"
113 .endnamespace
114 .endf mapdata