Package net.emustudio.emulib.runtime.io
Class IntelHEX
- java.lang.Object
-
- net.emustudio.emulib.runtime.io.IntelHEX
-
@NotThreadSafe public class IntelHEX extends java.lang.Object
Generator and loader of 16-bit Intel Hex files (I8HEX).File format is described here: https://en.wikipedia.org/wiki/Intel_HEX
-
-
Constructor Summary
Constructors Constructor Description IntelHEX()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
add(byte data)
Add code byte into the code table.int
add(java.lang.String hexString)
Add a series of bytes, encoded as hex String, into the code table.void
add(java.util.Map<java.lang.Integer,java.lang.String> hexCodeMap)
Adds code (encoded as hex String) into the code table.int
findProgramLocation()
Finds program location in memory.void
generate(java.io.Writer writer)
Generates a Intel Hex file based on the cached program map.void
generate(java.lang.String outputFileName)
Generates a Intel Hex file based on the cached program map.java.util.Map<java.lang.Integer,java.lang.Byte>
getCode()
Get the program hex code, encoded as map of addresses/bytes.static <T extends java.lang.Number>
intloadIntoMemory(java.io.File file, MemoryContext<T> memory, java.util.function.Function<java.lang.Byte,T> convert)
Parse Intel HEX file and load it into memory.<T extends java.lang.Number>
voidloadIntoMemory(MemoryContext<T> mem, java.util.function.Function<java.lang.Byte,T> convert)
Method is similar to generateHex() method in that way, that compiled program is also transformed into chunk of bytes, but not to hex file but to the operating memory.static IntelHEX
parse(java.io.File file)
Parses Intel HEX file.void
setNextAddress(int address)
Set the next address where the next value will be assigned.
-
-
-
Method Detail
-
add
public int add(java.lang.String hexString)
Add a series of bytes, encoded as hex String, into the code table.The bytes must be given as a hexadecimal string of any length. Each byte must have a form of two characters. For example:
0A0B10
represents 3 bytes:
0x0A
,0x0B
and0x10
.The code table is modified so that all addresses starting from the current one up to the code length will contain corresponding byte.
The current address is then increased by the code length. If a byte exists on an address already, it is overwritten.
- Parameters:
hexString
- Hexadecimal representation of binary code- Returns:
- updated current address
-
add
public int add(byte data)
Add code byte into the code table.- Parameters:
data
- byte to add- Returns:
- next address
-
add
public void add(java.util.Map<java.lang.Integer,java.lang.String> hexCodeMap)
Adds code (encoded as hex String) into the code table. The code is added in the order from lowest to highest address found in the map.Keys of the map represent addresses and values represent code.
- Parameters:
hexCodeMap
- sub-table with addresses and codes
-
getCode
public java.util.Map<java.lang.Integer,java.lang.Byte> getCode()
Get the program hex code, encoded as map of addresses/bytes.- Returns:
- Map representing the program code
-
setNextAddress
public void setNextAddress(int address)
Set the next address where the next value will be assigned.- Parameters:
address
- next address
-
loadIntoMemory
public <T extends java.lang.Number> void loadIntoMemory(MemoryContext<T> mem, java.util.function.Function<java.lang.Byte,T> convert)
Method is similar to generateHex() method in that way, that compiled program is also transformed into chunk of bytes, but not to hex file but to the operating memory.- Type Parameters:
T
- Specific memory type- Parameters:
mem
- context of operating memoryconvert
- conversion of byte to T
-
generate
public void generate(java.lang.String outputFileName) throws java.io.IOException
Generates a Intel Hex file based on the cached program map.- Parameters:
outputFileName
- file name where to store the hex file- Throws:
java.io.IOException
- if the HEX file could not be written
-
generate
public void generate(java.io.Writer writer) throws java.io.IOException
Generates a Intel Hex file based on the cached program map.- Parameters:
writer
- Writer used to store the Intel HEX content- Throws:
java.io.IOException
- if the HEX file could not be written
-
findProgramLocation
public int findProgramLocation()
Finds program location in memory.It is actually the the first address which has occurred in the program.
- Returns:
- program memory location
-
parse
public static IntelHEX parse(java.io.File file) throws java.lang.Exception
Parses Intel HEX file.Lines starting with ; are ignored.
- Parameters:
file
- file- Returns:
- new instance of IntelHEX
- Throws:
java.lang.Exception
- if the file cannot be parsed
-
loadIntoMemory
public static <T extends java.lang.Number> int loadIntoMemory(java.io.File file, MemoryContext<T> memory, java.util.function.Function<java.lang.Byte,T> convert) throws java.lang.Exception
Parse Intel HEX file and load it into memory.- Type Parameters:
T
- specific memory type- Parameters:
file
- filememory
- memoryconvert
- conversion of byte to T- Returns:
- program start address
- Throws:
java.lang.Exception
- if the file cannot be parsed
-
-