x65/x65.cpp

8517 lines
319 KiB
C++

//
// x65.cpp
//
//
// Created by Carl-Henrik Skårstedt on 9/23/15.
//
//
// A "simple" 6502/65C02/65816 assembler
//
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Carl-Henrik Skårstedt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Details, source and documentation at https://github.com/Sakrac/x65.
//
// "struse.h" can be found at https://github.com/Sakrac/struse, only the header file is required.
//
#define _CRT_SECURE_NO_WARNINGS // Windows shenanigans
#define STRUSE_IMPLEMENTATION // include implementation of struse in this file
#include "struse.h" // https://github.com/Sakrac/struse/blob/master/struse.h
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <assert.h>
#ifndef _WIN32
#define _strdup strdup
#endif
// Command line arguments
static const strref cmdarg_listing("lst"); // -lst / -lst=(file.lst) : generate disassembly text from result(file or stdout)
static const strref cmdarg_srcdebug("srcdbg"); // -srcdbg : generate debug, -srcdbg=(file) : save debug file
static const strref cmdarg_tass_listing("tsl"); // -tsl=(file) : generate listing file in TASS style
static const strref cmdarg_tass_labels("tl"); // -tl=(file) : generate labels in TASS style
static const strref cmdarg_allinstr("opcodes"); // -opcodes / -opcodes=(file.s) : dump all available opcodes(file or stdout)
static const strref cmdarg_endmacro("endm"); // -endm : macros end with endm or endmacro instead of scoped('{' - '}')
static const strref cmdarg_cpu("cpu"); // declare CPU type, use with argument: -cpu=6502/65c02/65c02wdc/65816
static const strref cmdarg_acc("acc"); // [65816] -acc=8/16: set the accumulator mode for 65816 at start, default is 8 bits
static const strref cmdarg_xy("xy"); // [65816] -xy=8/16: set the index register mode for 65816 at start, default is 8 bits
static const strref cmdarg_org("org"); // -org = $2000 or - org = 4096: force fixed address code at address
static const strref cmdarg_kickasm("kickasm"); // -kickasm: use Kick Assembler syntax
static const strref cmdarg_merlin("merlin"); // -merlin: use Merlin syntax
static const strref cmdarg_c64("c64"); // -c64 : Include load address(default)
static const strref cmdarg_a2b("a2b"); // -a2b : Apple II Dos 3.3 Binary
static const strref cmdarg_bin("bin"); // -bin : Produce raw binary\n"
static const strref cmdarg_a2p("a2p"); // -a2p : Apple II ProDos Binary
static const strref cmdarg_a2o("a2o"); // -a2o : Apple II GS OS executable (relocatable)
static const strref cmdarg_mrg("mrg"); // -mrg : Force merge all sections (use with -a2o)
static const strref cmdarg_sect("sect"); // -sect: display sections loaded and built
static const strref cmdarg_sym("sym"); // -sym (file.sym) : generate symbol file
static const strref cmdarg_obj("obj"); // -obj (file.x65) : generate object file for later linking
static const strref cmdarg_vice("vice"); // -vice (file.vs) : export a vice symbol file
static const strref cmdarg_xrefimp("xrefimp"); // -xrefimp : import directive means xref, not include/incbin
static const strref cmdarg_references("refs"); // -refs: show label dependencies before linking
// if the number of resolved labels exceed this in one late eval then skip
// checking for relevance and just eval all unresolved expressions.
#define MAX_LABELS_EVAL_ALL 16
// Max number of nested scopes (within { and })
#define MAX_SCOPE_DEPTH 32
// Max number of nested conditional expressions
#define MAX_CONDITIONAL_DEPTH 64
// The maximum complexity of expressions to be evaluated
#define MAX_EVAL_VALUES 32
#define MAX_EVAL_OPER 64
// Max capacity of each label pool
#define MAX_POOL_BYTES 255
// Max number of exported binary files from a single source
#define MAX_EXPORT_FILES 64
// Maximum number of opcodes, aliases and directives
#define MAX_OPCODES_DIRECTIVES 320
// minor variation of 6502
#define NUM_ILLEGAL_6502_OPS 21
// minor variation of 65C02
#define NUM_WDC_65C02_SPECIFIC_OPS 18
// To simplify some syntax disambiguation the preferred
// ruleset can be specified on the command line.
enum AsmSyntax {
SYNTAX_SANE,
SYNTAX_KICKASM,
SYNTAX_MERLIN
};
// Internal status and error type
enum StatusCode {
STATUS_OK, // everything is fine
STATUS_RELATIVE_SECTION, // value is relative to a single section
STATUS_NOT_READY, // label could not be evaluated at this time
STATUS_XREF_DEPENDENT, // evaluated but relied on an XREF label to do so
STATUS_NOT_STRUCT, // return is not a struct.
STATUS_EXPORT_NO_CODE_OR_DATA_SECTION,
FIRST_ERROR,
ERROR_UNDEFINED_CODE = FIRST_ERROR,
ERROR_UNEXPECTED_CHARACTER_IN_EXPRESSION,
ERROR_TOO_MANY_VALUES_IN_EXPRESSION,
ERROR_TOO_MANY_OPERATORS_IN_EXPRESSION,
ERROR_UNBALANCED_RIGHT_PARENTHESIS,
ERROR_EXPRESSION_OPERATION,
ERROR_EXPRESSION_MISSING_VALUES,
ERROR_INSTRUCTION_NOT_ZP,
ERROR_INVALID_ADDRESSING_MODE,
ERROR_LABEL_MISPLACED_INTERNAL,
ERROR_BAD_ADDRESSING_MODE,
ERROR_UNEXPECTED_CHARACTER_IN_ADDRESSING_MODE,
ERROR_UNEXPECTED_LABEL_ASSIGMENT_FORMAT,
ERROR_MODIFYING_CONST_LABEL,
ERROR_OUT_OF_LABELS_IN_POOL,
ERROR_INTERNAL_LABEL_POOL_ERROR,
ERROR_POOL_RANGE_EXPRESSION_EVAL,
ERROR_LABEL_POOL_REDECLARATION,
ERROR_POOL_LABEL_ALREADY_DEFINED,
ERROR_STRUCT_ALREADY_DEFINED,
ERROR_REFERENCED_STRUCT_NOT_FOUND,
ERROR_BAD_TYPE_FOR_DECLARE_CONSTANT,
ERROR_REPT_COUNT_EXPRESSION,
ERROR_HEX_WITH_ODD_NIBBLE_COUNT,
ERROR_DS_MUST_EVALUATE_IMMEDIATELY,
ERROR_NOT_AN_X65_OBJECT_FILE,
ERROR_COULD_NOT_INCLUDE_FILE,
ERROR_PULL_WITHOUT_PUSH,
ERROR_USER,
ERROR_STOP_PROCESSING_ON_HIGHER, // errors greater than this will stop execution
ERROR_BRANCH_OUT_OF_RANGE,
ERROR_INCOMPLETE_FUNCTION,
ERROR_FUNCTION_DID_NOT_RESOLVE,
ERROR_EXPRESSION_RECURSION,
ERROR_TARGET_ADDRESS_MUST_EVALUATE_IMMEDIATELY,
ERROR_TOO_DEEP_SCOPE,
ERROR_UNBALANCED_SCOPE_CLOSURE,
ERROR_BAD_MACRO_FORMAT,
ERROR_ALIGN_MUST_EVALUATE_IMMEDIATELY,
ERROR_OUT_OF_MEMORY_FOR_MACRO_EXPANSION,
ERROR_MACRO_ARGUMENT,
ERROR_CONDITION_COULD_NOT_BE_RESOLVED,
ERROR_ENDIF_WITHOUT_CONDITION,
ERROR_ELSE_WITHOUT_IF,
ERROR_STRUCT_CANT_BE_ASSEMBLED,
ERROR_ENUM_CANT_BE_ASSEMBLED,
ERROR_UNTERMINATED_CONDITION,
ERROR_REPT_MISSING_SCOPE,
ERROR_LINKER_MUST_BE_IN_FIXED_ADDRESS_SECTION,
ERROR_LINKER_CANT_LINK_TO_DUMMY_SECTION,
ERROR_UNABLE_TO_PROCESS,
ERROR_SECTION_TARGET_OFFSET_OUT_OF_RANGE,
ERROR_CPU_NOT_SUPPORTED,
ERROR_CANT_APPEND_SECTION_TO_TARGET,
ERROR_ZEROPAGE_SECTION_OUT_OF_RANGE,
ERROR_NOT_A_SECTION,
ERROR_CANT_REASSIGN_FIXED_SECTION,
ERROR_CANT_LINK_ZP_AND_NON_ZP,
ERROR_OUT_OF_MEMORY,
ERROR_CANT_WRITE_TO_FILE,
ERROR_ABORTED,
ERROR_CONDITION_TOO_NESTED,
STATUSCODE_COUNT
};
// The following strings are in the same order as StatusCode
const char *aStatusStrings[STATUSCODE_COUNT] = {
"ok", // STATUS_OK, // everything is fine
"relative section", // STATUS_RELATIVE_SECTION, // value is relative to a single section
"not ready", // STATUS_NOT_READY, // label could not be evaluated at this time
"XREF dependent result", // STATUS_XREF_DEPENDENT, // evaluated but relied on an XREF label to do so
"name is not a struct", // STATUS_NOT_STRUCT, // return is not a struct.
"Exporting binary without code or data section", // STATUS_EXPORT_NO_CODE_OR_DATA_SECTION,
"Undefined code", // ERROR_UNDEFINED_CODE = FIRST_ERROR,
"Unexpected character in expression", // ERROR_UNEXPECTED_CHARACTER_IN_EXPRESSION,
"Too many values in expression", // ERROR_TOO_MANY_VALUES_IN_EXPRESSION,
"Too many operators in expression", // ERROR_TOO_MANY_OPERATORS_IN_EXPRESSION,
"Unbalanced right parenthesis in expression", // ERROR_UNBALANCED_RIGHT_PARENTHESIS,
"Expression operation", // ERROR_EXPRESSION_OPERATION,
"Expression missing values", // ERROR_EXPRESSION_MISSING_VALUES,
"Instruction can not be zero page", // ERROR_INSTRUCTION_NOT_ZP,
"Invalid addressing mode for instruction", // ERROR_INVALID_ADDRESSING_MODE,
"Internal label organization mishap", // ERROR_LABEL_MISPLACED_INTERNAL,
"Bad addressing mode", // ERROR_BAD_ADDRESSING_MODE,
"Unexpected character in addressing mode", // ERROR_UNEXPECTED_CHARACTER_IN_ADDRESSING_MODE,
"Unexpected label assignment format", // ERROR_UNEXPECTED_LABEL_ASSIGMENT_FORMAT,
"Changing value of label that is constant", // ERROR_MODIFYING_CONST_LABEL,
"Out of labels in pool", // ERROR_OUT_OF_LABELS_IN_POOL,
"Internal label pool release confusion", // ERROR_INTERNAL_LABEL_POOL_ERROR,
"Label pool range evaluation failed", // ERROR_POOL_RANGE_EXPRESSION_EVAL,
"Label pool was redeclared within its scope", // ERROR_LABEL_POOL_REDECLARATION,
"Pool label already defined", // ERROR_POOL_LABEL_ALREADY_DEFINED,
"Struct already defined", // ERROR_STRUCT_ALREADY_DEFINED,
"Referenced struct not found", // ERROR_REFERENCED_STRUCT_NOT_FOUND,
"Declare constant type not recognized (dc.?)", // ERROR_BAD_TYPE_FOR_DECLARE_CONSTANT,
"rept count expression could not be evaluated", // ERROR_REPT_COUNT_EXPRESSION,
"hex must be followed by an even number of hex numbers", // ERROR_HEX_WITH_ODD_NIBBLE_COUNT,
"DS directive failed to evaluate immediately", // ERROR_DS_MUST_EVALUATE_IMMEDIATELY,
"File is not a valid x65 object file", // ERROR_NOT_AN_X65_OBJECT_FILE,
"Failed to read include file", // ERROR_COULD_NOT_INCLUDE_FILE,
"Using symbol PULL without first using a PUSH", // ERROR_PULL_WITHOUT_PUSH
"User invoked error", // ERROR_USER,
"Errors after this point will stop execution", // ERROR_STOP_PROCESSING_ON_HIGHER, // errors greater than this will stop execution
"Branch is out of range", // ERROR_BRANCH_OUT_OF_RANGE,
"Function declaration is missing name or expression", // ERROR_INCOMPLETE_FUNCTION,
"Function could not resolve the expression", // ERROR_FUNCTION_DID_NOT_RESOLVE
"Expression evaluateion recursion too deep", // ERROR_EXPRESSION_RECURSION
"Target address must evaluate immediately for this operation", // ERROR_TARGET_ADDRESS_MUST_EVALUATE_IMMEDIATELY,
"Scoping is too deep", // ERROR_TOO_DEEP_SCOPE,
"Unbalanced scope closure", // ERROR_UNBALANCED_SCOPE_CLOSURE,
"Unexpected macro formatting", // ERROR_BAD_MACRO_FORMAT,
"Align must evaluate immediately", // ERROR_ALIGN_MUST_EVALUATE_IMMEDIATELY,
"Out of memory for macro expansion", // ERROR_OUT_OF_MEMORY_FOR_MACRO_EXPANSION,
"Problem with macro argument", // ERROR_MACRO_ARGUMENT,
"Conditional could not be resolved", // ERROR_CONDITION_COULD_NOT_BE_RESOLVED,
"#endif encountered outside conditional block", // ERROR_ENDIF_WITHOUT_CONDITION,
"#else or #elif outside conditional block", // ERROR_ELSE_WITHOUT_IF,
"Struct can not be assembled as is", // ERROR_STRUCT_CANT_BE_ASSEMBLED,
"Enum can not be assembled as is", // ERROR_ENUM_CANT_BE_ASSEMBLED,
"Conditional assembly (#if/#ifdef) was not terminated in file or macro", // ERROR_UNTERMINATED_CONDITION,
"rept is missing a scope ('{ ... }')", // ERROR_REPT_MISSING_SCOPE,
"Link can only be used in a fixed address section", // ERROR_LINKER_MUST_BE_IN_FIXED_ADDRESS_SECTION,
"Link can not be used in dummy sections", // ERROR_LINKER_CANT_LINK_TO_DUMMY_SECTION,
"Can not process this line", // ERROR_UNABLE_TO_PROCESS,
"Unexpected target offset for reloc or late evaluation", // ERROR_SECTION_TARGET_OFFSET_OUT_OF_RANGE,
"CPU is not supported", // ERROR_CPU_NOT_SUPPORTED,
"Can't append sections", // ERROR_CANT_APPEND_SECTION_TO_TARGET,
"Zero page / Direct page section out of range", // ERROR_ZEROPAGE_SECTION_OUT_OF_RANGE,
"Attempting to assign an address to a non-existent section", // ERROR_NOT_A_SECTION,
"Attempting to assign an address to a fixed address section", // ERROR_CANT_REASSIGN_FIXED_SECTION,
"Can not link a zero page section with a non-zp section", // ERROR_CANT_LINK_ZP_AND_NON_ZP,
"Out of memory while building", // ERROR_OUT_OF_MEMORY,
"Can not write to file", // ERROR_CANT_WRITE_TO_FILE,
"Assembly aborted", // ERROR_ABORTED,
"Condition too deeply nested", // ERROR_CONDITION_TOO_NESTED,
};
// Assembler directives
enum AssemblerDirective {
AD_CPU, // CPU: Assemble for this target,
AD_ORG, // ORG: Assemble as if loaded at this address
AD_EXPORT, // EXPORT: export this section or disable export
AD_LOAD, // LOAD: If applicable, instruct to load at this address
AD_SECTION, // SECTION: Enable code that will be assigned a start address during a link step
AD_MERGE, // MERGE: Merge named sections in order listed
AD_LINK, // LINK: Put sections with this name at this address (must be ORG / fixed address section)
AD_XDEF, // XDEF: Externally declare a symbol
AD_XREF, // XREF: Reference an external symbol
AD_INCOBJ, // INCOBJ: Read in an object file saved from a previous build
AD_ALIGN, // ALIGN: Add to address to make it evenly divisible by this
AD_MACRO, // MACRO: Create a macro
AD_EVAL, // EVAL: Print expression to stdout during assemble
AD_BYTES, // BYTES: Add 8 bit values to output
AD_WORDS, // WORDS: Add 16 bit values to output
AD_DC, // DC.B/DC.W: Declare constant (same as BYTES/WORDS)
AD_TEXT, // TEXT: Add text to output
AD_INCLUDE, // INCLUDE: Load and assemble another file at this address
AD_INCBIN, // INCBIN: Load and directly insert another file at this address
AD_IMPORT, // IMPORT: Include or Incbin or Incobj or Incsym
AD_CONST, // CONST: Prevent a label from mutating during assemble
AD_LABEL, // LABEL: Create a mutable label (optional)
AD_STRING, // STRING: Declare a string symbol
AD_FUNCTION, // FUNCTION: Declare a user defined function
AD_UNDEF, // UNDEF: remove a string or a label
AD_INCSYM, // INCSYM: Reference labels from another assemble
AD_LABPOOL, // POOL: Create a pool of addresses to assign as labels dynamically
AD_IF, // #IF: Conditional assembly follows based on expression
AD_IFDEF, // #IFDEF: Conditional assembly follows based on label defined or not
AD_IFNDEF, // #IFNDEF: Conditional assembly inverted from IFDEF
AD_IFCONST, // #IFCONST: Conditional assembly follows based on label being const
AD_IFBLANK, // #IFBLANK: Conditional assembly follows based on rest of line empty
AD_IFNBLANK, // #IFNBLANK: Conditional assembly follows based on rest of line not empty
AD_ELSE, // #ELSE: Otherwise assembly
AD_ELIF, // #ELIF: Otherwise conditional assembly follows
AD_ENDIF, // #ENDIF: End a block of #IF/#IFDEF
AD_STRUCT, // STRUCT: Declare a set of labels offset from a base address
AD_ENUM, // ENUM: Declare a set of incremental labels
AD_REPT, // REPT: Repeat the assembly of the bracketed code a number of times
AD_INCDIR, // INCDIR: Add a folder to search for include files
AD_A16, // A16: Set 16 bit accumulator mode
AD_A8, // A8: Set 8 bit accumulator mode
AD_XY16, // A16: Set 16 bit index register mode
AD_XY8, // A8: Set 8 bit index register mode
AD_HEX, // HEX: LISA assembler data block
AD_ABORT, // ABORT: stop assembler and error
AD_EJECT, // EJECT: Page break for printing assembler code, ignore
AD_LST, // LST: Controls symbol listing
AD_DUMMY, // DUM: Start a dummy section (increment address but don't write anything???)
AD_DUMMY_END, // DEND: End a dummy section
AD_SCOPE, // SCOPE: Begin ca65 style scope
AD_ENDSCOPE, // ENDSCOPR: End ca65 style scope
AD_PUSH, // PUSH: Push the value of a variable symbol on a stack
AD_PULL, // PULL: Pull the value of a variable symbol from its stack, must be pushed first
AD_DS, // DS: Define section, zero out # bytes or rewind the address if negative
AD_USR, // USR: MERLIN user defined pseudo op, runs some code at a hard coded address on apple II, on PC does nothing.
AD_SAV, // SAV: MERLIN version of export but contains full filename, not an appendable name
AD_XC, // XC: MERLIN version of setting CPU
AD_MX, // MX: MERLIN control accumulator 16 bit mode
AD_LNK, // LNK: MERLIN load object and link
AD_ADR, // ADR: MERLIN store 3 byte word
AD_ADRL, // ADRL: MERLIN store 4 byte word
AD_ENT, // ENT: MERLIN extern this address label
AD_EXT, // EXT: MERLIN reference this address label from a different file
AD_CYC, // CYC: MERLIN start / stop cycle timer
AD_DBL_BYTES, // DDB: MERLIN Store 2 bytes, big endian format.
AD_ERROR,
};
// evaluation functions
enum EvalFuncs {
EF_DEFINED, // DEFINED(label) 1 if label is defined
EF_REFERENCED, // REFERENCED(label) 1 if label has been referenced in this file
EF_BLANK, // BLANK() 1 if the contents within the parenthesis is empty
EF_CONST, // CONST(label) 1 if label is a const label
EF_SIZEOF, // SIZEOF(struct) returns size of structs
EF_SIN, // SIN(index, period, amplitude)
};
// Operators are either instructions or directives
enum OperationType {
OT_NONE,
OT_MNEMONIC,
OT_DIRECTIVE
};
// These are expression tokens in order of precedence (last is highest precedence)
enum EvalOperator {
EVOP_NONE,
EVOP_VAL = 'a', // a, value => read from value queue
EVOP_EQU, // b, 1 if left equal to right otherwise 0
EVOP_LT, // c, 1 if left less than right otherwise 0
EVOP_GT, // d, 1 if left greater than right otherwise 0
EVOP_LTE, // e, 1 if left less than or equal to right otherwise 0
EVOP_GTE, // f, 1 if left greater than or equal to right otherwise 0
EVOP_LOB, // g, low byte of 16 bit value
EVOP_HIB, // h, high byte of 16 bit value
EVOP_BAB, // i, bank byte of 24 bit value
EVOP_LPR, // j, left parenthesis
EVOP_RPR, // k, right parenthesis
EVOP_ADD, // l, +
EVOP_SUB, // m, -
EVOP_MUL, // n, * (note: if not preceded by value or right paren this is current PC)
EVOP_DIV, // o, /
EVOP_AND, // p, &
EVOP_OR, // q, |
EVOP_EOR, // r, ^
EVOP_SHL, // s, <<
EVOP_SHR, // t, >>
EVOP_NOT, // u, ~
EVOP_NEG, // v, negate value
EVOP_STP, // w, Unexpected input, should stop and evaluate what we have
EVOP_NRY, // x, Not ready yet
EVOP_XRF, // y, value from XREF label
EVOP_EXP, // z, sub expression
EVOP_ERR, // z+1, Error
};
// Opcode encoding
typedef struct sOPLookup {
uint32_t op_hash;
uint8_t index; // ground index
uint8_t type; // mnemonic or
} OPLookup;
enum AddrMode {
// address mode bit index
// 6502
AMB_ZP_REL_X, // 0 ($12,x)
AMB_ZP, // 1 $12
AMB_IMM, // 2 #$12
AMB_ABS, // 3 $1234
AMB_ZP_Y_REL, // 4 ($12),y
AMB_ZP_X, // 5 $12,x
AMB_ABS_Y, // 6 $1234,y
AMB_ABS_X, // 7 $1234,x
AMB_REL, // 8 ($1234)
AMB_ACC, // 9 A
AMB_NON, // a
// 65C02
AMB_ZP_REL, // b ($12)
AMB_REL_X, // c ($1234,x)
AMB_ZP_ABS, // d $12, *+$12
// 65816
AMB_ZP_REL_L, // e [$02]
AMB_ZP_REL_Y_L, // f [$00],y
AMB_ABS_L, // 10 $bahilo
AMB_ABS_L_X, // 11 $123456,x
AMB_STK, // 12 $12,s
AMB_STK_REL_Y, // 13 ($12,s),y
AMB_REL_L, // 14 [$1234]
AMB_BLK_MOV, // 15 $12,$34
AMB_COUNT,
AMB_FLIPXY = AMB_COUNT, // 16 (indexing index using y treat as x address mode)
AMB_BRANCH, // 17 (relative address 8 bit)
AMB_BRANCH_L, // 18 (relative address 16 bit)
AMB_IMM_DBL_A, // 19 (immediate mode can be doubled in 16 bit mode)
AMB_IMM_DBL_XY, // 1a (immediate mode can be doubled in 16 bit mode)
AMB_ILL, // 1b illegal address mode
// address mode masks
AMM_NON = 1<<AMB_NON,
AMM_IMM = 1<<AMB_IMM,
AMM_ABS = 1<<AMB_ABS,
AMM_REL = 1<<AMB_REL,
AMM_ACC = 1<<AMB_ACC,
AMM_ZP = 1<<AMB_ZP,
AMM_ABS_X = 1<<AMB_ABS_X,
AMM_ABS_Y = 1<<AMB_ABS_Y,
AMM_ZP_X = 1<<AMB_ZP_X,
AMM_ZP_REL_X = 1<<AMB_ZP_REL_X,
AMM_ZP_Y_REL = 1<<AMB_ZP_Y_REL,
AMM_ZP_REL = 1<<AMB_ZP_REL, // b ($12)
AMM_REL_X = 1<<AMB_REL_X, // c ($1234,x)
AMM_ZP_ABS = 1<<AMB_ZP_ABS, // d $12, *+$12
AMM_ZP_REL_L = 1<<AMB_ZP_REL_L, // e [$02]
AMM_ZP_REL_Y_L = 1<<AMB_ZP_REL_Y_L, // f [$00],y
AMM_ABS_L = 1<<AMB_ABS_L, // 10 $bahilo
AMM_ABS_L_X = 1<<AMB_ABS_L_X, // 11 $123456,x
AMM_STK = 1<<AMB_STK, // 12 $12,s
AMM_STK_REL_Y = 1<<AMB_STK_REL_Y, // 13 ($12,s),y
AMM_REL_L = 1<<AMB_REL_L, // 14 [$1234]
AMM_BLK_MOV = 1<<AMB_BLK_MOV, // 15 $12,$34
AMM_FLIPXY = 1<<AMB_FLIPXY,
AMM_BRANCH = 1<<AMB_BRANCH,
AMM_BRANCH_L = 1<<AMB_BRANCH_L,
AMM_IMM_DBL_A = 1<<AMB_IMM_DBL_A,
AMM_IMM_DBL_XY = 1<<AMB_IMM_DBL_XY,
// instruction group specific masks
AMM_BRK = AMM_NON | AMM_IMM,
AMM_BRA = AMM_BRANCH | AMM_ABS,
AMM_ORA = AMM_IMM | AMM_ZP | AMM_ZP_X | AMM_ABS | AMM_ABS_Y | AMM_ABS_X | AMM_ZP_REL_X | AMM_ZP_Y_REL,
AMM_STA = AMM_ZP | AMM_ZP_X | AMM_ABS | AMM_ABS_Y | AMM_ABS_X | AMM_ZP_REL_X | AMM_ZP_Y_REL,
AMM_ASL = AMM_ACC | AMM_NON | AMM_ZP | AMM_ZP_X | AMM_ABS | AMM_ABS_X,
AMM_STX = AMM_FLIPXY | AMM_ZP | AMM_ZP_X | AMM_ABS, // note: for x ,x/,y flipped for this instr.
AMM_LDX = AMM_FLIPXY | AMM_IMM | AMM_ZP | AMM_ZP_X | AMM_ABS | AMM_ABS_X, // note: for x ,x/,y flipped for this instr.
AMM_STY = AMM_ZP | AMM_ZP_X | AMM_ABS,
AMM_LDY = AMM_IMM | AMM_ZP | AMM_ZP_X | AMM_ABS | AMM_ABS_X,
AMM_DEC = AMM_ZP | AMM_ZP_X | AMM_ABS | AMM_ABS_X,
AMM_BIT = AMM_ZP | AMM_ABS,
AMM_JMP = AMM_ABS | AMM_REL,
AMM_CPY = AMM_IMM | AMM_ZP | AMM_ABS,
// 6502 illegal modes
AMM_SLO = AMM_ZP | AMM_ZP_X | AMM_ABS | AMM_ABS_Y | AMM_ABS_X | AMM_ZP_REL_X | AMM_ZP_Y_REL,
AMM_AXS = AMM_FLIPXY | AMM_ZP | AMM_ZP_X | AMM_ZP_REL_X | AMM_ABS,
AMM_LAX = AMM_FLIPXY | AMM_ZP | AMM_ZP_X | AMM_ZP_REL_X | AMM_ABS | AMM_ABS_X | AMM_ZP_Y_REL,
AMM_AHX = AMM_FLIPXY | AMM_ZP_REL_X | AMM_ABS_X,
AMM_SHY = AMM_ABS_X,
AMM_SHX = AMM_ABS_Y,
// 65C02 groups
AMC_ORA = AMM_ORA | AMM_ZP_REL,
AMC_STA = AMM_STA | AMM_ZP_REL,
AMC_BIT = AMM_BIT | AMM_IMM | AMM_ZP_X | AMM_ABS_X,
AMC_DEC = AMM_DEC | AMM_NON | AMM_ACC,
AMC_JMP = AMM_JMP | AMM_REL_X,
AMC_STZ = AMM_ZP | AMM_ZP_X | AMM_ABS | AMM_ABS_X,
AMC_TRB = AMM_ZP | AMM_ABS,
AMC_BBR = AMM_ZP_ABS,
// 65816 groups
AM8_JSR = AMM_ABS | AMM_ABS_L | AMM_REL_X,
AM8_JSL = AMM_ABS_L,
AM8_BIT = AMM_IMM_DBL_A | AMC_BIT,
AM8_ORA = AMM_IMM_DBL_A | AMC_ORA | AMM_STK | AMM_ZP_REL_L | AMM_ABS_L | AMM_STK_REL_Y | AMM_ZP_REL_Y_L | AMM_ABS_L_X,
AM8_STA = AMC_STA | AMM_STK | AMM_ZP_REL_L | AMM_ABS_L | AMM_STK_REL_Y | AMM_ZP_REL_Y_L | AMM_ABS_L_X,
AM8_ORL = AMM_ABS_L | AMM_ABS_L_X,
AM8_STL = AMM_ABS_L | AMM_ABS_L_X,
AM8_LDX = AMM_IMM_DBL_XY | AMM_LDX,
AM8_LDY = AMM_IMM_DBL_XY | AMM_LDY,
AM8_CPY = AMM_IMM_DBL_XY | AMM_CPY,
AM8_JMP = AMC_JMP | AMM_REL_L | AMM_ABS_L | AMM_REL_X,
AM8_JML = AMM_REL_L | AMM_ABS_L,
AM8_BRL = AMM_BRANCH_L | AMM_ABS,
AM8_MVN = AMM_BLK_MOV,
AM8_PEI = AMM_ZP_REL | AMM_ZP,
AM8_PER = AMM_BRANCH_L | AMM_ABS,
AM8_REP = AMM_IMM | AMM_ZP, // Merlin allows this to look like a zp access
};
struct mnem {
const char *instr;
uint32_t modes;
uint8_t aCodes[AMB_COUNT];
};
struct mnem opcodes_6502[] = {
// nam modes (zp,x) zp # $0000 (zp),y zp,x abs,y abs,x (xx) A empty
{ "brk", AMM_BRK, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "jsr", AMM_ABS, { 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "rti", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40 } },
{ "rts", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60 } },
{ "ora", AMM_ORA, { 0x01, 0x05, 0x09, 0x0d, 0x11, 0x15, 0x19, 0x1d, 0x00, 0x00, 0x00 } },
{ "and", AMM_ORA, { 0x21, 0x25, 0x29, 0x2d, 0x31, 0x35, 0x39, 0x3d, 0x00, 0x00, 0x00 } },
{ "eor", AMM_ORA, { 0x41, 0x45, 0x49, 0x4d, 0x51, 0x55, 0x59, 0x5d, 0x00, 0x00, 0x00 } },
{ "adc", AMM_ORA, { 0x61, 0x65, 0x69, 0x6d, 0x71, 0x75, 0x79, 0x7d, 0x00, 0x00, 0x00 } },
{ "sta", AMM_STA, { 0x81, 0x85, 0x00, 0x8d, 0x91, 0x95, 0x99, 0x9d, 0x00, 0x00, 0x00 } },
{ "lda", AMM_ORA, { 0xa1, 0xa5, 0xa9, 0xad, 0xb1, 0xb5, 0xb9, 0xbd, 0x00, 0x00, 0x00 } },
{ "cmp", AMM_ORA, { 0xc1, 0xc5, 0xc9, 0xcd, 0xd1, 0xd5, 0xd9, 0xdd, 0x00, 0x00, 0x00 } },
{ "sbc", AMM_ORA, { 0xe1, 0xe5, 0xe9, 0xed, 0xf1, 0xf5, 0xf9, 0xfd, 0x00, 0x00, 0x00 } },
{ "asl", AMM_ASL, { 0x00, 0x06, 0x00, 0x0e, 0x00, 0x16, 0x00, 0x1e, 0x00, 0x0a, 0x0a } },
{ "rol", AMM_ASL, { 0x00, 0x26, 0x00, 0x2e, 0x00, 0x36, 0x00, 0x3e, 0x00, 0x2a, 0x2a } },
{ "lsr", AMM_ASL, { 0x00, 0x46, 0x00, 0x4e, 0x00, 0x56, 0x00, 0x5e, 0x00, 0x4a, 0x4a } },
{ "ror", AMM_ASL, { 0x00, 0x66, 0x00, 0x6e, 0x00, 0x76, 0x00, 0x7e, 0x00, 0x6a, 0x6a } },
{ "stx", AMM_STX, { 0x00, 0x86, 0x00, 0x8e, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ldx", AMM_LDX, { 0x00, 0xa6, 0xa2, 0xae, 0x00, 0xb6, 0x00, 0xbe, 0x00, 0x00, 0x00 } },
{ "dec", AMM_DEC, { 0x00, 0xc6, 0x00, 0xce, 0x00, 0xd6, 0x00, 0xde, 0x00, 0x00, 0x00 } },
{ "inc", AMM_DEC, { 0x00, 0xe6, 0x00, 0xee, 0x00, 0xf6, 0x00, 0xfe, 0x00, 0x00, 0x00 } },
{ "php", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 } },
{ "plp", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28 } },
{ "pha", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48 } },
{ "pla", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68 } },
{ "dey", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88 } },
{ "tay", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8 } },
{ "iny", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8 } },
{ "inx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8 } },
// nam modes (zp,x) zp # $0000 (zp),y zp,x abs,y abs,x (xx) A empty
{ "bpl", AMM_BRA, { 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bmi", AMM_BRA, { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bvc", AMM_BRA, { 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bvs", AMM_BRA, { 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bcc", AMM_BRA, { 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bcs", AMM_BRA, { 0x00, 0x00, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bne", AMM_BRA, { 0x00, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "beq", AMM_BRA, { 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "clc", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18 } },
{ "sec", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38 } },
{ "cli", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58 } },
{ "sei", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78 } },
{ "tya", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98 } },
{ "clv", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8 } },
{ "cld", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd8 } },
{ "sed", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8 } },
{ "bit", AMM_BIT, { 0x00, 0x24, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "jmp", AMM_JMP, { 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00 } },
{ "sty", AMM_STY, { 0x00, 0x84, 0x00, 0x8c, 0x00, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ldy", AMM_LDY, { 0x00, 0xa4, 0xa0, 0xac, 0x00, 0xb4, 0x00, 0xbc, 0x00, 0x00, 0x00 } },
{ "cpy", AMM_CPY, { 0x00, 0xc4, 0xc0, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "cpx", AMM_CPY, { 0x00, 0xe4, 0xe0, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "txa", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a } },
{ "txs", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a } },
{ "tax", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa } },
{ "tsx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba } },
{ "dex", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xca } },
{ "nop", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea } },
// 21 ILLEGAL 6502 OPCODES (http://www.oxyron.de/html/opcodes02.html)
// NOTE: If adding or removing, update NUM_ILLEGAL_6502_OPS
// nam modes (zp,x) zp # $0000 (zp),y zp,x abs,y abs,x (xx) A empty
{ "slo", AMM_SLO, { 0x03, 0x07, 0x00, 0x0f, 0x13, 0x17, 0x1b, 0x1f, 0x00, 0x00, 0x00 } },
{ "rla", AMM_SLO, { 0x23, 0x27, 0x00, 0x2f, 0x33, 0x37, 0x3b, 0x3f, 0x00, 0x00, 0x00 } },
{ "sre", AMM_SLO, { 0x43, 0x47, 0x00, 0x4f, 0x53, 0x57, 0x5b, 0x5f, 0x00, 0x00, 0x00 } },
{ "rra", AMM_SLO, { 0x63, 0x67, 0x00, 0x6f, 0x73, 0x77, 0x7b, 0x7f, 0x00, 0x00, 0x00 } },
{ "sax", AMM_IMM, { 0x00, 0x00, 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "lax", AMM_LAX, { 0xa3, 0xa7, 0x00, 0xaf, 0xb3, 0xb7, 0x00, 0xbf, 0x00, 0x00, 0x00 } },
{ "dcp", AMM_SLO, { 0xc3, 0xc7, 0x00, 0xcf, 0xd3, 0xd7, 0xdb, 0xdf, 0x00, 0x00, 0x00 } },
{ "isc", AMM_SLO, { 0xe3, 0xe7, 0x00, 0xef, 0xf3, 0xf7, 0xfb, 0xff, 0x00, 0x00, 0x00 } },
{ "anc", AMM_IMM, { 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "aac", AMM_IMM, { 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "alr", AMM_IMM, { 0x00, 0x00, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "arr", AMM_IMM, { 0x00, 0x00, 0x6b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "xaa", AMM_IMM, { 0x00, 0x00, 0x8b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{"lax2", AMM_IMM, { 0x00, 0x00, 0xab, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "axs", AMM_AXS, { 0x83, 0x87, 0x00, 0x8f, 0x00, 0x97, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "sbi", AMM_IMM, { 0x00, 0x00, 0xeb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ahx", AMM_AHX, { 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9f, 0x00, 0x00, 0x00 } },
{ "shy", AMM_SHY, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9c, 0x00, 0x00, 0x00 } },
{ "shx", AMM_SHX, { 0x00, 0x00, 0x00, 0x00, 0x93, 0x00, 0x9e, 0x00, 0x00, 0x00, 0x00 } },
{ "tas", AMM_SHX, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9b, 0x00, 0x00, 0x00, 0x00 } },
{ "las", AMM_SHX, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbb, 0x00, 0x00, 0x00, 0x00 } },
};
const char* aliases_6502[] = {
"bcc", "blt",
"bcs", "bge",
nullptr, nullptr
};
uint8_t timing_6502[] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x0e, 0x0c, 0xff, 0x0f, 0xff, 0x06, 0x0a, 0x0a, 0x06, 0x04, 0x04, 0x04, 0x04, 0x08, 0x0c, 0x0c, // 0
0x05, 0x0b, 0xff, 0x0f, 0xff, 0x08, 0x0c, 0x0c, 0x04, 0x09, 0x02, 0x0e, 0x04, 0x09, 0x0e, 0x0f, // 1
0x0c, 0x0c, 0xff, 0x0f, 0x06, 0x06, 0x0a, 0x0a, 0x08, 0x04, 0x04, 0x04, 0x08, 0x08, 0x0c, 0x0c, // 2
0x05, 0x0b, 0xff, 0x0f, 0xff, 0x08, 0x0c, 0x0c, 0x04, 0x09, 0x02, 0x0e, 0x04, 0x09, 0x0e, 0x0f, // 3
0x0c, 0x0c, 0xff, 0x0f, 0xff, 0x06, 0x0a, 0x0a, 0x06, 0x04, 0x04, 0x04, 0x06, 0x08, 0x0c, 0x0c, // 4
0x05, 0x0b, 0xff, 0x0f, 0xff, 0x08, 0x0c, 0x0c, 0x04, 0x09, 0x02, 0xff, 0x04, 0x09, 0x0e, 0x0f, // 5
0x0c, 0x0c, 0xff, 0x0f, 0xff, 0x06, 0x0a, 0x0a, 0x08, 0x04, 0x04, 0x04, 0x0a, 0x08, 0x0c, 0x0c, // 6
0x05, 0x0b, 0xff, 0x0f, 0xff, 0x08, 0x0c, 0x0c, 0x04, 0x09, 0x02, 0x0e, 0x04, 0x09, 0x0e, 0x0f, // 7
0xff, 0x0c, 0xff, 0x0c, 0x06, 0x06, 0x06, 0x06, 0x04, 0xff, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, // 8
0x05, 0x0c, 0xff, 0x0c, 0x08, 0x08, 0x08, 0x08, 0x04, 0x0a, 0x04, 0x0a, 0x05, 0x0a, 0x05, 0x0a, // 9
0x04, 0x0c, 0x04, 0x0c, 0x06, 0x06, 0x06, 0x06, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, // A
0x05, 0x0b, 0xff, 0x0a, 0x08, 0x08, 0x08, 0x08, 0x04, 0x09, 0x04, 0x09, 0x09, 0x09, 0x09, 0x0a, // B
0x04, 0x0c, 0xff, 0x0f, 0x06, 0x06, 0x0a, 0x0a, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x0c, 0x0c, // C
0x05, 0x0b, 0xff, 0x0f, 0xff, 0x08, 0x0c, 0x0c, 0x04, 0x09, 0x02, 0x0e, 0x04, 0x09, 0x0e, 0x0e, // D
0x04, 0x0c, 0xff, 0xff, 0x06, 0x06, 0x0a, 0x0a, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x0c, 0x0c, // E
0x05, 0x0b, 0xff, 0xff, 0xff, 0x08, 0x0c, 0x0c, 0x04, 0x09, 0x02, 0x0e, 0x04, 0x09, 0x0e, 0x0e // F
};
static const int num_opcodes_6502 = sizeof(opcodes_6502) / sizeof(opcodes_6502[0]);
struct mnem opcodes_65C02[] = {
// nam modes (zp,x) zp # $0000 (zp),y zp,x abs,y abs,x (xx) A empty (zp)(abs,x)zp,abs
{ "brk", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "jsr", AMM_ABS, { 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "rti", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00 } },
{ "rts", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00 } },
{ "ora", AMC_ORA, { 0x01, 0x05, 0x09, 0x0d, 0x11, 0x15, 0x19, 0x1d, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00 } },
{ "and", AMC_ORA, { 0x21, 0x25, 0x29, 0x2d, 0x31, 0x35, 0x39, 0x3d, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00 } },
{ "eor", AMC_ORA, { 0x41, 0x45, 0x49, 0x4d, 0x51, 0x55, 0x59, 0x5d, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00 } },
{ "adc", AMC_ORA, { 0x61, 0x65, 0x69, 0x6d, 0x71, 0x75, 0x79, 0x7d, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00 } },
{ "sta", AMC_STA, { 0x81, 0x85, 0x00, 0x8d, 0x91, 0x95, 0x99, 0x9d, 0x00, 0x00, 0x00, 0x92, 0x00, 0x00 } },
{ "lda", AMC_ORA, { 0xa1, 0xa5, 0xa9, 0xad, 0xb1, 0xb5, 0xb9, 0xbd, 0x00, 0x00, 0x00, 0xb2, 0x00, 0x00 } },
{ "cmp", AMC_ORA, { 0xc1, 0xc5, 0xc9, 0xcd, 0xd1, 0xd5, 0xd9, 0xdd, 0x00, 0x00, 0x00, 0xd2, 0x00, 0x00 } },
{ "sbc", AMC_ORA, { 0xe1, 0xe5, 0xe9, 0xed, 0xf1, 0xf5, 0xf9, 0xfd, 0x00, 0x00, 0x00, 0xf2, 0x00, 0x00 } },
{ "asl", AMM_ASL, { 0x00, 0x06, 0x00, 0x0e, 0x00, 0x16, 0x00, 0x1e, 0x00, 0x0a, 0x0a, 0x00, 0x00, 0x00 } },
{ "rol", AMM_ASL, { 0x00, 0x26, 0x00, 0x2e, 0x00, 0x36, 0x00, 0x3e, 0x00, 0x2a, 0x2a, 0x00, 0x00, 0x00 } },
{ "lsr", AMM_ASL, { 0x00, 0x46, 0x00, 0x4e, 0x00, 0x56, 0x00, 0x5e, 0x00, 0x4a, 0x4a, 0x00, 0x00, 0x00 } },
{ "ror", AMM_ASL, { 0x00, 0x66, 0x00, 0x6e, 0x00, 0x76, 0x00, 0x7e, 0x00, 0x6a, 0x6a, 0x00, 0x00, 0x00 } },
{ "stx", AMM_STX, { 0x00, 0x86, 0x00, 0x8e, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ldx", AMM_LDX, { 0x00, 0xa6, 0xa2, 0xae, 0x00, 0xb6, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "dec", AMC_DEC, { 0x00, 0xc6, 0x00, 0xce, 0x00, 0xd6, 0x00, 0xde, 0x00, 0x3a, 0x3a, 0x00, 0x00, 0x00 } },
{ "inc", AMC_DEC, { 0x00, 0xe6, 0x00, 0xee, 0x00, 0xf6, 0x00, 0xfe, 0x00, 0x1a, 0x1a, 0x00, 0x00, 0x00 } },
{ "dea", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xde, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00 } },
{ "ina", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00 } },
{ "php", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00 } },
{ "plp", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00 } },
{ "pha", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00 } },
{ "pla", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00 } },
{ "phy", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x00 } },
{ "ply", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7a, 0x00, 0x00, 0x00 } },
{ "phx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xda, 0x00, 0x00, 0x00 } },
{ "plx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x00 } },
{ "dey", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00 } },
{ "tay", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0x00, 0x00, 0x00 } },
{ "iny", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x00, 0x00 } },
{ "inx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x00, 0x00, 0x00 } },
// nam modes (zp,x) zp # $0000 (zp),y zp,x abs,y abs,x (xx) A empty (zp)(abs,x)zp,abs
{ "bpl", AMM_BRA, { 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bmi", AMM_BRA, { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bvc", AMM_BRA, { 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bvs", AMM_BRA, { 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bra", AMM_BRA, { 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bcc", AMM_BRA, { 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bcs", AMM_BRA, { 0x00, 0x00, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bne", AMM_BRA, { 0x00, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "beq", AMM_BRA, { 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "clc", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00 } },
{ "sec", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00 } },
{ "cli", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00 } },
{ "sei", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00 } },
{ "tya", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00 } },
{ "clv", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x00 } },
{ "cld", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd8, 0x00, 0x00, 0x00 } },
{ "sed", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00 } },
{ "bit", AMC_BIT, { 0x00, 0x24, 0x89, 0x2c, 0x00, 0x34, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "stz", AMC_STZ, { 0x00, 0x64, 0x00, 0x9c, 0x00, 0x74, 0x00, 0x9e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "trb", AMC_TRB, { 0x00, 0x14, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tsb", AMC_TRB, { 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "jmp", AMC_JMP, { 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x7c, 0x00 } },
{ "sty", AMM_STY, { 0x00, 0x84, 0x00, 0x8c, 0x00, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ldy", AMM_LDY, { 0x00, 0xa4, 0xa0, 0xac, 0x00, 0xb4, 0x00, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "cpy", AMM_CPY, { 0x00, 0xc4, 0xc0, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "cpx", AMM_CPY, { 0x00, 0xe4, 0xe0, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "txa", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x00, 0x00, 0x00 } },
{ "txs", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, 0x00, 0x00 } },
{ "tax", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, 0x00, 0x00 } },
{ "tsx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, 0x00, 0x00, 0x00 } },
{ "dex", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xca, 0x00, 0x00, 0x00 } },
{ "nop", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x00, 0x00, 0x00 } },
// WDC specific (18 instructions)
// nam modes (zp,x) zp # $0000 (zp),y zp,x abs,y abs,x (xx) A empty (zp)(abs,x)zp,abs
{ "stp", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdb, 0x00, 0x00, 0x00 } },
{ "wai", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb, 0x00, 0x00, 0x00 } },
{ "bbr0", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f } },
{ "bbr1", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f } },
{ "bbr2", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2f } },
{ "bbr3", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f } },
{ "bbr4", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f } },
{ "bbr5", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5f } },
{ "bbr6", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f } },
{ "bbr7", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f } },
{ "bbs0", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f } },
{ "bbs1", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9f } },
{ "bbs2", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaf } },
{ "bbs3", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbf } },
{ "bbs4", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcf } },
{ "bbs5", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdf } },
{ "bbs6", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef } },
{ "bbs7", AMC_BBR, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x00, 0x00, 0xff } },
};
const char* aliases_65C02[] = {
"bcc", "blt",
"bcs", "bge",
nullptr, nullptr
};
static const int num_opcodes_65C02 = sizeof(opcodes_65C02) / sizeof(opcodes_65C02[0]);
struct mnem opcodes_65816[] = {
// nam modes (zp,x) zp # $0000 (zp),y zp,x abs,y abs,x (xx) A empty (zp)(abs,x)zp,abs [zp] [zp],y absl absl,x b,s (b,s),y[$000] b,b
{ "brk", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "jsr", AM8_JSR, { 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "jsl", AM8_JSL, { 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "rti", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "rts", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "rtl", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ora", AM8_ORA, { 0x01, 0x05, 0x09, 0x0d, 0x11, 0x15, 0x19, 0x1d, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x07, 0x17, 0x0f, 0x1f, 0x03, 0x13, 0x00, 0x00 } },
{ "and", AM8_ORA, { 0x21, 0x25, 0x29, 0x2d, 0x31, 0x35, 0x39, 0x3d, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x27, 0x37, 0x2f, 0x3f, 0x23, 0x33, 0x00, 0x00 } },
{ "eor", AM8_ORA, { 0x41, 0x45, 0x49, 0x4d, 0x51, 0x55, 0x59, 0x5d, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x47, 0x57, 0x4f, 0x5f, 0x43, 0x53, 0x00, 0x00 } },
{ "adc", AM8_ORA, { 0x61, 0x65, 0x69, 0x6d, 0x71, 0x75, 0x79, 0x7d, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x67, 0x77, 0x6f, 0x7f, 0x63, 0x73, 0x00, 0x00 } },
{ "sta", AM8_STA, { 0x81, 0x85, 0x00, 0x8d, 0x91, 0x95, 0x99, 0x9d, 0x00, 0x00, 0x00, 0x92, 0x00, 0x00, 0x87, 0x97, 0x8f, 0x9f, 0x83, 0x93, 0x00, 0x00 } },
{ "lda", AM8_ORA, { 0xa1, 0xa5, 0xa9, 0xad, 0xb1, 0xb5, 0xb9, 0xbd, 0x00, 0x00, 0x00, 0xb2, 0x00, 0x00, 0xa7, 0xb7, 0xaf, 0xbf, 0xa3, 0xb3, 0x00, 0x00 } },
{ "cmp", AM8_ORA, { 0xc1, 0xc5, 0xc9, 0xcd, 0xd1, 0xd5, 0xd9, 0xdd, 0x00, 0x00, 0x00, 0xd2, 0x00, 0x00, 0xc7, 0xd7, 0xcf, 0xdf, 0xc3, 0xd3, 0x00, 0x00 } },
{ "sbc", AM8_ORA, { 0xe1, 0xe5, 0xe9, 0xed, 0xf1, 0xf5, 0xf9, 0xfd, 0x00, 0x00, 0x00, 0xf2, 0x00, 0x00, 0xe7, 0xf7, 0xef, 0xff, 0xe3, 0xf3, 0x00, 0x00 } },
{"oral", AM8_ORL, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x1f, 0x00, 0x00, 0x00, 0x00 } },
{"andl", AM8_ORL, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2f, 0x3f, 0x00, 0x00, 0x00, 0x00 } },
{"eorl", AM8_ORL, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x5f, 0x00, 0x00, 0x00, 0x00 } },
{"adcl", AM8_ORL, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0x7f, 0x00, 0x00, 0x00, 0x00 } },
{"stal", AM8_STL, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x9f, 0x00, 0x00, 0x00, 0x00 } },
{"ldal", AM8_ORL, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaf, 0xbf, 0x00, 0x00, 0x00, 0x00 } },
{"cmpl", AM8_ORL, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcf, 0xdf, 0x00, 0x00, 0x00, 0x00 } },
{"sbcl", AM8_ORL, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xff, 0x00, 0x00, 0x00, 0x00 } },
{ "asl", AMM_ASL, { 0x00, 0x06, 0x00, 0x0e, 0x00, 0x16, 0x00, 0x1e, 0x00, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "rol", AMM_ASL, { 0x00, 0x26, 0x00, 0x2e, 0x00, 0x36, 0x00, 0x3e, 0x00, 0x2a, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "lsr", AMM_ASL, { 0x00, 0x46, 0x00, 0x4e, 0x00, 0x56, 0x00, 0x5e, 0x00, 0x4a, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ror", AMM_ASL, { 0x00, 0x66, 0x00, 0x6e, 0x00, 0x76, 0x00, 0x7e, 0x00, 0x6a, 0x6a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "stx", AMM_STX, { 0x00, 0x86, 0x00, 0x8e, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ldx", AM8_LDX, { 0x00, 0xa6, 0xa2, 0xae, 0x00, 0xb6, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "dec", AMC_DEC, { 0x00, 0xc6, 0x00, 0xce, 0x00, 0xd6, 0x00, 0xde, 0x00, 0x3a, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "inc", AMC_DEC, { 0x00, 0xe6, 0x00, 0xee, 0x00, 0xf6, 0x00, 0xfe, 0x00, 0x1a, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "dea", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xde, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ina", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "php", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "plp", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "pha", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
// nam modes (zp,x) zp # $0000 (zp),y zp,x abs,y abs,x (xx) A empty (zp)(abs,x)zp,abs [zp] [zp],y absl absl,x b,s (b,s),y[$0000]b,b
{ "pla", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "phy", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ply", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "phx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "plx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "dey", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tay", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "iny", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "inx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bpl", AMM_BRA, { 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bmi", AMM_BRA, { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bvc", AMM_BRA, { 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bvs", AMM_BRA, { 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bra", AMM_BRA, { 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "brl", AM8_BRL, { 0x00, 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bcc", AMM_BRA, { 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bcs", AMM_BRA, { 0x00, 0x00, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bne", AMM_BRA, { 0x00, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "beq", AMM_BRA, { 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "clc", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "sec", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "cli", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "sei", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tya", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "clv", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "cld", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "sed", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "bit", AM8_BIT, { 0x00, 0x24, 0x89, 0x2c, 0x00, 0x34, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "stz", AMC_STZ, { 0x00, 0x64, 0x00, 0x9c, 0x00, 0x74, 0x00, 0x9e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "trb", AMC_TRB, { 0x00, 0x14, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tsb", AMC_TRB, { 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
// nam modes (zp,x) zp # $0000 (zp),y zp,x abs,y abs,x (xx) A empty (zp)(abs,x)zp,abs [zp] [zp],y absl absl,x b,s (b,s),y[$0000]b,b
{ "jmp", AM8_JMP, { 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, 0xdc, 0x00 } },
{ "jml", AM8_JML, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, 0xdc, 0x00 } },
{ "sty", AMM_STY, { 0x00, 0x84, 0x00, 0x8c, 0x00, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "ldy", AM8_LDY, { 0x00, 0xa4, 0xa0, 0xac, 0x00, 0xb4, 0x00, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "cpy", AM8_CPY, { 0x00, 0xc4, 0xc0, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "cpx", AM8_CPY, { 0x00, 0xe4, 0xe0, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "txa", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "txs", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tax", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tsx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "dex", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xca, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "nop", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "cop", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "wdm", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "mvp", AM8_MVN, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44 } },
{ "mvn", AM8_MVN, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54 } },
{ "pea", AMM_ABS, { 0x00, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "pei", AM8_PEI, { 0x00, 0xd4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "per", AM8_PER, { 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "rep", AM8_REP, { 0x00, 0xc2, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "sep", AM8_REP, { 0x00, 0xe2, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "phd", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tcs", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "pld", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tsc", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "phk", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tcd", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tdc", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "phb", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "txy", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "plb", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "tyx", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "wai", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "stp", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "xba", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xeB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
{ "xce", AMM_NON, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
};
const char* aliases_65816[] = {
"bcc", "blt",
"bcs", "bge",
"tcs", "tas",
"tsc", "tsa",
"xba", "swa",
"tcd", "tad",
"tdc", "tda",
nullptr, nullptr
};
static const int num_opcodes_65816 = sizeof(opcodes_65816) / sizeof(opcodes_65816[0]);
uint8_t timing_65816[] = {
0x4e, 0x1c, 0x4e, 0x28, 0x3a, 0x26, 0x3a, 0x1c, 0x46, 0x24, 0x44, 0x48, 0x4c, 0x28, 0x5c, 0x2a,
0x44, 0x1a, 0x1a, 0x2e, 0x3a, 0x18, 0x6c, 0x1c, 0x44, 0x28, 0x44, 0x44, 0x4c, 0x28, 0x5e, 0x2a,
0x4c, 0x1c, 0x50, 0x28, 0x16, 0x26, 0x3a, 0x1c, 0x48, 0x24, 0x44, 0x4a, 0x28, 0x28, 0x4c, 0x2a,
0x44, 0x1a, 0x1a, 0x2e, 0x18, 0x18, 0x3c, 0x1c, 0x44, 0x28, 0x44, 0x44, 0x28, 0x28, 0x4e, 0x2a,
0x4c, 0x1c, 0x42, 0x28, 0x42, 0x16, 0x6a, 0x1c, 0x26, 0x24, 0x44, 0x46, 0x46, 0x28, 0x5c, 0x2a,
0x44, 0x1a, 0x1a, 0x2e, 0x42, 0x18, 0x6c, 0x1c, 0x44, 0x28, 0x76, 0x44, 0x48, 0x28, 0x5e, 0x2a,
0x4c, 0x1c, 0x4c, 0x28, 0x16, 0x26, 0x3a, 0x1c, 0x28, 0x24, 0x44, 0x4c, 0x4a, 0x28, 0x4c, 0x2a,
0x44, 0x1a, 0x1a, 0x2e, 0x28, 0x18, 0x3c, 0x1c, 0x44, 0x28, 0x78, 0x44, 0x4c, 0x28, 0x4e, 0x2a,
0x46, 0x1c, 0x48, 0x28, 0x86, 0x16, 0x86, 0x1c, 0x44, 0x24, 0x44, 0x46, 0x78, 0x28, 0x78, 0x2a,
0x44, 0x1c, 0x1a, 0x2e, 0x88, 0x18, 0x88, 0x1c, 0x44, 0x2a, 0x44, 0x44, 0x28, 0x2a, 0x2a, 0x2a,
0x74, 0x1c, 0x74, 0x28, 0x86, 0x16, 0x86, 0x1c, 0x44, 0x24, 0x44, 0x48, 0x78, 0x28, 0x78, 0x2a,
0x44, 0x1a, 0x1a, 0x2e, 0x88, 0x18, 0x88, 0x1c, 0x44, 0x28, 0x44, 0x44, 0x78, 0x28, 0x78, 0x2a,
0x74, 0x1c, 0x46, 0x28, 0x86, 0x16, 0x6a, 0x1c, 0x44, 0x24, 0x44, 0x26, 0x78, 0x28, 0x5c, 0x2a,
0x44, 0x1a, 0x1a, 0x2e, 0x4c, 0x18, 0x6c, 0x1c, 0x44, 0x28, 0x76, 0x46, 0x4c, 0x28, 0x5e, 0x2a,
0x74, 0x3c, 0x46, 0x48, 0x86, 0x36, 0x6a, 0x3c, 0x44, 0x44, 0x44, 0x46, 0x78, 0x48, 0x5c, 0x4a,
0x44, 0x3a, 0x3a, 0x4e, 0x4a, 0x38, 0x6c, 0x3c, 0x44, 0x48, 0x78, 0x44, 0x50, 0x48, 0x5e, 0x4a
};
// m=0, i=0, dp!=0
uint8_t timing_65816_plus[9][3] = {
{ 0, 0, 0 }, // 6502 plus timing check bit 0
{ 1, 0, 1 }, // acc 16 bit + dp!=0
{ 1, 0, 0 }, // acc 16 bit
{ 0, 0, 1 }, // dp != 0
{ 0, 0, 0 }, // no plus
{ 2, 0, 0 }, // acc 16 bit yields 2+
{ 2, 0, 1 }, // acc 16 bit yields 2+ + dp!=0
{ 0, 1, 0 }, // idx 16 bit
{ 0, 1, 1 } // idx 16 bit + dp!=0
};
// 65C02
// http://6502.org/tutorials/65c02opcodes.html
// http://www.oxyron.de/html/opcodesc02.html
// 65816
// http://wiki.superfamicom.org/snes/show/65816+Reference#fn:14
// http://softpixel.com/~cwright/sianse/docs/65816NFO.HTM
// http://www.oxyron.de/html/opcodes816.html
// How instruction argument is encoded
enum CODE_ARG {
CA_NONE, // single byte instruction
CA_ONE_BYTE, // instruction carries one byte
CA_TWO_BYTES, // instruction carries two bytes
CA_THREE_BYTES, // instruction carries three bytes
CA_BRANCH, // instruction carries an 8 bit relative address
CA_BRANCH_16, // instruction carries a 16 bit relative address
CA_BYTE_BRANCH, // instruction carries one byte and one branch
CA_TWO_ARG_BYTES, // two separate values
};
enum CPUIndex {
CPU_6502,
CPU_6502_ILLEGAL,
CPU_65C02,
CPU_65C02_WDC,
CPU_65816
};
// CPU by index
struct CPUDetails {
mnem *opcodes;
int num_opcodes;
const char* name;
const char** aliases;
const uint8_t *timing;
} aCPUs[] = {
{ opcodes_6502, num_opcodes_6502 - NUM_ILLEGAL_6502_OPS, "6502", aliases_6502, timing_6502 },
{ opcodes_6502, num_opcodes_6502, "6502ill", aliases_6502, timing_6502 },
{ opcodes_65C02, num_opcodes_65C02 - NUM_WDC_65C02_SPECIFIC_OPS, "65C02", aliases_65C02, nullptr },
{ opcodes_65C02, num_opcodes_65C02, "65C02WDC", aliases_65C02, nullptr },
{ opcodes_65816, num_opcodes_65816, "65816", aliases_65816, timing_65816 },
};
static const int nCPUs = sizeof(aCPUs) / sizeof(aCPUs[0]);
// hardtexted strings
static const strref c_comment("//");
static const strref word_char_range("!0-9a-zA-Z_@$!#");
static const strref macro_arg_bookend("!0-9a-zA-Z_@$!.");
static const strref label_end_char_range("!0-9a-zA-Z_@$!.!:");
static const strref label_end_char_range_merlin("!0-9a-zA-Z_@$]:?");
static const strref filename_end_char_range("!0-9a-zA-Z_!@#$%&()/\\-.");
static const strref keyword_equ("equ");
static const strref str_label("label");
static const strref str_const("const");
static const strref struct_byte("byte");
static const strref struct_word("word");
static const strref import_source("source");
static const strref import_binary("binary");
static const strref import_c64("c64");
static const strref import_text("text");
static const strref import_object("object");
static const strref import_symbols("symbols");
static const strref pool_subpool("pool");
static const char* aAddrModeFmt[] = {
"%s ($%02x,x)", // 00
"%s $%02x", // 01
"%s #$%02x", // 02
"%s $%04x", // 03
"%s ($%02x),y", // 04
"%s $%02x,x", // 05
"%s $%04x,y", // 06
"%s $%04x,x", // 07
"%s ($%04x)", // 08
"%s A", // 09
"%s ", // 0a
"%s ($%02x)", // 0b
"%s ($%04x,x)", // 0c
"%s $%02x, $%04x", // 0d
"%s [$%02x]", // 0e
"%s [$%02x],y", // 0f
"%s $%06x", // 10
"%s $%06x,x", // 11
"%s $%02x,s", // 12
"%s ($%02x,s),y", // 13
"%s [$%04x]", // 14
"%s $%02x,$%02x", // 15
};
static const char *str_section_type[] = {
"UNDEFINED", // not set
"CODE", // default type
"DATA", // data section (matters for GS/OS OMF)
"BSS", // uninitialized data section
"ZEROPAGE" // uninitialized data section in zero page / direct page
};
static const int num_section_type_str = sizeof(str_section_type) / sizeof(str_section_type[0]);
typedef struct sDirectiveName {
const char *name;
AssemblerDirective directive;
} DirectiveName;
DirectiveName aDirectiveNames[] {
{ "CPU", AD_CPU },
{ "PROCESSOR", AD_CPU },
{ "PC", AD_ORG },
{ "ORG", AD_ORG },
{ "LOAD", AD_LOAD },
{ "EXPORT", AD_EXPORT },
{ "SECTION", AD_SECTION },
{ "SEG", AD_SECTION }, // DASM version of SECTION
{ "SEGMENT", AD_SECTION }, // CA65 version of SECTION
{ "MERGE", AD_MERGE },
{ "LINK", AD_LINK },
{ "XDEF", AD_XDEF },
{ "XREF", AD_XREF },
{ "INCOBJ", AD_INCOBJ },
{ "ALIGN", AD_ALIGN },
{ "MACRO", AD_MACRO },
{ "MAC", AD_MACRO }, // MERLIN
{ "EVAL", AD_EVAL },
{ "PRINT", AD_EVAL },
{ "ECHO", AD_EVAL }, // DASM version of EVAL/PRINT
{ "BYTE", AD_BYTES },
{ "BYTES", AD_BYTES },
{ "WORD", AD_WORDS },
{ "WORDS", AD_WORDS },
{ "LONG", AD_ADRL },
{ "DC", AD_DC },
{ "DV", AD_DC }, // DASM variation of DC which allows expressions
{ "TEXT", AD_TEXT },
{ "INCLUDE", AD_INCLUDE },
{ "INCBIN", AD_INCBIN },
{ "IMPORT", AD_IMPORT },
{ "CONST", AD_CONST },
{ "LABEL", AD_LABEL },
{ "STRING", AD_STRING },
{ "FUNCTION", AD_FUNCTION },
{ "UNDEF", AD_UNDEF },
{ "INCSYM", AD_INCSYM },
{ "LABPOOL", AD_LABPOOL },
{ "POOL", AD_LABPOOL },
{ "IF", AD_IF },
{ "IFDEF", AD_IFDEF },
{ "IFNDEF", AD_IFNDEF },
{ "IFCONST", AD_IFCONST },
{ "IFBLANK", AD_IFBLANK }, // #IFBLANK: Conditional assembly follows based on rest of line empty
{ "IFNBLANK", AD_IFNBLANK }, // #IFDEF: Conditional assembly follows based on rest of line not empty
{ "ELSE", AD_ELSE },
{ "ELIF", AD_ELIF },
{ "ENDIF", AD_ENDIF },
{ "STRUCT", AD_STRUCT },
{ "ENUM", AD_ENUM },
{ "REPT", AD_REPT },
{ "REPEAT", AD_REPT }, // ca65 version of rept
{ "INCDIR", AD_INCDIR },
{ "A16", AD_A16 }, // A16: Set 16 bit accumulator mode
{ "A8", AD_A8 }, // A8: Set 8 bit accumulator mode
{ "XY16", AD_XY16 }, // XY16: Set 16 bit index register mode
{ "XY8", AD_XY8 }, // XY8: Set 8 bit index register mode
{ "I16", AD_XY16 }, // I16: Set 16 bit index register mode
{ "I8", AD_XY8 }, // I8: Set 8 bit index register mode
{ "DUMMY", AD_DUMMY },
{ "DUMMY_END", AD_DUMMY_END },
{ "DS", AD_DS }, // Define space
{ "RES", AD_DS }, // Reserve space
{ "SCOPE", AD_SCOPE }, // SCOPE: Begin ca65 style scope
{ "ENDSCOPE", AD_ENDSCOPE },// ENDSCOPR: End ca65 style scope
{ "PUSH", AD_PUSH },
{ "PULL", AD_PULL },
{ "ABORT", AD_ABORT },
{ "ERR", AD_ABORT }, // DASM version of ABORT
};
// Merlin specific directives separated from regular directives to avoid confusion
DirectiveName aDirectiveNamesMerlin[] {
{ "MX", AD_MX }, // MERLIN
{ "STR", AD_LNK }, // MERLIN
{ "DA", AD_WORDS }, // MERLIN
{ "DW", AD_WORDS }, // MERLIN
{ "ASC", AD_TEXT }, // MERLIN
{ "PUT", AD_INCLUDE }, // MERLIN
{ "DDB", AD_DBL_BYTES }, // MERLIN
{ "DB", AD_BYTES }, // MERLIN
{ "DFB", AD_BYTES }, // MERLIN
{ "HEX", AD_HEX }, // MERLIN
{ "DO", AD_IF }, // MERLIN
{ "FIN", AD_ENDIF }, // MERLIN
{ "EJECT", AD_EJECT }, // MERLIN
{ "OBJ", AD_EJECT }, // MERLIN
{ "TR", AD_EJECT }, // MERLIN
{ "END", AD_EJECT }, // MERLIN
{ "REL", AD_EJECT }, // MERLIN
{ "USR", AD_USR }, // MERLIN
{ "DUM", AD_DUMMY }, // MERLIN
{ "DEND", AD_DUMMY_END }, // MERLIN
{ "LST", AD_LST }, // MERLIN
{ "LSTDO", AD_LST }, // MERLIN
{ "LUP", AD_REPT }, // MERLIN
{ "SAV", AD_SAV }, // MERLIN
{ "DSK", AD_SAV }, // MERLIN
{ "LNK", AD_LNK }, // MERLIN
{ "XC", AD_XC }, // MERLIN
{ "ENT", AD_ENT }, // MERLIN (xdef, but label on same line)
{ "EXT", AD_EXT }, // MERLIN (xref, which are implied in x65 object files)
{ "ADR", AD_ADR }, // ADR: MERLIN store 3 byte word
{ "ADRL", AD_ADRL }, // ADRL: MERLIN store 4 byte word
{ "CYC", AD_CYC }, // MERLIN: Start and stop cycle counter
};
struct EvalFuncNames {
const char* name;
EvalFuncs function;
};
EvalFuncNames aEvalFunctions[] = {
{ "DEFINED", EF_DEFINED }, // DEFINED(label) 1 if label is defined
{ "DEF", EF_DEFINED }, // DEFINED(label) 1 if label is defined
{ "REFERENCED", EF_REFERENCED }, // REFERENCED(label) 1 if label has been referenced in this file
{ "BLANK", EF_BLANK }, // BLANK() 1 if the contents within the parenthesis is empty
{ "CONST", EF_CONST }, // CONST(label) 1 if label is a const label
{ "SIZEOF", EF_SIZEOF}, // SIZEOF(struct) returns size of structs
{ "TRIGSIN", EF_SIN }, // TRIGSIN(index, period, amplitude)
};
static const int nDirectiveNames = sizeof(aDirectiveNames) / sizeof(aDirectiveNames[0]);
static const int nDirectiveNamesMerlin = sizeof(aDirectiveNamesMerlin) / sizeof(aDirectiveNamesMerlin[0]);
static const int nEvalFuncs = sizeof(aEvalFunctions) / sizeof(aEvalFunctions[0]);
// Binary search over an array of unsigned integers, may contain multiple instances of same key
uint32_t FindLabelIndex(uint32_t hash, uint32_t *table, uint32_t count)
{
uint32_t max = count;
uint32_t first = 0;
while (count!=first) {
int index = (first+count)/2;
uint32_t read = table[index];
if (hash==read) {
while (index && table[index-1]==hash)
index--; // guarantee first identical index returned on match
return index;
} else if (hash>read)
first = index+1;
else
count = index;
}
if (count<max && table[count]<hash)
count++;
else if (count && table[count-1]>hash)
count--;
return count;
}
char* StringCopy(strref str)
{
char* buf = (char*)calloc(1, (size_t)str.get_len() + 1);
if (buf && str.get_len()) { memcpy(buf, str.get(), str.get_len()); }
return buf;
}
//
//
// ASSEMBLER STATE
//
//
// pairArray is basically two vectors sharing a size without constructors on growth or insert
template <class H, class V> class pairArray {
protected:
H *keys;
V *values;
uint32_t _count;
uint32_t _capacity;
public:
pairArray() : keys(nullptr), values(nullptr), _count(0), _capacity(0) {}
void reserve(uint32_t size) {
if (size>_capacity) {
H *new_keys = (H*)malloc(sizeof(H) * size); if (!new_keys) { return; }
V *new_values = (V*)malloc(sizeof(V) * size); if (!new_values) { free(new_keys); return; }
if (keys && values) {
memcpy(new_keys, keys, sizeof(H) * _count);
memcpy(new_values, values, sizeof(V) * _count);
free(keys); free(values);
}
keys = new_keys;
values = new_values;
_capacity = size;
}
}
bool insert(uint32_t pos) {
if (pos>_count)
return false;
if (_count==_capacity)
reserve(_capacity+64);
if (pos<_count) {
memmove(keys+pos+1, keys+pos, sizeof(H) * (_count-pos));
memmove(values+pos+1, values+pos, sizeof(V) * (_count-pos));
}
memset(keys+pos, 0, sizeof(H));
memset(values+pos, 0, sizeof(V));
_count++;
return true;
}
bool insert(uint32_t pos, H key) {
if (insert(pos) && keys) {
keys[pos] = key;
return true;
}
return false;
}
void remove(uint32_t pos) {
if (pos<_count) {
_count--;
if (pos<_count) {
memmove(keys+pos, keys+pos+1, sizeof(H) * (_count-pos));
memmove(values+pos, values+pos+1, sizeof(V) * (_count-pos));
}
}
}
H* getKeys() { return keys; }
H& getKey(uint32_t pos) { return keys[pos]; }
V* getValues() { return values; }
V& getValue(uint32_t pos) { return values[pos]; }
uint32_t count() const { return _count; }
uint32_t capacity() const { return _capacity; }
void clear() {
if (keys!=nullptr)
free(keys);
keys = nullptr;
if (values!=nullptr)
free(values);
values = nullptr;
_capacity = 0;
_count = 0;
}
};
template< class KeyType, class ValueType, class CountType = size_t > struct HashTable {
CountType size, maxSteps, used;
KeyType* keys;
ValueType* values;
static CountType HashFunction(KeyType v) { return CountType(((v + (v >> 27) + (v << 29)) + 14695981039346656037UL) * 1099511628211UL); }
static CountType HashIndex(KeyType hash, CountType tableSize) { return hash & (tableSize - 1); }
static CountType GetNextIndex(KeyType hash, CountType tableSize) { return (hash + 1) & (tableSize - 1); }
static CountType KeyToIndex(KeyType key, CountType tableSize) { return HashIndex(HashFunction(key), tableSize); }
static CountType FindKeyIndex(KeyType hash, CountType hashTableSize, KeyType* hashKeys, CountType maxKeySteps) {
CountType index = KeyToIndex(hash, hashTableSize);
while (hashKeys) {
KeyType key = hashKeys[index];
if (!key || key == hash) { return index; }
index = GetNextIndex(index, hashTableSize);
if (!maxKeySteps--) { break; }
}
return index;
}
CountType KeyToIndex(KeyType key) { return KeyToIndex(key, size); }
CountType InsertKey(KeyType key, CountType index) {
const KeyType* hashKeys = keys;
CountType currSize = size;
CountType insertSteps = 0;
while (KeyType k = hashKeys[index]) {
if (k == key) { return index; } // key already exists
CountType kfirst = KeyToIndex(k, currSize);
CountType ksteps = kfirst > index ? (currSize + index - kfirst) : (index - kfirst);
if (insertSteps > ksteps) { return index; }
index = GetNextIndex(index, size);
++insertSteps;
}
return index;
}
CountType FindKeyIndex(KeyType hash) const { return FindKeyIndex(hash, size, keys, maxSteps); }
CountType Steps(KeyType hash) {
CountType slot = KeyToIndex(hash, size);
CountType numSteps = 0;
while (keys[slot] && keys[slot] != hash) {
++numSteps;
slot = GetNextIndex(slot, size);
}
return numSteps;
}
void UpdateSteps(CountType first, CountType slot) {
CountType steps = slot > first ? (slot - first) : (size + slot - first);
if (steps > maxSteps) { maxSteps = steps; }
}
ValueType* InsertFitted(KeyType key) {
assert(key); // key may not be 0
CountType first = KeyToIndex(key);
CountType slot = InsertKey(key, first);
UpdateSteps(first, slot);
if (keys[slot]) {
if (keys[slot] == key) { return &values[slot]; } else {
KeyType prvKey = keys[slot];
ValueType prev_value = values[slot];
keys[slot] = key;
for (;; ) {
CountType prev_first = KeyToIndex(prvKey);
CountType slotRH = InsertKey(prvKey, prev_first);
UpdateSteps(prev_first, slotRH);
if (keys[slotRH] && keys[slotRH] != prvKey) {
KeyType tmpKey = keys[slotRH];
keys[slotRH] = prvKey;
prvKey = tmpKey;
ValueType temp_value = values[slotRH];
values[slotRH] = prev_value;
prev_value = temp_value;
} else {
keys[slotRH] = prvKey;
values[slotRH] = prev_value;
++used;
return &values[slot];
}
}
}
}
keys[slot] = key;
++used;
return &values[slot];
}
HashTable() { Reset(); }
void Reset() {
used = 0;
size = 0;
maxSteps = 0;
keys = nullptr;
values = nullptr;
}
~HashTable() { Clear(); }
void Clear() {
if (values) {
for (CountType i = 0, n = size; i < n; ++i) {
values[i].~ValueType();
}
free(values);
}
if (keys) { free(keys); }
Reset();
}
CountType GetUsed() const { return used; }
bool TableMax() const { return used && (used << 4) >= (size * 13); }
void Grow() {
KeyType *prevKeys = keys;
ValueType *prevValues = values;
CountType prevSize = size, newSize = prevSize ? (prevSize << 1) : 64;
size = newSize;
keys = (KeyType*)calloc(1, newSize * sizeof(KeyType));
values = (ValueType*)calloc(1, newSize * sizeof(ValueType));
maxSteps = 0;
for (CountType i = 0; i < newSize; ++i) { new (values + i) ValueType; }
if (used) {
used = 0;
for (CountType i = 0; i < prevSize; i++) {
if (KeyType key = prevKeys[i]) { *InsertFitted(key) = prevValues[i]; }
}
}
if (prevKeys) { free(prevKeys); }
if (prevValues) {
for (CountType i = 0; i != prevSize; ++i) { prevValues[i].~ValueType(); }
free(prevValues);
}
}
ValueType* InsertKey(KeyType key)
{
if (!size || TableMax()) { Grow(); }
return InsertFitted(key);
}
ValueType* InsertKeyValue(KeyType key, ValueType& value)
{
ValueType* value_ptr = InsertKey(key);
*value_ptr = value;
return value_ptr;
}
bool KeyExists(KeyType key)
{
return size && key && keys[FindKeyIndex(key)] == key;
}
ValueType* GetValue(KeyType key)
{
if (size && key) {
CountType slot = FindKeyIndex(key);
if (keys[slot] == key) {
return &values[slot];
}
}
return nullptr;
}
};
// relocs are cheaper than full expressions and work with
// local labels for relative sections which would otherwise
// be out of scope at link time.
struct Reloc {
int base_value;
int section_offset; // offset into this section
int target_section; // which section does this reloc target?
int8_t bytes; // number of bytes to write
int8_t shift; // number of bits to shift to get value
Reloc() : base_value(0), section_offset(-1), target_section(-1), bytes(0), shift(0) {}
Reloc(int base, int offs, int sect, int8_t num_bytes, int8_t bit_shift) :
base_value(base), section_offset(offs), target_section(sect), bytes(num_bytes), shift(bit_shift) {}
};
typedef std::vector<struct Reloc> relocList;
// For assembly listing this remembers the location of each line
struct ListLine {
enum Flags {
MNEMONIC = 0x01,
KEYWORD = 0x02,
CYCLES_START = 0x04,
CYCLES_STOP = 0x08,
};
strref source_name; // source file index name
strref code; // line of code this represents
int column; // column of line
int address; // start address of this line
int size; // number of bytes generated for this line
int line_offs; // offset into code
int flags; // only output code if generated by code
bool wasMnemonic() const { return !!(flags & MNEMONIC); }
bool startClock() const { return !!(flags & CYCLES_START); }
bool stopClock() const { return !!(flags & CYCLES_STOP); }
};
typedef std::vector<struct ListLine> Listing;
// Source level debugging info that can be saved into linkable object files, this is close to ListLine so possibly combinable.
// this belongs in each section so it can be saved with that into the x65 files or generated if linked
struct SourceDebugEntry {
int source_file_index; // index into Assembler::source_file vector
int address; // local address in section
int source_file_offset; // can be converted into line/column while linking
int size:24;
int type : 8;
};
enum class SourceDebugType {
Code,
Label,
Breakpoint
};
typedef std::vector<struct SourceDebugEntry> SourceDebug;
enum SectionType : int8_t { // enum order indicates fixed address linking priority
ST_UNDEFINED, // not set
ST_CODE, // default type
ST_DATA, // data section (matters for GS/OS OMF)
ST_BSS, // uninitialized data section
ST_ZEROPAGE, // uninitialized data section in zero page / direct page
ST_REMOVED // removed, don't export to object file
};
// String data
typedef struct sStringSymbols {
public:
strref string_name; // name of the string
strref string_const; // string contents if source reference
strovl string_value; // string contents if modified, initialized to null string
StatusCode Append(strref append);
strref get() { return string_value.valid() ? string_value.get_strref() : string_const; }
void clear() {
if (string_value.cap()) {
free(string_value.charstr());
string_value.invalidate();
string_value.clear();
}
string_const.clear();
}
} StringSymbol;
// start of data section support
// Default is a relative section
// Whenever org or dum with address is encountered => new section
// If org is fixed and < $200 then it is a dummy section Otherwise clear dummy section
typedef struct Section {
// section name, same named section => append
strref name; // name of section for comparison
strref export_append; // append this name to export of file
strref include_from; // which file did this section originate from?
// generated address status
int load_address; // if assigned a load address
int start_address;
int address; // relative or absolute PC
int align_address; // for relative sections that needs alignment
// merged sections
int merged_at; // merged into a section at this offset
int merged_into; // -1 if not merged otherwise section merged into
int merged_size; // how many bytes were merged in
// data output
uint8_t *output; // memory for this section
uint8_t *curr; // current pointer for this section
size_t output_capacity; // current output capacity
// reloc data
relocList *pRelocs; // link time resolve (not all sections need this)
Listing *pListing; // if list output
SourceDebug *pSrcDbg; // if source level debugging info generated
// grouped sections
int next_group; // next section of a group of relative sections or -1
int first_group; // >=0 if another section is grouped with this section
bool address_assigned; // address is absolute if assigned
bool dummySection; // true if section does not generate data, only labels
SectionType type; // distinguishing section type for relocatable output
void reset() { // explicitly cleaning up sections, not called from Section destructor
name.clear(); export_append.clear(); include_from.clear();
start_address = address = load_address = 0x0; type = ST_CODE;
address_assigned = false; output = nullptr; curr = nullptr;
dummySection = false; output_capacity = 0;
merged_at = -1; merged_into = -1; merged_size = 0;
align_address = 1; if (pRelocs) delete pRelocs;
next_group = first_group = -1;
pRelocs = nullptr;
if (pListing) delete pListing;
pListing = nullptr;
if (pSrcDbg) delete pSrcDbg;
pSrcDbg = nullptr;
}
void Cleanup() { if (output) free(output); reset(); }
bool empty() const { return type != ST_REMOVED && curr==output; }
bool unused() const { return !address_assigned && address == start_address; }
int DataOffset() const { return int(curr - output); }
int size() const { return (int)(curr - output); }
int addr_size() const { return address - start_address; }
const uint8_t *get() { return output; }
int GetPC() const { return address; }
void AddAddress(int value) { address += value; }
void SetLoadAddress(int addr) { load_address = addr; }
int GetLoadAddress() const { return load_address; }
void SetDummySection(bool enable) { dummySection = enable; type = ST_BSS; }
bool IsDummySection() const { return dummySection; }
bool IsRelativeSection() const { return address_assigned == false; }
bool IsMergedSection() const { return false; }
void AddReloc(int base, int offset, int section, int8_t bytes, int8_t shift);
Section() : pRelocs(nullptr), pListing(nullptr) { reset(); }
Section(strref _name, int _address) : pRelocs(nullptr), pListing(nullptr), pSrcDbg(nullptr) {
reset(); name = _name; start_address = load_address = address = _address;
address_assigned = true;
}
Section(strref _name) : pRelocs(nullptr), pListing(nullptr), pSrcDbg(nullptr) {
reset(); name = _name;
start_address = load_address = address = 0; address_assigned = false;
}
~Section() { }
// Append data to a section
StatusCode CheckOutputCapacity(uint32_t addSize);
void AddByte(int b);
void AddWord(int w);
void AddTriple(int l);
void AddBin(const uint8_t *p, int size);
void AddText(strref line, strref text_prefix);
void AddIndexText(StringSymbol * strSym, strref text);
void SetByte(size_t offs, int b) { output[offs] = (uint8_t)b; }
void SetWord(size_t offs, int w) { output[offs] = (uint8_t)w; output[offs+1] = uint8_t(w>>8); }
void SetTriple(size_t offs, int w) { output[offs] = (uint8_t)w; output[offs+1] = uint8_t(w>>8); output[offs+2] = uint8_t(w>>16); }
void SetQuad(size_t offs, int w) { output[offs] = (uint8_t)w; output[offs+1] = uint8_t(w>>8); output[offs+2] = uint8_t(w>>16); output[offs+3] = uint8_t(w>>24); }
} Section;
// Symbol list entry (in order of parsing)
struct MapSymbol {
strref name; // string name
int value;
int16_t section;
int16_t orig_section;
bool local; // local variables
};
typedef std::vector<struct MapSymbol> MapSymbolArray;
// Data related to a label
typedef struct sLabel {
public:
strref label_name; // the name of this label
strref pool_name; // name of the pool that this label is related to
int value;
int section; // rel section address labels belong to a section, -1 if fixed address or assigned
int orig_section; // original section where the label was defined
int mapIndex; // index into map symbols in case of late resolve
bool evaluated; // a value may not yet be evaluated
bool pc_relative; // this is an inline label describing a point in the code
bool constant; // the value of this label can not change
bool external; // this label is globally accessible
bool reference; // this label is accessed from external and can't be used for evaluation locally
bool referenced; // this label has been found via GetLabel and can be assumed to be referenced for some purpose
} Label;
// If an expression can't be evaluated immediately, this is required
// to reconstruct the result when it can be.
typedef struct sLateEval {
enum Type { // When an expression is evaluated late, determine how to encode the result
LET_LABEL, // this evaluation applies to a label and not memory
LET_ABS_REF, // calculate an absolute address and store at 0, +1
LET_ABS_L_REF, // calculate a bank + absolute address and store at 0, +1, +2
LET_ABS_4_REF, // calculate a 32 bit number
LET_BRANCH, // calculate a branch offset and store at this address
LET_BRANCH_16, // calculate a branch offset of 16 bits and store at this address
LET_BYTE, // calculate a byte and store at this address
LET_DBL_BYTE, // calculate a 16-bit, big endian number.
};
int target; // offset into output buffer
int address; // current pc
int scope; // scope pc
int scope_depth; // relevant for scope end
int16_t section; // which section to apply to.
int16_t rept; // value of rept
int file_ref; // -1 if current or xdef'd otherwise index of file for label
char* expression_mem; // if a temporary string was used for the expression
strref label; // valid if this is not a target but another label
strref expression;
strref source_file;
Type type;
} LateEval;
// A macro is a text reference to where it was defined
typedef struct sMacro {
strref name;
strref macro;
strref source_name; // source file name (error output)
strref source_file; // entire source file (req. for line #)
bool params_first_line; // the first line of this macro are parameters
} Macro;
// All local labels are removed when a global label is defined but some when a scope ends
typedef struct sLocalLabelRecord {
strref label;
int scope_depth;
bool scope_reserve; // not released for global label, only scope
} LocalLabelRecord;
// Label pools allows C like stack frame label allocation
typedef struct sLabelPool {
strref pool_name;
int16_t numRanges; // normally 1 range, support multiple for ease of use
int16_t depth; // Required for scope closure cleanup
uint32_t start;
uint32_t end;
uint32_t scopeUsed[MAX_SCOPE_DEPTH][2]; // last address assigned + scope depth
StatusCode Reserve(uint32_t numBytes, uint32_t &ret_addr, uint16_t scope);
void ExitScope(uint16_t scope);
} LabelPool;
// One member of a label struct
struct MemberOffset {
uint16_t offset;
uint32_t name_hash;
strref name;
strref sub_struct;
};
// Label struct
typedef struct sLabelStruct {
strref name;
uint16_t first_member;
uint16_t numMembers;
uint16_t size;
} LabelStruct;
// object file labels that are not xdef'd end up here
struct ExtLabels {
pairArray<uint32_t, Label> labels;
};
// EvalExpression needs a location reference to work out some addresses
struct EvalContext {
int pc; // current address at point of eval
int scope_pc; // current scope open at point of eval
int scope_end_pc; // late scope closure after eval
int scope_depth; // scope depth for eval (must match current for scope_end_pc to eval)
int relative_section; // return can be relative to this section
int file_ref; // can access private label from this file or -1
int rept_cnt; // current repeat counter
int recursion; // track recursion depth
StatusCode internalErr; // if an error occured during an internal stage of evaluation
EvalContext() : pc(0), scope_pc(0), scope_end_pc(0), scope_depth(0), relative_section(-1),
file_ref(-1), rept_cnt(0), recursion(0), internalErr(STATUS_OK) {}
EvalContext(int _pc, int _scope, int _close, int _sect, int _rept_cnt) :
pc(_pc), scope_pc(_scope), scope_end_pc(_close), scope_depth(-1),
relative_section(_sect), file_ref(-1), rept_cnt(_rept_cnt),
recursion(0), internalErr(STATUS_OK) {}
};
// Source context is current file (include file, etc.) or current macro.
typedef struct sSourceContext {
strref source_name; // source file name (error output)
strref source_file; // entire source file (req. for line #)
strref code_segment; // the segment of the file for this context
strref read_source; // current position/length in source file
strref next_source; // next position/length in source file
int16_t repeat; // how many times to repeat this code segment
int16_t repeat_total; // initial number of repeats for this code segment
int16_t conditional_ctx; // conditional depth at root of this context
void restart() { read_source = code_segment; }
bool complete() { repeat--; return repeat <= 0; }
} SourceContext;
// Context stack is a stack of currently processing text
class ContextStack {
private:
std::vector<SourceContext> stack; // stack of contexts
SourceContext *currContext; // current context
public:
ContextStack() : currContext(nullptr) { stack.reserve(32); }
SourceContext& curr() { return *currContext; }
const SourceContext& curr() const { return *currContext; }
void push(strref src_name, strref src_file, strref code_seg, int rept = 1) {
if (currContext)
currContext->read_source = currContext->next_source;
SourceContext context;
context.source_name = src_name;
context.source_file = src_file;
context.code_segment = code_seg;
context.read_source = code_seg;
context.next_source = code_seg;
context.repeat = (int16_t)rept;
context.repeat_total = (int16_t)rept;
stack.push_back(context);
currContext = &stack[stack.size()-1];
}
void pop() { stack.pop_back(); currContext = stack.size() ? &stack[stack.size()-1] : nullptr; }
bool has_work() { return currContext!=nullptr; }
bool empty() const { return stack.size() == 0; }
};
// Support for the PULL and PUSH directives
typedef union { int value; char* string; } ValueOrString;
typedef std::vector < ValueOrString > SymbolStack;
class SymbolStackTable : public HashTable< uint64_t, SymbolStack* > {
public:
void PushSymbol(Label* symbol);
StatusCode PullSymbol(Label* symbol);
void PushSymbol(StringSymbol* string);
StatusCode PullSymbol(StringSymbol* string);
~SymbolStackTable();
};
// user declared functions
struct UserFunction {
const char* name;
const char* params;
const char* expression;
};
class UserFunctionMap : public HashTable<uint64_t, UserFunction*> {
public:
UserFunction *Get(strref name);
StatusCode Add(strref name, strref params, strref expresion);
~UserFunctionMap();
};
// The state of the assembler
class Asm {
public:
pairArray<uint32_t, Label> labels;
pairArray<uint32_t, StringSymbol> strings;
pairArray<uint32_t, Macro> macros;
pairArray<uint32_t, LabelPool> labelPools;
pairArray<uint32_t, LabelStruct> labelStructs;
pairArray<uint32_t, strref> xdefs; // labels matching xdef names will be marked as external
std::vector<char*> source_files; // all source files encountered while assembling. referenced by source level debugging
std::vector<LateEval> lateEval;
std::vector<LocalLabelRecord> localLabels;
std::vector<char*> loadedData; // free when assembler is completed
std::vector<MemberOffset> structMembers; // labelStructs refer to sets of structMembers
std::vector<strref> includePaths;
std::vector<Section> allSections;
std::vector<ExtLabels> externals; // external labels organized by object file
MapSymbolArray map;
SymbolStackTable symbolStacks; // enable push/pull of symbols
UserFunctionMap userFunctions; // user defined expression functions
// CPU target
struct mnem *opcode_table;
int opcode_count;
CPUIndex cpu, list_cpu;
OPLookup aInstructions[MAX_OPCODES_DIRECTIVES];
int num_instructions;
int default_org;
// context for macros / include files
ContextStack contextStack;
// Current section
Section *current_section;
// Special syntax rules
AsmSyntax syntax;
// Conditional assembly vars
int conditional_depth; // conditional depth / base depth for context
strref conditional_source[MAX_CONDITIONAL_DEPTH]; // start of conditional for error report
int8_t conditional_nesting[MAX_CONDITIONAL_DEPTH];
bool conditional_consumed[MAX_CONDITIONAL_DEPTH];
// Scope info
int scope_address[MAX_SCOPE_DEPTH];
int scope_depth;
int brace_depth; // scope depth defined only by braces, not files
strref export_base_name; // binary output name if available
strref last_label; // most recently defined label for Merlin macro
// ca65 style scope (for now treat global symbols as local symbols, no outside name lookup)
int directive_scope_depth;
// Eval relative result (only valid if EvalExpression returns STATUS_RELATIVE_SECTION)
int lastEvalSection;
int lastEvalValue;
int8_t lastEvalShift;
int8_t list_flags; // listing flags accumulating for each line
bool accumulator_16bit; // 65816 specific software dependent immediate mode
bool index_reg_16bit; // -"-
int8_t cycle_counter_level; // merlin toggles the cycle counter rather than hierarchically evals
bool error_encountered; // if any error encountered, don't export binary
bool list_assembly; // generate assembler listing
bool src_debug; // generate source debug info
bool end_macro_directive; // whether to use { } or macro / endmacro for macro scope
bool import_means_xref;
// Convert source to binary
void Assemble(strref source, strref filename, bool obj_target);
// Push a new context and handle enter / exit of context
StatusCode PushContext(strref src_name, strref src_file, strref code_seg, int rept = 1);
StatusCode PopContext();
// Generate assembler listing if requested
bool List(strref filename);
// Mimic TASS listing
bool ListTassStyle( strref filename );
// Export C64Debugger dbg xml file
bool SourceDebugExport(strref filename);
// Generate source for all valid instructions and addressing modes for current CPU
bool AllOpcodes(strref filename);
// Clean up memory allocations, reset assembler state
void Cleanup();
// Make sure there is room to write more code
StatusCode CheckOutputCapacity(uint32_t addSize);
// Operations on current section
void SetSection(strref name, int address); // fixed address section
void SetSection(strref name); // relative address section
void LinkLabelsToAddress(int section_id, int section_new, int section_address);
StatusCode LinkRelocs(int section_id, int section_new, int section_address);
StatusCode AssignAddressToSection(int section_id, int address);