diff frbl/test/frbl2.c @ 323:cefa700d1b8f

frbl: beginning of frbl2test
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 05 Mar 2020 22:05:01 +0000
parents
children 43c92df87ac6
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frbl/test/frbl2.c	Thu Mar 05 22:05:01 2020 +0000
@@ -0,0 +1,118 @@
+#include <sys/types.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include "srecreader.h"
+
+extern char *target_ttydev;
+extern int target_fd;
+extern struct srecreader srimage;
+
+#define	MAX_IMAGE_LEN	32768
+
+static u_char codeimage[MAX_IMAGE_LEN];
+static unsigned codeimage_len;
+static uint32_t loadaddr;
+
+read_srec_image()
+{
+	u_char *writep;
+	uint32_t endaddr;
+	int i;
+
+	if (open_srec_file(&srimage) < 0)
+		exit(1);
+	for (;;) {
+		if (read_s_record(&srimage) < 0)
+			exit(1);
+		switch (srimage.record_type) {
+		case '0':
+			if (srimage.lineno == 1)
+				continue;
+			fprintf(stderr,
+		"%s: S0 record found in line %d (expected in line 1 only)\n",
+				srimage.filename, srimage.lineno);
+			exit(1);
+		case '3':
+		case '7':
+			if (s3s7_get_addr_data(&srimage) < 0)
+				exit(1);
+			break;
+		default:
+			fprintf(stderr,
+				"%s line %d: S%c record type not supported\n",
+				srimage.filename, srimage.lineno,
+				srimage.record_type);
+			exit(1);
+		}
+		if (srimage.record_type == '7')
+			break;
+		/* must be S3 */
+		if (srimage.datalen < 1) {
+			fprintf(stderr,
+				"%s line %d: S3 record has zero data length\n",
+				srimage.filename, srimage.lineno);
+			exit(1);
+		}
+		if (srimage.datalen & 1) {
+			fprintf(stderr,
+				"%s line %d: S3 record has odd data length\n",
+				srimage.filename, srimage.lineno);
+			exit(1);
+		}
+		if (srimage.addr & 1) {
+			fprintf(stderr,
+				"%s line %d: S3 record has odd address\n",
+				srimage.filename, srimage.lineno);
+			exit(1);
+		}
+		/* handle first record */
+		if (!codeimage_len) {
+			endaddr = loadaddr = srimage.addr;
+			writep = codeimage;
+		}
+		if (srimage.addr != endaddr) {
+			fprintf(stderr, "%s line %d: address discontinuity\n",
+				srimage.filename, srimage.lineno);
+			exit(1);
+		}
+		if (codeimage_len + srimage.datalen > MAX_IMAGE_LEN) {
+			fprintf(stderr,
+				"%s line %d: max image length exceeded\n",
+				srimage.filename, srimage.lineno);
+			exit(1);
+		}
+		/* reverse byte order */
+		for (i = 0; i < srimage.datalen; i += 2) {
+			*writep++ = srimage.record[i+6];
+			*writep++ = srimage.record[i+5];
+		}
+		endaddr += srimage.datalen;
+		codeimage_len += srimage.datalen;
+	}
+	/* got S7 */
+	fclose(srimage.openfile);
+	if (!codeimage_len) {
+		fprintf(stderr,
+		"%s line %d: S7 without any preceding S3 data records\n",
+			srimage.filename, srimage.lineno);
+		exit(1);
+	}
+	if (srimage.addr != loadaddr) {
+		fprintf(stderr,
+		"%s line %d: S7 address differs from image load address\n",
+			srimage.filename, srimage.lineno);
+		exit(1);
+	}
+	/* all good */
+	return(0);
+}
+
+frbl_test_main()
+{
+	read_srec_image();
+	/* remainder to be implemented */
+}