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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# nesasm CE v3.1 - a 6502 assembler with specific NES support
Just another modification of nesasm. Based on modification by Tim Hentenaar which is based on modification by Bob Rost which is based on modification of nesasm 2.51 from MagicKit.
## What's new in this modification?
* Support for much longer filenames and labels
* Automatic generation of symbol files for FCEUX debugger
* GNU/POSIX style command line options
* It's possible to define all output filenames now
* Code cleanup: all warnings are fixed, PCE code leftovers removed
## Usage
Usage: nesasm [OPTION...] <source.asm>
-f, --symbols[=<prefix>] Create FCEUX symbol files
-F, --symbols-offset=<offset> Bank offset for FCEUX symbol files
-i, --listing Force listing
-l, --listing-level=# Listing file output level (0-3)
-L, --listing-file=<file.lst> Name of the listing file
-m, --macro-expansion Force macro expansion in listing
-o, --output=<file.nes> Name of the output file
-r, --raw Prevent adding a ROM header
-s, --segment-usage Show (more) segment usage
-W, --warnings Show overflow warnings
-?, --help give this help list
--usage give a short usage message
-V, --version print program version
The assembler accepts only one input file 'infile' that will be assembled into ROM file (.NES extension) directly useable by an emulator.
A listing file can also be generated (.LST extension) if the LIST directive is encountered in the input file.
Here's a description of the different options:
Option Description
------ -----------
-o <file.nes> Set output filename.
The default is input filename + ".nes" extension.
-f [prefix] Enable generation of symbol files for FCEUX debugger,
optionally you can specify filenames prefix.
-F [offset] Set bank offset for FCEUX symbol files.
-L <file.lst> Set listing filename.
The default is output filename + ".lst" extension.
-l # Control output of the listing file:
0 - disable completely the listing file even if the
LIST directive is used in the input file
1 - minimun level; code produced by DB, DW and DEFCHR
will not be dumped
2 - normal level; only code produced by DEFCHR will not
be dumped
3 - maximun level; all the code is dumped in the
listing file
The default level is level 2.
-s Show segment usage. If one of those options is specified
the assembler will display information on the ROM bank
usage. Use '-s' to show basic information and '-ss' to
show more detailed information.
-i Force listing file writing, even if the
LIST directive is not seen in the input file.
-m Force macros expansion in the listing file, even if the
MLIST directive is not seen in the input file.
-r Control the header generation. By default the assembler
always adds an header to the ROM file; unless '-raw' is
specified, in this case no ROM header is generated.
-W Show warnings on bank overflow when using .inc* directives.
### Include path
By default the assembler looks in the current directory when loading an include file, but when it doesn't find the file it then uses the environment variable 'NES_INCLUDE' to get a list of include paths.
### Symbols
Two types of symbol are supported, global symbols and local symbols. Local symbols are preceded by a dot '.' and are valid only between two global symbols. A symbol can be followed by a colon ':' but this is not necessary.
### Expressions
The assembler supports very complex expressions. You can use as many level of parenthesis as you want and spaces between operators and numbers are possible.
Numbers can be written in three bases : hexadecimal ($7F), binary (%0101) and decimal (48). Character values are also supported ('A').
All the usual operators are present :
+, -, *, /, %, ^, &, |, ~, <<, >>
As well as the comparison operators :
=, !=, !, <, >, <=, >=
For the priority, the same rules as C apply.
You can also use predefined or user-defined functions in an expression.
### Predefined functions
* HIGH() - Returns the high byte of a value.
* LOW() - Returns the low byte.
* BANK() - Returns the bank index of a symbol. If no symbol, or more than one, are given, the function will return an error.
* PAGE() - Returns the page index of a label. See above for errors.
* SIZEOF() - Returns the size of a data element.
### User-defined functions
User-defined functions are declared with the .FUNC directive, for example:
SCR_ADDR .func (\1) + ((\2) << 5)
Up to nine arguments, \1 to \9, can be used.
To call a function simply enclose arguments within parenthesis and separate them with a comma:
stw #SCR_ADDR(10,4)+$2000,<$20
User-defined functions can be very useful, one often needs to use the same calculation again and again in expressions. Defining a function will save you a lot of work, and reduce typo errors. :) Note that function calls can be nested, you can call one function from another without any problem, however, recursive calls will produce an error.
### Macros
While functions are very useful to replace common expressions by just a function call, macros are used to replace common groups of instructions by a single line of code.
You start a macro definition with:
label .macro
Or you can also place the label after the '.macro' keyword, like this:
.macro label
After follow the body of the macro, which is terminated by the '.endm' directive.
As an example let's define a 'neg' macro to negate the accumulator.
neg .macro
eor #$FF
inc A
.endm
Macros can also have parameters. In the macro body, you refer to a parameter by using the backslash character ('\') followed by a digit. Nine parameters can be used, \1 to \9.
Here's another example:
add .macro ; add a value to register A
clc ; (handle carry flag)
adc \1+1
.endm
Other 'special' parameters can be used, here's a list of all the possible parameter you can use inside a macro:
Parameter Description
--------- -----------
\1 - \9 Input parameter - up to nine can be used in a macro call
\# Number of input parameters
\?1 - \?9 Returns 'type' of input parameter:
ARG_NONE (= 0) = No argument
ARG_REG (= 1) = register -> A, X, Y
ARG_IMMEDIATE (= 2) = Immediate data type -> #xx
ARG_ABSOLUTE (= 3) = Abosulte addressing -> label, $xxxx
ARG_INDIRECT (= 4) = Indirect addressing -> [label]
ARG_STRING (= 5) = String argument -> "..."
ARG_LABEL (= 6) = Label argument -> label
\@ Special parameter that returns a different number for
each macro; can be used to define local symbols inside
macros:
abs .macro
lda \1
bpl .x\@
eor #$FF
inc A
sta \1
.x\@:
.endm
### Directives
LIST - Enable the listing file generation. You can later stop
temporarily the output with the NOLIST directive and
restart it again with LIST.
NOLIST - Stop the listing output.
MLIST - Allow macro expansion in the listing file.
NOMLIST - Stop expanding macros in the listing file. This directive
won't have any effect if you use the '-m' command line
option.
EQU - Assign a value to a symbol. The character '=' has
the same function too.
BANK - Select a 8KB ROM bank (0-127) and reset the location
counter to the latest known position in this bank.
ORG - Set the location of the program counter. The thirteen
lower bits of the address inform the assembler about
the offset in the ROM bank and the third upper bits
represent the page index.
DB - Store one or more data bytes at the current location.
DW - Store data words.
BYTE - Same as DB.
WORD - Same as DW.
DS - Reserve space at the current location. This space will
be filled with zeroes if this directive is used in the
CODE or DATA group.
RSSET - Set the internal counter of the RS directive to
a specified value.
RS - Assign a value to a symbol; a bit like EQU but here
the value assigned is taken from an internal counter,
and after the assignation this counter is increased
by the amount specified in the RS directive.
This is a very handy way of defining structure member
offsets, here's a small example:
; C:
; --
; struct {
; short p_x;
; short p_y;
; byte p_color;
; } pixel;
;
; ASM:
; ----
.rsset $0 ; set the initial value of RS counter
P_X .rs 2
P_Y .rs 2
P_COLOR .rs 1
You can later use these symbols as offsets in a 'pixel'
struct:
ldy #P_COLOR
lda [pixel_ptr],Y
MACRO - Start a macro definition.
ENDM - End a macro definition.
INCBIN - Include a binary file at the current location. If the file
is bigger than a ROM bank, as many successive banks as
necessary will be used.
INCLUDE - Include a source file at the current location.
Up to 7 levels are possible.
DEFCHR - Define a character tile (8x8 pixels). The directive takes
8 arguments (stored as 32-bit values of 8 nybbles each),
one argument for each row of pixel data. This directive
takes also care to reorganize the pixel data to the NES
required bit format. Note that only color indexes 0 to 3
can be used, as the NES tiles are only 4-color. An error
will be generated if you try to use more colors.
zero: .defchr $00111110,\
$01000011,\
$01000101,\
$01001001,\
$01010001,\
$01100001,\
$00111110,\
$00000000
ZP - Select the Zero-Page section ($0000-$00FF).
BSS - Select the RAM section ($0200-$07FF).
CODE - Select the program code section.
DATA - Select the program data section.
Note: In ZP and BSS sections you can only allocate storage,
---- you can *not* store initial values.
IF - Conditional assembly directive. This directive will evaluate
the supplied expression and then turn conditional assembly
on or off depending on the result. If the result is null
conditional assembly is turned off, and on if the result is
non null.
IFDEF
IFNDEF - These directives allow conditional assembly depending on
whether a label is defined or not.
ELSE - Toggle conditional assembly on to off, or vice verca.
ENDIF - Terminate the current level of conditional assembly.
Report an error if the number of IF's and ENDIF's doesn't
match.
FAIL - When the assembler encounters this directive, it aborts
the compilation. Can be used within a macro for argument
error detection.
INESPRG - Specifies the number of 16k prg banks.
INESCHR - Specifies the number of 8k chr banks.
INESMAP - Specifies the NES mapper used.
INESMIR - Specifies VRAM mirroring of the banks. Refer to iNES header
document (neshdr20.txt).
|