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 */
50 PROG_START = 0x600 /* The default entry point for the program */
54 SINGLE, IMMEDIATE_VALUE, IMMEDIATE_GREAT,
55 IMMEDIATE_LESS, INDIRECT_X, INDIRECT_Y,
57 ABS_VALUE, ABS_OR_BRANCH, ABS_X, ABS_Y,
58 ABS_LABEL_X, ABS_LABEL_Y, DCB_PARAM
61 typedef struct machine_6502 machine_6502;
64 char name[MAX_CMD_LEN];
76 void (*func) (machine_6502*, m6502_AddrMode);
79 /* Used to cache the index of each opcode */
85 /* Plotter is a function that will be called everytime a pixel
86 needs to be updated. The first two parameter are the x and y
87 values. The third parameter is the color index:
104 14 light blue #0088ff
107 The plotter state variable of the machine gets passed as the forth
108 parameter. You can use this parameter to store state information.
111 typedef void (*m6502_Plotter) (Bit8, Bit8, Bit8, void*);
113 struct machine_6502 {
119 Bit16 regPC; /* A pair of 8 bit registers */
122 Bit8 memory[MEM_64K];
127 m6502_Opcodes opcodes[NUM_OPCODES];
130 m6502_OpcodeIndex opcache[0xff];
135 /* build6502() - Creates an instance of the 6502 machine */
136 machine_6502 *m6502_build(void);
138 /* destroy6502() - compile the file and exectue it until the program
140 void m6502_destroy6502(machine_6502 *machine);
142 /* eval_file() - Compiles and runs a file until the program is
144 void m6502_eval_file(machine_6502 *machine, const char *filename,
145 m6502_Plotter plot, void *plotterState);
147 /* start_eval_file() - Compile the file and execute the first
149 void m6502_start_eval_file(machine_6502 *machine, const char *filename,
150 m6502_Plotter plot, void *plotterState);
153 void m6502_start_eval_binary(machine_6502 *machine, Bit8 *program,
154 unsigned int proglen,
155 Plotter plot, void *plotterState);
158 void m6502_start_eval_string(machine_6502 *machine, const char *code,
159 m6502_Plotter plot, void *plotterState);
161 /* next_eval() - Execute the next insno of machine instructions */
162 void m6502_next_eval(machine_6502 *machine, int insno);
164 /* hexDump() - Dumps memory to output */
165 void m6502_hexDump(machine_6502 *machine, Bit16 start,
166 Bit16 numbytes, FILE *output);
168 /* Disassemble() - Prints the assembly code for the program currently
170 void m6502_disassemble(machine_6502 *machine, FILE *output);
172 /* trace() - Prints to output the current value of registers, the
173 current nmemonic, memory address and value. */
174 void m6502_trace(machine_6502 *machine, FILE *output);
176 /* save_program() - Writes a binary file of the program loaded in
179 void save_program(machine_6502 *machine, const char *filename);
181 #endif /* __ASM6502_H__ */