From 459cde40c4a6e97570cf4ccf5c2fab1bfe87130e Mon Sep 17 00:00:00 2001
From: Andy McFadden
Date: Thu, 10 Jul 2025 11:17:41 -0700
Subject: [PATCH] Fix Merlin 32 DP op generation
Most assemblers treat '<' and '>' as byte-select operators. Merlin 32
treats them as shift operators, making '<' effectively a no-op. A
mask is applied based on the length implied by the opcode or pseudo-op,
e.g. "DFB = $100, the
lack of masking will cause it be treated as absolute. There is
currently no other mechanism to force the use of a direct-page opcode.
The only recourse is to append "&$ff" to the operand, so the
assembler knows that it can be handled as a DP op. The code generator
has been updated to add "&$ff" where needed, based on the label's
value. Unfortunately the assembler doesn't accept this for certain
specific addressing modes; this appears to be a bug.
The relevant test cases (2003x-labels-and-symbols) have been updated
to exercise these situations. The current test projects side-step the
failing assembler behavior by using a DP label instead. These should
be changed to correctly exercise the full behavior, with the code
generator outputting the instructions as raw hex values to work
around bugs.
There are some deliberately broken things (like a duplicate label) in
the 20030 case that were copied into the 20032 case when it was
created. For ease of editing these have been removed from 20032.
(issue #170)
---
Asm65/Formatter.cs | 57 +-
Asm65/OpDef.cs | 2 +-
SourceGen/AsmGen/AsmMerlin32.cs | 7 +-
SourceGen/AsmGen/GenCommon.cs | 19 +-
SourceGen/AsmGen/IGenerator.cs | 6 +
SourceGen/SGTestData/20030-labels-and-symbols | Bin 452 -> 460 bytes
.../SGTestData/20030-labels-and-symbols.dis65 | 36 +-
SourceGen/SGTestData/20032-labels-and-symbols | Bin 695 -> 729 bytes
.../SGTestData/20032-labels-and-symbols.dis65 | 665 ++++++++++++++----
.../20030-labels-and-symbols_64tass.S | 4 +
.../Expected/20030-labels-and-symbols_acme.S | 4 +
.../Expected/20030-labels-and-symbols_cc65.S | 4 +
.../20030-labels-and-symbols_merlin32.S | 4 +
.../20032-labels-and-symbols_64tass.S | 25 +-
.../Expected/20032-labels-and-symbols_acme.S | 3 +-
.../Expected/20032-labels-and-symbols_cc65.S | 25 +-
.../20032-labels-and-symbols_merlin32.S | 25 +-
.../20170-external-symbols_merlin32.S | 2 +-
.../Source/20030-labels-and-symbols.S | 4 +
.../Source/20032-labels-and-symbols.S | 23 +
docs/sgmanual/codegen.html | 7 +-
21 files changed, 726 insertions(+), 196 deletions(-)
diff --git a/Asm65/Formatter.cs b/Asm65/Formatter.cs
index 3734d73..a64e526 100644
--- a/Asm65/Formatter.cs
+++ b/Asm65/Formatter.cs
@@ -88,6 +88,8 @@ namespace Asm65 {
/// String to prefix operand with to force DP addressing.
public string ForceDirectOperandPrefix { get; set; } = string.Empty;
+ /// String to suffix operand with to force DP addressing.
+ public string ForceDirectOperandSuffix { get; set; } = string.Empty;
/// String to suffix opcode with to force abs addressing.
public string ForceAbsOpcodeSuffix { get; set; } = string.Empty;
/// String to prefix operand with to force abs addressing.
@@ -172,6 +174,7 @@ namespace Asm65 {
BankSelectBackQuote = src.BankSelectBackQuote;
ForceDirectOperandPrefix = src.ForceDirectOperandPrefix;
+ ForceDirectOperandSuffix = src.ForceDirectOperandSuffix;
ForceAbsOpcodeSuffix = src.ForceAbsOpcodeSuffix;
ForceAbsOperandPrefix = src.ForceAbsOperandPrefix;
ForceDirectOpcodeSuffix = src.ForceDirectOpcodeSuffix;
@@ -915,19 +918,23 @@ namespace Asm65 {
private string GenerateOperandFormat(OpDef.AddressMode addrMode,
OpDef.WidthDisambiguation wdis) {
string fmt;
- string wdisStr = string.Empty;
+ string pfxStr = string.Empty;
+ string dpsfxStr = string.Empty;
if (wdis == OpDef.WidthDisambiguation.ForceDirect) {
if (!string.IsNullOrEmpty(mFormatConfig.ForceDirectOperandPrefix)) {
- wdisStr = mFormatConfig.ForceDirectOperandPrefix;
+ pfxStr = mFormatConfig.ForceDirectOperandPrefix;
+ }
+ if (!string.IsNullOrEmpty(mFormatConfig.ForceDirectOperandSuffix)) {
+ dpsfxStr = mFormatConfig.ForceDirectOperandSuffix;
}
} else if (wdis == OpDef.WidthDisambiguation.ForceAbs) {
if (!string.IsNullOrEmpty(mFormatConfig.ForceAbsOperandPrefix)) {
- wdisStr = mFormatConfig.ForceAbsOperandPrefix;
+ pfxStr = mFormatConfig.ForceAbsOperandPrefix;
}
} else if (wdis == OpDef.WidthDisambiguation.ForceLong) {
if (!string.IsNullOrEmpty(mFormatConfig.ForceLongOperandPrefix)) {
- wdisStr = mFormatConfig.ForceLongOperandPrefix;
+ pfxStr = mFormatConfig.ForceLongOperandPrefix;
}
} else if (wdis == OpDef.WidthDisambiguation.ForceLongMaybe) {
// Don't add a width disambiguator to an operand that is unambiguously long.
@@ -940,35 +947,29 @@ namespace Asm65 {
case AddressMode.AbsLong:
case AddressMode.BlockMove:
case AddressMode.StackAbs:
- case AddressMode.DP:
- case AddressMode.DPPCRel: // BBR/BBS
+ case AddressMode.DPPCRel: // BBR/BBS (syntax varies)
case AddressMode.PCRel:
case AddressMode.PCRelLong: // BRL
case AddressMode.StackInt: // COP and two-byte BRK
case AddressMode.StackPCRelLong: // PER
case AddressMode.WDM:
- fmt = wdisStr + "{0}";
+ fmt = pfxStr + "{0}";
break;
case AddressMode.AbsIndexX:
case AddressMode.AbsIndexXLong:
- case AddressMode.DPIndexX:
- fmt = wdisStr + "{0}," + mXregChar;
+ fmt = pfxStr + "{0}," + mXregChar;
break;
- case AddressMode.DPIndexY:
case AddressMode.AbsIndexY:
- fmt = wdisStr + "{0}," + mYregChar;
+ fmt = pfxStr + "{0}," + mYregChar;
break;
case AddressMode.AbsIndexXInd:
- case AddressMode.DPIndexXInd:
- fmt = wdisStr + "({0}," + mXregChar + ")";
+ fmt = pfxStr + "({0}," + mXregChar + ")";
break;
case AddressMode.AbsInd:
- case AddressMode.DPInd:
case AddressMode.StackDPInd: // PEI
fmt = "({0})";
break;
case AddressMode.AbsIndLong:
- case AddressMode.DPIndLong:
// IIgs monitor uses "()" for AbsIndLong, E&L says "[]". Assemblers
// seem to expect the latter.
fmt = "[{0}]";
@@ -976,11 +977,31 @@ namespace Asm65 {
case AddressMode.Acc:
fmt = mAccChar;
break;
+ case AddressMode.DP:
+ fmt = pfxStr + "{0}" + dpsfxStr;
+ break;
+ case AddressMode.DPIndexX:
+ fmt = pfxStr + "{0}" + dpsfxStr + "," + mXregChar;
+ break;
+ case AddressMode.DPIndexY:
+ fmt = pfxStr + "{0}" + dpsfxStr + "," + mYregChar;
+ break;
+ case AddressMode.DPIndexXInd:
+ fmt = pfxStr + "({0}" + dpsfxStr + "," + mXregChar + ")";
+ break;
+ case AddressMode.DPInd:
+ fmt = "({0}" + dpsfxStr + ")";
+ break;
+ case AddressMode.DPIndLong:
+ // IIgs monitor uses "()" for AbsIndLong, E&L says "[]". Assemblers
+ // seem to expect the latter.
+ fmt = "[{0}" + dpsfxStr + "]";
+ break;
case AddressMode.DPIndIndexY:
- fmt = "({0})," + mYregChar;
+ fmt = "({0}" + dpsfxStr + ")," + mYregChar;
break;
case AddressMode.DPIndIndexYLong:
- fmt = "[{0}]," + mYregChar;
+ fmt = "[{0}" + dpsfxStr + "]," + mYregChar;
break;
case AddressMode.Imm:
case AddressMode.ImmLongA:
@@ -1021,7 +1042,7 @@ namespace Asm65 {
/// Formatted string.
public string FormatOperand(OpDef op, string contents, OpDef.WidthDisambiguation wdis) {
Debug.Assert(((int)op.AddrMode & 0xff) == (int) op.AddrMode);
- int key = (int) op.AddrMode | ((int)wdis << 8);
+ int key = (int) op.AddrMode | ((int)wdis << 8); // form unique cache key
if (!mOperandFormats.TryGetValue(key, out string format)) {
format = mOperandFormats[key] = GenerateOperandFormat(op.AddrMode, wdis);
diff --git a/Asm65/OpDef.cs b/Asm65/OpDef.cs
index ba3488e..ecb4905 100644
--- a/Asm65/OpDef.cs
+++ b/Asm65/OpDef.cs
@@ -124,7 +124,7 @@ namespace Asm65 {
///
public enum WidthDisambiguation : byte {
None = 0,
- ForceDirect, // only needed for forward DP label refs in single-pass assemblers
+ ForceDirect, // mainly needed for forward DP label refs in single-pass assemblers
ForceAbs,
ForceLong,
ForceLongMaybe // add opcode suffix but not operand prefix
diff --git a/SourceGen/AsmGen/AsmMerlin32.cs b/SourceGen/AsmGen/AsmMerlin32.cs
index b3a6d2f..944ea3e 100644
--- a/SourceGen/AsmGen/AsmMerlin32.cs
+++ b/SourceGen/AsmGen/AsmMerlin32.cs
@@ -178,6 +178,7 @@ namespace SourceGen.AsmGen {
Quirks.NoPcRelBankWrap = true;
Quirks.TracksSepRepNotEmu = true;
+ Quirks.ByteSelectionIsShift = true;
mWorkDirectory = workDirectory;
mFileNameBase = fileNameBase;
@@ -200,6 +201,7 @@ namespace SourceGen.AsmGen {
config.ForceAbsOpcodeSuffix = ":";
config.ForceLongOpcodeSuffix = "l";
config.ForceDirectOperandPrefix = string.Empty;
+ config.ForceDirectOperandSuffix = "&$ff";
config.ForceAbsOperandPrefix = string.Empty;
config.ForceLongOperandPrefix = string.Empty;
config.LocalVariableLabelPrefix = "]";
@@ -841,9 +843,10 @@ namespace SourceGen.AsmGen {
}
// Stdout: "C:\Src\WorkBench\Merlin32.exe v 1.0, (c) Brutal Deluxe ..."
- // "C:\Src\WorkBench\Merlin32.exe v 1.2 beta 1, (c) Brutal Deluxe ..."
+ // "... Merlin32.exe v 1.2 beta 1, (c) Brutal Deluxe ..."
+ // "... Merlin32_119.exe v1.1.9, (c) Brutal Deluxe ..."
// Other platforms may not have the ".exe". Start at first occurrence of " v ".
- private static string sVersionPattern = @" v (\d.\d)( [^,]+)?,";
+ private static string sVersionPattern = @" v *(\d.\d)(.\d)?( [^,]+)?,";
private static Regex sVersionRegex = new Regex(sVersionPattern);
// IAssembler
diff --git a/SourceGen/AsmGen/GenCommon.cs b/SourceGen/AsmGen/GenCommon.cs
index 51f69d8..81a606d 100644
--- a/SourceGen/AsmGen/GenCommon.cs
+++ b/SourceGen/AsmGen/GenCommon.cs
@@ -240,12 +240,13 @@ namespace SourceGen.AsmGen {
if (op.IsWidthPotentiallyAmbiguous) {
wdis = OpDef.GetWidthDisambiguation(instrLen, operand);
}
+ // Some DP addressing modes are ambiguous to one-pass assemblers.
if (gen.Quirks.SinglePassAssembler && wdis == OpDef.WidthDisambiguation.None &&
(op.AddrMode == OpDef.AddressMode.DP ||
- op.AddrMode == OpDef.AddressMode.DPIndexX) ||
- op.AddrMode == OpDef.AddressMode.DPIndexY) {
+ op.AddrMode == OpDef.AddressMode.DPIndexX ||
+ op.AddrMode == OpDef.AddressMode.DPIndexY)) {
// Could be a forward reference to a direct-page label. For ACME, we don't
- // care if it's forward or not.
+ // care if it's forward or not, only that it's referencing a user label.
if ((gen.Quirks.SinglePassNoLabelCorrection && IsLabelReference(gen, offset)) ||
IsForwardLabelReference(gen, offset)) {
wdis = OpDef.WidthDisambiguation.ForceDirect;
@@ -343,6 +344,18 @@ namespace SourceGen.AsmGen {
formattedOperand = PseudoOp.FormatNumericOperand(formatter, proj.SymbolTable,
lvLookup, gen.Localizer.LabelMap, dfd,
offset, operandForSymbol, operandLen, opFlags);
+ // Handle special case for DP args with non-DP labels.
+ if (gen.Quirks.ByteSelectionIsShift && wdis == OpDef.WidthDisambiguation.None &&
+ op.IsDirectPageInstruction && dfd.SymbolRef != null) {
+ // The '<' operator is effectively a no-op, so "LDA
+
The history is somewhat complicated, as there are two different versions
+of Merlin v1.1, updated by different authors. These are referred to as
+the "official" and "forked" versions.
-
Bugs:
+
Bugs (present in v1.0; unclear if/when these have been fixed):
PC relative branches don't wrap around at bank boundaries.
For some failures, an exit code of zero is returned.
@@ -357,6 +360,8 @@ target assembler doesn't handle it.
The byte selection operators ('<', '>', '^') are actually
word-selection operators, yielding 16-bit values when wide registers
are enabled on the 65816.
+
It's not possible to force direct-page addressing with a modifier.
+ "&$ff" must be added to the operand.
Values loaded into registers are implicitly mod 256 or 65536. There
is no need to explicitly mask an expression.
The assembler tracks register widths when it sees SEP/REP instructions,