From 7f4b14ee49464b32743159408ac23e02ca1e8c07 Mon Sep 17 00:00:00 2001
From: IrgendwerA8 <c.krueger.b@web.de>
Date: Sun, 14 Aug 2016 19:33:09 +0200
Subject: [PATCH] SMC macro fixes for changed .paramcount and byte overflow
 behavior

---
 asminc/smc.inc | 52 +++++++++++++++++++++++++++++++++++---------------
 1 file changed, 37 insertions(+), 15 deletions(-)

diff --git a/asminc/smc.inc b/asminc/smc.inc
index 383417c3d..8a06f3222 100644
--- a/asminc/smc.inc
+++ b/asminc/smc.inc
@@ -1,7 +1,7 @@
 ; smc.mac
 ; ca65 Macro-Pack for Self Modifying Code (SMC)
 ;
-; (c) Christian Kr�ger, latest change: 09-Nov-2011
+; (c) Christian Kr�ger, latest change: 17-Jul-2016
 ;
 ; This software is provided 'as-is', without any expressed or implied
 ; warranty.  In no event will the authors be held liable for any damages
@@ -53,7 +53,7 @@ _SMCDesignator: statement
 .endmacro
 
 .macro SMC_TransferOpcode       label, opcode, register
-.if .paramcount = 2 .or .match ({register}, a)
+.if .paramcount = 2 .or .match ({register}, a) .or .match ({register}, )
         lda #opcode
         sta _SMCDesignator
 .elseif .match ({register}, x)
@@ -62,44 +62,52 @@ _SMCDesignator: statement
 .elseif .match ({register}, y)
         ldy #opcode
         sty _SMCDesignator
+.else
+	.error "Invalid usage of macro 'SMC_TransferOpcode'"
 .endif
 .endmacro
 
 .macro SMC_LoadOpcode   label, register
-.if .paramcount = 1 .or .match ({register}, a)
+.if .paramcount = 1 .or .match ({register}, a) .or .match ({register}, )
         lda _SMCDesignator
 .elseif .match ({register}, x)
         ldx _SMCDesignator
 .elseif .match ({register}, y)
         ldy _SMCDesignator
+.else
+	.error "Invalid usage of macro 'SMC_TransferOpcode'"
 .endif
 .endmacro
 
 .macro SMC_StoreOpcode  label, register
-.if .paramcount = 1 .or .match ({register}, a)
+.if .paramcount = 1 .or .match ({register}, a) .or .match ({register}, )
         sta _SMCDesignator
 .elseif .match ({register}, x)
         stx _SMCDesignator
 .elseif .match ({register}, y)
         sty _SMCDesignator
+.else
+	.error "Invalid usage of macro 'SMC_StoreOpcode'"
 .endif
 .endmacro
 
 .macro SMC_ChangeBranch         label, destination, register
-.if .paramcount = 2 .or .match ({register}, a)
-        lda #(destination - _SMCDesignator -2)
+.if .paramcount = 2 .or .match ({register}, a) .or .match ({register}, )
+        lda #(<(destination - _SMCDesignator -2))
         sta _SMCDesignator+1
 .elseif .match ({register}, x)
-        ldx #(destination - _SMCDesignator - 2)
+        ldx #(<(destination - _SMCDesignator - 2))
         stx _SMCDesignator+1
 .elseif .match ({register}, y)
-        ldy #(destination - _SMCDesignator - 2)
+        ldy #(<(destination - _SMCDesignator - 2))
         sty _SMCDesignator+1
+.else
+	.error "Invalid usage of macro 'SMC_ChangeBranch'"
 .endif
 .endmacro
 
 .macro SMC_TransferValue        label, value, register
-.if .paramcount = 2 .or .match ({register}, a)
+.if .paramcount = 2 .or .match ({register}, a) .or .match ({register}, )
         lda value
         sta _SMCDesignator+1
 .elseif .match ({register}, x)
@@ -108,26 +116,32 @@ _SMCDesignator: statement
 .elseif .match ({register}, y)
         ldy value
         sty _SMCDesignator+1
+.else
+	.error "Invalid usage of macro 'SMC_TransferValue'"
 .endif
 .endmacro
 
 .macro SMC_LoadValue    label, register
-.if .paramcount = 1 .or .match ({register}, a)
+.if .paramcount = 1 .or .match ({register}, a) .or .match ({register}, )
         lda _SMCDesignator+1
 .elseif .match ({register}, x)
         ldx _SMCDesignator+1
 .elseif .match ({register}, y)
         ldy _SMCDesignator+1
+.else
+	.error "Invalid usage of macro 'SMC_LoadValue'"
 .endif
 .endmacro
 
 .macro SMC_StoreValue   label, register
-.if .paramcount = 1 .or .match ({register}, a)
+.if .paramcount = 1 .or .match ({register}, a) .or .match ({register}, )
         sta _SMCDesignator+1
 .elseif .match ({register}, x)
         stx _SMCDesignator+1
 .elseif .match ({register}, y)
         sty _SMCDesignator+1
+.else
+	.error "Invalid usage of macro 'SMC_StoreValue'"
 .endif
 .endmacro
 
@@ -145,7 +159,7 @@ SMC_StoreValue label, register
 .endmacro
 
 .macro SMC_TransferHighByte     label, value, register
-.if .paramcount = 2 .or .match ({register}, a)
+.if .paramcount = 2 .or .match ({register}, a) .or .match ({register}, )
         lda value
         sta _SMCDesignator+2
 .elseif .match ({register}, x)
@@ -154,31 +168,37 @@ SMC_StoreValue label, register
 .elseif .match ({register}, y)
         ldy value
         sty _SMCDesignator+2
+.else
+	.error "Invalid usage of macro 'SMC_TransferHighByte'"
 .endif
 .endmacro
 
 .macro SMC_LoadHighByte label, register
-.if .paramcount = 1 .or .match ({register}, a)
+.if .paramcount = 1 .or .match ({register}, a) .or .match ({register}, )
         lda _SMCDesignator+2
 .elseif .match ({register}, x)
         ldx _SMCDesignator+2
 .elseif .match ({register}, y)
         ldy _SMCDesignator+2
+.else
+	.error "Invalid usage of macro 'SMC_LoadHighByte'"
 .endif
 .endmacro
 
 .macro SMC_StoreHighByte        label, register
-.if .paramcount = 1 .or .match ({register}, a)
+.if .paramcount = 1 .or .match ({register}, a) .or .match ({register}, )
         sta _SMCDesignator+2
 .elseif .match ({register}, x)
         stx _SMCDesignator+2
 .elseif .match ({register}, y)
         sty _SMCDesignator+2
+.else
+	.error "Invalid usage of macro 'SMC_StoreHighByte'"
 .endif
 .endmacro
 
 .macro SMC_TransferAddressSingle        label, address, register
-.if .paramcount = 2 .or .match ((register), a)
+.if .paramcount = 2 .or .match ((register), a) .or .match ({register}, )
         .if (.match (.left (1, {address}), #))
                 ; immediate mode
                 lda #<(.right (.tcount ({address})-1, {address}))
@@ -220,6 +240,8 @@ SMC_StoreValue label, register
                 ldy 1+(address)
                 sty _SMCDesignator+2
         .endif
+.else
+	.error "Invalid usage of macro 'SMC_TransferAddressSingle'"
 .endif
 .endmacro