Compare commits

...

16 Commits

Author SHA1 Message Date
Anthony J. Bentley
d81d128a04 Don't specify strict warnings on MinGW. 2015-02-22 04:02:30 -07:00
Anthony J. Bentley
d067f31678 Point to the Github releases for Windows builds. 2015-02-22 04:00:06 -07:00
Anthony J. Bentley
d02294505c Avoid use of magic numbers.
From Antonio Niño Díaz.
2015-02-22 03:55:51 -07:00
Anthony J. Bentley
eb4bbb3e0b Update mapfile code to support new section types.
From Antonio Niño Díaz.
2015-02-22 03:52:28 -07:00
Anthony J. Bentley
2ab10a95e4 Fix a bug in handling WRAMX sections with/without hardcoded addresses.
From Antonio Niño Díaz.
2015-02-22 03:48:00 -07:00
Anthony J. Bentley
bc60b85785 Support BANK() correctly when given WRAMX/SRAM/VRAM labels.
(Mostly) from Antonio Niño Díaz.
2015-02-22 03:33:18 -07:00
Anthony J. Bentley
9d0203a4ee Delete commented-out code. 2015-02-22 03:31:22 -07:00
Anthony J. Bentley
a18b65de21 Update license for reallocarray. 2015-02-17 21:23:14 -07:00
Anthony J. Bentley
1c47ffcce8 Import reallocarray() for later use. 2015-02-14 00:14:39 -07:00
Anthony J. Bentley
60c3a7e2f3 Reformat code for better spacing, and provide a more detailed error. 2015-01-30 20:30:33 -07:00
Anthony J. Bentley
5702995978 extern: sync strlcpy and strlcat with upstream. 2015-01-28 23:36:08 -07:00
Anthony J. Bentley
483d94f88b rgbasm: Fixed a dumb typo; pointed out by stag019. 2015-01-27 05:42:51 -07:00
Anthony J. Bentley
9b4d16b0d8 Add another crashing regression test. 2015-01-27 05:02:57 -07:00
Anthony J. Bentley
a3e95f99d2 rgbasm: Fix a division by zero in section address specifiers. 2015-01-27 04:56:42 -07:00
Anthony J. Bentley
e3a31d7e59 Correct test output for divzero-section-bank. 2015-01-27 04:48:25 -07:00
Anthony J. Bentley
d9f5ce339a Add some basic regression tests.
Most cause crashes. Only macro-@ works correctly; the others need to
be fixed.
2015-01-27 04:33:38 -07:00
27 changed files with 234 additions and 79 deletions

View File

@@ -21,5 +21,8 @@ under the ISC license; see the source file for the text of the license.
extern/err.c is derived from the Musl C library, http://www.musl-libc.org,
and is released under the MIT license.
extern/reallocarray.c is derived from the OpenBSD Project,
http://www.openbsd.org, and is released under the ISC license.
extern/strl.c is derived from the OpenBSD Project, http://www.openbsd.org,
and is released under the BSD license.

View File

@@ -85,7 +85,7 @@ src/asm/asmy.h: src/asm/asmy.c
# install instructions instead.
mingw:
$Qenv PATH=/usr/local/mingw32/bin:/bin:/usr/bin:/usr/local/bin \
make CC=gcc CFLAGS="-I/usr/local/mingw32/include \
make WARNFLAGS= CC=gcc CFLAGS="-I/usr/local/mingw32/include \
-D__progname=\\\"\\\" \
${CFLAGS}"
$Qmv rgbasm rgbasm.exe

2
README
View File

@@ -53,6 +53,6 @@ this variable. Defaults to @.
Installing RGBDS (Windows)
============================
Windows builds are available here: http://anthony.bentley.name/rgbds/
Windows builds are available here: https://github.com/bentley/rgbds/releases
Copy the .exe files to C:\Windows\ or similar.

10
include/extern/reallocarray.h vendored Normal file
View File

@@ -0,0 +1,10 @@
#ifndef EXTERN_REALLOCARRAY_H
#define EXTERN_REALLOCARRAY_H
#include <stdlib.h>
#ifndef REALLOCARRAY_IN_LIBC
#define reallocarray rgbds_reallocarray
#endif
#endif

View File

@@ -992,8 +992,16 @@ const : T_ID { $$ = sym_GetConstantValue($1); }
| const T_OP_SHL const { $$ = $1 << $3; }
| const T_OP_SHR const { $$ = $1 >> $3; }
| const T_OP_MUL const { $$ = $1 * $3; }
| const T_OP_DIV const { $$ = $1 / $3; }
| const T_OP_MOD const { $$ = $1 % $3; }
| const T_OP_DIV const {
if ($3 == 0)
fatalerror("division by zero");
$$ = $1 / $3;
}
| const T_OP_MOD const {
if ($3 == 0)
fatalerror("division by zero");
$$ = $1 % $3;
}
| T_OP_ADD const %prec NEG { $$ = +$2; }
| T_OP_SUB const %prec NEG { $$ = -$2; }
| T_OP_NOT const %prec NEG { $$ = 0xFFFFFFFF^$2; }
@@ -1065,7 +1073,7 @@ section:
}
} else if ($4 == SECT_WRAMX) {
if ($8 >= 1 && $8 <= 7) {
out_NewAbsSection($2, $4, -1, $8);
out_NewAbsSection($2, $4, -1, $8 - 1);
} else {
yyerror("WRAMX bank value $%x out of range (1 to 7)", $8);
}
@@ -1102,7 +1110,7 @@ section:
} else if ($4 == SECT_WRAMX) {
if ($6 >= 0 && $6 < 0x10000) {
if ($11 >= 1 && $11 <= 7) {
out_NewAbsSection($2, $4, $6, $11);
out_NewAbsSection($2, $4, $6, $11 - 1);
} else {
yyerror("WRAMX bank value $%x out of range (1 to 7)", $11);
}

View File

@@ -440,24 +440,30 @@ checkcodesection(SLONG size)
errx(1, "Section '%s' cannot contain code or data (not a "
"ROM0 or ROMX)", pCurrentSection->pzName);
}
if (pCurrentSection->nPC + size <= MAXSECTIONSIZE) {
if (((pCurrentSection->nPC % SECTIONCHUNK) >
((pCurrentSection->nPC + size) % SECTIONCHUNK))
&& (pCurrentSection->nType == SECT_ROM0
|| pCurrentSection->nType == SECT_ROMX)) {
if ((pCurrentSection->tData =
(UBYTE *) realloc(pCurrentSection->tData,
((pCurrentSection->nPC +
size) / SECTIONCHUNK +
1) * SECTIONCHUNK)) != NULL) {
return;
} else
fatalerror
("Not enough memory to expand section");
if (pCurrentSection->nPC + size > MAXSECTIONSIZE) {
/*
* N.B.: This check is not sufficient to ensure the section
* will fit, because there can be multiple sections of this
* type. The definitive check must be done at the linking
* stage.
*/
errx(1, "Section '%s' is too big (old size %d + %d > %d)",
pCurrentSection->pzName, pCurrentSection->nPC, size,
MAXSECTIONSIZE);
}
if (((pCurrentSection->nPC % SECTIONCHUNK) >
((pCurrentSection->nPC + size) % SECTIONCHUNK)) &&
(pCurrentSection->nType == SECT_ROM0 ||
pCurrentSection->nType == SECT_ROMX)) {
pCurrentSection->tData = realloc(pCurrentSection->tData,
((pCurrentSection->nPC + size) / SECTIONCHUNK + 1) *
SECTIONCHUNK);
if (pCurrentSection->tData == NULL) {
err(1, "Could not expand section");
}
return;
} else
errx(1, "Section '%s' is too big", pCurrentSection->pzName);
}
return;
}
/*

38
src/extern/reallocarray.c vendored Normal file
View File

@@ -0,0 +1,38 @@
/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */
/*
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
*
* 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 <sys/types.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
/*
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
*/
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
void *
rgbds_reallocarray(void *optr, size_t nmemb, size_t size)
{
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
nmemb > 0 && SIZE_MAX / nmemb < size) {
errno = ENOMEM;
return NULL;
}
return realloc(optr, size * nmemb);
}

46
src/extern/strlcat.c vendored
View File

@@ -1,7 +1,7 @@
/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
/* $OpenBSD: strlcat.c,v 1.14 2015/01/15 03:54:12 millert Exp $ */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -20,36 +20,36 @@
#include <string.h>
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* Appends src to string dst of size dsize (unlike strncat, dsize is the
* full size of dst, not space left). At most dsize-1 characters
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
* Returns strlen(src) + MIN(dsize, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
size_t
rgbds_strlcat(char *dst, const char *src, size_t siz)
rgbds_strlcat(char *dst, const char *src, size_t dsize)
{
char *d = dst;
const char *s = src;
size_t n = siz;
const char *odst = dst;
const char *osrc = src;
size_t n = dsize;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
/* Find the end of dst and adjust bytes left but don't go past end. */
while (n-- != 0 && *dst != '\0')
dst++;
dlen = dst - odst;
n = dsize - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
if (n-- == 0)
return(dlen + strlen(src));
while (*src != '\0') {
if (n != 0) {
*dst++ = *src;
n--;
}
s++;
src++;
}
*d = '\0';
*dst = '\0';
return(dlen + (s - src)); /* count does not include NUL */
return(dlen + (src - osrc)); /* count does not include NUL */
}

37
src/extern/strlcpy.c vendored
View File

@@ -1,7 +1,7 @@
/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -20,32 +20,31 @@
#include <string.h>
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
* Copy string src to buffer dst of size dsize. At most dsize-1
* chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= dsize, truncation occurred.
*/
size_t
rgbds_strlcpy(char *dst, const char *src, size_t siz)
rgbds_strlcpy(char *dst, const char *src, size_t dsize)
{
char *d = dst;
const char *s = src;
size_t n = siz;
const char *osrc = src;
size_t nleft = dsize;
/* Copy as many bytes as will fit */
if (n != 0) {
while (--n != 0) {
if ((*d++ = *s++) == '\0')
/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
if ((*dst++ = *src++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}
return(s - src - 1); /* count does not include NUL */
return(src - osrc - 1); /* count does not include NUL */
}

View File

@@ -324,6 +324,7 @@ AssignSRAMSections(void)
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 {
@@ -343,6 +344,7 @@ AssignWRAMSections(void)
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 {
@@ -557,8 +559,8 @@ AssignSections(void)
* bank are hardcoded.
*/
if (pSection->nBank >= 1
&& pSection->nBank <= 7) {
if (pSection->nBank >= 0
&& pSection->nBank <= 6) {
pSection->nBank +=
BANK_WRAMX;
if (area_AllocAbs

View File

@@ -53,19 +53,36 @@ MapfileInitBank(SLONG bank)
{
if (mf) {
currentbank = bank;
if (bank == 0)
fprintf(mf, "Bank #0 (HOME):\n");
if (bank == BANK_ROM0)
fprintf(mf, "ROM Bank #0 (HOME):\n");
else if (bank < BANK_WRAM0)
fprintf(mf, "Bank #%ld:\n", bank);
fprintf(mf, "ROM Bank #%ld:\n", bank);
else if (bank == BANK_WRAM0)
fprintf(mf, "WRAM0:\n");
fprintf(mf, "WRAM Bank #0:\n");
else if (bank < BANK_VRAM)
fprintf(mf, "WRAM Bank #%ld:\n", bank - BANK_WRAMX + 1);
else if (bank == BANK_HRAM)
fprintf(mf, "HRAM:\n");
else if (bank == BANK_VRAM || bank == BANK_VRAM + 1)
fprintf(mf, "VRAM Bank #%ld:\n", bank - BANK_VRAM);
else if (bank < MAXBANKS)
fprintf(mf, "SRAM Bank #%ld:\n", bank - BANK_SRAM);
}
if (sf) {
sfbank = (bank >= 1 && bank <= 511) ? bank : 0;
if (bank < BANK_WRAM0)
sfbank = bank;
else if (bank == BANK_WRAM0)
sfbank = 0;
else if (bank < BANK_VRAM)
sfbank = bank - BANK_WRAMX + 1;
else if (bank == BANK_HRAM)
sfbank = 0;
else if (bank == BANK_VRAM || bank == BANK_VRAM + 1)
sfbank = bank - BANK_VRAM;
else if (bank < MAXBANKS)
sfbank = bank - BANK_SRAM;
else
sfbank = 0;
}
}

View File

@@ -86,7 +86,7 @@ Output(void)
fclose(f);
}
for (i = 256; i < MAXBANKS; i += 1) {
for (i = BANK_WRAM0; i < MAXBANKS; i++) {
struct sSection *pSect;
MapfileInitBank(i);
pSect = pSections;

View File

@@ -3,6 +3,7 @@
#include <string.h>
#include "extern/err.h"
#include "link/assign.h"
#include "link/mylink.h"
#include "link/symbol.h"
#include "link/main.h"
@@ -53,18 +54,28 @@ getsymvalue(SLONG symid)
SLONG
getsymbank(SLONG symid)
{
SLONG nBank;
switch (pCurrentSection->tSymbols[symid]->Type) {
case SYM_IMPORT:
return (sym_GetBank(pCurrentSection->tSymbols[symid]->pzName));
case SYM_IMPORT:
nBank = sym_GetBank(pCurrentSection->tSymbols[symid]->pzName);
break;
case SYM_EXPORT:
case SYM_LOCAL:
return (pCurrentSection->tSymbols[symid]->pSection->nBank);
//return (pCurrentSection->nBank);
default:
nBank = pCurrentSection->tSymbols[symid]->pSection->nBank;
break;
default:
errx(1, "*INTERNAL* UNKNOWN SYMBOL TYPE");
}
errx(1, "*INTERNAL* UNKNOWN SYMBOL TYPE");
if (nBank >= BANK_WRAMX && nBank <= (BANK_WRAMX+6))
return nBank - BANK_WRAMX + 1;
if (nBank >= BANK_VRAM && nBank <= (BANK_VRAM+1))
return nBank - BANK_VRAM;
if (nBank >= BANK_SRAM && nBank <= (BANK_SRAM+3))
return nBank - BANK_SRAM;
return nBank;
}
SLONG

View File

@@ -0,0 +1,2 @@
SECTION "sec", ROM0
db BANK(noexist)

View File

@@ -0,0 +1,2 @@
ERROR: bank-noexist.asm(2) :
'noexist' not defined

View File

@@ -0,0 +1,2 @@
SECTION "sec", ROM0
ld a, 1/0

View File

@@ -0,0 +1,2 @@
ERROR: divzero-instr.asm(2) :
division by zero

View File

@@ -0,0 +1 @@
SECTION "sec", ROMX[$/0]

View File

@@ -0,0 +1,2 @@
ERROR: divzero-section-bank.asm(1) :
division by zero

1
test/asm/macro-@.asm Normal file
View File

@@ -0,0 +1 @@
foo @bar

2
test/asm/macro-@.out Normal file
View File

@@ -0,0 +1,2 @@
ERROR: macro-@.asm(1) -> @(-1) :
Macro '@' not defined

BIN
test/asm/null-in-macro.asm Normal file

Binary file not shown.

View File

6
test/asm/test.sh Normal file
View File

@@ -0,0 +1,6 @@
fname=$(mktemp)
for i in *.asm; do
../../rgbasm $i >$fname 2>&1
diff -u $fname ${i%.asm}.out
done

View File

@@ -0,0 +1,3 @@
SECTION "sec", ROM0
foo:
add sp, .

View File

@@ -0,0 +1,2 @@
ERROR: undefined-dot.asm(3) :
'.' not defined

View File

@@ -0,0 +1,36 @@
; this should generate a rom consisting of the following bytes:
; 01 02 03 04 05 06 07 00 01 02 03 00 01
section "x",rom0
db bank(w1),bank(w2),bank(w3),bank(w4),bank(w5),bank(w6),bank(w7)
db bank(s0),bank(s1),bank(s2),bank(s3)
db bank(v0),bank(v1)
section "wa",wramx,bank[1]
w1:
section "wb",wramx,bank[2]
w2:
section "wc",wramx,bank[3]
w3:
section "wd",wramx,bank[4]
w4:
section "we",wramx,bank[5]
w5:
section "wf",wramx,bank[6]
w6:
section "wg",wramx,bank[7]
w7:
section "sa",sram,bank[0]
s0:
section "sb",sram,bank[1]
s1:
section "sc",sram,bank[2]
s2:
section "sd",sram,bank[3]
s3:
section "v00",vram,bank[0]
v0:
section "v01",vram,bank[1]
v1: