Code at a specific location

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Code at a specific location

Post by SHughes_Fusion » Fri Feb 06, 2015 9:44 am

I'm looking at making a modification to the Bluetooth bootloader I wrote and I was wondering if there is any way in Swordfish to put code in a particular location.

I've started playing with RCON and I've decided to change the bootloader so the main entry mechanism is the RESET command.

What I was hoping to do is start the bootloader with a jump to the main program. If there is no firmware programmed I need this location to contain the RESET command so the processor restarts, the bootloader spots the reset type and runs.

This seems a neater way than my current solution which is to write an EEPROM location to show there is firmware and re-write it to re-enter the bootloader.

So, how do I get location $800 to contain the RESET instruction?

TIA!

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Code at a specific location

Post by Jerry Messina » Fri Feb 06, 2015 10:35 am

I think what you're looking for is something like this...

If you want to add a reset instruction at 0x800 to your bootloader code you could use:

Code: Select all

// This macro creates an initialization record in the hex file
// it jams a RESET instruction at location 0x800
macro SET_RESET()
asm
    cur_loc set $                 ; record current program address
    org 0x800                     ; point to the location in program space
    reset                         ; insert a RESET instruction
    org cur_loc                   ; set program addr back to original
end asm
end macro

SET_RESET()

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Re: Code at a specific location

Post by octal » Fri Feb 06, 2015 2:20 pm

There is an entire section about code relocation in the HELP file. Just read it.
Attachments
codereloc.png
codereloc.png (44.9 KiB) Viewed 3954 times

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: Code at a specific location

Post by SHughes_Fusion » Fri Feb 06, 2015 2:25 pm

I did read that section before I posted. However, I was not able to work out whether those options would actually do what I want. The wording is not clear and assumes knowledge of the underlying ASM code which I don't have.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Code at a specific location

Post by Jerry Messina » Fri Feb 06, 2015 2:31 pm

I was under the impression that you just wanted to add a reset instruction @ 0x800 to your existing bootloader code.

If so, none of the #options org_xxx will do that.

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: Code at a specific location

Post by SHughes_Fusion » Fri Feb 06, 2015 2:37 pm

@Jerry - Yes, you are correct. And having just tried what Octal pointed at my original impression was correct and those options will not work.

I've not tried the code you suggested yet as I'm chasing another problem but I've just given it a quick go and now I need to learn how to use macros in Swordfish! And unfortunately if you search for 'macro' in Octal's beloved help file you get no topics found....

One for Monday I think as the other problem is more pressing.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Code at a specific location

Post by Jerry Messina » Fri Feb 06, 2015 3:08 pm

Unfortunately, macros aren't really documented. In a nutshell, they're a way of doing text substitution.

You can find some references here on the forum:
http://sfcompiler.co.uk/phpBB3/viewtopic.php?f=3&t=1308
http://sfcompiler.co.uk/phpBB3/viewtopic.php?f=3&t=1318


Here's some addt'l notes I had filed away from a PM...
Here is a sample program, which will give you some idea...

=============================================
// values that can be used in CheckParam()
Const
cpConst = $01,
cpVariable = $02,
cpArray = $04,
cpSize01 = $08,
cpSize08 = $10,
cpSize16 = $20,
cpSize32 = $40,

cpInteger = $0100,
cpReal = $0200,
cpString = $0400,
cpChar = $0800,
cpBoolean = $1000,

etError = 0,
etWarning = 1,
etMessage = 2,
etHint = 3


Macro movlw(val)
if not CheckParam(val, cpConst or cpInteger or cpsize01 or cpSize08) then
CheckParam(etError, "Value must be an 8 bit constant")
endif
asm
movlw val
end asm
End Macro


dim Value as byte
Value = 0

movlw(10)
movlw(Value) // generate error

=============================================

There are some limitations though. For example, performing CheckParam() is pretty much
a waste of time if the constant ends up on the left had side of an expression. As always,
it's best to manually replace the macro to get some idea what might be going on. For example,

macro mymac(a)
a = 10
end macro

mymac(100)

gives

100 = 10

the swordfish parser will throw a syntax error before it gets to semantic processing
(which is when CheckParam() executes)

in short, macros are not going to be bullet proof in terms of general error reporting,
but they are still useful. Also remember that swordfish macros have scope

macro mymac(a) // private
end macro

public macro mymac(a) // public
end macro

also, you have limited overloading...

Macro mymac(a)
a = 0
End Macro

Macro mymac(a, b)
a = b
End Macro

Dim a,b As Byte
mymac(a)
mymac(a,b)
Also, I should have pointed out that if you use that macro from my previous post you'll only see the data in the .hex file... it won't appear in the .cof file so if you're debugging w/mplab you won't see it.

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: Code at a specific location

Post by SHughes_Fusion » Fri Feb 06, 2015 3:19 pm

Thanks, Jerry.

The problem I had was that when I inserted the macro in to my code all my subs vanished from the code explorer window.

I didn't have time to work out what I was doing wrong so gave up.

Just went back to it and it has gone in and worked fine! Must just be one of those Swordfish things - the other day I had an error saying there was no BASIC file when I tried to compile! Closing and reopening SF fixed that one.

Presumably I don't actually need to use the Macro, I could just add

Code: Select all

Asm
  ORG 0x800
  RESET
End ASM
at the very end of my code to obtain the same effect? From what I can tell from the .asm file I don't actually need to save and restore the program address that way but there could be something I've missed...

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Code at a specific location

Post by Jerry Messina » Fri Feb 06, 2015 3:37 pm

Just went back to it and it has gone in and worked fine!
Sometimes it's a little finicky as to exactly where you place/invoke the macro.
Presumably I don't actually need to use the Macro, I could just add
Asm
ORG 0x800
RESET
End ASM
at the very end of my code to obtain the same effect? From what I can tell from the .asm file I don't actually need to save and restore the program address that way but there could be something I've missed...
If you do that there will be code located AFTER the asm RESET instruction, and I doubt you want that. The "saving/restoring" the current PC address statements are purely directives to the assembler... they don't produce any code other than the RESET instruction.

Put the 'SET_RESET()' macro invocation as the last line of your bootloader program module, and it should be happy. There may be other syntax that would work too, but I haven't run across one.

Post Reply