From 607668671eb6d10e7ca036df81b3f288bce57339 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sat, 20 Nov 2021 10:01:52 +0000 Subject: Added Intel HEX output handler --- .gitignore | 3 + doc/casm.html | 35 +++++++++++ src/Makefile | 60 +++++++++--------- src/example/Makefile | 5 +- src/example/emucpm.z80 | 40 ++++++++++++ src/hexout.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++ src/hexout.h | 56 +++++++++++++++++ src/output.c | 8 ++- src/output.h | 1 + 9 files changed, 343 insertions(+), 30 deletions(-) create mode 100644 src/example/emucpm.z80 create mode 100644 src/hexout.c create mode 100644 src/hexout.h diff --git a/.gitignore b/.gitignore index 7b374ef..a325e0d 100644 --- a/.gitignore +++ b/.gitignore @@ -42,5 +42,8 @@ src/casm *.p *.sfc *.nes +*.prg +*.hex +*.cdt src/A-Hdrive src/output diff --git a/doc/casm.html b/doc/casm.html index d52c0ea..89a2fe0 100644 --- a/doc/casm.html +++ b/doc/casm.html @@ -1030,6 +1030,13 @@ An Amstrad CPC CDT file. A Commodore 64 PRG file. + +hex + + +An Intel HEX format file. + + @@ -1591,6 +1598,34 @@ to the start of written memory in the bank. +

Intel HEX Output Format

+

+Generates a Intel HEX file for an emulator or real hardware. This format is +used by various tools and programmers. +

+ +

Intel HEX Output Format options

+ +

The Intel HEX output driver supports the following settings that can be +set via an option command. +

+ + + + + + + + + +
OptionDescription
+option hex-null, value + +Sets the value that the output handler considers an empty byte that doesn't +need to be output. Defaults to zero. +
+ +

Listing

diff --git a/src/Makefile b/src/Makefile index 448d332..0d06ee0 100644 --- a/src/Makefile +++ b/src/Makefile @@ -51,7 +51,8 @@ SOURCE = casm.c \ libout.c \ nesout.c \ cpcout.c \ - prgout.c + prgout.c \ + hexout.c OBJECTS = casm.o \ expr.o \ @@ -80,7 +81,8 @@ OBJECTS = casm.o \ libout.o \ nesout.o \ cpcout.o \ - prgout.o + prgout.o \ + hexout.o $(TARGET): $(OBJECTS) $(CC) $(CLAGS) -o $(TARGET) $(OBJECTS) @@ -89,56 +91,58 @@ clean: rm -f $(TARGET) $(TARGET).exe $(OBJECTS) core *.core 6502.o: 6502.c global.h basetype.h util.h state.h expr.h label.h parse.h \ - cmd.h codepage.h 6502.h + cmd.h codepage.h 6502.h 65c816.o: 65c816.c global.h basetype.h util.h state.h expr.h label.h \ - parse.h cmd.h codepage.h 65c816.h + parse.h cmd.h codepage.h 65c816.h alias.o: alias.c global.h basetype.h util.h state.h alias.h casm.o: casm.c global.h basetype.h util.h state.h expr.h label.h macro.h \ - cmd.h parse.h codepage.h stack.h listing.h alias.h output.h rawout.h \ - specout.h t64out.h zx81out.h gbout.h snesout.h libout.h nesout.h \ - cpcout.h prgout.h z80.h 6502.h gbcpu.h 65c816.h spc700.h + cmd.h parse.h codepage.h stack.h listing.h alias.h output.h rawout.h \ + specout.h t64out.h zx81out.h gbout.h snesout.h libout.h nesout.h \ + cpcout.h prgout.h hexout.h z80.h 6502.h gbcpu.h 65c816.h spc700.h codepage.o: codepage.c global.h basetype.h util.h state.h codepage.h \ - parse.h cmd.h + parse.h cmd.h cpcout.o: cpcout.c global.h basetype.h util.h state.h codepage.h parse.h \ - cmd.h cpcout.h expr.h + cmd.h cpcout.h expr.h expr.o: expr.c global.h basetype.h util.h state.h expr.h label.h gbcpu.o: gbcpu.c global.h basetype.h util.h state.h expr.h label.h \ - parse.h cmd.h codepage.h varchar.h gbcpu.h + parse.h cmd.h codepage.h varchar.h gbcpu.h gbout.o: gbout.c global.h basetype.h util.h state.h expr.h codepage.h \ - parse.h cmd.h gbout.h + parse.h cmd.h gbout.h +hexout.o: hexout.c global.h basetype.h util.h state.h codepage.h parse.h \ + cmd.h hexout.h expr.h label.o: label.c global.h basetype.h util.h state.h codepage.h parse.h \ - cmd.h stack.h label.h + cmd.h stack.h label.h libout.o: libout.c global.h basetype.h util.h state.h libout.h parse.h \ - cmd.h label.h + cmd.h label.h listing.o: listing.c global.h basetype.h util.h state.h label.h macro.h \ - cmd.h parse.h expr.h varchar.h listing.h + cmd.h parse.h expr.h varchar.h listing.h macro.o: macro.c global.h basetype.h util.h state.h codepage.h parse.h \ - cmd.h varchar.h macro.h + cmd.h varchar.h macro.h nesout.o: nesout.c global.h basetype.h util.h state.h expr.h codepage.h \ - parse.h cmd.h nesout.h + parse.h cmd.h nesout.h output.o: output.c global.h basetype.h util.h state.h output.h parse.h \ - cmd.h rawout.h specout.h t64out.h zx81out.h gbout.h snesout.h libout.h \ - nesout.h cpcout.h prgout.h + cmd.h rawout.h specout.h t64out.h zx81out.h gbout.h snesout.h libout.h \ + nesout.h cpcout.h prgout.h hexout.h parse.o: parse.c global.h basetype.h util.h state.h codepage.h parse.h \ - cmd.h + cmd.h prgout.o: prgout.c global.h basetype.h util.h state.h codepage.h parse.h \ - cmd.h prgout.h expr.h + cmd.h prgout.h expr.h rawout.o: rawout.c global.h basetype.h util.h state.h rawout.h parse.h \ - cmd.h + cmd.h snesout.o: snesout.c global.h basetype.h util.h state.h expr.h codepage.h \ - parse.h cmd.h snesout.h + parse.h cmd.h snesout.h spc700.o: spc700.c global.h basetype.h util.h state.h expr.h label.h \ - parse.h cmd.h codepage.h spc700.h + parse.h cmd.h codepage.h spc700.h specout.o: specout.c global.h basetype.h util.h state.h specout.h parse.h \ - cmd.h expr.h + cmd.h expr.h stack.o: stack.c global.h basetype.h util.h state.h stack.h state.o: state.c global.h basetype.h util.h state.h expr.h t64out.o: t64out.c global.h basetype.h util.h state.h codepage.h parse.h \ - cmd.h t64out.h expr.h + cmd.h t64out.h expr.h util.o: util.c global.h basetype.h util.h state.h varchar.o: varchar.c global.h basetype.h util.h state.h codepage.h \ - parse.h cmd.h varchar.h + parse.h cmd.h varchar.h z80.o: z80.c global.h basetype.h util.h state.h expr.h label.h parse.h \ - cmd.h codepage.h varchar.h z80.h + cmd.h codepage.h varchar.h z80.h zx81out.o: zx81out.c global.h basetype.h util.h state.h codepage.h \ - parse.h cmd.h zx81out.h + parse.h cmd.h zx81out.h diff --git a/src/example/Makefile b/src/example/Makefile index a76a21f..700e9e5 100644 --- a/src/example/Makefile +++ b/src/example/Makefile @@ -21,7 +21,7 @@ # ALL = spectrum.tap t64.t64 zx81.p gb.gb vcs.bin snes.sfc nes.nes cpc.cdt \ - prg.prg + prg.prg hex.hex CASM = ../casm all: $(ALL) $(CASM) @@ -58,6 +58,9 @@ nes.nes: nes.asm tiles.chr nes.pal $(CASM) prg.prg: prg.asm $(CASM) $(CASM) prg.asm +hex.hex: emucpm.z80 $(CASM) + $(CASM) emucpm.z80 + clean: rm -f $(ALL) diff --git a/src/example/emucpm.z80 b/src/example/emucpm.z80 new file mode 100644 index 0000000..0920a86 --- /dev/null +++ b/src/example/emucpm.z80 @@ -0,0 +1,40 @@ +; +; Quick hack to emulate some CPM bdos calls (well, enough to make it work) +; + option output-file, hex.hex + option output-format, hex + + org 0 + halt + + org 5 + jp cpm + + org $f000 +cpm: + push af + push bc + push de + push hl + ld a,9 + cp c + call z,print_string + ld a,2 + cp c + call z,print_char + pop hl + pop de + pop bc + pop af + ret + +print_string: + ld bc,$0082 + out (c),a + ret + +print_char: + ld a,e + ld bc,$0080 + out (c),a + ret diff --git a/src/hexout.c b/src/hexout.c new file mode 100644 index 0000000..ef41634 --- /dev/null +++ b/src/hexout.c @@ -0,0 +1,165 @@ +/* + + casm - Simple, portable assembler + + Copyright (C) 2003-2015 Ian Cowburn (ianc@noddybox.co.uk) + + 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 3 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, see . + + ------------------------------------------------------------------------- + + Intel HEX output handler. + +*/ +#include +#include +#include + +#include "global.h" +#include "codepage.h" +#include "hexout.h" +#include "expr.h" + + +/* ---------------------------------------- MACROS & TYPES +*/ + +/* ---------------------------------------- PRIVATE TYPES AND VARS +*/ +enum option_t +{ + OPT_NULL_BYTE +}; + +static const ValueTable option_set[]= +{ + {"hex-null", OPT_NULL_BYTE}, + {NULL} +}; + +typedef struct +{ + int null_byte; +} Options; + +static Options options = +{ + 0 +}; + + +/* ---------------------------------------- PRIVATE FUNCTIONS +*/ + +/* ---------------------------------------- INTERFACES +*/ +const ValueTable *HEXOutputOptions(void) +{ + return option_set; +} + +CommandStatus HEXOutputSetOption(int opt, int argc, char *argv[], + int quoted[], char *err, size_t errsize) +{ + CommandStatus stat = CMD_OK; + + CMD_ARGC_CHECK(1); + + switch(opt) + { + case OPT_NULL_BYTE: + CMD_EXPR(argv[0], options.null_byte); + break; + + default: + break; + } + + return stat; +} + +int HEXOutput(const char *filename, const char *filename_bank, + MemoryBank **bank, int count, char *error, size_t error_size) +{ + int f; + + for(f = 0; f < count; f++) + { + FILE *fp; + char buff[4096]; + const char *name; + Byte *mem; + int r; + + if (count == 1) + { + name = filename; + } + else + { + snprintf(buff, sizeof buff, filename_bank, bank[f]->number); + name = buff; + } + + if (!(fp = fopen(name, "wb"))) + { + snprintf(error, error_size, "Failed to open %s", name); + return FALSE; + } + + mem = bank[f]->memory; + + for(r = 0; r < 0xffff; r += 16) + { + int n; + int found = 0; + + for(n = 0; n < 16 && !found; n++) + { + if (mem[r+n] != options.null_byte) + { + found = 1; + } + } + + if (found) + { + Byte csum = 0; + + fprintf(fp, ":10%4.4X00", r); + + for(n = 0; n < 16; n++) + { + fprintf(fp, "%2.2X", mem[r+n]); + csum += mem[r+n]; + } + + csum = ~csum; + csum++; + + fprintf(fp, "%2.2X\n", csum); + } + } + + fprintf(fp, ":00000001FF\n"); + fclose(fp); + } + + return TRUE; +} + + +/* +vim: ai sw=4 ts=8 expandtab +*/ diff --git a/src/hexout.h b/src/hexout.h new file mode 100644 index 0000000..d97b681 --- /dev/null +++ b/src/hexout.h @@ -0,0 +1,56 @@ +/* + + casm - Simple, portable assembler + + Copyright (C) 2003-2015 Ian Cowburn (ianc@noddybox.co.uk) + + 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 3 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, see . + + ------------------------------------------------------------------------- + + Intel HEX format output + +*/ + +#ifndef CASM_HEXOUT_H +#define CASM_HEXOUT_H + +#include "parse.h" +#include "state.h" +#include "cmd.h" + +/* ---------------------------------------- INTERFACES +*/ + + +/* HEX Output options +*/ +const ValueTable *HEXOutputOptions(void); + +CommandStatus HEXOutputSetOption(int opt, int argc, char *argv[], + int quoted[], char *error, + size_t error_size); + + +/* Intel HEX output of assembly. Returns TRUE if OK, FALSE for failure. +*/ +int HEXOutput(const char *filename, const char *filename_bank, + MemoryBank **bank, int count, + char *error, size_t error_size); + +#endif + +/* +vim: ai sw=4 ts=8 expandtab +*/ diff --git a/src/output.c b/src/output.c index 9a30a76..b02e36f 100644 --- a/src/output.c +++ b/src/output.c @@ -57,7 +57,8 @@ typedef enum LIBRARY, NES, CPC, - PRG + PRG, + HEX } Format; static char output[4096] = "output"; @@ -77,6 +78,7 @@ static ValueTable format_table[] = {"nes", NES}, {"cpc", CPC}, {"prg", PRG}, + {"hex", HEX}, {NULL} }; @@ -178,6 +180,10 @@ int OutputCode(void) return PRGOutput(output, output_bank, bank, count, error, sizeof error); + case HEX: + return HEXOutput(output, output_bank, bank, count, + error, sizeof error); + default: break; } diff --git a/src/output.h b/src/output.h index b63e3a0..90cda5c 100644 --- a/src/output.h +++ b/src/output.h @@ -41,6 +41,7 @@ #include "nesout.h" #include "cpcout.h" #include "prgout.h" +#include "hexout.h" /* ---------------------------------------- INTERFACES */ -- cgit v1.2.3