/*
hxl (c) 2010-always Jan Panteltje

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


hxl can convert Intel hex format files to data in MCS-51 BASIC DATA statements.
The BASIC lines holding the data statements have the same start number as the code address 'org'.  
This makes it possible to have for example assembled code inline as DATA statements.
hxl also adds a for-next loop that pokes the code into RAM when called as a subroutine.   
hxl can also generate a binary file.


Example, given a Intel hex formt file file.hex that looks like this:
:10600000907000E0FA90700174032AF09000012271
:00000001FF

hxl -i file.hex -b file.bas -l 4

This generates the file file.bas, with maximum  4 data bytes per DATA statement:

24574 REM code from file.bas  by hxl from file.hex
24575 REM org=24576 last address used=24591
24576 DATA 144, 112, 0, 224
24577 DATA 250, 144, 112, 1
24578 DATA 116, 3, 42, 240
24579 DATA 144, 0, 1, 34
24580 FOR I=24576 TO 24591
24581 READ A : XBY(I)=A : NEXT
24582 RETURN


Example of a simple BASIC program that uses this code:

10 GOSUB 24574 : REM init, code to RAM
20 CALL 24576  : REM call code in RAM
30 END



In MCS 8052 BASIC use the command 'prog5' to prevent RAM from being erased  after a power sequence.
The code will then remain in RAM too.




You can also specify 1 data byte per DATA line, then the data in each BASIC DATA line corresponds to the code in RAM:

24574 REM code from file.bas  by hxl from file.hex
24575 REM org=24576 last address used=24591
24576 DATA 144
24577 DATA 112
24578 DATA 0
24579 DATA 224
24580 DATA 250
24581 DATA 144
24582 DATA 112
24583 DATA 1
24584 DATA 116
24585 DATA 3
24586 DATA 42
24587 DATA 240
24588 DATA 144
24589 DATA 0
24590 DATA 1
24591 DATA 34
24592 FOR I=24576 TO 24591
24593 READ A : XBY(I)=A : NEXT
24594 RETURN


If you now type:
print xby(24581)
it should show 144.



The above example was compiled with sdcc from this source file, wit hthe appended Makefile:
#include <math.h>
#include <malloc.h>
#include <stdio.h>


int dmain()
{
volatile __xdata __at (0x7000) unsigned char ival1;
volatile __xdata __at (0x7001) unsigned char uval1;

uval1 = ival1+3;

return 1;
}


This results in file.hex
:10600000907000E0FA90700174032AF09000012271
:00000001FF

Doing a:
make bas
results in file.bas:

24574 REM code from file.bas  by hxl from file.hex
24575 REM org=24576 last address used=24591
24576 DATA 144, 112, 0, 224, 250, 144, 112, 1, 116, 3
24577 DATA 42, 240, 144, 0, 1, 34
24578 FOR I=24576 TO 24591
24579 READ A : XBY(I)=A : NEXT
24580 RETURN




Uploading that to the MSC51 BASIC system, and adding a few BASIC lines to test it:
10     GOSUB 24574
20    XBY(7000H)=88
30     PRINT XBY(7001H)
40     CALL 24576
50     PRINT XBY(7001H)
100    END 
24574  REM code from file.bas  by hxl from file.hex
24575  REM org=24576 last address used=24591
24576  DATA 144,112,0,224,250,144,112,1,116,3
24577  DATA 42,240,144,0,1,34
24578  FOR I=24576 TO 24591
24579  READ A : XBY(I)=A :  NEXT 
24580  RETURN 

READY

>run
 0 
 91 

READY
>
 



Note:
When using C compiled code with the sdcc open source C compiler, use the following options:
CFLAGS := -c --main-return --model-large
LFLAGS := --code-loc 0x6000 --model-large

Rename the main() routine to somethin gelse, for example dmain(), else sdcc sets teh stackpointer,
and a return to MSC BASIC wil lno longer be possible.




Here and example for a Makefile:
# GNU Makefile example using SDCC
########################################################################
# GNU Makefile demonstrating combination of C and assembly source files
# File Name: makefile
# All targets in the makefile are processed sequentially by SDCC
# To create the file File.hex' using GNU make, just execute Make'
########################################################################

# The following lines defines additional directories to search for include files
#INCLUDES := -I"C:\Your Directory\Lab3\"

# The following line defines flags given to the SDCC C compiler
CFLAGS := -c --main-return --model-large $(INCLUDES)

# The following line defines flags given to the SDCC linker
# Non-specific: code-loc 0x6000 xram-loc 0xB000 model-large
#LFLAGS := --code-loc 0x6000 --code-size 0x5000 --xram-loc 0x000 --xram-size 0x8000 --model-large
#LFLAGS := --code-loc 0x6000 --model-large --xstack
LFLAGS := --code-loc 0x6000 --model-large

# The following line specifies the default target(s) to build
all: file.hex

# The following line specifies the object files that are to be linked together
OBJECTS := file.rel
# another_file.rel assembly_file.rel

# The following lines define a rule that sends the object files through the linker to
# create file.ihx which then has to be processed by packihx to create file.hex
file.hex : $(OBJECTS)
    sdcc $(LFLAGS) $^
    packihx file.ihx > file.hex

# The following rule sends each C file through the preprocessor and creates the asm file
# that is then assembled to create the rel file.
%.rel : %.c
    sdcc $(CFLAGS) $<

# The following rule sends each asm file (Not the asm files created by SDCC as an
# intermediate output of the compilation process.) through the assembler to create a rel
# file.
%.rel : %.asm
    asx8051 losa $<

# The following rule will clean all the derived objects from your directory. This will
# save you from accidentally tying Rm *' if you are developing on a UNIX platform.
# Note: To execute this rule type Make clean'
clean:
#   rm *.rel *.lst *.rst *.hex *.ihx
    rm *.sym *.asm *.mem *.map *.lnk *.lst *.hex *.ihx *.rel

bas:
    hxl -i file.hex -b file.bas

