-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathemu8051.h
262 lines (230 loc) · 7.75 KB
/
emu8051.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* 8051 emulator core
* Copyright 2006 Jari Komppa
*
* 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.
*
* (i.e. the MIT License)
*
* emu8051.h
* Emulator core header file
*/
#include <stdint.h>
#include <stdbool.h>
struct em8051;
// Operation: returns number of ticks the operation should take
typedef uint8_t (*em8051operation)(struct em8051 *aCPU);
// Decodes opcode at position, and fills the buffer with the assembler code.
// Returns how many bytes the opcode takes.
typedef uint8_t (*em8051decoder)(struct em8051 *aCPU, uint16_t aPosition, char *aBuffer);
// Callback: some exceptional situation occurred. See EM8051_EXCEPTION enum, below
typedef void (*em8051exception)(struct em8051 *aCPU, int aCode);
// Callback: an SFR register is about to be read (not called for 'a' ops nor psw changes)
// Default is to return the value in the SFR register. Ports may act differently.
typedef uint8_t (*em8051sfrread)(struct em8051 *aCPU, uint8_t aRegister);
// Callback: an SFR register has changed (not called for 'a' ops)
// Default is to do nothing
typedef void (*em8051sfrwrite)(struct em8051 *aCPU, uint8_t aRegister);
// Callback: writing to external memory
// Default is to update external memory
// (can be used to control some peripherals)
typedef void (*em8051xwrite)(struct em8051 *aCPU, uint16_t aAddress, uint8_t aValue);
// Callback: reading from external memory
// Default is to return the value in external memory
// (can be used to control some peripherals)
typedef uint8_t (*em8051xread)(struct em8051 *aCPU, uint16_t aAddress);
struct em8051
{
unsigned char *mCodeMem; // 1k - 64k, must be power of 2
uint16_t mCodeMemMaxIdx;
unsigned char *mExtData; // 0 - 64k, must be power of 2
uint16_t mExtDataMaxIdx;
unsigned char mLowerData[128]; // 128 bytes
unsigned char *mUpperData; // 0 or 128 bytes; leave to NULL if none
unsigned char mSFR[128]; // 128 bytes; (special function registers)
uint16_t mPC; // Program Counter; outside memory area
uint8_t mTickDelay; // How many ticks should we delay before continuing
em8051operation op[256]; // function pointers to opcode handlers
em8051decoder dec[256]; // opcode-to-string decoder handlers
em8051exception except; // callback: exceptional situation occurred
em8051sfrread sfrread[128]; // callback array: SFR register being read
em8051sfrwrite sfrwrite[128]; // callback array: SFR register written
em8051xread xread; // callback: external memory being read
em8051xwrite xwrite; // callback: external memory being written
// Internal values for interrupt services etc.
uint8_t mInterruptActive;
// Stored register values for interrupts (exception checking)
uint8_t int_a[2];
uint8_t int_psw[2];
uint8_t int_sp[2];
// Internal handling of UART
char serial_out[18]; // The shown size is only 18 chars
uint8_t serial_out_idx;
uint8_t serial_out_remaining_bits;
bool serial_interrupt_trigger;
};
// set the emulator into reset state. Must be called before tick(), as
// it also initializes the function pointers. aWipe tells whether to reset
// all memory to zero.
void reset(struct em8051 *aCPU, bool aWipe);
// run one emulator tick, or 12 hardware clock cycles.
// returns "true" if a new operation was executed.
bool tick(struct em8051 *aCPU);
// decode the next operation as character string.
// buffer must be big enough (64 bytes is very safe).
// Returns length of opcode.
uint8_t decode(struct em8051 *aCPU, uint16_t aPosition, char *aBuffer);
// Load an intel hex format object file. Returns negative for errors.
int load_obj(struct em8051 *aCPU, char *aFilename);
// Alternate way to execute an opcode (switch-structure instead of function pointers)
uint8_t do_op(struct em8051 *aCPU);
// Internal: Pushes a value into stack
void push_to_stack(struct em8051 *aCPU, uint8_t aValue);
// SFR register locations
enum SFR_REGS
{
REG_ACC = 0xE0 - 0x80,
REG_B = 0xF0 - 0x80,
REG_PSW = 0xD0 - 0x80,
REG_SP = 0x81 - 0x80,
REG_DPL = 0x82 - 0x80,
REG_DPH = 0x83 - 0x80,
REG_P0 = 0x80 - 0x80,
REG_P1 = 0x90 - 0x80,
REG_P2 = 0xA0 - 0x80,
REG_P3 = 0xB0 - 0x80,
REG_IP = 0xB8 - 0x80,
REG_IE = 0xA8 - 0x80,
REG_TMOD = 0x89 - 0x80,
REG_TCON = 0x88 - 0x80,
REG_TH0 = 0x8C - 0x80,
REG_TL0 = 0x8A - 0x80,
REG_TH1 = 0x8D - 0x80,
REG_TL1 = 0x8B - 0x80,
REG_SCON = 0x98 - 0x80,
REG_SBUF = 0x99 - 0x80,
REG_PCON = 0x87 - 0x80
};
enum PSW_BITS
{
PSW_P = 0,
PSW_UNUSED = 1,
PSW_OV = 2,
PSW_RS0 = 3,
PSW_RS1 = 4,
PSW_F0 = 5,
PSW_AC = 6,
PSW_C = 7
};
enum PSW_MASKS
{
PSWMASK_P = 0x01,
PSWMASK_UNUSED = 0x02,
PSWMASK_OV = 0x04,
PSWMASK_RS0 = 0x08,
PSWMASK_RS1 = 0x10,
PSWMASK_F0 = 0x20,
PSWMASK_AC = 0x40,
PSWMASK_C = 0x80
};
enum IE_MASKS
{
IEMASK_EX0 = 0x01,
IEMASK_ET0 = 0x02,
IEMASK_EX1 = 0x04,
IEMASK_ET1 = 0x08,
IEMASK_ES = 0x10,
IEMASK_ET2 = 0x20,
IEMASK_UNUSED = 0x40,
IEMASK_EA = 0x80
};
enum PT_MASKS
{
PTMASK_PX0 = 0x01,
PTMASK_PT0 = 0x02,
PTMASK_PX1 = 0x04,
PTMASK_PT1 = 0x08,
PTMASK_PS = 0x10,
PTMASK_PT2 = 0x20,
PTMASK_UNUSED1 = 0x40,
PTMASK_UNUSED2 = 0x80
};
enum TCON_MASKS
{
TCONMASK_IT0 = 0x01,
TCONMASK_IE0 = 0x02,
TCONMASK_IT1 = 0x04,
TCONMASK_IE1 = 0x08,
TCONMASK_TR0 = 0x10,
TCONMASK_TF0 = 0x20,
TCONMASK_TR1 = 0x40,
TCONMASK_TF1 = 0x80
};
enum TMOD_MASKS
{
TMODMASK_M0_0 = 0x01,
TMODMASK_M1_0 = 0x02,
TMODMASK_CT_0 = 0x04,
TMODMASK_GATE_0 = 0x08,
TMODMASK_M0_1 = 0x10,
TMODMASK_M1_1 = 0x20,
TMODMASK_CT_1 = 0x40,
TMODMASK_GATE_1 = 0x80
};
enum IP_MASKS
{
IPMASK_PX0 = 0x01,
IPMASK_PT0 = 0x02,
IPMASK_PX1 = 0x04,
IPMASK_PT1 = 0x08,
IPMASK_PS = 0x10,
IPMASK_PT2 = 0x20
};
enum SCON_MASKS
{
SCONMASK_RI = 0x01,
SCONMASK_TI = 0x02,
SCONMASK_RB8 = 0x04,
SCONMASK_TB8 = 0x08,
SCONMASK_REN = 0x10,
SCONMASK_SM2 = 0x20,
SCONMASK_SM1 = 0x40,
SCONMASK_SM0 = 0x80,
};
enum ISR_VECTORS
{
ISR_RST = 0x00,
ISR_INT0 = 0x03,
ISR_TF0 = 0x0B,
ISR_INT1 = 0x13,
ISR_TF1 = 0x1B,
ISR_SR = 0x23,
#ifdef __8052__
ISR_TF2 = 0x2B,
#endif // __8052__
};
enum EM8051_EXCEPTION
{
EXCEPTION_STACK, // stack address > 127 with no upper memory, or roll over
EXCEPTION_ACC_TO_A, // acc-to-a move operation; illegal (acc-to-acc is ok, a-to-acc is ok..)
EXCEPTION_IRET_PSW_MISMATCH, // psw not preserved over interrupt call (doesn't care about P, F0 or UNUSED)
EXCEPTION_IRET_SP_MISMATCH, // sp not preserved over interrupt call
EXCEPTION_IRET_ACC_MISMATCH, // acc not preserved over interrupt call
EXCEPTION_ILLEGAL_OPCODE // for the single 'reserved' opcode in the architecture
};