Fix extended address checksum

This commit is contained in:
Kimmo Kulovesi
2013-12-26 20:25:51 +02:00
parent c02c19ecec
commit 0e5196bc99
4 changed files with 23 additions and 10 deletions

View File

@@ -2,7 +2,7 @@ CC=clang
CFLAGS=-Wall -std=c99 -pedantic
OBJS = kk_ihex.o
BINS = bin2ihex bbin2ihex
BINS = bin2ihex
all: $(BINS)

View File

@@ -8,6 +8,8 @@
#include <stdio.h>
#include <stdlib.h>
//#define IHEX_WRITE_INITIAL_EXTENDED_ADDRESS_RECORD
int
main (void) {
struct ihex_state ihex;
@@ -16,6 +18,9 @@ main (void) {
ihex_init(&ihex);
ihex_write_at_address(&ihex, 0);
#ifdef IHEX_WRITE_INITIAL_EXTENDED_ADDRESS_RECORD
ihex.flags |= IHEX_FLAG_ADDRESS_OVERFLOW;
#endif
while ((count = fread(buf, 1, sizeof(buf), stdin))) {
ihex_write_bytes(&ihex, buf, count);
}

View File

@@ -108,13 +108,13 @@ ihex_linear_address (struct ihex_state *ihex) {
static char *
ihex_buffer_linear_address (char *w, const ihex_segment_t address_msb) {
unsigned int sum = IHEX_EXTENDED_LINEAR_ADDRESS_RECORD;
unsigned int sum = IHEX_EXTENDED_LINEAR_ADDRESS_RECORD + 2;
*w++ = IHEX_START; // :
w = ihex_buffer_byte(w, 0); // data
*w++ = IHEX_START; // :
w = ihex_buffer_byte(w, 2); // length
w = ihex_buffer_byte(w, 0); // address msb
w = ihex_buffer_byte(w, 0); // address lsb
w = ihex_buffer_byte(w, sum); // record type
w = ihex_buffer_byte(w, IHEX_EXTENDED_LINEAR_ADDRESS_RECORD); // record type
w = ihex_buffer_word(w, address_msb, &sum); // high bytes of 32-bit address
w = ihex_buffer_byte(w, 0x100U - sum); // checksum
return ihex_buffer_newline(w);
@@ -122,13 +122,13 @@ ihex_buffer_linear_address (char *w, const ihex_segment_t address_msb) {
static char *
ihex_buffer_segment_address (char *w, const ihex_segment_t segment) {
unsigned int sum = IHEX_EXTENDED_SEGMENT_ADDRESS_RECORD;
unsigned int sum = IHEX_EXTENDED_SEGMENT_ADDRESS_RECORD + 2;
*w++ = IHEX_START; // :
w = ihex_buffer_byte(w, 0); // data
w = ihex_buffer_byte(w, 2); // length
w = ihex_buffer_byte(w, 0); // address msb
w = ihex_buffer_byte(w, 0); // address lsb
w = ihex_buffer_byte(w, sum); // record type
w = ihex_buffer_byte(w, IHEX_EXTENDED_SEGMENT_ADDRESS_RECORD); // record type
w = ihex_buffer_word(w, segment, &sum); // segment selector
w = ihex_buffer_byte(w, 0x100U - sum); // checksum
return ihex_buffer_newline(w);
@@ -140,7 +140,7 @@ ihex_buffer_segment_address (char *w, const ihex_segment_t segment) {
static char *
ihex_buffer_end_of_file (char *w) {
*w++ = IHEX_START; // :
w = ihex_buffer_byte(w, 0); // data
w = ihex_buffer_byte(w, 0); // length
w = ihex_buffer_byte(w, 0); // address msb
w = ihex_buffer_byte(w, 0); // address lsb
w = ihex_buffer_byte(w, IHEX_END_OF_FILE_RECORD); // record type

View File

@@ -21,6 +21,14 @@
* may be overwritten immediately after `ihex_flush_buffer` returns;
* it must be copied if it needs to be preserved.
*
* For outputs larger than 64KiB, 32-bit linear addresses are output. Normally
* the initial linear extended address record of zero is NOT written - it can
* be forced by setting `ihex->flags |= IHEX_FLAG_ADDRESS_OVERFLOW` before
* writing the first byte.
*
* Gaps in the data may be created by calling `ihex_write_at_address` with the
* new starting address without calling `ihex_end_write` in between.
*
*
* Copyright (c) 2013 Kimmo Kulovesi, http://arkku.com/
* Provided with absolutely no warranty, use at your own risk only.
@@ -46,7 +54,7 @@ struct ihex_state {
uint8_t data[IHEX_LINE_LENGTH];
};
#define IHEX_FLAG_ADDRESS_OVERFLOW 1
#define IHEX_FLAG_ADDRESS_OVERFLOW 1 // 16-bit address overflow
enum ihex_record_type {
IHEX_DATA_RECORD,