Skip to content

Commit 4aba862

Browse files
committed
Update rgbasm(5) docs based on nummacway's feedback
1 parent df5162e commit 4aba862

File tree

1 file changed

+81
-64
lines changed

1 file changed

+81
-64
lines changed

man/rgbasm.5

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ but any program that processes RGBDS object files (described in
2727
can be used in its place.
2828
.Sh SYNTAX
2929
The syntax is line-based, just as in any other assembler.
30-
Each line may have components in this order:
31-
.Pp
32-
.Dl Oo Ar directive Oc Oo ;\ Ns Ar comment Oc
33-
.Dl Oo Ar label : Oc Oo Ar instruction Oo :: Ar instruction ... Oc Oc Oo ;\ Ns Ar comment Oc
30+
Each line may have components in either of these orders:
31+
.Bl -bullet -offset indent
32+
.It
33+
.Li Oo Ar directive Oc Oo ;\ Ns Ar comment Oc
34+
.It
35+
.Li Oo Ar label : Oc Oo Ar instruction Oo :: Ar instruction ... Oc Oc Oo ;\ Ns Ar comment Oc
36+
.El
3437
.Pp
3538
Directives are commands to the assembler itself, such as
3639
.Ic PRINTLN ,
@@ -84,8 +87,8 @@ as the opposite condition code; for example,
8487
for
8588
.Ic z .
8689
.Pp
87-
All reserved keywords (directives, register names, etc.) are case-insensitive;
88-
all identifiers (labels and other symbol names) are case-sensitive.
90+
All reserved keywords (directives, instructions, registers, built-in functions, etc.) are case-insensitive;
91+
all identifiers (labels, variables, etc) are case-sensitive.
8992
.Pp
9093
Comments are used to give humans information about the code, such as explanations.
9194
The assembler
@@ -124,17 +127,17 @@ To do so, put a backslash at the end of the line:
124127
world!"\ \ \ \ \ \ \ \ \ \ \ ;\ Any leading space is included
125128
.Ed
126129
.Ss Symbol interpolation
127-
A funky feature is writing a symbol between
128-
.Ql {braces} ,
129-
called
130-
.Dq symbol interpolation .
130+
Symbols with string or numeric values can be
131+
.Dq interpolated
132+
by writing them inside
133+
.Ql {braces} .
131134
This will paste the symbol's contents as if they were part of the source file.
132135
If it is a string symbol, its characters are simply inserted as-is.
133136
If it is a numeric symbol, its value is converted to hexadecimal notation with a dollar sign
134137
.Sq $
135138
prepended.
136139
.Pp
137-
Symbol interpolations can be nested, too!
140+
Symbol interpolations can be nested, too.
138141
.Bd -literal -offset indent
139142
DEF topic EQUS "life, the universe, and \e"everything\e""
140143
DEF meaning EQUS "answer"
@@ -145,11 +148,7 @@ PRINTLN "The {meaning} to {topic} is {{meaning}}"
145148
PURGE topic, meaning, {meaning}
146149
.Ed
147150
.Pp
148-
Symbols can be
149-
.Em interpolated
150-
even in the contexts that disable automatic
151-
.Em expansion
152-
of string constants:
151+
Symbols can be interpolated even in contexts that disable automatic expansion of string constants: that is,
153152
.Ql name
154153
will be expanded in all of
155154
.Ql DEF({name}) ,
@@ -159,8 +158,10 @@ will be expanded in all of
159158
.Ql PURGE {name} ,
160159
and
161160
.Ql MACRO {name} ,
162-
but, for example, won't be in
163-
.Ql DEF(name) .
161+
even though it won't be in
162+
.Ql DEF(name) ,
163+
.Ql PURGE {name} ,
164+
etc.
164165
.Pp
165166
It's possible to change the way symbols are printed by specifying a print format like so:
166167
.Ql {fmt:symbol} .
@@ -177,14 +178,18 @@ or
177178
.Ql \ .
178179
If specified, prints this character in front of non-negative numbers.
179180
.It Ql <exact> Ta May be
180-
.Ql # .
181-
If specified, prints the value in an "exact" format: with a base prefix for non-decimal integer types
182-
.Pq So $ Sc , So & Sc , or So % Sc ;
181+
.Ql #
182+
for non-decimal types
183+
.Pq not So d Sc or So u Sc .
184+
If specified, prints the value in an "exact" format: with a base prefix
185+
.Pq So $ Sc , So & Sc , or So % Sc
186+
for non-decimal integer types
187+
.Pq So x Sc / So X Sc , So o Sc , or So b Sc ;
183188
with a
184189
.Ql q
185190
precision suffix for fixed-point numbers; or with
186191
.Ql \e
187-
escape characters for strings.
192+
escape characters (but no enclosing quotes) for strings.
188193
.It Ql <align> Ta May be
189194
.Ql - .
190195
If specified, aligns left instead of right.
@@ -210,7 +215,7 @@ followed by zero
210215
.Ql 0
211216
\[en]
212217
.Ql 9
213-
prints zero fractional digits.)
218+
prints zero fractional digits and no decimal point.)
214219
.It Ql <prec> Ta May be
215220
.Ql q
216221
followed by one or more
@@ -226,11 +231,11 @@ option.
226231
.Pp
227232
All the format specifier parts are optional except the
228233
.Ql <type> .
229-
Valid print types are:
234+
Valid types are:
230235
.Bl -column -offset indent "Type" "Lowercase hexadecimal" "Example"
231236
.It Sy Type Ta Sy Format Ta Sy Example
232237
.It Ql d Ta Signed decimal Ta -42
233-
.It Ql u Ta Unsigned decimal Ta 42
238+
.It Ql u Ta Unsigned decimal Ta 4294967254
234239
.It Ql x Ta Lowercase hexadecimal Ta 2a
235240
.It Ql X Ta Uppercase hexadecimal Ta 2A
236241
.It Ql b Ta Binary Ta 101010
@@ -266,9 +271,10 @@ would be more appropriate; see
266271
.Sx String expressions
267272
below.
268273
.Sh EXPRESSIONS
269-
An expression can be composed of many things.
274+
Expressions can be one of two types: either numeric or string.
275+
.Pp
270276
Numeric expressions are always evaluated using signed 32-bit math.
271-
Zero is considered to be the only "false" number, all non-zero numbers (including negative) are "true".
277+
In Boolean logic contexts, zero is considered to be the only "false" number, and all non-zero numbers (including negative) are "true".
272278
.Pp
273279
An expression is said to be "constant" if
274280
.Nm
@@ -280,18 +286,21 @@ However, some operators can be constant even with non-constant operands, as expl
280286
.Sx Operators
281287
below.
282288
.Pp
283-
The instructions in the macro-language generally require constant expressions.
284-
.Ss Numeric formats
285-
There are a number of numeric formats.
286-
.Bl -column -offset indent "Precise fixed-point" "Possible prefixes"
287-
.It Sy Format type Ta Sy Possible prefixes Ta Sy Accepted characters
289+
Directives generally require constant expressions: for example,
290+
.Ic REPT
291+
requires the number of repetitions to be known at assembly time.
292+
.Ss Numeric literals
293+
.Nm
294+
supports a variety of numeric literals.
295+
.Bl -column -offset indent "Precise fixed-point" "Prefixes" "Accepted characters"
296+
.It Sy Format type Ta Sy Prefixes Ta Sy Accepted characters
288297
.It Decimal Ta none Ta 0123456789
289298
.It Hexadecimal Ta Li $ , 0x , 0X Ta 0123456789ABCDEF
290299
.It Octal Ta Li & , 0o , 0O Ta 01234567
291300
.It Binary Ta Li % , 0b , 0B Ta 01
292301
.It Fixed-point Ta none Ta 01234.56789
293302
.It Precise fixed-point Ta none Ta 12.34q8
294-
.It Character constant Ta none Ta 'ABYZ'
303+
.It Character constant Ta none Ta 'A'
295304
.It Game Boy graphics Ta Li \` Ta 0123
296305
.El
297306
.Pp
@@ -313,24 +322,23 @@ for information on charmaps, and
313322
.Sx String expressions
314323
for information on escape characters allowed in character constants.
315324
.Pp
316-
The last one, Game Boy graphics, is quite interesting and useful.
317-
After the backtick, 8 digits between 0 and 3 are expected, corresponding to pixel values.
318-
The resulting value is the two bytes of tile data that would produce that row of pixels.
325+
The last one, Game Boy graphics, expects up to eight digits between 0 and 3, corresponding to pixels' two-bit shade values.
326+
The resulting numeric value is the two bytes of tile data which would produce that row of pixels.
319327
For example,
320328
.Sq \`01012323
321329
is equivalent to
322330
.Sq $0F55 .
323331
.Pp
324-
You can also use symbols, which are implicitly replaced with their value.
332+
In place of a numeric literal, you can also use a numeric symbol's name, which is implicitly replaced with its value.
325333
.Ss Operators
326334
You can use these operators in numeric expressions (listed from highest to lowest precedence):
327335
.Bl -column -offset indent "!= == <= >= < >"
328336
.It Sy Operator Ta Sy Meaning
329337
.It Li \&( \&) Ta Grouping
330338
.It Li FUNC() Ta Built-in function call
331339
.It Li ** Ta Exponentiation
332-
.It Li + - ~ \&! Ta Unary plus, minus (negation), complement (bitwise negation), and Boolean negation
333-
.It Li * / % Ta Multiplication, division, and modulo (remainder)
340+
.It Li + - ~ \&! Ta Unary plus, unary minus (negation), complement (bitwise negation), and Boolean negation
341+
.It Li * / % Ta Multiplication, division (rounding down), and modulo (remainder)
334342
.It Li << >> >>> Ta Bit shifts (left, sign-extended right, zero-extended right)
335343
.It Li & \&| ^ Ta Bitwise AND/OR/XOR
336344
.It Li + - Ta Addition and subtraction
@@ -398,7 +406,7 @@ with a non-zero constant as either operand will be constant 1, even if the other
398406
returns 1 if the operand was 0, and 0 otherwise.
399407
Even a non-constant operand with any non-zero bits will return 0.
400408
.Ss Integer functions
401-
Besides operators, there are also some functions which have more specialized uses.
409+
Besides operators, there are also some functions which have more specialized uses:
402410
.Bl -column "BITWIDTH(n)"
403411
.It Sy Name Ta Sy Operation
404412
.It Fn HIGH n Ta Equivalent to Ql Po Ns Ar n No & $FF00 Pc >> 8 .
@@ -410,12 +418,13 @@ delim $$
410418
.Ar n .
411419
Some useful formulas:
412420
.Ic BITWIDTH Ns ( Ar n Ns )\ \-\ 1
413-
equals $\[lf] log sub 2 ( n ) \[rf]$,
421+
equals $\[lf] log sub 2 ( n ) \[rf]$;
414422
.Ic BITWIDTH Ns Pq Ar n Ns \ \-\ 1
415-
equals $\[lc] log sub 2 ( n ) \[rc]$, and
423+
equals $\[lc] log sub 2 ( n ) \[rc]$; and
416424
.No 32\ \-\ Ns Ic BITWIDTH Ns Pq Ar n
417-
equals $roman clz ( n )$.
418-
.It Fn TZCOUNT n Ta Returns $roman ctz ( n )$, the count of trailing zero bits at the end of the binary representation of
425+
equals $roman clz ( n )$, the count of leading zero bits in the binary representation of
426+
.Ar n .
427+
.It Fn TZCOUNT n Ta Returns $roman ctz ( n )$, the count of trailing zero bits in the binary representation of
419428
.Ar n .
420429
.El
421430
.EQ
@@ -436,29 +445,33 @@ command-line option, and/or by
436445
An individual fixed-point literal can specify its own precision, overriding the current default, by appending a
437446
.Dq q
438447
followed by the number of fractional bits: for example,
439-
.Ql 1234.5q8
440-
is equal to $0004d2_80
448+
.Ql 789.25q8
449+
is equal to $000315_40
441450
.EQ
442451
delim $$
443452
.EN
444-
($= 1234.5 * 2 sup 8$).
453+
($= 789.25 * 2 sup 8$).
445454
.Pp
446455
Since fixed-point values are still just integers, you can use them in normal integer expressions.
447-
You can easily truncate a fixed-point number into an integer by shifting it right by the number of fractional bits.
448-
It follows that you can convert an integer to a fixed-point number by shifting it left that same amount.
456+
You can easily truncate a fixed-point number into an integer by shifting it right by the number of fractional bits, or by dividing it by 1.0.
457+
It follows that you can convert an integer to a fixed-point number by shifting it left that same amount, or by multiplying it by 1.0.
458+
For example,
459+
.Ql 123.0 / 1.0 == 123 ,
460+
and
461+
.Ql 123 * 1.0 == 123.0 .
449462
.Pp
450463
Note that the current number of fractional bits can be computed as
451464
.Ic TZCOUNT Ns Pq 1.0 .
452465
.Pp
453-
The following functions are designed to operate with fixed-point numbers:
466+
The following functions are designed to operate with fixed-point numbers (which must be known constant):
454467
.Bl -column -offset indent "ATAN2(y, x)"
455468
.It Sy Name Ta Sy Operation
456469
.It Fn DIV x y Ta Fixed-point division
457470
.It Fn MUL x y Ta Fixed-point multiplication
458471
.It Fn FMOD x y Ta Fixed-point modulo
459472
.It Fn POW x y Ta $x sup y$
460473
.It Fn LOG x y Ta Logarithm of $x$ to the base $y$
461-
.It Fn ROUND x Ta Round $x$ to the nearest integer
474+
.It Fn ROUND x Ta Round $x$ half away from zero to the nearest integer
462475
.It Fn CEIL x Ta Round $x$ up to the nearest integer
463476
.It Fn FLOOR x Ta Round $x$ down to the nearest integer
464477
.It Fn SIN x Ta Sine of $x$
@@ -509,9 +522,7 @@ will produce a nonsensical (but technically correct) result:
509522
The
510523
.Ic FMOD
511524
function
512-
is used to get the remainder of the corresponding fixed-point division, so that
513-
.Ql MUL(DIV(x, y), y) + FMOD(x, y) == x
514-
is always true.
525+
is used to get the remainder of the corresponding fixed-point division.
515526
The result has the same sign as the
516527
.Em dividend ;
517528
this is the opposite of how the integer modulo operator
@@ -534,14 +545,15 @@ These functions are useful for automatic generation of various tables.
534545
For example:
535546
.Bd -literal -offset indent
536547
; Generate a table of 128 sine values
537-
; from sin(0.0) to sin(0.5) excluded,
538-
; with amplitude scaled from [-1.0, 1.0] to [0.0, 128.0].
548+
; from sin(0.0) included to sin(0.5) excluded,
549+
; with amplitude scaled from [-1.0, 1.0] to [0.0, 128.0],
550+
; then divided by 1.0 to round down to integer values.
539551
FOR angle, 0.0, 0.5, 0.5 / 128
540-
db MUL(SIN(angle) + 1.0, 128.0 / 2) >> 16
552+
db MUL(SIN(angle) + 1.0, 128.0 / 2) / 1.0
541553
ENDR
542554
.Ed
543555
.Ss String expressions
544-
The most basic string expression is any number of characters contained in double quotes
556+
The most basic string expression is a string literal: any number of characters contained in double quotes
545557
.Pq Ql \&"for instance" .
546558
The backslash character
547559
.Ql \e
@@ -562,14 +574,14 @@ There are a number of escape sequences you can use within a string:
562574
.It Ql \e0 Ta Null Pq ASCII $00
563575
.El
564576
.Pp
565-
Multi-line strings are contained in triple quotes
577+
Multi-line string literals are contained in triple quotes
566578
.Pq Ql \&"\&"\&"for instance""" .
567579
Escape sequences work the same way in multi-line strings; however, literal newline characters will be included as-is, without needing to escape them with
568580
.Ql \er
569581
or
570582
.Ql \en .
571583
.Pp
572-
Raw strings are prefixed by a hash
584+
Raw string literals are prefixed by a hash
573585
.Sq # .
574586
Inside them, backslashes and braces are treated like regular characters, so they will not be expanded as macro arguments, interpolated symbols, or escape sequences.
575587
For example, the raw string
@@ -601,7 +613,7 @@ and
601613
is equivalent to
602614
.Ql STRCMP("str", \&"ing") != 0 .
603615
.Pp
604-
The following functions operate on string expressions, and return strings themselves.
616+
The following functions operate on string expressions, and return strings themselves:
605617
.Bl -column "STRSLICE(str, start, stop)"
606618
.It Sy Name Ta Sy Operation
607619
.It Fn STRCAT strs... Ta Concatenates Ar strs .
@@ -612,7 +624,7 @@ in uppercase.
612624
.Pq Ql A-Z
613625
in lowercase.
614626
.It Fn STRSLICE str start stop Ta Returns a substring of Ar str No starting at Ar start No and ending at Ar stop No (exclusive). If Ar stop No is not specified, the substring continues to the end of Ar str .
615-
.It Fn STRRPL str old new Ta Returns Ar str No with each non-overlapping occurrence of the substring Ar old No replaced with Ar new .
627+
.It Fn STRRPL str old new Ta Returns Ar str No with each occurrence of the substring Ar old No replaced with Ar new .
616628
.It Fn STRFMT fmt args... Ta Returns the string Ar fmt No with each
617629
.Ql %spec
618630
pattern replaced by interpolating the format
@@ -622,11 +634,15 @@ with its corresponding argument in
622634
.Ar args
623635
.Pq So %% Sc is replaced by the So % Sc character .
624636
.It Fn STRCHAR str idx Ta Returns the substring of Ar str No for the charmap entry at Ar idx No with the current charmap . Pq Ar idx No counts charmap entries, not characters.
637+
.El
638+
.Pp
639+
The following functions take varying operands, and return strings:
640+
.Bl -column "READFILE(name, max)"
625641
.It Fn REVCHAR vals... Ta Returns the string that is mapped to Ar vals No with the current charmap. If there is no unique charmap entry for Ar vals Ns , an error occurs.
626642
.It Fn READFILE name max Ta Returns the contents of the file Ar name No as a string. Reads up to Ar max No bytes, or the entire contents if Ar max No is not specified. If the file isn't found in the current directory, the include-path list passed to Xr rgbasm 1 Ap s Fl I No option on the command line will be searched.
627643
.El
628644
.Pp
629-
The following functions operate on string expressions, but return integers.
645+
The following functions operate on string expressions, but return integers:
630646
.Bl -column "STRRFIND(str, sub)"
631647
.It Sy Name Ta Sy Operation
632648
.It Fn STRLEN str Ta Returns the number of characters in Ar str .
@@ -670,7 +686,8 @@ and
670686
being equivalent to
671687
.Ql dw 50, 53, $20ac .
672688
.Pp
673-
Any characters in a string without defined mappings will be copied directly, using the source file's encoding of characters to bytes.
689+
Character mappings are matched greedily, so the longest applicable one will be mapped in a string.
690+
Any characters in the string without defined mappings will be copied directly, using the source file's encoding of characters to bytes.
674691
.Pp
675692
It is possible to create multiple character maps and then switch between them as desired.
676693
This can be used to encode debug information in ASCII and use a different encoding for other purposes, for example.

0 commit comments

Comments
 (0)