From 1c7ab4833051639938a46d03413e5528487becf3 Mon Sep 17 00:00:00 2001 From: Neil McPhail Date: Mon, 11 Nov 2024 10:17:34 +0000 Subject: [PATCH] Simplify build by adapting checksum program This now prepends length bytes as well as adding checksum --- .gitignore | 2 +- Makefile | 41 +++++++++++++--------------- append_checksum.c | 26 ------------------ body.asm | 7 ----- header.asm | 2 +- program.asm | 5 ---- ttttt.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 89 insertions(+), 63 deletions(-) delete mode 100644 append_checksum.c delete mode 100644 body.asm delete mode 100644 program.asm create mode 100644 ttttt.c diff --git a/.gitignore b/.gitignore index 16e44d5..fe93128 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.tap *bin *bincs -ac +ttttt diff --git a/Makefile b/Makefile index df5d24d..d255a68 100644 --- a/Makefile +++ b/Makefile @@ -1,33 +1,28 @@ -program.tap: program.asm headerbincs body.tap - pasmo program.asm program.tap +program.tap: header.block body.block + cat header.block body.block > program.tap -headerbincs: headerbin ac - cp headerbin headerbincs - ./ac headerbincs +header.block: header ttttt + ./ttttt header -headerbin: header.tap - dd if=header.tap of=headerbin bs=1 count=18 +header: header.asm body.block + pasmo header.asm headerlong + dd if=headerlong of=header bs=18 count=1 + rm -f headerlong -header.tap: header.asm body.tap - pasmo header.asm header.tap +body.block: body ttttt + ./ttttt body -body.tap: body.asm bodybincs - pasmo body.asm body.tap +body: remload.asm code.asm + pasmo remload.asm body -bodybincs: bodybin ac - cp bodybin bodybincs - ./ac bodybincs - -bodybin: remload.asm code.asm - pasmo remload.asm bodybin - -ac: append_checksum.c - cc append_checksum.c -o ac +ttttt: ttttt.c + cc ttttt.c -o ttttt clean: rm -f *.tap - rm -f *bin - rm -f *bincs - rm -f ac + rm -f *.block + rm -f body + rm -f header + rm -f ttttt .PHONY: clean diff --git a/append_checksum.c b/append_checksum.c deleted file mode 100644 index fb1f309..0000000 --- a/append_checksum.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include - -int main(int argc, char *argv[]) -{ - int fd; - unsigned char tally = 0; - unsigned char next = 0; - if (argc!=2) return -1; - - fd = open(argv[1], O_RDWR|O_APPEND); - if (fd<0) return -2; - - while (read(fd, &next, 1)) { - //printf("Tally: %X, next byte: %X\n", tally, next); - tally ^= next; - }; - - printf("Final tally: %X\n", tally); - - write(fd, &tally, 1); - - close(fd); - return 0; -} diff --git a/body.asm b/body.asm deleted file mode 100644 index 3796e6e..0000000 --- a/body.asm +++ /dev/null @@ -1,7 +0,0 @@ -bodylength: - dw bodyend - bodycontent - -bodycontent: -INCBIN bodybincs - -bodyend: diff --git a/header.asm b/header.asm index 7c0758a..6d13bfa 100644 --- a/header.asm +++ b/header.asm @@ -20,5 +20,5 @@ checksum: db 0 bodytap: -INCBIN body.tap +INCBIN body.block endbodytap: diff --git a/program.asm b/program.asm deleted file mode 100644 index cebb655..0000000 --- a/program.asm +++ /dev/null @@ -1,5 +0,0 @@ -headlength: - dw 19 - -INCBIN headerbincs -INCBIN body.tap diff --git a/ttttt.c b/ttttt.c new file mode 100644 index 0000000..60c81cc --- /dev/null +++ b/ttttt.c @@ -0,0 +1,69 @@ +/* McPhail's Tip-Top TAP Top-Tailer + * Takes a raw code file as input + * Prepends data length and appends xor checksum + * Outputs to new file with .block suffix */ + + +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int fdin, fdout; + char outfilename[PATH_MAX]; + unsigned int count = 0; + unsigned char tally = 0; + unsigned char next = 0; + if (argc!=2) { + printf("Please specify file to read.\n"); + return -1; + } + + fdin = open(argv[1], O_RDONLY); + if (fdin<0) { + printf("Couldn't open %s. for reading\n", argv[1]); + return -1; + } + + snprintf(outfilename, PATH_MAX, "%s.block", argv[1]); + outfilename[PATH_MAX - 1] = '\0'; + if (! strcmp(argv[1], outfilename)) { + printf("Filename too long - would clobber existing.\n"); + close(fdin); + return -1; + } + + fdout = open(outfilename, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); + if (fdout<0) { + printf("Couldn't open %s for writing.\n", outfilename); + close(fdin); + return -1; + } + + while (read(fdin, &next, 1)) { + tally ^= next; + count ++; + }; + lseek(fdin, 0, SEEK_SET); + + char lsb = (count+1)&255; + char msb = ((count+1)>>8)&255; + write(fdout, &lsb, 1); + write(fdout, &msb, 1); + while (read(fdin, &next, 1)) { + write(fdout, &next, 1); + } + write(fdout, &tally, 1); + + close(fdout); + close(fdin); + + printf("File %s written.\n", outfilename); + printf("Code length including first byte is %d (0x%.4X).\n", count, count); + printf("Removing first byte gives 0x%.4X.\n", count-1); + + return 0; +}