From 3b86882357e8e02fc2740f16780d1c4620ea0c57 Mon Sep 17 00:00:00 2001 From: Ian C Date: Fri, 16 Dec 2016 16:49:34 +0000 Subject: Added initial files for CPC output. --- src/Makefile | 16 +-- src/casm.c | 1 + src/cpcout.c | 274 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/cpcout.h | 56 +++++++++++ src/example/Makefile | 5 +- src/output.c | 8 +- src/output.h | 1 + 7 files changed, 353 insertions(+), 8 deletions(-) create mode 100644 src/cpcout.c create mode 100644 src/cpcout.h diff --git a/src/Makefile b/src/Makefile index e95a5c8..68d4ef3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -49,7 +49,8 @@ SOURCE = casm.c \ gbout.c \ snesout.c \ libout.c \ - nesout.c + nesout.c \ + cpcout.c OBJECTS = casm.o \ expr.o \ @@ -76,7 +77,8 @@ OBJECTS = casm.o \ gbout.o \ snesout.o \ libout.o \ - nesout.o + nesout.o \ + cpcout.o $(TARGET): $(OBJECTS) $(CC) $(CLAGS) -o $(TARGET) $(OBJECTS) @@ -91,10 +93,12 @@ clean: 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 z80.h \ - 6502.h gbcpu.h 65c816.h spc700.h + specout.h t64out.h zx81out.h gbout.h snesout.h libout.h nesout.h \ + cpcout.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 +cpcout.o: cpcout.c global.h basetype.h util.h state.h codepage.h parse.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 @@ -112,7 +116,7 @@ nesout.o: nesout.c global.h basetype.h util.h state.h expr.h codepage.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 + nesout.h cpcout.h parse.o: parse.c global.h basetype.h util.h state.h codepage.h parse.h \ cmd.h rawout.o: rawout.c global.h basetype.h util.h state.h rawout.h parse.h \ @@ -122,7 +126,7 @@ snesout.o: snesout.c global.h basetype.h util.h state.h expr.h codepage.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 specout.o: specout.c global.h basetype.h util.h state.h specout.h parse.h \ - cmd.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 \ diff --git a/src/casm.c b/src/casm.c index 79cf3d6..cd5f116 100644 --- a/src/casm.c +++ b/src/casm.c @@ -693,6 +693,7 @@ int main(int argc, char *argv[]) PushValTableHandler(SNESOutputOptions(), SNESOutputSetOption); PushValTableHandler(LibOutputOptions(), LibOutputSetOption); PushValTableHandler(NESOutputOptions(), NESOutputSetOption); + PushValTableHandler(CPCOutputOptions(), CPCOutputSetOption); ClearState(); diff --git a/src/cpcout.c b/src/cpcout.c new file mode 100644 index 0000000..6dbafc1 --- /dev/null +++ b/src/cpcout.c @@ -0,0 +1,274 @@ +/* + + 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 . + + ------------------------------------------------------------------------- + + Commodore CPC tape output handler. + +*/ +#include +#include +#include + +#include "global.h" +#include "codepage.h" +#include "cpcout.h" +#include "expr.h" + + +/* ---------------------------------------- MACROS & TYPES +*/ + +enum option_t +{ + OPT_START_ADDR, +}; + +static const ValueTable option_set[] = +{ + {"cpc-start", OPT_START_ADDR}, + {NULL} +}; + +typedef struct +{ + int start_addr; +} Options; + +static Options options = {-1}; + +/* ---------------------------------------- PRIVATE FUNCTIONS +*/ + +static int PokeB(Byte *mem, int addr, Byte b) +{ + mem[addr++] = b; + return (addr % 0x10000); +} + + +static int PokeW(Byte *mem, int addr, int w) +{ + addr = PokeB(mem, addr, w & 0xff); + return PokeB(mem, addr, (w & 0xff00) >> 8); +} + + +static int PokeS(Byte *mem, int addr, const char *str) +{ + while(*str) + { + addr = PokeB(mem, addr, CodeFromNative(CP_ASCII, *str++)); + } + + return addr; +} + + +static void WriteByte(FILE *fp, Byte b) +{ + putc(b, fp); +} + + +static void WriteWord(FILE *fp, int w) +{ + WriteByte(fp, w & 0xff); + WriteByte(fp, (w & 0xff00) >> 8); +} + + +static void WriteLong(FILE *fp, unsigned long l) +{ + int f; + + for(f = 0; f < 4; f++) + { + WriteByte(fp, l & 0xff); + l >>= 8u; + } +} + + +static void WriteString(FILE *fp, const char *p, int len, + Byte fill, Codepage cp) +{ + while(len--) + { + WriteByte(fp, *p ? CodeFromNative(cp, *p++) : CodeFromNative(cp, fill)); + } +} + + +/* ---------------------------------------- INTERFACES +*/ +const ValueTable *CPCOutputOptions(void) +{ + return option_set; +} + +CommandStatus CPCOutputSetOption(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_START_ADDR: + CMD_EXPR(argv[0], options.start_addr); + break; + + default: + break; + } + + return stat; +} + +int CPCOutput(const char *filename, const char *filename_bank, + MemoryBank **bank, int count, char *error, size_t error_size) +{ + FILE *fp = fopen(filename, "wb"); + int f; + int offset; + + if (!fp) + { + snprintf(error, error_size, "Failed to create %s", filename); + return FALSE; + } + + /* Write signature + */ + WriteString(fp, "C64 tape image file", 32, 0, CP_ASCII); + + /* Write directory header + */ + WriteWord(fp, 0x200); + WriteWord(fp, count); + WriteWord(fp, count); + WriteString(fp, filename, 24, ' ', CP_ASCII); + + /* Offset to tape data + */ + offset = 64 + 32 * count; + + /* Write directory entries + */ + for(f = 0; f < count; f++) + { + int min, max, len; + + min = bank[f]->min_address_used; + max = bank[f]->max_address_used; + + /* If this is the first bank, we're going to prepend some BASIC + */ + if (f == 0) + { + if (min < 0x810) + { + snprintf(error, error_size, "First bank starts below a safe " + "area to add BASIC loader"); + + return FALSE; + } + + min = 0x801; /* Start of BASIC */ + } + + len = max - min + 1; + + WriteByte(fp, 1); + WriteByte(fp, 0x82); + WriteWord(fp, min); + WriteWord(fp, max + 1); + WriteLong(fp, offset); + + if (count == 1) + { + WriteString(fp, filename, 16, ' ', CP_CBM); + } + else + { + char fn[16]; + + snprintf(fn, sizeof fn, filename_bank, bank[f]->number); + WriteString(fp, fn, 16, ' ', CP_CBM); + } + + /* +2 is to include the 2-byte PRG header + offset += len + 2; + */ + offset += len; + } + + /* Write actual contents + */ + for(f = 0; f < count; f++) + { + Byte *mem; + int min, max, len; + + mem = bank[f]->memory; + + min = bank[f]->min_address_used; + max = bank[f]->max_address_used; + + /* If this is the first bank, we're going to prepend some BASIC. + Note that output drivers are allowed to manipulate memory directly. + */ + if (f == 0) + { + char sys[16]; + int a = 0x803; + int next; + + snprintf(sys, sizeof sys, "%u", min); + + a = PokeW(mem, a, 10); + a = PokeB(mem, a, 0x9e); + a = PokeS(mem, a, sys); + a = PokeB(mem, a, 0x00); + + next = a; + + a = PokeW(mem, a, 0x00); + + PokeW(mem, 0x801, next); + + min = 0x801; + } + + len = max - min + 1; + + fwrite(mem + min, len, 1, fp); + } + + fclose(fp); + + return TRUE; +} + + +/* +vim: ai sw=4 ts=8 expandtab +*/ diff --git a/src/cpcout.h b/src/cpcout.h new file mode 100644 index 0000000..7117f58 --- /dev/null +++ b/src/cpcout.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 . + + ------------------------------------------------------------------------- + + Amstrad CPC tape output + +*/ + +#ifndef CASM_CPCOUT_H +#define CASM_CPCOUT_H + +#include "parse.h" +#include "state.h" +#include "cmd.h" + +/* ---------------------------------------- INTERFACES +*/ + + +/* RAW Output options +*/ +const ValueTable *CPCOutputOptions(void); + +CommandStatus CPCOutputSetOption(int opt, int argc, char *argv[], + int quoted[], char *error, + size_t error_size); + + +/* C64 CPC tape output of assembly. Returns TRUE if OK, FALSE for failure. +*/ +int CPCOutput(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/example/Makefile b/src/example/Makefile index f9f15db..a1807a2 100644 --- a/src/example/Makefile +++ b/src/example/Makefile @@ -20,7 +20,7 @@ # Makefile for examples # -ALL = spectrum.tap c64.t64 zx81.p gb.gb vcs.bin snes.sfc nes.nes +ALL = spectrum.tap c64.t64 zx81.p gb.gb vcs.bin snes.sfc nes.nes cpc.cdt CASM = ../casm all: $(ALL) $(CASM) @@ -36,6 +36,9 @@ spectrum.tap: spectrum.asm $(CASM) c64.t64: c64.asm $(CASM) $(CASM) c64.asm +cpc.cdt: cpc.asm + $(CASM) cpc.asm + zx81.p: zx81.asm $(CASM) $(CASM) zx81.asm diff --git a/src/output.c b/src/output.c index 1067b96..66abfae 100644 --- a/src/output.c +++ b/src/output.c @@ -55,7 +55,8 @@ typedef enum GAMEBOY, SNES, LIBRARY, - NES + NES, + CPC } Format; static char output[4096] = "output"; @@ -73,6 +74,7 @@ static ValueTable format_table[] = {"snes", SNES}, {"lib", LIBRARY}, {"nes", NES}, + {"cpc", CPC}, {NULL} }; @@ -166,6 +168,10 @@ int OutputCode(void) return NESOutput(output, output_bank, bank, count, error, sizeof error); + case CPC: + return CPCOutput(output, output_bank, bank, count, + error, sizeof error); + default: break; } diff --git a/src/output.h b/src/output.h index b2e208c..84d0e33 100644 --- a/src/output.h +++ b/src/output.h @@ -39,6 +39,7 @@ #include "snesout.h" #include "libout.h" #include "nesout.h" +#include "cpcout.h" /* ---------------------------------------- INTERFACES */ -- cgit v1.2.3