2000-09-25 07:06:46 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* */
|
|
|
|
|
/* data.c */
|
|
|
|
|
/* */
|
|
|
|
|
/* Data output routines */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
2003-08-18 20:14:31 +00:00
|
|
|
|
/* (C) 2000-2003 Ullrich von Bassewitz */
|
|
|
|
|
/* R<>merstrasse 52 */
|
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
|
/* EMail: uz@cc65.org */
|
2000-09-25 07:06:46 +00:00
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
|
/* distribution. */
|
|
|
|
|
/* */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* da65 */
|
|
|
|
|
#include "attrtab.h"
|
2000-09-26 07:08:38 +00:00
|
|
|
|
#include "code.h"
|
2000-09-25 07:06:46 +00:00
|
|
|
|
#include "error.h"
|
|
|
|
|
#include "global.h"
|
|
|
|
|
#include "output.h"
|
|
|
|
|
#include "data.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Code */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-09-26 07:08:38 +00:00
|
|
|
|
static unsigned GetSpan (attr_t Style)
|
|
|
|
|
/* Get the number of bytes for a given style */
|
2000-09-25 07:06:46 +00:00
|
|
|
|
{
|
2000-09-26 07:08:38 +00:00
|
|
|
|
/* Get the number of bytes still available */
|
|
|
|
|
unsigned RemainingBytes = GetRemainingBytes ();
|
|
|
|
|
|
|
|
|
|
/* Count how many bytes are available. This number is limited by the
|
|
|
|
|
* number of remaining bytes, a label, or the end of the given Style
|
2000-09-25 07:06:46 +00:00
|
|
|
|
* attribute.
|
|
|
|
|
*/
|
|
|
|
|
unsigned Count = 1;
|
|
|
|
|
while (Count < RemainingBytes) {
|
2000-09-29 12:26:34 +00:00
|
|
|
|
if (MustDefLabel(PC+Count) || GetStyleAttr (PC+Count) != Style) {
|
2000-09-25 07:06:46 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++Count;
|
|
|
|
|
}
|
2000-09-26 07:08:38 +00:00
|
|
|
|
|
|
|
|
|
/* Return the number of bytes */
|
|
|
|
|
return Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned DoTable (attr_t Style, unsigned MemberSize, void (*TableFunc) (unsigned))
|
|
|
|
|
/* Output a table of bytes */
|
|
|
|
|
{
|
|
|
|
|
unsigned BytesLeft;
|
|
|
|
|
|
|
|
|
|
/* Count how many bytes may be output. */
|
|
|
|
|
unsigned Count = GetSpan (Style);
|
|
|
|
|
|
2000-09-29 12:26:34 +00:00
|
|
|
|
/* If the count is less than the member size, print a row of Count data
|
|
|
|
|
* bytes. We assume here that there is no member with a size that is less
|
|
|
|
|
* than BytesPerLine.
|
|
|
|
|
*/
|
|
|
|
|
if (Count < MemberSize) {
|
|
|
|
|
DataByteLine (Count);
|
|
|
|
|
return Count;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-26 07:08:38 +00:00
|
|
|
|
/* Make Count an even number of multiples of MemberSize */
|
|
|
|
|
Count &= ~(MemberSize-1);
|
2000-09-25 07:06:46 +00:00
|
|
|
|
|
|
|
|
|
/* Output as many data bytes lines as needed */
|
2000-09-26 07:08:38 +00:00
|
|
|
|
BytesLeft = Count;
|
|
|
|
|
while (BytesLeft > 0) {
|
2000-09-25 07:06:46 +00:00
|
|
|
|
|
|
|
|
|
/* Calculate the number of bytes for the next line */
|
2000-09-26 07:08:38 +00:00
|
|
|
|
unsigned Chunk = (BytesLeft > BytesPerLine)? BytesPerLine : BytesLeft;
|
2000-09-25 07:06:46 +00:00
|
|
|
|
|
|
|
|
|
/* Output a line with these bytes */
|
2000-09-26 07:08:38 +00:00
|
|
|
|
TableFunc (Chunk);
|
2000-09-25 07:06:46 +00:00
|
|
|
|
|
|
|
|
|
/* Next line */
|
2000-09-26 07:08:38 +00:00
|
|
|
|
BytesLeft -= Chunk;
|
|
|
|
|
PC += Chunk;
|
2000-09-25 07:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-26 07:08:38 +00:00
|
|
|
|
/* If the next line is not the same style, add a separator */
|
2000-09-29 12:26:34 +00:00
|
|
|
|
if (CodeLeft() && GetStyleAttr (PC) != Style) {
|
2000-09-25 07:06:46 +00:00
|
|
|
|
SeparatorLine ();
|
|
|
|
|
}
|
2000-09-26 07:08:38 +00:00
|
|
|
|
|
|
|
|
|
/* Return the number of bytes output */
|
|
|
|
|
return Count;
|
2000-09-25 07:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-09-26 07:08:38 +00:00
|
|
|
|
unsigned ByteTable (void)
|
|
|
|
|
/* Output a table of bytes */
|
2000-09-25 07:06:46 +00:00
|
|
|
|
{
|
2000-09-26 07:08:38 +00:00
|
|
|
|
/* Call the low level function */
|
|
|
|
|
return DoTable (atByteTab, 1, DataByteLine);
|
|
|
|
|
}
|
2000-09-25 07:06:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-09-26 07:08:38 +00:00
|
|
|
|
unsigned WordTable (void)
|
|
|
|
|
/* Output a table of words */
|
|
|
|
|
{
|
|
|
|
|
/* Call the low level function */
|
|
|
|
|
return DoTable (atWordTab, 2, DataWordLine);
|
|
|
|
|
}
|
2000-09-25 07:06:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-09-26 07:08:38 +00:00
|
|
|
|
unsigned DWordTable (void)
|
|
|
|
|
/* Output a table of double words */
|
|
|
|
|
{
|
|
|
|
|
/* Call the low level function */
|
|
|
|
|
return DoTable (atDWordTab, 4, DataDWordLine);
|
2000-09-25 07:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-09-26 07:08:38 +00:00
|
|
|
|
unsigned AddrTable (void)
|
2000-09-25 07:06:46 +00:00
|
|
|
|
/* Output a table of addresses */
|
|
|
|
|
{
|
2000-09-26 07:08:38 +00:00
|
|
|
|
unsigned BytesLeft;
|
|
|
|
|
|
|
|
|
|
/* Count how many bytes may be output. */
|
|
|
|
|
unsigned Count = GetSpan (atAddrTab);
|
2000-09-25 07:06:46 +00:00
|
|
|
|
|
2000-12-09 10:10:07 +00:00
|
|
|
|
/* Handle Count == 1 ### */
|
|
|
|
|
if (Count == 1) {
|
|
|
|
|
ByteTable ();
|
|
|
|
|
}
|
2000-09-29 12:26:34 +00:00
|
|
|
|
|
2000-09-25 07:06:46 +00:00
|
|
|
|
/* Make the given number even */
|
|
|
|
|
Count &= ~1U;
|
|
|
|
|
|
|
|
|
|
/* Output as many data bytes lines as needed. For addresses, each line
|
|
|
|
|
* will hold just one address.
|
2000-09-29 12:26:34 +00:00
|
|
|
|
*/
|
2000-09-26 07:08:38 +00:00
|
|
|
|
BytesLeft = Count;
|
|
|
|
|
while (BytesLeft > 0) {
|
2000-09-25 07:06:46 +00:00
|
|
|
|
|
|
|
|
|
/* Get the address */
|
|
|
|
|
unsigned Addr = GetCodeWord (PC);
|
|
|
|
|
|
|
|
|
|
/* In pass 1, define a label, in pass 2 output the line */
|
|
|
|
|
if (Pass == 1) {
|
|
|
|
|
if (!HaveLabel (Addr)) {
|
2000-09-29 12:26:34 +00:00
|
|
|
|
AddLabel (Addr, atIntLabel, MakeLabelName (Addr));
|
2000-09-25 07:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const char* Label = GetLabel (Addr);
|
|
|
|
|
if (Label == 0) {
|
2000-09-26 07:08:38 +00:00
|
|
|
|
/* OOPS! Should not happen */
|
|
|
|
|
Internal ("OOPS - Label for address %04X disappeard!", Addr);
|
2000-09-25 07:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
Indent (MIndent);
|
|
|
|
|
Output (".word");
|
|
|
|
|
Indent (AIndent);
|
|
|
|
|
Output ("%s", Label);
|
|
|
|
|
LineComment (PC, 2);
|
|
|
|
|
LineFeed ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Next line */
|
2000-09-26 07:08:38 +00:00
|
|
|
|
PC += 2;
|
|
|
|
|
BytesLeft -= 2;
|
2000-09-25 07:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If the next line is not a byte table line, add a separator */
|
2000-09-29 12:26:34 +00:00
|
|
|
|
if (CodeLeft() && GetStyleAttr (PC) != atAddrTab) {
|
2000-09-25 07:06:46 +00:00
|
|
|
|
SeparatorLine ();
|
2000-09-29 12:26:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return the number of bytes output */
|
|
|
|
|
return Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned RtsTable (void)
|
|
|
|
|
/* Output a table of RTS addresses (address - 1) */
|
|
|
|
|
{
|
|
|
|
|
unsigned BytesLeft;
|
|
|
|
|
|
|
|
|
|
/* Count how many bytes may be output. */
|
|
|
|
|
unsigned Count = GetSpan (atRtsTab);
|
|
|
|
|
|
|
|
|
|
/* Need to handle Count == 1 here!!! ### */
|
|
|
|
|
|
|
|
|
|
/* Make the given number even */
|
|
|
|
|
Count &= ~1U;
|
|
|
|
|
|
|
|
|
|
/* Output as many data bytes lines as needed. For addresses, each line
|
|
|
|
|
* will hold just one address.
|
|
|
|
|
*/
|
|
|
|
|
BytesLeft = Count;
|
|
|
|
|
while (BytesLeft > 0) {
|
|
|
|
|
|
|
|
|
|
/* Get the address */
|
2003-08-18 20:14:31 +00:00
|
|
|
|
unsigned Addr = (GetCodeWord (PC) + 1) & 0xFFFF;
|
2000-09-29 12:26:34 +00:00
|
|
|
|
|
|
|
|
|
/* In pass 1, define a label, in pass 2 output the line */
|
|
|
|
|
if (Pass == 1) {
|
|
|
|
|
if (!HaveLabel (Addr)) {
|
|
|
|
|
AddLabel (Addr, atIntLabel, MakeLabelName (Addr));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const char* Label = GetLabel (Addr);
|
|
|
|
|
if (Label == 0) {
|
|
|
|
|
/* OOPS! Should not happen */
|
|
|
|
|
Internal ("OOPS - Label for address %04X disappeard!", Addr);
|
|
|
|
|
}
|
|
|
|
|
Indent (MIndent);
|
|
|
|
|
Output (".word");
|
|
|
|
|
Indent (AIndent);
|
|
|
|
|
Output ("%s-1", Label);
|
|
|
|
|
LineComment (PC, 2);
|
|
|
|
|
LineFeed ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Next line */
|
|
|
|
|
PC += 2;
|
|
|
|
|
BytesLeft -= 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If the next line is not a byte table line, add a separator */
|
|
|
|
|
if (CodeLeft() && GetStyleAttr (PC) != atRtsTab) {
|
|
|
|
|
SeparatorLine ();
|
|
|
|
|
}
|
2000-09-26 07:08:38 +00:00
|
|
|
|
|
|
|
|
|
/* Return the number of bytes output */
|
|
|
|
|
return Count;
|
2000-09-25 07:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-16 18:16:09 +00:00
|
|
|
|
unsigned TextTable (void)
|
|
|
|
|
/* Output a table of text messages */
|
|
|
|
|
{
|
|
|
|
|
/* Count how many bytes may be output. */
|
|
|
|
|
unsigned ByteCount = GetSpan (atTextTab);
|
|
|
|
|
|
|
|
|
|
/* Output as many data bytes lines as needed. */
|
|
|
|
|
unsigned BytesLeft = ByteCount;
|
|
|
|
|
while (BytesLeft > 0) {
|
|
|
|
|
|
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
|
|
/* Count the number of characters that can be output as such */
|
|
|
|
|
unsigned Count = 0;
|
|
|
|
|
while (Count < BytesLeft && Count < BytesPerLine*4-1) {
|
|
|
|
|
unsigned char C = GetCodeByte (PC + Count);
|
|
|
|
|
if (C >= 0x20 && C <= 0x7E && C != '\"') {
|
|
|
|
|
++Count;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If we have text, output it */
|
|
|
|
|
if (Count > 0) {
|
|
|
|
|
unsigned CBytes;
|
|
|
|
|
Indent (MIndent);
|
|
|
|
|
Output (".text");
|
|
|
|
|
Indent (AIndent);
|
|
|
|
|
Output ("\"");
|
|
|
|
|
for (I = 0; I < Count; ++I) {
|
|
|
|
|
Output ("%c", GetCodeByte (PC+I));
|
|
|
|
|
}
|
|
|
|
|
Output ("\"");
|
|
|
|
|
CBytes = Count;
|
|
|
|
|
while (CBytes > 0) {
|
|
|
|
|
unsigned Chunk = CBytes;
|
|
|
|
|
if (Chunk > BytesPerLine) {
|
|
|
|
|
Chunk = BytesPerLine;
|
|
|
|
|
}
|
|
|
|
|
LineComment (PC, Chunk);
|
|
|
|
|
LineFeed ();
|
|
|
|
|
CBytes -= Chunk;
|
|
|
|
|
PC += Chunk;
|
|
|
|
|
}
|
|
|
|
|
BytesLeft -= Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Count the number of bytes that must be output as bytes */
|
|
|
|
|
Count = 0;
|
|
|
|
|
while (Count < BytesLeft && Count < BytesPerLine) {
|
|
|
|
|
unsigned char C = GetCodeByte (PC + Count);
|
2003-08-18 20:14:31 +00:00
|
|
|
|
if (C < 0x20 || C > 0x7E || C == '\"') {
|
2001-09-16 18:16:09 +00:00
|
|
|
|
++Count;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If we have raw output bytes, print them */
|
|
|
|
|
if (Count > 0) {
|
|
|
|
|
DataByteLine (Count);
|
|
|
|
|
PC += Count;
|
|
|
|
|
BytesLeft -= Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If the next line is not a byte table line, add a separator */
|
|
|
|
|
if (CodeLeft() && GetStyleAttr (PC) != atTextTab) {
|
|
|
|
|
SeparatorLine ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return the number of bytes output */
|
|
|
|
|
return ByteCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|