s502 assembler
A very simple assembler for the 6502 line of processors written in C
|
Go to the documentation of this file. 1 #include "debugmalloc.h"
32 if (token->
instr.inst == NULL)
return -1;
144 int s_x = 0, s_y = 0, s_close = 0, s_sep = 0;
146 for (
int i = 0; i < t->
len; i++) {
158 if ((s_x || s_y) && !s_sep)
226 ERROR(
"Unknown instruction!\n");
230 ERROR(
"Can not determine instruction address mode!\n");
234 ERROR(
"Invalid instruction-addressmode combination!\n");
246 FAIL(
"Error while analyzing token: \n");
270 t->
instr.number = -1;
276 ERROR(
"Can not recognize token:\n");
281 LOG(4,
"Recognized token as %d:\n", t->
type);
296 for (; *begin != 0 && (*begin ==
' ' || *begin ==
'*' || *begin ==
'(' || *begin ==
'#'); begin++);
297 for (end = begin; *end != 0 && *end !=
')' && *end !=
',' && *end !=
' '; end++);
299 char* buff = malloc(end - begin + 1);
300 strncpy(buff, begin, end - begin + 1);
301 buff[end - begin] = 0;
308 FAIL(
"Opcode operand parsing failed!\n");
324 ERROR(
"Something went terribly wrong!\n");
325 ERROR(
"A LABEL in last pass!\n");
329 char* data = malloc(size);
332 int n = t->
instr.number - t->
instr.address - 2;
334 if (-128 > n || 127 < n) {
335 ERROR(
"Relative addressing jump too far!\n");
336 printf(
"Target: $%x, from: $%x (diff: $%x)\n", t->
instr.number, t->
instr.address, n);
344 data[0] = t->
instr.inst->opcs[t->
instr.addressmode];
346 data[1] = t->
instr.number & 0xff;
349 data[2] = (t->
instr.number >> 8) & 0xff;
@ ADRM_ZPG_X
Zeropage, X indexed.
@ TT_INSTR
instruction token
struct Token::@1 instr
instruction data. Not used if token type is not TT_INSTR
const char * ADRM_NAMES[]
Human-readable names of address modes.
char stripped[TOKEN_BUFFER_SIZE]
stripped text from source file
@ ADRM_REL
Relative (8bit, signed 2's complement)
int token_link_instruction(State *s, Token *token)
find and link the instruction entry for a token
int token_get_addressmode(Token *t)
Determine the address mode of a token.
@ ADRM_IND_X
Indirect (8 bit), X indexed.
@ ADRM_IND
Indirect (16 bit)
Instruction * instr
instruction data
#define LOG(LVL,...)
logging macro - works like printf
Compiler pseudo-global state.
#define LOGDO(LVL, x)
Conditional macro. Wraps contents into a conditional based on log level.
int ADRM_SIZES[ADRM_COUNT+1]
operand sizes of addressmodes
int token_analyze_instruction(State *s, Token *t)
analyze instruction token (instruction, addressmode & operand)
@ ADRM_ZPG_Y
Zeropage, Y indexed.
step 1 and 3 processing for directive tokens
int util_match_char(char a, char b)
Case-insensitive character compare.
Token type to store token information.
int binSize
number of bytes this token will generate
@ TT_DIRECTIVE
directive token
Token type member methods.
enum tokenType type
type of this token
void token_print(Token *token)
Pretty-print one token, with its source and length.
int directive_compile(State *s, Token *t, char **dataptr)
Compile a directive into binary data.
int token_recognize(Token *t)
Parse token - test if it's an opcode, a label or a directive.
@ ADRM_IND_Y
Indirect (8 bit), Y indexed.
int token_compile(State *s, Token *t, char **dataptr)
compile token into binary data
logging and fancy-printing
struct Token::@2 source
source of this token
int len
length of stripped text
int token_get_operand(State *s, Token *t)
parse the operand of the instruction as a number
@ ADRM_ABS_Y
Absolute, Y indexed.
Instruction * instruction_find(Instruction *list, char *mnem)
find the Instruction entry for a given mnemonic
@ ADRM_ABS_X
Absolute, X indexed.
@ NUMBER_ERROR
Could not parse a number or constant is undefined.
Number module to parse numbers.
various utility functions
@ ADRM_IMP
Implied (no operand)
int number_get_number(State *s, char *str)
interpret a string as a constant, label or number
@ ADRM_ABS
Absolute addressing.
@ OPC_INVALID
An invalid opcode to signal invalid / non-existent variations.
#define FAIL(...)
Fancy-print a fail (failed step). Works like printf.
#define ERROR(...)
Fancy-print an error (cause of faliure). Works like printf.
@ NUMBER_LABEL_NODEF
Undefined label.