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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
# nesasm CE v3.6 - 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 which is based on 6502 assembler by J. H. Van Ornum.
## What's new in this modification?
* Support for much longer filenames and labels
* Automatic generation of symbol files for FCEUX debugger
* NES 2.0 support with very large files support, mappers up to 4095, submappers, etc.
* Predefined NES specific constants: PPU/APU registers
* GNU/POSIX style command line options
* It's possible to define all output filenames now
* Option to assign a value to a symbol from command line
* Code cleanup: all warnings are fixed, PCE code leftovers removed
## Usage
Usage: nesasm [OPTION...] <source.asm>
-C, --sequ=<name>=<value> Assign a string value to a symbol
-D, --equ=<name>=<value> Assign an integer value to a symbol
-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, use '-' for stdout
-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.
Use '-' for stdout output.
-D <name>=<value> Assign an integer value to a symbol.
Example: -D delay=10
It will be equal to: delay .equ 10
at the beginning of your code, also you can use '$' and '%'
prefixes for hexadecimal and binary values.
-C <name>=<value> Assign a string value to a symbol.
Example: -C image_file=image.bin
It will be equal to: image_file .sequ "image.bin"
at the beginning of your code.
-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.
### Predefined constants
There are predefines NES register addresses:
* PPUCTRL and PPU_CTRL - $2000
* PPUMASK and PPU_MASK - $2001
* PPUSTATUS and PPU_STATUS - $2002
* OAMADDR and OAM_ADDR - $2003
* OAMDATA and OAM_DATA - $2004
* PPUSCROLL and PPU_SCROLL - $2005
* PPUADDR and PPU_ADDR - $2006
* PPUDATA and PPU_DATA - $2007
* OAMDMA and OAM_DMA - $4014
* SQ1VOL and SQ1_VOL - $4000
* SQ1SWEEP and SQ1_SWEEP - $4001
* SQ1LO and SQ1_LO - $4002
* SQ1HI and SQ1_HI - $4003
* SQ2VOL and SQ2_VOL - $4004
* SQ2SWEEP and SQ2_SWEEP - $4005
* SQ2LO and SQ2_LO - $4006
* SQ2HI and SQ2_HI - $4007
* TRILINEAR and TRI_LINEAR - $4008
* TRILO and TRI_LO - $400A
* TRIHI and TRI_HI - $400B
* NOISEVOL and NOISE_VOL - $400C
* NOISELO and NOISE_LO - $400E
* NOISEHI and NOISE_HI - $400F
* DMCFREQ and DMC_FREQ - $4010
* DMCRAW and DMC_RAW - $4011
* DMCSTART and DMC_START - $4012
* DMCLEN and DMC_LEN - $4013
* APUSTATUS and APU_STATUS - $4015
* JOY1 - $4016
* JOY2 and JOY2_FRAME - $4017
### 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 an integer value to a symbol. The character '=' has
the same function too.
SEQU - Assign a string value to a symbol.
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.
STR - Stores a string, the first byte is the length of the string,
followed by the string content,
The effect is equivalent to . DB is preceded with a length,
here's a small example:
;use DB specified a length + string:
DB 12,"Hello World!"
;can be replaced with STR:
STR "Hello World!"
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 or just PRG size if it > $EFF.
INESCHR - Specifies the number of 8k CHR banks or just CHR size if it > $EFF.
INESMAP - Specifies the NES mapper used, up to 4095.
INESSUBMAP - Specifies the NES submapper used, up to 15.
INESMIR - Specifies VRAM mirroring of the banks.
0: Horizontal or mapper-controlled, 1: Vertical, 2: Hard-wired four-screen
INESPRGRAM - Specifies the size of PRG RAM.
INESPRGNVRAM - Specifies the size of PRG NVRAM (non-volatile).
INESCHRRAM - Specifies the size of CHR RAM.
INESCHRNVRAM - Specifies the size of CHR NVRAM (non-volatile).
INESBAT - Specifies "battery" and other non-volatile memory
0: Not present, 1: Present
INESTIM - Specifies CPU/PPU timing
0: RP2C02 ("NTSC NES"), 1: RP2C07 ("Licensed PAL NES"), 2: Multiple-region, 3: UMC 6527P ("Dendy")
## How to build
### Linux
* Just run `make all` from `sources` directory
### Windows
* Install [MSYS2](https://www.msys2.org/)
* Install *base-devel*, *gcc*, *git* and *libargp-devel* packages using command: `pacman -S base-devel gcc git libargp-devel`
* Run `make all` from `sources` directory
### MacOS
* Install *argp-standalone* package using command: `brew install argp-standalone`
* Run `make all` from `sources` directory
## Contacts
https://clusterrr.com
clusterrr@clusterrr.com
[![C/C++ CI](https://github.com/ClusterM/nesasm/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/ClusterM/nesasm/actions/workflows/c-cpp.yml)
|