added callback support for memory access
[dcpu16] / dcpu16.h
1 #ifndef DCPU16_H_3XXIQQG2
2 #define DCPU16_H_3XXIQQG2
3
4 /* the target system's concept of a word */
5 typedef unsigned short DCPU16_WORD;
6
7 /* how much ram the system has */
8 #define DCPU16_RAM 0x10000
9
10 /* these are used for accounting/watchpointing/&c */
11 typedef unsigned int dcpu16_acct_event_;
12 #define DCPU16_ACCT_EV_READ (1<<1)
13 #define DCPU16_ACCT_EV_WRITE (1<<2)
14 #define DCPU16_ACCT_RAM (1<<3)
15
16 struct dcpu16_acct_cb {
17 void (*fn)(dcpu16_acct_event_ e, DCPU16_WORD addr);
18 dcpu16_acct_event_ match_all;
19 dcpu16_acct_event_ match_any;
20 };
21
22 /* a self-contained dcpu16 core */
23 struct dcpu16 {
24 unsigned long long cycle; /* number of cycles this core has executed */
25 DCPU16_WORD reg[8]; /* system registers, a b c x y z i j */
26 DCPU16_WORD pc; /* program counter */
27 DCPU16_WORD sp; /* stack pointer */
28 DCPU16_WORD o; /* overflow */
29 unsigned int skip_ : 1; /* skip execution of next instruction */
30 DCPU16_WORD ram[DCPU16_RAM]; /* memory */
31 DCPU16_WORD reg_work_[2]; /* (private) work registers for holding literal values while decoding instructions */
32 struct dcpu16_acct_cb *cb_table_; /* list of callbacks to invoke under certain conditions */
33 size_t cb_table_entries_; /* callback list maintenance */
34 size_t cb_table_allocated_; /* callback list maintenance */
35 };
36
37 /* instantiate a new core */
38 struct dcpu16 *dcpu16_new(void);
39
40 /* reset a core to initial state */
41 void dcpu16_reset(struct dcpu16 *);
42
43 /* print the current state of a core */
44 void dcpu16_state_print(struct dcpu16 *);
45
46 /* print the contents of ram from second to third argument */
47 void dcpu16_dump_ram(struct dcpu16 *, DCPU16_WORD, DCPU16_WORD);
48
49 /* print the instruction at the specified address */
50 void dcpu16_disassemble_print(struct dcpu16 *, DCPU16_WORD);
51
52 /* register a callback for an accounting event */
53 int dcpu16_acct_add(struct dcpu16 *, dcpu16_acct_event_ match_all, dcpu16_acct_event_ match_any, void (*fn)(dcpu16_acct_event_, DCPU16_WORD));
54
55 /* execute the next instruction */
56 void dcpu16_step(struct dcpu16 *);
57
58 /* release a core */
59 void dcpu16_delete(struct dcpu16 **);
60
61 /* register callbacks to handle warning and debug messages, default is writing to stderr, may be set to null */
62 void dcpu16_warn_cb_set(void (*)(char *, ...));
63 void dcpu16_trace_cb_set(void (*)(char *, ...));
64
65 #endif /* DCPU16_H_3XXIQQG2 */