diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..66f7e10a --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +rgbasm +rgblink +rgbfix +rgbgfx +*.o +*.exe +*.html diff --git a/GNUmakefile b/GNUmakefile new file mode 100644 index 00000000..9650e67f --- /dev/null +++ b/GNUmakefile @@ -0,0 +1,6 @@ +# GNU Make 3.x doesn't support the "!=" shell syntax, so here's an alternative + +PKG_CONFIG = pkg-config +PNGFLAGS = $(shell ${PKG_CONFIG} --cflags libpng) + +include Makefile diff --git a/LICENSE b/LICENSE index 8304b668..a8d669a3 100644 --- a/LICENSE +++ b/LICENSE @@ -18,6 +18,12 @@ released under the following license: rgbfix was rewritten from scratch by Anthony J. Bentley, and is released under the ISC license; see the source file for the text of the license. +rgbgfx was written by stag019, and is released under the ISC license. + +The UTF-8 decoder in src/asm/charmap.c was written by Björn Höhrmann and is +released under the MIT license. The remainder of charmap.c was written by +stag019, and is released under the ISC license. + extern/err.c is derived from the Musl C library, http://www.musl-libc.org, and is released under the MIT license. diff --git a/Makefile b/Makefile index b84a311e..5f3be4aa 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ -.POSIX: - +PKG_CONFIG = pkg-config WARNFLAGS = -Wall -Werror=implicit -REALCFLAGS = ${CFLAGS} ${WARNFLAGS} -Iinclude -g \ +PNGFLAGS != ${PKG_CONFIG} --cflags libpng +REALCFLAGS = ${CFLAGS} ${WARNFLAGS} ${PNGFLAGS} -Iinclude -g \ -std=c99 -D_POSIX_C_SOURCE=200809L # User-defined variables @@ -42,13 +42,20 @@ rgbfix_obj = \ src/fix/main.o \ src/extern/err.o -all: rgbasm rgblink rgbfix +rgbgfx_obj = \ + src/gfx/gb.o \ + src/gfx/main.o \ + src/gfx/makepng.o \ + src/extern/err.o + +all: rgbasm rgblink rgbfix rgbgfx clean: $Qrm -rf rgbds.html $Qrm -rf rgbasm rgbasm.exe ${rgbasm_obj} rgbasm.html $Qrm -rf rgblink rgblink.exe ${rgblink_obj} rgblink.html $Qrm -rf rgbfix rgbfix.exe ${rgbfix_obj} rgbfix.html + $Qrm -rf rgbgfx rgbgfx.exe ${rgbgfx_obj} rgbgfx.html $Qrm -rf src/asm/asmy.c src/asm/asmy.h install: all @@ -56,11 +63,13 @@ install: all $Qinstall -s -m 555 rgbasm ${BINPREFIX}/rgbasm $Qinstall -s -m 555 rgbfix ${BINPREFIX}/rgbfix $Qinstall -s -m 555 rgblink ${BINPREFIX}/rgblink + $Qinstall -s -m 555 rgbgfx ${BINPREFIX}/rgbgfx $Qmkdir -p ${MANPREFIX}/man1 ${MANPREFIX}/man7 $Qinstall -m 444 src/rgbds.7 ${MANPREFIX}/man7/rgbds.7 $Qinstall -m 444 src/asm/rgbasm.1 ${MANPREFIX}/man1/rgbasm.1 $Qinstall -m 444 src/fix/rgbfix.1 ${MANPREFIX}/man1/rgbfix.1 $Qinstall -m 444 src/link/rgblink.1 ${MANPREFIX}/man1/rgblink.1 + $Qinstall -m 444 src/gfx/rgbgfx.1 ${MANPREFIX}/man1/rgbgfx.1 rgbasm: ${rgbasm_obj} $Q${CC} ${REALCFLAGS} -o $@ ${rgbasm_obj} -lm @@ -71,6 +80,9 @@ rgblink: ${rgblink_obj} rgbfix: ${rgbfix_obj} $Q${CC} ${REALCFLAGS} -o $@ ${rgbfix_obj} +rgbgfx: ${rgbgfx_obj} + $Q${CC} ${REALCFLAGS} -o $@ ${rgbgfx_obj} `${PKG_CONFIG} --libs libpng` + .y.c: $Q${YACC} -d ${YFLAGS} -o $@ $< @@ -91,6 +103,7 @@ mingw: $Qmv rgbasm rgbasm.exe $Qmv rgblink rgblink.exe $Qmv rgbfix rgbfix.exe + $Qmv rgbgfx rgbgfx.exe # Below is a target for the project maintainer to easily create web manuals. # It relies on mandoc: http://mdocml.bsd.lv @@ -105,3 +118,5 @@ wwwman: rgbfix.html $Qmandoc ${MANDOC} src/link/rgblink.1 | sed s/OpenBSD/General/ > \ rgblink.html + $Qmandoc ${MANDOC} src/gfx/rgbgfx.1 | sed s/OpenBSD/General/ > \ + rgbgfx.html diff --git a/README b/README.md similarity index 51% rename from README rename to README.md index 9917c619..304cefce 100644 --- a/README +++ b/README.md @@ -1,6 +1,4 @@ ----------------- - RGBDS README ----------------- +# RGBDS RGBDS (Rednex Game Boy Development System) is a free assembler/linker package for the Game Boy and Game Boy Color. It consists of: @@ -8,50 +6,64 @@ for the Game Boy and Game Boy Color. It consists of: - rgbasm (assembler) - rgblink (linker) - rgbfix (checksum/header fixer) + - rgbgfx (PNG‐to‐Game Boy graphics converter) rgbds-linux is a fork of the original RGBDS which aims to make the programs more like other UNIX tools. - Installing RGBDS (UNIX) -========================= +## Installing RGBDS (UNIX) + +RGBDS requires libpng and pkg-config to be installed. + +On Mac OS X, install them with [Homebrew](http://brew.sh/). On other Unixes, +use the built-in package manager. + +You can test if they're installed by running `pkg-config --cflags libpng`: +if the output is a path, then you're good, and if it outputs an error then +you need to install them via a package manager. To build the programs on a UNIX or UNIX-like system, just run in your terminal: - make +```sh +make +``` Then to install the compiled programs and manual pages, run (with appropriate privileges): - make install +```sh +make install +``` After installation, you can read the manuals with the man(1) command. E.g., - man 1 rgbasm - +```sh +man 1 rgbasm +``` Note: the variables described below can affect installation behavior when given on the make command line. For example, to install rgbds in your home directory instead of systemwide, run the following: - mkdir -p $HOME/{bin,man/man1,man/man7} - make install PREFIX=$HOME +```sh +mkdir -p $HOME/{bin,man/man1,man/man7} +make install PREFIX=$HOME +``` +`PREFIX`: Location where RGBDS will be installed. Defaults to /usr/local. -PREFIX: Location where RGBDS will be installed. Defaults to /usr/local. - -BINPREFIX: Location where the RGBDS programs will be installed. Defaults +`BINPREFIX`: Location where the RGBDS programs will be installed. Defaults to ${PREFIX}/bin. -MANPREFIX: Location where the RGBDS man pages will be installed. Defaults +`MANPREFIX`: Location where the RGBDS man pages will be installed. Defaults to ${PREFIX}/man. -Q: Whether to quiet the build or not. To make the build more verbose, clear +`Q`: Whether to quiet the build or not. To make the build more verbose, clear this variable. Defaults to @. - Installing RGBDS (Windows) -============================ +## Installing RGBDS (Windows) Windows builds are available here: https://github.com/bentley/rgbds/releases diff --git a/doc/asm.htm b/doc/asm.htm deleted file mode 100644 index 735dc01e..00000000 --- a/doc/asm.htm +++ /dev/null @@ -1,129 +0,0 @@ - - - - - xAsm - - - -

xAsm Documentation

-

Table of Contents

-

General stuff

- - -

Symbols

- - -

The macrolanguage

- - -

Other ways than mnemonics to define data

- - -

Target specific information

- - -

Alphabetical list of the macro-language instructions and functions

- - -

Last updated 20 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/db.htm b/doc/asm/db.htm deleted file mode 100644 index e4cfc3c1..00000000 --- a/doc/asm/db.htm +++ /dev/null @@ -1,26 +0,0 @@ - - - - - xAsm DB, DW - - - -

DB, DW

-

DB defines a list of bytes that will be stored in the final image. Ideal for tables and text.

-
DB   1,2,3,4,"This is a string"
-

Alternatively you can use DW to store a list of words. Strings are not allowed as arguments to DW.

-

You can also use DB and DW without arguments. This works exactly like “DS 1” and “DS 2” respectively. Consequently DB and DW can be used in a WRAM0/WRAMX/HRAM/VRAM/SRAM section.

-

See also:

- - -
-

Last updated 02 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/ds.htm b/doc/asm/ds.htm deleted file mode 100644 index 8ec5b22e..00000000 --- a/doc/asm/ds.htm +++ /dev/null @@ -1,20 +0,0 @@ - - - - - xAsm DS - - - -

DS

-

DS allocates a number of bytes. The content is undefined. This is the preferred method of allocationg space in a RAM section. You can however also use DB and DW without any arguments.

-
DS   str_SIZEOF     ;allocate str_SIZEOF bytes
-

See also:

- -
-

Last updated 02 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/equ.htm b/doc/asm/equ.htm deleted file mode 100644 index bd88e6d1..00000000 --- a/doc/asm/equ.htm +++ /dev/null @@ -1,21 +0,0 @@ - - - - - xAsm EQU - - - -

EQU

-

EQUates are constant symbols. They can for example be used for things such as bit-definitions of hardware-registers.

-
DONUT_ISGOOD   EQU  $01
-DONUT_ISBAD    EQU  $02
-

Note that a colon (:) following the label-name is not allowed. EQUates can be exported and imported. They don't change their value during the link process.

-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/equs.htm b/doc/asm/equs.htm deleted file mode 100644 index 5209bf34..00000000 --- a/doc/asm/equs.htm +++ /dev/null @@ -1,27 +0,0 @@ - - - - - xAsm EQUS - - - -

EQUS

-

EQUS is used to define string-symbols. Wherever the assembler meets a string symbol its name is replaced with its value. If you are familiar with C you can think of it as the same as #define.

-
COUNTREG EQUS "[hl+]"
-          
-         ld   a,COUNTREG
-

(Note that : following the label-name is not allowed.)

-

This will be interpreted as:

-
         ld   a,[hl+]
-

String-symbols can also be used to define small one-line macros:

-
PUSHA    EQUS "push af\npush bc\npush de\npush hl\n"
-

Note that a colon (:) following the label-name is not allowed. String equates can't be exported or imported.

-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/export.htm b/doc/asm/export.htm deleted file mode 100644 index 07a987f0..00000000 --- a/doc/asm/export.htm +++ /dev/null @@ -1,35 +0,0 @@ - - - - - xAsm EXPORT/XREF, IMPORT/XDEF, GLOBAL - - - -

EXPORT/XREF, IMPORT/XDEF, GLOBAL

-

Importing and exporting of symbols is a feature that is very useful when your project spans many source-files and for example you need to jump to a routine defined in another file.

- - - - - - - - - - - - - - - - - - - - -
Import/export commands
CommandMeaning
IMPORT (or XREF) label[,label,...]This instructs the assembler to define label as if it were present - in the current file but leave the address calculation to the linker.
EXPORT (or XDEF) label[,label,...]The assembler will make label accessible to other files during the link process.
GLOBAL label[,label,...]If label is defined during the assembly it will be exported, if not it will be imported. Handy (very!) for include-files.
-
-

Last updated 21 June 1997 by Carsten Sorensen

- diff --git a/doc/asm/expr_fix.htm b/doc/asm/expr_fix.htm deleted file mode 100644 index bede2bac..00000000 --- a/doc/asm/expr_fix.htm +++ /dev/null @@ -1,76 +0,0 @@ - - - - - xAsm Fixed-point expression - - - -

Fixed‐point Expressions

-

Fixed point constants are basically normal 32-bit constants where the upper 16 bits are used for the integer part and the lower 16 bits are used for the fraction (65536ths). This means that you can use them in normal integer expression and indeed some integer operators like plus and minus don't care whether the operands are integer or fixed-point. You can easily convert a fixed-point number to an integer by shifting it right 16 bits. It follows that you can convert an integer to a fixed-point number by shifting it left.

-

Some things are different for fixed-point math though. Which is why you have the following functions to use:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameOperation
DIV(x,y)x/y
MUL(x,y)x*y
SIN(x)sin(x)
COS(x)cos(x)
TAN(x)tan(x)
ASIN(x)sin-1(x)
ACOS(x)cos-1(x)
ATAN(x)tan-1(x)
ATAN2(x,y)(x,y) angle
-

These functions are extremely useful for automatic generation of various tables. A circle has 65536.0 degrees. Sine values are between [-1.0;1.0]

-
; --
-; -- Generate a 256 byte sine table with values between 0 and 128
-; --
-ANGLE   SET     0.0
-        REPT    256
-        DB      (MUL(64.0,SIN(ANGLE))+64.0)>>16
-ANGLE   SET     ANGLE+256.0
-        ENDR
-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/expr_int.htm b/doc/asm/expr_int.htm deleted file mode 100644 index 4e6579af..00000000 --- a/doc/asm/expr_int.htm +++ /dev/null @@ -1,92 +0,0 @@ - - - - - xAsm Integer/Boolean expressions - - - -

Integer and Boolean expressions

-

An expression can be composed of many things. Expressions are always evaluated using signed 32-bit math.

-

The most basic expression is just a single number.

-

Numeric Formats

-

xAsm has a number of numeric formats.

- -

The last one, Gameboy graphics, is quite interesting and useful. The values are actually pixel values and it converts the “chunky” data to “planar” data as used in the Gameboy.

-
DW   `01012323
-

Admittedly an expression with just a single number is quite boring. To spice things up a bit there’s a few operators you can use to perform calculations between numbers.

-

Operators

-

A great number of operators you can use in expressions are available (listed in order of precedence):

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Operators
OperatorMeaning
( )Precedence override
FUNC()Functioncall
~ + -Unary not/plus/minus
* / %Multiply/divide/modulo
<< >>Shift left/right
& | ^Binary and/or/xor
+ -Add/subtract
!= == <= >= < >Boolean comparison
&& ||Boolean and/or
!Unary Boolean not
- -

The result of the boolean operators is zero if when FALSE and non-zero when TRUE. Thus it is legal to use an integer as the condition for IF blocks. You can use symbols instead of numbers in your expression if you wish.

- -

An expression is said to be constant when it doesn't change its value during linking. This basically means that you can't use labels in those expressions. The instructions in the macro-language all require expressions that are constant.

-

See also:

- - -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/expr_str.htm b/doc/asm/expr_str.htm deleted file mode 100644 index d35abf2a..00000000 --- a/doc/asm/expr_str.htm +++ /dev/null @@ -1,114 +0,0 @@ - - - - - xAsm String expressions - - - -

String Expressions

-

The most basic string expression is any number of characters contained in double quotes ("for instance"). As in C the escape character is \ and there is a number of commands you can use within a string:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Escape characters:
Character sequenceMeaningNotes
\\Backslash
\"Double-quote
\{Curly bracket left
\}Curly bracket right
\nNewline ($0A)
\tTab ($09)
\1-\9MacroargumentOnly in macros
\@Labelname suffixOnly in macros and repts
- -

A funky feature is {symbol} withing a string. This will examine the type of the symbol and insert its value accordingly. If symbol is a string symbol the symbols value is simply copied. If it's a numeric symbol the value is converted to hexadecimal notation and inserted as a string.

-

HINT: The {symbol} construct can also be used outside strings. The symbols value is again inserted as a string. This is just a short way of doing "{symbol}".

-

Whenever the macro-language expects a string you can actually use a string expression. This consists of one or more of these function. Yes, you can nest them. Note that some of these functions actually return an integer and can be used as part of an integer expression!

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
String functions:
NameOperation
STRLEN(stringexpr)Returns the number of characters in string
STRCAT(stringexpr1,stringexpr2)Appends stringexpr2 to stringexpr1.
STRCMP(stringexpr1,stringexpr2)Returns negative if stringexpr1 is alphabetically less than stringexpr2
Zero if they match
Positive if greater than
STRIN(haystack,needle)Returns needles position within haystack or zero if it's not present
STRSUB(stringexpr,pos,count)Returns a substring of stringexpr starting at pos (first character is position 1) and with count characters
STRUPR(stringexpr)Converts all characters in string to capitals and returns the new string
STRLWR(string)Converts all characters in string to lower case and returns the new string
- -

See also:

- - -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/fail.htm b/doc/asm/fail.htm deleted file mode 100644 index 93a347e7..00000000 --- a/doc/asm/fail.htm +++ /dev/null @@ -1,20 +0,0 @@ - - - - - xAsm FAIL, WARN - - - -

FAIL, WARN

-

FAIL and WARN can be used to terminate the assembling process if you wish to do so. This is especially useful for macros that get an invalid argument. FAIL and WARN take a string as the only argument and they will print this string out as a normal error with a linenumber.

-

FAIL stops assembling immediately while WARN continues after printing the errormessage.

- -

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/history.htm b/doc/asm/history.htm deleted file mode 100644 index ecfde119..00000000 --- a/doc/asm/history.htm +++ /dev/null @@ -1,224 +0,0 @@ - - - - - xAsm History - - - -

xAsm History

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The history of xAsm
VersionDatedRelease notes
1.01 Oct. 96First release
1.011 Dec. 96
    -
  • Fixed bug in INCBIN (sometimes reported the section full)
  • -
  • Added DEF() function -
1.0212 Feb. 97
1.0323 Mar. 97
    -
  • The HRAM section was 128 bytes long instead of 127. potentially thrashing the interrupt enable register if you filled the HRAM.
  • -
  • The BANK() function, when used on a symbol defined in the current sourcefile, returned the wrong bank ID. (reported by Harry P. Mulder)
  • -
  • The BANK() function didn't check whether the argument was a properly defined symbol. (reported by Harry P. Mulder)
  • -
  • Completely new lexical analyser module. This fixed several linenumber bugs and other macro/if/rept related bugs. Also fixed a bug which made it possible to have equated symbols with the same name as a reserved keyword (if you get a "parse error" with this release on some of your sources, this is probably what is going on)
  • -
  • FAIL and WARN commands.
  • -
  • __LINE__, __FILE__, __TIME__, __DATE__ predefined symbols added.
  • -
-
1.0403 July 1997
  • First ASMotor release
  • -
  • __TIME__ and __DATE__ give todays date instead of when the assembler was compiled.
  • -
  • Sometimes the first line of a file wouldn't assemble correctly. Reported by Jeff Frohwein.
  • -
  • Unrolling multiline string symbols left the linecounter in a sorry state. Jeff Frohwein again.
  • -
  • DB and DW can now (officially ;-) be used in BSS/HRAM/VRAM sections without any arguments to reserve a byte or a word respectively. Reported/suggested/inspired by Mr. Frohwein.
  • -
  • The character # can now be used as part of a symbol name. Jeff....
  • -
  • The RS counter "_RS" is now defined from the very start of the assembly process instead of after the first RSSET or RSRESET.
  • -
  • Bug fixed: You couldn't use \0-\9 and \@ in {} constructs
  • -
  • PURGE pseudo-op added. Purges a symbol from the symboltable and memory. Use with extreme caution! Inspired by Harry P. Mulder
  • -
  • MACRO parameter passing method changed drastically. Read (and re-read) the manual for details. Suggested by Harry P. Mulder.
  • -
-
1.0520 July 1997RGBDS fixes:
- RGBAsm supports the LDD and LDI syntax plus [HLD] and [HLI]. LDH is - synonymous with LDIO.
- General fixes:
-There was a bug in the macro parameter passing. Any whitespace after the -last parameter would be appended to the last parameter. Reported by Jeff Frohwein.
-A section stack has been implemented. Look up POPS and PUSHS. Jeff Frohweins doing again.
-OPT command added for defining and changing some options while assembling.
-You can now define which characters are used for the Gameboy graphics -integer (`) using the commandline or the new OPT command. Cool idea by (surprise surprise) Jeff Frohwein.
-Also, an option stack has been added. Look up POPO and PUSHO in the -manual.
-Fixed yet another line number bug reported by Jeff Frohwein (when will this guy leave me alone? ;)
-
1.0622 July 1997General fixes:
-The lamest typo bug of all time has been fixed. RGBAsm would output a word defined with DW as 4 bytes instead of 2. Jeff Frohwein reported this.
-The first line of an included file didn't assemble correctly.
--b option added for setting the characters used for binary constants.
-
1.0821 September 1997General fixes:
-A crash occured if you tried to use a macro symbol in an expression. -(Jeff Frohwein)
-You couldn't use STRCMP, STRLEN and STRIN in relocatable expressions. (Harry -P. Mulder)
-Relocatable symbols are no longer allowed as arguments to the DEF function.
-Bug fixed in the assembler where it would sometimes write out too many bytes -for HRAM section definitions.
-
1.0802 July 1999 - Feature: -
- DQ directive added for defining 32-bit data constants. See operation of DW & DB. -
-
1.0805 July 1999 - Feature: -
- Allow only a part of a binary file to be included instead of the whole thing. -
-
1.0810 June 1999 - Feature: -
- Added output of file dependency information for each file included/assembled. Enabled with a command line option. -
-
1.08?? ???? 1999 - Feature: -
- Added ORG directive to allow anonymous sections. -
-
1.08?? ???? 1999 - Feature: -
- Added ability to output error information in either RGBDS or Microsoft Developer Studio format. -
-
1.08?? ???? 1999 - Feature: -
- Added pseudo-instructions to handle NE (not equal), EQ (equal), and LT (less than) on JR/JP/CALL instructions -
-
1.08?? ???? 1999 - Feature: -
- Added STRTRIM, STRLTRIM, STRRTRIM directives to allow trimming of white space from strings in macro arguments. -
-
1.08?? ???? 1999 - Bug Fix: -
- When an "unknown symbol" error was reported during the link phase the undefined symbol was not given. -
-
1.08?? ???? 1999 - Bug Fix: -
- Declaring a symbol as GLOBAL in a header file and then referencing it in code but never defining it would crash the linker. -
-
1.0908 February 2000 - Feature: -
- Can now use a command line option to set the number format between a slightly tweaked Motorola/RGBDS format and Zilog. -
- Hex numbers can now be represented as $FF or FFh. -
- Octal as &77 or 77o. -
- Binary as %10010110 or 10010110b. -
- Gameboy graphics numbers as `1001 or 1001g. -
- Decimal numbers remain unchanged. -
-
- -

-

Last updated 21 September 1997 by Carsten Sorensen

diff --git a/doc/asm/if.htm b/doc/asm/if.htm deleted file mode 100644 index df0ec6d8..00000000 --- a/doc/asm/if.htm +++ /dev/null @@ -1,24 +0,0 @@ - - - - - xAsm IF, ELSE, ENDC - - - -

IF, ELSE, ENDC

-

These three commands is used to conditionally assemble parts of your file. It is a powerful feature commonly used in macros.

-
IF   2+2==4
-PRINTT    "2+2==4\n"
-ELSE
-PRINTT    "2+2!=4\n"
-ENDC
-

The ELSE block is optional. IF/ELSE/ENDC-blocks can be nested.

-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/incbin.htm b/doc/asm/incbin.htm deleted file mode 100644 index 5dfdeb61..00000000 --- a/doc/asm/incbin.htm +++ /dev/null @@ -1,22 +0,0 @@ - - - - - xAsm INCBIN - - - -

INCBIN

-

You probably have some graphics you’d like to include. Use INCBIN to include a raw binary file as it is. If the file isn’t found in the current directory the include-path list will be searched.

-
INCBIN    "titlepic.bin"
-INCBIN    "sprites\\hero.bin"
-

You can also include only part of a file with INCBIN. The example below includes 256 bytes from data.bin starting from position 78.

-
INCBIN    "data.bin",78,256
-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/include.htm b/doc/asm/include.htm deleted file mode 100644 index 41f34f74..00000000 --- a/doc/asm/include.htm +++ /dev/null @@ -1,19 +0,0 @@ - - - - - xAsm INCLUDE - - - -

INCLUDE

-

Use INCLUDE to process another assembler-file and then return to the current file when done. If the file isn't found in the current directory the include-path list will be searched. You may nest INCLUDE calls infinitely (or until you run out of memory whichever comes first).

-
INCLUDE   "irq.inc"
-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/labels.htm b/doc/asm/labels.htm deleted file mode 100644 index 755442f9..00000000 --- a/doc/asm/labels.htm +++ /dev/null @@ -1,22 +0,0 @@ - - - - - xAsm Labels - - - -

Labels

-

One of the assembler’s main tasks is to keep track of addresses for you so you dor’t have to remember obscure numbers but can make do with a meaningful name, a label.

-

This can be done in a number of ways:

-
GlobalLabel
-AnotherGlobal:
-.locallabel
-.yet_a_local:
-ThisWillBeExported::        ;note the two colons
-

This is very similar to other assemblers. Local labels are only accessible within the scope they are defined. A scope starts after a global label and ends at the next global label. You may or may not have seen the :: feature before. It declares a normal global label but does an EXPORT at the same time.

-

Labels will normally change their value during the link process and are thus not constant.

-
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/macro.htm b/doc/asm/macro.htm deleted file mode 100644 index d2fdbb52..00000000 --- a/doc/asm/macro.htm +++ /dev/null @@ -1,60 +0,0 @@ - - - - - xAsm MACRO/ENDM - - - -

MACRO, ENDM

-

One of the best features of an assembler is the ability to write macros for it. Macros also provide a method of passing arguments to them and they can then react to the input using IF-constructs.

-
MyMacro:  MACRO
-          ld   a,80
-          call MyFunc
-          ENDM
-

The above example is a very simple macro. You execute the macro by typing its name.

-
          add  a,b
-          ld   sp,hl
-          MyMacro        ;This will be expanded
-          sub  a,87
-

When the assembler meets MyMacro it will insert the macrodefinition (the text enclosed in MACRO/ENDM).

-

Suppose your macro contains a loop.

-
LoopyMacro:    MACRO
-               xor  a,a
-.loop          ld   [hl+],a
-               dec  c
-               jr   nz,.loop
-               ENDM
-

This is fine. That is, if you only use the macro once per scope. To get around this problem there is a special label string equate called \@ that you can append to your labels and it will then expand to a unique string.

-

\@ also works in REPT-blocks should you have any loops there.

-
LoopyMacro:    MACRO
-               xor  a,a
-.loop\@        ld   [hl+],a
-               dec  c
-               jr   nz,.loop\@
-               ENDM
-

Arguments

-

I’d like LoopyMacro a lot better if I didn’t have to pre-load the registers with values and then call it. What I’d like is the ability to pass it arguments and it then loaded the registers itself.

-

And I can do that. In macros you can get the arguments by using the special macro string equates \1 through \9, \1 being the first argument specified on the calling of the macro.

-
LoopyMacro:    MACRO
-               ld   hl,\1
-               ld   c,\2
-               xor  a,a
-.loop\@        ld   [hl+],a
-               dec  c
-               jr   nz,.loop\@
-               ENDM
-

Now I can call the macro specifying two arguments. The first being the address and the second being a bytecount. The macro will then reset all bytes in this range.

-
               LoopyMacro     MyVars,54
-

You can specify up to nine arguments when calling a macro. Arguments are passed as string equates. There’s no need to enclose them in quotes. Parameter passing has changed a bit since v1.03 in that an expression will not be evaluated first but passed directly. This means that it’s probably a very good idea to use brackets around \1–\9 if you perform further calculations on them. For instance if you pass 1+2 as the first argument and then do PRINTV \1*2 -you will get the value 5 on screen and not 6 as you might have expected.

-

Note that a colon (:) following the macro-name is required. Macros can't be exported or imported. It's valid to call a macro from a macro (yes, even the same one).

- -

See also:

- -
-

Last updated 02 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/miscfunc.htm b/doc/asm/miscfunc.htm deleted file mode 100644 index c843de12..00000000 --- a/doc/asm/miscfunc.htm +++ /dev/null @@ -1,38 +0,0 @@ - - - - - xAsm Other functions - - - -

Other functions

-

There's a few other functions that do various useful things:

- - - - - - - - - - - - - - - - -
Other functions
NameOperation
BANK(label)Gameboy ONLY: Returns the bank number label is in. The link will have to resolve this so it can't be used when the expression has to be constant
DEF(label)Returns TRUE if label has been defined
-

See alse:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/opt.htm b/doc/asm/opt.htm deleted file mode 100644 index d01f4a4c..00000000 --- a/doc/asm/opt.htm +++ /dev/null @@ -1,27 +0,0 @@ - - - - - xAsm OPT - - - -

OPT

-

OPT can be used to change some of the options during assembling you source instead of defining them on the commandline.

-

OPT takes a comma-seperated list of options as its argument:

- -
    PUSHO
-    OPT   g.oOX    ;Set the GB graphics constants to use these characters
-    DW    `..ooOOXX
-    POPO
-    DW    `00112233
-

The options that OPT can modify are currently: b, e and g

-

See also:

- -
-

Last updated 20 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/popo.htm b/doc/asm/popo.htm deleted file mode 100644 index 4843c5ea..00000000 --- a/doc/asm/popo.htm +++ /dev/null @@ -1,18 +0,0 @@ - - - - - xAsm POPO, PUSHO - - - -

POPO, PUSHO

-

POPO and PUSHO provide the interface to the option stack. PUSHO will push the current set of options on the option stack. POPO can then later be used to restore them. Useful if you want to change some options in an include file and you don't want to destroy the options set by the program that included your file. The stacks number of entries is limited only by the amount of memory in your machine.

-

See also:

- -
-

Last updated 20 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/pops.htm b/doc/asm/pops.htm deleted file mode 100644 index cf5cd712..00000000 --- a/doc/asm/pops.htm +++ /dev/null @@ -1,18 +0,0 @@ - - - - - xAsm POPS, PUSHS - - - -

POPS, PUSHS

-

POPS and PUSHS provide the interface to the section stack. PUSHS will push the current section context on the section stack. POPS can then later be used to restore it. Useful for defining sections in included files when you don't want to destroy the section context for the program that included your file. The stacks number of entries is limited only by the amount of memory in your machine.

-

See also:

- -
-

Last updated 18 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/presym.htm b/doc/asm/presym.htm deleted file mode 100644 index a7846f07..00000000 --- a/doc/asm/presym.htm +++ /dev/null @@ -1,63 +0,0 @@ - - - - - xAsm Predeclared symbols - - - -

Predeclared symbols

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Symbols
NameContentsType
@PC valueEQU
_PIFixed point ¶EQU
_RS_RS counterSET
_NARGNumber of arguments passed to macroEQU
__LINE__The current linenumberEQU
__FILE__The current filenameEQUS
__DATE__Todays dateEQUS
__TIME__The current timeEQUS
-
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/print.htm b/doc/asm/print.htm deleted file mode 100644 index 9e7f91b2..00000000 --- a/doc/asm/print.htm +++ /dev/null @@ -1,29 +0,0 @@ - - - - - xAsm PRINTT, PRINTV, PRINTF - - - -

PRINTT, PRINTV, PRINTF

-

These three instructions type text and values to stdout. Useful for debugging macros or wherever you may feel the need to tell yourself some important information.

-
PRINTT    "I'm the greatest programmer in the whole wide world\n"
-PRINTV    (2+3)/5
-PRINTF    MUL(3.14,3987.0)
- - -

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/purge.htm b/doc/asm/purge.htm deleted file mode 100644 index 2763e60b..00000000 --- a/doc/asm/purge.htm +++ /dev/null @@ -1,18 +0,0 @@ - - - - - xAsm PURGE - - - -

PURGE

-

The PURGE command allows you to completely remove a symbol from the symbol table as if it had never existed. USE WITH EXTREME CAUTION!!! I can’t stress this enough but you seriously need to know what you are doing. DON’T purge symbol that you use in expressions the linker needs to calculate. In fact, it’s probably not even safe to purge anything other than string symbols and macros.

-
Kamikaze    EQUS    "I don't want to live anymore"
-AOLer       EQUS    "Me too"
-            PURGE   Kamikaze,AOLer
-

Note that string symbols that are part of a PURGE command WILL NOT BE EXPANDED as the ONLY exception to this rule.

-
-

Last updated 02 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/rept.htm b/doc/asm/rept.htm deleted file mode 100644 index 60abe0e3..00000000 --- a/doc/asm/rept.htm +++ /dev/null @@ -1,32 +0,0 @@ - - - - - xAsm REPT, ENDR - - - -

REPT, ENDR

-

Suppose you’re feeling lazy and you want to unroll a time consuming loop. REPT is here for that purpose. Everything between REPT and ENDR will be repeated a number of times just as if you done a copy/paste operation yourself

-
REPT 4
-add  a,c
-ENDR
-

This will assemble add a,c four times.

-

You can also use REPT to generate tables on the fly:

-
; --
-; -- Generate a 256 byte sine table with values between 0 and 128
-; --
-ANGLE   SET     0.0
-        REPT    256
-        DB      (MUL(64.0,SIN(ANGLE))+64.0)>>16
-ANGLE   SET     ANGLE+256.0
-        ENDR
-

REPT is also very useful in recursive macros and as in macros you can also use the special label operator \@. REPT-blocks can be nested.

-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/rs.htm b/doc/asm/rs.htm deleted file mode 100644 index 6980a441..00000000 --- a/doc/asm/rs.htm +++ /dev/null @@ -1,76 +0,0 @@ - - - - - xAsm RSSET, RSRESET, RB, RW - - - -

RSSET, RERESET, RB, RW

-

The RS group of commands is a handy way of defining structures:

-
              RSRESET
-str_pStuff    RW   1
-str_tData     RB   256
-str_bCount    RB   1
-str_SIZEOF    RB   0
-

The example defines four equated symbols:

- - - - - - - - - - - - - - - - - - - - - - - - -
Defined symbols
NameValue
str_pStuff0
str_tData2
str_bCount258
str_SIZEOF259
-

There are four commands in the RS group of commands:

- - - - - - - - - - - - - - - - - - - - - - - - -
RS related commands
CommandMeaning
RSRESETResets the _RS counter to zero
RSSET constexprSets the _RS counter to constexpr
RB constexprSets the preceding symbol to _RS and adds constexpr to _RS
RW constexprSets the preceding symbol to _RS and adds constexpr*2 to _RS
-

Note that a colon (:) following the symbol-name is not allowed. RS symbols can be exported and imported. They don't change their value during the link process.

-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/section.htm b/doc/asm/section.htm deleted file mode 100644 index 59d2a054..00000000 --- a/doc/asm/section.htm +++ /dev/null @@ -1,70 +0,0 @@ - - - - - xAsm SECTION - - - -

SECTION

-

Before you can start writing code you must define a section. This tells the assembler what kind of data follows and if it is code where to put it.

-
SECTION   "CoolStuff",ROMX
-

This switches to the section called "CoolStuff" (or creates it if it doesn't already exits) and it defines it as a code section. All sections within a sourcefile must be identified by a unique name.

- -

Possible section types are as follows: - -

-
ROM0
-
A ROM section. Mapped to memory at $0000–$3fff.
- -
ROMX
-
A banked ROM section. Mapped to memory at $4000–$7fff. Valid banks range from 1 to 511.
- -
VRAM
-
A banked video RAM section. Mapped to memory at $8000–$9fff. Can only allocate memory, not fill it. Valid banks range from 0 to 1.
- -
SRAM
-
A banked external (save) RAM section. Mapped to memory at $a000–$bfff. Can only allocate memory, not fill it. Valid banks range from 0 to 3.
- -
WRAM0
-
A general-purpose RAM section. Mapped to memory at $c000–$cfff. Can only allocate memory, not fill it.
- -
WRAMX
-
A banked general-purpose RAM section. Mapped to memory at $d000–$dfff. Can only allocate memory, not fill it. Valid banks range from 1 to 7.
- -
HRAM
-
A high RAM section. Mapped to memory at $ff80–$fffe. Can only allocate memory, not fill it. NOTE WELL: if you use this method of allocating HRAM the assembler will NOT choose the short addressingmode in the LD instruction because the actual address calculation is done by the linker! If you find this undesirable you can use RSSET/RB/RW instead or use the LDIO mnemonic. The address calculation is then done by the assembler.
-
- -

The following deprecated section names are aliases for some of the above sections: - -

-
HOME
-
Alias for ROM0.
- -
CODE
-
DATA
-
Alias for ROMX.
- -
BSS
-
Alias for WRAM0.
-
- -

Due to quite a lot of emails requesting an ORG directive you can now add an address to the sectiontype for the Gameboy:

-
SECTION   "CoolStuff",ROM0[$1234]
-

This will force the section to address $1234. This also works with the other sectiontypes. For ROMX sections the linker will then place the section in any bank at the address you specify. If you also want to specify the bank you can do:

-
SECTION   "CoolStuff",ROMX[$4567],BANK[3]
-

And if you only want to force the section into a certain bank, and not it's position within the bank, that's also possible:

-
SECTION   "CoolStuff",ROMX,BANK[7]
-

HINT: If you think this is a lot of typing for doing a simple ORG type thing you can quite easily write an intelligent macro (called ORG for example) that uses \@ for the sectionname and determines correct sectiontype etc as arguments for SECTION

-

See also:

- -
-

Last updated 18 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/set.htm b/doc/asm/set.htm deleted file mode 100644 index a78489c4..00000000 --- a/doc/asm/set.htm +++ /dev/null @@ -1,22 +0,0 @@ - - - - - xAsm SET - - - -

SET

-

SETs are like EQUates also constant symbols in the sense that their values are defined during the assembly process. These symbols are normally used in macros.

-
KINKYCOUNT     SET  2
-KINKYCOUNT     SET  DONUT_ISGOOD+KINKYCOUNT
-

Note that a colon (:) following the label-name is not allowed. SETs can be exported and imported but the result is undefined and might change in a later release. Alternatively you can use = as a synonym for SET.

-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - - diff --git a/doc/asm/shift.htm b/doc/asm/shift.htm deleted file mode 100644 index be81b943..00000000 --- a/doc/asm/shift.htm +++ /dev/null @@ -1,19 +0,0 @@ - - - - - xAsm SHIFT - - - -

SHIFT

-

SHIFT is a special command only available in macros. Very useful in REPT-blocks. It will "shift" the arguments by one "to the left". \1 will get \2's value, \2 will get \3's value and so forth.

-

See also:

- -
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/symbols.htm b/doc/asm/symbols.htm deleted file mode 100644 index 1af4fde0..00000000 --- a/doc/asm/symbols.htm +++ /dev/null @@ -1,31 +0,0 @@ - - - - - xAsm Symbols - - - -

xAsm Symbols

-

xAsm supports several types of symbols:

- - -

A symbol cannot have the same name as a reserved keyword.

- -

See also:

- -
-

Last updated 02 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/syntax.htm b/doc/asm/syntax.htm deleted file mode 100644 index c308ddc1..00000000 --- a/doc/asm/syntax.htm +++ /dev/null @@ -1,23 +0,0 @@ - - - - - xAsm Syntax - - - - -

xAsm Syntax

-

The syntax line‐based, just as in any other assembler. Meaning that you do one instruction or pseudo‐op per line:

-
[label] [instruction] [;comment]
-

Example:

-
John:          ld   a,87 ;Weee
-

A comment can also be an asterisk (*) followed by the comment if the asterisk is the first character on the line:

-
********************************
-* These are full line comments *
-********************************
-

All pseudo‐ops, mnemonics and registers (reserved keywords) are case‐insensitive and all labels are case‐sensitive.

-
-

Last updated 21 June 1997 by Carsten Sorensen

- - diff --git a/doc/asm/trg_gb.htm b/doc/asm/trg_gb.htm deleted file mode 100644 index fae41ca3..00000000 --- a/doc/asm/trg_gb.htm +++ /dev/null @@ -1,102 +0,0 @@ - - - - - xAsm Gameboy - - - -

Gameboy

-

The full GB-Z80 instruction-set is implemented with a few modifications to the original Zilog syntax. This is due to a Bison problem but in my opinion the syntax is better than the original one.

-

Instructions

- -

Note that you can use both -

    OR   A,B
-    OR   B
-

Addressingmodes

-

Indirect addressing has been changed to [ ] instead of ( ):

- -

The assembler will intelligently decide between [n16] and [$FF00+n8] in the LD instruction. Note however that if you use any constant symbols in the expression they had better be defined before the instruction or your symbol-table may become mangled. Also worth noting is that it will only ever select the short $FF00 mode when you use constant symbols. NOT if you use symbols defined in a HRAM section. As this defies the whole point of implementing the HRAM sectiontype I've added the LDIO mnemonic. It works like the LD instruction but it will ALWAYS generate the $FF00+ form and it will also automatically logically AND the expression with $FF if it is relocatable. Which is what you want. Trust me ;)

-

Conditioncodes

- -
-

Last updated 20 July 1997 by Carsten Sorensen

- - diff --git a/doc/asm/usage.htm b/doc/asm/usage.htm deleted file mode 100644 index f48ac865..00000000 --- a/doc/asm/usage.htm +++ /dev/null @@ -1,30 +0,0 @@ - - - - - xAsm Usage - - - -

xAsm Usage

-
C:\>xAsm [options] asmfile
-

Options are preceded by a hyphen (-) and go as follows:

-
    oobjectfile : Write an object-file for xLink
-    ipath       : Add an extra include-path
-    h           : Short help text
-    e(l|b)      : Change endianness (CAUTION!)
-    gASCI       : Change the four characters used for Gameboy graphics
-                  constants (default is 0123)
-    bAS         : Change the two characters used for binary constants
-                  (default is 01)
-    zHX         : Set the byte value (hex format) used for uninitialised data (default is ? for random)
-
- -

See also:

- -
-

Last updated 08 October 1997 by Carsten Sorensen

- - diff --git a/doc/geninfo.htm b/doc/geninfo.htm deleted file mode 100644 index cd28edcc..00000000 --- a/doc/geninfo.htm +++ /dev/null @@ -1,116 +0,0 @@ - - - - - General Information - - - -

RGBDS—ASMotor General Information

-

Table of Contents

- -

License

-

The ASMotor package (xAsm, xLink, RGBFix, examples and documentation) is freeware and distributed as is. The author retains his copyright and right to modify the specifications and operation of the software without notice. -

In other words this means I encourage you to… -

-

This also means you can’t… -

-

The Author

-

Any questions? Write me! -

Address

-

-Carsten Sorensen
-1 Spring Court
-Guildford
-Surrey GU2 6QW
-United Kingdom
-

e-mail:

- -

Get the latest version from my web page at http://www.matilde.demon.co.uk -

History

- - - - - - - - - - - - - - - - - - - - - - - - - - -
The history of ASMotor
VersionDatedRelease notes
1.003 July 1997First release
1.0120 July 1997

RGBDS fixes:

-
    -
  • RGBFix can now also truncate the ROM-images to a valid size. -
  • RGBAsm supports the LDD and LDI syntax plus [HLD] and [HLI]. LDH is synonymous with LDIO. -
  • Example filenames have been changed to adhere to Jeff Frohweins proposed standard. -
-

General fixes:

-
    -
  • RGBLink knows about big and little endian. Plus it can do range checking on intermediate results in an expression. This is necessary to support -different types of CPUs. -
  • RGBLink didn’t know about the special PC symbol “@” so if you used it more than once per sourcefile in an expression the linker had to resolve, things would go horribly wrong. -
  • There was a bug in the macro parameter passing. Any whitespace after the last parameter would be appended to the last parameter. Reported by Jeff Frohwein. -
  • A section stack has been implemented. Look up POPS and PUSHS in the -manual. Jeff Frohwein’s doing again. -
  • OPT command added for defining and changing some options while assembling. -
  • You can now define which characters are used for the Gameboy graphics integer (`) using the commandline or the new OPT command. Cool idea by (surprise surprise) Jeff Frohwein. -
  • Also, an option stack has been added. Look up POPO and PUSHO in the manual. -
  • Fixed yet another line number bug reported by Jeff Frohwein (when will this guy leave me alone? ;) -
-
1.0222 July 1997

General fixes:

-
    -
  • The lamest typo bug of all time has been fixed. RGBAsm would output a word defined with DW as 4 bytes instead of 2. Jeff Frohwein reported this. -
  • The first line of an included file didn’t assemble correctly. -
  • -b option added for setting the characters used for binary constants. -
-
1.1021 Sep 1997

General fixes:

-
    -
  • The assembler would crash if you tried to use a macro symbol in an expression. (Jeff Frohwein) -
  • You couldn’t use STRCMP, STRLEN and STRIN in relocatable expressions. (Harry P. Mulder) -
  • Relocatable symbols are no longer allowed as arguments to the DEF function. -
  • Finally! A librarian and smart linking has been added. -
  • Bug fixed in the assembler where it would sometimes write out too many bytes for HRAM section definitions. -
  • -z options (set fill value used for uninitialised data) added to the -assembler and linker. -
  • The assembler will now read in any type of ASCII file on any type of OS. -
-
-

Last updated 08 October 1997 by Carsten Sorensen

- - diff --git a/doc/index.htm b/doc/index.htm deleted file mode 100644 index 148e4e34..00000000 --- a/doc/index.htm +++ /dev/null @@ -1,20 +0,0 @@ - - - - - RGBDS—ASMotor Documentation - - - -

RGBDS—ASMotor v1.10 Documentation

-

Table of Contents

- -

Last updated 21 September 1997 by Carsten Sorensen

- - diff --git a/doc/link.htm b/doc/link.htm deleted file mode 100644 index 3f4d74d0..00000000 --- a/doc/link.htm +++ /dev/null @@ -1,118 +0,0 @@ - - - - - xLink - - - -

xLink Documentation

-

Table of Contents

- -
-

History

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The history of xLink
VersionDatedRelease notes
1.01 Oct. 96First release (RGBDS)
1.013 Dec. 96 -
    -
  • BANK() didn't work. Fixed.
  • -
  • Sections were quite often output in the wrong order. Fixed.
  • -
-
1.0212 Feb. 97-s switch and mapfile option added
1.0323 Mar. 97 -
1.0403 July 1997First ASMotor release. Supports big-endian CPUs as well. Usage changed to allow for different output fileformats
1.0520 July 1997 -
    -
  • We can now do range checking on intermediate results in an expression. This is necessary to support different types of CPUs.
  • -
  • RGBLink DIDN’T know about the special PC symbol "@" so if you used it more than once per sourcefile in an expression the linker had to resolve, things would go horribly wrong.
  • -
-
1.0621 September 1997
  • Smart linking and library support added -
  • Program renamed to xLink -
-
-

Usage

-
    xlink [options] linkfile
-

Options are preceded by a hyphen (-) and go as follows: -

    h         : Short help text
-    mmapfile  : Write a mapfile
-    t         : Output target:
-      tg      : Gameboy ROM image (default)
-      ts      : Gameboy Small mode (32kB) ROM image
-      tp      : Psion2 relocatable module
-    zHX       : Set the byte value (hex format) used for uninitialised data (default is ? for random)
-
-

The Linkfile

-

A linkfile is used to tell xLink which objects to include and what the outputname should be. It is in plain ASCII-format. -

    # Linkfile for foobar.gb
-      
-    [Objects]
-    foo.obj
-    bar.obj
-      
-    [Libraries]
-    mylib.lib
-
-    [Output]
-    foobar.gb
-

A line starting with # is ignored. -

If you use libraries they will only be included if one of the objects actually reference them. This works on a SECTION level and not on a module level. This means that when you write libraries you can put each subroutine in its own SECTION so only the relevant bits are included. -

Operation for Gameboy (-tg)

-

Sections created with ROM0 in the assembler are placed in the GB bank #0 (the fixed bank $0000-$3FFF) in the order they are loaded from the objectfiles specified in the linkfile. So you want the first file in the linkfile to contain your header. ROMX sections are placed in any bank other than #0. This means you have absolutely no control over which sections goes where. This insures minimal slack (unused bytes) at the end of each bank in the image. -

Currently the linker doesn't calculate the GB checksums. -You must use RGBFix to do this. - -

Operation for Gameboy small mode (-ts)

-

Small mode forces all ROMX sections to be of type ROM0 and increases the ROM0 section size from 16kB to 32kB. This also means that ROM0/ROMX sections are written to the final image in the order you have specified on the command line. -

Currently the linker doesn't calculate the GB checksums. You must use RGBFix to do this. - -


-

Last updated 08 October 1997 by Carsten Sorensen

- - diff --git a/doc/rgb0.htm b/doc/rgb0.htm deleted file mode 100644 index b0f7a658..00000000 --- a/doc/rgb0.htm +++ /dev/null @@ -1,210 +0,0 @@ - - - - - RGB? Fileformat - - - -

The RGB ObjectFileFormats

-

Table of Contents

- -

Background

-

I developed the RGB0 fileformat mainly because I needed a suitable dataformat to hold the output from xAsm that was powerful to accomodate all the features I needed and also would make it easy for me to add new ones. The reason for documenting it is so people can write converters between it and other formats. Perhaps even develop other compilers for it?

-

The RGB1 fileformat saw the light of day with the V1.02 of the old RGBDS release because of the addition of fixed sections.

-

The RGB2 fileformat emerged because I needed to add support for big endian CPUs.

-

FileStructure

- -

Down to business...

-
-    ; There's a header...
-      
-    BYTE    ID[4]                      ;"RGB0", "RGB1", "RGB2"
-    LONG    NumberOfSymbols            ;The number of symbols used in this file
-    LONG    NumberOfSections           ;The number of sections used in this file
-      
-    ; Now for some symbols
-      
-    REPT    NumberOfSymbols            ;NumberOfSymbols symboldefs follow
-        STRING  Name                   ;The name of this symbol
-        BYTE    Type                   ;0 = LOCAL symbol only used in this file
-                                       ;1 = IMPORT this symbol from elsewhere
-                                       ;2 = EXPORT this symbol to other objects
-        IF      Type != 1
-            LONG    SectionID          ;The section number in which this symbol
-                                       ;is defined.  If -1 this symbol is an EQUate
-            LONG Value                 ;The symbols value. If SectionID!=-1 it's the
-                                       ;offset into that section
-        ENDC
-    ENDR
-      
-    ; And I'll be... Sections!
-      
-    REPT NumberOfSections
-        LONG    Size                   ;Size in bytes of this section
-        BYTE    Type                   ;0 = WRAM0
-                                       ;1 = VRAM
-                                       ;2 = ROMX
-                                       ;3 = ROM0
-                                       ;4 = HRAM
-        LONG    Org                    ;Only present in RGB1. Address to fix this
-                                       ;section at. -1 if the linker should
-                                       ;decide (normal operation)
-        LONG    Bank                   ;Only present in RGB1. Bank to load this
-                                       ;section into. -1 if the linker should
-                                       ;decide (normal operation). This field is
-                                       ;only valid for ROMX sections.
-        IF      Type==ROMX || Type==ROM0
-            BYTE Data[Size]
-            LONG NumberOfPatches
-               
-            ; These types of sections may have patches
-               
-            REPT NumberOfPatches
-                STRING    SourceFile   ;The name of the sourcefile (for
-                                       ;printing an errormessage)
-                LONG Line              ;The line of the sourcefile
-                LONG Offset            ;Offset into the section where patch
-                                       ;should be applied
-                BYTE Type              ;0 = BYTE patch
-                                       ;1 = little endian WORD patch
-                                       ;2 = little endianLONG patch
-                                       ;3 = big endian WORD patch (RGB2 and later)
-                                       ;4 = big endianLONG patch (RGB2 and later)
-                LONG RPNSize
-                BYTE RPN[RPNSize]      ;RPN definition below
-            ENDR
-        ENDC
-    ENDR
-

Rpn Data

-

Expressions in the objectfile are stored as RPN. This is an expression of the form “2 5 +”. This will first push the value “2” to the stack. Then “5”. The “+” operator pops two arguments from the stack, adds them, and then pushes the result on the stack, effectively replacing the two top arguments with their sum. In the RGB format RPN expressions are stored as BYTEs with some bytes being special prefixes for integers and symbols.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RPN Expressions
Byte valueMeaning
$00+ operator
$01- operator
$02* operator
$03/ operator
$04% operator
$05unary -
$06| operator
$07& operator
$08^ operator
$09unary ~
$0A&& comparison
$0B|| comparison
$0Cunary !
$0D== comparison
$0E!= comparison
$0F> comparison
$10< comparison
$11>= comparison
$12<= comparison
$13<< operator
$14>> operator
$15BANK() function for Gameboy, a symbol ID follows
$16HRAMCheck for Gameboy, check if value is in HRAM and logically and it with 0xFF
$17ZeroPageCheck for PC-Engine, check if value is in ZP (0x2000-0x20FF) and logically and it with 0xFF
$18RangeCheck. LOW and HIGH signed LONGs follow. Checks a value to see if within the range [LOW;HIGH]. If not, generate an error. -
$80LONG integer follows
$81Symbol ID follows
-
-

Last updated 18 July 1997 by Carsten Sorensen

- - diff --git a/doc/style.css b/doc/style.css deleted file mode 100644 index e69de29b..00000000 diff --git a/include/asm/asm.h b/include/asm/asm.h index 1f70f6f7..609de1c0 100644 --- a/include/asm/asm.h +++ b/include/asm/asm.h @@ -30,6 +30,6 @@ extern struct sSymbol *pPCSymbol; extern bool oDontExpandStrings; #define MAXMACROARGS 256 -#define MAXINCPATHS 16 +#define MAXINCPATHS 128 #endif /* // ASM_H */ diff --git a/include/asm/charmap.h b/include/asm/charmap.h index 6f958040..bea54ef2 100644 --- a/include/asm/charmap.h +++ b/include/asm/charmap.h @@ -2,7 +2,7 @@ #define RGBDS_ASM_CHARMAP_H #define MAXCHARMAPS 512 -#define CHARMAPLENGTH 8 +#define CHARMAPLENGTH 16 struct Charmap { int count; diff --git a/include/asm/lexer.h b/include/asm/lexer.h index 3caea4c6..b20df46f 100644 --- a/include/asm/lexer.h +++ b/include/asm/lexer.h @@ -59,9 +59,7 @@ extern void yyunputbytes(ULONG count); extern YY_BUFFER_STATE pCurrentBuffer; -#ifdef __GNUC__ -extern void strupr(char *s); -extern void strlwr(char *s); -#endif +extern void upperstring(char *s); +extern void lowerstring(char *s); #endif diff --git a/include/asm/main.h b/include/asm/main.h index 56b4d740..9488512d 100644 --- a/include/asm/main.h +++ b/include/asm/main.h @@ -2,6 +2,7 @@ #define RGBDS_MAIN_H #include +#include "extern/stdnoreturn.h" struct sOptions { char gbgfx[4]; @@ -9,6 +10,7 @@ struct sOptions { SLONG fillchar; bool verbose; bool haltnop; + bool exportall; //-1 == random }; @@ -23,7 +25,7 @@ extern void opt_Push(void); extern void opt_Pop(void); extern void opt_Parse(char *s); -void fatalerror(const char *fmt, ...); +noreturn void fatalerror(const char *fmt, ...); void yyerror(const char *fmt, ...); #define YY_FATAL_ERROR fatalerror diff --git a/include/asm/symbol.h b/include/asm/symbol.h index 1b8ceaff..727f44c4 100644 --- a/include/asm/symbol.h +++ b/include/asm/symbol.h @@ -36,6 +36,7 @@ struct sSymbol { * not be changed during linking */ ULONG calchash(char *s); +void sym_SetExportAll(BBOOL set); void sym_PrepPass1(void); void sym_PrepPass2(void); void sym_AddLocalReloc(char *tzSym); diff --git a/include/gfx/gb.h b/include/gfx/gb.h new file mode 100644 index 00000000..3d901bef --- /dev/null +++ b/include/gfx/gb.h @@ -0,0 +1,30 @@ +/* + * Copyright © 2013 stag019 + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef RGBDS_GFX_GB_H +#define RGBDS_GFX_GB_H + +#include +#include "gfx/main.h" + +void png_to_gb(struct PNGImage png, struct GBImage *gb); +void output_file(struct Options opts, struct GBImage gb); +int get_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles, int tile_size); +void create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap); +void output_tilemap_file(struct Options opts, struct Tilemap tilemap); +void output_palette_file(struct Options opts, struct PNGImage png); + +#endif diff --git a/include/gfx/main.h b/include/gfx/main.h new file mode 100644 index 00000000..a6377819 --- /dev/null +++ b/include/gfx/main.h @@ -0,0 +1,75 @@ +/* + * Copyright © 2013 stag019 + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef RGBDS_GFX_MAIN_H +#define RGBDS_GFX_MAIN_H + +#include +#include +#include + +#include "extern/err.h" + +struct Options { + bool debug; + bool verbose; + bool hardfix; + bool fix; + bool horizontal; + bool unique; + int trim; + char *mapfile; + bool mapout; + char *palfile; + bool palout; + char *outfile; + char *infile; +}; + +struct PNGImage { + png_struct *png; + png_info *info; + png_byte **data; + int width; + int height; + png_byte depth; + png_byte type; + bool horizontal; + int trim; + char *mapfile; + bool mapout; + char *palfile; + bool palout; +}; + +struct GBImage { + uint8_t *data; + int size; + bool horizontal; + int trim; +}; + +struct Tilemap { + uint8_t *data; + int size; +}; + +int depth, colors; + +#include "gfx/makepng.h" +#include "gfx/gb.h" + +#endif diff --git a/include/gfx/makepng.h b/include/gfx/makepng.h new file mode 100644 index 00000000..1ae2a56f --- /dev/null +++ b/include/gfx/makepng.h @@ -0,0 +1,28 @@ +/* + * Copyright © 2013 stag019 + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef RGBDS_GFX_PNG_H +#define RGBDS_GFX_PNG_H + +#include "gfx/main.h" + +void input_png_file(struct Options opts, struct PNGImage *img); +void get_text(struct PNGImage *png); +void set_text(struct PNGImage *png); +void output_png_file(struct Options opts, struct PNGImage *png); +void free_png_data(struct PNGImage *png); + +#endif diff --git a/include/link/assign.h b/include/link/assign.h index 5b6cd2a9..7ffa3ce8 100644 --- a/include/link/assign.h +++ b/include/link/assign.h @@ -5,13 +5,26 @@ enum eBankDefine { BANK_ROM0 = 0, + BANK_ROMX, BANK_WRAM0 = 512, BANK_WRAMX, BANK_VRAM = 520, BANK_HRAM = 522, BANK_SRAM = 523 }; -#define MAXBANKS 527 + +enum eBankCount { + BANK_COUNT_ROM0 = 1, + BANK_COUNT_ROMX = 511, + BANK_COUNT_WRAM0 = 1, + BANK_COUNT_WRAMX = 7, + BANK_COUNT_VRAM = 2, + BANK_COUNT_HRAM = 1, + BANK_COUNT_SRAM = 16 +}; + +#define MAXBANKS (BANK_COUNT_ROM0 + BANK_COUNT_ROMX + BANK_COUNT_WRAM0 + BANK_COUNT_WRAMX \ + + BANK_COUNT_VRAM + BANK_COUNT_HRAM + BANK_COUNT_SRAM) extern SLONG area_Avail(SLONG bank); extern void AssignSections(void); diff --git a/src/asm/asmy.y b/src/asm/asmy.y index 46d4554d..6264f74e 100644 --- a/src/asm/asmy.y +++ b/src/asm/asmy.y @@ -24,7 +24,7 @@ void bankrangecheck(char *name, ULONG secttype, SLONG org, SLONG bank) { SLONG minbank, maxbank; - char *stype; + char *stype = NULL; switch (secttype) { case SECT_ROMX: stype = "ROMX"; @@ -51,7 +51,7 @@ bankrangecheck(char *name, ULONG secttype, SLONG org, SLONG bank) "ROMX, WRAMX, SRAM, or VRAM sections"); } - if (bank < minbank || bank > maxbank) { + if (stype && (bank < minbank || bank > maxbank)) { yyerror("%s bank value $%x out of range ($%x to $%x)", stype, bank, minbank, maxbank); } @@ -142,8 +142,9 @@ void copyrept( void ) { SLONG level=1, len, instring=0; char *src=pCurrentBuffer->pBuffer; + char *bufferEnd = pCurrentBuffer->pBufferStart + pCurrentBuffer->nBufferSize; - while( *src && level ) + while( src < bufferEnd && level ) { if( instring==0 ) { @@ -182,6 +183,10 @@ void copyrept( void ) } } + if (level != 0) { + fatalerror("Unterminated REPT block"); + } + len=src-pCurrentBuffer->pBuffer-4; src=pCurrentBuffer->pBuffer; @@ -217,8 +222,9 @@ void copymacro( void ) { SLONG level=1, len, instring=0; char *src=pCurrentBuffer->pBuffer; + char *bufferEnd = pCurrentBuffer->pBufferStart + pCurrentBuffer->nBufferSize; - while( *src && level ) + while( src < bufferEnd && level ) { if( instring==0 ) { @@ -257,6 +263,10 @@ void copymacro( void ) } } + if (level != 0) { + fatalerror("Unterminated MACRO definition"); + } + len=src-pCurrentBuffer->pBuffer-4; src=pCurrentBuffer->pBuffer; @@ -348,6 +358,10 @@ void if_skip_to_else( void ) } } + if (level != 0) { + fatalerror("Unterminated IF construct"); + } + len=src-pCurrentBuffer->pBuffer; yyskipbytes( len ); @@ -403,6 +417,10 @@ void if_skip_to_endc( void ) } } + if (level != 0) { + fatalerror("Unterminated IF construct"); + } + len=src-pCurrentBuffer->pBuffer; yyskipbytes( len ); @@ -1079,9 +1097,9 @@ string : T_STRING | T_OP_STRCAT '(' string ',' string ')' { strcpy($$,$3); strcat($$,$5); } | T_OP_STRUPR '(' string ')' - { strcpy($$,$3); strupr($$); } + { strcpy($$,$3); upperstring($$); } | T_OP_STRLWR '(' string ')' - { strcpy($$,$3); strlwr($$); } + { strcpy($$,$3); lowerstring($$); } ; section: T_POP_SECTION string ',' sectiontype @@ -1263,12 +1281,16 @@ z80_ldi : T_Z80_LDI T_MODE_HL_IND comma T_MODE_A { out_AbsByte(0x02|(2<<4)); } | T_Z80_LDI T_MODE_A comma T_MODE_HL { out_AbsByte(0x0A|(2<<4)); } + | T_Z80_LDI T_MODE_A comma T_MODE_HL_IND + { out_AbsByte(0x0A|(2<<4)); } ; z80_ldd : T_Z80_LDD T_MODE_HL_IND comma T_MODE_A { out_AbsByte(0x02|(3<<4)); } | T_Z80_LDD T_MODE_A comma T_MODE_HL { out_AbsByte(0x0A|(3<<4)); } + | T_Z80_LDD T_MODE_A comma T_MODE_HL_IND + { out_AbsByte(0x0A|(3<<4)); } ; z80_ldio : T_Z80_LDIO T_MODE_A comma op_mem_ind diff --git a/src/asm/charmap.c b/src/asm/charmap.c index 4f2aecf4..370f1986 100644 --- a/src/asm/charmap.c +++ b/src/asm/charmap.c @@ -1,3 +1,57 @@ +/* + * UTF-8 decoder copyright © 2008–2009 Björn Höhrmann + * http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include + +static const uint8_t utf8d[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf + 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df + 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef + 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff + 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0 + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2 + 1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4 + 1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6 + 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8 +}; + +uint32_t +decode(uint32_t* state, uint32_t* codep, uint32_t byte) { + uint32_t type = utf8d[byte]; + + *codep = (*state != 0) ? + (byte & 0x3fu) | (*codep << 6) : + (0xff >> type) & (byte); + + *state = utf8d[256 + *state*16 + type]; + return *state; +} + /* * Copyright © 2013 stag019 * @@ -28,122 +82,94 @@ struct Charmap globalCharmap = {0}; extern struct Section *pCurrentSection; int -readUTF8Char(char *destination, char *source) +readUTF8Char(char *dest, char *src) { - int size; - UBYTE first; - first = source[0]; + uint32_t state; + uint32_t codep; + int i; - if(first >= 0xFC) - { - size = 6; + for (i = 0, state = 0;; i++) { + if (decode(&state, &codep, (uint8_t)src[i]) == 1) { + fatalerror("invalid UTF-8 character"); + } + + dest[i] = src[i]; + + i++; + if (state == 0) { + dest[i] = '\0'; + return i; + } + dest[i] = src[i]; } - else if(first >= 0xF8) - { - size = 5; - } - else if(first >= 0xF0) - { - size = 4; - } - else if(first >= 0xE0) - { - size = 3; - } - else if(first >= 0xC0) - { - size = 2; - } - else if(first != '\0') - { - size = 1; - } - else - { - size = 0; - } - strncpy(destination, source, size); - destination[size] = 0; - return size; } int charmap_Add(char *input, UBYTE output) { int i, input_length; - char temp1i[CHARMAPLENGTH + 1], temp2i[CHARMAPLENGTH + 1], temp1o = 0, temp2o = 0; + char temp1i[CHARMAPLENGTH + 1], temp2i[CHARMAPLENGTH + 1], temp1o = 0, + temp2o = 0; struct Charmap *charmap; - if(pCurrentSection) - { - if(pCurrentSection -> charmap) - { - charmap = pCurrentSection -> charmap; - } - else - { - if((charmap = (struct Charmap *) calloc(1, sizeof(struct Charmap))) == NULL) - { + if (pCurrentSection) { + if (pCurrentSection->charmap) { + charmap = pCurrentSection->charmap; + } else { + if ((charmap = calloc(1, sizeof(struct Charmap))) == + NULL) { fatalerror("Not enough memory for charmap"); } - pCurrentSection -> charmap = charmap; + pCurrentSection->charmap = charmap; } - } - else - { + } else { charmap = &globalCharmap; } - if(nPass == 2) - { - return charmap -> count; + if (nPass == 2) { + return charmap->count; } - if(charmap -> count > MAXCHARMAPS || strlen(input) > CHARMAPLENGTH) - { + if (charmap->count > MAXCHARMAPS || strlen(input) > CHARMAPLENGTH) { return -1; } input_length = strlen(input); - if(input_length > 1) - { + if (input_length > 1) { i = 0; - while(i < charmap -> count + 1) - { - if(input_length > strlen(charmap -> input[i])) - { - memcpy(temp1i, charmap -> input[i], CHARMAPLENGTH + 1); - memcpy(charmap -> input[i], input, input_length); - temp1o = charmap -> output[i]; - charmap -> output[i] = output; + while (i < charmap->count + 1) { + if (input_length > strlen(charmap->input[i])) { + memcpy(temp1i, charmap->input[i], + CHARMAPLENGTH + 1); + memcpy(charmap->input[i], input, input_length); + temp1o = charmap->output[i]; + charmap->output[i] = output; i++; break; } i++; } - while(i < charmap -> count + 1) - { - memcpy(temp2i, charmap -> input[i], CHARMAPLENGTH + 1); - memcpy(charmap -> input[i], temp1i, CHARMAPLENGTH + 1); + while (i < charmap->count + 1) { + memcpy(temp2i, charmap->input[i], CHARMAPLENGTH + 1); + memcpy(charmap->input[i], temp1i, CHARMAPLENGTH + 1); memcpy(temp1i, temp2i, CHARMAPLENGTH + 1); - temp2o = charmap -> output[i]; - charmap -> output[i] = temp1o; + temp2o = charmap->output[i]; + charmap->output[i] = temp1o; temp1o = temp2o; i++; } - memcpy(charmap -> input[charmap -> count + 1], temp1i, CHARMAPLENGTH + 1); - charmap -> output[charmap -> count + 1] = temp1o; + memcpy(charmap->input[charmap->count + 1], temp1i, + CHARMAPLENGTH + 1); + charmap->output[charmap->count + 1] = temp1o; + } else { + memcpy(charmap->input[charmap->count], input, input_length); + charmap->output[charmap->count] = output; } - else - { - memcpy(charmap -> input[charmap -> count], input, input_length); - charmap -> output[charmap -> count] = output; - } - return ++charmap -> count; + return ++charmap->count; } -int +int charmap_Convert(char **input) { struct Charmap *charmap; @@ -152,47 +178,35 @@ charmap_Convert(char **input) char *buffer; int i, j, length; - if(pCurrentSection && pCurrentSection -> charmap) - { - charmap = pCurrentSection -> charmap; - } - else - { + if (pCurrentSection && pCurrentSection->charmap) { + charmap = pCurrentSection->charmap; + } else { charmap = &globalCharmap; } - if((buffer = (char *) malloc(strlen(*input))) == NULL) - { + if ((buffer = malloc(strlen(*input))) == NULL) { fatalerror("Not enough memory for buffer"); } length = 0; - while(**input) - { + while (**input) { j = 0; - for(i = 0; i < charmap -> count; i++) - { - j = strlen(charmap -> input[i]); - if(memcmp(*input, charmap -> input[i], j) == 0) - { - outchar[0] = charmap -> output[i]; + for (i = 0; i < charmap->count; i++) { + j = strlen(charmap->input[i]); + if (memcmp(*input, charmap->input[i], j) == 0) { + outchar[0] = charmap->output[i]; outchar[1] = 0; break; } j = 0; } - if(!j) - { + if (!j) { j = readUTF8Char(outchar, *input); } - if(!outchar[0]) - { + if (!outchar[0]) { buffer[length++] = 0; - } - else - { - for(i = 0; outchar[i]; i++) - { + } else { + for (i = 0; outchar[i]; i++) { buffer[length++] = outchar[i]; } } @@ -201,4 +215,3 @@ charmap_Convert(char **input) *input = buffer; return length; } - diff --git a/src/asm/fstack.c b/src/asm/fstack.c index 56fc46d0..4fe762ab 100644 --- a/src/asm/fstack.c +++ b/src/asm/fstack.c @@ -179,7 +179,15 @@ fstk_Dump(void) void fstk_AddIncludePath(char *s) { - strcpy(IncludePaths[NextIncPath++], s); + if (NextIncPath == MAXINCPATHS) { + fatalerror("Too many include directories passed from command line"); + return; + } + + if (strlcpy(IncludePaths[NextIncPath++], s, _MAX_PATH) >= _MAX_PATH) { + fatalerror("Include path too long '%s'",s); + return; + } } FILE * diff --git a/src/asm/globlex.c b/src/asm/globlex.c index cbf6235f..9f9523a8 100644 --- a/src/asm/globlex.c +++ b/src/asm/globlex.c @@ -97,7 +97,7 @@ ascii2bin(char *s) while (*s != '\0') { c = convertfunc(*s++); - result = result * 2 + ((c & 1) << 8) + ((c & 2) >> 1); + result = result * 2 + ((c & 2) << 7) + (c & 1); } } else { while (*s != '\0') diff --git a/src/asm/lexer.c b/src/asm/lexer.c index 4b2548f4..33dd0904 100644 --- a/src/asm/lexer.c +++ b/src/asm/lexer.c @@ -39,9 +39,8 @@ ULONG tFloatingChars[256]; ULONG nFloating; enum eLexerState lexerstate = LEX_STATE_NORMAL; -#ifdef __GNUC__ void -strupr(char *s) +upperstring(char *s) { while (*s) { *s = toupper(*s); @@ -50,14 +49,14 @@ strupr(char *s) } void -strlwr(char *s) +lowerstring(char *s) { while (*s) { *s = tolower(*s); s += 1; } } -#endif + void yyskipbytes(ULONG count) { @@ -352,7 +351,7 @@ lex_AddStrings(struct sLexInitString * lex) (*ppHash)->nToken = lex->nToken; (*ppHash)->pNext = NULL; - strupr((*ppHash)->tzName); + upperstring((*ppHash)->tzName); if ((*ppHash)->nNameLength > nLexMaxLength) nLexMaxLength = (*ppHash)->nNameLength; diff --git a/src/asm/main.c b/src/asm/main.c index 99a7cc39..cf7d97cc 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -257,7 +257,7 @@ static void usage(void) { printf( -"Usage: rgbasm [-hv] [-b chars] [-Dname[=value]] [-g chars] [-i path]\n" +"Usage: rgbasm [-hvE] [-b chars] [-Dname[=value]] [-g chars] [-i path]\n" " [-o outfile] [-p pad_value] file.asm\n"); exit(1); } @@ -296,12 +296,13 @@ main(int argc, char *argv[]) DefaultOptions.fillchar = 0; DefaultOptions.verbose = false; DefaultOptions.haltnop = true; + DefaultOptions.exportall = false; opt_SetCurrentOptions(&DefaultOptions); newopt = CurrentOptions; - while ((ch = getopt(argc, argv, "b:D:g:hi:o:p:v")) != -1) { + while ((ch = getopt(argc, argv, "b:D:g:hi:o:p:vE")) != -1) { switch (ch) { case 'b': if (strlen(optarg) == 2) { @@ -348,6 +349,9 @@ main(int argc, char *argv[]) case 'v': newopt.verbose = true; break; + case 'E': + newopt.exportall = true; + break; default: usage(); } @@ -379,6 +383,7 @@ main(int argc, char *argv[]) nPass = 1; nErrors = 0; sym_PrepPass1(); + sym_SetExportAll(CurrentOptions.exportall); fstk_Init(tzMainfile); opt_ParseDefines(); diff --git a/src/asm/output.c b/src/asm/output.c index ebe2a01f..d4147a1b 100644 --- a/src/asm/output.c +++ b/src/asm/output.c @@ -243,25 +243,27 @@ writesymbol(struct sSymbol * pSym, FILE * f) offset = 0; sectid = -1; type = SYM_IMPORT; - } else if (pSym->nType & SYMF_EXPORT) { - /* Symbol should be exported */ - strcpy(symname, pSym->tzName); - type = SYM_EXPORT; - offset = pSym->nValue; - if (pSym->nType & SYMF_CONST) - sectid = -1; - else - sectid = getsectid(pSym->pSection); } else { - /* Symbol is local to this file */ if (pSym->nType & SYMF_LOCAL) { strcpy(symname, pSym->pScope->tzName); strcat(symname, pSym->tzName); } else strcpy(symname, pSym->tzName); - type = SYM_LOCAL; - offset = pSym->nValue; - sectid = getsectid(pSym->pSection); + + if (pSym->nType & SYMF_EXPORT) { + /* Symbol should be exported */ + type = SYM_EXPORT; + offset = pSym->nValue; + if (pSym->nType & SYMF_CONST) + sectid = -1; + else + sectid = getsectid(pSym->pSection); + } else { + /* Symbol is local to this file */ + type = SYM_LOCAL; + offset = pSym->nValue; + sectid = getsectid(pSym->pSection); + } } fputstring(symname, f); @@ -282,6 +284,7 @@ addsymbol(struct sSymbol * pSym) struct PatchSymbol *pPSym, **ppPSym; static ULONG nextID = 0; ULONG hash; + hash = calchash(pSym->tzName); ppPSym = &(tHashedPatchSymbols[hash]); diff --git a/src/asm/rgbasm.1 b/src/asm/rgbasm.1 index 9434729a..8de28de8 100644 --- a/src/asm/rgbasm.1 +++ b/src/asm/rgbasm.1 @@ -6,7 +6,7 @@ .Nd Game Boy assembler .Sh SYNOPSIS .Nm rgbasm -.Op Fl hv +.Op Fl Ehv .Op Fl b Ar chars .Op Fl D Ar name Ns Op = Ns Ar value .Op Fl g Ar chars @@ -29,6 +29,8 @@ Add string symbol to the compiled source code. This is equivalent to .Cm EQUS .Qq Ar "value" in code. If a value is not specified, a value of 1 is given. +.It Fl E +Export all labels, including unreferenced and local labels. .It Fl g Ar chars Change the four characters used for binary constants. The defaults are 0123. @@ -67,6 +69,8 @@ and .Xr rgbfix 1 , .Xr rgblink 1 , .Xr rgbds 7 +.Pp +.Lk https://rednex.github.io/rgbds/asm.htm rgbasm assembly commands .Sh HISTORY .Nm was originally written by Carsten S\(/orensen as part of the ASMotor package, diff --git a/src/asm/rpn.c b/src/asm/rpn.c index 3a8dba27..fcbba7f8 100644 --- a/src/asm/rpn.c +++ b/src/asm/rpn.c @@ -114,14 +114,11 @@ void rpn_Bank(struct Expression * expr, char *tzSym) { if (!sym_isConstant(tzSym)) { - struct sSymbol *psym; - rpn_Reset(expr); - psym = sym_FindSymbol(tzSym); - if (nPass == 2 && psym == NULL) { - yyerror("'%s' not defined", tzSym); - } + /* Check that the symbol exists by evaluating and discarding the value. */ + sym_GetValue(tzSym); + expr->isReloc = 1; pushbyte(expr, RPN_BANK); while (*tzSym) @@ -316,6 +313,9 @@ rpn_DIV(struct Expression * expr, struct Expression * src1, struct Expression * src2) { joinexpr(); + if (src2->nVal == 0) { + fatalerror("division by zero"); + } expr->nVal = (expr->nVal / src2->nVal); pushbyte(expr, RPN_DIV); } @@ -325,6 +325,9 @@ rpn_MOD(struct Expression * expr, struct Expression * src1, struct Expression * src2) { joinexpr(); + if (src2->nVal == 0) { + fatalerror("division by zero"); + } expr->nVal = (expr->nVal % src2->nVal); pushbyte(expr, RPN_MOD); } diff --git a/src/asm/symbol.c b/src/asm/symbol.c index f75d041b..8e6fa982 100644 --- a/src/asm/symbol.c +++ b/src/asm/symbol.c @@ -21,6 +21,7 @@ char *currentmacroargs[MAXMACROARGS + 1]; char *newmacroargs[MAXMACROARGS + 1]; char SavedTIME[256]; char SavedDATE[256]; +bool exportall; SLONG Callback_NARG(struct sSymbol * sym) @@ -575,6 +576,9 @@ sym_AddLocalReloc(char *tzSym) nsym->nValue = nPC; nsym->nType |= SYMF_RELOC | SYMF_LOCAL | SYMF_DEFINED; + if (exportall) { + nsym->nType |= SYMF_EXPORT; + } nsym->pScope = pScope; nsym->pSection = pCurrentSection; } @@ -604,6 +608,9 @@ sym_AddReloc(char *tzSym) if (nsym) { nsym->nValue = nPC; nsym->nType |= SYMF_RELOC | SYMF_DEFINED; + if (exportall) { + nsym->nType |= SYMF_EXPORT; + } nsym->pScope = NULL; nsym->pSection = pCurrentSection; } @@ -709,6 +716,13 @@ sym_AddMacro(char *tzSym) } } +/* + * Set whether to export all relocable symbols by default + */ +void sym_SetExportAll(BBOOL set) { + exportall = set; +} + /* * Prepare for pass #1 */ diff --git a/src/gfx/gb.c b/src/gfx/gb.c new file mode 100644 index 00000000..cb8a6aa9 --- /dev/null +++ b/src/gfx/gb.c @@ -0,0 +1,203 @@ +/* + * Copyright © 2013 stag019 + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "gfx/main.h" + +void +transpose_tiles(struct GBImage *gb, int width) +{ + uint8_t *newdata; + int i; + int newbyte; + + newdata = calloc(gb->size, 1); + for (i = 0; i < gb->size; i++) { + newbyte = i / (8 * depth) * width * 8 * depth; + newbyte = newbyte % gb->size + 8 * depth * (newbyte / gb->size) + i % (8 * depth); + newdata[newbyte] = gb->data[i]; + } + + free(gb->data); + + gb->data = newdata; +} + +void +png_to_gb(struct PNGImage png, struct GBImage *gb) +{ + int x, y, byte; + png_byte index; + + for (y = 0; y < png.height; y++) { + for (x = 0; x < png.width; x++) { + index = png.data[y][x]; + index &= (1 << depth) - 1; + + if (!gb->horizontal) { + byte = y * depth + x / 8 * png.height / 8 * 8 * depth; + } else { + byte = y * depth + x / 8 * png.height / 8 * 8 * depth; + } + gb->data[byte] |= (index & 1) << (7 - x % 8); + if (depth == 2) { + gb->data[byte + 1] |= (index >> 1) << (7 - x % 8); + } + } + } + + if (!gb->horizontal) { + transpose_tiles(gb, png.width / 8); + } +} + +void +output_file(struct Options opts, struct GBImage gb) +{ + FILE *f; + + f = fopen(opts.outfile, "wb"); + if (!f) { + err(1, "Opening output file '%s' failed", opts.outfile); + } + fwrite(gb.data, 1, gb.size - gb.trim * 8 * depth, f); + + fclose(f); +} + +int +get_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles, int tile_size) +{ + int i, j; + for (i = 0; i < num_tiles; i++) { + for (j = 0; j < tile_size; j++) { + if (tile[j] != tiles[i][j]) { + break; + } + } + if (j >= tile_size) { + return i; + } + } + return -1; +} + +void +create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap) +{ + int i, j; + int gb_i; + int tile_size; + int max_tiles; + int num_tiles; + int index; + int gb_size; + uint8_t *tile; + uint8_t **tiles; + + tile_size = sizeof(uint8_t) * depth * 8; + gb_size = gb->size - (gb->trim * tile_size); + max_tiles = gb_size / tile_size; + tiles = malloc(sizeof(uint8_t*) * max_tiles); + num_tiles = 0; + + tilemap->data = malloc(sizeof(uint8_t) * max_tiles); + tilemap->size = 0; + + gb_i = 0; + while (gb_i < gb_size) { + tile = malloc(tile_size); + for (i = 0; i < tile_size; i++) { + tile[i] = gb->data[gb_i]; + gb_i++; + } + if (opts.unique) { + index = get_tile_index(tile, tiles, num_tiles, tile_size); + if (index < 0) { + index = num_tiles; + tiles[num_tiles] = tile; + num_tiles++; + } + } else { + index = num_tiles; + tiles[num_tiles] = tile; + num_tiles++; + } + tilemap->data[tilemap->size] = index; + tilemap->size++; + } + + if (opts.unique) { + free(gb->data); + gb->data = malloc(tile_size * num_tiles); + for (i = 0; i < num_tiles; i++) { + tile = tiles[i]; + for (j = 0; j < tile_size; j++) { + gb->data[i * tile_size + j] = tile[j]; + } + } + gb->size = i * tile_size; + } + + for (i = 0; i < num_tiles; i++) { + free(tiles[i]); + } + free(tiles); +} + +void +output_tilemap_file(struct Options opts, struct Tilemap tilemap) +{ + FILE *f; + + f = fopen(opts.mapfile, "wb"); + if (!f) { + err(1, "Opening tilemap file '%s' failed", opts.mapfile); + } + + fwrite(tilemap.data, 1, tilemap.size, f); + fclose(f); + + if (opts.mapout) { + free(opts.mapfile); + } +} + +void +output_palette_file(struct Options opts, struct PNGImage png) +{ + FILE *f; + int i, colors, color; + png_color *palette; + + if (png_get_PLTE(png.png, png.info, &palette, &colors)) { + f = fopen(opts.palfile, "wb"); + if (!f) { + err(1, "Opening palette file '%s' failed", opts.palfile); + } + for (i = 0; i < colors; i++) { + color = palette[i].blue >> 3 << 10 | palette[i].green >> 3 << 5 | palette[i].red >> 3; + fwrite(&color, 2, 1, f); + } + fclose(f); + } + + if (opts.palout) { + free(opts.palfile); + } +} diff --git a/src/gfx/main.c b/src/gfx/main.c new file mode 100644 index 00000000..2496eaf7 --- /dev/null +++ b/src/gfx/main.c @@ -0,0 +1,255 @@ +/* + * Copyright © 2013 stag019 + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include "gfx/main.h" + +char *progname; + +static void +usage(void) +{ + printf( +"usage: rgbgfx [-DFfhPTuv] [-d #] [-o outfile] [-p palfile] [-t mapfile]\n" +"[-x #] infile\n"); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + int ch, size; + struct Options opts = {0}; + struct PNGImage png = {0}; + struct GBImage gb = {0}; + struct Tilemap tilemap = {0}; + char *ext; + const char *errmsg = "Warning: The PNG's %s setting is not the same as the setting defined on the command line."; + + progname = argv[0]; + + if (argc == 1) { + usage(); + } + + opts.mapfile = ""; + opts.palfile = ""; + opts.outfile = ""; + + depth = 2; + + while((ch = getopt(argc, argv, "DvFfd:hx:Tt:uPp:o:")) != -1) { + switch(ch) { + case 'D': + opts.debug = true; + break; + case 'v': + opts.verbose = true; + break; + case 'F': + opts.hardfix = true; + case 'f': + opts.fix = true; + break; + case 'd': + depth = strtoul(optarg, NULL, 0); + break; + case 'h': + opts.horizontal = true; + break; + case 'x': + opts.trim = strtoul(optarg, NULL, 0); + break; + case 'T': + opts.mapout = true; + break; + case 't': + opts.mapfile = optarg; + break; + case 'u': + opts.unique = true; + break; + case 'P': + opts.palout = true; + break; + case 'p': + opts.palfile = optarg; + break; + case 'o': + opts.outfile = optarg; + break; + default: + usage(); + } + } + argc -= optind; + argv += optind; + + if (argc == 0) { + usage(); + } + + opts.infile = argv[argc - 1]; + + if (depth != 1 && depth != 2) { + errx(1, "Depth option must be either 1 or 2."); + } + colors = 1 << depth; + + input_png_file(opts, &png); + + png.mapfile = ""; + png.palfile = ""; + + get_text(&png); + + if (png.horizontal != opts.horizontal) { + if (opts.verbose) { + warnx(errmsg, "horizontal"); + } + if (opts.hardfix) { + png.horizontal = opts.horizontal; + } + } + if (png.horizontal) { + opts.horizontal = png.horizontal; + } + + if (png.trim != opts.trim) { + if (opts.verbose) { + warnx(errmsg, "trim"); + } + if (opts.hardfix) { + png.trim = opts.trim; + } + } + if (png.trim) { + opts.trim = png.trim; + } + if (opts.trim > png.width / 8 - 1) { + errx(1, "Trim (%i) for input png file '%s' too large (max: %i)", opts.trim, opts.infile, png.width / 8 - 1); + } + + if (strcmp(png.mapfile, opts.mapfile) != 0) { + if (opts.verbose) { + warnx(errmsg, "tilemap file"); + } + if (opts.hardfix) { + png.mapfile = opts.mapfile; + } + } + if (!*opts.mapfile) { + opts.mapfile = png.mapfile; + } + + if (png.mapout != opts.mapout) { + if (opts.verbose) { + warnx(errmsg, "tilemap file"); + } + if (opts.hardfix) { + png.mapout = opts.mapout; + } + } + if (png.mapout) { + opts.mapout = png.mapout; + } + + if (strcmp(png.palfile, opts.palfile) != 0) { + if (opts.verbose) { + warnx(errmsg, "palette file"); + } + if (opts.hardfix) { + png.palfile = opts.palfile; + } + } + if (!*opts.palfile) { + opts.palfile = png.palfile; + } + + if (png.palout != opts.palout) { + if (opts.verbose) { + warnx(errmsg, "palette file"); + } + if (opts.hardfix) { + png.palout = opts.palout; + } + } + if (png.palout) { + opts.palout = png.palout; + } + + if (!*opts.mapfile && opts.mapout) { + if ((ext = strrchr(opts.infile, '.')) != NULL) { + size = ext - opts.infile + 9; + opts.mapfile = malloc(size); + strncpy(opts.mapfile, opts.infile, size); + *strrchr(opts.mapfile, '.') = '\0'; + strcat(opts.mapfile, ".tilemap"); + } else { + opts.mapfile = malloc(strlen(opts.infile) + 9); + strcpy(opts.mapfile, opts.infile); + strcat(opts.mapfile, ".tilemap"); + } + } + + if (!*opts.palfile && opts.palout) { + if ((ext = strrchr(opts.infile, '.')) != NULL) { + size = ext - opts.infile + 5; + opts.palfile = malloc(size); + strncpy(opts.palfile, opts.infile, size); + *strrchr(opts.palfile, '.') = '\0'; + strcat(opts.palfile, ".pal"); + } else { + opts.palfile = malloc(strlen(opts.infile) + 5); + strcpy(opts.palfile, opts.infile); + strcat(opts.palfile, ".pal"); + } + } + + gb.size = png.width * png.height * depth / 8; + gb.data = calloc(gb.size, 1); + gb.trim = opts.trim; + gb.horizontal = opts.horizontal; + + if (*opts.outfile || *opts.mapfile) { + png_to_gb(png, &gb); + create_tilemap(opts, &gb, &tilemap); + } + + if (*opts.outfile) { + output_file(opts, gb); + } + + if (*opts.mapfile) { + output_tilemap_file(opts, tilemap); + } + + if (*opts.palfile) { + output_palette_file(opts, png); + } + + if (opts.fix || opts.debug) { + set_text(&png); + output_png_file(opts, &png); + } + + free_png_data(&png); + free(gb.data); + + return 0; +} diff --git a/src/gfx/makepng.c b/src/gfx/makepng.c new file mode 100644 index 00000000..d5f81912 --- /dev/null +++ b/src/gfx/makepng.c @@ -0,0 +1,339 @@ +/* + * Copyright © 2013 stag019 + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include "gfx/main.h" + +void +input_png_file(struct Options opts, struct PNGImage *img) +{ + FILE *f; + int i, y, num_trans; + bool has_palette = false; + png_byte *trans_alpha; + png_color_16 *trans_values; + bool *full_alpha; + png_color *palette; + + f = fopen(opts.infile, "rb"); + if (!f) { + err(1, "Opening input png file '%s' failed", opts.infile); + } + + img->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!img->png) { + errx(1, "Creating png structure failed"); + } + + img->info = png_create_info_struct(img->png); + if (!img->info) { + errx(1, "Creating png info structure failed"); + } + + /* Better error handling here? */ + if (setjmp(png_jmpbuf(img->png))) { + exit(1); + } + + png_init_io(img->png, f); + + png_read_info(img->png, img->info); + + img->width = png_get_image_width(img->png, img->info); + img->height = png_get_image_height(img->png, img->info); + img->depth = png_get_bit_depth(img->png, img->info); + img->type = png_get_color_type(img->png, img->info); + + if (img->type & PNG_COLOR_MASK_ALPHA) { + png_set_strip_alpha(img->png); + } + + if (img->depth != depth) { + if (opts.verbose) { + warnx("Image bit depth is not %i (is %i).", depth, + img->depth); + } + } + + if (img->type == PNG_COLOR_TYPE_GRAY) { + if (img->depth < 8) { + png_set_expand_gray_1_2_4_to_8(img->png); + } + png_set_gray_to_rgb(img->png); + } else { + if (img->depth < 8) { + png_set_expand_gray_1_2_4_to_8(img->png); + } + has_palette = png_get_PLTE(img->png, img->info, &palette, + &colors); + } + + if (png_get_tRNS(img->png, img->info, &trans_alpha, &num_trans, + &trans_values)) { + if (img->type == PNG_COLOR_TYPE_PALETTE) { + full_alpha = malloc(sizeof(bool) * num_trans); + + for (i = 0; i < num_trans; i++) { + if (trans_alpha[i] > 0) { + full_alpha[i] = false; + } else { + full_alpha[i] = true; + } + } + + for (i = 0; i < num_trans; i++) { + if (full_alpha[i]) { + palette[i].red = 0xFF; + palette[i].green = 0x00; + palette[i].blue = 0xFF; + /* + * Set to the lightest color in the + * palette. + */ + } + } + + free(full_alpha); + } else { + /* Set to the lightest color in the image. */ + } + + png_free_data(img->png, img->info, PNG_FREE_TRNS, -1); + } + + if (has_palette) { + /* Make sure palette only has the amount of colors you want. */ + } else { + /* + * Eventually when this copies colors from the image itself, + * make sure order is lightest to darkest. + */ + palette = malloc(sizeof(png_color) * colors); + + if (strcmp(opts.infile, "rgb.png") == 0) { + palette[0].red = 0xFF; + palette[0].green = 0xEF; + palette[0].blue = 0xFF; + + palette[1].red = 0xF7; + palette[1].green = 0xF7; + palette[1].blue = 0x8C; + + palette[2].red = 0x94; + palette[2].green = 0x94; + palette[2].blue = 0xC6; + + palette[3].red = 0x39; + palette[3].green = 0x39; + palette[3].blue = 0x84; + } else { + palette[0].red = 0xFF; + palette[0].green = 0xFF; + palette[0].blue = 0xFF; + + palette[1].red = 0xA9; + palette[1].green = 0xA9; + palette[1].blue = 0xA9; + + palette[2].red = 0x55; + palette[2].green = 0x55; + palette[2].blue = 0x55; + + palette[3].red = 0x00; + palette[3].green = 0x00; + palette[3].blue = 0x00; + } + } + + /* + * Also unfortunately, this sets it at 8 bit, and I can't find any + * option to reduce to 2 or 1 bit. + */ +#if PNG_LIBPNG_VER < 10402 + png_set_dither(img->png, palette, colors, colors, NULL, 1); +#else + png_set_quantize(img->png, palette, colors, colors, NULL, 1); +#endif + + if (!has_palette) { + png_set_PLTE(img->png, img->info, palette, colors); + free(palette); + } + + /* + * If other useless chunks exist (sRGB, bKGD, pHYs, gAMA, cHRM, iCCP, + * etc.) offer to remove? + */ + + png_read_update_info(img->png, img->info); + + img->data = malloc(sizeof(png_byte *) * img->height); + for (y = 0; y < img->height; y++) { + img->data[y] = malloc(png_get_rowbytes(img->png, img->info)); + } + + png_read_image(img->png, img->data); + png_read_end(img->png, img->info); + + fclose(f); +} + +void +get_text(struct PNGImage *png) +{ + png_text *text; + int i, numtxts, numremoved; + + png_get_text(png->png, png->info, &text, &numtxts); + for (i = 0; i < numtxts; i++) { + if (strcmp(text[i].key, "h") == 0 && !*text[i].text) { + png->horizontal = true; + png_free_data(png->png, png->info, PNG_FREE_TEXT, i); + } else if (strcmp(text[i].key, "x") == 0) { + png->trim = strtoul(text[i].text, NULL, 0); + png_free_data(png->png, png->info, PNG_FREE_TEXT, i); + } else if (strcmp(text[i].key, "t") == 0) { + png->mapfile = text[i].text; + png_free_data(png->png, png->info, PNG_FREE_TEXT, i); + } else if (strcmp(text[i].key, "T") == 0 && !*text[i].text) { + png->mapout = true; + png_free_data(png->png, png->info, PNG_FREE_TEXT, i); + } else if (strcmp(text[i].key, "p") == 0) { + png->palfile = text[i].text; + png_free_data(png->png, png->info, PNG_FREE_TEXT, i); + } else if (strcmp(text[i].key, "P") == 0 && !*text[i].text) { + png->palout = true; + png_free_data(png->png, png->info, PNG_FREE_TEXT, i); + } + } + + /* TODO: Remove this and simply change the warning function not to warn instead. */ + for (i = 0, numremoved = 0; i < numtxts; i++) { + if (text[i].key == NULL) { + numremoved++; + } + text[i].key = text[i + numremoved].key; + text[i].text = text[i + numremoved].text; + text[i].compression = text[i + numremoved].compression; + } + png_set_text(png->png, png->info, text, numtxts - numremoved); +} + +void +set_text(struct PNGImage *png) +{ + png_text *text; + char buffer[3]; + + text = malloc(sizeof(png_text)); + + if (png->horizontal) { + text[0].key = "h"; + text[0].text = ""; + text[0].compression = PNG_TEXT_COMPRESSION_NONE; + png_set_text(png->png, png->info, text, 1); + } + if (png->trim) { + text[0].key = "x"; + snprintf(buffer, 3, "%d", png->trim); + text[0].text = buffer; + text[0].compression = PNG_TEXT_COMPRESSION_NONE; + png_set_text(png->png, png->info, text, 1); + } + if (*png->mapfile) { + text[0].key = "t"; + text[0].text = ""; + text[0].compression = PNG_TEXT_COMPRESSION_NONE; + png_set_text(png->png, png->info, text, 1); + } + if (png->mapout) { + text[0].key = "T"; + text[0].text = ""; + text[0].compression = PNG_TEXT_COMPRESSION_NONE; + png_set_text(png->png, png->info, text, 1); + } + if (*png->palfile) { + text[0].key = "p"; + text[0].text = ""; + text[0].compression = PNG_TEXT_COMPRESSION_NONE; + png_set_text(png->png, png->info, text, 1); + } + if (png->palout) { + text[0].key = "P"; + text[0].text = ""; + text[0].compression = PNG_TEXT_COMPRESSION_NONE; + png_set_text(png->png, png->info, text, 1); + } + + free(text); +} + +void +output_png_file(struct Options opts, struct PNGImage *png) +{ + FILE *f; + char *outfile; + png_struct *img; + + /* Variable outfile is for debugging purposes. Eventually, opts.infile will be used directly. */ + if (opts.debug) { + outfile = malloc(strlen(opts.infile) + 5); + strcpy(outfile, opts.infile); + strcat(outfile, ".out"); + } else { + outfile = opts.infile; + } + + f = fopen(outfile, "wb"); + if (!f) { + err(1, "Opening output png file '%s' failed", outfile); + } + + img = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!img) { + errx(1, "Creating png structure failed"); + } + + /* Better error handling here? */ + if (setjmp(png_jmpbuf(img))) { + exit(1); + } + + png_init_io(img, f); + + png_write_info(img, png->info); + + png_write_image(img, png->data); + png_write_end(img, NULL); + + fclose(f); + + if (opts.debug) { + free(outfile); + } +} + +void +free_png_data(struct PNGImage *png) +{ + int y; + + for (y = 0; y < png->height; y++) { + free(png->data[y]); + } + free(png->data); +} diff --git a/src/gfx/rgbgfx.1 b/src/gfx/rgbgfx.1 new file mode 100644 index 00000000..ec3531bd --- /dev/null +++ b/src/gfx/rgbgfx.1 @@ -0,0 +1,90 @@ +.Dd $Mdocdate$ +.Dt RGBGFX 1 +.Os RGBDS Manual +.Sh NAME +.Nm rgbgfx +.Nd Game Boy graphics converter +.Sh SYNOPSIS +.Nm rgbgfx +.Op Fl DfFhPTv +.Op Fl o Ar outfile +.Op Fl d Ar depth +.Op Fl p Ar palfile +.Op Fl t Ar mapfile +.Op Fl x Ar tiles +.Ar file +.Sh DESCRIPTION +The +.Nm +program converts PNG images into the Nintendo Game Boy's planar tile format. +The arguments are as follows: +.Bl -tag -width Ds +.It Fl D +Debug features are enabled. +.It Fl f +Fix the input PNG file to be a correctly indexed image. +.It Fl F +Same as +.Fl f , +but additionally, the input PNG file is fixed to have its parameters match the +command line's parameters. +.It Fl d Ar depth +The bitdepth of the output image (either 1 or 2). +By default, the bitdepth is 2 (two bits per pixel). +.It Fl h +Lay out tiles horizontally rather than vertically. +.It Fl o Ar outfile +The name of the output file. +.It Fl p Ar palfile +Raw bytes (8 bytes for two bits per pixel, 4 bytes for one bit per pixel) +containing the RGB15 values in the little-endian byte order and then ordered +from lightest to darkest. +.It Fl P +Same as +.Fl p , +but the pallete file output name is made by taking the input filename, +removing the file extension, and appending +.Pa .pal . +.It Fl t Ar mapfile +If any tiles are the same, don't place the repeat tiles in the output file, and +make a tilemap file. +.It Fl T +Same as +.Fl t , +but the tilemap file output name is made by taking the input filename, +removing the file extension, and appending +.Pa .tilemap . +.It Fl u +Truncate repeated tiles. Useful with tilemaps. +.It Fl v +Verbose. +Print errors when the command line parameters and the parameters in +the PNG file don't match. +.It Fl x Ar tiles +Trim the end of the output file by this many tiles. +.El +.Sh EXAMPLES +The following will take a PNG file with a bitdepth of 1, 2, or 8, and output +planar 2bpp data: +.Pp +.D1 $ rgbgfx -o out.2bpp in.png +.Pp +The following creates a planar 2bpp file with only unique tiles, and its tilemap +.Pa out.tilemap : +.Pp +.D1 $ rgbgfx -T -u -o out.2bpp in.png +.Pp +The following will do nothing: +.Pp +.D1 $ rgbgfx in.png +.Sh SEE ALSO +.Xr rgbds 7 , +.Xr rgbasm 1 , +.Xr rgblink 1 , +.Xr rgbfix 1 , +.Xr gbz80 7 +.Sh HISTORY +.Nm +was created by +.An stag019 +to be included in RGBDS. diff --git a/src/link/assign.c b/src/link/assign.c index c5bd0961..14ff1206 100644 --- a/src/link/assign.c +++ b/src/link/assign.c @@ -1,5 +1,6 @@ #include #include +#include #include "extern/err.h" #include "link/mylink.h" @@ -13,6 +14,14 @@ struct sFreeArea { struct sFreeArea *pPrev, *pNext; }; +struct sSectionAttributes { + const char *name; + SLONG bank; + SLONG offset; // bank + offset = bank originally stored in a section struct + SLONG minBank; + SLONG bankCount; +}; + struct sFreeArea *BankFree[MAXBANKS]; SLONG MaxAvail[MAXBANKS]; SLONG MaxBankUsed; @@ -20,11 +29,37 @@ SLONG MaxWBankUsed; SLONG MaxSBankUsed; SLONG MaxVBankUsed; -#define DOMAXBANK(x) {if( (x)>MaxBankUsed ) MaxBankUsed=(x);} +const enum eSectionType SECT_MIN = SECT_WRAM0; +const enum eSectionType SECT_MAX = SECT_SRAM; +const struct sSectionAttributes SECT_ATTRIBUTES[] = { + {"WRAM0", BANK_WRAM0, 0, 0, BANK_COUNT_WRAM0}, + {"VRAM", BANK_VRAM, 0, 0, BANK_COUNT_VRAM}, + {"ROMX", BANK_ROMX, -1, 1, BANK_COUNT_ROMX}, + {"ROM0", BANK_ROM0, 0, 0, BANK_COUNT_ROM0}, + {"HRAM", BANK_HRAM, 0, 0, BANK_COUNT_HRAM}, + {"WRAMX", BANK_WRAMX, 0, 0, BANK_COUNT_WRAMX}, + {"SRAM", BANK_SRAM, 0, 0, BANK_COUNT_SRAM} +}; + +#define DOMAXBANK(x, y) {switch (x) { \ + case SECT_ROMX: DOMAXRBANK(y); break; \ + case SECT_WRAMX: DOMAXWBANK(y); break; \ + case SECT_SRAM: DOMAXSBANK(y); break; \ + case SECT_VRAM: DOMAXVBANK(y); break; \ + default: errx(1, "DOMAXBANK used with invalid parameters"); break; }} +#define DOMAXRBANK(x) {if( (x)>MaxBankUsed ) MaxBankUsed=(x);} #define DOMAXWBANK(x) {if( (x)>MaxWBankUsed ) MaxWBankUsed=(x);} #define DOMAXSBANK(x) {if( (x)>MaxSBankUsed ) MaxSBankUsed=(x);} #define DOMAXVBANK(x) {if( (x)>MaxVBankUsed ) MaxVBankUsed=(x);} +void +ensureSectionTypeIsValid(enum eSectionType type) +{ + if (type < SECT_MIN || type > SECT_MAX) { + errx(1, "(INTERNAL) Invalid section type found."); + } +} + SLONG area_Avail(SLONG bank) { @@ -42,7 +77,7 @@ area_Avail(SLONG bank) return (r); } -SLONG +SLONG area_AllocAbs(struct sFreeArea ** ppArea, SLONG org, SLONG size) { struct sFreeArea *pArea; @@ -54,12 +89,12 @@ area_AllocAbs(struct sFreeArea ** ppArea, SLONG org, SLONG size) if (org == pArea->nOrg) { pArea->nOrg += size; pArea->nSize -= size; - return (org); + return 0; } else { if ((org + size - 1) == (pArea->nOrg + pArea->nSize - 1)) { pArea->nSize -= size; - return (org); + return 0; } else { struct sFreeArea *pNewArea; @@ -75,7 +110,7 @@ area_AllocAbs(struct sFreeArea ** ppArea, SLONG org, SLONG size) pNewArea->nSize -= size + pArea->nSize; - return (org); + return 0; } else { err(1, NULL); } @@ -86,62 +121,26 @@ area_AllocAbs(struct sFreeArea ** ppArea, SLONG org, SLONG size) pArea = *ppArea; } - return (-1); + return -1; } SLONG -area_AllocAbsSRAMAnyBank(SLONG org, SLONG size) +area_AllocAbsAnyBank(SLONG org, SLONG size, enum eSectionType type) { - int i; - for (i = 0; i < 4; ++i) { - if (area_AllocAbs(&BankFree[BANK_SRAM + i], org, size) == org) { - return BANK_SRAM + i; + ensureSectionTypeIsValid(type); + + SLONG startBank = SECT_ATTRIBUTES[type].bank; + SLONG bankCount = SECT_ATTRIBUTES[type].bankCount; + + for (int i = 0; i < bankCount; i++) { + if (area_AllocAbs(&BankFree[startBank + i], org, size) != -1) { + return startBank + i; } } return -1; } -SLONG -area_AllocAbsWRAMAnyBank(SLONG org, SLONG size) -{ - SLONG i; - - for (i = 1; i <= 7; i += 1) { - if (area_AllocAbs(&BankFree[BANK_WRAMX + i - 1], org, size) == org) { - return BANK_WRAMX + i - 1; - } - } - - return -1; -} - -SLONG -area_AllocAbsVRAMAnyBank(SLONG org, SLONG size) -{ - if (area_AllocAbs(&BankFree[BANK_VRAM], org, size) == org) { - return BANK_VRAM; - } - if (area_AllocAbs(&BankFree[BANK_VRAM + 1], org, size) == org) { - return BANK_VRAM + 1; - } - - return -1; -} - -SLONG -area_AllocAbsROMXAnyBank(SLONG org, SLONG size) -{ - SLONG i; - - for (i = 1; i <= 511; i += 1) { - if (area_AllocAbs(&BankFree[i], org, size) == org) - return (i); - } - - return (-1); -} - SLONG area_Alloc(struct sFreeArea ** ppArea, SLONG size) { @@ -166,67 +165,31 @@ area_Alloc(struct sFreeArea ** ppArea, SLONG size) } SLONG -area_AllocVRAMAnyBank(SLONG size) -{ - SLONG i, org; +area_AllocAnyBank(SLONG size, enum eSectionType type) { + ensureSectionTypeIsValid(type); - for (i = BANK_VRAM; i <= BANK_VRAM + 1; i += 1) { - if ((org = area_Alloc(&BankFree[i], size)) != -1) - return ((i << 16) | org); - } - - return (-1); -} - -SLONG -area_AllocSRAMAnyBank(SLONG size) -{ - SLONG i, org; - for (i = 0; i < 4; ++i) { - if ((org = area_Alloc(&BankFree[BANK_SRAM + i], size)) != -1) { - return (i << 16) | org; + SLONG startBank = SECT_ATTRIBUTES[type].bank; + SLONG bankCount = SECT_ATTRIBUTES[type].bankCount; + + for (int i = 0; i < bankCount; i++) { + SLONG org = area_Alloc(&BankFree[startBank + i], size); + if (org != -1) { + return ((startBank + i) << 16) | org; } } return -1; } -SLONG -area_AllocWRAMAnyBank(SLONG size) -{ - SLONG i, org; - - for (i = 1; i <= 7; i += 1) { - if ((org = area_Alloc(&BankFree[BANK_WRAMX + i - 1], size)) != -1) { - return (i << 16) | org; - } - } - - return -1; -} - -SLONG -area_AllocROMXAnyBank(SLONG size) -{ - SLONG i, org; - - for (i = 1; i <= 511; i += 1) { - if ((org = area_Alloc(&BankFree[i], size)) != -1) - return ((i << 16) | org); - } - - return (-1); -} - struct sSection * -FindLargestWRAM(void) +FindLargestSection(enum eSectionType type) { struct sSection *pSection, *r = NULL; SLONG nLargest = 0; pSection = pSections; while (pSection) { - if (pSection->oAssigned == 0 && pSection->Type == SECT_WRAMX) { + if (pSection->oAssigned == 0 && pSection->Type == type) { if (pSection->nByteSize > nLargest) { nLargest = pSection->nByteSize; r = pSection; @@ -237,142 +200,44 @@ FindLargestWRAM(void) return r; } -struct sSection * -FindLargestVRAM(void) -{ - struct sSection *pSection, *r = NULL; - SLONG nLargest = 0; - - pSection = pSections; - while (pSection) { - if (pSection->oAssigned == 0 && pSection->Type == SECT_VRAM) { - if (pSection->nByteSize > nLargest) { - nLargest = pSection->nByteSize; - r = pSection; - } - } - pSection = pSection->pNext; - } - return r; -} - -struct sSection * -FindLargestSRAM(void) -{ - struct sSection *pSection, *r = NULL; - SLONG nLargest = 0; - - pSection = pSections; - while (pSection) { - if (pSection->oAssigned == 0 && pSection->Type == SECT_SRAM) { - if (pSection->nByteSize > nLargest) { - nLargest = pSection->nByteSize; - r = pSection; - } - } - pSection = pSection->pNext; - } - return r; -} - -struct sSection * -FindLargestCode(void) -{ - struct sSection *pSection, *r = NULL; - SLONG nLargest = 0; - - pSection = pSections; - while (pSection) { - if (pSection->oAssigned == 0 && pSection->Type == SECT_ROMX) { - if (pSection->nByteSize > nLargest) { - nLargest = pSection->nByteSize; - r = pSection; - } - } - pSection = pSection->pNext; - } - return (r); -} - void -AssignVRAMSections(void) +AssignBankedSections(enum eSectionType type) { + ensureSectionTypeIsValid(type); + struct sSection *pSection; - while ((pSection = FindLargestVRAM())) { + while ((pSection = FindLargestSection(type))) { SLONG org; - if ((org = area_AllocVRAMAnyBank(pSection->nByteSize)) != -1) { + if ((org = area_AllocAnyBank(pSection->nByteSize, type)) != -1) { pSection->nOrg = org & 0xFFFF; pSection->nBank = org >> 16; pSection->oAssigned = 1; - DOMAXVBANK(pSection->nBank); + DOMAXBANK(pSection->Type, pSection->nBank); } else { - errx(1, "Unable to place VRAM section anywhere"); + errx(1, "Unable to place %s section anywhere", + SECT_ATTRIBUTES[type].name); } } } +bool +VerifyAndSetBank(struct sSection *pSection) +{ + ensureSectionTypeIsValid(pSection->Type); + + if (pSection->nBank >= SECT_ATTRIBUTES[pSection->Type].minBank + && pSection->nBank < SECT_ATTRIBUTES[pSection->Type].minBank + SECT_ATTRIBUTES[pSection->Type].bankCount) { + pSection->nBank += SECT_ATTRIBUTES[pSection->Type].bank + SECT_ATTRIBUTES[pSection->Type].offset; + return true; + + } else { + return false; + } +} + void -AssignSRAMSections(void) -{ - struct sSection *pSection; - - while ((pSection = FindLargestSRAM())) { - SLONG org; - - if ((org = area_AllocSRAMAnyBank(pSection->nByteSize)) != -1) { - pSection->nOrg = org & 0xFFFF; - pSection->nBank = org >> 16; - pSection->nBank += BANK_SRAM; - pSection->oAssigned = 1; - DOMAXSBANK(pSection->nBank); - } else { - errx(1, "Unable to place SRAM section anywhere"); - } - } -} - -void -AssignWRAMSections(void) -{ - struct sSection *pSection; - - while ((pSection = FindLargestWRAM())) { - SLONG org; - - if ((org = area_AllocWRAMAnyBank(pSection->nByteSize)) != -1) { - pSection->nOrg = org & 0xFFFF; - pSection->nBank = org >> 16; - pSection->nBank += BANK_WRAMX - 1; - pSection->oAssigned = 1; - DOMAXWBANK(pSection->nBank); - } else { - errx(1, "Unable to place WRAMX section anywhere"); - } - } -} - -void -AssignCodeSections(void) -{ - struct sSection *pSection; - - while ((pSection = FindLargestCode())) { - SLONG org; - - if ((org = area_AllocROMXAnyBank(pSection->nByteSize)) != -1) { - pSection->nOrg = org & 0xFFFF; - pSection->nBank = org >> 16; - pSection->oAssigned = 1; - DOMAXBANK(pSection->nBank); - } else { - errx(1, "Unable to place ROMX section anywhere"); - } - } -} - -void AssignSections(void) { SLONG i; @@ -392,17 +257,15 @@ AssignSections(void) err(1, NULL); } - if (i == 0) { + if (i == BANK_ROM0) { /* ROM0 bank */ BankFree[i]->nOrg = 0x0000; if (options & OPT_SMALL) { BankFree[i]->nSize = 0x8000; - MaxAvail[i] = 0x8000; } else { BankFree[i]->nSize = 0x4000; - MaxAvail[i] = 0x4000; } - } else if (i >= 1 && i <= 511) { + } else if (i >= BANK_ROMX && i < BANK_ROMX + BANK_COUNT_ROMX) { /* Swappable ROM bank */ BankFree[i]->nOrg = 0x4000; /* @@ -411,37 +274,34 @@ AssignSections(void) */ if (options & OPT_SMALL) { BankFree[i]->nSize = 0; - MaxAvail[i] = 0; } else { BankFree[i]->nSize = 0x4000; - MaxAvail[i] = 0x4000; } } else if (i == BANK_WRAM0) { /* WRAM */ BankFree[i]->nOrg = 0xC000; BankFree[i]->nSize = 0x1000; - MaxAvail[i] = 0x1000; - } else if (i >= BANK_SRAM && i <= BANK_SRAM + 3) { + } else if (i >= BANK_SRAM && i < BANK_SRAM + BANK_COUNT_SRAM) { /* Swappable SRAM bank */ BankFree[i]->nOrg = 0xA000; BankFree[i]->nSize = 0x2000; - MaxAvail[i] = 0x2000; - } else if (i >= BANK_WRAMX && i <= BANK_WRAMX + 6) { + } else if (i >= BANK_WRAMX && i < BANK_WRAMX + BANK_COUNT_WRAMX) { /* Swappable WRAM bank */ BankFree[i]->nOrg = 0xD000; BankFree[i]->nSize = 0x1000; - MaxAvail[i] = 0x1000; - } else if (i == BANK_VRAM || i == BANK_VRAM + 1) { + } else if (i >= BANK_VRAM && i < BANK_VRAM + BANK_COUNT_VRAM) { /* Swappable VRAM bank */ BankFree[i]->nOrg = 0x8000; BankFree[i]->nSize = 0x2000; - MaxAvail[i] = 0x2000; } else if (i == BANK_HRAM) { /* HRAM */ BankFree[i]->nOrg = 0xFF80; BankFree[i]->nSize = 0x007F; - MaxAvail[i] = 0x007F; + } else { + errx(1, "(INTERNAL) Unknown bank type!"); } + + MaxAvail[i] = BankFree[i]->nSize; BankFree[i]->pPrev = NULL; BankFree[i]->pNext = NULL; } @@ -460,241 +320,31 @@ AssignSections(void) switch (pSection->Type) { case SECT_WRAM0: - if (area_AllocAbs - (&BankFree[BANK_WRAM0], pSection->nOrg, - pSection->nByteSize) != pSection->nOrg) { - errx(1, - "Unable to load fixed WRAM0 " - "section at $%lX", pSection->nOrg); - } - pSection->oAssigned = 1; - pSection->nBank = BANK_WRAM0; - break; case SECT_HRAM: - if (area_AllocAbs - (&BankFree[BANK_HRAM], pSection->nOrg, - pSection->nByteSize) != pSection->nOrg) { - errx(1, "Unable to load fixed HRAM " - "section at $%lX", pSection->nOrg); - } - pSection->oAssigned = 1; - pSection->nBank = BANK_HRAM; - break; - case SECT_SRAM: - if (pSection->nBank == -1) { - /* - * User doesn't care which bank. - * Therefore he must here be specifying - * position within the bank. - * Defer until later. - */ - ; - } else { - /* - * User specified which bank to use. - * Does he also care about position - * within the bank? - */ - if (pSection->nOrg == -1) { - /* - * Nope, any position will do - * Again, we'll do that later - * - */ - ; - } else { - /* - * Bank and position within the - * bank are hardcoded. - */ - - if (pSection->nBank >= 0 - && pSection->nBank <= 3) { - pSection->nBank += - BANK_SRAM; - if (area_AllocAbs - (&BankFree - [pSection->nBank], - pSection->nOrg, - pSection->nByteSize) - != pSection->nOrg) { - errx(1, -"Unable to load fixed SRAM section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); - } - DOMAXVBANK(pSection-> - nBank); - pSection->oAssigned = 1; - } else { - errx(1, -"Unable to load fixed SRAM section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); - } - } - } - break; - case SECT_WRAMX: - if (pSection->nBank == -1) { - /* - * User doesn't care which bank. - * Therefore he must here be specifying - * position within the bank. - * Defer until later. - */ - ; - } else { - /* - * User specified which bank to use. - * Does he also care about position - * within the bank? - */ - if (pSection->nOrg == -1) { - /* - * Nope, any position will do - * Again, we'll do that later - * - */ - ; - } else { - /* - * Bank and position within the - * bank are hardcoded. - */ - - if (pSection->nBank >= 0 - && pSection->nBank <= 6) { - pSection->nBank += - BANK_WRAMX; - if (area_AllocAbs - (&BankFree - [pSection->nBank], - pSection->nOrg, - pSection->nByteSize) - != pSection->nOrg) { - errx(1, -"Unable to load fixed WRAMX section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); - } - DOMAXWBANK(pSection-> - nBank); - pSection->oAssigned = 1; - } else { - errx(1, -"Unable to load fixed WRAMX section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); - } - } - } - break; - case SECT_VRAM: - if (pSection->nBank == -1) { - /* - * User doesn't care which bank. - * Therefore he must here be specifying - * position within the bank. - * Defer until later. - */ - ; - } else { - /* - * User specified which bank to use. - * Does he also care about position - * within the bank? - */ - if (pSection->nOrg == -1) { - /* - * Nope, any position will do - * Again, we'll do that later - * - */ - ; - } else { - /* - * Bank and position within the - * bank are hardcoded. - */ - - if (pSection->nBank >= 0 - && pSection->nBank <= 1) { - pSection->nBank += - BANK_VRAM; - if (area_AllocAbs - (&BankFree - [pSection->nBank], - pSection->nOrg, - pSection->nByteSize) - != pSection->nOrg) { - errx(1, -"Unable to load fixed VRAM section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); - } - DOMAXVBANK(pSection-> - nBank); - pSection->oAssigned = 1; - } else { - errx(1, -"Unable to load fixed VRAM section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); - } - } - } - break; case SECT_ROM0: - if (area_AllocAbs - (&BankFree[BANK_ROM0], pSection->nOrg, - pSection->nByteSize) != pSection->nOrg) { - errx(1, "Unable to load fixed ROM0 " - "section at $%lX", pSection->nOrg); + pSection->nBank = SECT_ATTRIBUTES[pSection->Type].bank; + if (area_AllocAbs(&BankFree[pSection->nBank], pSection->nOrg, + pSection->nByteSize) == -1) { + errx(1, "Unable to load fixed %s section at $%lX", + SECT_ATTRIBUTES[pSection->Type].name, + pSection->nOrg); } pSection->oAssigned = 1; - pSection->nBank = BANK_ROM0; break; + + case SECT_SRAM: + case SECT_WRAMX: + case SECT_VRAM: case SECT_ROMX: - if (pSection->nBank == -1) { - /* - * User doesn't care which bank, so he must want to - * decide which position within that bank. - * We'll do that at a later stage when the really - * hardcoded things are allocated - * - */ - } else { - /* - * User wants to decide which bank we use - * Does he care about the position as well? - * - */ - - if (pSection->nOrg == -1) { - /* - * Nope, any position will do - * Again, we'll do that later - * - */ + if (pSection->nBank != -1 && pSection->nOrg != -1) { + if (VerifyAndSetBank(pSection) && + area_AllocAbs(&BankFree[pSection->nBank], pSection->nOrg, pSection->nByteSize) != -1) { + DOMAXBANK(pSection->Type, pSection->nBank); + pSection->oAssigned = 1; } else { - /* - * How hardcore can you possibly get? Why does - * he even USE this package? Yeah let's just - * direct address everything, shall we? - * Oh well, the customer is always right - * - */ - - if (pSection->nBank >= 1 - && pSection->nBank <= 511) { - if (area_AllocAbs - (&BankFree - [pSection->nBank], - pSection->nOrg, - pSection-> - nByteSize) != - pSection->nOrg) { - errx(1, - "Unable to load fixed ROMX section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); - } - DOMAXBANK(pSection-> - nBank); - pSection->oAssigned = 1; - } else { - errx(1, - "Unable to load fixed ROMX section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank); - } + errx(1, + "Unable to load fixed %s section at $%lX in bank $%02lX", SECT_ATTRIBUTES[pSection->Type].name, pSection->nOrg, pSection->nBank); } - } break; } @@ -710,74 +360,30 @@ AssignSections(void) pSection = pSections; while (pSection) { if (pSection->oAssigned == 0 - && pSection->Type == SECT_ROMX - && pSection->nOrg == -1 && pSection->nBank != -1) { - /* User wants to have a say... and he's pissed */ - if (options & OPT_OVERLAY) { - errx(1, "All ROM sections must be fixed when using overlay"); + && pSection->nOrg == -1 && pSection->nBank != -1) { + if (pSection->Type == SECT_ROMX && options & OPT_OVERLAY) { + errx(1, "All ROMX sections must be fixed when using overlay"); } - - if (pSection->nBank >= 1 && pSection->nBank <= 511) { - if ((pSection->nOrg = - area_Alloc(&BankFree[pSection->nBank], - pSection->nByteSize)) == -1) { - errx(1, - "Unable to load fixed ROMX section into bank $%02lX", pSection->nBank); + switch (pSection->Type) { + case SECT_ROMX: + case SECT_SRAM: + case SECT_VRAM: + case SECT_WRAMX: + if (VerifyAndSetBank(pSection) && + (pSection->nOrg = area_Alloc(&BankFree[pSection->nBank], pSection->nByteSize)) != -1) { + pSection->oAssigned = 1; + DOMAXBANK(pSection->Type, pSection->nBank); + } else { + errx(1, "Unable to load fixed %s section into bank $%02lX", + SECT_ATTRIBUTES[pSection->Type].name, pSection->nBank); } - pSection->oAssigned = 1; - DOMAXBANK(pSection->nBank); - } else { - errx(1, "Unable to load fixed ROMX section into bank $%02lX", pSection->nBank); - } - } else if (pSection->oAssigned == 0 - && pSection->Type == SECT_SRAM - && pSection->nOrg == -1 && pSection->nBank != -1) { - pSection->nBank += BANK_SRAM; - /* User wants to have a say... and he's pissed */ - if (pSection->nBank >= BANK_SRAM && pSection->nBank <= BANK_SRAM + 3) { - if ((pSection->nOrg = - area_Alloc(&BankFree[pSection->nBank], - pSection->nByteSize)) == -1) { - errx(1, "Unable to load fixed SRAM section into bank $%02lX", pSection->nBank); - } - pSection->oAssigned = 1; - DOMAXSBANK(pSection->nBank); - } else { - errx(1, "Unable to load fixed VRAM section into bank $%02lX", pSection->nBank); - } - } else if (pSection->oAssigned == 0 - && pSection->Type == SECT_VRAM - && pSection->nOrg == -1 && pSection->nBank != -1) { - pSection->nBank += BANK_VRAM; - /* User wants to have a say... and he's pissed */ - if (pSection->nBank >= BANK_VRAM && pSection->nBank <= BANK_VRAM + 1) { - if ((pSection->nOrg = - area_Alloc(&BankFree[pSection->nBank], - pSection->nByteSize)) == -1) { - errx(1, "Unable to load fixed VRAM section into bank $%02lX", pSection->nBank); - } - pSection->oAssigned = 1; - DOMAXVBANK(pSection->nBank); - } else { - errx(1, "Unable to load fixed VRAM section into bank $%02lX", pSection->nBank); - } - } else if (pSection->oAssigned == 0 - && pSection->Type == SECT_WRAMX - && pSection->nOrg == -1 && pSection->nBank != -1) { - pSection->nBank += BANK_WRAMX; - /* User wants to have a say... and he's pissed */ - if (pSection->nBank >= BANK_WRAMX && pSection->nBank <= BANK_WRAMX + 6) { - if ((pSection->nOrg = - area_Alloc(&BankFree[pSection->nBank], - pSection->nByteSize)) == -1) { - errx(1, "Unable to load fixed WRAMX section into bank $%02lX", pSection->nBank - BANK_WRAMX); - } - pSection->oAssigned = 1; - DOMAXWBANK(pSection->nBank); - } else { - errx(1, "Unable to load fixed WRAMX section into bank $%02lX", pSection->nBank - BANK_WRAMX); + break; + + default: // Handle other sections later + break; } } + pSection = pSection->pNext; } @@ -789,61 +395,30 @@ AssignSections(void) pSection = pSections; while (pSection) { if (pSection->oAssigned == 0 - && pSection->Type == SECT_ROMX - && pSection->nOrg != -1 && pSection->nBank == -1) { - /* User wants to have a say... and he's back with a - * vengeance */ - if (options & OPT_OVERLAY) { - errx(1, "All ROM sections must be fixed when using overlay"); + && pSection->nOrg != -1 && pSection->nBank == -1) { + if (pSection->Type == SECT_ROMX && options & OPT_OVERLAY) { + errx(1, "All ROMX sections must be fixed when using overlay"); } - if ((pSection->nBank = - area_AllocAbsROMXAnyBank(pSection->nOrg, - pSection->nByteSize)) == - -1) { - errx(1, "Unable to load fixed ROMX section at $%lX into any bank", pSection->nOrg); + switch (pSection->Type) { + case SECT_ROMX: + case SECT_VRAM: + case SECT_SRAM: + case SECT_WRAMX: + if ((pSection->nBank = + area_AllocAbsAnyBank(pSection->nOrg, pSection->nByteSize, + pSection->Type)) == -1) { + errx(1, "Unable to load fixed %s section at $%lX into any bank", + SECT_ATTRIBUTES[pSection->Type].name, pSection->nOrg); + } + pSection->oAssigned = 1; + DOMAXBANK(pSection->Type, pSection->nBank); + break; + + default: // Handle other sections later + break; } - pSection->oAssigned = 1; - DOMAXBANK(pSection->nBank); - } else if (pSection->oAssigned == 0 - && pSection->Type == SECT_VRAM - && pSection->nOrg != -1 && pSection->nBank == -1) { - /* User wants to have a say... and he's back with a - * vengeance */ - if ((pSection->nBank = - area_AllocAbsVRAMAnyBank(pSection->nOrg, - pSection->nByteSize)) == - -1) { - errx(1, "Unable to load fixed VRAM section at $%lX into any bank", pSection->nOrg); - } - pSection->oAssigned = 1; - DOMAXVBANK(pSection->nBank); - } else if (pSection->oAssigned == 0 - && pSection->Type == SECT_SRAM - && pSection->nOrg != -1 && pSection->nBank == -1) { - /* User wants to have a say... and he's back with a - * vengeance */ - if ((pSection->nBank = - area_AllocAbsSRAMAnyBank(pSection->nOrg, - pSection->nByteSize)) == - -1) { - errx(1, "Unable to load fixed SRAM section at $%lX into any bank", pSection->nOrg); - } - pSection->oAssigned = 1; - DOMAXSBANK(pSection->nBank); - } else if (pSection->oAssigned == 0 - && pSection->Type == SECT_WRAMX - && pSection->nOrg != -1 && pSection->nBank == -1) { - /* User wants to have a say... and he's back with a - * vengeance */ - if ((pSection->nBank = - area_AllocAbsWRAMAnyBank(pSection->nOrg, - pSection->nByteSize)) == - -1) { - errx(1, "Unable to load fixed WRAMX section at $%lX into any bank", pSection->nOrg); - } - pSection->oAssigned = 1; - DOMAXWBANK(pSection->nBank); } + pSection = pSection->pNext; } @@ -856,48 +431,28 @@ AssignSections(void) pSection = pSections; while (pSection) { if (pSection->oAssigned == 0) { + if (pSection->Type == SECT_ROMX && options & OPT_OVERLAY) { + errx(1, "All ROMX sections must be fixed when using overlay"); + } switch (pSection->Type) { case SECT_WRAM0: - if ((pSection->nOrg = - area_Alloc(&BankFree[BANK_WRAM0], - pSection->nByteSize)) == -1) { - errx(1, "WRAM0 section too large"); - } - pSection->nBank = BANK_WRAM0; - pSection->oAssigned = 1; - break; case SECT_HRAM: - if ((pSection->nOrg = - area_Alloc(&BankFree[BANK_HRAM], - pSection->nByteSize)) == -1) { - errx(1, "HRAM section too large"); - } - pSection->nBank = BANK_HRAM; - pSection->oAssigned = 1; - break; - case SECT_SRAM: - break; - case SECT_VRAM: - break; - case SECT_WRAMX: - break; case SECT_ROM0: - if (options & OPT_OVERLAY) { - errx(1, "All ROM sections must be fixed when using overlay"); - } + pSection->nBank = SECT_ATTRIBUTES[pSection->Type].bank; if ((pSection->nOrg = - area_Alloc(&BankFree[BANK_ROM0], + area_Alloc(&BankFree[pSection->nBank], pSection->nByteSize)) == -1) { - errx(1, "ROM0 section too large"); + errx(1, "%s section too large", SECT_ATTRIBUTES[pSection->Type].name); } - pSection->nBank = BANK_ROM0; pSection->oAssigned = 1; break; + + case SECT_SRAM: + case SECT_VRAM: + case SECT_WRAMX: case SECT_ROMX: - if (options & OPT_OVERLAY) { - errx(1, "All ROM sections must be fixed when using overlay"); - } break; + default: errx(1, "(INTERNAL) Unknown section type!"); break; @@ -906,10 +461,10 @@ AssignSections(void) pSection = pSection->pNext; } - AssignCodeSections(); - AssignVRAMSections(); - AssignWRAMSections(); - AssignSRAMSections(); + AssignBankedSections(SECT_ROMX); + AssignBankedSections(SECT_VRAM); + AssignBankedSections(SECT_WRAMX); + AssignBankedSections(SECT_SRAM); } void diff --git a/src/link/patch.c b/src/link/patch.c index 70af5551..25e59ed3 100644 --- a/src/link/patch.c +++ b/src/link/patch.c @@ -68,6 +68,7 @@ getsymbank(SLONG symid) errx(1, "*INTERNAL* UNKNOWN SYMBOL TYPE"); } + if (nBank == BANK_WRAM0) return 0; if (nBank >= BANK_WRAMX && nBank <= (BANK_WRAMX+6)) return nBank - BANK_WRAMX + 1; if (nBank >= BANK_VRAM && nBank <= (BANK_VRAM+1))