2 * Copyright (c) 2007 Jeremy English <jhe@jeremyenglish.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
12 * Created: 07-May-2007
16 This is a port of the javascript 6502 assembler, compiler and
17 debugger. The orignal code was copyright 2006 by Stian Soreng -
20 The stack space is in page $100 to $1ff. The video buffer starts at
21 $200 and is 1024 bytes. Programs get loaded at address
22 $600. Address $fe is random and byte $ff is used for user input.
24 User input is disabled.
31 typedef uint16_t Bit16;
32 typedef uint32_t Bit32;
43 NUM_OPCODES = 56, /* Number of unique instructions not counting DCB */
44 MEM_64K = 65536, /* We have 64k of memory to work with. */
45 MAX_PARAM_VALUE = 25, /* The number of values allowed behind dcb */
46 MAX_CMD_LEN = 4, /* Each assembly command is 3 characeters long */
47 /* The stack works from the top down in page $100 to $1ff */
53 SINGLE, IMMEDIATE_VALUE, IMMEDIATE_GREAT,
54 IMMEDIATE_LESS, INDIRECT_X, INDIRECT_Y,
56 ABS_VALUE, ABS_OR_BRANCH, ABS_X, ABS_Y,
57 ABS_LABEL_X, ABS_LABEL_Y, DCB_PARAM
60 typedef struct machine_6502 machine_6502;
63 char name[MAX_CMD_LEN];
75 void (*func) (machine_6502*, AddrMode);
78 /* Used to cache the index of each opcode */
84 /* Plotter is a function that will be called everytime a pixel
85 needs to be updated. The first two parameter are the x and y
86 values. The third parameter is the color index:
103 14 light blue #0088ff
106 The plotter state variable of the machine gets passed as the forth
107 parameter. You can use this parameter to store state information.
110 typedef void (*Plotter) (Bit8, Bit8, Bit8, void*);
112 struct machine_6502 {
118 Bit16 regPC; /* A pair of 8 bit registers */
120 Bit8 memory[MEM_64K];
125 Opcodes opcodes[NUM_OPCODES];
128 OpcodeIndex opcache[0xff];
133 /* build6502() - Creates an instance of the 6502 machine */
134 machine_6502 *build6502(void);
136 /* destroy6502() - compile the file and exectue it until the program
138 void destroy6502(machine_6502 *machine);
140 /* eval_file() - Compiles and runs a file until the program is
142 void eval_file(machine_6502 *machine, char *filename,
143 Plotter plot, void *plotterState);
145 /* start_eval_file() - Compile the file and execute the first
147 void start_eval_file(machine_6502 *machine, char *filename,
148 Plotter plot, void *plotterState);
150 void start_eval_binary(machine_6502 *machine, Bit8 *program,
151 unsigned int proglen,
152 Plotter plot, void *plotterState);
154 /* next_eval() - Execute the next insno of machine instructions */
155 void next_eval(machine_6502 *machine, int insno);
157 /* hexDump() - Dumps memory to output */
158 void hexDump(machine_6502 *machine, Bit16 start,
159 Bit16 numbytes, FILE *output);
161 /* trace() - Prints to output the current value of registers, the
162 current nmemonic, memory address and value. */
163 void trace(machine_6502 *machine, FILE *output);
165 /* save_program() - Writes a binary file of the program loaded in
167 void save_program(machine_6502 *machine, char *filename);
169 #endif /* __ASM6502_H__ */