-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe652.h
More file actions
107 lines (85 loc) · 2.78 KB
/
e652.h
File metadata and controls
107 lines (85 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* E652 -- A 6502 emulator.
*
* Copyright (c) 2026 Vincent Yanzee J. Tan
* This project is licensed under the MIT License.
* See LICENSE for more info.
*/
#ifndef E652
#define E652 1
#include <stdint.h>
typedef uint8_t byte;
typedef uint16_t word;
/*
* e652 emulation context.
*/
struct e652 {
byte *m; /* 64KB */
word PC; /* Program counter. */
byte A; /* Accumulator register. */
byte X, Y; /* Index (general) regs. */
byte P; /* Status register. */
byte S; /* Stack pointer. */
};
extern struct e652 E;
#define EN (1<<7) /* Negative */
#define EV (1<<6) /* Overflow */
#define EB (1<<4) /* Break */
#define ED (1<<3) /* Decimal mode */
#define EI (1<<2) /* Interrupt disable */
#define EZ (1<<1) /* Zero */
#define EC (1<<0) /* Carry */
#define Pset(f,c) (E.P = (E.P & ~(f)) | ((c) ? (f) : 0)) /* Assigns a flag */
#define Phas(f) ((E.P & (f)) != 0) /* Is f set? */
#define MLEN (1<<16) /* Length of memory */
#define MMAX (MLEN-1) /* Max possible address (can be used as a wrap mask) */
#define ZPAGE (0x0000) /* Zero page */
#define SPAGE (0x0100) /* Stack page */
#define VPAGE (0xff00) /* Vector page */
#define V_NMI (VPAGE + 0xfa) /* Non maskable interrupt */
#define V_RES (VPAGE + 0xfc) /* Reset vector */
#define V_IRQ (VPAGE + 0xfe) /* Interrupt request */
#define b1(addr) (E.m[(addr) & MMAX]) /* 1B write + wrap-around */
#define b2(addr) (b1((addr)) | b1((addr)+1) << 8) /* 2B write + wrap-around */
#define push(v) (e652_write(SPAGE + E.S--, (v))) /* Stack push */
#define pop() (e652_read(SPAGE + ++E.S)) /* Stack pop */
/* addressing modes (bbb) for cc == 01 */
#define A_INDX (0) /* (indirect,X) */
#define A_ZPG (1) /* zeropage */
#define A_IMM (2) /* immediate */
#define A_ABS (3) /* absolute */
#define A_INDY (4) /* (indirect),Y */
#define A_ZPGX (5) /* zeropage,X */
#define A_ABSY (6) /* absolute,Y */
#define A_ABSX (7) /* absolute,X */
/* other addressing modes (outside cc == 01) */
#define A_REL (8) /* pc-relative */
#define A_IND (9) /* indirect */
#define A_NONE (10) /* none/implied */
#define A_ACC (11) /* accumulator */
#define A_ZPGY (12) /* zeropage,Y */
/*
* Initialize a 6502 emulation context.
*/
void e652_reset (void);
/*
* Get the effective memory address. Returns a mem address between 0 to 65536.
* Returns -1 on error. Works only for opcodes with cc == 01.
*/
int e652_effaddr01 (byte opcode);
/*
* Read byte from memory.
*/
byte e652_read (word addr);
/*
* Write byte onto memory.
*/
void e652_write (word addr, byte val);
/*
* Execute next instruction. Returns the current state.
*/
int e652_execnext (void);
#define H_OK (0) /* Ok */
#define H_DBUG (1) /* Debug */
#define H_ILL (2) /* Illegal instruction */
#endif