fixed REM alignment issue

This commit is contained in:
Denis Molony 2021-02-22 09:24:34 +10:00
parent a8030c469c
commit 5507271a74
3 changed files with 75 additions and 64 deletions

View File

@ -62,21 +62,13 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
super (name, buffer); super (name, buffer);
int ptr = 0; int ptr = 0;
int currentAddress = 0;
int max = buffer.length - 6; // need at least 6 bytes to make a SourceLine while (buffer[ptr + 1] != 0) // msb of link field
while (ptr <= max)
{ {
int nextAddress = unsignedShort (buffer, ptr);
if (nextAddress <= currentAddress) // usually zero when finished
break;
SourceLine line = new SourceLine (this, buffer, ptr); SourceLine line = new SourceLine (this, buffer, ptr);
sourceLines.add (line); sourceLines.add (line);
checkXref (line); checkXref (line);
ptr += line.length; // assumes lines are contiguous ptr += line.length; // assumes lines are contiguous
currentAddress = nextAddress;
} }
endPtr = ptr; endPtr = ptr;
@ -127,56 +119,59 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
{ {
int loadAddress = getLoadAddress (); int loadAddress = getLoadAddress ();
int ptr = 0; int ptr = 0;
int nextLine; int linkField;
byte b; byte b;
StringBuilder text = new StringBuilder (); StringBuilder currentLine = new StringBuilder ();
while ((nextLine = unsignedShort (buffer, ptr)) != 0) while ((linkField = unsignedShort (buffer, ptr)) != 0)
{ {
int lineNumber = unsignedShort (buffer, ptr + 2); int lineNumber = unsignedShort (buffer, ptr + 2);
text.append (String.format (" %d ", lineNumber)); currentLine.append (String.format (" %d ", lineNumber));
ptr += 4; ptr += 4;
while ((b = buffer[ptr++]) != 0) while ((b = buffer[ptr++]) != 0)
if (isHighBitSet (b)) if (isHighBitSet (b))
text.append (String.format (" %s ", ApplesoftConstants.tokens[b & 0x7F])); currentLine
.append (String.format (" %s ", ApplesoftConstants.tokens[b & 0x7F]));
else else
switch (b) switch (b)
{ {
case Utility.ASCII_CR: case Utility.ASCII_CR:
text.append (NEWLINE); currentLine.append (NEWLINE);
break; break;
case Utility.ASCII_BACKSPACE: case Utility.ASCII_BACKSPACE:
if (text.length () > 0) if (currentLine.length () > 0)
text.deleteCharAt (text.length () - 1); currentLine.deleteCharAt (currentLine.length () - 1);
break; break;
case Utility.ASCII_LF: case Utility.ASCII_LF:
int indent = getIndent (text); int indent = getIndent (currentLine);
text.append ("\n"); currentLine.append ("\n");
for (int i = 0; i < indent; i++) for (int i = 0; i < indent; i++)
text.append (" "); currentLine.append (" ");
break; break;
default: default:
text.append ((char) b); currentLine.append ((char) b);
} }
if (ptr != (nextLine - loadAddress)) if (ptr != (linkField - loadAddress))
{ {
System.out.printf ("ptr: %04X, nextLine: %04X%n", ptr, nextLine - loadAddress); System.out.printf ("%s: ptr: %04X, nextLine: %04X%n", name, ptr + loadAddress,
// ptr = nextLine - loadAddress; linkField);
// ptr = linkField - loadAddress; // use this one day
} }
text.append (NEWLINE);
currentLine.append (NEWLINE);
// List<String> lines = wrap (text, 29); // List<String> lines = wrap (text, 29);
// fullText.append (String.format ("%d %s%n", lineNumber, lines.get (0))); // fullText.append (String.format ("%d %s%n", lineNumber, lines.get (0)));
// for (int i = 1; i < lines.size (); i++) // for (int i = 1; i < lines.size (); i++)
// fullText.append (String.format (" %s%n", lines.get (i))); // fullText.append (String.format (" %s%n", lines.get (i)));
fullText.append (text); fullText.append (currentLine);
text.setLength (0); currentLine.setLength (0);
} }
} }
@ -329,8 +324,8 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
int ptr = endPtr + 2; int ptr = endPtr + 2;
if (ptr < buffer.length - 1) // sometimes there's an extra byte on the end if (ptr < buffer.length - 1) // sometimes there's an extra byte on the end
{ {
int offset = unsignedShort (buffer, 0); int linkField = unsignedShort (buffer, 0);
int programLoadAddress = offset - getLineLength (0); int programLoadAddress = linkField - getLineLength (0);
fullText.append ("\nExtra data:\n\n"); fullText.append ("\nExtra data:\n\n");
fullText.append (HexFormatter.formatNoHeader (buffer, ptr, buffer.length - ptr, fullText.append (HexFormatter.formatNoHeader (buffer, ptr, buffer.length - ptr,
programLoadAddress + ptr)); programLoadAddress + ptr));
@ -786,8 +781,8 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
private String getDebugText (StringBuilder text) private String getDebugText (StringBuilder text)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int offset = unsignedShort (buffer, 0); int linkField = unsignedShort (buffer, 0);
int programLoadAddress = offset - getLineLength (0); int programLoadAddress = linkField - getLineLength (0);
for (SourceLine sourceLine : sourceLines) for (SourceLine sourceLine : sourceLines)
{ {
@ -815,7 +810,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
int length = buffer.length - endPtr; int length = buffer.length - endPtr;
int ptr = endPtr; int ptr = endPtr;
if (length > 2) if (length >= 2)
{ {
text.append (" "); text.append (" ");
text.append ( text.append (
@ -825,19 +820,22 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
length -= 2; length -= 2;
} }
// show the extra bytes as a hex dump if (length > 0)
String formattedHex = HexFormatter.formatNoHeader (buffer, ptr, buffer.length - ptr, {
programLoadAddress + ptr); // show the extra bytes as a hex dump
for (String bytes : formattedHex.split (NEWLINE)) String formattedHex = HexFormatter.formatNoHeader (buffer, ptr,
text.append (String.format (" %s%n", bytes)); buffer.length - ptr, programLoadAddress + ptr);
for (String bytes : formattedHex.split (NEWLINE))
text.append (String.format (" %s%n", bytes));
// show the extra bytes as a disassembly // show the extra bytes as a disassembly
byte[] extraBuffer = new byte[length]; byte[] extraBuffer = new byte[length];
System.arraycopy (buffer, ptr, extraBuffer, 0, extraBuffer.length); System.arraycopy (buffer, ptr, extraBuffer, 0, extraBuffer.length);
AssemblerProgram assemblerProgram = AssemblerProgram assemblerProgram =
new AssemblerProgram ("extra", extraBuffer, programLoadAddress + ptr); new AssemblerProgram ("extra", extraBuffer, programLoadAddress + ptr);
text.append ("\n"); text.append ("\n");
text.append (assemblerProgram.getText ()); text.append (assemblerProgram.getText ());
}
} }
return Utility.rtrim (text); return Utility.rtrim (text);
@ -878,29 +876,25 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
int getLoadAddress () int getLoadAddress ()
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int programLoadAddress = 0; return (buffer.length > 1) ? unsignedShort (buffer, 0) - getLineLength (0) : 0;
if (buffer.length > 1)
{
int offset = unsignedShort (buffer, 0);
programLoadAddress = offset - getLineLength (0);
}
return programLoadAddress;
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private int getLineLength (int ptr) private int getLineLength (int ptr)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int offset = unsignedShort (buffer, ptr); int linkField = unsignedShort (buffer, ptr);
if (offset == 0) if (linkField == 0)
return 0; return 2;
ptr += 4; // skip offset and line number ptr += 4; // skip link field and line number
int length = 5; int length = 5;
while (ptr < buffer.length && buffer[ptr++] != 0) while (ptr < buffer.length && buffer[ptr++] != 0)
length++; length++;
// System.out.printf ("Length: %4d, Ptr: %4d%n", length, ptr);
assert length == ptr;
return length; return length;
} }

View File

@ -12,7 +12,7 @@ public class SourceLine implements ApplesoftConstants
// -----------------------------------------------------------------------------------// // -----------------------------------------------------------------------------------//
{ {
ApplesoftBasicProgram parent; ApplesoftBasicProgram parent;
int addressNext; int linkField;
int lineNumber; int lineNumber;
int linePtr; int linePtr;
int length; int length;
@ -28,7 +28,7 @@ public class SourceLine implements ApplesoftConstants
this.buffer = buffer; this.buffer = buffer;
linePtr = ptr; linePtr = ptr;
addressNext = unsignedShort (buffer, ptr); linkField = unsignedShort (buffer, ptr);
lineNumber = unsignedShort (buffer, ptr + 2); lineNumber = unsignedShort (buffer, ptr + 2);
int startPtr = ptr += 4; // skip link to next line and lineNumber int startPtr = ptr += 4; // skip link to next line and lineNumber

View File

@ -80,8 +80,9 @@ public class SubLine implements ApplesoftConstants
else if (isEndOfLine (firstByte)) // empty subline else if (isEndOfLine (firstByte)) // empty subline
return; return;
else // probably Beagle Bros 0D or 0A else // probably Beagle Bros 0D or 0A
System.out.printf ("Unexpected bytes at %5d: %s%n", parent.lineNumber, System.out.printf ("%s unexpected bytes at %5d: %s%n", parent.parent.name,
HexFormatter.formatNoHeader (buffer, startPtr, length).substring (5)); parent.lineNumber,
HexFormatter.formatNoHeader (buffer, startPtr, length).substring (7));
} }
String var = ""; String var = "";
@ -92,7 +93,7 @@ public class SubLine implements ApplesoftConstants
int stringPtr = 0; int stringPtr = 0;
int max = startPtr + length - 1; int max = startPtr + length - 1;
if (isEndOfLine (buffer[max])) while (isEndOfLine (buffer[max]))
--max; --max;
while (ptr <= max) while (ptr <= max)
@ -536,11 +537,16 @@ public class SubLine implements ApplesoftConstants
int ptr = startPtr + 1; int ptr = startPtr + 1;
int max = startPtr + length - 1; int max = startPtr + length - 1;
// apple format uses left-justified line numbers so the length varies
if (isFirst ()) if (isFirst ())
{ {
text.setLength (0); if (containsBackspaces (ptr, max)) // probably going to erase the line number
text.append (String.format (" %d REM ", parent.lineNumber)); // mimic apple {
// apple format uses left-justified line numbers so the length varies
text.setLength (0);
text.append (String.format (" %d REM ", parent.lineNumber)); // mimic apple
}
else
text.append (" REM ");
} }
else else
text.append ("REM "); text.append ("REM ");
@ -573,6 +579,17 @@ public class SubLine implements ApplesoftConstants
} }
} }
// ---------------------------------------------------------------------------------//
private boolean containsBackspaces (int ptr, int max)
// ---------------------------------------------------------------------------------//
{
while (ptr < max)
if (buffer[ptr++] == Utility.ASCII_BACKSPACE)
return true;
return false;
}
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
public String getAlignedText (int alignEqualsPos) public String getAlignedText (int alignEqualsPos)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//