Skip to content

Commit 0c0c211

Browse files
committed
Script to create M100 BASIC programs of exact size in bytes
Usage: lorem.sh 5.25 KB > foo.do tokenize foo.do foo.ba (foo.ba is now 5.25 * 1024 == 5376 bytes). This is useful to see how a tokenizer handles programs that are too large to fit in memory. For example, theoretically, one could tokenize a program of exactly 32KB for the Model 100, but the pointers might wrap around badly if tokenized on a Tandy 200.
1 parent b3ba96c commit 0c0c211

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

degenerate/lorem.sh

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash -eu
2+
# Generate BASIC programs as long as necessary.
3+
4+
# Given a number of bytes, generate a M100 BASIC program that uses up
5+
# exactly that many bytes when tokenized.
6+
7+
# Note maximum line length is 256 characters, but that includes four
8+
# bytes for the line number and pointer to the next line plus a fifth
9+
# byte for the NULL at the end. So, actual number of bytes allowed: 251.
10+
11+
# Peculiar note on tokenizing BASIC from serial port or via EDIT: When
12+
# character 252 is a digit, then tokenization proceeds as if there had
13+
# been a newline and the digit is the start of the next line number!
14+
15+
main() {
16+
if [[ $1 ]]; then targetsize=$(parse_iec "$*"); fi
17+
18+
# Avoid problem where the last line has less than the minimum # bytes.
19+
if (( (targetsize % $MAX) > 0 &&
20+
(targetsize % $MAX) <= 5 &&
21+
targetsize > 5 )); then
22+
print_next_line 6
23+
targetsize=$((targetsize - 6))
24+
fi
25+
26+
while (( targetsize > 0 )); do
27+
if (( targetsize < $MAX )); then
28+
print_next_line $targetsize
29+
targetsize=0
30+
else
31+
print_next_line $MAX
32+
targetsize=$((targetsize - $MAX))
33+
fi
34+
done
35+
}
36+
37+
parse_iec() {
38+
# Allow 16384, 16k, 16K, 16KiB, 1 Kbyte, 0.25 Kibibytes, and so on
39+
local maybeiec=$1
40+
local suffix
41+
local rv
42+
maybeiec=${maybeiec^^} # uppercase
43+
maybeiec=${maybeiec// /} # no spaces
44+
for suffix in BYTES BYTE B IBI ILO EGA EBI I; do # discard suffices
45+
maybeiec=${maybeiec%$suffix}
46+
done
47+
48+
echo $maybeiec | numfmt --from=iec
49+
return
50+
}
51+
52+
53+
print_next_line() {
54+
local -i incr=${1:-$MAX}
55+
56+
if (( incr <= 5 )); then
57+
echo "Error: cannot create a line that uses five or fewer bytes.">&2
58+
exit 1
59+
fi
60+
61+
incr=$((incr - 5)) # 2 line num + 2 ptr + 1 null == 5
62+
incr=$((incr - 4)) # ?""; == 4
63+
if (( incr >= 0 )); then
64+
printf "$linenum ?"
65+
printf '"'
66+
printf "${lorem: pos: incr}"
67+
printf '";\n'
68+
pos=$(( pos + incr ))
69+
if (( pos > loremsize )); then
70+
pos=$((pos - loremsize))
71+
fi
72+
else
73+
incr=$((incr + 4)) # Don't use ?"";
74+
printf "$linenum "
75+
colons=":::::"
76+
printf "${colons: 0: incr}\n"
77+
fi
78+
linenum=$((linenum + 10))
79+
}
80+
81+
## Initialization
82+
83+
lorem="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? "
84+
85+
lorem+="At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. "
86+
87+
loremsize=${#lorem}
88+
lorem+="$lorem"
89+
90+
declare -g -i targetsize=16*1024
91+
declare -g -i linenum=10
92+
declare -g -i pos=0
93+
declare -g -i MAX=256
94+
95+
96+
main "$@"
97+

0 commit comments

Comments
 (0)