From c44c7d9f97d08f03fda2dc240015ea3ee51393e1 Mon Sep 17 00:00:00 2001 From: uz Date: Sat, 10 Mar 2012 18:50:40 +0000 Subject: [PATCH] New attribute "base". git-svn-id: svn://svn.cc65.org/cc65/trunk@5590 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/sp65/asm.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/sp65/asm.c b/src/sp65/asm.c index e95bfc664..f39931a32 100644 --- a/src/sp65/asm.c +++ b/src/sp65/asm.c @@ -38,6 +38,7 @@ #include /* common */ +#include "check.h" #include "cmdline.h" #include "version.h" @@ -60,6 +61,7 @@ void WriteAsmFile (const StrBuf* Data, const Collection* A) FILE* F; const char* D; unsigned Size; + unsigned Base = 16; unsigned BytesPerLine = 16; char C; @@ -69,11 +71,18 @@ void WriteAsmFile (const StrBuf* Data, const Collection* A) /* Check for a bytesperline attribute */ const char* V = GetAttrVal (A, "bytesperline"); - if ((V && sscanf (V, "%u%c", &BytesPerLine, &C) != 1) || + if ((V && sscanf (V, "%u%c", &BytesPerLine, &C) != 1) || (BytesPerLine < 1 || BytesPerLine > 64)) { Error ("Invalid value for attribute `bytesperline'"); } + /* Check for a base attribute */ + V = GetAttrVal (A, "base"); + if ((V && sscanf (V, "%u%c", &Base, &C) != 1) || + (Base != 2 && Base != 10 && Base != 16)) { + Error ("Invalid value for attribute `base'"); + } + /* Open the output file */ F = fopen (Name, "w"); if (F == 0) { @@ -99,11 +108,32 @@ void WriteAsmFile (const StrBuf* Data, const Collection* A) /* Output one line */ unsigned Chunk = Size; if (Chunk > BytesPerLine) { - Chunk = BytesPerLine; + Chunk = BytesPerLine; } - fprintf (F, " .byte $%02X", (*D++ & 0xFF)); - for (I = 1; I < Chunk; ++I) { - fprintf (F, ",%02X", (*D++ & 0xFF)); + fputs (" .byte ", F); + for (I = 0; I < Chunk; ++I) { + unsigned char V = *D++; + if (I > 0) { + fputc (',', F); + } + switch (Base) { + case 2: + fprintf (F, "%%%u%u%u%u%u%u%u%u", + (V >> 7) & 0x01, (V >> 6) & 0x01, + (V >> 5) & 0x01, (V >> 4) & 0x01, + (V >> 3) & 0x01, (V >> 2) & 0x01, + (V >> 1) & 0x01, (V >> 0) & 0x01); + break; + + case 10: + fprintf (F, "%u", V); + break; + + case 16: + fprintf (F, "$%02X", V); + break; + + } } fputc ('\n', F); -- 2.39.5