Code at a specific location
Moderators: David Barker, Jerry Messina
-
- Posts: 219
- Joined: Wed Sep 11, 2013 1:27 pm
- Location: Chesterfield
Code at a specific location
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!
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!
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Code at a specific location
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:
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()
Re: Code at a specific location
There is an entire section about code relocation in the HELP file. Just read it.
- Attachments
-
- codereloc.png (44.9 KiB) Viewed 3952 times
-
- Posts: 219
- Joined: Wed Sep 11, 2013 1:27 pm
- Location: Chesterfield
Re: Code at a specific location
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.
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Code at a specific location
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.
If so, none of the #options org_xxx will do that.
-
- Posts: 219
- Joined: Wed Sep 11, 2013 1:27 pm
- Location: Chesterfield
Re: Code at a specific location
@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.
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.
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Code at a specific location
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...
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...
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.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)
-
- Posts: 219
- Joined: Wed Sep 11, 2013 1:27 pm
- Location: Chesterfield
Re: Code at a specific location
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
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...
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
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Code at a specific location
Sometimes it's a little finicky as to exactly where you place/invoke the macro.Just went back to it and it has gone in and worked fine!
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.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...
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.