86 lines
2.5 KiB
C
86 lines
2.5 KiB
C
#include "assemblerConfig.h"
|
|
|
|
typedef struct assembler_config_reader_t {
|
|
int pos;
|
|
int lineLen;
|
|
int done;
|
|
char line[256];
|
|
} assembler_config_reader_t;
|
|
|
|
int finished(assembler_config_reader_t *reader, buffer_t *buffer) {
|
|
return reader->pos==buffer->size;
|
|
}
|
|
|
|
void readLine(assembler_config_reader_t *reader, buffer_t *buffer) {
|
|
if(reader->done) return;
|
|
strcpyt(((char*) buffer->data)+reader->pos, reader->line, '\n');
|
|
reader->lineLen=strlent(reader->line, '\n')-1;
|
|
reader->pos+=reader->lineLen+1;
|
|
reader->done=finished(reader, buffer);
|
|
}
|
|
|
|
int readConfig(buffer_t *buffer, assembler_config_t *config) {
|
|
//BEGIN setup everything
|
|
//setup reader struct
|
|
assembler_config_reader_t reader;
|
|
reader.pos=0;
|
|
reader.lineLen=0;
|
|
reader.done=finished(&reader, buffer);
|
|
|
|
//setup config struct
|
|
config->codeOffset=0x0000;
|
|
config->entryPoint=0x0000;
|
|
config->mappingCount=0;
|
|
//END setup everything
|
|
|
|
//BEGIN read line 1
|
|
//the first line contains the entry point, in hex
|
|
if(reader.done) return 0;
|
|
readLine(&reader, buffer);
|
|
config->entryPoint=hextoi(reader.line);
|
|
//END read line 1
|
|
|
|
//BEGIN read line 2
|
|
//the second line contains the code offset, in hex
|
|
if(reader.done) return 0;
|
|
readLine(&reader, buffer);
|
|
config->codeOffset=hextoi(reader.line);
|
|
//END read line 2
|
|
|
|
//BEGIN read mappings
|
|
while(!reader.done) {
|
|
//first line of a block contains the type, in dec
|
|
readLine(&reader, buffer);
|
|
config->mappings[config->mappingCount].type=dectoi(reader.line);
|
|
|
|
//second line of a block contains the access, in hex
|
|
if(reader.done) return 0;
|
|
readLine(&reader, buffer);
|
|
config->mappings[config->mappingCount].access=hextoi(reader.line);
|
|
|
|
//third line of a block contains the offset, in hex
|
|
if(reader.done) return 0;
|
|
readLine(&reader, buffer);
|
|
config->mappings[config->mappingCount].offset=hextoi(reader.line);
|
|
|
|
//fourth line of a block contains the length, in hex
|
|
if(reader.done) return 0;
|
|
readLine(&reader, buffer);
|
|
config->mappings[config->mappingCount].length=hextoi(reader.line);
|
|
|
|
if(config->mappings[config->mappingCount].type==MAPPING_TYPE_file||config->mappings[config->mappingCount].type==MAPPING_TYPE_file_raw) {
|
|
//fifth line of a file block contains the filename
|
|
readLine(&reader, buffer);
|
|
config->mappings[config->mappingCount].filenameLen=strlenl(reader.line, 16);
|
|
for(int i=0; i<16; i++) config->mappings[config->mappingCount].filename[i]='\0';
|
|
strcpyl(reader.line, config->mappings[config->mappingCount].filename, 16);
|
|
}
|
|
|
|
//increment the number of mappings
|
|
config->mappingCount++;
|
|
}
|
|
//END read mappings
|
|
|
|
return 1;
|
|
}
|