2012-02-27 21:14:46 +00:00
|
|
|
// ACME - a crossassembler for producing 6502/65c02/65816 code.
|
2014-03-10 00:17:10 +00:00
|
|
|
// Copyright (C) 1998-2014 Marco Baye
|
2012-02-27 21:14:46 +00:00
|
|
|
// Have a look at "acme.c" for further info
|
|
|
|
//
|
|
|
|
// Character encoding stuff
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "alu.h"
|
|
|
|
#include "acme.h"
|
|
|
|
#include "dynabuf.h"
|
|
|
|
#include "encoding.h"
|
2014-12-03 22:18:06 +00:00
|
|
|
#include "global.h" // FIXME - remove when no longer needed
|
2012-02-27 21:14:46 +00:00
|
|
|
#include "output.h"
|
2014-12-03 22:18:06 +00:00
|
|
|
#include "pseudoopcodes.h" // FIXME - remove when no longer needed
|
2012-02-27 21:14:46 +00:00
|
|
|
#include "input.h"
|
|
|
|
#include "tree.h"
|
|
|
|
|
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
// struct definition
|
|
|
|
struct encoder {
|
|
|
|
char (*fn)(char);
|
|
|
|
// maybe add table pointer?
|
|
|
|
};
|
2012-02-27 21:14:46 +00:00
|
|
|
|
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
// constants
|
2012-02-27 21:14:46 +00:00
|
|
|
static const char s_pet[] = "pet";
|
|
|
|
static const char s_raw[] = "raw";
|
|
|
|
static const char s_scr[] = "scr";
|
|
|
|
|
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
// variables
|
|
|
|
static char outermost_table[256]; // space for encoding table...
|
|
|
|
static char *loaded_table = outermost_table; // ...loaded from file
|
|
|
|
static struct encoder *current_encoder; // gets set before each pass
|
2012-02-27 21:14:46 +00:00
|
|
|
|
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
// encoder functions:
|
2012-02-27 21:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
// convert raw to raw (do not convert at all)
|
2014-12-04 23:58:00 +00:00
|
|
|
static char encoderfn_raw(char byte)
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
// convert raw to petscii
|
2014-12-04 23:58:00 +00:00
|
|
|
static char encoderfn_pet(char byte)
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
|
|
|
if ((byte >= 'A') && (byte <= 'Z'))
|
|
|
|
return (char) (byte | 0x80); // FIXME - check why SAS-C
|
|
|
|
if ((byte >= 'a') && (byte <= 'z')) // wants these casts.
|
|
|
|
return (char) (byte - 32); // There are more below.
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
// convert raw to C64 screencode
|
2014-12-04 23:58:00 +00:00
|
|
|
static char encoderfn_scr(char byte)
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
|
|
|
if ((byte >= 'a') && (byte <= 'z'))
|
|
|
|
return (char) (byte - 96); // shift uppercase down
|
|
|
|
if ((byte >= '[') && (byte <= '_'))
|
|
|
|
return (char) (byte - 64); // shift [\]^_ down
|
|
|
|
if (byte == '`')
|
|
|
|
return 64; // shift ` down
|
|
|
|
if (byte == '@')
|
|
|
|
return 0; // shift @ down
|
|
|
|
return byte;
|
|
|
|
}
|
2014-12-04 23:58:00 +00:00
|
|
|
// convert raw to whatever is defined in table
|
|
|
|
static char encoderfn_file(char byte)
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
2014-12-04 23:58:00 +00:00
|
|
|
return loaded_table[(unsigned char) byte];
|
2012-02-27 21:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
// predefined encoder structs:
|
2012-02-27 21:14:46 +00:00
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
|
|
|
|
static struct encoder encoder_raw = {
|
|
|
|
encoderfn_raw
|
|
|
|
};
|
|
|
|
static struct encoder encoder_pet = {
|
|
|
|
encoderfn_pet
|
|
|
|
};
|
|
|
|
static struct encoder encoder_scr = {
|
|
|
|
encoderfn_scr
|
|
|
|
};
|
|
|
|
static struct encoder encoder_file = {
|
|
|
|
encoderfn_file
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// functions
|
|
|
|
|
|
|
|
// convert character using current encoding (exported for use by alu.c)
|
|
|
|
char encoding_encode_char(char byte)
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
2014-12-04 23:58:00 +00:00
|
|
|
return current_encoder->fn(byte);
|
2012-02-27 21:14:46 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
// insert string(s)
|
|
|
|
static enum eos encode_string(struct encoder *inner_encoder, char xor)
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
2014-12-04 23:58:00 +00:00
|
|
|
struct encoder *outer_encoder = current_encoder; // buffer encoder
|
|
|
|
|
|
|
|
// make given encoder the current one (for ALU-parsed values)
|
|
|
|
current_encoder = inner_encoder;
|
|
|
|
do {
|
|
|
|
if (GotByte == '"') {
|
|
|
|
// read initial character
|
|
|
|
GetQuotedByte();
|
|
|
|
// send characters until closing quote is reached
|
|
|
|
while (GotByte && (GotByte != '"')) {
|
|
|
|
output_8(xor ^ current_encoder->fn(GotByte));
|
|
|
|
GetQuotedByte();
|
|
|
|
}
|
|
|
|
if (GotByte == CHAR_EOS)
|
|
|
|
return AT_EOS_ANYWAY;
|
|
|
|
|
|
|
|
// after closing quote, proceed with next char
|
|
|
|
GetByte();
|
|
|
|
} else {
|
|
|
|
// Parse value. No problems with single characters
|
|
|
|
// because the current encoding is
|
|
|
|
// temporarily set to the given one.
|
|
|
|
output_8(ALU_any_int());
|
|
|
|
}
|
|
|
|
} while (Input_accept_comma());
|
|
|
|
current_encoder = outer_encoder; // reactivate buffered encoder
|
|
|
|
return ENSURE_EOS;
|
2012-02-27 21:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read encoding table from file
|
2014-03-11 12:07:11 +00:00
|
|
|
static enum eos user_defined_encoding(void)
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
char local_table[256],
|
|
|
|
*buffered_table = loaded_table;
|
2014-12-04 23:58:00 +00:00
|
|
|
struct encoder *buffered_encoder = current_encoder;
|
2012-02-27 21:14:46 +00:00
|
|
|
|
|
|
|
fd = fopen(GLOBALDYNABUF_CURRENT, FILE_READBINARY);
|
|
|
|
if (fd) {
|
|
|
|
if (fread(local_table, sizeof(char), 256, fd) != 256)
|
|
|
|
Throw_error("Conversion table incomplete.");
|
|
|
|
fclose(fd);
|
|
|
|
} else {
|
|
|
|
Throw_error(exception_cannot_open_input_file);
|
|
|
|
}
|
2014-12-04 23:58:00 +00:00
|
|
|
current_encoder = &encoder_file; // activate new encoding
|
2012-02-27 21:14:46 +00:00
|
|
|
loaded_table = local_table; // activate local table
|
|
|
|
// If there's a block, parse that and then restore old values
|
2014-12-04 23:58:00 +00:00
|
|
|
if (Parse_optional_block()) {
|
|
|
|
current_encoder = buffered_encoder;
|
|
|
|
} else {
|
2012-02-27 21:14:46 +00:00
|
|
|
// if there's *no* block, the table must be used from now on.
|
|
|
|
// copy the local table to the "outer" table
|
|
|
|
memcpy(buffered_table, local_table, 256);
|
2014-12-04 23:58:00 +00:00
|
|
|
}
|
2012-02-27 21:14:46 +00:00
|
|
|
// re-activate "outer" table (it might have been changed by memcpy())
|
|
|
|
loaded_table = buffered_table;
|
|
|
|
return ENSURE_EOS;
|
|
|
|
}
|
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
// keywords for "!convtab" pseudo opcode
|
|
|
|
static struct ronode *encoder_tree = NULL; // tree to hold encoders
|
|
|
|
static struct ronode encoder_list[] = {
|
|
|
|
//no! PREDEFNODE("file", &encoder_file), "!ct file" is not needed; just use {} after initial loading of table!
|
|
|
|
PREDEFNODE(s_pet, &encoder_pet),
|
|
|
|
PREDEFNODE(s_raw, &encoder_raw),
|
|
|
|
PREDEFLAST(s_scr, &encoder_scr),
|
|
|
|
// ^^^^ this marks the last element
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-27 21:14:46 +00:00
|
|
|
// use one of the pre-defined encodings (raw, pet, scr)
|
2014-03-11 12:07:11 +00:00
|
|
|
static enum eos predefined_encoding(void)
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
|
|
|
void *node_body;
|
|
|
|
char local_table[256],
|
|
|
|
*buffered_table = loaded_table;
|
2014-12-04 23:58:00 +00:00
|
|
|
struct encoder *buffered_encoder = current_encoder;
|
2012-02-27 21:14:46 +00:00
|
|
|
|
|
|
|
// use one of the pre-defined encodings
|
|
|
|
if (Input_read_and_lower_keyword()) {
|
2014-12-04 23:58:00 +00:00
|
|
|
// make sure tree is initialised
|
|
|
|
if (encoder_tree == NULL)
|
|
|
|
Tree_add_table(&encoder_tree, encoder_list);
|
|
|
|
// perform lookup
|
|
|
|
if (Tree_easy_scan(encoder_tree, &node_body, GlobalDynaBuf)) {
|
|
|
|
current_encoder = (struct encoder *) node_body; // activate new encoder
|
|
|
|
} else {
|
2012-02-27 21:14:46 +00:00
|
|
|
Throw_error("Unknown encoding.");
|
2014-12-04 23:58:00 +00:00
|
|
|
}
|
2012-02-27 21:14:46 +00:00
|
|
|
}
|
|
|
|
loaded_table = local_table; // activate local table
|
|
|
|
// If there's a block, parse that and then restore old values
|
|
|
|
if (Parse_optional_block())
|
2014-12-04 23:58:00 +00:00
|
|
|
current_encoder = buffered_encoder;
|
2012-02-27 21:14:46 +00:00
|
|
|
// re-activate "outer" table
|
|
|
|
loaded_table = buffered_table;
|
|
|
|
return ENSURE_EOS;
|
|
|
|
}
|
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
|
|
|
|
// exported functions
|
|
|
|
|
|
|
|
// set "raw" as default encoding
|
|
|
|
void encoding_passinit(void)
|
|
|
|
{
|
|
|
|
current_encoder = &encoder_raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME - move this to pseudoopcodes.c:
|
|
|
|
|
|
|
|
// insert text string (default format)
|
|
|
|
static enum eos po_text(void)
|
|
|
|
{
|
|
|
|
return encode_string(current_encoder, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert raw string
|
|
|
|
static enum eos po_raw(void)
|
|
|
|
{
|
|
|
|
return encode_string(&encoder_raw, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert PetSCII string
|
|
|
|
static enum eos po_pet(void)
|
|
|
|
{
|
|
|
|
return encode_string(&encoder_pet, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert screencode string
|
|
|
|
static enum eos po_scr(void)
|
|
|
|
{
|
|
|
|
return encode_string(&encoder_scr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert screencode string, XOR'd
|
|
|
|
static enum eos po_scrxor(void)
|
|
|
|
{
|
|
|
|
intval_t num = ALU_any_int();
|
|
|
|
|
|
|
|
if (Input_accept_comma() == FALSE) {
|
|
|
|
Throw_error(exception_syntax);
|
|
|
|
return SKIP_REMAINDER;
|
|
|
|
}
|
|
|
|
return encode_string(&encoder_scr, num);
|
|
|
|
}
|
|
|
|
|
2012-02-27 21:14:46 +00:00
|
|
|
// Set current encoding ("!convtab" pseudo opcode)
|
2014-12-04 23:58:00 +00:00
|
|
|
static enum eos po_convtab(void)
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
2014-12-04 23:58:00 +00:00
|
|
|
if ((GotByte == '<') || (GotByte == '"')) {
|
|
|
|
// if file name is missing, don't bother continuing
|
|
|
|
if (Input_read_filename(TRUE))
|
|
|
|
return SKIP_REMAINDER;
|
2012-02-27 21:14:46 +00:00
|
|
|
return user_defined_encoding();
|
2014-12-04 23:58:00 +00:00
|
|
|
} else {
|
2012-02-27 21:14:46 +00:00
|
|
|
return predefined_encoding();
|
2014-12-04 23:58:00 +00:00
|
|
|
}
|
2012-02-27 21:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pseudo opcode table
|
2014-11-24 14:52:05 +00:00
|
|
|
static struct ronode pseudo_opcodes[] = {
|
2014-12-04 23:58:00 +00:00
|
|
|
PREDEFNODE("ct", po_convtab),
|
|
|
|
PREDEFNODE("convtab", po_convtab),
|
|
|
|
PREDEFNODE(s_pet, po_pet),
|
|
|
|
PREDEFNODE(s_raw, po_raw),
|
|
|
|
PREDEFNODE(s_scr, po_scr),
|
|
|
|
PREDEFNODE(s_scrxor, po_scrxor),
|
|
|
|
PREDEFNODE("text", po_text),
|
|
|
|
PREDEFLAST("tx", po_text),
|
2012-02-27 21:14:46 +00:00
|
|
|
// ^^^^ this marks the last element
|
|
|
|
};
|
|
|
|
|
2014-12-04 23:58:00 +00:00
|
|
|
// register pseudo opcodes
|
2012-02-27 21:14:46 +00:00
|
|
|
void Encoding_init(void)
|
|
|
|
{
|
|
|
|
Tree_add_table(&pseudo_opcode_tree, pseudo_opcodes);
|
|
|
|
}
|